source: trunk/libtransmission/natpmp.c @ 5193

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

first cut at adding i18n hooks into libtransmission.

  • Property svn:keywords set to Date Rev Author Id
File size: 6.2 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 5193 2008-03-04 02:02:25Z 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 "shared.h"
29#include "utils.h"
30
31#define LIFETIME_SECS 3600
32#define COMMAND_WAIT_SECS 8
33
34#define KEY "Port Mapping (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        tr_dbg( _( KEY "%s returned 'try again'" ), func );
69    else if( ret >= 0 )
70        tr_dbg( _( KEY "%s returned success (%d)" ), func, ret );
71    else
72        tr_err( _( KEY "%s returned error %d, errno is %d (%s)" ), func, ret, errno, tr_strerror(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    assert( !nat->isMapped );
89    assert( ( nat->state == TR_NATPMP_IDLE )
90         || ( nat->state == TR_NATPMP_ERR )
91         || ( nat->state == TR_NATPMP_DISCOVER ) );
92
93    closenatpmp( &nat->natpmp );
94    tr_free( nat );
95}
96
97static int
98canSendCommand( const struct tr_natpmp * nat )
99{
100    return time(NULL) >= nat->commandTime;
101}
102
103static void
104setCommandTime( struct tr_natpmp * nat )
105{
106    nat->commandTime = time(NULL) + COMMAND_WAIT_SECS;
107}
108
109static void
110setErrorState( struct tr_natpmp * nat )
111{
112    tr_err( _( KEY "If your router supports NAT-PMP, please make sure NAT-PMP is enabled!" ) );
113    tr_err( _( KEY "NAT-PMP port forwarding unsuccessful, trying UPnP next" ) );
114    nat->state = TR_NATPMP_ERR;
115}
116
117int
118tr_natpmpPulse( struct tr_natpmp * nat, int port, int isEnabled )
119{
120    int ret;
121
122    if( isEnabled && ( nat->state == TR_NATPMP_DISCOVER ) )
123    {
124        int val = initnatpmp( &nat->natpmp );
125        logVal( "initnatpmp", val );
126        val = sendpublicaddressrequest( &nat->natpmp );
127        logVal( "sendpublicaddressrequest", val );
128        nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_PUB;
129        nat->hasDiscovered = 1;
130        setCommandTime( nat );
131    }
132
133    if( ( nat->state == TR_NATPMP_RECV_PUB ) && canSendCommand( nat ) )
134    {
135        natpmpresp_t response;
136        const int val = readnatpmpresponseorretry( &nat->natpmp, &response );
137        logVal( "readnatpmpresponseorretry", val );
138        if( val >= 0 ) {
139            tr_inf( _( KEY "found public address %s" ), inet_ntoa( response.publicaddress.addr ) );
140            nat->state = TR_NATPMP_IDLE;
141        } else if( val != NATPMP_TRYAGAIN ) {
142            setErrorState( nat );
143        }
144    }
145
146    if( ( nat->state == TR_NATPMP_IDLE ) || ( nat->state == TR_NATPMP_ERR ) )
147    {
148        if( nat->isMapped && ( !isEnabled || ( nat->port != port ) ) )
149            nat->state = TR_NATPMP_SEND_UNMAP;
150    }
151
152    if( ( nat->state == TR_NATPMP_SEND_UNMAP ) && canSendCommand( nat ) )
153    {
154        const int val = sendnewportmappingrequest( &nat->natpmp, NATPMP_PROTOCOL_TCP, nat->port, nat->port, 0 );
155        logVal( "sendnewportmappingrequest", val );
156        nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_UNMAP;
157        setCommandTime( nat );
158    }
159
160    if( nat->state == TR_NATPMP_RECV_UNMAP )
161    {
162        natpmpresp_t resp;
163        const int val = readnatpmpresponseorretry( &nat->natpmp, &resp );
164        logVal( "readnatpmpresponseorretry", val );
165        if( val >= 0 ) {
166            tr_inf( _( KEY "port %d has been unmapped." ), nat->port );
167            nat->state = TR_NATPMP_IDLE;
168            nat->port = -1;
169            nat->isMapped = 0;
170        } else if( val != NATPMP_TRYAGAIN ) {
171            setErrorState( nat );
172        }
173    }
174
175    if( nat->state == TR_NATPMP_IDLE )
176    {
177        if( isEnabled && !nat->isMapped && nat->hasDiscovered )
178            nat->state = TR_NATPMP_SEND_MAP;
179
180        else if( nat->isMapped && time(NULL) >= nat->renewTime )
181            nat->state = TR_NATPMP_SEND_MAP;
182    }
183
184    if( ( nat->state == TR_NATPMP_SEND_MAP ) && canSendCommand( nat ) )
185    {
186        const int val = sendnewportmappingrequest( &nat->natpmp, NATPMP_PROTOCOL_TCP, port, port, LIFETIME_SECS );
187        logVal( "sendnewportmappingrequest", val );
188        nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_MAP;
189        setCommandTime( nat );
190    }
191
192    if( nat->state == TR_NATPMP_RECV_MAP )
193    {
194        natpmpresp_t resp;
195        const int val = readnatpmpresponseorretry( &nat->natpmp, &resp );
196        logVal( "readnatpmpresponseorretry", val );
197        if( val >= 0 ) {
198            nat->state = TR_NATPMP_IDLE;
199            nat->isMapped = 1;
200            nat->renewTime = time( NULL ) + LIFETIME_SECS;
201            nat->port = resp.newportmapping.privateport;
202            tr_inf( _( KEY "port %d mapped successfully" ), nat->port );
203        } else if( val != NATPMP_TRYAGAIN ) {
204            setErrorState( nat );
205        }
206    }
207
208    switch( nat->state ) {
209        case TR_NATPMP_IDLE:        ret = nat->isMapped ? TR_NAT_TRAVERSAL_MAPPED : TR_NAT_TRAVERSAL_UNMAPPED; break;
210        case TR_NATPMP_DISCOVER:    ret = TR_NAT_TRAVERSAL_UNMAPPED; break;
211        case TR_NATPMP_RECV_PUB:
212        case TR_NATPMP_SEND_MAP:
213        case TR_NATPMP_RECV_MAP:    ret = TR_NAT_TRAVERSAL_MAPPING; break;
214        case TR_NATPMP_SEND_UNMAP:
215        case TR_NATPMP_RECV_UNMAP:  ret = TR_NAT_TRAVERSAL_UNMAPPING; break;
216        default:                    ret = TR_NAT_TRAVERSAL_ERROR; break;
217    }
218    return ret;
219}
Note: See TracBrowser for help on using the repository browser.