source: trunk/libtransmission/natpmp.c @ 4140

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

fix the 500ms remap reported by Chinstrap

  • Property svn:keywords set to Date Rev Author Id
File size: 5.2 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 4140 2007-12-13 01:20:16Z 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
32#define KEY "Port Mapping (NAT-PMP): "
33
34typedef enum
35{
36    TR_NATPMP_IDLE,
37    TR_NATPMP_ERR,
38    TR_NATPMP_RECV_PUB,
39    TR_NATPMP_SEND_MAP,
40    TR_NATPMP_RECV_MAP,
41    TR_NATPMP_SEND_UNMAP,
42    TR_NATPMP_RECV_UNMAP
43}
44tr_natpmp_state;
45   
46struct tr_natpmp
47{
48    int port;
49    int isMapped;
50    time_t renewTime;
51    tr_natpmp_state state;
52    natpmp_t natpmp;
53};
54
55/**
56***
57**/
58
59static void
60logVal( const char * func, int ret )
61{
62    if( ret==NATPMP_TRYAGAIN )
63        tr_dbg( KEY "%s returned 'try again'", func );
64    else if( ret >= 0 )
65        tr_dbg( KEY "%s returned success (%d)", func, ret );
66    else
67        tr_err( KEY "%s returned error %d, errno is %d (%s)", func, ret, errno, strerror(errno) );
68}
69
70struct tr_natpmp*
71tr_natpmpInit( void )
72{
73    struct tr_natpmp * nat = tr_new0( struct tr_natpmp, 1 );
74    int val;
75
76    val = initnatpmp( &nat->natpmp );
77    logVal( "initnatpmp", val );
78    val = sendpublicaddressrequest( &nat->natpmp );
79    logVal( "sendpublicaddressrequest", val );
80
81    nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_PUB;
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 ) || ( nat->state == TR_NATPMP_ERR ) );
91
92    closenatpmp( &nat->natpmp );
93    tr_free( nat );
94}
95
96int
97tr_natpmpPulse( struct tr_natpmp * nat, int port, int isEnabled )
98{
99    int ret;
100
101    if( nat->state == TR_NATPMP_RECV_PUB )
102    {
103        natpmpresp_t response;
104        const int val = readnatpmpresponseorretry( &nat->natpmp, &response );
105        logVal( "readnatpmpresponseorretry", val );
106        if( val >= 0 ) {
107            tr_inf( KEY "found public address %s", inet_ntoa( response.publicaddress.addr ) );
108            nat->state = TR_NATPMP_IDLE;
109        } else if( val != NATPMP_TRYAGAIN ) {
110            nat->state = TR_NATPMP_ERR;
111        }
112    }
113
114    if( ( nat->state == TR_NATPMP_IDLE ) || ( nat->state == TR_NATPMP_ERR ) )
115    {
116        if( nat->isMapped && ( !isEnabled || ( nat->port != port ) ) )
117            nat->state = TR_NATPMP_SEND_UNMAP;
118    }
119
120    if( nat->state == TR_NATPMP_SEND_UNMAP )
121    {
122        const int val = sendnewportmappingrequest( &nat->natpmp, NATPMP_PROTOCOL_TCP, nat->port, nat->port, 0 );
123        logVal( "sendnewportmappingrequest", val );
124        nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_UNMAP;
125    }
126
127    if( nat->state == TR_NATPMP_RECV_UNMAP )
128    {
129        natpmpresp_t resp;
130        const int val = readnatpmpresponseorretry( &nat->natpmp, &resp );
131        logVal( "readnatpmpresponseorretry", val );
132        if( val >= 0 ) {
133            tr_inf( KEY "port %d has been unmapped.", nat->port );
134            nat->state = TR_NATPMP_IDLE;
135            nat->port = -1;
136            nat->isMapped = 0;
137        } else if( val != NATPMP_TRYAGAIN ) {
138            nat->state = TR_NATPMP_ERR;
139        }
140    }
141
142    if( nat->state == TR_NATPMP_IDLE )
143    {
144        if( isEnabled && !nat->isMapped )
145            nat->state = TR_NATPMP_SEND_MAP;
146
147        else if( nat->isMapped && time(NULL) >= nat->renewTime )
148            nat->state = TR_NATPMP_SEND_MAP;
149    }
150
151    if( nat->state == TR_NATPMP_SEND_MAP )
152    {
153        const int val = sendnewportmappingrequest( &nat->natpmp, NATPMP_PROTOCOL_TCP, port, port, LIFETIME_SECS );
154        logVal( "sendnewportmappingrequest", val );
155        nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_MAP;
156    }
157
158    if( nat->state == TR_NATPMP_RECV_MAP )
159    {
160        natpmpresp_t resp;
161        const int val = readnatpmpresponseorretry( &nat->natpmp, &resp );
162        logVal( "readnatpmpresponseorretry", val );
163        if( val >= 0 ) {
164            nat->state = TR_NATPMP_IDLE;
165            nat->isMapped = 1;
166            nat->renewTime = time( NULL ) + LIFETIME_SECS;
167            nat->port = resp.newportmapping.privateport;
168            tr_inf( KEY "port %d mapped successfully", nat->port );
169        } else if( val != NATPMP_TRYAGAIN ) {
170            nat->state = TR_NATPMP_ERR;
171        }
172    }
173
174    if( nat->state == TR_NATPMP_ERR )
175        ret = TR_NAT_TRAVERSAL_ERROR;
176    else if( ( nat->state == TR_NATPMP_IDLE ) &&  ( nat->isMapped ) )
177        ret = TR_NAT_TRAVERSAL_MAPPED;
178    else if( ( nat->state == TR_NATPMP_IDLE ) &&  ( !nat->isMapped ) )
179        ret = TR_NAT_TRAVERSAL_UNMAPPED;
180    else if( ( nat->state == TR_NATPMP_SEND_MAP ) || ( nat->state == TR_NATPMP_RECV_MAP ) )
181        ret = TR_NAT_TRAVERSAL_MAPPING;
182    else if( ( nat->state == TR_NATPMP_SEND_UNMAP ) || ( nat->state == TR_NATPMP_RECV_UNMAP ) )
183        ret = TR_NAT_TRAVERSAL_UNMAPPING;
184    return ret;
185}
Note: See TracBrowser for help on using the repository browser.