1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-26 09:37:56 +00:00
transmission/libtransmission/upnp.c
2007-11-12 22:26:10 +00:00

147 lines
3.6 KiB
C

/*
* This file Copyright (C) 2007 Charles Kerr <charles@rebelbase.com>
*
* 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.
*
* $Id$
*/
#include <stdio.h> /* printf */
#include <miniupnp/miniwget.h>
#include <miniupnp/miniupnpc.h>
#include <miniupnp/upnpcommands.h>
#include "transmission.h"
#include "internal.h"
#include "utils.h"
#include "upnp.h"
struct tr_upnp
{
struct UPNPUrls urls;
struct IGDdatas data;
int port;
char lanaddr[16];
unsigned int isForwarding : 1;
unsigned int isEnabled : 1;
unsigned int hasDiscovered : 1;
};
/**
***
**/
tr_upnp*
tr_upnpInit( void )
{
tr_upnp * ret = tr_new0( tr_upnp, 1 );
ret->port = -1;
return ret;
}
void
tr_upnpClose( tr_upnp * handle )
{
tr_upnpStop( handle );
if( handle->hasDiscovered )
FreeUPNPUrls( &handle->urls );
tr_free( handle );
}
/**
***
**/
void
tr_upnpStart( tr_upnp * handle )
{
if( !handle->hasDiscovered )
{
struct UPNPDev * devlist = upnpDiscover( 2000, NULL );
if( UPNP_GetValidIGD( devlist, &handle->urls, &handle->data, handle->lanaddr, sizeof(handle->lanaddr))) {
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;
if( handle->port >= 0 )
{
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" ) );
tr_inf( "UPNP: Port Forwarding via '%s', service '%s'. (local address: %s:%d)",
handle->urls.controlURL, handle->data.servicetype, handle->lanaddr, handle->port );
tr_inf( "UPNP: Port Forwarding Enabled? %s", (handle->isForwarding?"Yes":"No") );
}
}
void
tr_upnpRemoveForwarding ( tr_upnp * handle )
{
handle->port = -1;
if( handle->isForwarding )
{
char portStr[16];
snprintf( portStr, sizeof(portStr), "%d", handle->port );
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 );
handle->isForwarding = FALSE;
}
}
void
tr_upnpForwardPort( tr_upnp * handle, int publicPort )
{
tr_upnpRemoveForwarding( handle ); /* remove the old forwarding */
handle->port = publicPort;
if( handle->isEnabled )
tr_upnpStart( handle );
}
void
tr_upnpStop( tr_upnp * handle )
{
tr_upnpRemoveForwarding( handle );
handle->isEnabled = 0;
}
int
tr_upnpStatus( tr_upnp * handle )
{
if( !handle->isEnabled )
return TR_NAT_TRAVERSAL_DISABLED;
if( !handle->isForwarding )
return TR_NAT_TRAVERSAL_ERROR;
return TR_NAT_TRAVERSAL_MAPPED;
}
void
tr_upnpPulse( tr_upnp * handle UNUSED )
{
/* no-op */
}