transmission/libtransmission/upnp.c

173 lines
5.3 KiB
C
Raw Normal View History

/*
2008-01-01 17:20:20 +00:00
* This file Copyright (C) 2007-2008 Charles Kerr <charles@rebelbase.com>
2006-09-25 18:37:45 +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)
* so that the bulk of its code can remain under the MIT license.
* This exemption does not extend to derived works not owned by
* the Transmission project.
2006-09-25 18:37:45 +00:00
*
* $Id$
*/
#include <assert.h>
#include <errno.h>
#include <stdio.h> /* snprintf */
2007-08-04 02:55:06 +00:00
#include <miniupnp/miniwget.h>
#include <miniupnp/miniupnpc.h>
#include <miniupnp/upnpcommands.h>
2006-09-25 18:37:45 +00:00
#include "transmission.h"
#include "internal.h"
#include "shared.h"
#include "utils.h"
#include "upnp.h"
2006-09-25 18:37:45 +00:00
typedef enum
{
TR_UPNP_IDLE,
TR_UPNP_ERR,
TR_UPNP_DISCOVER,
TR_UPNP_MAP,
TR_UPNP_UNMAP
}
tr_upnp_state;
struct tr_upnp
2006-09-25 18:37:45 +00:00
{
struct UPNPUrls urls;
struct IGDdatas data;
int port;
char lanaddr[16];
unsigned int isMapped;
unsigned int hasDiscovered : 1;
tr_upnp_state state;
2006-09-25 18:37:45 +00:00
};
/**
***
**/
tr_upnp*
tr_upnpInit( void )
2006-09-25 18:37:45 +00:00
{
tr_upnp * ret = tr_new0( tr_upnp, 1 );
ret->state = TR_UPNP_DISCOVER;
ret->port = -1;
2006-09-25 18:37:45 +00:00
return ret;
}
void
tr_upnpClose( tr_upnp * handle )
2006-09-25 18:37:45 +00:00
{
assert( !handle->isMapped );
assert( ( handle->state == TR_UPNP_IDLE )
|| ( handle->state == TR_UPNP_ERR )
|| ( handle->state == TR_UPNP_DISCOVER ) );
if( handle->hasDiscovered )
FreeUPNPUrls( &handle->urls );
tr_free( handle );
2006-09-25 18:37:45 +00:00
}
/**
***
**/
int
tr_upnpPulse( tr_upnp * handle, int port, int isEnabled )
2006-09-25 18:37:45 +00:00
{
int ret;
if( isEnabled && ( handle->state == TR_UPNP_DISCOVER ) )
{
struct UPNPDev * devlist;
errno = 0;
2007-12-20 20:18:22 +00:00
devlist = upnpDiscover( 2000, NULL, NULL );
if( devlist == NULL ) {
2008-03-06 21:59:00 +00:00
tr_err( _( "Port Mapping (UPnP): upnpDiscover returned NULL (errno %d - %s)" ), errno, tr_strerror(errno) );
}
errno = 0;
if( UPNP_GetValidIGD( devlist, &handle->urls, &handle->data, handle->lanaddr, sizeof(handle->lanaddr))) {
2008-03-06 21:59:00 +00:00
tr_inf( _( "Port Mapping (UPnP): Found Internet Gateway Device '%s'" ), handle->urls.controlURL );
tr_inf( _( "Port Mapping (UPnP): Local LAN IP Address is '%s'" ), handle->lanaddr );
handle->state = TR_UPNP_IDLE;
handle->hasDiscovered = 1;
} else {
handle->state = TR_UPNP_ERR;
2008-03-06 21:59:00 +00:00
tr_err( _( "Port Mapping (UPnP): UPNP_GetValidIGD failed. (errno %d - %s)" ), errno, tr_strerror(errno) );
tr_err( _( "Port Mapping (UPnP): If your router supports UPnP, please make sure UPnP is enabled!" ) );
}
freeUPNPDevlist( devlist );
}
if( handle->state == TR_UPNP_IDLE )
2006-09-25 18:37:45 +00:00
{
if( handle->isMapped && ( !isEnabled || ( handle->port != port ) ) )
handle->state = TR_UPNP_UNMAP;
}
2006-09-25 18:37:45 +00:00
if( handle->state == TR_UPNP_UNMAP )
2006-09-25 18:37:45 +00:00
{
char portStr[16];
snprintf( portStr, sizeof(portStr), "%d", handle->port );
UPNP_DeletePortMapping( handle->urls.controlURL,
handle->data.servicetype,
portStr, "TCP" );
2008-03-06 21:59:00 +00:00
tr_dbg( "Port Mapping (UPnP): Stopping port forwarding of '%s', service '%s'",
handle->urls.controlURL, handle->data.servicetype );
handle->isMapped = 0;
handle->state = TR_UPNP_IDLE;
handle->port = -1;
2006-09-25 18:37:45 +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
if( handle->state == TR_UPNP_MAP )
{
int err = -1;
char portStr[16];
snprintf( portStr, sizeof(portStr), "%d", port );
errno = 0;
if( !handle->urls.controlURL || !handle->data.servicetype )
handle->isMapped = 0;
else {
err = UPNP_AddPortMapping( handle->urls.controlURL,
handle->data.servicetype,
portStr, portStr, handle->lanaddr,
"Transmission", "TCP" );
handle->isMapped = !err;
}
2008-03-06 21:59:00 +00:00
tr_inf( _( "Port Mapping (UPnP): Port forwarding via '%s', service '%s'. (local address: %s:%d)" ),
2007-12-15 03:25:13 +00:00
handle->urls.controlURL, handle->data.servicetype, handle->lanaddr, port );
if( handle->isMapped ) {
2008-03-06 21:59:00 +00:00
tr_inf( _( "Port Mapping (UPnP): Port forwarding successful!" ) );
handle->port = port;
handle->state = TR_UPNP_IDLE;
} else {
2008-03-06 21:59:00 +00:00
tr_err( _( "Port Mapping (UPnP): Port forwarding failed with err %d (%d - %s)" ), err, errno, tr_strerror(errno) );
tr_err( _( "Port Mapping (UPnP): If your router supports UPnP, please make sure UPnP is enabled!" ) );
handle->port = -1;
handle->state = TR_UPNP_ERR;
}
}
2006-09-25 18:37:45 +00:00
switch( handle->state )
{
case TR_UPNP_DISCOVER: ret = TR_NAT_TRAVERSAL_UNMAPPED; break;
case TR_UPNP_MAP: ret = TR_NAT_TRAVERSAL_MAPPING; break;
case TR_UPNP_UNMAP: ret = TR_NAT_TRAVERSAL_UNMAPPING; break;
case TR_UPNP_IDLE: ret = handle->isMapped ? TR_NAT_TRAVERSAL_MAPPED
: TR_NAT_TRAVERSAL_UNMAPPED; break;
default: ret = TR_NAT_TRAVERSAL_ERROR; break;
}
return ret;
2006-09-25 18:37:45 +00:00
}