2008-07-22 23:28:28 +00:00
|
|
|
/*
|
|
|
|
* This file Copyright (C) 2008 Charles Kerr <charles@rebelbase.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"
|
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-09-23 19:11:04 +00:00
|
|
|
unsigned int isEnabled : 1;
|
|
|
|
unsigned int isShuttingDown : 1;
|
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-09-23 19:11:04 +00:00
|
|
|
int bindPort;
|
|
|
|
int bindSocket;
|
|
|
|
int publicPort;
|
2008-04-12 21:47:10 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_handle * h;
|
|
|
|
tr_timer * pulseTimer;
|
2008-04-12 21:47:10 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_upnp * upnp;
|
|
|
|
tr_natpmp * natpmp;
|
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 )
|
|
|
|
{
|
2008-03-08 01:43:20 +00:00
|
|
|
/* we're in the process of trying to set up port forwarding */
|
2008-09-23 19:11:04 +00:00
|
|
|
case TR_PORT_MAPPING:
|
|
|
|
return _( "Starting" );
|
|
|
|
|
2008-03-08 01:43:20 +00:00
|
|
|
/* we've successfully forwarded the port */
|
2008-09-23 19:11:04 +00:00
|
|
|
case TR_PORT_MAPPED:
|
|
|
|
return _( "Forwarded" );
|
|
|
|
|
2008-03-08 01:43:20 +00:00
|
|
|
/* we're cancelling the port forwarding */
|
2008-09-23 19:11:04 +00:00
|
|
|
case TR_PORT_UNMAPPING:
|
|
|
|
return _( "Stopping" );
|
|
|
|
|
2008-03-08 01:43:20 +00:00
|
|
|
/* the port isn't forwarded */
|
2008-09-23 19:11:04 +00:00
|
|
|
case TR_PORT_UNMAPPED:
|
|
|
|
return _( "Not forwarded" );
|
|
|
|
|
|
|
|
case TR_PORT_ERROR:
|
|
|
|
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
|
|
|
return "notfound";
|
2007-01-19 04:42:31 +00:00
|
|
|
}
|
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
static void
|
|
|
|
natPulse( tr_shared * s )
|
2007-01-19 04:42:31 +00:00
|
|
|
{
|
2007-12-08 19:34:15 +00:00
|
|
|
const int port = s->publicPort;
|
|
|
|
const int isEnabled = s->isEnabled && !s->isShuttingDown;
|
2008-09-23 19:11:04 +00:00
|
|
|
int oldStatus;
|
|
|
|
int newStatus;
|
|
|
|
|
2008-04-12 21:47:10 +00:00
|
|
|
oldStatus = tr_sharedTraversalStatus( s );
|
|
|
|
s->natpmpStatus = tr_natpmpPulse( s->natpmp, port, isEnabled );
|
|
|
|
s->upnpStatus = tr_upnpPulse( s->upnp, port, isEnabled );
|
|
|
|
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
|
2007-12-09 00:28:34 +00:00
|
|
|
incomingPeersPulse( tr_shared * s )
|
2007-01-19 04:42:31 +00:00
|
|
|
{
|
2007-12-09 00:28:34 +00:00
|
|
|
if( s->bindSocket >= 0 && ( s->bindPort != s->publicPort ) )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_ninf( getKey( ), _( "Closing port %d" ), s->bindPort );
|
2007-12-09 00:28:34 +00:00
|
|
|
tr_netClose( s->bindSocket );
|
|
|
|
s->bindSocket = -1;
|
|
|
|
}
|
|
|
|
|
2008-04-06 16:50:41 +00:00
|
|
|
if( ( s->publicPort > 0 ) && ( s->bindPort != s->publicPort ) )
|
2007-12-09 00:28:34 +00:00
|
|
|
{
|
|
|
|
int socket;
|
|
|
|
errno = 0;
|
|
|
|
socket = tr_netBindTCP( s->publicPort );
|
2008-09-23 19:11:04 +00:00
|
|
|
if( socket >= 0 )
|
|
|
|
{
|
|
|
|
tr_ninf( getKey( ),
|
|
|
|
_(
|
|
|
|
"Opened port %d to listen for incoming peer connections" ),
|
|
|
|
s->publicPort );
|
2007-12-09 00:28:34 +00:00
|
|
|
s->bindPort = s->publicPort;
|
|
|
|
s->bindSocket = socket;
|
|
|
|
listen( s->bindSocket, 5 );
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_nerr( getKey( ),
|
|
|
|
_(
|
|
|
|
"Couldn't open port %d to listen for incoming peer connections (errno %d - %s)" ),
|
|
|
|
s->publicPort, errno, tr_strerror( errno ) );
|
2007-12-09 00:28:34 +00:00
|
|
|
s->bindPort = -1;
|
|
|
|
s->bindSocket = -1;
|
|
|
|
}
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
for( ; ; ) /* check for new incoming peer connections */
|
2007-01-19 04:42:31 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int socket;
|
|
|
|
uint16_t port;
|
2007-12-08 19:34:15 +00:00
|
|
|
struct in_addr addr;
|
2007-01-19 04:42:31 +00:00
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
if( s->bindSocket < 0 )
|
|
|
|
break;
|
2007-09-21 05:31:29 +00:00
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
socket = tr_netAccept( s->bindSocket, &addr, &port );
|
|
|
|
if( socket < 0 )
|
|
|
|
break;
|
2007-09-21 05:31:29 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_deepLog( __FILE__, __LINE__, NULL,
|
|
|
|
"New INCOMING connection %d (%s)",
|
|
|
|
socket, tr_peerIoAddrStr( &addr,
|
|
|
|
port ) );
|
2008-08-16 21:06:57 +00:00
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
tr_peerMgrAddIncoming( s->h->peerMgr, &addr, port, socket );
|
2007-01-19 04:42:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
static int
|
|
|
|
sharedPulse( void * vshared )
|
2007-01-27 21:17:10 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int keepPulsing = 1;
|
2007-12-08 19:34:15 +00:00
|
|
|
tr_shared * shared = vshared;
|
2007-01-27 21:17:10 +00:00
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
natPulse( shared );
|
|
|
|
|
|
|
|
if( !shared->isShuttingDown )
|
2007-01-19 04:42:31 +00:00
|
|
|
{
|
2007-12-09 00:28:34 +00:00
|
|
|
incomingPeersPulse( shared );
|
2007-01-19 04:42:31 +00:00
|
|
|
}
|
2008-04-01 19:52:21 +00:00
|
|
|
else
|
2007-01-19 04:42:31 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_ninf( getKey( ), _( "Stopped" ) );
|
2008-04-01 19:52:21 +00:00
|
|
|
tr_timerFree( &shared->pulseTimer );
|
2007-12-08 19:34:15 +00:00
|
|
|
tr_netClose( shared->bindSocket );
|
|
|
|
tr_natpmpClose( shared->natpmp );
|
|
|
|
tr_upnpClose( shared->upnp );
|
2008-04-01 19:52:21 +00:00
|
|
|
shared->h->shared = NULL;
|
2007-12-08 19:34:15 +00:00
|
|
|
tr_free( shared );
|
|
|
|
keepPulsing = 0;
|
2007-01-19 04:42:31 +00:00
|
|
|
}
|
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
return keepPulsing;
|
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
|
|
|
tr_shared *
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_sharedInit( tr_handle * h,
|
|
|
|
int isEnabled,
|
|
|
|
int publicPort )
|
2007-01-19 04:42:31 +00:00
|
|
|
{
|
2007-12-08 19:34:15 +00:00
|
|
|
tr_shared * s = tr_new0( tr_shared, 1 );
|
|
|
|
|
|
|
|
s->h = h;
|
2007-12-24 07:02:40 +00:00
|
|
|
s->publicPort = publicPort;
|
2007-12-08 19:34:15 +00:00
|
|
|
s->bindPort = -1;
|
|
|
|
s->bindSocket = -1;
|
2008-09-23 19:11:04 +00:00
|
|
|
s->natpmp = tr_natpmpInit( );
|
|
|
|
s->upnp = tr_upnpInit( );
|
2008-05-06 15:52:57 +00:00
|
|
|
s->pulseTimer = tr_timerNew( h, sharedPulse, s, 1000 );
|
2007-12-24 07:02:40 +00:00
|
|
|
s->isEnabled = isEnabled ? 1 : 0;
|
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
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
return s;
|
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
void
|
|
|
|
tr_sharedShuttingDown( tr_shared * s )
|
|
|
|
{
|
|
|
|
s->isShuttingDown = 1;
|
2007-01-19 04:42:31 +00:00
|
|
|
}
|
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_sharedSetPort( tr_shared * s,
|
|
|
|
int port )
|
2007-01-27 21:17:10 +00:00
|
|
|
{
|
2008-05-10 00:19:00 +00:00
|
|
|
tr_torrent * tor = NULL;
|
2007-01-27 21:17:10 +00:00
|
|
|
|
|
|
|
s->publicPort = port;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
while( ( tor = tr_torrentNext( s->h, tor ) ) )
|
2007-09-21 05:31:29 +00:00
|
|
|
tr_torrentChangeMyPort( tor );
|
2007-01-27 21:17:10 +00:00
|
|
|
}
|
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
int
|
2008-05-23 16:18:58 +00:00
|
|
|
tr_sharedGetPeerPort( const tr_shared * s )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2007-12-08 19:34:15 +00:00
|
|
|
return s->publicPort;
|
|
|
|
}
|
2007-01-19 04:42:31 +00:00
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_sharedTraversalEnable( tr_shared * s,
|
|
|
|
int isEnabled )
|
2007-12-08 19:34:15 +00:00
|
|
|
{
|
|
|
|
s->isEnabled = isEnabled;
|
|
|
|
}
|
2007-01-19 04:42:31 +00:00
|
|
|
|
2008-05-12 16:33:17 +00:00
|
|
|
int
|
|
|
|
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
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|