2007-11-06 16:02:50 +00:00
|
|
|
/*
|
2009-01-10 23:09:07 +00:00
|
|
|
* This file Copyright (C) 2007-2009 Charles Kerr <charles@transmissionbt.com>
|
2006-09-25 18:37:45 +00:00
|
|
|
*
|
2007-11-06 16:02:50 +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-11-06 16:02:50 +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-11-07 00:42:19 +00:00
|
|
|
* $Id$
|
2007-11-06 16:02:50 +00:00
|
|
|
*/
|
2007-07-29 18:11:21 +00:00
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
#include <assert.h>
|
2007-12-09 17:50:05 +00:00
|
|
|
#include <errno.h>
|
2007-08-04 02:55:06 +00:00
|
|
|
|
2007-11-06 16:02:50 +00:00
|
|
|
#include <miniupnp/miniupnpc.h>
|
|
|
|
#include <miniupnp/upnpcommands.h>
|
2007-07-14 16:29:21 +00:00
|
|
|
|
2006-09-25 18:37:45 +00:00
|
|
|
#include "transmission.h"
|
2008-04-11 17:01:13 +00:00
|
|
|
#include "port-forwarding.h"
|
|
|
|
#include "session.h"
|
2007-07-30 18:04:10 +00:00
|
|
|
#include "utils.h"
|
2007-11-06 16:02:50 +00:00
|
|
|
#include "upnp.h"
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
static const char *
|
|
|
|
getKey( void ) { return _( "Port Forwarding (UPnP)" ); }
|
2008-03-07 03:26:59 +00:00
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
TR_UPNP_IDLE,
|
|
|
|
TR_UPNP_ERR,
|
|
|
|
TR_UPNP_DISCOVER,
|
|
|
|
TR_UPNP_MAP,
|
|
|
|
TR_UPNP_UNMAP
|
|
|
|
}
|
|
|
|
tr_upnp_state;
|
|
|
|
|
2007-11-06 16:02:50 +00:00
|
|
|
struct tr_upnp
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2008-11-28 22:11:41 +00:00
|
|
|
tr_bool hasDiscovered;
|
2008-09-23 19:11:04 +00:00
|
|
|
struct UPNPUrls urls;
|
|
|
|
struct IGDdatas data;
|
|
|
|
int port;
|
|
|
|
char lanaddr[16];
|
|
|
|
unsigned int isMapped;
|
|
|
|
tr_upnp_state state;
|
2006-09-25 18:37:45 +00:00
|
|
|
};
|
|
|
|
|
2007-11-06 16:02:50 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
2007-06-10 22:26:59 +00:00
|
|
|
|
2007-11-06 16:02:50 +00:00
|
|
|
tr_upnp*
|
2007-08-21 20:38:34 +00:00
|
|
|
tr_upnpInit( void )
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2007-11-06 16:02:50 +00:00
|
|
|
tr_upnp * ret = tr_new0( tr_upnp, 1 );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-12-13 18:56:22 +00:00
|
|
|
ret->state = TR_UPNP_DISCOVER;
|
2007-11-06 16:02:50 +00:00
|
|
|
ret->port = -1;
|
2006-09-25 18:37:45 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-11-06 16:02:50 +00:00
|
|
|
tr_upnpClose( tr_upnp * handle )
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2007-12-08 19:34:15 +00:00
|
|
|
assert( !handle->isMapped );
|
2007-12-13 20:19:52 +00:00
|
|
|
assert( ( handle->state == TR_UPNP_IDLE )
|
2008-09-23 19:11:04 +00:00
|
|
|
|| ( handle->state == TR_UPNP_ERR )
|
|
|
|
|| ( handle->state == TR_UPNP_DISCOVER ) );
|
2007-12-08 19:34:15 +00:00
|
|
|
|
2007-11-09 16:10:48 +00:00
|
|
|
if( handle->hasDiscovered )
|
|
|
|
FreeUPNPUrls( &handle->urls );
|
2007-11-06 16:02:50 +00:00
|
|
|
tr_free( handle );
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
2007-11-06 16:02:50 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
2007-04-15 07:36:24 +00:00
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_upnpPulse( tr_upnp * handle,
|
|
|
|
int port,
|
|
|
|
int isEnabled )
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2007-12-08 19:34:15 +00:00
|
|
|
int ret;
|
|
|
|
|
2007-12-13 18:56:22 +00:00
|
|
|
if( isEnabled && ( handle->state == TR_UPNP_DISCOVER ) )
|
2007-11-09 16:10:48 +00:00
|
|
|
{
|
2007-12-13 02:48:30 +00:00
|
|
|
struct UPNPDev * devlist;
|
|
|
|
errno = 0;
|
2008-10-04 15:23:47 +00:00
|
|
|
devlist = upnpDiscover( 2000, NULL, NULL, 0 );
|
2008-09-23 19:11:04 +00:00
|
|
|
if( devlist == NULL )
|
|
|
|
{
|
|
|
|
tr_ndbg(
|
|
|
|
getKey( ), "upnpDiscover failed (errno %d - %s)", errno,
|
|
|
|
tr_strerror( errno ) );
|
2007-12-13 02:48:30 +00:00
|
|
|
}
|
2007-12-09 17:50:05 +00:00
|
|
|
errno = 0;
|
2008-09-23 19:11:04 +00:00
|
|
|
if( UPNP_GetValidIGD( devlist, &handle->urls, &handle->data,
|
|
|
|
handle->lanaddr, sizeof( handle->lanaddr ) ) )
|
|
|
|
{
|
|
|
|
tr_ninf( getKey( ), _(
|
|
|
|
"Found Internet Gateway Device \"%s\"" ),
|
|
|
|
handle->urls.controlURL );
|
|
|
|
tr_ninf( getKey( ), _(
|
|
|
|
"Local Address is \"%s\"" ), handle->lanaddr );
|
2007-12-08 19:34:15 +00:00
|
|
|
handle->state = TR_UPNP_IDLE;
|
|
|
|
handle->hasDiscovered = 1;
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-12-08 19:34:15 +00:00
|
|
|
handle->state = TR_UPNP_ERR;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_ndbg(
|
|
|
|
getKey( ), "UPNP_GetValidIGD failed (errno %d - %s)",
|
|
|
|
errno,
|
|
|
|
tr_strerror( errno ) );
|
|
|
|
tr_ndbg(
|
|
|
|
getKey( ),
|
|
|
|
"If your router supports UPnP, please make sure UPnP is enabled!" );
|
2007-11-09 16:10:48 +00:00
|
|
|
}
|
|
|
|
freeUPNPDevlist( devlist );
|
|
|
|
}
|
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
if( handle->state == TR_UPNP_IDLE )
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2007-12-08 19:34:15 +00:00
|
|
|
if( handle->isMapped && ( !isEnabled || ( handle->port != port ) ) )
|
|
|
|
handle->state = TR_UPNP_UNMAP;
|
2007-06-10 22:26:59 +00:00
|
|
|
}
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
if( handle->state == TR_UPNP_UNMAP )
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2007-11-06 16:02:50 +00:00
|
|
|
char portStr[16];
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_snprintf( portStr, sizeof( portStr ), "%d", handle->port );
|
2007-11-06 16:02:50 +00:00
|
|
|
UPNP_DeletePortMapping( handle->urls.controlURL,
|
|
|
|
handle->data.servicetype,
|
|
|
|
portStr, "TCP" );
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_ninf( getKey( ),
|
|
|
|
_(
|
|
|
|
"Stopping port forwarding through \"%s\", service \"%s\"" ),
|
|
|
|
handle->urls.controlURL, handle->data.servicetype );
|
2007-12-08 19:34:15 +00:00
|
|
|
handle->isMapped = 0;
|
|
|
|
handle->state = TR_UPNP_IDLE;
|
|
|
|
handle->port = -1;
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
if( handle->state == TR_UPNP_IDLE )
|
|
|
|
{
|
|
|
|
if( isEnabled && !handle->isMapped )
|
|
|
|
handle->state = TR_UPNP_MAP;
|
|
|
|
}
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
if( handle->state == TR_UPNP_MAP )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int err = -1;
|
2007-12-09 17:50:05 +00:00
|
|
|
errno = 0;
|
2007-12-24 07:02:40 +00:00
|
|
|
|
|
|
|
if( !handle->urls.controlURL || !handle->data.servicetype )
|
|
|
|
handle->isMapped = 0;
|
2008-09-23 19:11:04 +00:00
|
|
|
else
|
|
|
|
{
|
2008-12-10 06:12:04 +00:00
|
|
|
char portStr[16];
|
|
|
|
char desc[64];
|
|
|
|
tr_snprintf( portStr, sizeof( portStr ), "%d", port );
|
|
|
|
tr_snprintf( desc, sizeof( desc ), "%s at %d", TR_NAME, port );
|
2007-12-24 07:02:40 +00:00
|
|
|
err = UPNP_AddPortMapping( handle->urls.controlURL,
|
|
|
|
handle->data.servicetype,
|
|
|
|
portStr, portStr, handle->lanaddr,
|
2008-12-10 06:12:04 +00:00
|
|
|
desc, "TCP" );
|
2007-12-24 07:02:40 +00:00
|
|
|
handle->isMapped = !err;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_ninf( getKey( ),
|
|
|
|
_(
|
|
|
|
"Port forwarding through \"%s\", service \"%s\". (local address: %s:%d)" ),
|
|
|
|
handle->urls.controlURL, handle->data.servicetype,
|
|
|
|
handle->lanaddr, port );
|
|
|
|
if( handle->isMapped )
|
|
|
|
{
|
|
|
|
tr_ninf( getKey( ), _( "Port forwarding successful!" ) );
|
2007-12-08 19:34:15 +00:00
|
|
|
handle->port = port;
|
|
|
|
handle->state = TR_UPNP_IDLE;
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_ndbg(
|
|
|
|
getKey( ),
|
|
|
|
"Port forwarding failed with error %d (errno %d - %s)", err,
|
|
|
|
errno, tr_strerror( errno ) );
|
|
|
|
tr_ndbg(
|
|
|
|
getKey( ),
|
|
|
|
"If your router supports UPnP, please make sure UPnP is enabled!" );
|
2007-12-08 19:34:15 +00:00
|
|
|
handle->port = -1;
|
|
|
|
handle->state = TR_UPNP_ERR;
|
|
|
|
}
|
|
|
|
}
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2007-12-13 18:56:22 +00:00
|
|
|
switch( handle->state )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
case TR_UPNP_DISCOVER:
|
|
|
|
ret = TR_PORT_UNMAPPED; break;
|
|
|
|
|
|
|
|
case TR_UPNP_MAP:
|
|
|
|
ret = TR_PORT_MAPPING; break;
|
|
|
|
|
|
|
|
case TR_UPNP_UNMAP:
|
|
|
|
ret = TR_PORT_UNMAPPING; break;
|
|
|
|
|
|
|
|
case TR_UPNP_IDLE:
|
|
|
|
ret = handle->isMapped ? TR_PORT_MAPPED
|
|
|
|
: TR_PORT_UNMAPPED; 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
|
|
|
|