source: trunk/libtransmission/natpmp.c @ 4599

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

mingw portability fixes: #includes in natpmp.c

  • Property svn:keywords set to Date Rev Author Id
File size: 6.2 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 4599 2008-01-10 19:27:13Z 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#ifdef WIN32
20#include <winsock2.h> /* inet_ntoa */
21#else
22#include <arpa/inet.h> /* inet_ntoa */
23#endif
24
25#include <libnatpmp/natpmp.h>
26
27#include "transmission.h"
28#include "natpmp.h"
29#include "shared.h"
30#include "utils.h"
31
32#define LIFETIME_SECS 3600
33#define COMMAND_WAIT_SECS 8
34
35#define KEY "Port Mapping (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        tr_dbg( KEY "%s returned 'try again'", func );
70    else if( ret >= 0 )
71        tr_dbg( KEY "%s returned success (%d)", func, ret );
72    else
73        tr_err( KEY "%s returned error %d, errno is %d (%s)", func, ret, errno, strerror(errno) );
74}
75
76struct tr_natpmp*
77tr_natpmpInit( void )
78{
79    struct tr_natpmp * nat;
80    nat = tr_new0( struct tr_natpmp, 1 );
81    nat->state = TR_NATPMP_DISCOVER;
82    nat->port = -1;
83    return nat;
84}
85
86void
87tr_natpmpClose( tr_natpmp * nat )
88{
89    assert( !nat->isMapped );
90    assert( ( nat->state == TR_NATPMP_IDLE )
91         || ( nat->state == TR_NATPMP_ERR )
92         || ( nat->state == TR_NATPMP_DISCOVER ) );
93
94    closenatpmp( &nat->natpmp );
95    tr_free( nat );
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
110static void
111setErrorState( struct tr_natpmp * nat )
112{
113    tr_err( KEY "If your router supports NAT-PMP, please make sure NAT-PMP is enabled!" );
114    tr_err( KEY "NAT-PMP port forwarding unsuccessful, trying UPnP next" );
115    nat->state = TR_NATPMP_ERR;
116}
117
118int
119tr_natpmpPulse( struct tr_natpmp * nat, int port, int isEnabled )
120{
121    int ret;
122
123    if( isEnabled && ( nat->state == TR_NATPMP_DISCOVER ) )
124    {
125        int val = initnatpmp( &nat->natpmp );
126        logVal( "initnatpmp", val );
127        val = sendpublicaddressrequest( &nat->natpmp );
128        logVal( "sendpublicaddressrequest", val );
129        nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_PUB;
130        nat->hasDiscovered = 1;
131        setCommandTime( nat );
132    }
133
134    if( ( nat->state == TR_NATPMP_RECV_PUB ) && canSendCommand( nat ) )
135    {
136        natpmpresp_t response;
137        const int val = readnatpmpresponseorretry( &nat->natpmp, &response );
138        logVal( "readnatpmpresponseorretry", val );
139        if( val >= 0 ) {
140            tr_inf( KEY "found public address %s", inet_ntoa( response.publicaddress.addr ) );
141            nat->state = TR_NATPMP_IDLE;
142        } else if( val != NATPMP_TRYAGAIN ) {
143            setErrorState( nat );
144        }
145    }
146
147    if( ( nat->state == TR_NATPMP_IDLE ) || ( nat->state == TR_NATPMP_ERR ) )
148    {
149        if( nat->isMapped && ( !isEnabled || ( nat->port != port ) ) )
150            nat->state = TR_NATPMP_SEND_UNMAP;
151    }
152
153    if( ( nat->state == TR_NATPMP_SEND_UNMAP ) && canSendCommand( nat ) )
154    {
155        const int val = sendnewportmappingrequest( &nat->natpmp, NATPMP_PROTOCOL_TCP, nat->port, nat->port, 0 );
156        logVal( "sendnewportmappingrequest", val );
157        nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_UNMAP;
158        setCommandTime( nat );
159    }
160
161    if( nat->state == TR_NATPMP_RECV_UNMAP )
162    {
163        natpmpresp_t resp;
164        const int val = readnatpmpresponseorretry( &nat->natpmp, &resp );
165        logVal( "readnatpmpresponseorretry", val );
166        if( val >= 0 ) {
167            tr_inf( KEY "port %d has been unmapped.", nat->port );
168            nat->state = TR_NATPMP_IDLE;
169            nat->port = -1;
170            nat->isMapped = 0;
171        } else if( val != NATPMP_TRYAGAIN ) {
172            setErrorState( nat );
173        }
174    }
175
176    if( nat->state == TR_NATPMP_IDLE )
177    {
178        if( isEnabled && !nat->isMapped && nat->hasDiscovered )
179            nat->state = TR_NATPMP_SEND_MAP;
180
181        else if( nat->isMapped && time(NULL) >= nat->renewTime )
182            nat->state = TR_NATPMP_SEND_MAP;
183    }
184
185    if( ( nat->state == TR_NATPMP_SEND_MAP ) && canSendCommand( nat ) )
186    {
187        const int val = sendnewportmappingrequest( &nat->natpmp, NATPMP_PROTOCOL_TCP, port, port, LIFETIME_SECS );
188        logVal( "sendnewportmappingrequest", val );
189        nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_MAP;
190        setCommandTime( nat );
191    }
192
193    if( nat->state == TR_NATPMP_RECV_MAP )
194    {
195        natpmpresp_t resp;
196        const int val = readnatpmpresponseorretry( &nat->natpmp, &resp );
197        logVal( "readnatpmpresponseorretry", val );
198        if( val >= 0 ) {
199            nat->state = TR_NATPMP_IDLE;
200            nat->isMapped = 1;
201            nat->renewTime = time( NULL ) + LIFETIME_SECS;
202            nat->port = resp.newportmapping.privateport;
203            tr_inf( KEY "port %d mapped successfully", nat->port );
204        } else if( val != NATPMP_TRYAGAIN ) {
205            setErrorState( nat );
206        }
207    }
208
209    switch( nat->state ) {
210        case TR_NATPMP_IDLE:        ret = nat->isMapped ? TR_NAT_TRAVERSAL_MAPPED : TR_NAT_TRAVERSAL_UNMAPPED; break;
211        case TR_NATPMP_DISCOVER:    ret = TR_NAT_TRAVERSAL_UNMAPPED; break;
212        case TR_NATPMP_RECV_PUB:
213        case TR_NATPMP_SEND_MAP:
214        case TR_NATPMP_RECV_MAP:    ret = TR_NAT_TRAVERSAL_MAPPING; break;
215        case TR_NATPMP_SEND_UNMAP:
216        case TR_NATPMP_RECV_UNMAP:  ret = TR_NAT_TRAVERSAL_UNMAPPING; break;
217        default:                    ret = TR_NAT_TRAVERSAL_ERROR; break;
218    }
219    return ret;
220}
Note: See TracBrowser for help on using the repository browser.