source: trunk/libtransmission/natpmp.c @ 4154

Last change on this file since 4154 was 4154, checked in by charles, 14 years ago

take pea_'s suggestion of not sending out natpmp/upnp discover messages until port forwarding is enabled

  • Property svn:keywords set to Date Rev Author Id
File size: 5.4 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: natpmp.c 4154 2007-12-13 18:56:22Z charles $
11 */
12
13#include <assert.h>
14#include <errno.h>
15#include <time.h>
16#include <inttypes.h>
17#include <string.h> /* strerror */
18
19#include <sys/socket.h>
20#include <netinet/in.h>
21#include <arpa/inet.h>
22
23#include <libnatpmp/natpmp.h>
24
25#include "transmission.h"
26#include "natpmp.h"
27#include "shared.h"
28#include "utils.h"
29
30#define LIFETIME_SECS 3600
31
32#define KEY "Port Mapping (NAT-PMP): "
33
34typedef enum
35{
36    TR_NATPMP_IDLE,
37    TR_NATPMP_ERR,
38    TR_NATPMP_DISCOVER,
39    TR_NATPMP_RECV_PUB,
40    TR_NATPMP_SEND_MAP,
41    TR_NATPMP_RECV_MAP,
42    TR_NATPMP_SEND_UNMAP,
43    TR_NATPMP_RECV_UNMAP
44}
45tr_natpmp_state;
46   
47struct tr_natpmp
48{
49    int port;
50    unsigned int isMapped      : 1;
51    unsigned int hasDiscovered : 1;
52    time_t renewTime;
53    tr_natpmp_state state;
54    natpmp_t natpmp;
55};
56
57/**
58***
59**/
60
61static void
62logVal( const char * func, int ret )
63{
64    if( ret==NATPMP_TRYAGAIN )
65        tr_dbg( KEY "%s returned 'try again'", func );
66    else if( ret >= 0 )
67        tr_dbg( KEY "%s returned success (%d)", func, ret );
68    else
69        tr_err( KEY "%s returned error %d, errno is %d (%s)", func, ret, errno, strerror(errno) );
70}
71
72struct tr_natpmp*
73tr_natpmpInit( void )
74{
75    struct tr_natpmp * nat;
76    nat = tr_new0( struct tr_natpmp, 1 );
77    nat->state = TR_NATPMP_DISCOVER;
78    nat->port = -1;
79    return nat;
80}
81
82void
83tr_natpmpClose( tr_natpmp * nat )
84{
85    assert( !nat->isMapped );
86    assert( ( nat->state == TR_NATPMP_IDLE ) || ( nat->state == TR_NATPMP_ERR ) );
87
88    closenatpmp( &nat->natpmp );
89    tr_free( nat );
90}
91
92int
93tr_natpmpPulse( struct tr_natpmp * nat, int port, int isEnabled )
94{
95    int ret;
96
97    if( isEnabled && ( nat->state == TR_NATPMP_DISCOVER ) )
98    {
99        int val = initnatpmp( &nat->natpmp );
100        logVal( "initnatpmp", val );
101        val = sendpublicaddressrequest( &nat->natpmp );
102        logVal( "sendpublicaddressrequest", val );
103        nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_PUB;
104        nat->hasDiscovered = 1;
105    }
106
107    if( nat->state == TR_NATPMP_RECV_PUB )
108    {
109        natpmpresp_t response;
110        const int val = readnatpmpresponseorretry( &nat->natpmp, &response );
111        logVal( "readnatpmpresponseorretry", val );
112        if( val >= 0 ) {
113            tr_inf( KEY "found public address %s", inet_ntoa( response.publicaddress.addr ) );
114            nat->state = TR_NATPMP_IDLE;
115        } else if( val != NATPMP_TRYAGAIN ) {
116            nat->state = TR_NATPMP_ERR;
117        }
118    }
119
120    if( ( nat->state == TR_NATPMP_IDLE ) || ( nat->state == TR_NATPMP_ERR ) )
121    {
122        if( nat->isMapped && ( !isEnabled || ( nat->port != port ) ) )
123            nat->state = TR_NATPMP_SEND_UNMAP;
124    }
125
126    if( nat->state == TR_NATPMP_SEND_UNMAP )
127    {
128        const int val = sendnewportmappingrequest( &nat->natpmp, NATPMP_PROTOCOL_TCP, nat->port, nat->port, 0 );
129        logVal( "sendnewportmappingrequest", val );
130        nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_UNMAP;
131    }
132
133    if( nat->state == TR_NATPMP_RECV_UNMAP )
134    {
135        natpmpresp_t resp;
136        const int val = readnatpmpresponseorretry( &nat->natpmp, &resp );
137        logVal( "readnatpmpresponseorretry", val );
138        if( val >= 0 ) {
139            tr_inf( KEY "port %d has been unmapped.", nat->port );
140            nat->state = TR_NATPMP_IDLE;
141            nat->port = -1;
142            nat->isMapped = 0;
143        } else if( val != NATPMP_TRYAGAIN ) {
144            nat->state = TR_NATPMP_ERR;
145        }
146    }
147
148    if( nat->state == TR_NATPMP_IDLE )
149    {
150        if( isEnabled && !nat->isMapped && nat->hasDiscovered )
151            nat->state = TR_NATPMP_SEND_MAP;
152
153        else if( nat->isMapped && time(NULL) >= nat->renewTime )
154            nat->state = TR_NATPMP_SEND_MAP;
155    }
156
157    if( nat->state == TR_NATPMP_SEND_MAP )
158    {
159        const int val = sendnewportmappingrequest( &nat->natpmp, NATPMP_PROTOCOL_TCP, port, port, LIFETIME_SECS );
160        logVal( "sendnewportmappingrequest", val );
161        nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_MAP;
162    }
163
164    if( nat->state == TR_NATPMP_RECV_MAP )
165    {
166        natpmpresp_t resp;
167        const int val = readnatpmpresponseorretry( &nat->natpmp, &resp );
168        logVal( "readnatpmpresponseorretry", val );
169        if( val >= 0 ) {
170            nat->state = TR_NATPMP_IDLE;
171            nat->isMapped = 1;
172            nat->renewTime = time( NULL ) + LIFETIME_SECS;
173            nat->port = resp.newportmapping.privateport;
174            tr_inf( KEY "port %d mapped successfully", nat->port );
175        } else if( val != NATPMP_TRYAGAIN ) {
176            nat->state = TR_NATPMP_ERR;
177        }
178    }
179
180    switch( nat->state ) {
181        case TR_NATPMP_IDLE:        ret = nat->isMapped ? TR_NAT_TRAVERSAL_MAPPED : TR_NAT_TRAVERSAL_UNMAPPED; break;
182        case TR_NATPMP_DISCOVER:    ret = TR_NAT_TRAVERSAL_UNMAPPED; break;
183        case TR_NATPMP_RECV_PUB:
184        case TR_NATPMP_SEND_MAP:
185        case TR_NATPMP_RECV_MAP:    ret = TR_NAT_TRAVERSAL_MAPPING; break;
186        case TR_NATPMP_SEND_UNMAP:
187        case TR_NATPMP_RECV_UNMAP:  ret = TR_NAT_TRAVERSAL_UNMAPPING; break;
188        default:                    ret = TR_NAT_TRAVERSAL_ERROR; break;
189    }
190    return ret;
191}
Note: See TracBrowser for help on using the repository browser.