2008-07-22 23:28:28 +00:00
|
|
|
/*
|
2009-01-10 23:09:07 +00:00
|
|
|
* This file Copyright (C) 2008-2009 Charles Kerr <charles@transmissionbt.com>
|
2007-01-19 04:42:31 +00:00
|
|
|
*
|
2008-07-22 23:28:28 +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.
|
2008-07-22 23:28:28 +00:00
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
2007-01-19 04:42:31 +00:00
|
|
|
*
|
2008-07-22 23:28:28 +00:00
|
|
|
* $Id$
|
|
|
|
*/
|
2007-01-19 04:42:31 +00:00
|
|
|
|
2008-01-10 18:52:46 +00:00
|
|
|
#include <errno.h>
|
2007-07-29 18:11:21 +00:00
|
|
|
#include <string.h>
|
2007-09-20 16:32:01 +00:00
|
|
|
#include <stdio.h>
|
2007-07-29 18:11:21 +00:00
|
|
|
|
2007-07-14 16:29:21 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2007-07-09 20:10:42 +00:00
|
|
|
#include "transmission.h"
|
|
|
|
#include "natpmp.h"
|
|
|
|
#include "net.h"
|
2008-08-16 21:06:57 +00:00
|
|
|
#include "peer-io.h"
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "peer-mgr.h"
|
2008-04-11 17:01:13 +00:00
|
|
|
#include "port-forwarding.h"
|
2009-07-01 14:58:57 +00:00
|
|
|
#include "session.h"
|
2007-12-25 05:37:32 +00:00
|
|
|
#include "torrent.h"
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "trevent.h"
|
2007-07-09 20:10:42 +00:00
|
|
|
#include "upnp.h"
|
2007-07-30 18:04:10 +00:00
|
|
|
#include "utils.h"
|
2007-01-19 04:42:31 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
static const char *
|
|
|
|
getKey( void ) { return _( "Port Forwarding" ); }
|
2008-03-07 03:26:59 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
struct tr_shared
|
2007-01-19 04:42:31 +00:00
|
|
|
{
|
2008-11-28 22:11:41 +00:00
|
|
|
tr_bool isEnabled;
|
|
|
|
tr_bool isShuttingDown;
|
2009-05-14 13:42:29 +00:00
|
|
|
tr_bool doPortCheck;
|
2008-04-12 21:47:10 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_port_forwarding natpmpStatus;
|
|
|
|
tr_port_forwarding upnpStatus;
|
2007-01-19 04:42:31 +00:00
|
|
|
|
2008-11-21 16:32:55 +00:00
|
|
|
tr_upnp * upnp;
|
|
|
|
tr_natpmp * natpmp;
|
|
|
|
tr_session * session;
|
2009-05-14 13:42:29 +00:00
|
|
|
|
|
|
|
struct event * timer;
|
2007-01-19 04:42:31 +00:00
|
|
|
};
|
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2007-01-19 04:42:31 +00:00
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
static const char*
|
|
|
|
getNatStateStr( int state )
|
2007-01-19 04:42:31 +00:00
|
|
|
{
|
2007-12-08 19:34:15 +00:00
|
|
|
switch( state )
|
|
|
|
{
|
2009-05-14 13:42:29 +00:00
|
|
|
case TR_PORT_MAPPING: return _( "Starting" );
|
|
|
|
case TR_PORT_MAPPED: return _( "Forwarded" );
|
|
|
|
case TR_PORT_UNMAPPING: return _( "Stopping" );
|
|
|
|
case TR_PORT_UNMAPPED: return _( "Not forwarded" );
|
|
|
|
default: return "???";
|
2007-12-08 19:34:15 +00:00
|
|
|
}
|
2007-01-19 04:42:31 +00:00
|
|
|
}
|
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
static void
|
2009-04-22 16:00:45 +00:00
|
|
|
natPulse( tr_shared * s, tr_bool doPortCheck )
|
2007-01-19 04:42:31 +00:00
|
|
|
{
|
2009-05-14 13:42:29 +00:00
|
|
|
const tr_port port = s->session->peerPort;
|
|
|
|
const int isEnabled = s->isEnabled && !s->isShuttingDown;
|
|
|
|
int oldStatus;
|
|
|
|
int newStatus;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-05-26 20:52:08 +00:00
|
|
|
if( s->natpmp == NULL )
|
|
|
|
s->natpmp = tr_natpmpInit( );
|
|
|
|
if( s->upnp == NULL )
|
|
|
|
s->upnp = tr_upnpInit( );
|
|
|
|
|
2008-04-12 21:47:10 +00:00
|
|
|
oldStatus = tr_sharedTraversalStatus( s );
|
|
|
|
s->natpmpStatus = tr_natpmpPulse( s->natpmp, port, isEnabled );
|
2009-04-22 16:00:45 +00:00
|
|
|
s->upnpStatus = tr_upnpPulse( s->upnp, port, isEnabled, doPortCheck );
|
2008-04-12 21:47:10 +00:00
|
|
|
newStatus = tr_sharedTraversalStatus( s );
|
|
|
|
|
|
|
|
if( newStatus != oldStatus )
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_ninf( getKey( ), _( "State changed from \"%1$s\" to \"%2$s\"" ),
|
|
|
|
getNatStateStr( oldStatus ),
|
|
|
|
getNatStateStr( newStatus ) );
|
2007-01-19 04:42:31 +00:00
|
|
|
}
|
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
static void
|
2009-10-29 17:25:03 +00:00
|
|
|
set_evtimer_from_status( tr_shared * s )
|
2007-01-19 04:42:31 +00:00
|
|
|
{
|
2009-06-15 03:24:40 +00:00
|
|
|
int sec=0, msec=0;
|
2007-09-21 05:31:29 +00:00
|
|
|
|
2009-05-14 13:42:29 +00:00
|
|
|
/* when to wake up again */
|
|
|
|
switch( tr_sharedTraversalStatus( s ) )
|
2008-12-15 00:17:08 +00:00
|
|
|
{
|
2009-05-14 13:42:29 +00:00
|
|
|
case TR_PORT_MAPPED:
|
|
|
|
/* if we're mapped, everything is fine... check back in 20 minutes
|
|
|
|
* to renew the port forwarding if it's expired */
|
|
|
|
s->doPortCheck = TRUE;
|
2009-06-15 03:24:40 +00:00
|
|
|
sec = 60 * 20;
|
2009-05-14 13:42:29 +00:00
|
|
|
break;
|
2007-12-08 19:34:15 +00:00
|
|
|
|
2009-05-14 13:42:29 +00:00
|
|
|
case TR_PORT_ERROR:
|
|
|
|
/* some kind of an error. wait 60 seconds and retry */
|
2009-06-15 03:24:40 +00:00
|
|
|
sec = 60;
|
2009-05-14 13:42:29 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* in progress. pulse frequently. */
|
2009-06-15 03:24:40 +00:00
|
|
|
msec = 333000;
|
2009-05-14 13:42:29 +00:00
|
|
|
break;
|
2007-01-19 04:42:31 +00:00
|
|
|
}
|
|
|
|
|
2009-10-30 03:20:53 +00:00
|
|
|
if( s->timer != NULL )
|
|
|
|
tr_timerAdd( s->timer, sec, msec );
|
2009-04-22 16:00:45 +00:00
|
|
|
}
|
|
|
|
|
2009-10-29 17:25:03 +00:00
|
|
|
static void
|
|
|
|
onTimer( int fd UNUSED, short what UNUSED, void * vshared )
|
|
|
|
{
|
|
|
|
tr_shared * s = vshared;
|
|
|
|
|
|
|
|
assert( s );
|
|
|
|
assert( s->timer );
|
|
|
|
|
|
|
|
/* do something */
|
|
|
|
natPulse( s, s->doPortCheck );
|
|
|
|
s->doPortCheck = FALSE;
|
|
|
|
|
|
|
|
/* set up the timer for the next pulse */
|
|
|
|
set_evtimer_from_status( s );
|
|
|
|
}
|
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2007-01-19 04:42:31 +00:00
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
tr_shared *
|
2009-10-23 03:41:36 +00:00
|
|
|
tr_sharedInit( tr_session * session )
|
2007-01-19 04:42:31 +00:00
|
|
|
{
|
2007-12-08 19:34:15 +00:00
|
|
|
tr_shared * s = tr_new0( tr_shared, 1 );
|
|
|
|
|
2008-11-21 16:32:55 +00:00
|
|
|
s->session = session;
|
2009-10-23 03:41:36 +00:00
|
|
|
s->isEnabled = FALSE;
|
2008-05-22 12:31:47 +00:00
|
|
|
s->upnpStatus = TR_PORT_UNMAPPED;
|
|
|
|
s->natpmpStatus = TR_PORT_UNMAPPED;
|
2007-01-19 04:42:31 +00:00
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
#if 0
|
2009-05-14 13:42:29 +00:00
|
|
|
if( isEnabled )
|
2009-05-20 17:35:41 +00:00
|
|
|
{
|
|
|
|
s->timer = tr_new0( struct event, 1 );
|
|
|
|
evtimer_set( s->timer, onTimer, s );
|
2009-06-15 03:24:40 +00:00
|
|
|
tr_timerAdd( s->timer, 0, 333000 );
|
2009-05-20 17:35:41 +00:00
|
|
|
}
|
2009-10-23 03:41:36 +00:00
|
|
|
#endif
|
2009-05-14 13:42:29 +00:00
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
return s;
|
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2009-05-14 13:42:29 +00:00
|
|
|
static void
|
|
|
|
stop_timer( tr_shared * s )
|
2007-12-08 19:34:15 +00:00
|
|
|
{
|
2009-05-14 13:42:29 +00:00
|
|
|
if( s->timer != NULL )
|
|
|
|
{
|
|
|
|
evtimer_del( s->timer );
|
|
|
|
tr_free( s->timer );
|
|
|
|
s->timer = NULL;
|
|
|
|
}
|
2007-01-19 04:42:31 +00:00
|
|
|
}
|
|
|
|
|
2009-05-14 13:42:29 +00:00
|
|
|
static void
|
|
|
|
stop_forwarding( tr_shared * s )
|
2007-01-27 21:17:10 +00:00
|
|
|
{
|
2009-06-23 14:15:23 +00:00
|
|
|
tr_ninf( getKey( ), "%s", _( "Stopped" ) );
|
2009-05-14 13:42:29 +00:00
|
|
|
natPulse( s, FALSE );
|
|
|
|
tr_natpmpClose( s->natpmp );
|
2009-05-26 20:52:08 +00:00
|
|
|
s->natpmp = NULL;
|
2009-05-14 13:42:29 +00:00
|
|
|
tr_upnpClose( s->upnp );
|
2009-05-26 20:52:08 +00:00
|
|
|
s->upnp = NULL;
|
2009-05-14 13:42:29 +00:00
|
|
|
stop_timer( s );
|
|
|
|
}
|
2007-01-27 21:17:10 +00:00
|
|
|
|
2009-05-14 13:42:29 +00:00
|
|
|
void
|
|
|
|
tr_sharedClose( tr_session * session )
|
|
|
|
{
|
|
|
|
tr_shared * s = session->shared;
|
2007-01-27 21:17:10 +00:00
|
|
|
|
2009-05-14 13:42:29 +00:00
|
|
|
s->isShuttingDown = TRUE;
|
|
|
|
stop_forwarding( s );
|
|
|
|
s->session->shared = NULL;
|
|
|
|
tr_free( s );
|
2007-01-27 21:17:10 +00:00
|
|
|
}
|
|
|
|
|
2009-05-20 17:35:41 +00:00
|
|
|
static void
|
|
|
|
start_timer( tr_shared * s )
|
|
|
|
{
|
|
|
|
s->timer = tr_new0( struct event, 1 );
|
|
|
|
evtimer_set( s->timer, onTimer, s );
|
2009-10-29 17:25:03 +00:00
|
|
|
set_evtimer_from_status( s );
|
2009-05-20 17:35:41 +00:00
|
|
|
}
|
|
|
|
|
2009-05-14 13:42:29 +00:00
|
|
|
void
|
|
|
|
tr_sharedTraversalEnable( tr_shared * s, tr_bool isEnabled )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2009-05-14 13:42:29 +00:00
|
|
|
if(( s->isEnabled = isEnabled ))
|
|
|
|
start_timer( s );
|
|
|
|
else
|
|
|
|
stop_forwarding( s );
|
2007-12-08 19:34:15 +00:00
|
|
|
}
|
2007-01-19 04:42:31 +00:00
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
void
|
2009-05-14 13:42:29 +00:00
|
|
|
tr_sharedPortChanged( tr_session * session )
|
2007-12-08 19:34:15 +00:00
|
|
|
{
|
2009-05-14 13:42:29 +00:00
|
|
|
tr_shared * s = session->shared;
|
|
|
|
|
|
|
|
if( s->isEnabled )
|
|
|
|
{
|
|
|
|
stop_timer( s );
|
|
|
|
start_timer( s );
|
|
|
|
}
|
2007-12-08 19:34:15 +00:00
|
|
|
}
|
2007-01-19 04:42:31 +00:00
|
|
|
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_bool
|
2008-05-12 16:33:17 +00:00
|
|
|
tr_sharedTraversalIsEnabled( const tr_shared * s )
|
|
|
|
{
|
|
|
|
return s->isEnabled;
|
|
|
|
}
|
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
int
|
|
|
|
tr_sharedTraversalStatus( const tr_shared * s )
|
|
|
|
{
|
2008-04-12 21:47:10 +00:00
|
|
|
return MAX( s->natpmpStatus, s->upnpStatus );
|
2007-01-19 04:42:31 +00:00
|
|
|
}
|