source: trunk/libtransmission/natpmp.c @ 4155

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

follow Chinstrap's suggestion of waiting a small interval between sending nat commands as per the nat spec

  • Property svn:keywords set to Date Rev Author Id
File size: 5.9 KB
Line 
1/*
2 * This file Copyright (C) 2007 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 4155 2007-12-13 19:28:51Z 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#include <sys/socket.h>
20#include <netinet/in.h>
21#include <arpa/inet.h>
22
23#include <libnatpmp/natpmp.h>
24
25#include "transmission.h"
26#include "natpmp.h"
27#include "shared.h"
28#include "utils.h"
29
30#define LIFETIME_SECS 3600
31#define COMMAND_WAIT_SECS 8
32
33#define KEY "Port Mapping (NAT-PMP): "
34
35typedef enum
36{
37    TR_NATPMP_IDLE,
38    TR_NATPMP_ERR,
39    TR_NATPMP_DISCOVER,
40    TR_NATPMP_RECV_PUB,
41    TR_NATPMP_SEND_MAP,
42    TR_NATPMP_RECV_MAP,
43    TR_NATPMP_SEND_UNMAP,
44    TR_NATPMP_RECV_UNMAP
45}
46tr_natpmp_state;
47   
48struct tr_natpmp
49{
50    int port;
51    unsigned int isMapped      : 1;
52    unsigned int hasDiscovered : 1;
53    time_t renewTime;
54    time_t commandTime;
55    tr_natpmp_state state;
56    natpmp_t natpmp;
57};
58
59/**
60***
61**/
62
63static void
64logVal( const char * func, int ret )
65{
66    if( ret==NATPMP_TRYAGAIN )
67        tr_dbg( KEY "%s returned 'try again'", func );
68    else if( ret >= 0 )
69        tr_dbg( KEY "%s returned success (%d)", func, ret );
70    else
71        tr_err( KEY "%s returned error %d, errno is %d (%s)", func, ret, errno, strerror(errno) );
72}
73
74struct tr_natpmp*
75tr_natpmpInit( void )
76{
77    struct tr_natpmp * nat;
78    nat = tr_new0( struct tr_natpmp, 1 );
79    nat->state = TR_NATPMP_DISCOVER;
80    nat->port = -1;
81    return nat;
82}
83
84void
85tr_natpmpClose( tr_natpmp * nat )
86{
87    assert( !nat->isMapped );
88    assert( ( nat->state == TR_NATPMP_IDLE ) || ( nat->state == TR_NATPMP_ERR ) );
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
106int
107tr_natpmpPulse( struct tr_natpmp * nat, int port, int isEnabled )
108{
109    int ret;
110
111    if( isEnabled && ( nat->state == TR_NATPMP_DISCOVER ) )
112    {
113        int val = initnatpmp( &nat->natpmp );
114        logVal( "initnatpmp", val );
115        val = sendpublicaddressrequest( &nat->natpmp );
116        logVal( "sendpublicaddressrequest", val );
117        nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_PUB;
118        nat->hasDiscovered = 1;
119        setCommandTime( nat );
120    }
121
122    if( ( nat->state == TR_NATPMP_RECV_PUB ) && canSendCommand( nat ) )
123    {
124        natpmpresp_t response;
125        const int val = readnatpmpresponseorretry( &nat->natpmp, &response );
126        logVal( "readnatpmpresponseorretry", val );
127        if( val >= 0 ) {
128            tr_inf( KEY "found public address %s", inet_ntoa( response.publicaddress.addr ) );
129            nat->state = TR_NATPMP_IDLE;
130        } else if( val != NATPMP_TRYAGAIN ) {
131            nat->state = TR_NATPMP_ERR;
132        }
133    }
134
135    if( ( nat->state == TR_NATPMP_IDLE ) || ( nat->state == TR_NATPMP_ERR ) )
136    {
137        if( nat->isMapped && ( !isEnabled || ( nat->port != port ) ) )
138            nat->state = TR_NATPMP_SEND_UNMAP;
139    }
140
141    if( ( nat->state == TR_NATPMP_SEND_UNMAP ) && canSendCommand( nat ) )
142    {
143        const int val = sendnewportmappingrequest( &nat->natpmp, NATPMP_PROTOCOL_TCP, nat->port, nat->port, 0 );
144        logVal( "sendnewportmappingrequest", val );
145        nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_UNMAP;
146        setCommandTime( nat );
147    }
148
149    if( nat->state == TR_NATPMP_RECV_UNMAP )
150    {
151        natpmpresp_t resp;
152        const int val = readnatpmpresponseorretry( &nat->natpmp, &resp );
153        logVal( "readnatpmpresponseorretry", val );
154        if( val >= 0 ) {
155            tr_inf( KEY "port %d has been unmapped.", nat->port );
156            nat->state = TR_NATPMP_IDLE;
157            nat->port = -1;
158            nat->isMapped = 0;
159        } else if( val != NATPMP_TRYAGAIN ) {
160            nat->state = TR_NATPMP_ERR;
161        }
162    }
163
164    if( nat->state == TR_NATPMP_IDLE )
165    {
166        if( isEnabled && !nat->isMapped && nat->hasDiscovered )
167            nat->state = TR_NATPMP_SEND_MAP;
168
169        else if( nat->isMapped && time(NULL) >= nat->renewTime )
170            nat->state = TR_NATPMP_SEND_MAP;
171    }
172
173    if( ( nat->state == TR_NATPMP_SEND_MAP ) && canSendCommand( nat ) )
174    {
175        const int val = sendnewportmappingrequest( &nat->natpmp, NATPMP_PROTOCOL_TCP, port, port, LIFETIME_SECS );
176        logVal( "sendnewportmappingrequest", val );
177        nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_MAP;
178        setCommandTime( nat );
179    }
180
181    if( nat->state == TR_NATPMP_RECV_MAP )
182    {
183        natpmpresp_t resp;
184        const int val = readnatpmpresponseorretry( &nat->natpmp, &resp );
185        logVal( "readnatpmpresponseorretry", val );
186        if( val >= 0 ) {
187            nat->state = TR_NATPMP_IDLE;
188            nat->isMapped = 1;
189            nat->renewTime = time( NULL ) + LIFETIME_SECS;
190            nat->port = resp.newportmapping.privateport;
191            tr_inf( KEY "port %d mapped successfully", nat->port );
192        } else if( val != NATPMP_TRYAGAIN ) {
193            nat->state = TR_NATPMP_ERR;
194        }
195    }
196
197    switch( nat->state ) {
198        case TR_NATPMP_IDLE:        ret = nat->isMapped ? TR_NAT_TRAVERSAL_MAPPED : TR_NAT_TRAVERSAL_UNMAPPED; break;
199        case TR_NATPMP_DISCOVER:    ret = TR_NAT_TRAVERSAL_UNMAPPED; break;
200        case TR_NATPMP_RECV_PUB:
201        case TR_NATPMP_SEND_MAP:
202        case TR_NATPMP_RECV_MAP:    ret = TR_NAT_TRAVERSAL_MAPPING; break;
203        case TR_NATPMP_SEND_UNMAP:
204        case TR_NATPMP_RECV_UNMAP:  ret = TR_NAT_TRAVERSAL_UNMAPPING; break;
205        default:                    ret = TR_NAT_TRAVERSAL_ERROR; break;
206    }
207    return ret;
208}
Note: See TracBrowser for help on using the repository browser.