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"
|
2008-12-23 17:27:15 +00:00
|
|
|
#include "session.h"
|
2007-07-09 20:10:42 +00:00
|
|
|
#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-11-28 22:11:41 +00:00
|
|
|
tr_bool isEnabled;
|
|
|
|
tr_bool isShuttingDown;
|
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-12-15 00:17:08 +00:00
|
|
|
tr_bool shouldChange;
|
|
|
|
tr_socketList * bindSockets;
|
|
|
|
tr_port publicPort;
|
2008-04-12 21:47:10 +00:00
|
|
|
|
2008-11-21 16:32:55 +00:00
|
|
|
tr_timer * pulseTimer;
|
2008-04-12 21:47:10 +00:00
|
|
|
|
2008-11-21 16:32:55 +00:00
|
|
|
tr_upnp * upnp;
|
|
|
|
tr_natpmp * natpmp;
|
|
|
|
tr_session * session;
|
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
|
|
|
{
|
2008-12-15 00:17:08 +00:00
|
|
|
const tr_port port = s->publicPort;
|
|
|
|
const int isEnabled = s->isEnabled && !s->isShuttingDown;
|
|
|
|
int oldStatus;
|
|
|
|
int newStatus;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-12-15 00:17:08 +00:00
|
|
|
/*
|
|
|
|
* Callbacks for socket list
|
|
|
|
*/
|
2007-12-08 19:34:15 +00:00
|
|
|
static void
|
2008-12-15 00:17:08 +00:00
|
|
|
closeCb( int * const socket,
|
|
|
|
tr_address * const addr,
|
|
|
|
void * const userData )
|
2007-01-19 04:42:31 +00:00
|
|
|
{
|
2008-12-15 00:17:08 +00:00
|
|
|
tr_shared * s = ( tr_shared * )userData;
|
|
|
|
if( *socket >= 0 )
|
2007-12-09 00:28:34 +00:00
|
|
|
{
|
2008-12-15 00:17:08 +00:00
|
|
|
tr_ninf( getKey( ), _( "Closing port %d on %s" ), s->publicPort,
|
|
|
|
tr_ntop_non_ts( addr ) );
|
|
|
|
tr_netClose( *socket );
|
2007-12-09 00:28:34 +00:00
|
|
|
}
|
2008-12-15 00:17:08 +00:00
|
|
|
}
|
2007-12-09 00:28:34 +00:00
|
|
|
|
2008-12-15 00:17:08 +00:00
|
|
|
static void
|
2008-12-15 04:37:44 +00:00
|
|
|
acceptCb( int * const socket,
|
|
|
|
tr_address * const addr UNUSED,
|
|
|
|
void * const userData )
|
2008-12-15 00:17:08 +00:00
|
|
|
{
|
|
|
|
tr_shared * s = ( tr_shared * )userData;
|
|
|
|
tr_address clientAddr;
|
|
|
|
tr_port clientPort;
|
|
|
|
int clientSocket;
|
|
|
|
clientSocket = tr_netAccept( s->session, *socket, &clientAddr, &clientPort );
|
|
|
|
if( clientSocket > 0 )
|
2007-12-09 00:28:34 +00:00
|
|
|
{
|
2008-12-15 00:17:08 +00:00
|
|
|
tr_deepLog( __FILE__, __LINE__, NULL,
|
|
|
|
"New INCOMING connection %d (%s)",
|
|
|
|
clientSocket, tr_peerIoAddrStr( &clientAddr, clientPort ) );
|
|
|
|
|
|
|
|
tr_peerMgrAddIncoming( s->session->peerMgr, &clientAddr, clientPort,
|
|
|
|
clientSocket );
|
2007-12-09 00:28:34 +00:00
|
|
|
}
|
2008-12-15 00:17:08 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-12-15 00:17:08 +00:00
|
|
|
static void
|
|
|
|
bindCb( int * const socket,
|
|
|
|
tr_address * const addr,
|
|
|
|
void * const userData )
|
|
|
|
{
|
|
|
|
tr_shared * s = ( tr_shared * )userData;
|
|
|
|
*socket = tr_netBindTCP( addr, s->publicPort, FALSE );
|
|
|
|
if( *socket >= 0 )
|
2007-01-19 04:42:31 +00:00
|
|
|
{
|
2008-12-15 00:17:08 +00:00
|
|
|
tr_ninf( getKey( ),
|
|
|
|
_( "Opened port %d on %s to listen for incoming peer connections" ),
|
|
|
|
s->publicPort, tr_ntop_non_ts( addr ) );
|
|
|
|
listen( *socket, 10 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_nerr( getKey( ),
|
|
|
|
_(
|
|
|
|
"Couldn't open port %d on %s to listen for incoming peer connections (errno %d - %s)" ),
|
|
|
|
s->publicPort, tr_ntop_non_ts( addr ), errno, tr_strerror( errno ) );
|
|
|
|
}
|
|
|
|
}
|
2007-09-21 05:31:29 +00:00
|
|
|
|
2008-12-15 00:17:08 +00:00
|
|
|
static void
|
|
|
|
incomingPeersPulse( tr_shared * s )
|
|
|
|
{
|
|
|
|
int allPaused;
|
|
|
|
tr_torrent * tor;
|
|
|
|
|
|
|
|
if( s->shouldChange )
|
|
|
|
{
|
|
|
|
tr_socketListForEach( s->bindSockets, &closeCb, s );
|
|
|
|
s->shouldChange = FALSE;
|
|
|
|
if( s->publicPort > 0 )
|
|
|
|
tr_socketListForEach( s->bindSockets, &bindCb, s );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* see if any torrents aren't paused */
|
|
|
|
allPaused = 1;
|
|
|
|
tor = NULL;
|
|
|
|
while( ( tor = tr_torrentNext( s->session, tor ) ) )
|
|
|
|
{
|
|
|
|
if( TR_STATUS_IS_ACTIVE( tr_torrentGetActivity( tor ) ) )
|
|
|
|
{
|
|
|
|
allPaused = 0;
|
2007-12-08 19:34:15 +00:00
|
|
|
break;
|
2008-12-15 00:17:08 +00:00
|
|
|
}
|
2007-01-19 04:42:31 +00:00
|
|
|
}
|
2008-12-15 00:17:08 +00:00
|
|
|
|
|
|
|
/* if we have any running torrents, check for new incoming peer connections */
|
|
|
|
/* (jhujhiti):
|
|
|
|
* This has been changed from a loop that will end when the listener queue
|
|
|
|
* is exhausted to one that will only check for one connection at a time.
|
|
|
|
* I think it unlikely that we get many more than one connection in the
|
|
|
|
* time between pulses (currently one second). However, just to be safe,
|
|
|
|
* I have increased the length of the listener queue from 5 to 10
|
|
|
|
* (see acceptCb() above). */
|
|
|
|
if( !allPaused )
|
|
|
|
tr_socketListForEach( s->bindSockets, &acceptCb, s );
|
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-12-02 22:51:44 +00:00
|
|
|
tr_bool 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 );
|
2008-12-15 00:17:08 +00:00
|
|
|
tr_socketListForEach( shared->bindSockets, &closeCb, shared );
|
2008-12-15 04:34:06 +00:00
|
|
|
tr_socketListFree( shared->bindSockets );
|
2007-12-08 19:34:15 +00:00
|
|
|
tr_natpmpClose( shared->natpmp );
|
|
|
|
tr_upnpClose( shared->upnp );
|
2008-11-21 16:32:55 +00:00
|
|
|
shared->session->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
|
|
|
|
2008-12-15 00:17:08 +00:00
|
|
|
static tr_socketList *
|
|
|
|
setupBindSockets( tr_port port )
|
|
|
|
{
|
2008-12-18 05:55:22 +00:00
|
|
|
tr_net_af_support support = tr_net_getAFSupport( port );
|
2008-12-15 00:17:08 +00:00
|
|
|
tr_socketList * socks = NULL;
|
2008-12-18 05:55:22 +00:00
|
|
|
if( support.has_inet6 )
|
2008-12-15 00:17:08 +00:00
|
|
|
socks = tr_socketListNew( &tr_in6addr_any );
|
2008-12-18 05:55:22 +00:00
|
|
|
if( support.needs_inet4 )
|
2008-12-15 00:17:08 +00:00
|
|
|
{
|
|
|
|
if( socks )
|
|
|
|
tr_socketListAppend( socks, &tr_inaddr_any );
|
|
|
|
else
|
|
|
|
socks = tr_socketListNew( &tr_inaddr_any );
|
|
|
|
}
|
2008-12-18 05:55:22 +00:00
|
|
|
return socks; /* Because the dryer gremlins won't */
|
2008-12-15 00:17:08 +00:00
|
|
|
}
|
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
tr_shared *
|
2008-11-21 16:32:55 +00:00
|
|
|
tr_sharedInit( tr_session * session,
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_bool isEnabled,
|
2008-12-14 22:48:11 +00:00
|
|
|
tr_port 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 );
|
|
|
|
|
2008-11-21 16:32:55 +00:00
|
|
|
s->session = session;
|
2007-12-24 07:02:40 +00:00
|
|
|
s->publicPort = publicPort;
|
2008-12-15 00:17:08 +00:00
|
|
|
s->shouldChange = TRUE;
|
|
|
|
s->bindSockets = setupBindSockets( publicPort );
|
|
|
|
s->shouldChange = TRUE;
|
2008-09-23 19:11:04 +00:00
|
|
|
s->natpmp = tr_natpmpInit( );
|
|
|
|
s->upnp = tr_upnpInit( );
|
2008-11-21 16:32:55 +00:00
|
|
|
s->pulseTimer = tr_timerNew( session, sharedPulse, s, 1000 );
|
2008-12-14 22:48:11 +00:00
|
|
|
s->isEnabled = isEnabled;
|
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-12-14 11:21:11 +00:00
|
|
|
tr_sharedSetPort( tr_shared * s, tr_port 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
|
|
|
|
2008-12-15 00:17:08 +00:00
|
|
|
s->publicPort = port;
|
|
|
|
s->shouldChange = TRUE;
|
2007-01-27 21:17:10 +00:00
|
|
|
|
2008-11-21 16:32:55 +00:00
|
|
|
while( ( tor = tr_torrentNext( s->session, tor ) ) )
|
2007-09-21 05:31:29 +00:00
|
|
|
tr_torrentChangeMyPort( tor );
|
2007-01-27 21:17:10 +00:00
|
|
|
}
|
|
|
|
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_port
|
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-12-14 11:21:11 +00:00
|
|
|
tr_sharedTraversalEnable( tr_shared * s, tr_bool isEnabled )
|
2007-12-08 19:34:15 +00:00
|
|
|
{
|
|
|
|
s->isEnabled = isEnabled;
|
|
|
|
}
|
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
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|