source: trunk/libtransmission/natpmp.c @ 4157

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

fix crash on shutdown on natpmp routers reported by SoftwareElves?

  • 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 4157 2007-12-13 20:19:52Z 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 )
89         || ( nat->state == TR_NATPMP_ERR )
90         || ( nat->state == TR_NATPMP_DISCOVER ) );
91
92    closenatpmp( &nat->natpmp );
93    tr_free( nat );
94}
95
96static int
97canSendCommand( const struct tr_natpmp * nat )
98{
99    return time(NULL) >= nat->commandTime;
100}
101
102static void
103setCommandTime( struct tr_natpmp * nat )
104{
105    nat->commandTime = time(NULL) + COMMAND_WAIT_SECS;
106}
107
108int
109tr_natpmpPulse( struct tr_natpmp * nat, int port, int isEnabled )
110{
111    int ret;
112
113    if( isEnabled && ( nat->state == TR_NATPMP_DISCOVER ) )
114    {
115        int val = initnatpmp( &nat->natpmp );
116        logVal( "initnatpmp", val );
117        val = sendpublicaddressrequest( &nat->natpmp );
118        logVal( "sendpublicaddressrequest", val );
119        nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_PUB;
120        nat->hasDiscovered = 1;
121        setCommandTime( nat );
122    }
123
124    if( ( nat->state == TR_NATPMP_RECV_PUB ) && canSendCommand( nat ) )
125    {
126        natpmpresp_t response;
127        const int val = readnatpmpresponseorretry( &nat->natpmp, &response );
128        logVal( "readnatpmpresponseorretry", val );
129        if( val >= 0 ) {
130            tr_inf( KEY "found public address %s", inet_ntoa( response.publicaddress.addr ) );
131            nat->state = TR_NATPMP_IDLE;
132        } else if( val != NATPMP_TRYAGAIN ) {
133            nat->state = TR_NATPMP_ERR;
134        }
135    }
136
137    if( ( nat->state == TR_NATPMP_IDLE ) || ( nat->state == TR_NATPMP_ERR ) )
138    {
139        if( nat->isMapped && ( !isEnabled || ( nat->port != port ) ) )
140            nat->state = TR_NATPMP_SEND_UNMAP;
141    }
142
143    if( ( nat->state == TR_NATPMP_SEND_UNMAP ) && canSendCommand( nat ) )
144    {
145        const int val = sendnewportmappingrequest( &nat->natpmp, NATPMP_PROTOCOL_TCP, nat->port, nat->port, 0 );
146        logVal( "sendnewportmappingrequest", val );
147        nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_UNMAP;
148        setCommandTime( nat );
149    }
150
151    if( nat->state == TR_NATPMP_RECV_UNMAP )
152    {
153        natpmpresp_t resp;
154        const int val = readnatpmpresponseorretry( &nat->natpmp, &resp );
155        logVal( "readnatpmpresponseorretry", val );
156        if( val >= 0 ) {
157            tr_inf( KEY "port %d has been unmapped.", nat->port );
158            nat->state = TR_NATPMP_IDLE;
159            nat->port = -1;
160            nat->isMapped = 0;
161        } else if( val != NATPMP_TRYAGAIN ) {
162            nat->state = TR_NATPMP_ERR;
163        }
164    }
165
166    if( nat->state == TR_NATPMP_IDLE )
167    {
168        if( isEnabled && !nat->isMapped && nat->hasDiscovered )
169            nat->state = TR_NATPMP_SEND_MAP;
170
171        else if( nat->isMapped && time(NULL) >= nat->renewTime )
172            nat->state = TR_NATPMP_SEND_MAP;
173    }
174
175    if( ( nat->state == TR_NATPMP_SEND_MAP ) && canSendCommand( nat ) )
176    {
177        const int val = sendnewportmappingrequest( &nat->natpmp, NATPMP_PROTOCOL_TCP, port, port, LIFETIME_SECS );
178        logVal( "sendnewportmappingrequest", val );
179        nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_MAP;
180        setCommandTime( nat );
181    }
182
183    if( nat->state == TR_NATPMP_RECV_MAP )
184    {
185        natpmpresp_t resp;
186        const int val = readnatpmpresponseorretry( &nat->natpmp, &resp );
187        logVal( "readnatpmpresponseorretry", val );
188        if( val >= 0 ) {
189            nat->state = TR_NATPMP_IDLE;
190            nat->isMapped = 1;
191            nat->renewTime = time( NULL ) + LIFETIME_SECS;
192            nat->port = resp.newportmapping.privateport;
193            tr_inf( KEY "port %d mapped successfully", nat->port );
194        } else if( val != NATPMP_TRYAGAIN ) {
195            nat->state = TR_NATPMP_ERR;
196        }
197    }
198
199    switch( nat->state ) {
200        case TR_NATPMP_IDLE:        ret = nat->isMapped ? TR_NAT_TRAVERSAL_MAPPED : TR_NAT_TRAVERSAL_UNMAPPED; break;
201        case TR_NATPMP_DISCOVER:    ret = TR_NAT_TRAVERSAL_UNMAPPED; break;
202        case TR_NATPMP_RECV_PUB:
203        case TR_NATPMP_SEND_MAP:
204        case TR_NATPMP_RECV_MAP:    ret = TR_NAT_TRAVERSAL_MAPPING; break;
205        case TR_NATPMP_SEND_UNMAP:
206        case TR_NATPMP_RECV_UNMAP:  ret = TR_NAT_TRAVERSAL_UNMAPPING; break;
207        default:                    ret = TR_NAT_TRAVERSAL_ERROR; break;
208    }
209    return ret;
210}
Note: See TracBrowser for help on using the repository browser.