source: trunk/libtransmission/natpmp.c @ 4175

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

add messages asking the user to check & make sure that port forwarding is enabled on their router

  • Property svn:keywords set to Date Rev Author Id
File size: 6.1 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 4175 2007-12-15 05:28: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#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            tr_err( KEY "If your router supports NAT-PMP, please make sure NAT-PMP is enabled!" );
134            nat->state = TR_NATPMP_ERR;
135        }
136    }
137
138    if( ( nat->state == TR_NATPMP_IDLE ) || ( nat->state == TR_NATPMP_ERR ) )
139    {
140        if( nat->isMapped && ( !isEnabled || ( nat->port != port ) ) )
141            nat->state = TR_NATPMP_SEND_UNMAP;
142    }
143
144    if( ( nat->state == TR_NATPMP_SEND_UNMAP ) && canSendCommand( nat ) )
145    {
146        const int val = sendnewportmappingrequest( &nat->natpmp, NATPMP_PROTOCOL_TCP, nat->port, nat->port, 0 );
147        logVal( "sendnewportmappingrequest", val );
148        nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_UNMAP;
149        setCommandTime( nat );
150    }
151
152    if( nat->state == TR_NATPMP_RECV_UNMAP )
153    {
154        natpmpresp_t resp;
155        const int val = readnatpmpresponseorretry( &nat->natpmp, &resp );
156        logVal( "readnatpmpresponseorretry", val );
157        if( val >= 0 ) {
158            tr_inf( KEY "port %d has been unmapped.", nat->port );
159            nat->state = TR_NATPMP_IDLE;
160            nat->port = -1;
161            nat->isMapped = 0;
162        } else if( val != NATPMP_TRYAGAIN ) {
163            tr_err( KEY "If your router supports NAT-PMP, please make sure NAT-PMP is enabled!" );
164            nat->state = TR_NATPMP_ERR;
165        }
166    }
167
168    if( nat->state == TR_NATPMP_IDLE )
169    {
170        if( isEnabled && !nat->isMapped && nat->hasDiscovered )
171            nat->state = TR_NATPMP_SEND_MAP;
172
173        else if( nat->isMapped && time(NULL) >= nat->renewTime )
174            nat->state = TR_NATPMP_SEND_MAP;
175    }
176
177    if( ( nat->state == TR_NATPMP_SEND_MAP ) && canSendCommand( nat ) )
178    {
179        const int val = sendnewportmappingrequest( &nat->natpmp, NATPMP_PROTOCOL_TCP, port, port, LIFETIME_SECS );
180        logVal( "sendnewportmappingrequest", val );
181        nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_MAP;
182        setCommandTime( nat );
183    }
184
185    if( nat->state == TR_NATPMP_RECV_MAP )
186    {
187        natpmpresp_t resp;
188        const int val = readnatpmpresponseorretry( &nat->natpmp, &resp );
189        logVal( "readnatpmpresponseorretry", val );
190        if( val >= 0 ) {
191            nat->state = TR_NATPMP_IDLE;
192            nat->isMapped = 1;
193            nat->renewTime = time( NULL ) + LIFETIME_SECS;
194            nat->port = resp.newportmapping.privateport;
195            tr_inf( KEY "port %d mapped successfully", nat->port );
196        } else if( val != NATPMP_TRYAGAIN ) {
197            nat->state = TR_NATPMP_ERR;
198        }
199    }
200
201    switch( nat->state ) {
202        case TR_NATPMP_IDLE:        ret = nat->isMapped ? TR_NAT_TRAVERSAL_MAPPED : TR_NAT_TRAVERSAL_UNMAPPED; break;
203        case TR_NATPMP_DISCOVER:    ret = TR_NAT_TRAVERSAL_UNMAPPED; break;
204        case TR_NATPMP_RECV_PUB:
205        case TR_NATPMP_SEND_MAP:
206        case TR_NATPMP_RECV_MAP:    ret = TR_NAT_TRAVERSAL_MAPPING; break;
207        case TR_NATPMP_SEND_UNMAP:
208        case TR_NATPMP_RECV_UNMAP:  ret = TR_NAT_TRAVERSAL_UNMAPPING; break;
209        default:                    ret = TR_NAT_TRAVERSAL_ERROR; break;
210    }
211    return ret;
212}
Note: See TracBrowser for help on using the repository browser.