2008-07-22 23:28:28 +00:00
|
|
|
/*
|
2010-01-04 21:00:47 +00:00
|
|
|
* This file Copyright (C) 2008-2010 Mnemosyne LLC
|
2006-07-16 19:39:23 +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.
|
2006-07-16 19:39:23 +00:00
|
|
|
*
|
2008-07-22 23:28:28 +00:00
|
|
|
* $Id$
|
|
|
|
*/
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-11-09 20:07:52 +00:00
|
|
|
#include <assert.h>
|
2010-02-07 22:17:42 +00:00
|
|
|
#include <errno.h> /* ENOENT */
|
2007-07-29 18:11:21 +00:00
|
|
|
#include <stdlib.h>
|
2008-02-19 18:39:49 +00:00
|
|
|
#include <string.h> /* memcpy */
|
2007-07-29 18:11:21 +00:00
|
|
|
|
2007-07-12 17:51:45 +00:00
|
|
|
#include <signal.h>
|
2009-07-02 02:37:49 +00:00
|
|
|
#include <sys/types.h> /* stat(), umask() */
|
|
|
|
#include <sys/stat.h> /* stat(), umask() */
|
2007-07-21 18:46:54 +00:00
|
|
|
#include <unistd.h> /* stat */
|
|
|
|
#include <dirent.h> /* opendir */
|
2007-07-29 18:11:21 +00:00
|
|
|
|
2009-03-25 19:50:58 +00:00
|
|
|
#include <event.h>
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#include "transmission.h"
|
2009-09-25 21:05:59 +00:00
|
|
|
#include "announcer.h"
|
2008-11-24 04:21:23 +00:00
|
|
|
#include "bandwidth.h"
|
2008-12-13 23:17:36 +00:00
|
|
|
#include "bencode.h"
|
2008-03-30 13:22:45 +00:00
|
|
|
#include "blocklist.h"
|
2009-04-13 19:04:21 +00:00
|
|
|
#include "crypto.h"
|
2007-07-09 20:10:42 +00:00
|
|
|
#include "fdlimit.h"
|
2007-07-21 18:46:54 +00:00
|
|
|
#include "list.h"
|
2008-04-14 14:39:13 +00:00
|
|
|
#include "metainfo.h" /* tr_metainfoFree */
|
2007-07-09 20:10:42 +00:00
|
|
|
#include "net.h"
|
2009-05-14 13:42:29 +00:00
|
|
|
#include "peer-io.h"
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "peer-mgr.h"
|
2008-02-19 18:39:49 +00:00
|
|
|
#include "platform.h" /* tr_lock */
|
2008-04-11 17:01:13 +00:00
|
|
|
#include "port-forwarding.h"
|
2008-05-18 16:44:30 +00:00
|
|
|
#include "rpc-server.h"
|
2009-07-01 14:58:57 +00:00
|
|
|
#include "session.h"
|
2007-11-21 20:03:53 +00:00
|
|
|
#include "stats.h"
|
2007-12-25 05:37:32 +00:00
|
|
|
#include "torrent.h"
|
2009-07-01 14:58:57 +00:00
|
|
|
#include "tr-dht.h"
|
2007-08-18 03:02:32 +00:00
|
|
|
#include "trevent.h"
|
2007-07-30 18:04:10 +00:00
|
|
|
#include "utils.h"
|
2009-04-16 13:10:25 +00:00
|
|
|
#include "verify.h"
|
2009-07-01 14:58:57 +00:00
|
|
|
#include "version.h"
|
2008-04-24 01:42:53 +00:00
|
|
|
#include "web.h"
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2009-08-13 14:47:56 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
SAVE_INTERVAL_SECS = 120
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-01-02 19:56:06 +00:00
|
|
|
#define dbgmsg( ... ) \
|
|
|
|
do { \
|
|
|
|
if( tr_deepLoggingIsActive( ) ) \
|
|
|
|
tr_deepLog( __FILE__, __LINE__, NULL, __VA_ARGS__ ); \
|
|
|
|
} while( 0 )
|
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
static tr_port
|
|
|
|
getRandomPort( tr_session * s )
|
|
|
|
{
|
|
|
|
return tr_cryptoWeakRandInt( s->randomPortHigh - s->randomPortLow + 1) + s->randomPortLow;
|
|
|
|
}
|
2008-12-11 04:41:23 +00:00
|
|
|
|
2007-07-16 21:57:22 +00:00
|
|
|
/* Generate a peer id : "-TRxyzb-" + 12 random alphanumeric
|
|
|
|
characters, where x is the major version number, y is the
|
|
|
|
minor version number, z is the maintenance number, and b
|
|
|
|
designates beta (Azureus-style) */
|
2008-01-07 06:19:34 +00:00
|
|
|
uint8_t*
|
|
|
|
tr_peerIdNew( void )
|
2007-07-16 11:19:44 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int i;
|
|
|
|
int val;
|
|
|
|
int total = 0;
|
|
|
|
uint8_t * buf = tr_new( uint8_t, 21 );
|
2008-01-07 06:19:34 +00:00
|
|
|
const char * pool = "0123456789abcdefghijklmnopqrstuvwxyz";
|
2008-09-23 19:11:04 +00:00
|
|
|
const int base = 36;
|
2008-01-07 06:19:34 +00:00
|
|
|
|
|
|
|
memcpy( buf, PEERID_PREFIX, 8 );
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 8; i < 19; ++i )
|
|
|
|
{
|
2008-08-14 11:11:25 +00:00
|
|
|
val = tr_cryptoRandInt( base );
|
2008-01-07 06:19:34 +00:00
|
|
|
total += val;
|
|
|
|
buf[i] = pool[val];
|
|
|
|
}
|
2007-07-28 04:10:09 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
val = total % base ? base - ( total % base ) : 0;
|
2008-01-07 17:52:50 +00:00
|
|
|
buf[19] = pool[val];
|
2008-01-07 06:19:34 +00:00
|
|
|
buf[20] = '\0';
|
2008-01-07 17:52:50 +00:00
|
|
|
|
2008-01-07 06:19:34 +00:00
|
|
|
return buf;
|
2007-07-16 11:19:44 +00:00
|
|
|
}
|
|
|
|
|
2008-01-07 06:19:34 +00:00
|
|
|
const uint8_t*
|
|
|
|
tr_getPeerId( void )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-01-07 06:19:34 +00:00
|
|
|
static uint8_t * id = NULL;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-01-07 06:19:34 +00:00
|
|
|
if( id == NULL )
|
|
|
|
id = tr_peerIdNew( );
|
|
|
|
return id;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
tr_encryption_mode
|
2008-05-12 16:33:17 +00:00
|
|
|
tr_sessionGetEncryption( tr_session * session )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-08-01 16:43:22 +00:00
|
|
|
assert( session );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-04-24 01:42:53 +00:00
|
|
|
return session->encryptionMode;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_sessionSetEncryption( tr_session * session,
|
|
|
|
tr_encryption_mode mode )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-08-01 16:43:22 +00:00
|
|
|
assert( session );
|
2008-09-23 19:11:04 +00:00
|
|
|
assert( mode == TR_ENCRYPTION_PREFERRED
|
|
|
|
|| mode == TR_ENCRYPTION_REQUIRED
|
|
|
|
|| mode == TR_CLEAR_PREFERRED );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-04-24 01:42:53 +00:00
|
|
|
session->encryptionMode = mode;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2007-08-18 03:02:32 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2007-08-14 20:45:23 +00:00
|
|
|
|
2009-05-14 13:42:29 +00:00
|
|
|
struct tr_bindinfo
|
|
|
|
{
|
|
|
|
int socket;
|
|
|
|
tr_address addr;
|
|
|
|
struct event ev;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
close_bindinfo( struct tr_bindinfo * b )
|
|
|
|
{
|
2009-10-23 03:41:36 +00:00
|
|
|
if( ( b != NULL ) && ( b->socket >=0 ) )
|
2009-05-14 13:42:29 +00:00
|
|
|
{
|
|
|
|
event_del( &b->ev );
|
2009-10-27 20:27:27 +00:00
|
|
|
tr_netCloseSocket( b->socket );
|
2009-05-14 13:42:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
close_incoming_peer_port( tr_session * session )
|
|
|
|
{
|
|
|
|
close_bindinfo( session->public_ipv4 );
|
|
|
|
close_bindinfo( session->public_ipv6 );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
free_incoming_peer_port( tr_session * session )
|
|
|
|
{
|
|
|
|
close_bindinfo( session->public_ipv4 );
|
|
|
|
tr_free( session->public_ipv4 );
|
|
|
|
session->public_ipv4 = NULL;
|
|
|
|
|
|
|
|
close_bindinfo( session->public_ipv6 );
|
|
|
|
tr_free( session->public_ipv6 );
|
|
|
|
session->public_ipv6 = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
accept_incoming_peer( int fd, short what UNUSED, void * vsession )
|
|
|
|
{
|
|
|
|
int clientSocket;
|
|
|
|
tr_port clientPort;
|
|
|
|
tr_address clientAddr;
|
|
|
|
tr_session * session = vsession;
|
|
|
|
|
|
|
|
clientSocket = tr_netAccept( session, fd, &clientAddr, &clientPort );
|
|
|
|
if( clientSocket > 0 ) {
|
|
|
|
tr_deepLog( __FILE__, __LINE__, NULL, "new incoming connection %d (%s)",
|
|
|
|
clientSocket, tr_peerIoAddrStr( &clientAddr, clientPort ) );
|
|
|
|
tr_peerMgrAddIncoming( session->peerMgr, &clientAddr, clientPort, clientSocket );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
open_incoming_peer_port( tr_session * session )
|
|
|
|
{
|
|
|
|
struct tr_bindinfo * b;
|
|
|
|
|
|
|
|
/* bind an ipv4 port to listen for incoming peers... */
|
|
|
|
b = session->public_ipv4;
|
|
|
|
b->socket = tr_netBindTCP( &b->addr, session->peerPort, FALSE );
|
|
|
|
if( b->socket >= 0 ) {
|
|
|
|
event_set( &b->ev, b->socket, EV_READ | EV_PERSIST, accept_incoming_peer, session );
|
|
|
|
event_add( &b->ev, NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* and do the exact same thing for ipv6, if it's supported... */
|
|
|
|
if( tr_net_hasIPv6( session->peerPort ) ) {
|
|
|
|
b = session->public_ipv6;
|
|
|
|
b->socket = tr_netBindTCP( &b->addr, session->peerPort, FALSE );
|
|
|
|
if( b->socket >= 0 ) {
|
|
|
|
event_set( &b->ev, b->socket, EV_READ | EV_PERSIST, accept_incoming_peer, session );
|
|
|
|
event_add( &b->ev, NULL );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const tr_address*
|
|
|
|
tr_sessionGetPublicAddress( const tr_session * session, int tr_af_type )
|
|
|
|
{
|
|
|
|
switch( tr_af_type )
|
|
|
|
{
|
|
|
|
case TR_AF_INET: return &session->public_ipv4->addr;
|
|
|
|
case TR_AF_INET6: return &session->public_ipv6->addr; break;
|
|
|
|
default: return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
#ifdef TR_EMBEDDED
|
2009-03-29 23:05:32 +00:00
|
|
|
#define TR_DEFAULT_ENCRYPTION TR_CLEAR_PREFERRED
|
2008-12-13 23:17:36 +00:00
|
|
|
#else
|
2009-03-29 23:05:32 +00:00
|
|
|
#define TR_DEFAULT_ENCRYPTION TR_ENCRYPTION_PREFERRED
|
2008-12-13 23:17:36 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
2009-10-19 05:05:00 +00:00
|
|
|
tr_sessionGetDefaultSettings( const char * configDir, tr_benc * d )
|
2008-12-13 23:17:36 +00:00
|
|
|
{
|
2009-10-19 05:05:00 +00:00
|
|
|
char * incompleteDir = tr_buildPath( configDir, "Incomplete", NULL );
|
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
assert( tr_bencIsDict( d ) );
|
|
|
|
|
2009-03-25 19:18:00 +00:00
|
|
|
tr_bencDictReserve( d, 35 );
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_BLOCKLIST_ENABLED, FALSE );
|
2009-05-21 00:15:54 +00:00
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_DHT_ENABLED, TRUE );
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddStr ( d, TR_PREFS_KEY_DOWNLOAD_DIR, tr_getDefaultDownloadDir( ) );
|
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_DSPEED, 100 );
|
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_DSPEED_ENABLED, FALSE );
|
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_ENCRYPTION, TR_DEFAULT_ENCRYPTION );
|
2009-10-19 05:05:00 +00:00
|
|
|
tr_bencDictAddStr ( d, TR_PREFS_KEY_INCOMPLETE_DIR, incompleteDir );
|
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_INCOMPLETE_DIR_ENABLED, FALSE );
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_LAZY_BITFIELD, TRUE );
|
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_MSGLEVEL, TR_MSG_INF );
|
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_OPEN_FILE_LIMIT, atoi( TR_DEFAULT_OPEN_FILE_LIMIT_STR ) );
|
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_PEER_LIMIT_GLOBAL, atoi( TR_DEFAULT_PEER_LIMIT_GLOBAL_STR ) );
|
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_PEER_LIMIT_TORRENT, atoi( TR_DEFAULT_PEER_LIMIT_TORRENT_STR ) );
|
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_PEER_PORT, atoi( TR_DEFAULT_PEER_PORT_STR ) );
|
2009-04-03 04:29:27 +00:00
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_PEER_PORT_RANDOM_ON_START, FALSE );
|
2009-05-24 01:51:53 +00:00
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_PEER_PORT_RANDOM_LOW, 49152 );
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_PEER_PORT_RANDOM_HIGH, 65535 );
|
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_PEER_SOCKET_TOS, atoi( TR_DEFAULT_PEER_SOCKET_TOS_STR ) );
|
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_PEX_ENABLED, TRUE );
|
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_PORT_FORWARDING, TRUE );
|
2010-02-02 03:01:25 +00:00
|
|
|
#ifdef HAVE_FALLOCATE64
|
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_PREALLOCATION, TR_PREALLOCATE_FULL );
|
|
|
|
#else
|
2009-07-03 22:22:01 +00:00
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_PREALLOCATION, TR_PREALLOCATE_SPARSE );
|
2010-02-02 03:01:25 +00:00
|
|
|
#endif
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddStr ( d, TR_PREFS_KEY_PROXY, "" );
|
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_PROXY_AUTH_ENABLED, FALSE );
|
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_PROXY_ENABLED, FALSE );
|
|
|
|
tr_bencDictAddStr ( d, TR_PREFS_KEY_PROXY_PASSWORD, "" );
|
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_PROXY_PORT, 80 );
|
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_PROXY_TYPE, TR_PROXY_HTTP );
|
|
|
|
tr_bencDictAddStr ( d, TR_PREFS_KEY_PROXY_USERNAME, "" );
|
|
|
|
tr_bencDictAddReal( d, TR_PREFS_KEY_RATIO, 2.0 );
|
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_RATIO_ENABLED, FALSE );
|
2009-10-20 03:14:44 +00:00
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_RENAME_PARTIAL_FILES, TRUE );
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_RPC_AUTH_REQUIRED, FALSE );
|
2009-04-15 21:05:58 +00:00
|
|
|
tr_bencDictAddStr ( d, TR_PREFS_KEY_RPC_BIND_ADDRESS, "0.0.0.0" );
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_RPC_ENABLED, TRUE );
|
|
|
|
tr_bencDictAddStr ( d, TR_PREFS_KEY_RPC_PASSWORD, "" );
|
|
|
|
tr_bencDictAddStr ( d, TR_PREFS_KEY_RPC_USERNAME, "" );
|
|
|
|
tr_bencDictAddStr ( d, TR_PREFS_KEY_RPC_WHITELIST, TR_DEFAULT_RPC_WHITELIST );
|
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_RPC_WHITELIST_ENABLED, TRUE );
|
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_RPC_PORT, atoi( TR_DEFAULT_RPC_PORT_STR ) );
|
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_ALT_SPEED_ENABLED, FALSE );
|
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_ALT_SPEED_UP, 50 ); /* half the regular */
|
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_ALT_SPEED_DOWN, 50 ); /* half the regular */
|
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_ALT_SPEED_TIME_BEGIN, 540 ); /* 9am */
|
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_ALT_SPEED_TIME_ENABLED, FALSE );
|
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_ALT_SPEED_TIME_END, 1020 ); /* 5pm */
|
2009-04-04 02:17:39 +00:00
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_ALT_SPEED_TIME_DAY, TR_SCHED_ALL );
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_USPEED, 100 );
|
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_USPEED_ENABLED, FALSE );
|
2009-07-02 02:37:49 +00:00
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_UMASK, 022 );
|
2009-07-17 11:20:30 +00:00
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_UPLOAD_SLOTS_PER_TORRENT, 14 );
|
2009-04-15 21:05:58 +00:00
|
|
|
tr_bencDictAddStr ( d, TR_PREFS_KEY_BIND_ADDRESS_IPV4, TR_DEFAULT_BIND_ADDRESS_IPV4 );
|
|
|
|
tr_bencDictAddStr ( d, TR_PREFS_KEY_BIND_ADDRESS_IPV6, TR_DEFAULT_BIND_ADDRESS_IPV6 );
|
2009-10-19 05:05:00 +00:00
|
|
|
|
|
|
|
tr_free( incompleteDir );
|
2008-12-13 23:17:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_sessionGetSettings( tr_session * s, struct tr_benc * d )
|
|
|
|
{
|
|
|
|
assert( tr_bencIsDict( d ) );
|
|
|
|
|
|
|
|
tr_bencDictReserve( d, 30 );
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_BLOCKLIST_ENABLED, tr_blocklistIsEnabled( s ) );
|
2009-05-21 00:15:54 +00:00
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_DHT_ENABLED, s->isDHTEnabled );
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddStr ( d, TR_PREFS_KEY_DOWNLOAD_DIR, s->downloadDir );
|
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_DSPEED, tr_sessionGetSpeedLimit( s, TR_DOWN ) );
|
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_DSPEED_ENABLED, tr_sessionIsSpeedLimited( s, TR_DOWN ) );
|
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_ENCRYPTION, s->encryptionMode );
|
2009-10-19 05:05:00 +00:00
|
|
|
tr_bencDictAddStr ( d, TR_PREFS_KEY_INCOMPLETE_DIR, tr_sessionGetIncompleteDir( s ) );
|
2009-10-20 03:14:44 +00:00
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_INCOMPLETE_DIR_ENABLED, tr_sessionIsIncompleteDirEnabled( s ) );
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_LAZY_BITFIELD, s->useLazyBitfield );
|
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_MSGLEVEL, tr_getMessageLevel( ) );
|
2009-10-23 03:41:36 +00:00
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_OPEN_FILE_LIMIT, tr_fdGetFileLimit( s ) );
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_PEER_LIMIT_GLOBAL, tr_sessionGetPeerLimit( s ) );
|
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_PEER_LIMIT_TORRENT, s->peerLimitPerTorrent );
|
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_PEER_PORT, tr_sessionGetPeerPort( s ) );
|
2009-04-03 04:29:27 +00:00
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_PEER_PORT_RANDOM_ON_START, s->isPortRandom );
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_PEER_PORT_RANDOM_LOW, s->randomPortLow );
|
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_PEER_PORT_RANDOM_HIGH, s->randomPortHigh );
|
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_PEER_SOCKET_TOS, s->peerSocketTOS );
|
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_PEX_ENABLED, s->isPexEnabled );
|
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_PORT_FORWARDING, tr_sessionIsPortForwardingEnabled( s ) );
|
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_PREALLOCATION, s->preallocationMode );
|
|
|
|
tr_bencDictAddStr ( d, TR_PREFS_KEY_PROXY, s->proxy );
|
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_PROXY_AUTH_ENABLED, s->isProxyAuthEnabled );
|
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_PROXY_ENABLED, s->isProxyEnabled );
|
|
|
|
tr_bencDictAddStr ( d, TR_PREFS_KEY_PROXY_PASSWORD, s->proxyPassword );
|
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_PROXY_PORT, s->proxyPort );
|
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_PROXY_TYPE, s->proxyType );
|
|
|
|
tr_bencDictAddStr ( d, TR_PREFS_KEY_PROXY_USERNAME, s->proxyUsername );
|
|
|
|
tr_bencDictAddReal( d, TR_PREFS_KEY_RATIO, s->desiredRatio );
|
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_RATIO_ENABLED, s->isRatioLimited );
|
2009-10-20 04:43:51 +00:00
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_RENAME_PARTIAL_FILES, tr_sessionIsIncompleteFileNamingEnabled( s ) );
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_RPC_AUTH_REQUIRED, tr_sessionIsRPCPasswordEnabled( s ) );
|
2009-04-15 21:05:58 +00:00
|
|
|
tr_bencDictAddStr ( d, TR_PREFS_KEY_RPC_BIND_ADDRESS, tr_sessionGetRPCBindAddress( s ) );
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_RPC_ENABLED, tr_sessionIsRPCEnabled( s ) );
|
2009-04-16 19:14:37 +00:00
|
|
|
tr_bencDictAddStr ( d, TR_PREFS_KEY_RPC_PASSWORD, tr_sessionGetRPCPassword( s ) );
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_RPC_PORT, tr_sessionGetRPCPort( s ) );
|
2009-04-16 19:14:37 +00:00
|
|
|
tr_bencDictAddStr ( d, TR_PREFS_KEY_RPC_USERNAME, tr_sessionGetRPCUsername( s ) );
|
|
|
|
tr_bencDictAddStr ( d, TR_PREFS_KEY_RPC_WHITELIST, tr_sessionGetRPCWhitelist( s ) );
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_RPC_WHITELIST_ENABLED, tr_sessionGetRPCWhitelistEnabled( s ) );
|
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_ALT_SPEED_ENABLED, tr_sessionUsesAltSpeed( s ) );
|
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_ALT_SPEED_UP, tr_sessionGetAltSpeed( s, TR_UP ) );
|
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_ALT_SPEED_DOWN, tr_sessionGetAltSpeed( s, TR_DOWN ) );
|
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_ALT_SPEED_TIME_BEGIN, tr_sessionGetAltSpeedBegin( s ) );
|
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_ALT_SPEED_TIME_ENABLED, tr_sessionUsesAltSpeedTime( s ) );
|
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_ALT_SPEED_TIME_END, tr_sessionGetAltSpeedEnd( s ) );
|
2009-04-04 02:17:39 +00:00
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_ALT_SPEED_TIME_DAY, tr_sessionGetAltSpeedDay( s ) );
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_USPEED, tr_sessionGetSpeedLimit( s, TR_UP ) );
|
|
|
|
tr_bencDictAddBool( d, TR_PREFS_KEY_USPEED_ENABLED, tr_sessionIsSpeedLimited( s, TR_UP ) );
|
2009-07-02 02:37:49 +00:00
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_UMASK, s->umask );
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddInt ( d, TR_PREFS_KEY_UPLOAD_SLOTS_PER_TORRENT, s->uploadSlotsPerTorrent );
|
2009-05-28 13:24:35 +00:00
|
|
|
tr_bencDictAddStr ( d, TR_PREFS_KEY_BIND_ADDRESS_IPV4, tr_ntop_non_ts( &s->public_ipv4->addr ) );
|
|
|
|
tr_bencDictAddStr ( d, TR_PREFS_KEY_BIND_ADDRESS_IPV6, tr_ntop_non_ts( &s->public_ipv6->addr ) );
|
2008-12-13 23:17:36 +00:00
|
|
|
}
|
|
|
|
|
2010-01-12 16:43:37 +00:00
|
|
|
tr_bool
|
2008-12-13 23:17:36 +00:00
|
|
|
tr_sessionLoadSettings( tr_benc * d, const char * configDir, const char * appName )
|
|
|
|
{
|
2010-02-07 22:17:42 +00:00
|
|
|
int err = 0;
|
2008-12-13 23:17:36 +00:00
|
|
|
char * filename;
|
|
|
|
tr_benc fileSettings;
|
2009-03-10 15:24:40 +00:00
|
|
|
tr_benc sessionDefaults;
|
|
|
|
tr_benc tmp;
|
2010-01-12 16:43:37 +00:00
|
|
|
tr_bool success = FALSE;
|
2008-12-13 23:17:36 +00:00
|
|
|
|
|
|
|
assert( tr_bencIsDict( d ) );
|
|
|
|
|
2009-03-10 15:24:40 +00:00
|
|
|
/* initializing the defaults: caller may have passed in some app-level defaults.
|
|
|
|
* preserve those and use the session defaults to fill in any missing gaps. */
|
|
|
|
tr_bencInitDict( &sessionDefaults, 0 );
|
2009-10-19 05:05:00 +00:00
|
|
|
tr_sessionGetDefaultSettings( configDir, &sessionDefaults );
|
2009-03-10 15:24:40 +00:00
|
|
|
tr_bencMergeDicts( &sessionDefaults, d );
|
|
|
|
tmp = *d; *d = sessionDefaults; sessionDefaults = tmp;
|
2008-12-13 23:17:36 +00:00
|
|
|
|
|
|
|
/* if caller didn't specify a config dir, use the default */
|
|
|
|
if( !configDir || !*configDir )
|
|
|
|
configDir = tr_getDefaultConfigDir( appName );
|
|
|
|
|
|
|
|
/* file settings override the defaults */
|
|
|
|
filename = tr_buildPath( configDir, "settings.json", NULL );
|
2010-02-07 22:17:42 +00:00
|
|
|
err = tr_bencLoadFile( &fileSettings, TR_FMT_JSON, filename );
|
|
|
|
if( !err ) {
|
2008-12-13 23:17:36 +00:00
|
|
|
tr_bencMergeDicts( d, &fileSettings );
|
|
|
|
tr_bencFree( &fileSettings );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cleanup */
|
2009-03-10 15:24:40 +00:00
|
|
|
tr_bencFree( &sessionDefaults );
|
2008-12-13 23:17:36 +00:00
|
|
|
tr_free( filename );
|
2010-02-07 22:17:42 +00:00
|
|
|
success = (err==0) || (err==ENOENT);
|
2010-01-12 16:43:37 +00:00
|
|
|
return success;
|
2008-12-13 23:17:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-03-28 16:47:01 +00:00
|
|
|
tr_sessionSaveSettings( tr_session * session,
|
|
|
|
const char * configDir,
|
|
|
|
const tr_benc * clientSettings )
|
2008-12-13 23:17:36 +00:00
|
|
|
{
|
2009-03-28 16:47:01 +00:00
|
|
|
tr_benc settings;
|
|
|
|
char * filename = tr_buildPath( configDir, "settings.json", NULL );
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2009-03-28 16:47:01 +00:00
|
|
|
assert( tr_bencIsDict( clientSettings ) );
|
2009-03-01 13:56:22 +00:00
|
|
|
|
2009-03-28 16:47:01 +00:00
|
|
|
tr_bencInitDict( &settings, 0 );
|
|
|
|
|
|
|
|
/* the existing file settings are the fallback values */
|
|
|
|
{
|
|
|
|
tr_benc fileSettings;
|
2010-02-07 22:17:42 +00:00
|
|
|
const int err = tr_bencLoadFile( &fileSettings, TR_FMT_JSON, filename );
|
|
|
|
if( !err )
|
2009-03-28 16:47:01 +00:00
|
|
|
{
|
|
|
|
tr_bencMergeDicts( &settings, &fileSettings );
|
|
|
|
tr_bencFree( &fileSettings );
|
|
|
|
}
|
|
|
|
}
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2009-03-28 16:47:01 +00:00
|
|
|
/* the client's settings override the file settings */
|
|
|
|
tr_bencMergeDicts( &settings, clientSettings );
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2009-03-28 16:47:01 +00:00
|
|
|
/* the session's true values override the file & client settings */
|
|
|
|
{
|
|
|
|
tr_benc sessionSettings;
|
|
|
|
tr_bencInitDict( &sessionSettings, 0 );
|
|
|
|
tr_sessionGetSettings( session, &sessionSettings );
|
|
|
|
tr_bencMergeDicts( &settings, &sessionSettings );
|
|
|
|
tr_bencFree( &sessionSettings );
|
2008-12-13 23:17:36 +00:00
|
|
|
}
|
|
|
|
|
2009-03-28 16:47:01 +00:00
|
|
|
/* save the result */
|
2009-06-02 01:48:48 +00:00
|
|
|
tr_bencToFile( &settings, TR_FMT_JSON, filename );
|
2009-03-28 16:47:01 +00:00
|
|
|
|
|
|
|
/* cleanup */
|
2008-12-13 23:17:36 +00:00
|
|
|
tr_free( filename );
|
2009-03-28 16:47:01 +00:00
|
|
|
tr_bencFree( &settings );
|
2008-12-13 23:17:36 +00:00
|
|
|
}
|
|
|
|
|
2009-08-13 14:47:56 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Periodically save the .resume files of any torrents whose
|
|
|
|
* status has recently changed. This prevents loss of metadata
|
|
|
|
* in the case of a crash, unclean shutdown, clumsy user, etc.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
onSaveTimer( int foo UNUSED, short bar UNUSED, void * vsession )
|
|
|
|
{
|
|
|
|
tr_torrent * tor = NULL;
|
|
|
|
tr_session * session = vsession;
|
|
|
|
|
|
|
|
while(( tor = tr_torrentNext( session, tor )))
|
|
|
|
tr_torrentSave( tor );
|
|
|
|
|
2009-12-08 20:51:45 +00:00
|
|
|
tr_statsSaveDirty( session );
|
|
|
|
|
2009-08-13 14:47:56 +00:00
|
|
|
tr_timerAdd( session->saveTimer, SAVE_INTERVAL_SECS, 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2009-01-26 08:23:11 +00:00
|
|
|
static void tr_sessionInitImpl( void * );
|
2008-04-14 14:39:13 +00:00
|
|
|
|
2009-02-04 16:58:52 +00:00
|
|
|
struct init_data
|
|
|
|
{
|
|
|
|
tr_session * session;
|
|
|
|
const char * configDir;
|
2010-01-01 22:35:49 +00:00
|
|
|
tr_bool done;
|
2009-02-04 16:58:52 +00:00
|
|
|
tr_bool messageQueuingEnabled;
|
|
|
|
tr_benc * clientSettings;
|
|
|
|
};
|
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
tr_session *
|
|
|
|
tr_sessionInit( const char * tag,
|
|
|
|
const char * configDir,
|
|
|
|
tr_bool messageQueuingEnabled,
|
|
|
|
tr_benc * clientSettings )
|
|
|
|
{
|
2009-10-23 05:00:16 +00:00
|
|
|
int64_t i;
|
2008-12-13 23:17:36 +00:00
|
|
|
tr_session * session;
|
2009-02-04 16:58:52 +00:00
|
|
|
struct init_data data;
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2009-11-27 15:25:14 +00:00
|
|
|
tr_msgInit( );
|
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
assert( tr_bencIsDict( clientSettings ) );
|
|
|
|
|
2009-02-04 16:58:52 +00:00
|
|
|
/* initialize the bare skeleton of the session object */
|
2008-12-13 23:17:36 +00:00
|
|
|
session = tr_new0( tr_session, 1 );
|
|
|
|
session->bandwidth = tr_bandwidthNew( session, NULL );
|
|
|
|
session->lock = tr_lockNew( );
|
|
|
|
session->tag = tr_strdup( tag );
|
2009-01-29 16:56:43 +00:00
|
|
|
session->magicNumber = SESSION_MAGIC_NUMBER;
|
2010-02-23 07:20:57 +00:00
|
|
|
session->buffer = tr_valloc( SESSION_BUFFER_SIZE );
|
2009-04-04 05:29:08 +00:00
|
|
|
tr_bencInitList( &session->removedTorrents, 0 );
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2009-10-23 05:00:16 +00:00
|
|
|
/* nice to start logging at the very beginning */
|
|
|
|
if( tr_bencDictFindInt( clientSettings, TR_PREFS_KEY_MSGLEVEL, &i ) )
|
|
|
|
tr_setMessageLevel( i );
|
|
|
|
|
2009-02-04 16:58:52 +00:00
|
|
|
/* start the libtransmission thread */
|
|
|
|
tr_netInit( ); /* must go before tr_eventInit */
|
|
|
|
tr_eventInit( session );
|
|
|
|
assert( session->events != NULL );
|
|
|
|
|
|
|
|
/* run the rest in the libtransmission thread */
|
2010-01-01 22:35:49 +00:00
|
|
|
data.done = FALSE;
|
2009-02-04 16:58:52 +00:00
|
|
|
data.session = session;
|
|
|
|
data.configDir = configDir;
|
|
|
|
data.messageQueuingEnabled = messageQueuingEnabled;
|
|
|
|
data.clientSettings = clientSettings;
|
|
|
|
tr_runInEventThread( session, tr_sessionInitImpl, &data );
|
2010-01-01 22:35:49 +00:00
|
|
|
while( !data.done )
|
2010-01-01 22:30:36 +00:00
|
|
|
tr_wait_msec( 100 );
|
2009-02-04 16:58:52 +00:00
|
|
|
|
|
|
|
return session;
|
|
|
|
}
|
|
|
|
|
2010-02-04 23:39:50 +00:00
|
|
|
static void turtleCheckClock( tr_session * session, struct tr_turtle_info * t, tr_bool byUser );
|
2009-03-30 17:25:35 +00:00
|
|
|
|
2009-11-27 02:24:48 +00:00
|
|
|
static void
|
|
|
|
onNowTimer( int foo UNUSED, short bar UNUSED, void * vsession )
|
|
|
|
{
|
|
|
|
int usec;
|
|
|
|
const int min = 100;
|
|
|
|
const int max = 999999;
|
|
|
|
struct timeval tv;
|
|
|
|
tr_session * session = vsession;
|
|
|
|
|
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
assert( session->nowTimer != NULL );
|
|
|
|
|
|
|
|
/* schedule the next timer for right after the next second begins */
|
|
|
|
gettimeofday( &tv, NULL );
|
|
|
|
usec = 1000000 - tv.tv_usec;
|
|
|
|
if( usec > max ) usec = max;
|
|
|
|
if( usec < min ) usec = min;
|
|
|
|
tr_timerAdd( session->nowTimer, 0, usec );
|
2010-02-04 23:39:50 +00:00
|
|
|
/* fprintf( stderr, "time %zu sec, %zu microsec\n", (size_t)tr_time(), (size_t)tv.tv_usec ); */
|
2010-01-19 19:37:00 +00:00
|
|
|
|
2010-02-04 23:39:50 +00:00
|
|
|
/* tr_session things to do once per second */
|
2009-11-26 18:47:08 +00:00
|
|
|
tr_timeUpdate( tv.tv_sec );
|
2010-02-04 23:39:50 +00:00
|
|
|
turtleCheckClock( session, &session->turtle, FALSE );
|
2009-11-26 18:47:08 +00:00
|
|
|
}
|
|
|
|
|
2009-12-01 00:07:18 +00:00
|
|
|
static void loadBlocklists( tr_session * session );
|
|
|
|
|
2009-02-04 16:58:52 +00:00
|
|
|
static void
|
|
|
|
tr_sessionInitImpl( void * vdata )
|
|
|
|
{
|
|
|
|
tr_benc settings;
|
|
|
|
struct init_data * data = vdata;
|
|
|
|
tr_benc * clientSettings = data->clientSettings;
|
|
|
|
tr_session * session = data->session;
|
|
|
|
|
|
|
|
assert( tr_amInEventThread( session ) );
|
|
|
|
assert( tr_bencIsDict( clientSettings ) );
|
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
dbgmsg( "tr_sessionInit: the session's top-level bandwidth object is %p",
|
|
|
|
session->bandwidth );
|
2008-12-13 23:17:36 +00:00
|
|
|
|
|
|
|
tr_bencInitDict( &settings, 0 );
|
2009-10-19 05:05:00 +00:00
|
|
|
tr_sessionGetDefaultSettings( data->configDir, &settings );
|
2008-12-13 23:17:36 +00:00
|
|
|
tr_bencMergeDicts( &settings, clientSettings );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2009-11-27 02:24:48 +00:00
|
|
|
session->nowTimer = tr_new0( struct event, 1 );
|
|
|
|
evtimer_set( session->nowTimer, onNowTimer, session );
|
2009-11-26 18:47:08 +00:00
|
|
|
onNowTimer( 0, 0, session );
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
#ifndef WIN32
|
|
|
|
/* Don't exit when writing on a broken socket */
|
|
|
|
signal( SIGPIPE, SIG_IGN );
|
|
|
|
#endif
|
|
|
|
|
2009-02-06 23:35:47 +00:00
|
|
|
tr_setMessageQueuing( data->messageQueuingEnabled );
|
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
tr_setConfigDir( session, data->configDir );
|
2009-02-06 23:35:47 +00:00
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
session->peerMgr = tr_peerMgrNew( session );
|
2009-10-19 05:05:00 +00:00
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
session->shared = tr_sharedInit( session );
|
2009-10-20 03:14:44 +00:00
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
/**
|
|
|
|
*** Blocklist
|
|
|
|
**/
|
2009-02-06 23:35:47 +00:00
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
{
|
|
|
|
char * filename = tr_buildPath( session->configDir, "blocklists", NULL );
|
|
|
|
tr_mkdirp( filename, 0777 );
|
|
|
|
tr_free( filename );
|
|
|
|
loadBlocklists( session );
|
|
|
|
}
|
2009-02-06 23:35:47 +00:00
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
assert( tr_isSession( session ) );
|
2009-02-06 23:35:47 +00:00
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
session->saveTimer = tr_new0( struct event, 1 );
|
|
|
|
evtimer_set( session->saveTimer, onSaveTimer, session );
|
|
|
|
tr_timerAdd( session->saveTimer, SAVE_INTERVAL_SECS, 0 );
|
2009-02-06 23:35:47 +00:00
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
tr_announcerInit( session );
|
2009-02-06 23:35:47 +00:00
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
/* first %s is the application name
|
|
|
|
second %s is the version number */
|
|
|
|
tr_inf( _( "%s %s started" ), TR_NAME, LONG_VERSION_STRING );
|
2009-02-06 23:35:47 +00:00
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
tr_statsInit( session );
|
2009-02-06 23:35:47 +00:00
|
|
|
|
2010-03-06 15:05:05 +00:00
|
|
|
tr_webInit( session );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
tr_sessionSet( session, &settings );
|
2007-12-24 07:02:40 +00:00
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
if( session->isDHTEnabled )
|
|
|
|
{
|
|
|
|
tr_dhtInit( session, &session->public_ipv4->addr );
|
|
|
|
}
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
/* cleanup */
|
|
|
|
tr_bencFree( &settings );
|
2010-01-01 22:35:49 +00:00
|
|
|
data->done = TRUE;
|
2009-10-23 03:41:36 +00:00
|
|
|
}
|
2009-02-06 23:35:47 +00:00
|
|
|
|
2010-02-08 19:21:07 +00:00
|
|
|
static void turtleBootstrap( tr_session *, struct tr_turtle_info * );
|
2010-02-04 23:39:50 +00:00
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
static void
|
|
|
|
sessionSetImpl( void * vdata )
|
|
|
|
{
|
|
|
|
int64_t i;
|
|
|
|
double d;
|
|
|
|
tr_bool boolVal;
|
|
|
|
const char * str;
|
|
|
|
struct tr_bindinfo b;
|
|
|
|
struct init_data * data = vdata;
|
|
|
|
tr_session * session = data->session;
|
|
|
|
tr_benc * settings = data->clientSettings;
|
2010-02-04 23:39:50 +00:00
|
|
|
struct tr_turtle_info * turtle = &session->turtle;
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
assert( tr_bencIsDict( settings ) );
|
|
|
|
assert( tr_amInEventThread( session ) );
|
2009-02-06 23:35:47 +00:00
|
|
|
|
2009-10-23 05:00:16 +00:00
|
|
|
if( tr_bencDictFindInt( settings, TR_PREFS_KEY_MSGLEVEL, &i ) )
|
|
|
|
tr_setMessageLevel( i );
|
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
if( tr_bencDictFindInt( settings, TR_PREFS_KEY_UMASK, &i ) ) {
|
|
|
|
session->umask = (mode_t)i;
|
|
|
|
umask( session->umask );
|
|
|
|
}
|
2009-02-06 23:35:47 +00:00
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
/* misc features */
|
|
|
|
if( tr_bencDictFindBool( settings, TR_PREFS_KEY_LAZY_BITFIELD, &boolVal ) )
|
|
|
|
tr_sessionSetLazyBitfieldEnabled( session, boolVal );
|
|
|
|
if( tr_bencDictFindInt( settings, TR_PREFS_KEY_PEER_LIMIT_TORRENT, &i ) )
|
|
|
|
tr_sessionSetPeerLimitPerTorrent( session, i );
|
|
|
|
if( tr_bencDictFindBool( settings, TR_PREFS_KEY_PEX_ENABLED, &boolVal ) )
|
|
|
|
tr_sessionSetPexEnabled( session, boolVal );
|
|
|
|
if( tr_bencDictFindBool( settings, TR_PREFS_KEY_DHT_ENABLED, &boolVal ) )
|
|
|
|
tr_sessionSetDHTEnabled( session, boolVal );
|
|
|
|
if( tr_bencDictFindInt( settings, TR_PREFS_KEY_ENCRYPTION, &i ) )
|
|
|
|
tr_sessionSetEncryption( session, i );
|
|
|
|
if( tr_bencDictFindInt( settings, TR_PREFS_KEY_PEER_SOCKET_TOS, &i ) )
|
|
|
|
session->peerSocketTOS = i;
|
|
|
|
if( tr_bencDictFindBool( settings, TR_PREFS_KEY_BLOCKLIST_ENABLED, &boolVal ) )
|
|
|
|
tr_blocklistSetEnabled( session, boolVal );
|
|
|
|
|
|
|
|
/* files and directories */
|
|
|
|
if( tr_bencDictFindInt( settings, TR_PREFS_KEY_PREALLOCATION, &i ) )
|
|
|
|
session->preallocationMode = i;
|
|
|
|
if( tr_bencDictFindStr( settings, TR_PREFS_KEY_DOWNLOAD_DIR, &str ) )
|
|
|
|
tr_sessionSetDownloadDir( session, str );
|
|
|
|
if( tr_bencDictFindStr( settings, TR_PREFS_KEY_INCOMPLETE_DIR, &str ) )
|
|
|
|
tr_sessionSetIncompleteDir( session, str );
|
|
|
|
if( tr_bencDictFindBool( settings, TR_PREFS_KEY_INCOMPLETE_DIR_ENABLED, &boolVal ) )
|
|
|
|
tr_sessionSetIncompleteDirEnabled( session, boolVal );
|
|
|
|
if( tr_bencDictFindBool( settings, TR_PREFS_KEY_RENAME_PARTIAL_FILES, &boolVal ) )
|
|
|
|
tr_sessionSetIncompleteFileNamingEnabled( session, boolVal );
|
|
|
|
|
|
|
|
/* proxies */
|
|
|
|
if( tr_bencDictFindBool( settings, TR_PREFS_KEY_PROXY_ENABLED, &boolVal ) )
|
|
|
|
tr_sessionSetProxyEnabled( session, boolVal );
|
|
|
|
if( tr_bencDictFindStr( settings, TR_PREFS_KEY_PROXY, &str ) )
|
|
|
|
tr_sessionSetProxy( session, str );
|
|
|
|
if( tr_bencDictFindInt( settings, TR_PREFS_KEY_PROXY_PORT, &i ) )
|
|
|
|
tr_sessionSetProxyPort( session, i );
|
|
|
|
if( tr_bencDictFindInt( settings, TR_PREFS_KEY_PROXY_TYPE, &i ) )
|
|
|
|
tr_sessionSetProxyType( session, i );
|
|
|
|
if( tr_bencDictFindBool( settings, TR_PREFS_KEY_PROXY_AUTH_ENABLED, &boolVal ) )
|
|
|
|
tr_sessionSetProxyAuthEnabled( session, boolVal );
|
|
|
|
if( tr_bencDictFindStr( settings, TR_PREFS_KEY_PROXY_USERNAME, &str ) )
|
|
|
|
tr_sessionSetProxyUsername( session, str );
|
|
|
|
if( tr_bencDictFindStr( settings, TR_PREFS_KEY_PROXY_PASSWORD, &str ) )
|
|
|
|
tr_sessionSetProxyPassword( session, str );
|
|
|
|
|
|
|
|
/* rpc server */
|
|
|
|
if( session->rpcServer != NULL ) /* close the old one */
|
|
|
|
tr_rpcClose( &session->rpcServer );
|
|
|
|
session->rpcServer = tr_rpcInit( session, settings );
|
2009-04-15 21:05:58 +00:00
|
|
|
|
2009-05-14 13:42:29 +00:00
|
|
|
/* public addresses */
|
2009-04-15 21:05:58 +00:00
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
free_incoming_peer_port( session );
|
2009-04-15 21:05:58 +00:00
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
str = TR_PREFS_KEY_BIND_ADDRESS_IPV4;
|
|
|
|
tr_bencDictFindStr( settings, TR_PREFS_KEY_BIND_ADDRESS_IPV4, &str );
|
|
|
|
if( !tr_pton( str, &b.addr ) || ( b.addr.type != TR_AF_INET ) )
|
|
|
|
b.addr = tr_inaddr_any;
|
|
|
|
b.socket = -1;
|
|
|
|
session->public_ipv4 = tr_memdup( &b, sizeof( struct tr_bindinfo ) );
|
2010-03-06 15:05:05 +00:00
|
|
|
tr_webSetInterface( session, &session->public_ipv4->addr );
|
2009-10-23 03:41:36 +00:00
|
|
|
|
|
|
|
str = TR_PREFS_KEY_BIND_ADDRESS_IPV6;
|
|
|
|
tr_bencDictFindStr( settings, TR_PREFS_KEY_BIND_ADDRESS_IPV6, &str );
|
|
|
|
if( !tr_pton( str, &b.addr ) || ( b.addr.type != TR_AF_INET6 ) )
|
|
|
|
b.addr = tr_in6addr_any;
|
|
|
|
b.socket = -1;
|
|
|
|
session->public_ipv6 = tr_memdup( &b, sizeof( struct tr_bindinfo ) );
|
|
|
|
|
|
|
|
/* incoming peer port */
|
|
|
|
if( tr_bencDictFindInt ( settings, TR_PREFS_KEY_PEER_PORT_RANDOM_LOW, &i ) )
|
|
|
|
session->randomPortLow = i;
|
|
|
|
if( tr_bencDictFindInt ( settings, TR_PREFS_KEY_PEER_PORT_RANDOM_HIGH, &i ) )
|
|
|
|
session->randomPortHigh = i;
|
|
|
|
if( tr_bencDictFindBool( settings, TR_PREFS_KEY_PEER_PORT_RANDOM_ON_START, &boolVal ) )
|
|
|
|
tr_sessionSetPeerPortRandomOnStart( session, boolVal );
|
|
|
|
if( !tr_bencDictFindInt( settings, TR_PREFS_KEY_PEER_PORT, &i ) )
|
|
|
|
i = session->peerPort;
|
|
|
|
tr_sessionSetPeerPort( session, boolVal ? getRandomPort( session ) : i );
|
|
|
|
if( tr_bencDictFindBool( settings, TR_PREFS_KEY_PORT_FORWARDING, &boolVal ) )
|
|
|
|
tr_sessionSetPortForwardingEnabled( session, boolVal );
|
|
|
|
|
|
|
|
/* file and peer socket limits */
|
|
|
|
if( tr_bencDictFindInt( settings, TR_PREFS_KEY_PEER_LIMIT_GLOBAL, &i ) )
|
|
|
|
tr_fdSetPeerLimit( session, i );
|
|
|
|
if( tr_bencDictFindInt( settings, TR_PREFS_KEY_OPEN_FILE_LIMIT, &i ) )
|
|
|
|
tr_fdSetFileLimit( session, i );
|
2009-02-06 23:35:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
**/
|
2009-01-09 15:45:44 +00:00
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
if( tr_bencDictFindInt( settings, TR_PREFS_KEY_UPLOAD_SLOTS_PER_TORRENT, &i ) )
|
|
|
|
session->uploadSlotsPerTorrent = i;
|
2009-02-06 23:35:47 +00:00
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
if( tr_bencDictFindInt( settings, TR_PREFS_KEY_USPEED, &i ) )
|
|
|
|
tr_sessionSetSpeedLimit( session, TR_UP, i );
|
|
|
|
if( tr_bencDictFindBool( settings, TR_PREFS_KEY_USPEED_ENABLED, &boolVal ) )
|
|
|
|
tr_sessionLimitSpeed( session, TR_UP, boolVal );
|
2009-02-06 23:35:47 +00:00
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
if( tr_bencDictFindInt( settings, TR_PREFS_KEY_DSPEED, &i ) )
|
|
|
|
tr_sessionSetSpeedLimit( session, TR_DOWN, i );
|
|
|
|
if( tr_bencDictFindBool( settings, TR_PREFS_KEY_DSPEED_ENABLED, &boolVal ) )
|
|
|
|
tr_sessionLimitSpeed( session, TR_DOWN, boolVal );
|
2007-06-22 20:59:23 +00:00
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
if( tr_bencDictFindReal( settings, TR_PREFS_KEY_RATIO, &d ) )
|
|
|
|
tr_sessionSetRatioLimit( session, d );
|
|
|
|
if( tr_bencDictFindBool( settings, TR_PREFS_KEY_RATIO_ENABLED, &boolVal ) )
|
|
|
|
tr_sessionSetRatioLimited( session, boolVal );
|
2009-02-13 18:23:56 +00:00
|
|
|
|
2009-03-25 19:18:00 +00:00
|
|
|
/**
|
2010-02-04 23:39:50 +00:00
|
|
|
*** Turtle Mode
|
2009-03-25 19:18:00 +00:00
|
|
|
**/
|
2009-03-28 16:47:01 +00:00
|
|
|
|
2010-02-04 23:39:50 +00:00
|
|
|
/* update the turtle mode's fields */
|
2009-10-23 03:41:36 +00:00
|
|
|
if( tr_bencDictFindInt( settings, TR_PREFS_KEY_ALT_SPEED_UP, &i ) )
|
2010-02-04 23:39:50 +00:00
|
|
|
turtle->speedLimit[TR_UP] = i;
|
2009-10-23 03:41:36 +00:00
|
|
|
if( tr_bencDictFindInt( settings, TR_PREFS_KEY_ALT_SPEED_DOWN, &i ) )
|
2010-02-04 23:39:50 +00:00
|
|
|
turtle->speedLimit[TR_DOWN] = i;
|
2009-10-23 03:41:36 +00:00
|
|
|
if( tr_bencDictFindInt( settings, TR_PREFS_KEY_ALT_SPEED_TIME_BEGIN, &i ) )
|
2010-02-04 23:39:50 +00:00
|
|
|
turtle->beginMinute = i;
|
2009-10-23 03:41:36 +00:00
|
|
|
if( tr_bencDictFindInt( settings, TR_PREFS_KEY_ALT_SPEED_TIME_END, &i ) )
|
2010-02-04 23:39:50 +00:00
|
|
|
turtle->endMinute = i;
|
2009-10-23 03:41:36 +00:00
|
|
|
if( tr_bencDictFindInt( settings, TR_PREFS_KEY_ALT_SPEED_TIME_DAY, &i ) )
|
2010-02-04 23:39:50 +00:00
|
|
|
turtle->days = i;
|
2009-10-23 03:41:36 +00:00
|
|
|
if( tr_bencDictFindBool( settings, TR_PREFS_KEY_ALT_SPEED_TIME_ENABLED, &boolVal ) )
|
2010-02-04 23:39:50 +00:00
|
|
|
turtle->isClockEnabled = boolVal;
|
2010-02-16 00:00:22 +00:00
|
|
|
if( tr_bencDictFindBool( settings, TR_PREFS_KEY_ALT_SPEED_ENABLED, &boolVal ) )
|
|
|
|
turtle->isEnabled = boolVal;
|
2010-02-08 19:21:07 +00:00
|
|
|
turtleBootstrap( session, turtle );
|
2009-03-28 16:47:01 +00:00
|
|
|
|
2010-01-01 22:35:49 +00:00
|
|
|
data->done = TRUE;
|
2009-10-23 03:41:36 +00:00
|
|
|
}
|
2009-01-26 08:23:11 +00:00
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
void
|
|
|
|
tr_sessionSet( tr_session * session, struct tr_benc * settings )
|
|
|
|
{
|
|
|
|
struct init_data data;
|
2010-01-01 22:35:49 +00:00
|
|
|
data.done = FALSE;
|
2009-10-23 03:41:36 +00:00
|
|
|
data.session = session;
|
|
|
|
data.clientSettings = settings;
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
/* run the rest in the libtransmission thread */
|
|
|
|
tr_runInEventThread( session, sessionSetImpl, &data );
|
2010-01-01 22:35:49 +00:00
|
|
|
while( !data.done )
|
2010-01-01 22:30:36 +00:00
|
|
|
tr_wait_msec( 100 );
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
void
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_sessionSetDownloadDir( tr_session * session, const char * dir )
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-12-14 11:21:11 +00:00
|
|
|
if( session->downloadDir != dir )
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_free( session->downloadDir );
|
|
|
|
session->downloadDir = tr_strdup( dir );
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_sessionGetDownloadDir( const tr_session * session )
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-12-14 11:21:11 +00:00
|
|
|
return session->downloadDir;
|
2007-12-24 07:02:40 +00:00
|
|
|
}
|
|
|
|
|
2007-10-01 15:17:15 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2009-10-20 03:14:44 +00:00
|
|
|
void
|
2009-10-20 04:43:51 +00:00
|
|
|
tr_sessionSetIncompleteFileNamingEnabled( tr_session * session, tr_bool b )
|
2009-10-20 03:14:44 +00:00
|
|
|
{
|
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
assert( tr_isBool( b ) );
|
|
|
|
|
2009-10-20 04:43:51 +00:00
|
|
|
session->isIncompleteFileNamingEnabled = b;
|
2009-10-20 03:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_bool
|
2009-10-20 04:43:51 +00:00
|
|
|
tr_sessionIsIncompleteFileNamingEnabled( const tr_session * session )
|
2009-10-20 03:14:44 +00:00
|
|
|
{
|
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2009-10-20 04:43:51 +00:00
|
|
|
return session->isIncompleteFileNamingEnabled;
|
2009-10-20 03:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
|
2009-10-19 05:05:00 +00:00
|
|
|
void
|
|
|
|
tr_sessionSetIncompleteDir( tr_session * session, const char * dir )
|
|
|
|
{
|
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
|
|
|
if( session->incompleteDir != dir )
|
|
|
|
{
|
|
|
|
tr_free( session->incompleteDir );
|
|
|
|
|
|
|
|
session->incompleteDir = tr_strdup( dir );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char*
|
|
|
|
tr_sessionGetIncompleteDir( const tr_session * session )
|
|
|
|
{
|
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
|
|
|
return session->incompleteDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_sessionSetIncompleteDirEnabled( tr_session * session, tr_bool b )
|
|
|
|
{
|
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
assert( tr_isBool( b ) );
|
|
|
|
|
|
|
|
session->isIncompleteDirEnabled = b;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_bool
|
2009-10-20 03:14:44 +00:00
|
|
|
tr_sessionIsIncompleteDirEnabled( const tr_session * session )
|
2009-10-19 05:05:00 +00:00
|
|
|
{
|
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
|
|
|
return session->isIncompleteDirEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2010-02-23 07:20:57 +00:00
|
|
|
void*
|
|
|
|
tr_sessionGetBuffer( tr_session * session )
|
|
|
|
{
|
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
assert( !session->bufferInUse );
|
|
|
|
assert( tr_amInEventThread( session ) );
|
|
|
|
|
|
|
|
session->bufferInUse = TRUE;
|
|
|
|
return session->buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_sessionReleaseBuffer( tr_session * session )
|
|
|
|
{
|
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
assert( session->bufferInUse );
|
|
|
|
assert( tr_amInEventThread( session ) );
|
|
|
|
|
|
|
|
session->bufferInUse = FALSE;
|
|
|
|
}
|
|
|
|
|
2007-10-01 15:17:15 +00:00
|
|
|
void
|
2010-01-04 09:05:02 +00:00
|
|
|
tr_sessionLock( tr_session * session )
|
2007-10-01 15:17:15 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_lockLock( session->lock );
|
2007-10-01 15:17:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-01-04 09:05:02 +00:00
|
|
|
tr_sessionUnlock( tr_session * session )
|
2007-10-01 15:17:15 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_lockUnlock( session->lock );
|
2007-10-01 15:17:15 +00:00
|
|
|
}
|
|
|
|
|
2008-12-03 07:10:09 +00:00
|
|
|
tr_bool
|
2010-01-04 09:05:02 +00:00
|
|
|
tr_sessionIsLocked( const tr_session * session )
|
2007-10-01 15:17:15 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
return tr_isSession( session ) && tr_lockHave( session->lock );
|
2007-10-01 15:17:15 +00:00
|
|
|
}
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
/***********************************************************************
|
|
|
|
* tr_setBindPort
|
|
|
|
***********************************************************************
|
2008-09-23 19:11:04 +00:00
|
|
|
*
|
2006-07-16 19:39:23 +00:00
|
|
|
**********************************************************************/
|
2007-11-22 06:13:57 +00:00
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
static void
|
2009-05-14 13:42:29 +00:00
|
|
|
setPeerPort( void * session )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2009-05-14 13:42:29 +00:00
|
|
|
tr_torrent * tor = NULL;
|
2009-01-29 16:56:43 +00:00
|
|
|
|
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2009-05-14 13:42:29 +00:00
|
|
|
close_incoming_peer_port( session );
|
|
|
|
open_incoming_peer_port( session );
|
|
|
|
tr_sharedPortChanged( session );
|
|
|
|
|
|
|
|
while(( tor = tr_torrentNext( session, tor )))
|
|
|
|
tr_torrentChangeMyPort( tor );
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
void
|
2009-10-23 03:41:36 +00:00
|
|
|
tr_sessionSetPeerPort( tr_session * session, tr_port port )
|
2008-12-13 23:17:36 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
if( session->peerPort != port )
|
|
|
|
{
|
|
|
|
session->peerPort = port;
|
2009-05-14 13:42:29 +00:00
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
tr_runInEventThread( session, setPeerPort, session );
|
|
|
|
}
|
2008-12-13 23:17:36 +00:00
|
|
|
}
|
|
|
|
|
2009-04-03 04:29:27 +00:00
|
|
|
tr_port
|
|
|
|
tr_sessionGetPeerPort( const tr_session * session )
|
|
|
|
{
|
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
|
|
|
return session->peerPort;
|
|
|
|
}
|
|
|
|
|
2008-12-11 04:41:23 +00:00
|
|
|
tr_port
|
|
|
|
tr_sessionSetPeerPortRandom( tr_session * session )
|
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2009-05-14 13:42:29 +00:00
|
|
|
tr_sessionSetPeerPort( session, getRandomPort( session ) );
|
2008-12-13 23:17:36 +00:00
|
|
|
return session->peerPort;
|
2008-12-11 04:41:23 +00:00
|
|
|
}
|
|
|
|
|
2009-04-03 04:29:27 +00:00
|
|
|
void
|
|
|
|
tr_sessionSetPeerPortRandomOnStart( tr_session * session,
|
|
|
|
tr_bool random )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2009-04-03 04:31:25 +00:00
|
|
|
session->isPortRandom = random;
|
2009-04-03 04:29:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_bool
|
|
|
|
tr_sessionGetPeerPortRandomOnStart( tr_session * session )
|
|
|
|
{
|
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
|
|
|
return session->isPortRandom;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2008-05-22 12:31:47 +00:00
|
|
|
tr_port_forwarding
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_sessionGetPortForwarding( const tr_session * session )
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-12-14 11:21:11 +00:00
|
|
|
return tr_sharedTraversalStatus( session->shared );
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
2007-07-20 08:33:59 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2009-03-01 13:56:22 +00:00
|
|
|
static void
|
|
|
|
updateSeedRatio( tr_session * session )
|
|
|
|
{
|
|
|
|
tr_torrent * tor = NULL;
|
|
|
|
|
|
|
|
while(( tor = tr_torrentNext( session, tor )))
|
2009-04-09 14:10:31 +00:00
|
|
|
tor->needsSeedRatioCheck = TRUE;
|
2009-03-01 13:56:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_sessionSetRatioLimited( tr_session * session, tr_bool isLimited )
|
|
|
|
{
|
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
|
|
|
session->isRatioLimited = isLimited;
|
|
|
|
updateSeedRatio( session );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_sessionSetRatioLimit( tr_session * session, double desiredRatio )
|
|
|
|
{
|
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
|
|
|
session->desiredRatio = desiredRatio;
|
|
|
|
updateSeedRatio( session );
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_bool
|
|
|
|
tr_sessionIsRatioLimited( const tr_session * session )
|
|
|
|
{
|
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
|
|
|
return session->isRatioLimited;
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
tr_sessionGetRatioLimit( const tr_session * session )
|
|
|
|
{
|
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
|
|
|
return session->desiredRatio;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
2010-02-04 23:39:50 +00:00
|
|
|
****
|
2009-03-28 16:47:01 +00:00
|
|
|
**** SPEED LIMITS
|
2010-02-04 23:39:50 +00:00
|
|
|
****
|
2009-03-01 13:56:22 +00:00
|
|
|
***/
|
|
|
|
|
2009-03-28 16:47:01 +00:00
|
|
|
tr_bool
|
|
|
|
tr_sessionGetActiveSpeedLimit( const tr_session * session, tr_direction dir, int * setme )
|
|
|
|
{
|
|
|
|
int isLimited = TRUE;
|
|
|
|
|
2009-06-30 18:08:50 +00:00
|
|
|
if( !tr_isSession( session ) )
|
|
|
|
return FALSE;
|
|
|
|
|
2009-03-28 16:47:01 +00:00
|
|
|
if( tr_sessionUsesAltSpeed( session ) )
|
|
|
|
*setme = tr_sessionGetAltSpeed( session, dir );
|
|
|
|
else if( tr_sessionIsSpeedLimited( session, dir ) )
|
|
|
|
*setme = tr_sessionGetSpeedLimit( session, dir );
|
|
|
|
else
|
|
|
|
isLimited = FALSE;
|
|
|
|
|
|
|
|
return isLimited;
|
|
|
|
}
|
|
|
|
|
2008-12-13 22:52:15 +00:00
|
|
|
static void
|
|
|
|
updateBandwidth( tr_session * session, tr_direction dir )
|
|
|
|
{
|
2009-05-13 15:54:04 +00:00
|
|
|
int limit = 0;
|
2009-03-28 16:47:01 +00:00
|
|
|
const tr_bool isLimited = tr_sessionGetActiveSpeedLimit( session, dir, &limit );
|
2009-03-25 19:18:00 +00:00
|
|
|
const tr_bool zeroCase = isLimited && !limit;
|
|
|
|
|
|
|
|
tr_bandwidthSetLimited( session->bandwidth, dir, isLimited && !zeroCase );
|
|
|
|
|
|
|
|
tr_bandwidthSetDesiredSpeed( session->bandwidth, dir, limit );
|
|
|
|
}
|
|
|
|
|
2010-03-10 22:19:31 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
MINUTES_PER_HOUR = 60,
|
|
|
|
MINUTES_PER_DAY = MINUTES_PER_HOUR * 24,
|
|
|
|
MINUTES_PER_WEEK = MINUTES_PER_DAY * 7
|
|
|
|
};
|
|
|
|
|
2010-02-04 23:39:50 +00:00
|
|
|
static void
|
2010-03-10 22:19:31 +00:00
|
|
|
turtleUpdateTable( struct tr_turtle_info * t )
|
2010-02-04 23:39:50 +00:00
|
|
|
{
|
2010-02-07 17:13:38 +00:00
|
|
|
int day;
|
2010-03-10 22:19:31 +00:00
|
|
|
tr_bitfield * b = &t->minutes;
|
2010-02-04 23:39:50 +00:00
|
|
|
|
2010-03-10 22:19:31 +00:00
|
|
|
tr_bitfieldClear( b );
|
2010-02-04 23:39:50 +00:00
|
|
|
|
2010-03-10 22:19:31 +00:00
|
|
|
for( day=0; day<7; ++day )
|
|
|
|
{
|
|
|
|
if( t->days & (1<<day) )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
const time_t begin = t->beginMinute;
|
|
|
|
time_t end = t->endMinute;
|
|
|
|
|
|
|
|
if( end <= begin )
|
|
|
|
end += MINUTES_PER_DAY;
|
|
|
|
|
|
|
|
for( i=begin; i<end; ++i )
|
|
|
|
tr_bitfieldAdd( b, (i+day*MINUTES_PER_DAY) % MINUTES_PER_WEEK );
|
|
|
|
}
|
2010-02-04 23:39:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-28 16:47:01 +00:00
|
|
|
static void
|
|
|
|
altSpeedToggled( void * vsession )
|
|
|
|
{
|
|
|
|
tr_session * session = vsession;
|
2010-02-04 23:39:50 +00:00
|
|
|
struct tr_turtle_info * t = &session->turtle;
|
2009-03-28 16:47:01 +00:00
|
|
|
|
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
|
|
|
updateBandwidth( session, TR_UP );
|
|
|
|
updateBandwidth( session, TR_DOWN );
|
|
|
|
|
2010-02-04 23:39:50 +00:00
|
|
|
if( t->callback != NULL )
|
|
|
|
(*t->callback)( session, t->isEnabled, t->changedByUser, t->callbackUserData );
|
2009-03-28 16:47:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-02-04 23:39:50 +00:00
|
|
|
useAltSpeed( tr_session * s, struct tr_turtle_info * t, tr_bool enabled, tr_bool byUser )
|
2009-03-28 16:47:01 +00:00
|
|
|
{
|
2010-02-04 23:39:50 +00:00
|
|
|
assert( tr_isSession( s ) );
|
|
|
|
assert( t != NULL );
|
|
|
|
assert( tr_isBool( enabled ) );
|
|
|
|
assert( tr_isBool( byUser ) );
|
2009-03-28 16:47:01 +00:00
|
|
|
|
2010-02-04 23:39:50 +00:00
|
|
|
if( t->isEnabled != enabled )
|
|
|
|
{
|
|
|
|
t->isEnabled = enabled;
|
|
|
|
t->changedByUser = byUser;
|
|
|
|
tr_runInEventThread( s, altSpeedToggled, s );
|
|
|
|
}
|
|
|
|
}
|
2009-03-28 16:47:01 +00:00
|
|
|
|
2010-03-10 22:19:31 +00:00
|
|
|
static tr_bool
|
|
|
|
testTurtleTime( const struct tr_turtle_info * t )
|
2009-03-25 19:18:00 +00:00
|
|
|
{
|
2010-03-10 22:19:31 +00:00
|
|
|
struct tm tm;
|
|
|
|
size_t minute_of_the_week;
|
2010-02-04 23:39:50 +00:00
|
|
|
const time_t now = tr_time( );
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2010-03-10 22:19:31 +00:00
|
|
|
tr_localtime_r( &now, &tm );
|
|
|
|
|
|
|
|
minute_of_the_week = tm.tm_wday * MINUTES_PER_DAY
|
|
|
|
+ tm.tm_hour * MINUTES_PER_HOUR
|
|
|
|
+ tm.tm_min;
|
|
|
|
|
|
|
|
if( minute_of_the_week >= MINUTES_PER_WEEK ) /* leap minutes? */
|
|
|
|
minute_of_the_week = MINUTES_PER_WEEK - 1;
|
|
|
|
|
|
|
|
return tr_bitfieldHasFast( &t->minutes, minute_of_the_week );
|
|
|
|
}
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2010-03-10 22:19:31 +00:00
|
|
|
static void
|
|
|
|
turtleCheckClock( tr_session * session, struct tr_turtle_info * t, tr_bool byUser )
|
|
|
|
{
|
|
|
|
if( t->isClockEnabled )
|
2009-03-25 19:18:00 +00:00
|
|
|
{
|
2010-03-10 22:19:31 +00:00
|
|
|
const tr_bool hit = testTurtleTime( t );
|
2010-02-08 19:21:07 +00:00
|
|
|
|
2010-03-10 22:19:31 +00:00
|
|
|
if( hit != t->isEnabled )
|
2009-03-28 16:47:01 +00:00
|
|
|
{
|
2010-03-10 22:19:31 +00:00
|
|
|
tr_inf( "Time to turn %s turtle mode!", (hit?"on":"off") );
|
|
|
|
useAltSpeed( session, t, hit, byUser );
|
2009-03-28 16:47:01 +00:00
|
|
|
}
|
2009-03-25 19:18:00 +00:00
|
|
|
}
|
2010-02-04 23:39:50 +00:00
|
|
|
}
|
2009-03-25 19:18:00 +00:00
|
|
|
|
2010-02-04 23:39:50 +00:00
|
|
|
/* Called after the turtle's fields are loaded from an outside source.
|
|
|
|
* It initializes the implementation fields
|
|
|
|
* and turns on turtle mode if the clock settings say to. */
|
|
|
|
static void
|
2010-02-08 19:21:07 +00:00
|
|
|
turtleBootstrap( tr_session * session, struct tr_turtle_info * turtle )
|
2010-02-04 23:39:50 +00:00
|
|
|
{
|
2010-02-19 06:19:47 +00:00
|
|
|
turtle->changedByUser = FALSE;
|
|
|
|
|
2010-03-10 22:19:31 +00:00
|
|
|
tr_bitfieldConstruct( &turtle->minutes, MINUTES_PER_WEEK );
|
|
|
|
|
|
|
|
turtleUpdateTable( turtle );
|
|
|
|
|
2010-02-16 00:00:22 +00:00
|
|
|
if( turtle->isClockEnabled )
|
2010-03-10 22:19:31 +00:00
|
|
|
turtle->isEnabled = testTurtleTime( turtle );
|
2010-02-16 00:00:22 +00:00
|
|
|
|
2010-02-19 06:19:47 +00:00
|
|
|
altSpeedToggled( session );
|
2008-12-13 22:52:15 +00:00
|
|
|
}
|
|
|
|
|
2009-03-28 16:47:01 +00:00
|
|
|
/***
|
|
|
|
**** Primary session speed limits
|
|
|
|
***/
|
|
|
|
|
2007-07-20 08:33:59 +00:00
|
|
|
void
|
2009-03-28 16:47:01 +00:00
|
|
|
tr_sessionSetSpeedLimit( tr_session * s, tr_direction d, int KB_s )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2009-03-28 16:47:01 +00:00
|
|
|
assert( tr_isSession( s ) );
|
|
|
|
assert( tr_isDirection( d ) );
|
|
|
|
assert( KB_s >= 0 );
|
|
|
|
|
|
|
|
s->speedLimit[d] = KB_s;
|
|
|
|
|
|
|
|
updateBandwidth( s, d );
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
tr_sessionGetSpeedLimit( const tr_session * s, tr_direction d )
|
|
|
|
{
|
|
|
|
assert( tr_isSession( s ) );
|
|
|
|
assert( tr_isDirection( d ) );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2009-03-28 16:47:01 +00:00
|
|
|
return s->speedLimit[d];
|
2008-05-12 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
2007-07-20 08:33:59 +00:00
|
|
|
void
|
2009-03-28 16:47:01 +00:00
|
|
|
tr_sessionLimitSpeed( tr_session * s, tr_direction d, tr_bool b )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2009-03-28 16:47:01 +00:00
|
|
|
assert( tr_isSession( s ) );
|
|
|
|
assert( tr_isDirection( d ) );
|
|
|
|
assert( tr_isBool( b ) );
|
2008-12-13 22:52:15 +00:00
|
|
|
|
2009-03-28 16:47:01 +00:00
|
|
|
s->speedLimitEnabled[d] = b;
|
|
|
|
|
|
|
|
updateBandwidth( s, d );
|
2008-12-13 22:52:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_bool
|
2009-03-28 16:47:01 +00:00
|
|
|
tr_sessionIsSpeedLimited( const tr_session * s, tr_direction d )
|
2008-12-13 22:52:15 +00:00
|
|
|
{
|
2009-03-28 16:47:01 +00:00
|
|
|
assert( tr_isSession( s ) );
|
|
|
|
assert( tr_isDirection( d ) );
|
2008-12-13 22:52:15 +00:00
|
|
|
|
2009-03-28 16:47:01 +00:00
|
|
|
return s->speedLimitEnabled[d];
|
2007-01-08 21:53:55 +00:00
|
|
|
}
|
|
|
|
|
2009-03-28 16:47:01 +00:00
|
|
|
/***
|
2009-08-10 20:04:08 +00:00
|
|
|
**** Alternative speed limits that are used during scheduled times
|
2009-03-28 16:47:01 +00:00
|
|
|
***/
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_sessionSetAltSpeed( tr_session * s, tr_direction d, int KB_s )
|
2007-07-20 08:33:59 +00:00
|
|
|
{
|
2009-03-28 16:47:01 +00:00
|
|
|
assert( tr_isSession( s ) );
|
|
|
|
assert( tr_isDirection( d ) );
|
|
|
|
assert( KB_s >= 0 );
|
|
|
|
|
2010-02-04 23:39:50 +00:00
|
|
|
s->turtle.speedLimit[d] = KB_s;
|
2008-12-13 22:52:15 +00:00
|
|
|
|
2009-03-28 16:47:01 +00:00
|
|
|
updateBandwidth( s, d );
|
2007-07-20 08:33:59 +00:00
|
|
|
}
|
|
|
|
|
2009-03-28 16:47:01 +00:00
|
|
|
int
|
|
|
|
tr_sessionGetAltSpeed( const tr_session * s, tr_direction d )
|
2009-03-25 19:50:58 +00:00
|
|
|
{
|
2009-03-28 16:47:01 +00:00
|
|
|
assert( tr_isSession( s ) );
|
|
|
|
assert( tr_isDirection( d ) );
|
|
|
|
|
2010-02-04 23:39:50 +00:00
|
|
|
return s->turtle.speedLimit[d];
|
2009-03-25 19:50:58 +00:00
|
|
|
}
|
|
|
|
|
2010-02-04 23:39:50 +00:00
|
|
|
static void
|
|
|
|
userPokedTheClock( tr_session * s, struct tr_turtle_info * t )
|
2009-03-25 19:18:00 +00:00
|
|
|
{
|
2010-02-04 23:39:50 +00:00
|
|
|
tr_dbg( "Refreshing the turtle mode clock due to user changes" );
|
2009-03-29 16:44:59 +00:00
|
|
|
|
2010-03-10 22:19:31 +00:00
|
|
|
turtleUpdateTable( t );
|
2009-03-25 19:18:00 +00:00
|
|
|
|
2010-03-10 22:19:31 +00:00
|
|
|
if( t->isClockEnabled )
|
|
|
|
useAltSpeed( s, t, testTurtleTime( t ), TRUE );
|
2009-03-25 19:18:00 +00:00
|
|
|
}
|
2010-02-04 23:39:50 +00:00
|
|
|
|
2009-03-30 17:25:35 +00:00
|
|
|
void
|
|
|
|
tr_sessionUseAltSpeedTime( tr_session * s, tr_bool b )
|
|
|
|
{
|
2010-02-04 23:39:50 +00:00
|
|
|
struct tr_turtle_info * t = &s->turtle;
|
|
|
|
|
|
|
|
assert( tr_isSession( s ) );
|
|
|
|
assert( tr_isBool ( b ) );
|
|
|
|
|
|
|
|
if( t->isClockEnabled != b ) {
|
|
|
|
t->isClockEnabled = b;
|
|
|
|
userPokedTheClock( s, t );
|
|
|
|
}
|
2009-03-30 17:25:35 +00:00
|
|
|
}
|
2009-03-25 19:18:00 +00:00
|
|
|
|
|
|
|
tr_bool
|
2009-03-28 16:47:01 +00:00
|
|
|
tr_sessionUsesAltSpeedTime( const tr_session * s )
|
2009-03-25 19:18:00 +00:00
|
|
|
{
|
2009-03-28 16:47:01 +00:00
|
|
|
assert( tr_isSession( s ) );
|
2009-03-25 19:18:00 +00:00
|
|
|
|
2010-02-04 23:39:50 +00:00
|
|
|
return s->turtle.isClockEnabled;
|
2009-03-25 19:18:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-02-04 23:39:50 +00:00
|
|
|
tr_sessionSetAltSpeedBegin( tr_session * s, int minute )
|
2009-03-25 19:18:00 +00:00
|
|
|
{
|
2009-03-28 16:47:01 +00:00
|
|
|
assert( tr_isSession( s ) );
|
2010-02-04 23:39:50 +00:00
|
|
|
assert( 0<=minute && minute<(60*24) );
|
2009-03-25 19:50:58 +00:00
|
|
|
|
2010-02-04 23:39:50 +00:00
|
|
|
if( s->turtle.beginMinute != minute ) {
|
|
|
|
s->turtle.beginMinute = minute;
|
|
|
|
userPokedTheClock( s, &s->turtle );
|
2009-03-28 16:47:01 +00:00
|
|
|
}
|
2009-03-25 19:18:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-03-28 16:47:01 +00:00
|
|
|
tr_sessionGetAltSpeedBegin( const tr_session * s )
|
2009-03-25 19:18:00 +00:00
|
|
|
{
|
2009-03-28 16:47:01 +00:00
|
|
|
assert( tr_isSession( s ) );
|
2009-03-25 19:18:00 +00:00
|
|
|
|
2010-02-04 23:39:50 +00:00
|
|
|
return s->turtle.beginMinute;
|
2009-03-25 19:18:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-02-04 23:39:50 +00:00
|
|
|
tr_sessionSetAltSpeedEnd( tr_session * s, int minute )
|
2009-03-25 19:18:00 +00:00
|
|
|
{
|
2009-03-28 16:47:01 +00:00
|
|
|
assert( tr_isSession( s ) );
|
2010-02-04 23:39:50 +00:00
|
|
|
assert( 0<=minute && minute<(60*24) );
|
2009-03-25 19:18:00 +00:00
|
|
|
|
2010-02-04 23:39:50 +00:00
|
|
|
if( s->turtle.endMinute != minute ) {
|
|
|
|
s->turtle.endMinute = minute;
|
|
|
|
userPokedTheClock( s, &s->turtle );
|
2009-03-28 16:47:01 +00:00
|
|
|
}
|
2009-03-25 19:18:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-03-28 16:47:01 +00:00
|
|
|
tr_sessionGetAltSpeedEnd( const tr_session * s )
|
2009-03-25 19:18:00 +00:00
|
|
|
{
|
2009-03-28 16:47:01 +00:00
|
|
|
assert( tr_isSession( s ) );
|
2009-03-25 19:18:00 +00:00
|
|
|
|
2010-02-04 23:39:50 +00:00
|
|
|
return s->turtle.endMinute;
|
2009-03-25 19:18:00 +00:00
|
|
|
}
|
|
|
|
|
2009-04-04 02:17:39 +00:00
|
|
|
void
|
2010-02-04 23:39:50 +00:00
|
|
|
tr_sessionSetAltSpeedDay( tr_session * s, tr_sched_day days )
|
2009-04-04 02:17:39 +00:00
|
|
|
{
|
|
|
|
assert( tr_isSession( s ) );
|
|
|
|
|
2010-02-04 23:39:50 +00:00
|
|
|
if( s->turtle.days != days ) {
|
|
|
|
s->turtle.days = days;
|
|
|
|
userPokedTheClock( s, &s->turtle );
|
2009-04-04 02:17:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_sched_day
|
|
|
|
tr_sessionGetAltSpeedDay( const tr_session * s )
|
|
|
|
{
|
|
|
|
assert( tr_isSession( s ) );
|
|
|
|
|
2010-02-04 23:39:50 +00:00
|
|
|
return s->turtle.days;
|
2009-04-04 02:17:39 +00:00
|
|
|
}
|
|
|
|
|
2009-03-30 17:25:35 +00:00
|
|
|
void
|
|
|
|
tr_sessionUseAltSpeed( tr_session * session, tr_bool enabled )
|
|
|
|
{
|
2010-02-04 23:39:50 +00:00
|
|
|
useAltSpeed( session, &session->turtle, enabled, TRUE );
|
2009-03-30 17:25:35 +00:00
|
|
|
}
|
2009-03-25 19:18:00 +00:00
|
|
|
|
2009-03-28 16:47:01 +00:00
|
|
|
tr_bool
|
|
|
|
tr_sessionUsesAltSpeed( const tr_session * s )
|
2009-03-25 19:18:00 +00:00
|
|
|
{
|
2009-03-28 16:47:01 +00:00
|
|
|
assert( tr_isSession( s ) );
|
2009-03-25 19:18:00 +00:00
|
|
|
|
2010-02-04 23:39:50 +00:00
|
|
|
return s->turtle.isEnabled;
|
2009-03-25 19:18:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-03-28 16:47:01 +00:00
|
|
|
tr_sessionSetAltSpeedFunc( tr_session * session,
|
|
|
|
tr_altSpeedFunc func,
|
|
|
|
void * userData )
|
2009-03-25 19:18:00 +00:00
|
|
|
{
|
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2010-02-04 23:39:50 +00:00
|
|
|
session->turtle.callback = func;
|
|
|
|
session->turtle.callbackUserData = userData;
|
2009-03-25 19:18:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-03-28 16:47:01 +00:00
|
|
|
tr_sessionClearAltSpeedFunc( tr_session * session )
|
2009-03-25 19:18:00 +00:00
|
|
|
{
|
2009-03-28 16:47:01 +00:00
|
|
|
tr_sessionSetAltSpeedFunc( session, NULL, NULL );
|
2009-03-25 19:18:00 +00:00
|
|
|
}
|
|
|
|
|
2008-05-12 16:33:17 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2007-12-20 21:44:16 +00:00
|
|
|
|
|
|
|
void
|
2009-03-01 13:56:22 +00:00
|
|
|
tr_sessionSetPeerLimit( tr_session * session, uint16_t maxGlobalPeers )
|
2007-12-20 21:44:16 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
tr_fdSetPeerLimit( session, maxGlobalPeers );
|
2007-12-20 21:44:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t
|
2009-01-29 16:56:43 +00:00
|
|
|
tr_sessionGetPeerLimit( const tr_session * session )
|
2007-12-20 21:44:16 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
return tr_fdGetPeerLimit( session );
|
2007-12-20 21:44:16 +00:00
|
|
|
}
|
|
|
|
|
2008-12-14 22:57:08 +00:00
|
|
|
void
|
|
|
|
tr_sessionSetPeerLimitPerTorrent( tr_session * session, uint16_t n )
|
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-12-14 22:57:08 +00:00
|
|
|
session->peerLimitPerTorrent = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t
|
|
|
|
tr_sessionGetPeerLimitPerTorrent( const tr_session * session )
|
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-12-14 22:57:08 +00:00
|
|
|
return session->peerLimitPerTorrent;
|
|
|
|
}
|
|
|
|
|
2008-05-12 16:33:17 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-11-08 02:49:04 +00:00
|
|
|
double
|
|
|
|
tr_sessionGetPieceSpeed( const tr_session * session, tr_direction dir )
|
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
return tr_isSession( session ) ? tr_bandwidthGetPieceSpeed( session->bandwidth, 0, dir ) : 0.0;
|
2008-11-08 02:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
tr_sessionGetRawSpeed( const tr_session * session, tr_direction dir )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2010-03-06 15:05:05 +00:00
|
|
|
return tr_isSession( session ) ? tr_bandwidthGetRawSpeed( session->bandwidth, 0, dir ) : 0.0;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 23:33:46 +00:00
|
|
|
int
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_sessionCountTorrents( const tr_session * session )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
return tr_isSession( session ) ? session->torrentCount : 0;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 17:22:26 +00:00
|
|
|
static int
|
2008-12-14 11:21:11 +00:00
|
|
|
compareTorrentByCur( const void * va, const void * vb )
|
2008-05-30 17:22:26 +00:00
|
|
|
{
|
|
|
|
const tr_torrent * a = *(const tr_torrent**)va;
|
|
|
|
const tr_torrent * b = *(const tr_torrent**)vb;
|
2008-09-23 19:11:04 +00:00
|
|
|
const uint64_t aCur = a->downloadedCur + a->uploadedCur;
|
|
|
|
const uint64_t bCur = b->downloadedCur + b->uploadedCur;
|
2008-08-22 14:52:20 +00:00
|
|
|
|
|
|
|
if( aCur != bCur )
|
|
|
|
return aCur > bCur ? -1 : 1; /* close the biggest torrents first */
|
|
|
|
|
|
|
|
return 0;
|
2008-05-30 17:22:26 +00:00
|
|
|
}
|
|
|
|
|
2009-12-01 00:07:18 +00:00
|
|
|
static void closeBlocklists( tr_session * );
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
static void
|
2009-05-19 18:38:26 +00:00
|
|
|
sessionCloseImpl( void * vsession )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_session * session = vsession;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_torrent * tor;
|
|
|
|
int i, n;
|
2008-05-30 17:22:26 +00:00
|
|
|
tr_torrent ** torrents;
|
2007-11-10 22:29:28 +00:00
|
|
|
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2009-05-14 13:42:29 +00:00
|
|
|
free_incoming_peer_port( session );
|
|
|
|
|
2009-05-19 18:38:26 +00:00
|
|
|
if( session->isDHTEnabled )
|
|
|
|
tr_dhtUninit( session );
|
|
|
|
|
2009-08-13 14:47:56 +00:00
|
|
|
evtimer_del( session->saveTimer );
|
|
|
|
tr_free( session->saveTimer );
|
|
|
|
session->saveTimer = NULL;
|
|
|
|
|
2009-11-26 18:47:08 +00:00
|
|
|
evtimer_del( session->nowTimer );
|
|
|
|
tr_free( session->nowTimer );
|
|
|
|
session->nowTimer = NULL;
|
|
|
|
|
2009-04-16 13:10:25 +00:00
|
|
|
tr_verifyClose( session );
|
2009-05-14 13:42:29 +00:00
|
|
|
tr_sharedClose( session );
|
2008-08-01 13:46:03 +00:00
|
|
|
tr_rpcClose( &session->rpcServer );
|
2007-11-29 02:31:21 +00:00
|
|
|
|
2008-05-30 17:22:26 +00:00
|
|
|
/* close the torrents. get the most active ones first so that
|
|
|
|
* if we can't get them all closed in a reasonable amount of time,
|
|
|
|
* at least we get the most important ones first. */
|
|
|
|
tor = NULL;
|
2008-08-01 13:46:03 +00:00
|
|
|
n = session->torrentCount;
|
2008-09-23 19:11:04 +00:00
|
|
|
torrents = tr_new( tr_torrent *, session->torrentCount );
|
|
|
|
for( i = 0; i < n; ++i )
|
2008-08-01 13:46:03 +00:00
|
|
|
torrents[i] = tor = tr_torrentNext( session, tor );
|
2008-09-23 19:11:04 +00:00
|
|
|
qsort( torrents, n, sizeof( tr_torrent* ), compareTorrentByCur );
|
|
|
|
for( i = 0; i < n; ++i )
|
2008-05-30 17:22:26 +00:00
|
|
|
tr_torrentFree( torrents[i] );
|
|
|
|
tr_free( torrents );
|
2007-11-10 22:29:28 +00:00
|
|
|
|
2009-09-26 23:54:30 +00:00
|
|
|
tr_announcerClose( session );
|
|
|
|
tr_statsClose( session );
|
2008-08-01 13:46:03 +00:00
|
|
|
tr_peerMgrFree( session->peerMgr );
|
2010-03-06 15:05:05 +00:00
|
|
|
tr_webClose( session, TR_WEB_CLOSE_WHEN_IDLE );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2009-12-01 00:07:18 +00:00
|
|
|
closeBlocklists( session );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2010-01-14 14:22:43 +00:00
|
|
|
tr_fdClose( session );
|
|
|
|
|
2008-08-01 13:46:03 +00:00
|
|
|
session->isClosed = TRUE;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2007-11-05 05:42:25 +00:00
|
|
|
|
|
|
|
static int
|
2010-02-11 04:07:13 +00:00
|
|
|
deadlineReached( const time_t deadline )
|
2007-11-05 05:42:25 +00:00
|
|
|
{
|
2010-02-11 04:07:13 +00:00
|
|
|
return time( NULL ) >= deadline;
|
2007-11-05 05:42:25 +00:00
|
|
|
}
|
|
|
|
|
2009-11-30 04:58:03 +00:00
|
|
|
#define SHUTDOWN_MAX_SECONDS 20
|
2007-11-29 00:43:58 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
void
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_sessionClose( tr_session * session )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2010-02-11 04:07:13 +00:00
|
|
|
const time_t deadline = time( NULL ) + SHUTDOWN_MAX_SECONDS;
|
2007-11-05 05:42:25 +00:00
|
|
|
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-08-01 13:46:03 +00:00
|
|
|
dbgmsg( "shutting down transmission session %p", session );
|
2008-02-04 19:54:47 +00:00
|
|
|
|
2008-08-01 13:46:03 +00:00
|
|
|
/* close the session */
|
2009-05-19 18:38:26 +00:00
|
|
|
tr_runInEventThread( session, sessionCloseImpl, session );
|
2008-09-23 19:11:04 +00:00
|
|
|
while( !session->isClosed && !deadlineReached( deadline ) )
|
|
|
|
{
|
2009-11-30 04:58:03 +00:00
|
|
|
dbgmsg( "waiting for the libtransmission thread to finish" );
|
2010-01-01 22:30:36 +00:00
|
|
|
tr_wait_msec( 100 );
|
2008-08-01 13:46:03 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-08-01 13:46:03 +00:00
|
|
|
/* "shared" and "tracker" have live sockets,
|
|
|
|
* so we need to keep the transmission thread alive
|
|
|
|
* for a bit while they tell the router & tracker
|
|
|
|
* that we're closing now */
|
2010-03-06 15:05:05 +00:00
|
|
|
while( ( session->shared || session->web || session->announcer )
|
|
|
|
&& !deadlineReached( deadline ) )
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2009-09-25 21:05:59 +00:00
|
|
|
dbgmsg( "waiting on port unmap (%p) or announcer (%p)",
|
|
|
|
session->shared, session->announcer );
|
2010-01-01 22:30:36 +00:00
|
|
|
tr_wait_msec( 100 );
|
2008-08-01 13:46:03 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2010-03-06 15:05:05 +00:00
|
|
|
tr_webClose( session, TR_WEB_CLOSE_NOW );
|
|
|
|
|
2008-08-01 13:46:03 +00:00
|
|
|
/* close the libtransmission thread */
|
|
|
|
tr_eventClose( session );
|
2009-11-30 04:58:03 +00:00
|
|
|
while( session->events != NULL )
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2009-11-30 04:58:03 +00:00
|
|
|
static tr_bool forced = FALSE;
|
|
|
|
dbgmsg( "waiting for libtransmission thread to finish" );
|
2010-02-11 04:07:13 +00:00
|
|
|
tr_wait_msec( 500 );
|
2009-11-30 04:58:03 +00:00
|
|
|
if( deadlineReached( deadline ) && !forced )
|
|
|
|
{
|
|
|
|
event_loopbreak( );
|
|
|
|
forced = TRUE;
|
2010-02-11 04:07:13 +00:00
|
|
|
|
|
|
|
if( time( NULL ) >= deadline + 3 )
|
|
|
|
break;
|
2009-11-30 04:58:03 +00:00
|
|
|
}
|
2008-08-01 13:46:03 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-08-01 13:46:03 +00:00
|
|
|
/* free the session memory */
|
2009-04-04 05:29:08 +00:00
|
|
|
tr_bencFree( &session->removedTorrents );
|
2008-11-25 21:35:17 +00:00
|
|
|
tr_bandwidthFree( session->bandwidth );
|
2010-03-10 22:19:31 +00:00
|
|
|
tr_bitfieldDestruct( &session->turtle.minutes );
|
2008-08-01 13:46:03 +00:00
|
|
|
tr_lockFree( session->lock );
|
2009-06-09 21:57:11 +00:00
|
|
|
if( session->metainfoLookup ) {
|
|
|
|
tr_bencFree( session->metainfoLookup );
|
|
|
|
tr_free( session->metainfoLookup );
|
|
|
|
}
|
2010-02-23 07:20:57 +00:00
|
|
|
tr_free( session->buffer );
|
2008-08-01 13:46:03 +00:00
|
|
|
tr_free( session->tag );
|
|
|
|
tr_free( session->configDir );
|
|
|
|
tr_free( session->resumeDir );
|
|
|
|
tr_free( session->torrentDir );
|
|
|
|
tr_free( session->downloadDir );
|
2009-11-25 04:44:12 +00:00
|
|
|
tr_free( session->incompleteDir );
|
2008-08-01 13:46:03 +00:00
|
|
|
tr_free( session->proxy );
|
|
|
|
tr_free( session->proxyUsername );
|
|
|
|
tr_free( session->proxyPassword );
|
|
|
|
tr_free( session );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2007-07-21 18:46:54 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrent **
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_sessionLoadTorrents( tr_session * session,
|
|
|
|
tr_ctor * ctor,
|
|
|
|
int * setmeCount )
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
|
|
|
int i, n = 0;
|
|
|
|
struct stat sb;
|
|
|
|
DIR * odir = NULL;
|
2008-12-14 11:21:11 +00:00
|
|
|
const char * dirname = tr_getTorrentDir( session );
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrent ** torrents;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_list * l = NULL, *list = NULL;
|
2007-07-21 18:46:54 +00:00
|
|
|
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2007-12-22 17:30:31 +00:00
|
|
|
tr_ctorSetSave( ctor, FALSE ); /* since we already have them */
|
|
|
|
|
2007-12-21 22:18:40 +00:00
|
|
|
if( !stat( dirname, &sb )
|
2008-09-23 19:11:04 +00:00
|
|
|
&& S_ISDIR( sb.st_mode )
|
|
|
|
&& ( ( odir = opendir ( dirname ) ) ) )
|
2007-07-21 18:46:54 +00:00
|
|
|
{
|
|
|
|
struct dirent *d;
|
2008-09-23 19:11:04 +00:00
|
|
|
for( d = readdir( odir ); d != NULL; d = readdir( odir ) )
|
2007-07-21 18:46:54 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
if( d->d_name && d->d_name[0] != '.' ) /* skip dotfiles, ., and ..
|
|
|
|
*/
|
2007-07-21 18:46:54 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrent * tor;
|
2008-10-14 03:03:29 +00:00
|
|
|
char * path = tr_buildPath( dirname, d->d_name, NULL );
|
|
|
|
tr_ctorSetMetainfoFromFile( ctor, path );
|
2009-04-02 17:30:29 +00:00
|
|
|
if(( tor = tr_torrentNew( ctor, NULL )))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2008-07-25 19:55:09 +00:00
|
|
|
tr_list_append( &list, tor );
|
|
|
|
++n;
|
2007-07-21 18:46:54 +00:00
|
|
|
}
|
2008-10-14 03:03:29 +00:00
|
|
|
tr_free( path );
|
2007-07-21 18:46:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir( odir );
|
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
torrents = tr_new( tr_torrent *, n );
|
|
|
|
for( i = 0, l = list; l != NULL; l = l->next )
|
2007-09-20 16:32:01 +00:00
|
|
|
torrents[i++] = (tr_torrent*) l->data;
|
2008-09-23 19:11:04 +00:00
|
|
|
assert( i == n );
|
2007-07-21 18:46:54 +00:00
|
|
|
|
2007-09-28 14:27:56 +00:00
|
|
|
tr_list_free( &list, NULL );
|
2007-07-21 18:46:54 +00:00
|
|
|
|
2008-03-19 20:34:35 +00:00
|
|
|
if( n )
|
|
|
|
tr_inf( _( "Loaded %d torrents" ), n );
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
if( setmeCount )
|
|
|
|
*setmeCount = n;
|
2007-07-21 18:46:54 +00:00
|
|
|
return torrents;
|
|
|
|
}
|
2007-12-24 05:03:40 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
void
|
2008-12-03 07:10:09 +00:00
|
|
|
tr_sessionSetPexEnabled( tr_session * session,
|
|
|
|
tr_bool enabled )
|
2007-12-24 05:03:40 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-12-03 07:10:09 +00:00
|
|
|
session->isPexEnabled = enabled != 0;
|
2007-12-24 05:03:40 +00:00
|
|
|
}
|
|
|
|
|
2008-12-03 07:10:09 +00:00
|
|
|
tr_bool
|
|
|
|
tr_sessionIsPexEnabled( const tr_session * session )
|
2007-12-24 05:03:40 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-12-03 07:10:09 +00:00
|
|
|
return session->isPexEnabled;
|
2007-12-24 05:03:40 +00:00
|
|
|
}
|
2008-04-01 19:20:21 +00:00
|
|
|
|
2009-06-21 08:57:26 +00:00
|
|
|
tr_bool
|
|
|
|
tr_sessionAllowsDHT( const tr_session * session UNUSED )
|
|
|
|
{
|
|
|
|
return tr_sessionIsDHTEnabled( session );
|
|
|
|
}
|
|
|
|
|
2009-05-19 18:38:26 +00:00
|
|
|
tr_bool
|
|
|
|
tr_sessionIsDHTEnabled( const tr_session * session )
|
|
|
|
{
|
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
|
|
|
return session->isDHTEnabled;
|
|
|
|
}
|
|
|
|
|
2009-06-12 22:17:59 +00:00
|
|
|
static void
|
|
|
|
toggleDHTImpl( void * data )
|
2009-05-21 00:15:54 +00:00
|
|
|
{
|
2009-06-12 22:17:59 +00:00
|
|
|
tr_session * session = data;
|
2009-05-21 00:15:54 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2009-06-12 22:17:59 +00:00
|
|
|
if( session->isDHTEnabled )
|
|
|
|
tr_dhtUninit( session );
|
2009-05-21 00:15:54 +00:00
|
|
|
|
2009-06-12 22:17:59 +00:00
|
|
|
session->isDHTEnabled = !session->isDHTEnabled;
|
2009-05-21 00:15:54 +00:00
|
|
|
|
2009-06-12 22:17:59 +00:00
|
|
|
if( session->isDHTEnabled )
|
2009-08-07 14:47:46 +00:00
|
|
|
tr_dhtInit( session, &session->public_ipv4->addr );
|
2009-06-12 22:17:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_sessionSetDHTEnabled( tr_session * session, tr_bool enabled )
|
|
|
|
{
|
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
assert( tr_isBool( enabled ) );
|
|
|
|
|
|
|
|
if( ( enabled != 0 ) != ( session->isDHTEnabled != 0 ) )
|
|
|
|
tr_runInEventThread( session, toggleDHTImpl, session );
|
2009-05-21 00:15:54 +00:00
|
|
|
}
|
|
|
|
|
2008-04-01 19:20:21 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-08-16 21:06:57 +00:00
|
|
|
void
|
2008-12-03 07:10:09 +00:00
|
|
|
tr_sessionSetLazyBitfieldEnabled( tr_session * session,
|
|
|
|
tr_bool enabled )
|
2008-08-16 21:06:57 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-12-03 07:10:09 +00:00
|
|
|
session->useLazyBitfield = enabled != 0;
|
2008-08-16 21:06:57 +00:00
|
|
|
}
|
|
|
|
|
2008-12-03 07:10:09 +00:00
|
|
|
tr_bool
|
|
|
|
tr_sessionIsLazyBitfieldEnabled( const tr_session * session )
|
2008-08-16 21:06:57 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-12-03 07:10:09 +00:00
|
|
|
return session->useLazyBitfield;
|
2008-08-16 21:06:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2010-02-06 20:25:25 +00:00
|
|
|
struct port_forwarding_data
|
2008-05-12 16:33:17 +00:00
|
|
|
{
|
2010-02-06 20:25:25 +00:00
|
|
|
tr_bool enabled;
|
|
|
|
struct tr_shared * shared;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
setPortForwardingEnabled( void * vdata )
|
|
|
|
{
|
|
|
|
struct port_forwarding_data * data = vdata;
|
|
|
|
tr_sharedTraversalEnable( data->shared, data->enabled );
|
|
|
|
tr_free( data );
|
|
|
|
}
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2010-02-06 20:25:25 +00:00
|
|
|
void
|
|
|
|
tr_sessionSetPortForwardingEnabled( tr_session * session, tr_bool enabled )
|
|
|
|
{
|
|
|
|
struct port_forwarding_data * d;
|
|
|
|
d = tr_new0( struct port_forwarding_data, 1 );
|
|
|
|
d->shared = session->shared;
|
|
|
|
d->enabled = enabled;
|
|
|
|
tr_runInEventThread( session, setPortForwardingEnabled, d );
|
2008-05-12 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
2008-12-03 07:10:09 +00:00
|
|
|
tr_bool
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_sessionIsPortForwardingEnabled( const tr_session * session )
|
2008-05-12 16:33:17 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-12-14 11:21:11 +00:00
|
|
|
return tr_sharedTraversalIsEnabled( session->shared );
|
2008-05-12 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2009-12-01 00:07:18 +00:00
|
|
|
static int
|
|
|
|
tr_stringEndsWith( const char * str, const char * end )
|
|
|
|
{
|
|
|
|
const size_t slen = strlen( str );
|
|
|
|
const size_t elen = strlen( end );
|
|
|
|
|
|
|
|
return slen >= elen && !memcmp( &str[slen - elen], end, elen );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
loadBlocklists( tr_session * session )
|
|
|
|
{
|
|
|
|
int binCount = 0;
|
|
|
|
int newCount = 0;
|
|
|
|
struct stat sb;
|
|
|
|
char * dirname;
|
|
|
|
DIR * odir = NULL;
|
|
|
|
tr_list * list = NULL;
|
|
|
|
const tr_bool isEnabled = session->isBlocklistEnabled;
|
|
|
|
|
|
|
|
/* walk through the directory and find blocklists */
|
|
|
|
dirname = tr_buildPath( session->configDir, "blocklists", NULL );
|
|
|
|
if( !stat( dirname,
|
|
|
|
&sb ) && S_ISDIR( sb.st_mode )
|
|
|
|
&& ( ( odir = opendir( dirname ) ) ) )
|
|
|
|
{
|
|
|
|
struct dirent *d;
|
|
|
|
for( d = readdir( odir ); d; d = readdir( odir ) )
|
|
|
|
{
|
|
|
|
char * filename;
|
|
|
|
|
|
|
|
if( !d->d_name || d->d_name[0] == '.' ) /* skip dotfiles, ., and ..
|
|
|
|
*/
|
|
|
|
continue;
|
|
|
|
|
|
|
|
filename = tr_buildPath( dirname, d->d_name, NULL );
|
|
|
|
|
|
|
|
if( tr_stringEndsWith( filename, ".bin" ) )
|
|
|
|
{
|
|
|
|
/* if we don't already have this blocklist, add it */
|
|
|
|
if( !tr_list_find( list, filename,
|
|
|
|
(TrListCompareFunc)strcmp ) )
|
|
|
|
{
|
|
|
|
tr_list_append( &list,
|
|
|
|
_tr_blocklistNew( filename, isEnabled ) );
|
|
|
|
++binCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* strip out the file suffix, if there is one, and add ".bin"
|
|
|
|
instead */
|
|
|
|
tr_blocklist * b;
|
|
|
|
const char * dot = strrchr( d->d_name, '.' );
|
|
|
|
const int len = dot ? dot - d->d_name
|
|
|
|
: (int)strlen( d->d_name );
|
|
|
|
char * tmp = tr_strdup_printf(
|
|
|
|
"%s" TR_PATH_DELIMITER_STR "%*.*s.bin",
|
|
|
|
dirname, len, len, d->d_name );
|
|
|
|
b = _tr_blocklistNew( tmp, isEnabled );
|
|
|
|
_tr_blocklistSetContent( b, filename );
|
|
|
|
tr_list_append( &list, b );
|
|
|
|
++newCount;
|
|
|
|
tr_free( tmp );
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_free( filename );
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir( odir );
|
|
|
|
}
|
|
|
|
|
|
|
|
session->blocklists = list;
|
|
|
|
|
|
|
|
if( binCount )
|
|
|
|
tr_dbg( "Found %d blocklists in \"%s\"", binCount, dirname );
|
|
|
|
if( newCount )
|
|
|
|
tr_dbg( "Found %d new blocklists in \"%s\"", newCount, dirname );
|
|
|
|
|
|
|
|
tr_free( dirname );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
closeBlocklists( tr_session * session )
|
|
|
|
{
|
|
|
|
tr_list_free( &session->blocklists,
|
|
|
|
(TrListForeachFunc)_tr_blocklistFree );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_sessionReloadBlocklists( tr_session * session )
|
|
|
|
{
|
|
|
|
closeBlocklists( session );
|
|
|
|
loadBlocklists( session );
|
|
|
|
}
|
|
|
|
|
2008-04-01 19:20:21 +00:00
|
|
|
int
|
2008-06-11 16:15:45 +00:00
|
|
|
tr_blocklistGetRuleCount( const tr_session * session )
|
2008-04-01 19:20:21 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int n = 0;
|
2008-06-11 16:15:45 +00:00
|
|
|
tr_list * l;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( l = session->blocklists; l; l = l->next )
|
2008-06-11 16:15:45 +00:00
|
|
|
n += _tr_blocklistGetRuleCount( l->data );
|
|
|
|
return n;
|
2008-04-01 19:20:21 +00:00
|
|
|
}
|
|
|
|
|
2008-12-03 07:10:09 +00:00
|
|
|
tr_bool
|
2008-06-11 16:15:45 +00:00
|
|
|
tr_blocklistIsEnabled( const tr_session * session )
|
2008-04-01 19:20:21 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-06-11 16:15:45 +00:00
|
|
|
return session->isBlocklistEnabled;
|
2008-04-01 19:20:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_blocklistSetEnabled( tr_session * session,
|
2008-12-03 04:55:10 +00:00
|
|
|
tr_bool isEnabled )
|
2008-04-01 19:20:21 +00:00
|
|
|
{
|
2008-06-11 16:15:45 +00:00
|
|
|
tr_list * l;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-12-03 04:55:10 +00:00
|
|
|
session->isBlocklistEnabled = isEnabled != 0;
|
|
|
|
|
|
|
|
for( l=session->blocklists; l!=NULL; l=l->next )
|
2008-06-11 16:15:45 +00:00
|
|
|
_tr_blocklistSetEnabled( l->data, isEnabled );
|
2008-04-01 19:20:21 +00:00
|
|
|
}
|
|
|
|
|
2008-12-03 07:10:09 +00:00
|
|
|
tr_bool
|
2008-06-11 16:15:45 +00:00
|
|
|
tr_blocklistExists( const tr_session * session )
|
2008-04-01 19:20:21 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-06-11 16:15:45 +00:00
|
|
|
return session->blocklists != NULL;
|
2008-04-01 19:20:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_blocklistSetContent( tr_session * session,
|
|
|
|
const char * contentFilename )
|
2008-04-01 19:20:21 +00:00
|
|
|
{
|
2010-01-29 18:10:50 +00:00
|
|
|
tr_list * l;
|
|
|
|
int ruleCount;
|
2008-06-11 16:15:45 +00:00
|
|
|
tr_blocklist * b;
|
2010-01-29 18:10:50 +00:00
|
|
|
const char * defaultName = "level1.bin";
|
|
|
|
tr_sessionLock( session );
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( b = NULL, l = session->blocklists; !b && l; l = l->next )
|
|
|
|
if( tr_stringEndsWith( _tr_blocklistGetFilename( l->data ),
|
|
|
|
defaultName ) )
|
2008-06-11 16:15:45 +00:00
|
|
|
b = l->data;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !b )
|
|
|
|
{
|
2008-10-14 03:03:29 +00:00
|
|
|
char * path = tr_buildPath( session->configDir, "blocklists", defaultName, NULL );
|
|
|
|
b = _tr_blocklistNew( path, session->isBlocklistEnabled );
|
2008-06-11 16:15:45 +00:00
|
|
|
tr_list_append( &session->blocklists, b );
|
2008-10-14 03:03:29 +00:00
|
|
|
tr_free( path );
|
2008-06-11 16:15:45 +00:00
|
|
|
}
|
|
|
|
|
2010-01-29 18:10:50 +00:00
|
|
|
ruleCount = _tr_blocklistSetContent( b, contentFilename );
|
|
|
|
tr_sessionUnlock( session );
|
|
|
|
return ruleCount;
|
2008-04-01 19:20:21 +00:00
|
|
|
}
|
|
|
|
|
2008-12-03 07:10:09 +00:00
|
|
|
tr_bool
|
2008-12-02 03:41:58 +00:00
|
|
|
tr_sessionIsAddressBlocked( const tr_session * session,
|
|
|
|
const tr_address * addr )
|
2008-06-11 16:15:45 +00:00
|
|
|
{
|
|
|
|
tr_list * l;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( l = session->blocklists; l; l = l->next )
|
2008-06-11 16:15:45 +00:00
|
|
|
if( _tr_blocklistHasAddress( l->data, addr ) )
|
|
|
|
return TRUE;
|
|
|
|
return FALSE;
|
2008-04-01 19:20:21 +00:00
|
|
|
}
|
2008-04-14 14:39:13 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
static void
|
2009-06-09 21:57:11 +00:00
|
|
|
metainfoLookupInit( tr_session * session )
|
2008-04-14 14:39:13 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
struct stat sb;
|
2008-12-14 11:21:11 +00:00
|
|
|
const char * dirname = tr_getTorrentDir( session );
|
2008-09-23 19:11:04 +00:00
|
|
|
DIR * odir = NULL;
|
|
|
|
tr_ctor * ctor = NULL;
|
2009-06-09 21:57:11 +00:00
|
|
|
tr_benc * lookup;
|
|
|
|
int n = 0;
|
2008-04-14 14:39:13 +00:00
|
|
|
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-04-14 14:39:13 +00:00
|
|
|
/* walk through the directory and find the mappings */
|
2009-06-09 21:57:11 +00:00
|
|
|
lookup = tr_new0( tr_benc, 1 );
|
|
|
|
tr_bencInitDict( lookup, 0 );
|
2008-12-14 11:21:11 +00:00
|
|
|
ctor = tr_ctorNew( session );
|
2008-04-14 14:39:13 +00:00
|
|
|
tr_ctorSetSave( ctor, FALSE ); /* since we already have them */
|
2008-12-14 11:21:11 +00:00
|
|
|
if( !stat( dirname, &sb ) && S_ISDIR( sb.st_mode ) && ( ( odir = opendir( dirname ) ) ) )
|
2008-04-14 14:39:13 +00:00
|
|
|
{
|
|
|
|
struct dirent *d;
|
2009-06-09 21:57:11 +00:00
|
|
|
while(( d = readdir( odir )))
|
2008-04-14 14:39:13 +00:00
|
|
|
{
|
2009-06-09 21:57:11 +00:00
|
|
|
if( d->d_name && d->d_name[0] != '.' )
|
2008-04-14 14:39:13 +00:00
|
|
|
{
|
|
|
|
tr_info inf;
|
2008-10-14 03:03:29 +00:00
|
|
|
char * path = tr_buildPath( dirname, d->d_name, NULL );
|
|
|
|
tr_ctorSetMetainfoFromFile( ctor, path );
|
2009-04-02 17:30:29 +00:00
|
|
|
if( !tr_torrentParse( ctor, &inf ) )
|
2008-04-14 14:39:13 +00:00
|
|
|
{
|
2009-06-09 21:57:11 +00:00
|
|
|
++n;
|
|
|
|
tr_bencDictAddStr( lookup, inf.hashString, path );
|
2008-04-14 14:39:13 +00:00
|
|
|
}
|
2008-10-14 03:03:29 +00:00
|
|
|
tr_free( path );
|
2008-04-14 14:39:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir( odir );
|
|
|
|
}
|
|
|
|
tr_ctorFree( ctor );
|
|
|
|
|
2009-06-09 21:57:11 +00:00
|
|
|
session->metainfoLookup = lookup;
|
2008-04-14 14:39:13 +00:00
|
|
|
tr_dbg( "Found %d torrents in \"%s\"", n, dirname );
|
|
|
|
}
|
|
|
|
|
2009-05-21 19:54:59 +00:00
|
|
|
const char*
|
2009-06-09 21:57:11 +00:00
|
|
|
tr_sessionFindTorrentFile( const tr_session * session,
|
|
|
|
const char * hashString )
|
2009-05-21 19:54:59 +00:00
|
|
|
{
|
2009-06-09 21:57:11 +00:00
|
|
|
const char * filename = NULL;
|
|
|
|
if( !session->metainfoLookup )
|
|
|
|
metainfoLookupInit( (tr_session*)session );
|
|
|
|
tr_bencDictFindStr( session->metainfoLookup, hashString, &filename );
|
|
|
|
return filename;
|
2009-05-21 19:54:59 +00:00
|
|
|
}
|
|
|
|
|
2008-04-14 14:39:13 +00:00
|
|
|
void
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_sessionSetTorrentFile( tr_session * session,
|
2008-09-23 19:11:04 +00:00
|
|
|
const char * hashString,
|
|
|
|
const char * filename )
|
2008-04-14 14:39:13 +00:00
|
|
|
{
|
2009-05-21 19:54:59 +00:00
|
|
|
/* since we walk session->configDir/torrents/ to build the lookup table,
|
|
|
|
* and tr_sessionSetTorrentFile() is just to tell us there's a new file
|
|
|
|
* in that same directory, we don't need to do anything here if the
|
|
|
|
* lookup table hasn't been built yet */
|
2009-06-09 21:57:11 +00:00
|
|
|
if( session->metainfoLookup )
|
|
|
|
tr_bencDictAddStr( session->metainfoLookup, hashString, filename );
|
2008-04-14 14:39:13 +00:00
|
|
|
}
|
2008-05-10 00:19:00 +00:00
|
|
|
|
|
|
|
tr_torrent*
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_torrentNext( tr_session * session,
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_torrent * tor )
|
2008-05-10 00:19:00 +00:00
|
|
|
{
|
2009-09-25 21:05:59 +00:00
|
|
|
tr_torrent * ret;
|
|
|
|
|
|
|
|
assert( !session || tr_isSession( session ) );
|
|
|
|
|
|
|
|
if( !session )
|
|
|
|
ret = NULL;
|
|
|
|
else if( !tor )
|
|
|
|
ret = session->torrentList;
|
|
|
|
else
|
|
|
|
ret = tor->next;
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
return ret;
|
2008-05-10 00:19:00 +00:00
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_sessionSetRPCEnabled( tr_session * session,
|
2008-12-13 23:39:12 +00:00
|
|
|
tr_bool isEnabled )
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_rpcSetEnabled( session->rpcServer, isEnabled );
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-12-13 23:39:12 +00:00
|
|
|
tr_bool
|
2008-06-10 16:16:31 +00:00
|
|
|
tr_sessionIsRPCEnabled( const tr_session * session )
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
return tr_rpcIsEnabled( session->rpcServer );
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_sessionSetRPCPort( tr_session * session,
|
2008-12-01 20:51:01 +00:00
|
|
|
tr_port port )
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_rpcSetPort( session->rpcServer, port );
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-02-06 23:35:47 +00:00
|
|
|
tr_port
|
2008-06-10 16:16:31 +00:00
|
|
|
tr_sessionGetRPCPort( const tr_session * session )
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
return tr_rpcGetPort( session->rpcServer );
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_sessionSetRPCCallback( tr_session * session,
|
|
|
|
tr_rpc_func func,
|
|
|
|
void * user_data )
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
session->rpc_func = func;
|
|
|
|
session->rpc_func_user_data = user_data;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-09-25 18:48:09 +00:00
|
|
|
void
|
2008-10-01 20:23:57 +00:00
|
|
|
tr_sessionSetRPCWhitelist( tr_session * session,
|
|
|
|
const char * whitelist )
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-10-14 01:29:33 +00:00
|
|
|
tr_rpcSetWhitelist( session->rpcServer, whitelist );
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-04-16 19:14:37 +00:00
|
|
|
const char*
|
2008-10-01 20:23:57 +00:00
|
|
|
tr_sessionGetRPCWhitelist( const tr_session * session )
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-10-01 20:23:57 +00:00
|
|
|
return tr_rpcGetWhitelist( session->rpcServer );
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-10-01 22:59:29 +00:00
|
|
|
void
|
|
|
|
tr_sessionSetRPCWhitelistEnabled( tr_session * session,
|
2008-12-13 23:39:12 +00:00
|
|
|
tr_bool isEnabled )
|
2008-10-01 22:59:29 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-10-14 01:29:33 +00:00
|
|
|
tr_rpcSetWhitelistEnabled( session->rpcServer, isEnabled );
|
2008-10-01 22:59:29 +00:00
|
|
|
}
|
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
tr_bool
|
2008-10-01 22:59:29 +00:00
|
|
|
tr_sessionGetRPCWhitelistEnabled( const tr_session * session )
|
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-10-01 22:59:29 +00:00
|
|
|
return tr_rpcGetWhitelistEnabled( session->rpcServer );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-05 16:23:03 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_sessionSetRPCPassword( tr_session * session,
|
|
|
|
const char * password )
|
2008-06-05 16:23:03 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-06-05 16:23:03 +00:00
|
|
|
tr_rpcSetPassword( session->rpcServer, password );
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-04-16 19:14:37 +00:00
|
|
|
const char*
|
2009-03-25 00:36:11 +00:00
|
|
|
tr_sessionGetRPCPassword( const tr_session * session )
|
|
|
|
{
|
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
|
|
|
return tr_rpcGetPassword( session->rpcServer );
|
|
|
|
}
|
|
|
|
|
2008-06-05 16:23:03 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_sessionSetRPCUsername( tr_session * session,
|
|
|
|
const char * username )
|
2008-06-05 16:23:03 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-06-05 16:23:03 +00:00
|
|
|
tr_rpcSetUsername( session->rpcServer, username );
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-04-16 19:14:37 +00:00
|
|
|
const char*
|
2008-06-10 16:16:31 +00:00
|
|
|
tr_sessionGetRPCUsername( const tr_session * session )
|
2008-06-05 16:23:03 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-06-05 16:23:03 +00:00
|
|
|
return tr_rpcGetUsername( session->rpcServer );
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-06-05 16:23:03 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_sessionSetRPCPasswordEnabled( tr_session * session,
|
2008-12-13 23:39:12 +00:00
|
|
|
tr_bool isEnabled )
|
2008-06-05 16:23:03 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-06-05 16:23:03 +00:00
|
|
|
tr_rpcSetPasswordEnabled( session->rpcServer, isEnabled );
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
tr_bool
|
2008-06-10 16:16:31 +00:00
|
|
|
tr_sessionIsRPCPasswordEnabled( const tr_session * session )
|
2008-06-05 16:23:03 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-06-05 16:23:03 +00:00
|
|
|
return tr_rpcIsPasswordEnabled( session->rpcServer );
|
|
|
|
}
|
2008-06-10 16:16:31 +00:00
|
|
|
|
2009-04-15 21:05:58 +00:00
|
|
|
const char *
|
|
|
|
tr_sessionGetRPCBindAddress( const tr_session * session )
|
|
|
|
{
|
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
|
|
|
return tr_rpcGetBindAddress( session->rpcServer );
|
|
|
|
}
|
|
|
|
|
2008-06-10 16:16:31 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
tr_bool
|
2008-06-10 16:16:31 +00:00
|
|
|
tr_sessionIsProxyEnabled( const tr_session * session )
|
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-06-10 16:16:31 +00:00
|
|
|
return session->isProxyEnabled;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-06-10 16:16:31 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_sessionSetProxyEnabled( tr_session * session,
|
2008-12-03 04:55:10 +00:00
|
|
|
tr_bool isEnabled )
|
2008-06-10 16:16:31 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
2009-10-23 03:41:36 +00:00
|
|
|
assert( tr_isBool( isEnabled ) );
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2008-12-03 04:55:10 +00:00
|
|
|
session->isProxyEnabled = isEnabled != 0;
|
2008-06-10 16:16:31 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-06-11 20:45:53 +00:00
|
|
|
tr_proxy_type
|
|
|
|
tr_sessionGetProxyType( const tr_session * session )
|
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-06-11 20:45:53 +00:00
|
|
|
return session->proxyType;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-06-11 20:45:53 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_sessionSetProxyType( tr_session * session,
|
|
|
|
tr_proxy_type type )
|
2008-06-11 20:45:53 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-06-11 20:45:53 +00:00
|
|
|
session->proxyType = type;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-06-10 16:16:31 +00:00
|
|
|
const char*
|
|
|
|
tr_sessionGetProxy( const tr_session * session )
|
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-06-10 16:16:31 +00:00
|
|
|
return session->proxy;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
tr_port
|
2008-07-15 01:03:03 +00:00
|
|
|
tr_sessionGetProxyPort( const tr_session * session )
|
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-07-15 01:03:03 +00:00
|
|
|
return session->proxyPort;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-06-10 16:16:31 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_sessionSetProxy( tr_session * session,
|
|
|
|
const char * proxy )
|
2008-06-10 16:16:31 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-06-10 16:16:31 +00:00
|
|
|
if( proxy != session->proxy )
|
|
|
|
{
|
|
|
|
tr_free( session->proxy );
|
|
|
|
session->proxy = tr_strdup( proxy );
|
|
|
|
}
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-07-15 01:03:03 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_sessionSetProxyPort( tr_session * session,
|
2008-12-03 04:55:10 +00:00
|
|
|
tr_port port )
|
2008-07-15 01:03:03 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-07-15 01:03:03 +00:00
|
|
|
session->proxyPort = port;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
tr_bool
|
2008-06-10 16:16:31 +00:00
|
|
|
tr_sessionIsProxyAuthEnabled( const tr_session * session )
|
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-06-10 16:16:31 +00:00
|
|
|
return session->isProxyAuthEnabled;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-06-10 16:16:31 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_sessionSetProxyAuthEnabled( tr_session * session,
|
2008-12-03 04:55:10 +00:00
|
|
|
tr_bool isEnabled )
|
2008-06-10 16:16:31 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
2009-10-23 03:41:36 +00:00
|
|
|
assert( tr_isBool( isEnabled ) );
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2008-12-03 04:55:10 +00:00
|
|
|
session->isProxyAuthEnabled = isEnabled != 0;
|
2008-06-10 16:16:31 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-06-10 16:16:31 +00:00
|
|
|
const char*
|
|
|
|
tr_sessionGetProxyUsername( const tr_session * session )
|
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-06-10 16:16:31 +00:00
|
|
|
return session->proxyUsername;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-06-10 16:16:31 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_sessionSetProxyUsername( tr_session * session,
|
|
|
|
const char * username )
|
2008-06-10 16:16:31 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-06-10 16:16:31 +00:00
|
|
|
if( username != session->proxyUsername )
|
|
|
|
{
|
|
|
|
tr_free( session->proxyUsername );
|
|
|
|
session->proxyUsername = tr_strdup( username );
|
|
|
|
}
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-06-10 16:16:31 +00:00
|
|
|
const char*
|
|
|
|
tr_sessionGetProxyPassword( const tr_session * session )
|
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-06-10 16:16:31 +00:00
|
|
|
return session->proxyPassword;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-06-10 16:16:31 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_sessionSetProxyPassword( tr_session * session,
|
|
|
|
const char * password )
|
2008-06-10 16:16:31 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-06-10 16:16:31 +00:00
|
|
|
if( password != session->proxyPassword )
|
|
|
|
{
|
|
|
|
tr_free( session->proxyPassword );
|
|
|
|
session->proxyPassword = tr_strdup( password );
|
|
|
|
}
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-01-13 16:39:19 +00:00
|
|
|
int
|
|
|
|
tr_sessionGetActiveTorrentCount( tr_session * session )
|
|
|
|
{
|
2009-01-14 13:30:51 +00:00
|
|
|
int ret = 0;
|
2009-01-13 21:06:07 +00:00
|
|
|
tr_torrent * tor = NULL;
|
2009-01-13 16:39:19 +00:00
|
|
|
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2009-01-13 16:39:19 +00:00
|
|
|
while(( tor = tr_torrentNext( session, tor )))
|
|
|
|
if( tr_torrentGetActivity( tor ) != TR_STATUS_STOPPED )
|
|
|
|
++ret;
|
2009-02-06 23:35:47 +00:00
|
|
|
|
2009-01-13 16:39:19 +00:00
|
|
|
return ret;
|
|
|
|
}
|