2007-12-08 19:34:15 +00:00
|
|
|
/*
|
2008-12-16 00:20:44 +00:00
|
|
|
* This file Copyright (C) 2007-2008 Charles Kerr <charles@transmissionbt.com>
|
2006-09-25 18:37:45 +00:00
|
|
|
*
|
2007-12-08 19:34:15 +00:00
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
|
|
|
* Transmission project are granted a special exemption to clause 2(b)
|
2008-09-23 19:11:04 +00:00
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
2007-12-08 19:34:15 +00:00
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
2006-09-25 18:37:45 +00:00
|
|
|
*
|
2007-12-13 01:20:16 +00:00
|
|
|
* $Id$
|
2007-12-08 19:34:15 +00:00
|
|
|
*/
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2007-11-09 20:07:52 +00:00
|
|
|
#include <assert.h>
|
2007-07-29 18:11:21 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <time.h>
|
2007-12-08 19:34:15 +00:00
|
|
|
#include <inttypes.h>
|
2007-07-29 18:11:21 +00:00
|
|
|
|
2008-07-15 03:26:53 +00:00
|
|
|
#define ENABLE_STRNATPMPERR
|
2007-12-08 19:34:15 +00:00
|
|
|
#include <libnatpmp/natpmp.h>
|
2007-07-14 16:29:21 +00:00
|
|
|
|
2006-09-25 18:37:45 +00:00
|
|
|
#include "transmission.h"
|
2007-07-09 20:10:42 +00:00
|
|
|
#include "natpmp.h"
|
2008-10-22 13:57:30 +00:00
|
|
|
#include "net.h" /* inet_ntoa() */
|
2008-04-11 17:01:13 +00:00
|
|
|
#include "port-forwarding.h"
|
2007-07-30 18:04:10 +00:00
|
|
|
#include "utils.h"
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
#define LIFETIME_SECS 3600
|
2007-12-13 19:28:51 +00:00
|
|
|
#define COMMAND_WAIT_SECS 8
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
static const char *
|
|
|
|
getKey( void ) { return _( "Port Forwarding (NAT-PMP)" ); }
|
2008-03-07 03:26:59 +00:00
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
typedef enum
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2007-12-08 19:34:15 +00:00
|
|
|
TR_NATPMP_IDLE,
|
|
|
|
TR_NATPMP_ERR,
|
2007-12-13 18:56:22 +00:00
|
|
|
TR_NATPMP_DISCOVER,
|
2007-12-08 19:34:15 +00:00
|
|
|
TR_NATPMP_RECV_PUB,
|
|
|
|
TR_NATPMP_SEND_MAP,
|
|
|
|
TR_NATPMP_RECV_MAP,
|
|
|
|
TR_NATPMP_SEND_UNMAP,
|
|
|
|
TR_NATPMP_RECV_UNMAP
|
|
|
|
}
|
|
|
|
tr_natpmp_state;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-11-06 16:02:50 +00:00
|
|
|
struct tr_natpmp
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2008-11-28 22:11:41 +00:00
|
|
|
tr_bool isMapped;
|
|
|
|
tr_bool hasDiscovered;
|
2008-09-23 19:11:04 +00:00
|
|
|
int port;
|
|
|
|
time_t renewTime;
|
|
|
|
time_t commandTime;
|
|
|
|
tr_natpmp_state state;
|
|
|
|
natpmp_t natpmp;
|
2006-09-25 18:37:45 +00:00
|
|
|
};
|
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
2007-02-06 03:24:55 +00:00
|
|
|
|
2007-04-15 07:36:24 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
logVal( const char * func,
|
|
|
|
int ret )
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2008-04-14 20:06:37 +00:00
|
|
|
if( ret == NATPMP_TRYAGAIN )
|
|
|
|
return;
|
|
|
|
if( ret >= 0 )
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_ninf( getKey( ), _( "%s succeeded (%d)" ), func, ret );
|
2006-09-25 18:37:45 +00:00
|
|
|
else
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_ndbg(
|
|
|
|
getKey( ),
|
|
|
|
"%s failed. natpmp returned %d (%s); errno is %d (%s)",
|
|
|
|
func, ret, strnatpmperr( ret ), errno, tr_strerror( errno ) );
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
struct tr_natpmp*
|
|
|
|
tr_natpmpInit( void )
|
2007-04-15 07:36:24 +00:00
|
|
|
{
|
2007-12-13 18:56:22 +00:00
|
|
|
struct tr_natpmp * nat;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-12-13 18:56:22 +00:00
|
|
|
nat = tr_new0( struct tr_natpmp, 1 );
|
|
|
|
nat->state = TR_NATPMP_DISCOVER;
|
2007-12-08 19:34:15 +00:00
|
|
|
nat->port = -1;
|
|
|
|
return nat;
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-12-08 19:34:15 +00:00
|
|
|
tr_natpmpClose( tr_natpmp * nat )
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2008-04-12 02:26:45 +00:00
|
|
|
if( nat )
|
|
|
|
{
|
|
|
|
closenatpmp( &nat->natpmp );
|
|
|
|
tr_free( nat );
|
|
|
|
}
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
2007-12-13 19:28:51 +00:00
|
|
|
static int
|
|
|
|
canSendCommand( const struct tr_natpmp * nat )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
return time( NULL ) >= nat->commandTime;
|
2007-12-13 19:28:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
setCommandTime( struct tr_natpmp * nat )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
nat->commandTime = time( NULL ) + COMMAND_WAIT_SECS;
|
2007-12-13 19:28:51 +00:00
|
|
|
}
|
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_natpmpPulse( struct tr_natpmp * nat,
|
|
|
|
int port,
|
|
|
|
int isEnabled )
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2007-12-08 19:34:15 +00:00
|
|
|
int ret;
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2007-12-13 18:56:22 +00:00
|
|
|
if( isEnabled && ( nat->state == TR_NATPMP_DISCOVER ) )
|
|
|
|
{
|
|
|
|
int val = initnatpmp( &nat->natpmp );
|
|
|
|
logVal( "initnatpmp", val );
|
|
|
|
val = sendpublicaddressrequest( &nat->natpmp );
|
|
|
|
logVal( "sendpublicaddressrequest", val );
|
|
|
|
nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_PUB;
|
|
|
|
nat->hasDiscovered = 1;
|
2007-12-13 19:28:51 +00:00
|
|
|
setCommandTime( nat );
|
2007-12-13 18:56:22 +00:00
|
|
|
}
|
|
|
|
|
2007-12-13 19:28:51 +00:00
|
|
|
if( ( nat->state == TR_NATPMP_RECV_PUB ) && canSendCommand( nat ) )
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2007-12-08 19:34:15 +00:00
|
|
|
natpmpresp_t response;
|
2008-09-23 19:11:04 +00:00
|
|
|
const int val = readnatpmpresponseorretry( &nat->natpmp,
|
|
|
|
&response );
|
2007-12-08 19:34:15 +00:00
|
|
|
logVal( "readnatpmpresponseorretry", val );
|
2008-09-23 19:11:04 +00:00
|
|
|
if( val >= 0 )
|
|
|
|
{
|
|
|
|
tr_ninf( getKey( ), _(
|
|
|
|
"Found public address \"%s\"" ),
|
|
|
|
inet_ntoa( response.pnu.publicaddress.addr ) );
|
2007-12-08 19:34:15 +00:00
|
|
|
nat->state = TR_NATPMP_IDLE;
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
|
|
|
else if( val != NATPMP_TRYAGAIN )
|
|
|
|
{
|
2008-04-12 21:47:10 +00:00
|
|
|
nat->state = TR_NATPMP_ERR;
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
if( ( nat->state == TR_NATPMP_IDLE ) || ( nat->state == TR_NATPMP_ERR ) )
|
2007-02-06 03:24:55 +00:00
|
|
|
{
|
2007-12-08 19:34:15 +00:00
|
|
|
if( nat->isMapped && ( !isEnabled || ( nat->port != port ) ) )
|
|
|
|
nat->state = TR_NATPMP_SEND_UNMAP;
|
2007-02-06 03:24:55 +00:00
|
|
|
}
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2007-12-13 19:28:51 +00:00
|
|
|
if( ( nat->state == TR_NATPMP_SEND_UNMAP ) && canSendCommand( nat ) )
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
const int val =
|
|
|
|
sendnewportmappingrequest( &nat->natpmp, NATPMP_PROTOCOL_TCP,
|
|
|
|
nat->port, nat->port,
|
|
|
|
0 );
|
2007-12-08 19:34:15 +00:00
|
|
|
logVal( "sendnewportmappingrequest", val );
|
|
|
|
nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_UNMAP;
|
2007-12-13 19:28:51 +00:00
|
|
|
setCommandTime( nat );
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
if( nat->state == TR_NATPMP_RECV_UNMAP )
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2007-12-08 19:34:15 +00:00
|
|
|
natpmpresp_t resp;
|
2008-09-23 19:11:04 +00:00
|
|
|
const int val = readnatpmpresponseorretry( &nat->natpmp, &resp );
|
2007-12-08 19:34:15 +00:00
|
|
|
logVal( "readnatpmpresponseorretry", val );
|
2008-09-23 19:11:04 +00:00
|
|
|
if( val >= 0 )
|
|
|
|
{
|
2009-01-01 18:38:49 +00:00
|
|
|
const int p = resp.pnu.newportmapping.privateport;
|
|
|
|
tr_ninf( getKey( ), _( "no longer forwarding port %d" ), p );
|
|
|
|
if( nat->port == p )
|
2008-11-01 16:39:57 +00:00
|
|
|
{
|
|
|
|
nat->port = -1;
|
|
|
|
nat->state = TR_NATPMP_IDLE;
|
|
|
|
nat->isMapped = 0;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
|
|
|
else if( val != NATPMP_TRYAGAIN )
|
|
|
|
{
|
2008-04-12 21:47:10 +00:00
|
|
|
nat->state = TR_NATPMP_ERR;
|
2007-02-06 03:24:55 +00:00
|
|
|
}
|
2007-01-28 08:54:19 +00:00
|
|
|
}
|
2007-02-06 03:24:55 +00:00
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
if( nat->state == TR_NATPMP_IDLE )
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2007-12-13 18:56:22 +00:00
|
|
|
if( isEnabled && !nat->isMapped && nat->hasDiscovered )
|
2007-12-08 19:34:15 +00:00
|
|
|
nat->state = TR_NATPMP_SEND_MAP;
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
else if( nat->isMapped && time( NULL ) >= nat->renewTime )
|
2007-12-08 19:34:15 +00:00
|
|
|
nat->state = TR_NATPMP_SEND_MAP;
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
2007-02-06 03:24:55 +00:00
|
|
|
|
2007-12-13 19:28:51 +00:00
|
|
|
if( ( nat->state == TR_NATPMP_SEND_MAP ) && canSendCommand( nat ) )
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
const int val =
|
|
|
|
sendnewportmappingrequest( &nat->natpmp, NATPMP_PROTOCOL_TCP,
|
|
|
|
port,
|
|
|
|
port,
|
|
|
|
LIFETIME_SECS );
|
2007-12-08 19:34:15 +00:00
|
|
|
logVal( "sendnewportmappingrequest", val );
|
|
|
|
nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_MAP;
|
2007-12-13 19:28:51 +00:00
|
|
|
setCommandTime( nat );
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
if( nat->state == TR_NATPMP_RECV_MAP )
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2007-12-08 19:34:15 +00:00
|
|
|
natpmpresp_t resp;
|
2008-09-23 19:11:04 +00:00
|
|
|
const int val = readnatpmpresponseorretry( &nat->natpmp, &resp );
|
2007-12-08 19:34:15 +00:00
|
|
|
logVal( "readnatpmpresponseorretry", val );
|
2008-09-23 19:11:04 +00:00
|
|
|
if( val >= 0 )
|
|
|
|
{
|
2007-12-08 19:34:15 +00:00
|
|
|
nat->state = TR_NATPMP_IDLE;
|
|
|
|
nat->isMapped = 1;
|
|
|
|
nat->renewTime = time( NULL ) + LIFETIME_SECS;
|
2008-05-29 13:39:39 +00:00
|
|
|
nat->port = resp.pnu.newportmapping.privateport;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_ninf( getKey( ), _(
|
|
|
|
"Port %d forwarded successfully" ), nat->port );
|
|
|
|
}
|
|
|
|
else if( val != NATPMP_TRYAGAIN )
|
|
|
|
{
|
2008-04-12 21:47:10 +00:00
|
|
|
nat->state = TR_NATPMP_ERR;
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
switch( nat->state )
|
|
|
|
{
|
|
|
|
case TR_NATPMP_IDLE:
|
|
|
|
ret = nat->isMapped ? TR_PORT_MAPPED : TR_PORT_UNMAPPED; break;
|
|
|
|
|
|
|
|
case TR_NATPMP_DISCOVER:
|
|
|
|
ret = TR_PORT_UNMAPPED; break;
|
|
|
|
|
2007-12-13 18:56:22 +00:00
|
|
|
case TR_NATPMP_RECV_PUB:
|
|
|
|
case TR_NATPMP_SEND_MAP:
|
2008-09-23 19:11:04 +00:00
|
|
|
case TR_NATPMP_RECV_MAP:
|
|
|
|
ret = TR_PORT_MAPPING; break;
|
|
|
|
|
2007-12-13 18:56:22 +00:00
|
|
|
case TR_NATPMP_SEND_UNMAP:
|
2008-09-23 19:11:04 +00:00
|
|
|
case TR_NATPMP_RECV_UNMAP:
|
|
|
|
ret = TR_PORT_UNMAPPING; break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ret = TR_PORT_ERROR; break;
|
2007-12-13 18:56:22 +00:00
|
|
|
}
|
2007-12-08 19:34:15 +00:00
|
|
|
return ret;
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|