2007-01-19 04:42:31 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
2008-01-01 17:20:20 +00:00
|
|
|
* Copyright (c) 2005-2008 Transmission authors and contributors
|
2007-01-19 04:42:31 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
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"
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "peer-mgr.h"
|
2007-07-09 20:10:42 +00:00
|
|
|
#include "shared.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
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
struct tr_shared
|
2007-01-19 04:42:31 +00:00
|
|
|
{
|
2007-12-08 19:34:15 +00:00
|
|
|
tr_handle * h;
|
|
|
|
tr_timer * pulseTimer;
|
2007-01-19 04:42:31 +00:00
|
|
|
|
|
|
|
/* Incoming connections */
|
2007-09-20 16:32:01 +00:00
|
|
|
int bindPort;
|
|
|
|
int bindSocket;
|
2007-01-19 04:42:31 +00:00
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
/* port forwarding */
|
|
|
|
int isEnabled;
|
|
|
|
int publicPort;
|
|
|
|
tr_nat_traversal_status natStatus;
|
|
|
|
tr_upnp * upnp;
|
2007-11-06 16:02:50 +00:00
|
|
|
tr_natpmp * natpmp;
|
2007-12-08 19:34:15 +00:00
|
|
|
|
|
|
|
int isShuttingDown;
|
2007-01-19 04:42:31 +00:00
|
|
|
};
|
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
#define NATKEY "Port Mapping: "
|
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 )
|
|
|
|
{
|
|
|
|
case TR_NAT_TRAVERSAL_MAPPING: return "mapping";
|
|
|
|
case TR_NAT_TRAVERSAL_MAPPED: return "mapped";
|
|
|
|
case TR_NAT_TRAVERSAL_UNMAPPING: return "unmapping";
|
2008-01-03 05:29:31 +00:00
|
|
|
case TR_NAT_TRAVERSAL_UNMAPPED: return "not mapped";
|
2007-12-08 19:34:15 +00:00
|
|
|
case TR_NAT_TRAVERSAL_ERROR: return "error";
|
|
|
|
}
|
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
|
|
|
tr_nat_traversal_status status;
|
|
|
|
const int port = s->publicPort;
|
|
|
|
const int isEnabled = s->isEnabled && !s->isShuttingDown;
|
|
|
|
|
|
|
|
status = tr_natpmpPulse( s->natpmp, port, isEnabled );
|
|
|
|
if( status == TR_NAT_TRAVERSAL_ERROR )
|
|
|
|
status = tr_upnpPulse( s->upnp, port, isEnabled );
|
|
|
|
if( status != s->natStatus ) {
|
|
|
|
tr_inf( NATKEY "mapping state changed from '%s' to '%s'", getNatStateStr(s->natStatus), getNatStateStr(status) );
|
|
|
|
s->natStatus = status;
|
|
|
|
}
|
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 ) )
|
|
|
|
{
|
|
|
|
tr_inf( NATKEY "closing port %d", s->bindPort );
|
|
|
|
tr_netClose( s->bindSocket );
|
|
|
|
s->bindSocket = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( s->bindPort != s->publicPort )
|
|
|
|
{
|
|
|
|
int socket;
|
|
|
|
errno = 0;
|
|
|
|
socket = tr_netBindTCP( s->publicPort );
|
|
|
|
if( socket >= 0 ) {
|
|
|
|
tr_inf( NATKEY "opened port %d to listen for incoming peer connections", s->publicPort );
|
|
|
|
s->bindPort = s->publicPort;
|
|
|
|
s->bindSocket = socket;
|
|
|
|
listen( s->bindSocket, 5 );
|
|
|
|
} else {
|
|
|
|
tr_err( NATKEY "unable to open port %d to listen for incoming peer connections (errno is %d - %s)",
|
2008-02-19 04:16:04 +00:00
|
|
|
s->publicPort, errno, tr_strerror(errno) );
|
2007-12-09 00:28:34 +00:00
|
|
|
s->bindPort = -1;
|
|
|
|
s->bindSocket = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for( ;; ) /* check for new incoming peer connections */
|
2007-01-19 04:42:31 +00:00
|
|
|
{
|
2007-12-08 19:34:15 +00:00
|
|
|
int socket;
|
|
|
|
uint16_t port;
|
|
|
|
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
|
|
|
|
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
|
|
|
{
|
2007-12-08 19:34:15 +00:00
|
|
|
int keepPulsing = 1;
|
|
|
|
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
|
|
|
}
|
2007-12-08 19:34:15 +00:00
|
|
|
else if( ( shared->natStatus == TR_NAT_TRAVERSAL_ERROR ) || ( shared->natStatus == TR_NAT_TRAVERSAL_UNMAPPED ) )
|
2007-01-19 04:42:31 +00:00
|
|
|
{
|
2007-12-08 19:34:15 +00:00
|
|
|
tr_dbg( NATKEY "port mapping shut down" );
|
|
|
|
shared->h->shared = NULL;
|
|
|
|
tr_netClose( shared->bindSocket );
|
|
|
|
tr_natpmpClose( shared->natpmp );
|
|
|
|
tr_upnpClose( shared->upnp );
|
|
|
|
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 *
|
2007-12-24 07:02:40 +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;
|
|
|
|
s->natpmp = tr_natpmpInit();
|
|
|
|
s->upnp = tr_upnpInit();
|
|
|
|
s->pulseTimer = tr_timerNew( h, sharedPulse, s, 500 );
|
2007-12-24 07:02:40 +00:00
|
|
|
s->isEnabled = isEnabled ? 1 : 0;
|
2007-12-08 19:34:15 +00:00
|
|
|
s->natStatus = TR_NAT_TRAVERSAL_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
|
|
|
|
tr_sharedSetPort( tr_shared * s, int port )
|
2007-01-27 21:17:10 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrent * tor;
|
2007-01-27 21:17:10 +00:00
|
|
|
|
|
|
|
s->publicPort = port;
|
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
for( tor = s->h->torrentList; tor; tor = tor->next )
|
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
|
|
|
|
tr_sharedGetPublicPort( 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
|
|
|
|
tr_sharedTraversalEnable( tr_shared * s, int isEnabled )
|
|
|
|
{
|
|
|
|
s->isEnabled = isEnabled;
|
|
|
|
}
|
2007-01-19 04:42:31 +00:00
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
int
|
|
|
|
tr_sharedTraversalStatus( const tr_shared * s )
|
|
|
|
{
|
|
|
|
return s->natStatus;
|
2007-01-19 04:42:31 +00:00
|
|
|
}
|