source: trunk/libtransmission/natpmp.c @ 5208

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

more i18n strings work

  • Property svn:keywords set to Date Rev Author Id
File size: 6.3 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 5208 2008-03-06 21:59:00Z 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
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    time_t commandTime;
54    tr_natpmp_state state;
55    natpmp_t natpmp;
56};
57
58/**
59***
60**/
61
62static void
63logVal( const char * func, int ret )
64{
65    if( ret==NATPMP_TRYAGAIN )
66        tr_dbg( _( "Port Mapping (NAT-PMP): %s returned 'try again'" ), func );
67    else if( ret >= 0 )
68        tr_dbg( _( "Port Mapping (NAT-PMP): %s returned success (%d)" ), func, ret );
69    else
70        tr_err( _( "Port Mapping (NAT-PMP): %s returned error %d, errno is %d (%s)" ), func, ret, errno, tr_strerror(errno) );
71}
72
73struct tr_natpmp*
74tr_natpmpInit( void )
75{
76    struct tr_natpmp * nat;
77    nat = tr_new0( struct tr_natpmp, 1 );
78    nat->state = TR_NATPMP_DISCOVER;
79    nat->port = -1;
80    return nat;
81}
82
83void
84tr_natpmpClose( tr_natpmp * nat )
85{
86    assert( !nat->isMapped );
87    assert( ( nat->state == TR_NATPMP_IDLE )
88         || ( nat->state == TR_NATPMP_ERR )
89         || ( nat->state == TR_NATPMP_DISCOVER ) );
90
91    closenatpmp( &nat->natpmp );
92    tr_free( nat );
93}
94
95static int
96canSendCommand( const struct tr_natpmp * nat )
97{
98    return time(NULL) >= nat->commandTime;
99}
100
101static void
102setCommandTime( struct tr_natpmp * nat )
103{
104    nat->commandTime = time(NULL) + COMMAND_WAIT_SECS;
105}
106
107static void
108setErrorState( struct tr_natpmp * nat )
109{
110    tr_err( _( "Port Mapping (NAT-PMP): If your router supports NAT-PMP, please make sure NAT-PMP is enabled!" ) );
111    tr_err( _( "Port Mapping (NAT-PMP): NAT-PMP port forwarding unsuccessful, trying UPnP next" ) );
112    nat->state = TR_NATPMP_ERR;
113}
114
115int
116tr_natpmpPulse( struct tr_natpmp * nat, int port, int isEnabled )
117{
118    int ret;
119
120    if( isEnabled && ( nat->state == TR_NATPMP_DISCOVER ) )
121    {
122        int val = initnatpmp( &nat->natpmp );
123        logVal( "initnatpmp", val );
124        val = sendpublicaddressrequest( &nat->natpmp );
125        logVal( "sendpublicaddressrequest", val );
126        nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_PUB;
127        nat->hasDiscovered = 1;
128        setCommandTime( nat );
129    }
130
131    if( ( nat->state == TR_NATPMP_RECV_PUB ) && canSendCommand( nat ) )
132    {
133        natpmpresp_t response;
134        const int val = readnatpmpresponseorretry( &nat->natpmp, &response );
135        logVal( "readnatpmpresponseorretry", val );
136        if( val >= 0 ) {
137            tr_inf( _( "Port Mapping (NAT-PMP): found public address %s" ), inet_ntoa( response.publicaddress.addr ) );
138            nat->state = TR_NATPMP_IDLE;
139        } else if( val != NATPMP_TRYAGAIN ) {
140            setErrorState( nat );
141        }
142    }
143
144    if( ( nat->state == TR_NATPMP_IDLE ) || ( nat->state == TR_NATPMP_ERR ) )
145    {
146        if( nat->isMapped && ( !isEnabled || ( nat->port != port ) ) )
147            nat->state = TR_NATPMP_SEND_UNMAP;
148    }
149
150    if( ( nat->state == TR_NATPMP_SEND_UNMAP ) && canSendCommand( nat ) )
151    {
152        const int val = sendnewportmappingrequest( &nat->natpmp, NATPMP_PROTOCOL_TCP, nat->port, nat->port, 0 );
153        logVal( "sendnewportmappingrequest", val );
154        nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_UNMAP;
155        setCommandTime( nat );
156    }
157
158    if( nat->state == TR_NATPMP_RECV_UNMAP )
159    {
160        natpmpresp_t resp;
161        const int val = readnatpmpresponseorretry( &nat->natpmp, &resp );
162        logVal( "readnatpmpresponseorretry", val );
163        if( val >= 0 ) {
164            tr_inf( _( "Port Mapping (NAT-PMP): port %d has been unmapped." ), nat->port );
165            nat->state = TR_NATPMP_IDLE;
166            nat->port = -1;
167            nat->isMapped = 0;
168        } else if( val != NATPMP_TRYAGAIN ) {
169            setErrorState( nat );
170        }
171    }
172
173    if( nat->state == TR_NATPMP_IDLE )
174    {
175        if( isEnabled && !nat->isMapped && nat->hasDiscovered )
176            nat->state = TR_NATPMP_SEND_MAP;
177
178        else if( nat->isMapped && time(NULL) >= nat->renewTime )
179            nat->state = TR_NATPMP_SEND_MAP;
180    }
181
182    if( ( nat->state == TR_NATPMP_SEND_MAP ) && canSendCommand( nat ) )
183    {
184        const int val = sendnewportmappingrequest( &nat->natpmp, NATPMP_PROTOCOL_TCP, port, port, LIFETIME_SECS );
185        logVal( "sendnewportmappingrequest", val );
186        nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_MAP;
187        setCommandTime( nat );
188    }
189
190    if( nat->state == TR_NATPMP_RECV_MAP )
191    {
192        natpmpresp_t resp;
193        const int val = readnatpmpresponseorretry( &nat->natpmp, &resp );
194        logVal( "readnatpmpresponseorretry", val );
195        if( val >= 0 ) {
196            nat->state = TR_NATPMP_IDLE;
197            nat->isMapped = 1;
198            nat->renewTime = time( NULL ) + LIFETIME_SECS;
199            nat->port = resp.newportmapping.privateport;
200            tr_inf( _( "Port Mapping (NAT-PMP): port %d mapped successfully" ), nat->port );
201        } else if( val != NATPMP_TRYAGAIN ) {
202            setErrorState( nat );
203        }
204    }
205
206    switch( nat->state ) {
207        case TR_NATPMP_IDLE:        ret = nat->isMapped ? TR_NAT_TRAVERSAL_MAPPED : TR_NAT_TRAVERSAL_UNMAPPED; break;
208        case TR_NATPMP_DISCOVER:    ret = TR_NAT_TRAVERSAL_UNMAPPED; break;
209        case TR_NATPMP_RECV_PUB:
210        case TR_NATPMP_SEND_MAP:
211        case TR_NATPMP_RECV_MAP:    ret = TR_NAT_TRAVERSAL_MAPPING; break;
212        case TR_NATPMP_SEND_UNMAP:
213        case TR_NATPMP_RECV_UNMAP:  ret = TR_NAT_TRAVERSAL_UNMAPPING; break;
214        default:                    ret = TR_NAT_TRAVERSAL_ERROR; break;
215    }
216    return ret;
217}
Note: See TracBrowser for help on using the repository browser.