source: trunk/libtransmission/upnp.c @ 3731

Last change on this file since 3731 was 3731, checked in by charles, 15 years ago

use Thomas Bernard's miniupnp library instead of rolling our own.

  • Property svn:keywords set to Date Rev Author Id
File size: 3.3 KB
Line 
1/*
2 * This file Copyright (C) 2007 Charles Kerr <charles@rebelbase.com>
3 *
4 * This file is licensed by the GPL version 2.  Works owned by the
5 * Transmission project are granted a special exemption to clause 2(b)
6 * so that the bulk of its code can remain under the MIT license.
7 * This exemption does not extend to derived works not owned by
8 * the Transmission project.
9 *
10 * $Id:$
11 */
12
13#include <stdio.h> /* printf */
14
15#include <miniupnp/miniwget.h>
16#include <miniupnp/miniupnpc.h>
17#include <miniupnp/upnpcommands.h>
18
19#include "transmission.h"
20#include "internal.h"
21#include "utils.h"
22#include "upnp.h"
23
24struct tr_upnp
25{
26    struct UPNPUrls urls;
27    struct IGDdatas data;
28    int port;
29    char lanaddr[16];
30    unsigned int isForwarding : 1;
31    unsigned int isEnabled : 1;
32};
33
34/**
35***
36**/
37
38tr_upnp*
39tr_upnpInit( void )
40{
41    tr_upnp * ret = tr_new0( tr_upnp, 1 );
42    struct UPNPDev * devlist = upnpDiscover( 2000, NULL );
43    if( UPNP_GetValidIGD( devlist, &ret->urls, &ret->data, ret->lanaddr, sizeof(ret->lanaddr))) {
44        tr_dbg( "Found Internet Gateway Device '%s'", ret->urls.controlURL );
45        tr_dbg( "Local LAN IP Address is '%s'", ret->lanaddr );
46    }
47    ret->port = -1;
48    freeUPNPDevlist( devlist );
49    return ret;
50}
51
52void
53tr_upnpClose( tr_upnp * handle )
54{
55    tr_upnpStop( handle );
56    FreeUPNPUrls( &handle->urls );
57    tr_free( handle );
58}
59
60/**
61***
62**/
63
64void
65tr_upnpStart( tr_upnp * handle )
66{
67    handle->isEnabled = 1;
68
69    if( handle->port >= 0 )
70    {
71        char portStr[16];
72        snprintf( portStr, sizeof(portStr), "%d", handle->port );
73        handle->isForwarding = UPNP_AddPortMapping( handle->urls.controlURL,
74                                                    handle->data.servicetype,
75                                                    portStr, portStr, handle->lanaddr,
76                                                    "Transmission", "TCP" );
77
78        tr_dbg( "UPNP Port Forwarding via '%s', service '%s'.  (local address: %s:%d)",
79                handle->urls.controlURL, handle->data.servicetype, handle->lanaddr, handle->port );
80        tr_dbg( "UPNP Port Forwarding Enabled?  %s", (handle->isForwarding?"Yes":"No") );
81    }
82}
83
84void
85tr_upnpRemoveForwarding ( tr_upnp * handle )
86{
87    handle->port = -1;
88
89    if( handle->isForwarding )
90    {
91        char portStr[16];
92        snprintf( portStr, sizeof(portStr), "%d", handle->port );
93
94        UPNP_DeletePortMapping( handle->urls.controlURL,
95                                handle->data.servicetype,
96                                portStr, "TCP" );
97        tr_dbg( "Stopping port forwarding of '%s', service '%s'",
98                handle->urls.controlURL, handle->data.servicetype );
99
100        handle->isForwarding = FALSE;
101    }
102}
103
104void
105tr_upnpForwardPort( tr_upnp * handle, int publicPort )
106{
107    tr_upnpRemoveForwarding( handle ); /* remove the old forwarding */
108
109    handle->port = publicPort;
110
111    if( handle->isEnabled )
112        tr_upnpStart( handle );
113}
114
115void
116tr_upnpStop( tr_upnp * handle )
117{
118    tr_upnpRemoveForwarding( handle );
119    handle->isEnabled = 0;
120}
121
122int
123tr_upnpStatus( tr_upnp * handle )
124{
125    if( !handle->isEnabled )
126        return TR_NAT_TRAVERSAL_DISABLED;
127
128    if( !handle->isForwarding )
129        return TR_NAT_TRAVERSAL_ERROR;
130
131    return TR_NAT_TRAVERSAL_MAPPED;
132}
133
134void
135tr_upnpPulse( tr_upnp * handle UNUSED )
136{
137    /* no-op */
138}
Note: See TracBrowser for help on using the repository browser.