source: trunk/libtransmission/natpmp.c @ 6332

Last change on this file since 6332 was 6332, checked in by charles, 13 years ago

better nat-pmp error logging. (this will very briefly break the mac build)

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