source: trunk/libtransmission/natpmp.c @ 5486

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

fix one cause of slow shutdown: even if we weren't using natpmp, we would wait on it...

  • Property svn:keywords set to Date Rev Author Id
File size: 6.1 KB
Line 
1/*
2 * This file Copyright (C) 2007-2008 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 5486 2008-04-01 19:52:21Z charles $
11 */
12
13#include <assert.h>
14#include <errno.h>
15#include <time.h>
16#include <inttypes.h>
17
18#ifdef WIN32
19#include <winsock2.h> /* inet_ntoa */
20#else
21#include <arpa/inet.h> /* inet_ntoa */
22#endif
23
24#include <libnatpmp/natpmp.h>
25
26#include "transmission.h"
27#include "natpmp.h"
28#include "shared.h"
29#include "utils.h"
30
31#define LIFETIME_SECS 3600
32#define COMMAND_WAIT_SECS 8
33
34static const char * getKey( void ) { return _( "Port Forwarding (NAT-PMP)" ); }
35
36typedef enum
37{
38    TR_NATPMP_IDLE,
39    TR_NATPMP_ERR,
40    TR_NATPMP_DISCOVER,
41    TR_NATPMP_RECV_PUB,
42    TR_NATPMP_SEND_MAP,
43    TR_NATPMP_RECV_MAP,
44    TR_NATPMP_SEND_UNMAP,
45    TR_NATPMP_RECV_UNMAP
46}
47tr_natpmp_state;
48   
49struct tr_natpmp
50{
51    int port;
52    unsigned int isMapped      : 1;
53    unsigned int hasDiscovered : 1;
54    time_t renewTime;
55    time_t commandTime;
56    tr_natpmp_state state;
57    natpmp_t natpmp;
58};
59
60/**
61***
62**/
63
64static void
65logVal( const char * func, int ret )
66{
67    if( ret==NATPMP_TRYAGAIN )
68        tr_ndbg( getKey(), _( "%s responded \"try again\"" ), func );
69    else if( ret >= 0 )
70        tr_ninf( getKey(), _( "%s succeeded (%d)" ), func, ret );
71    else
72        tr_ninf( getKey(), _( "%s failed (%d): %s (%d)" ), func, ret, tr_strerror(errno), errno );
73}
74
75struct tr_natpmp*
76tr_natpmpInit( void )
77{
78    struct tr_natpmp * nat;
79    nat = tr_new0( struct tr_natpmp, 1 );
80    nat->state = TR_NATPMP_DISCOVER;
81    nat->port = -1;
82    return nat;
83}
84
85void
86tr_natpmpClose( tr_natpmp * nat )
87{
88    assert( !nat->isMapped );
89
90    closenatpmp( &nat->natpmp );
91    tr_free( nat );
92}
93
94static int
95canSendCommand( const struct tr_natpmp * nat )
96{
97    return time(NULL) >= nat->commandTime;
98}
99
100static void
101setCommandTime( struct tr_natpmp * nat )
102{
103    nat->commandTime = time(NULL) + COMMAND_WAIT_SECS;
104}
105
106static void
107setErrorState( struct tr_natpmp * nat )
108{
109    tr_ninf( getKey(), _( "If your router supports NAT-PMP, please make sure NAT-PMP is enabled!" ) );
110    tr_ninf( getKey(), _( "NAT-PMP port forwarding unsuccessful, trying UPnP next" ) );
111    nat->state = TR_NATPMP_ERR;
112}
113
114int
115tr_natpmpPulse( struct tr_natpmp * nat, int port, int isEnabled )
116{
117    int ret;
118
119    if( isEnabled && ( nat->state == TR_NATPMP_DISCOVER ) )
120    {
121        int val = initnatpmp( &nat->natpmp );
122        logVal( "initnatpmp", val );
123        val = sendpublicaddressrequest( &nat->natpmp );
124        logVal( "sendpublicaddressrequest", val );
125        nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_PUB;
126        nat->hasDiscovered = 1;
127        setCommandTime( nat );
128    }
129
130    if( ( nat->state == TR_NATPMP_RECV_PUB ) && canSendCommand( nat ) )
131    {
132        natpmpresp_t response;
133        const int val = readnatpmpresponseorretry( &nat->natpmp, &response );
134        logVal( "readnatpmpresponseorretry", val );
135        if( val >= 0 ) {
136            tr_ninf( getKey(), _( "Found public address \"%s\"" ), inet_ntoa( response.publicaddress.addr ) );
137            nat->state = TR_NATPMP_IDLE;
138        } else if( val != NATPMP_TRYAGAIN ) {
139            setErrorState( nat );
140        }
141    }
142
143    if( ( nat->state == TR_NATPMP_IDLE ) || ( nat->state == TR_NATPMP_ERR ) )
144    {
145        if( nat->isMapped && ( !isEnabled || ( nat->port != port ) ) )
146            nat->state = TR_NATPMP_SEND_UNMAP;
147    }
148
149    if( ( nat->state == TR_NATPMP_SEND_UNMAP ) && canSendCommand( nat ) )
150    {
151        const int val = sendnewportmappingrequest( &nat->natpmp, NATPMP_PROTOCOL_TCP, nat->port, nat->port, 0 );
152        logVal( "sendnewportmappingrequest", val );
153        nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_UNMAP;
154        setCommandTime( nat );
155    }
156
157    if( nat->state == TR_NATPMP_RECV_UNMAP )
158    {
159        natpmpresp_t resp;
160        const int val = readnatpmpresponseorretry( &nat->natpmp, &resp );
161        logVal( "readnatpmpresponseorretry", val );
162        if( val >= 0 ) {
163            tr_ninf( getKey(), _( "no longer forwarding port %d" ), nat->port );
164            nat->state = TR_NATPMP_IDLE;
165            nat->port = -1;
166            nat->isMapped = 0;
167        } else if( val != NATPMP_TRYAGAIN ) {
168            setErrorState( nat );
169        }
170    }
171
172    if( nat->state == TR_NATPMP_IDLE )
173    {
174        if( isEnabled && !nat->isMapped && nat->hasDiscovered )
175            nat->state = TR_NATPMP_SEND_MAP;
176
177        else if( nat->isMapped && time(NULL) >= nat->renewTime )
178            nat->state = TR_NATPMP_SEND_MAP;
179    }
180
181    if( ( nat->state == TR_NATPMP_SEND_MAP ) && canSendCommand( nat ) )
182    {
183        const int val = sendnewportmappingrequest( &nat->natpmp, NATPMP_PROTOCOL_TCP, port, port, LIFETIME_SECS );
184        logVal( "sendnewportmappingrequest", val );
185        nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_MAP;
186        setCommandTime( nat );
187    }
188
189    if( nat->state == TR_NATPMP_RECV_MAP )
190    {
191        natpmpresp_t resp;
192        const int val = readnatpmpresponseorretry( &nat->natpmp, &resp );
193        logVal( "readnatpmpresponseorretry", val );
194        if( val >= 0 ) {
195            nat->state = TR_NATPMP_IDLE;
196            nat->isMapped = 1;
197            nat->renewTime = time( NULL ) + LIFETIME_SECS;
198            nat->port = resp.newportmapping.privateport;
199            tr_ninf( getKey(), _( "port %d forwarded successfully" ), nat->port );
200        } else if( val != NATPMP_TRYAGAIN ) {
201            setErrorState( nat );
202        }
203    }
204
205    switch( nat->state ) {
206        case TR_NATPMP_IDLE:        ret = nat->isMapped ? TR_NAT_TRAVERSAL_MAPPED : TR_NAT_TRAVERSAL_UNMAPPED; break;
207        case TR_NATPMP_DISCOVER:    ret = TR_NAT_TRAVERSAL_UNMAPPED; break;
208        case TR_NATPMP_RECV_PUB:
209        case TR_NATPMP_SEND_MAP:
210        case TR_NATPMP_RECV_MAP:    ret = TR_NAT_TRAVERSAL_MAPPING; break;
211        case TR_NATPMP_SEND_UNMAP:
212        case TR_NATPMP_RECV_UNMAP:  ret = TR_NAT_TRAVERSAL_UNMAPPING; break;
213        default:                    ret = TR_NAT_TRAVERSAL_ERROR; break;
214    }
215    return ret;
216}
Note: See TracBrowser for help on using the repository browser.