transmission/libtransmission/upnp.c

148 lines
3.6 KiB
C
Raw Normal View History

/*
* This file Copyright (C) 2007 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 <stdio.h> /* printf */
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 "utils.h"
#include "upnp.h"
2006-09-25 18:37:45 +00:00
struct tr_upnp
2006-09-25 18:37:45 +00:00
{
struct UPNPUrls urls;
struct IGDdatas data;
int port;
char lanaddr[16];
unsigned int isForwarding : 1;
unsigned int isEnabled : 1;
unsigned int hasDiscovered : 1;
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->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
{
tr_upnpStop( handle );
if( handle->hasDiscovered )
FreeUPNPUrls( &handle->urls );
tr_free( handle );
2006-09-25 18:37:45 +00:00
}
/**
***
**/
2006-09-25 18:37:45 +00:00
void
tr_upnpStart( tr_upnp * handle )
2006-09-25 18:37:45 +00:00
{
if( !handle->hasDiscovered )
{
struct UPNPDev * devlist = upnpDiscover( 2000, NULL );
if( UPNP_GetValidIGD( devlist, &handle->urls, &handle->data, handle->lanaddr, sizeof(handle->lanaddr))) {
2007-11-12 22:26:10 +00:00
tr_dbg( "UPNP: Found Internet Gateway Device '%s'", handle->urls.controlURL );
tr_dbg( "UPNP: Local LAN IP Address is '%s'", handle->lanaddr );
}
freeUPNPDevlist( devlist );
handle->hasDiscovered = 1;
}
handle->isEnabled = 1;
2006-09-25 18:37:45 +00:00
if( handle->port >= 0 )
2006-09-25 18:37:45 +00:00
{
char portStr[16];
snprintf( portStr, sizeof(portStr), "%d", handle->port );
handle->isForwarding = ( handle->urls.controlURL != NULL ) &&
( handle->data.servicetype != NULL ) &&
( UPNP_AddPortMapping( handle->urls.controlURL,
handle->data.servicetype,
portStr, portStr, handle->lanaddr,
"Transmission", "TCP" ) );
2006-09-25 18:37:45 +00:00
2007-11-12 22:26:10 +00:00
tr_inf( "UPNP: Port Forwarding via '%s', service '%s'. (local address: %s:%d)",
handle->urls.controlURL, handle->data.servicetype, handle->lanaddr, handle->port );
2007-11-12 22:26:10 +00:00
tr_inf( "UPNP: Port Forwarding Enabled? %s", (handle->isForwarding?"Yes":"No") );
}
2006-09-25 18:37:45 +00:00
}
void
tr_upnpRemoveForwarding ( tr_upnp * handle )
2006-09-25 18:37:45 +00:00
{
handle->port = -1;
2006-09-25 18:37:45 +00:00
if( handle->isForwarding )
2006-09-25 18:37:45 +00:00
{
char portStr[16];
snprintf( portStr, sizeof(portStr), "%d", handle->port );
2006-09-25 18:37:45 +00:00
UPNP_DeletePortMapping( handle->urls.controlURL,
handle->data.servicetype,
portStr, "TCP" );
tr_dbg( "Stopping port forwarding of '%s', service '%s'",
handle->urls.controlURL, handle->data.servicetype );
2006-09-25 18:37:45 +00:00
handle->isForwarding = FALSE;
2006-09-25 18:37:45 +00:00
}
}
void
tr_upnpForwardPort( tr_upnp * handle, int publicPort )
2006-09-25 18:37:45 +00:00
{
tr_upnpRemoveForwarding( handle ); /* remove the old forwarding */
2006-09-25 18:37:45 +00:00
handle->port = publicPort;
2006-09-25 18:37:45 +00:00
if( handle->isEnabled )
tr_upnpStart( handle );
2006-09-25 18:37:45 +00:00
}
void
tr_upnpStop( tr_upnp * handle )
2006-09-25 18:37:45 +00:00
{
tr_upnpRemoveForwarding( handle );
handle->isEnabled = 0;
2006-09-25 18:37:45 +00:00
}
int
tr_upnpStatus( tr_upnp * handle )
2006-09-25 18:37:45 +00:00
{
if( !handle->isEnabled )
return TR_NAT_TRAVERSAL_DISABLED;
2006-09-25 18:37:45 +00:00
if( !handle->isForwarding )
return TR_NAT_TRAVERSAL_ERROR;
2006-09-25 18:37:45 +00:00
return TR_NAT_TRAVERSAL_MAPPED;
2006-09-25 18:37:45 +00:00
}
void
tr_upnpPulse( tr_upnp * handle UNUSED )
2006-09-25 18:37:45 +00:00
{
/* no-op */
2006-09-25 18:37:45 +00:00
}