source: trunk/libtransmission/natpmp.c @ 12918

Last change on this file since 12918 was 12225, checked in by jordan, 11 years ago

(trunk libT) copyediting: remove a bunch of seemingly-unneeded network headers in net.[ch].

I'm less certain that these are unneeded because networking APIs seem to have more variation between platforms, but it's better to remove the cruft and then add back whatever headers $PLATFORM users complain about, than to not remove the cruft at all...

  • Property svn:keywords set to Date Rev Author Id
File size: 6.9 KB
RevLine 
[4092]1/*
[11709]2 * This file Copyright (C) Mnemosyne LLC
[864]3 *
[11599]4 * This file is licensed by the GPL version 2. Works owned by the
[4092]5 * Transmission project are granted a special exemption to clause 2(b)
[6795]6 * so that the bulk of its code can remain under the MIT license.
[4092]7 * This exemption does not extend to derived works not owned by
8 * the Transmission project.
[864]9 *
[4140]10 * $Id: natpmp.c 12225 2011-03-24 22:57:39Z jordan $
[4092]11 */
[864]12
[2544]13#include <errno.h>
14#include <time.h>
[4092]15#include <inttypes.h>
[2544]16
[12225]17#include <event2/util.h> /* evutil_inet_ntop() */
18
[6332]19#define ENABLE_STRNATPMPERR
[4092]20#include <libnatpmp/natpmp.h>
[2343]21
[864]22#include "transmission.h"
[2311]23#include "natpmp.h"
[12225]24#include "net.h" /* tr_netCloseSocket */
[5585]25#include "port-forwarding.h"
[2555]26#include "utils.h"
[864]27
[4092]28#define LIFETIME_SECS 3600
[4155]29#define COMMAND_WAIT_SECS 8
[864]30
[6795]31static const char *
32getKey( void ) { return _( "Port Forwarding (NAT-PMP)" ); }
[5209]33
[4092]34typedef enum
[864]35{
[4092]36    TR_NATPMP_IDLE,
37    TR_NATPMP_ERR,
[4154]38    TR_NATPMP_DISCOVER,
[4092]39    TR_NATPMP_RECV_PUB,
40    TR_NATPMP_SEND_MAP,
41    TR_NATPMP_RECV_MAP,
42    TR_NATPMP_SEND_UNMAP,
43    TR_NATPMP_RECV_UNMAP
44}
45tr_natpmp_state;
[6795]46
[3731]47struct tr_natpmp
[864]48{
[12204]49    bool              has_discovered;
50    bool              is_mapped;
[10945]51
52    tr_port           public_port;
53    tr_port           private_port;
54
55    time_t            renew_time;
56    time_t            command_time;
57    tr_natpmp_state   state;
58    natpmp_t          natpmp;
[864]59};
60
[4092]61/**
62***
63**/
[1460]64
[1720]65static void
[6795]66logVal( const char * func,
67        int          ret )
[864]68{
[5620]69    if( ret == NATPMP_TRYAGAIN )
70        return;
71    if( ret >= 0 )
[6795]72        tr_ninf( getKey( ), _( "%s succeeded (%d)" ), func, ret );
[864]73    else
[6795]74        tr_ndbg(
75             getKey( ),
[11599]76            "%s failed. Natpmp returned %d (%s); errno is %d (%s)",
[6795]77            func, ret, strnatpmperr( ret ), errno, tr_strerror( errno ) );
[864]78}
79
[4092]80struct tr_natpmp*
81tr_natpmpInit( void )
[864]82{
[4154]83    struct tr_natpmp * nat;
[6795]84
[4154]85    nat = tr_new0( struct tr_natpmp, 1 );
86    nat->state = TR_NATPMP_DISCOVER;
[10945]87    nat->public_port = 0;
88    nat->private_port = 0;
[9418]89    nat->natpmp.s = -1; /* socket */
[4092]90    return nat;
[864]91}
92
93void
[4092]94tr_natpmpClose( tr_natpmp * nat )
[864]95{
[5592]96    if( nat )
97    {
[9418]98        if( nat->natpmp.s >= 0 )
99            tr_netCloseSocket( nat->natpmp.s );
[5592]100        tr_free( nat );
101    }
[1720]102}
103
[4155]104static int
105canSendCommand( const struct tr_natpmp * nat )
106{
[10945]107    return tr_time( ) >= nat->command_time;
[4155]108}
109
110static void
111setCommandTime( struct tr_natpmp * nat )
112{
[10945]113    nat->command_time = tr_time( ) + COMMAND_WAIT_SECS;
[4155]114}
115
[4092]116int
[12204]117tr_natpmpPulse( struct tr_natpmp * nat, tr_port private_port, bool is_enabled, tr_port * public_port )
[864]118{
[4092]119    int ret;
[864]120
[10945]121    if( is_enabled && ( nat->state == TR_NATPMP_DISCOVER ) )
[4154]122    {
123        int val = initnatpmp( &nat->natpmp );
124        logVal( "initnatpmp", val );
125        val = sendpublicaddressrequest( &nat->natpmp );
126        logVal( "sendpublicaddressrequest", val );
127        nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_PUB;
[12204]128        nat->has_discovered = true;
[4155]129        setCommandTime( nat );
[4154]130    }
131
[4155]132    if( ( nat->state == TR_NATPMP_RECV_PUB ) && canSendCommand( nat ) )
[864]133    {
[4092]134        natpmpresp_t response;
[10945]135        const int val = readnatpmpresponseorretry( &nat->natpmp, &response );
[4092]136        logVal( "readnatpmpresponseorretry", val );
[6795]137        if( val >= 0 )
138        {
[12225]139            char str[128];
140            evutil_inet_ntop( AF_INET, &response.pnu.publicaddress.addr, str, sizeof( str ) );
141            tr_ninf( getKey( ), _( "Found public address \"%s\"" ), str );
[4092]142            nat->state = TR_NATPMP_IDLE;
[6795]143        }
144        else if( val != NATPMP_TRYAGAIN )
145        {
[5600]146            nat->state = TR_NATPMP_ERR;
[4092]147        }
[864]148    }
149
[4092]150    if( ( nat->state == TR_NATPMP_IDLE ) || ( nat->state == TR_NATPMP_ERR ) )
[1730]151    {
[10945]152        if( nat->is_mapped && ( !is_enabled || ( nat->private_port != private_port ) ) )
[4092]153            nat->state = TR_NATPMP_SEND_UNMAP;
[1730]154    }
155
[4155]156    if( ( nat->state == TR_NATPMP_SEND_UNMAP ) && canSendCommand( nat ) )
[864]157    {
[10945]158        const int val = sendnewportmappingrequest( &nat->natpmp, NATPMP_PROTOCOL_TCP,
159                                                   nat->private_port,
160                                                   nat->public_port,
161                                                   0 );
[4092]162        logVal( "sendnewportmappingrequest", val );
163        nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_UNMAP;
[4155]164        setCommandTime( nat );
[864]165    }
166
[4092]167    if( nat->state == TR_NATPMP_RECV_UNMAP )
[1720]168    {
[4092]169        natpmpresp_t resp;
[10945]170        const int val = readnatpmpresponseorretry( &nat->natpmp, &resp );
[4092]171        logVal( "readnatpmpresponseorretry", val );
[6795]172        if( val >= 0 )
173        {
[10945]174            const int private_port = resp.pnu.newportmapping.privateport;
175
176            tr_ninf( getKey( ), _( "no longer forwarding port %d" ), private_port );
177
178            if( nat->private_port == private_port )
[7009]179            {
[10945]180                nat->private_port = 0;
181                nat->public_port = 0;
[7009]182                nat->state = TR_NATPMP_IDLE;
[12204]183                nat->is_mapped = false;
[7009]184            }
[6795]185        }
186        else if( val != NATPMP_TRYAGAIN )
187        {
[5600]188            nat->state = TR_NATPMP_ERR;
[864]189        }
190    }
191
[4092]192    if( nat->state == TR_NATPMP_IDLE )
[864]193    {
[10945]194        if( is_enabled && !nat->is_mapped && nat->has_discovered )
[4092]195            nat->state = TR_NATPMP_SEND_MAP;
[864]196
[10945]197        else if( nat->is_mapped && tr_time( ) >= nat->renew_time )
[4092]198            nat->state = TR_NATPMP_SEND_MAP;
[864]199    }
[1460]200
[4155]201    if( ( nat->state == TR_NATPMP_SEND_MAP ) && canSendCommand( nat ) )
[864]202    {
[10945]203        const int val = sendnewportmappingrequest( &nat->natpmp, NATPMP_PROTOCOL_TCP, private_port, private_port, LIFETIME_SECS );
[4092]204        logVal( "sendnewportmappingrequest", val );
205        nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_MAP;
[4155]206        setCommandTime( nat );
[864]207    }
[1460]208
[4092]209    if( nat->state == TR_NATPMP_RECV_MAP )
[864]210    {
[4092]211        natpmpresp_t resp;
[6795]212        const int    val = readnatpmpresponseorretry( &nat->natpmp, &resp );
[4092]213        logVal( "readnatpmpresponseorretry", val );
[6795]214        if( val >= 0 )
215        {
[4092]216            nat->state = TR_NATPMP_IDLE;
[12204]217            nat->is_mapped = true;
[11391]218            nat->renew_time = tr_time( ) + ( resp.pnu.newportmapping.lifetime / 2 );
[10945]219            nat->private_port = resp.pnu.newportmapping.privateport;
220            nat->public_port = resp.pnu.newportmapping.mappedpublicport;
221            tr_ninf( getKey( ), _( "Port %d forwarded successfully" ), nat->private_port );
[6795]222        }
223        else if( val != NATPMP_TRYAGAIN )
224        {
[5600]225            nat->state = TR_NATPMP_ERR;
[864]226        }
227    }
228
[6795]229    switch( nat->state )
230    {
231        case TR_NATPMP_IDLE:
[10945]232            *public_port = nat->public_port;
233            return nat->is_mapped ? TR_PORT_MAPPED : TR_PORT_UNMAPPED;
234            break;
[6795]235
236        case TR_NATPMP_DISCOVER:
237            ret = TR_PORT_UNMAPPED; break;
238
[4154]239        case TR_NATPMP_RECV_PUB:
240        case TR_NATPMP_SEND_MAP:
[6795]241        case TR_NATPMP_RECV_MAP:
242            ret = TR_PORT_MAPPING; break;
243
[4154]244        case TR_NATPMP_SEND_UNMAP:
[6795]245        case TR_NATPMP_RECV_UNMAP:
246            ret = TR_PORT_UNMAPPING; break;
247
248        default:
249            ret = TR_PORT_ERROR; break;
[4154]250    }
[901]251    return ret;
[864]252}
[6795]253
Note: See TracBrowser for help on using the repository browser.