source: trunk/libtransmission/natpmp.c @ 5900

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

more janitorial cleanup on the "tr_session*" namespace. Today's victim: tr_handle_status.

  • Property svn:keywords set to Date Rev Author Id
File size: 5.8 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 5900 2008-05-22 12:31:47Z 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 "port-forwarding.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        return;
69    if( ret >= 0 )
70        tr_ninf( getKey(), _( "%s succeeded (%d)" ), func, ret );
71    else
72        tr_ndbg( 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    if( nat )
89    {
90        closenatpmp( &nat->natpmp );
91        tr_free( nat );
92    }
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
107int
108tr_natpmpPulse( struct tr_natpmp * nat, int port, int isEnabled )
109{
110    int ret;
111
112    if( isEnabled && ( nat->state == TR_NATPMP_DISCOVER ) )
113    {
114        int val = initnatpmp( &nat->natpmp );
115        logVal( "initnatpmp", val );
116        val = sendpublicaddressrequest( &nat->natpmp );
117        logVal( "sendpublicaddressrequest", val );
118        nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_PUB;
119        nat->hasDiscovered = 1;
120        setCommandTime( nat );
121    }
122
123    if( ( nat->state == TR_NATPMP_RECV_PUB ) && canSendCommand( nat ) )
124    {
125        natpmpresp_t response;
126        const int val = readnatpmpresponseorretry( &nat->natpmp, &response );
127        logVal( "readnatpmpresponseorretry", val );
128        if( val >= 0 ) {
129            tr_ninf( getKey(), _( "Found public address \"%s\"" ), inet_ntoa( response.publicaddress.addr ) );
130            nat->state = TR_NATPMP_IDLE;
131        } else if( val != NATPMP_TRYAGAIN ) {
132            nat->state = TR_NATPMP_ERR;
133        }
134    }
135
136    if( ( nat->state == TR_NATPMP_IDLE ) || ( nat->state == TR_NATPMP_ERR ) )
137    {
138        if( nat->isMapped && ( !isEnabled || ( nat->port != port ) ) )
139            nat->state = TR_NATPMP_SEND_UNMAP;
140    }
141
142    if( ( nat->state == TR_NATPMP_SEND_UNMAP ) && canSendCommand( nat ) )
143    {
144        const int val = sendnewportmappingrequest( &nat->natpmp, NATPMP_PROTOCOL_TCP, nat->port, nat->port, 0 );
145        logVal( "sendnewportmappingrequest", val );
146        nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_UNMAP;
147        setCommandTime( nat );
148    }
149
150    if( nat->state == TR_NATPMP_RECV_UNMAP )
151    {
152        natpmpresp_t resp;
153        const int val = readnatpmpresponseorretry( &nat->natpmp, &resp );
154        logVal( "readnatpmpresponseorretry", val );
155        if( val >= 0 ) {
156            tr_ninf( getKey(), _( "no longer forwarding port %d" ), nat->port );
157            nat->state = TR_NATPMP_IDLE;
158            nat->port = -1;
159            nat->isMapped = 0;
160        } else if( val != NATPMP_TRYAGAIN ) {
161            nat->state = TR_NATPMP_ERR;
162        }
163    }
164
165    if( nat->state == TR_NATPMP_IDLE )
166    {
167        if( isEnabled && !nat->isMapped && nat->hasDiscovered )
168            nat->state = TR_NATPMP_SEND_MAP;
169
170        else if( nat->isMapped && time(NULL) >= nat->renewTime )
171            nat->state = TR_NATPMP_SEND_MAP;
172    }
173
174    if( ( nat->state == TR_NATPMP_SEND_MAP ) && canSendCommand( nat ) )
175    {
176        const int val = sendnewportmappingrequest( &nat->natpmp, NATPMP_PROTOCOL_TCP, port, port, LIFETIME_SECS );
177        logVal( "sendnewportmappingrequest", val );
178        nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_MAP;
179        setCommandTime( nat );
180    }
181
182    if( nat->state == TR_NATPMP_RECV_MAP )
183    {
184        natpmpresp_t resp;
185        const int val = readnatpmpresponseorretry( &nat->natpmp, &resp );
186        logVal( "readnatpmpresponseorretry", val );
187        if( val >= 0 ) {
188            nat->state = TR_NATPMP_IDLE;
189            nat->isMapped = 1;
190            nat->renewTime = time( NULL ) + LIFETIME_SECS;
191            nat->port = resp.newportmapping.privateport;
192            tr_ninf( getKey(), _( "Port %d forwarded successfully" ), nat->port );
193        } else if( val != NATPMP_TRYAGAIN ) {
194            nat->state = TR_NATPMP_ERR;
195        }
196    }
197
198    switch( nat->state ) {
199        case TR_NATPMP_IDLE:        ret = nat->isMapped ? TR_PORT_MAPPED : TR_PORT_UNMAPPED; break;
200        case TR_NATPMP_DISCOVER:    ret = TR_PORT_UNMAPPED; break;
201        case TR_NATPMP_RECV_PUB:
202        case TR_NATPMP_SEND_MAP:
203        case TR_NATPMP_RECV_MAP:    ret = TR_PORT_MAPPING; break;
204        case TR_NATPMP_SEND_UNMAP:
205        case TR_NATPMP_RECV_UNMAP:  ret = TR_PORT_UNMAPPING; break;
206        default:                    ret = TR_PORT_ERROR; break;
207    }
208    return ret;
209}
Note: See TracBrowser for help on using the repository browser.