2008-07-22 23:28:28 +00:00
|
|
|
/*
|
2009-01-10 23:09:07 +00:00
|
|
|
* This file Copyright (C) 2008-2009 Charles Kerr <charles@transmissionbt.com>
|
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>
|
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>
|
2007-07-21 18:46:54 +00:00
|
|
|
#include <sys/types.h> /* stat */
|
|
|
|
#include <sys/stat.h> /* stat */
|
|
|
|
#include <unistd.h> /* stat */
|
|
|
|
#include <dirent.h> /* opendir */
|
2007-07-29 18:11:21 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#include "transmission.h"
|
2008-12-23 17:27:15 +00:00
|
|
|
#include "session.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"
|
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"
|
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"
|
2007-11-21 20:03:53 +00:00
|
|
|
#include "stats.h"
|
2007-12-25 05:37:32 +00:00
|
|
|
#include "torrent.h"
|
2007-11-29 02:31:21 +00:00
|
|
|
#include "tracker.h"
|
2007-08-18 03:02:32 +00:00
|
|
|
#include "trevent.h"
|
2007-07-30 18:04:10 +00:00
|
|
|
#include "utils.h"
|
2008-04-24 01:42:53 +00:00
|
|
|
#include "web.h"
|
2008-08-14 11:11:25 +00:00
|
|
|
#include "crypto.h"
|
2006-07-16 19:39:23 +00:00
|
|
|
|
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
|
|
|
|
2008-08-21 16:12:17 +00:00
|
|
|
static int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_stringEndsWith( const char * str,
|
|
|
|
const char * end )
|
2008-08-21 16:12:17 +00:00
|
|
|
{
|
|
|
|
const size_t slen = strlen( str );
|
|
|
|
const size_t elen = strlen( end );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
return slen >= elen && !memcmp( &str[slen - elen], end, elen );
|
2008-08-21 16:12:17 +00:00
|
|
|
}
|
|
|
|
|
2008-06-11 16:15:45 +00:00
|
|
|
static void
|
|
|
|
loadBlocklists( tr_session * session )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int binCount = 0;
|
|
|
|
int newCount = 0;
|
2008-06-11 16:15:45 +00:00
|
|
|
struct stat sb;
|
2008-10-14 03:03:29 +00:00
|
|
|
char * dirname;
|
2008-09-23 19:11:04 +00:00
|
|
|
DIR * odir = NULL;
|
|
|
|
tr_list * list = NULL;
|
2008-12-03 04:55:10 +00:00
|
|
|
const tr_bool isEnabled = session->isBlocklistEnabled;
|
2008-06-11 16:15:45 +00:00
|
|
|
|
|
|
|
/* walk through the directory and find blocklists */
|
2008-10-14 03:03:29 +00:00
|
|
|
dirname = tr_buildPath( session->configDir, "blocklists", NULL );
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !stat( dirname,
|
|
|
|
&sb ) && S_ISDIR( sb.st_mode )
|
|
|
|
&& ( ( odir = opendir( dirname ) ) ) )
|
2008-06-11 16:15:45 +00:00
|
|
|
{
|
|
|
|
struct dirent *d;
|
2008-09-23 19:11:04 +00:00
|
|
|
for( d = readdir( odir ); d; d = readdir( odir ) )
|
2008-06-11 16:15:45 +00:00
|
|
|
{
|
2008-10-14 03:03:29 +00:00
|
|
|
char * filename;
|
2008-06-11 16:15:45 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !d->d_name || d->d_name[0] == '.' ) /* skip dotfiles, ., and ..
|
|
|
|
*/
|
2008-06-11 16:15:45 +00:00
|
|
|
continue;
|
|
|
|
|
2008-10-14 03:03:29 +00:00
|
|
|
filename = tr_buildPath( dirname, d->d_name, NULL );
|
2008-06-11 16:15:45 +00:00
|
|
|
|
|
|
|
if( tr_stringEndsWith( filename, ".bin" ) )
|
|
|
|
{
|
|
|
|
/* if we don't already have this blocklist, add it */
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !tr_list_find( list, filename,
|
|
|
|
(TrListCompareFunc)strcmp ) )
|
2008-06-11 16:15:45 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_list_append( &list,
|
|
|
|
_tr_blocklistNew( filename, isEnabled ) );
|
2008-06-11 16:15:45 +00:00
|
|
|
++binCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
/* strip out the file suffix, if there is one, and add ".bin"
|
|
|
|
instead */
|
2008-06-11 16:15:45 +00:00
|
|
|
tr_blocklist * b;
|
2008-09-23 19:11:04 +00:00
|
|
|
const char * dot = strrchr( d->d_name, '.' );
|
2008-10-14 03:54:57 +00:00
|
|
|
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 );
|
2008-06-11 16:15:45 +00:00
|
|
|
b = _tr_blocklistNew( tmp, isEnabled );
|
|
|
|
_tr_blocklistSetContent( b, filename );
|
|
|
|
tr_list_append( &list, b );
|
|
|
|
++newCount;
|
2008-10-14 03:54:57 +00:00
|
|
|
tr_free( tmp );
|
2008-06-11 16:15:45 +00:00
|
|
|
}
|
2008-10-14 03:03:29 +00:00
|
|
|
|
|
|
|
tr_free( filename );
|
2008-06-11 16:15:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 );
|
2008-10-14 03:03:29 +00:00
|
|
|
|
|
|
|
tr_free( dirname );
|
2008-06-11 16:15:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
#ifdef TR_EMBEDDED
|
|
|
|
#define TR_DEFAULT_ENCRYPTION TR_CLEAR_PREFERRED
|
|
|
|
#else
|
|
|
|
#define TR_DEFAULT_ENCRYPTION TR_ENCRYPTION_PREFERRED
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_sessionGetDefaultSettings( tr_benc * d )
|
|
|
|
{
|
|
|
|
assert( tr_bencIsDict( d ) );
|
|
|
|
|
|
|
|
tr_bencDictReserve( d, 30 );
|
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_BLOCKLIST_ENABLED, FALSE );
|
|
|
|
tr_bencDictAddStr( d, TR_PREFS_KEY_DOWNLOAD_DIR, tr_getDefaultDownloadDir( ) );
|
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_DSPEED, 100 );
|
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_DSPEED_ENABLED, 0 );
|
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_ENCRYPTION, TR_DEFAULT_ENCRYPTION );
|
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_LAZY_BITFIELD, TRUE );
|
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_MSGLEVEL, TR_MSG_INF );
|
2009-01-08 19:20:24 +00:00
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_OPEN_FILE_LIMIT, atoi( TR_DEFAULT_OPEN_FILE_LIMIT_STR ) );
|
2008-12-30 18:18:34 +00:00
|
|
|
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 ) );
|
2008-12-13 23:17:36 +00:00
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_PEER_PORT, atoi( TR_DEFAULT_PEER_PORT_STR ) );
|
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_PEER_PORT_RANDOM_ENABLED, FALSE );
|
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_PEER_PORT_RANDOM_LOW, 1024 );
|
|
|
|
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_bencDictAddInt( d, TR_PREFS_KEY_PEX_ENABLED, TRUE );
|
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_PORT_FORWARDING, TRUE );
|
2009-01-16 16:38:16 +00:00
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_PREALLOCATION, TR_PREALLOCATE_SPARSE );
|
2008-12-13 23:17:36 +00:00
|
|
|
tr_bencDictAddStr( d, TR_PREFS_KEY_PROXY, "" );
|
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_PROXY_AUTH_ENABLED, FALSE );
|
|
|
|
tr_bencDictAddInt( 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, "" );
|
2009-02-13 18:23:56 +00:00
|
|
|
tr_bencDictAddDouble( d, TR_PREFS_KEY_RATIO, 2.0 );
|
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_RATIO_ENABLED, FALSE );
|
2008-12-13 23:17:36 +00:00
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_RPC_AUTH_REQUIRED, FALSE );
|
|
|
|
tr_bencDictAddInt( 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_bencDictAddInt( d, TR_PREFS_KEY_RPC_WHITELIST_ENABLED, TRUE );
|
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_RPC_PORT, atoi( TR_DEFAULT_RPC_PORT_STR ) );
|
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_USPEED, 100 );
|
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_USPEED_ENABLED, 0 );
|
2009-01-09 15:45:44 +00:00
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_UPLOAD_SLOTS_PER_TORRENT, 14 );
|
2008-12-13 23:17:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_sessionGetSettings( tr_session * s, struct tr_benc * d )
|
|
|
|
{
|
|
|
|
int i, n=0;
|
|
|
|
char * freeme[16];
|
|
|
|
|
|
|
|
assert( tr_bencIsDict( d ) );
|
|
|
|
|
|
|
|
tr_bencDictReserve( d, 30 );
|
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_BLOCKLIST_ENABLED, tr_blocklistIsEnabled( s ) );
|
|
|
|
tr_bencDictAddStr( d, TR_PREFS_KEY_DOWNLOAD_DIR, s->downloadDir );
|
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_DSPEED, tr_sessionGetSpeedLimit( s, TR_DOWN ) );
|
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_DSPEED_ENABLED, tr_sessionIsSpeedLimitEnabled( s, TR_DOWN ) );
|
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_ENCRYPTION, s->encryptionMode );
|
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_LAZY_BITFIELD, s->useLazyBitfield );
|
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_MSGLEVEL, tr_getMessageLevel( ) );
|
2009-01-08 19:20:24 +00:00
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_OPEN_FILE_LIMIT, s->openFileLimit );
|
2008-12-13 23:17:36 +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 ) );
|
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_PEER_PORT_RANDOM_ENABLED, s->isPortRandom );
|
|
|
|
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_bencDictAddInt( d, TR_PREFS_KEY_PEX_ENABLED, s->isPexEnabled );
|
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_PORT_FORWARDING, tr_sessionIsPortForwardingEnabled( s ) );
|
2009-01-16 16:38:16 +00:00
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_PREALLOCATION, s->preallocationMode );
|
2008-12-13 23:17:36 +00:00
|
|
|
tr_bencDictAddStr( d, TR_PREFS_KEY_PROXY, s->proxy );
|
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_PROXY_AUTH_ENABLED, s->isProxyAuthEnabled );
|
|
|
|
tr_bencDictAddInt( 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 );
|
2009-02-13 18:23:56 +00:00
|
|
|
tr_bencDictAddDouble( d, TR_PREFS_KEY_RATIO, s->desiredRatio );
|
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_RATIO_ENABLED, s->isRatioLimited );
|
2008-12-13 23:17:36 +00:00
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_RPC_AUTH_REQUIRED, tr_sessionIsRPCPasswordEnabled( s ) );
|
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_RPC_ENABLED, tr_sessionIsRPCEnabled( s ) );
|
|
|
|
tr_bencDictAddStr( d, TR_PREFS_KEY_RPC_PASSWORD, freeme[n++] = tr_sessionGetRPCPassword( s ) );
|
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_RPC_PORT, tr_sessionGetRPCPort( s ) );
|
|
|
|
tr_bencDictAddStr( d, TR_PREFS_KEY_RPC_USERNAME, freeme[n++] = tr_sessionGetRPCUsername( s ) );
|
|
|
|
tr_bencDictAddStr( d, TR_PREFS_KEY_RPC_WHITELIST, freeme[n++] = tr_sessionGetRPCWhitelist( s ) );
|
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_RPC_WHITELIST_ENABLED, tr_sessionGetRPCWhitelistEnabled( s ) );
|
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_USPEED, tr_sessionGetSpeedLimit( s, TR_UP ) );
|
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_USPEED_ENABLED, tr_sessionIsSpeedLimitEnabled( s, TR_UP ) );
|
2009-01-09 15:45:44 +00:00
|
|
|
tr_bencDictAddInt( d, TR_PREFS_KEY_UPLOAD_SLOTS_PER_TORRENT, s->uploadSlotsPerTorrent );
|
2008-12-13 23:17:36 +00:00
|
|
|
|
|
|
|
for( i=0; i<n; ++i )
|
|
|
|
tr_free( freeme[i] );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_sessionLoadSettings( tr_benc * d, const char * configDir, const char * appName )
|
|
|
|
{
|
|
|
|
char * filename;
|
|
|
|
tr_benc fileSettings;
|
|
|
|
|
|
|
|
assert( tr_bencIsDict( d ) );
|
|
|
|
|
|
|
|
/* get the defaults */
|
|
|
|
tr_sessionGetDefaultSettings( d );
|
|
|
|
|
|
|
|
/* 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 );
|
|
|
|
if( !tr_bencLoadJSONFile( filename, &fileSettings ) ) {
|
|
|
|
tr_bencMergeDicts( d, &fileSettings );
|
|
|
|
tr_bencFree( &fileSettings );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
tr_free( filename );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_sessionSaveSettings( tr_session * session, const char * configDir, tr_benc * settings )
|
|
|
|
{
|
|
|
|
tr_benc fileSettings;
|
|
|
|
char * filename;
|
|
|
|
|
|
|
|
assert( tr_bencIsDict( settings ) );
|
2009-03-01 13:56:22 +00:00
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
filename = tr_buildPath( configDir, "settings.json", NULL );
|
|
|
|
|
|
|
|
tr_sessionGetSettings( session, settings );
|
|
|
|
|
|
|
|
if( tr_bencLoadJSONFile( filename, &fileSettings ) ) {
|
|
|
|
tr_bencSaveJSONFile( filename, settings );
|
|
|
|
} else {
|
|
|
|
tr_bencMergeDicts( &fileSettings, settings );
|
|
|
|
tr_bencSaveJSONFile( filename, &fileSettings );
|
|
|
|
tr_bencFree( &fileSettings );
|
|
|
|
}
|
|
|
|
|
2008-12-14 01:22:06 +00:00
|
|
|
tr_inf( "Saved \"%s\"", filename );
|
2008-12-13 23:17:36 +00:00
|
|
|
tr_free( filename );
|
|
|
|
}
|
|
|
|
|
2008-12-14 11:21:11 +00:00
|
|
|
static void metainfoLookupRescan( tr_session * );
|
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;
|
|
|
|
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 )
|
|
|
|
{
|
|
|
|
tr_session * session;
|
2009-02-04 16:58:52 +00:00
|
|
|
struct init_data data;
|
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;
|
|
|
|
|
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 */
|
|
|
|
session->isWaiting = TRUE;
|
|
|
|
data.session = session;
|
|
|
|
data.configDir = configDir;
|
|
|
|
data.messageQueuingEnabled = messageQueuingEnabled;
|
|
|
|
data.clientSettings = clientSettings;
|
|
|
|
tr_runInEventThread( session, tr_sessionInitImpl, &data );
|
|
|
|
while( session->isWaiting )
|
|
|
|
tr_wait( 100 );
|
|
|
|
|
|
|
|
return session;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tr_sessionInitImpl( void * vdata )
|
|
|
|
{
|
|
|
|
int64_t i;
|
|
|
|
int64_t j;
|
2009-02-13 18:23:56 +00:00
|
|
|
double d;
|
2009-02-04 16:58:52 +00:00
|
|
|
tr_bool found;
|
|
|
|
const char * str;
|
|
|
|
tr_benc settings;
|
|
|
|
char * filename;
|
|
|
|
struct init_data * data = vdata;
|
|
|
|
tr_benc * clientSettings = data->clientSettings;
|
|
|
|
tr_session * session = data->session;
|
|
|
|
|
|
|
|
assert( tr_amInEventThread( session ) );
|
|
|
|
assert( tr_bencIsDict( clientSettings ) );
|
|
|
|
|
2009-01-02 19:56:06 +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 );
|
|
|
|
tr_sessionGetDefaultSettings( &settings );
|
|
|
|
tr_bencMergeDicts( &settings, clientSettings );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
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
|
|
|
found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_PEER_LIMIT_TORRENT, &i );
|
|
|
|
assert( found );
|
|
|
|
session->peerLimitPerTorrent = i;
|
|
|
|
|
|
|
|
found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_MSGLEVEL, &i );
|
|
|
|
assert( found );
|
|
|
|
tr_setMessageLevel( i );
|
|
|
|
tr_setMessageQueuing( data->messageQueuingEnabled );
|
|
|
|
|
|
|
|
|
|
|
|
found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_PEX_ENABLED, &i );
|
|
|
|
assert( found );
|
|
|
|
session->isPexEnabled = i != 0;
|
|
|
|
|
|
|
|
found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_ENCRYPTION, &i );
|
|
|
|
assert( found );
|
2009-01-16 16:38:16 +00:00
|
|
|
assert( tr_isEncryptionMode( i ) );
|
2009-02-06 23:35:47 +00:00
|
|
|
session->encryptionMode = i;
|
2009-01-16 16:38:16 +00:00
|
|
|
|
|
|
|
found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_PREALLOCATION, &i );
|
|
|
|
assert( found );
|
|
|
|
assert( tr_isPreallocationMode( i ) );
|
|
|
|
session->preallocationMode = i;
|
2009-02-06 23:35:47 +00:00
|
|
|
|
|
|
|
found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_PEER_SOCKET_TOS, &i );
|
|
|
|
assert( found );
|
|
|
|
session->peerSocketTOS = i;
|
|
|
|
|
|
|
|
found = tr_bencDictFindStr( &settings, TR_PREFS_KEY_DOWNLOAD_DIR, &str );
|
|
|
|
assert( found );
|
|
|
|
session->downloadDir = tr_strdup( str );
|
|
|
|
|
|
|
|
found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_PROXY_ENABLED, &i );
|
|
|
|
assert( found );
|
|
|
|
session->isProxyEnabled = i != 0;
|
|
|
|
|
|
|
|
found = tr_bencDictFindStr( &settings, TR_PREFS_KEY_PROXY, &str );
|
|
|
|
assert( found );
|
|
|
|
session->proxy = tr_strdup( str );
|
|
|
|
|
|
|
|
found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_PROXY_PORT, &i );
|
|
|
|
assert( found );
|
|
|
|
session->proxyPort = i;
|
|
|
|
|
|
|
|
found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_PROXY_TYPE, &i );
|
|
|
|
assert( found );
|
|
|
|
session->proxyType = i;
|
|
|
|
|
|
|
|
found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_PROXY_AUTH_ENABLED, &i );
|
|
|
|
assert( found );
|
|
|
|
session->isProxyAuthEnabled = i != 0;
|
|
|
|
|
|
|
|
found = tr_bencDictFindStr( &settings, TR_PREFS_KEY_PROXY_USERNAME, &str );
|
|
|
|
assert( found );
|
|
|
|
session->proxyUsername = tr_strdup( str );
|
|
|
|
|
|
|
|
found = tr_bencDictFindStr( &settings, TR_PREFS_KEY_PROXY_PASSWORD, &str );
|
|
|
|
assert( found );
|
|
|
|
session->proxyPassword = tr_strdup( str );
|
|
|
|
|
|
|
|
session->so_sndbuf = 1500 * 3; /* 3x MTU for most ethernet/wireless */
|
|
|
|
session->so_rcvbuf = 8192;
|
|
|
|
|
|
|
|
tr_setConfigDir( session, data->configDir );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2009-02-04 16:58:52 +00:00
|
|
|
tr_trackerSessionInit( session );
|
2009-02-04 18:19:37 +00:00
|
|
|
assert( session->tracker != NULL );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
session->peerMgr = tr_peerMgrNew( session );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2009-02-06 23:35:47 +00:00
|
|
|
found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_LAZY_BITFIELD, &i );
|
|
|
|
assert( found );
|
|
|
|
session->useLazyBitfield = i != 0;
|
2008-08-16 21:06:57 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
/* Initialize rate and file descripts controls */
|
2007-12-24 07:02:40 +00:00
|
|
|
|
2009-02-06 23:35:47 +00:00
|
|
|
found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_OPEN_FILE_LIMIT, &i );
|
|
|
|
assert( found );
|
2009-01-08 19:20:24 +00:00
|
|
|
session->openFileLimit = i;
|
2009-02-06 23:35:47 +00:00
|
|
|
found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_PEER_LIMIT_GLOBAL, &j );
|
|
|
|
assert( found );
|
2009-01-08 19:20:24 +00:00
|
|
|
tr_fdInit( session->openFileLimit, j );
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2009-02-06 23:35:47 +00:00
|
|
|
/**
|
|
|
|
*** random port
|
|
|
|
**/
|
|
|
|
|
|
|
|
found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_PEER_PORT_RANDOM_ENABLED, &i );
|
|
|
|
assert( found );
|
|
|
|
session->isPortRandom = i != 0;
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2009-02-06 23:35:47 +00:00
|
|
|
found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_PEER_PORT_RANDOM_LOW, &i );
|
|
|
|
assert( found );
|
|
|
|
session->randomPortLow = i;
|
|
|
|
|
|
|
|
found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_PEER_PORT_RANDOM_HIGH, &i );
|
|
|
|
assert( found );
|
|
|
|
session->randomPortHigh = i;
|
|
|
|
|
|
|
|
found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_PORT_FORWARDING, &i )
|
|
|
|
&& tr_bencDictFindInt( &settings, TR_PREFS_KEY_PEER_PORT, &j );
|
|
|
|
assert( found );
|
|
|
|
session->peerPort = session->isPortRandom ? getRandomPort( session ) : j;
|
|
|
|
session->shared = tr_sharedInit( session, i, session->peerPort );
|
|
|
|
session->isPortSet = session->isPortRandom || j>0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
**/
|
2009-01-09 15:45:44 +00:00
|
|
|
|
|
|
|
found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_UPLOAD_SLOTS_PER_TORRENT, &i );
|
|
|
|
assert( found );
|
|
|
|
session->uploadSlotsPerTorrent = i;
|
2009-02-06 23:35:47 +00:00
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_USPEED, &i )
|
|
|
|
&& tr_bencDictFindInt( &settings, TR_PREFS_KEY_USPEED_ENABLED, &j );
|
2009-02-06 23:35:47 +00:00
|
|
|
assert( found );
|
2008-12-13 23:17:36 +00:00
|
|
|
tr_sessionSetSpeedLimit( session, TR_UP, i );
|
|
|
|
tr_sessionSetSpeedLimitEnabled( session, TR_UP, j );
|
2009-02-06 23:35:47 +00:00
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_DSPEED, &i )
|
|
|
|
&& tr_bencDictFindInt( &settings, TR_PREFS_KEY_DSPEED_ENABLED, &j );
|
2009-02-06 23:35:47 +00:00
|
|
|
assert( found );
|
2008-12-13 23:17:36 +00:00
|
|
|
tr_sessionSetSpeedLimit( session, TR_DOWN, i );
|
|
|
|
tr_sessionSetSpeedLimitEnabled( session, TR_DOWN, j );
|
2007-06-22 20:59:23 +00:00
|
|
|
|
2009-02-13 18:23:56 +00:00
|
|
|
found = tr_bencDictFindDouble( &settings, TR_PREFS_KEY_RATIO, &d )
|
|
|
|
&& tr_bencDictFindInt( &settings, TR_PREFS_KEY_RATIO_ENABLED, &j );
|
|
|
|
assert( found );
|
|
|
|
tr_sessionSetRatioLimit( session, d );
|
|
|
|
tr_sessionSetRatioLimited( session, j );
|
|
|
|
|
2008-04-01 19:20:21 +00:00
|
|
|
/* initialize the blocklist */
|
2008-12-13 23:17:36 +00:00
|
|
|
filename = tr_buildPath( session->configDir, "blocklists", NULL );
|
2008-04-01 19:20:21 +00:00
|
|
|
tr_mkdirp( filename, 0777 );
|
2008-10-14 03:03:29 +00:00
|
|
|
tr_free( filename );
|
2009-02-06 23:35:47 +00:00
|
|
|
found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_BLOCKLIST_ENABLED, &i );
|
|
|
|
assert( found );
|
|
|
|
session->isBlocklistEnabled = i;
|
|
|
|
loadBlocklists( session );
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2009-02-06 23:35:47 +00:00
|
|
|
session->rpcServer = tr_rpcInit( session, &settings );
|
2009-01-26 08:23:11 +00:00
|
|
|
|
|
|
|
tr_bencFree( &settings );
|
|
|
|
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
2009-02-06 23:35:47 +00:00
|
|
|
|
2009-01-26 08:23:11 +00:00
|
|
|
/* first %s is the application name
|
|
|
|
second %s is the version number */
|
|
|
|
tr_inf( _( "%s %s started" ), TR_NAME, LONG_VERSION_STRING );
|
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
tr_statsInit( session );
|
2009-02-06 23:35:47 +00:00
|
|
|
session->web = tr_webInit( session );
|
2008-12-13 23:17:36 +00:00
|
|
|
metainfoLookupRescan( session );
|
2009-01-27 02:01:04 +00:00
|
|
|
session->isWaiting = FALSE;
|
2009-02-04 18:19:37 +00:00
|
|
|
dbgmsg( "returning session %p; session->tracker is %p", session, session->tracker );
|
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
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
void
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_globalLock( 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
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_globalUnlock( 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
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_globalIsLocked( 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
|
|
|
|
|
|
|
struct bind_port_data
|
|
|
|
{
|
2008-12-03 07:10:09 +00:00
|
|
|
tr_session * session;
|
|
|
|
tr_port port;
|
2007-11-22 06:13:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
tr_setBindPortImpl( void * vdata )
|
|
|
|
{
|
|
|
|
struct bind_port_data * data = vdata;
|
2008-12-03 07:10:09 +00:00
|
|
|
tr_session * session = data->session;
|
|
|
|
const tr_port port = data->port;
|
2007-11-22 06:13:57 +00:00
|
|
|
|
2008-12-03 07:10:09 +00:00
|
|
|
session->isPortSet = 1;
|
|
|
|
tr_sharedSetPort( session->shared, port );
|
2007-11-22 06:13:57 +00:00
|
|
|
|
|
|
|
tr_free( data );
|
|
|
|
}
|
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
static void
|
|
|
|
setPortImpl( tr_session * session, tr_port port )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
struct bind_port_data * data;
|
|
|
|
|
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
|
|
|
data = tr_new( struct bind_port_data, 1 );
|
2008-12-03 07:10:09 +00:00
|
|
|
data->session = session;
|
2007-11-22 06:13:57 +00:00
|
|
|
data->port = port;
|
2008-12-03 07:10:09 +00:00
|
|
|
tr_runInEventThread( session, tr_setBindPortImpl, data );
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
void
|
|
|
|
tr_sessionSetPeerPort( tr_session * session,
|
|
|
|
tr_port port )
|
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
session->isPortRandom = FALSE;
|
|
|
|
session->peerPort = port;
|
|
|
|
setPortImpl( session, 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 ) );
|
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
session->isPortRandom = TRUE;
|
|
|
|
session->peerPort = getRandomPort( session );
|
|
|
|
setPortImpl( session, session->peerPort );
|
|
|
|
return session->peerPort;
|
2008-12-11 04:41:23 +00:00
|
|
|
}
|
|
|
|
|
2008-12-03 07:10:09 +00:00
|
|
|
tr_port
|
2008-12-13 23:17:36 +00:00
|
|
|
tr_sessionGetPeerPort( const tr_session * session )
|
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
|
|
|
|
|
|
|
return session->peerPort;
|
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 )))
|
|
|
|
tr_torrentCheckSeedRatio( tor );
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-12-13 22:52:15 +00:00
|
|
|
static void
|
|
|
|
updateBandwidth( tr_session * session, tr_direction dir )
|
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
tr_bool zeroCase;
|
|
|
|
|
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
|
|
|
zeroCase = session->speedLimit[dir] < 1 && session->isSpeedLimited[dir];
|
2008-12-13 22:52:15 +00:00
|
|
|
|
|
|
|
tr_bandwidthSetLimited( session->bandwidth, dir, session->isSpeedLimited[dir] && !zeroCase );
|
|
|
|
|
|
|
|
tr_bandwidthSetDesiredSpeed( session->bandwidth, dir, session->speedLimit[dir] );
|
|
|
|
}
|
|
|
|
|
2007-07-20 08:33:59 +00:00
|
|
|
void
|
2008-11-25 21:35:17 +00:00
|
|
|
tr_sessionSetSpeedLimitEnabled( tr_session * session,
|
|
|
|
tr_direction dir,
|
2008-12-13 22:52:15 +00:00
|
|
|
tr_bool isLimited )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isDirection( dir ) );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-12-13 22:52:15 +00:00
|
|
|
session->isSpeedLimited[dir] = isLimited;
|
|
|
|
updateBandwidth( session, dir );
|
2008-05-12 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
2007-07-20 08:33:59 +00:00
|
|
|
void
|
2008-11-25 21:35:17 +00:00
|
|
|
tr_sessionSetSpeedLimit( tr_session * session,
|
|
|
|
tr_direction dir,
|
|
|
|
int desiredSpeed )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isDirection( dir ) );
|
2008-12-13 22:52:15 +00:00
|
|
|
|
|
|
|
session->speedLimit[dir] = desiredSpeed;
|
|
|
|
updateBandwidth( session, dir );
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_bool
|
2009-03-01 13:56:22 +00:00
|
|
|
tr_sessionIsSpeedLimitEnabled( const tr_session * session, tr_direction dir )
|
2008-12-13 22:52:15 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isDirection( dir ) );
|
2008-12-13 22:52:15 +00:00
|
|
|
|
|
|
|
return session->isSpeedLimited[dir];
|
2007-01-08 21:53:55 +00:00
|
|
|
}
|
|
|
|
|
2008-05-12 16:33:17 +00:00
|
|
|
int
|
2009-03-01 13:56:22 +00:00
|
|
|
tr_sessionGetSpeedLimit( const tr_session * session, tr_direction dir )
|
2007-07-20 08:33:59 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isDirection( dir ) );
|
2008-12-13 22:52:15 +00:00
|
|
|
|
|
|
|
return session->speedLimit[dir];
|
2007-07-20 08:33:59 +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 ) );
|
|
|
|
|
2007-12-20 21:44:16 +00:00
|
|
|
tr_fdSetPeerLimit( maxGlobalPeers );
|
|
|
|
}
|
|
|
|
|
|
|
|
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 ) );
|
|
|
|
|
2007-12-20 21:44:16 +00:00
|
|
|
return tr_fdGetPeerLimit( );
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
return tr_isSession( session ) ? tr_bandwidthGetPieceSpeed( 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
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
static void
|
2008-08-01 13:46:03 +00:00
|
|
|
tr_closeAllConnections( 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 ) );
|
|
|
|
|
2008-08-01 13:46:03 +00:00
|
|
|
tr_statsClose( session );
|
|
|
|
tr_sharedShuttingDown( session->shared );
|
|
|
|
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
|
|
|
|
2008-08-01 13:46:03 +00:00
|
|
|
tr_peerMgrFree( session->peerMgr );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-08-01 13:46:03 +00:00
|
|
|
tr_trackerSessionClose( session );
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_list_free( &session->blocklists,
|
|
|
|
(TrListForeachFunc)_tr_blocklistFree );
|
2008-08-01 13:46:03 +00:00
|
|
|
tr_webClose( &session->web );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
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
|
|
|
|
deadlineReached( const uint64_t deadline )
|
|
|
|
{
|
|
|
|
return tr_date( ) >= deadline;
|
|
|
|
}
|
|
|
|
|
2007-11-29 00:43:58 +00:00
|
|
|
#define SHUTDOWN_MAX_SECONDS 30
|
|
|
|
|
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
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int i;
|
|
|
|
const int maxwait_msec = SHUTDOWN_MAX_SECONDS * 1000;
|
2007-11-05 05:42:25 +00:00
|
|
|
const uint64_t deadline = tr_date( ) + maxwait_msec;
|
|
|
|
|
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 */
|
|
|
|
tr_runInEventThread( session, tr_closeAllConnections, session );
|
2008-09-23 19:11:04 +00:00
|
|
|
while( !session->isClosed && !deadlineReached( deadline ) )
|
|
|
|
{
|
|
|
|
dbgmsg(
|
|
|
|
"waiting for the shutdown commands to run in the main thread" );
|
2007-10-06 18:20:52 +00:00
|
|
|
tr_wait( 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 */
|
2008-09-23 19:11:04 +00:00
|
|
|
while( ( session->shared
|
|
|
|
|| session->tracker ) && !deadlineReached( deadline ) )
|
|
|
|
{
|
2008-08-01 13:46:03 +00:00
|
|
|
dbgmsg( "waiting on port unmap (%p) or tracker (%p)",
|
|
|
|
session->shared, session->tracker );
|
|
|
|
tr_wait( 100 );
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-08-01 13:46:03 +00:00
|
|
|
tr_fdClose( );
|
2008-04-28 13:29:38 +00:00
|
|
|
|
2008-08-01 13:46:03 +00:00
|
|
|
/* close the libtransmission thread */
|
|
|
|
tr_eventClose( session );
|
2008-09-23 19:11:04 +00:00
|
|
|
while( session->events && !deadlineReached( deadline ) )
|
|
|
|
{
|
2008-08-01 13:46:03 +00:00
|
|
|
dbgmsg( "waiting for the libevent thread to shutdown cleanly" );
|
2007-10-06 18:20:52 +00:00
|
|
|
tr_wait( 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
|
|
|
/* free the session memory */
|
2008-11-25 21:35:17 +00:00
|
|
|
tr_bandwidthFree( session->bandwidth );
|
2008-08-01 13:46:03 +00:00
|
|
|
tr_lockFree( session->lock );
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < session->metainfoLookupCount; ++i )
|
2008-08-01 13:46:03 +00:00
|
|
|
tr_free( session->metainfoLookup[i].filename );
|
|
|
|
tr_free( session->metainfoLookup );
|
|
|
|
tr_free( session->tag );
|
|
|
|
tr_free( session->configDir );
|
|
|
|
tr_free( session->resumeDir );
|
|
|
|
tr_free( session->torrentDir );
|
|
|
|
tr_free( session->downloadDir );
|
|
|
|
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 );
|
2008-12-14 11:21:11 +00:00
|
|
|
if(( tor = tr_torrentNew( session, 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
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-05-12 16:33:17 +00:00
|
|
|
void
|
2008-12-03 07:10:09 +00:00
|
|
|
tr_sessionSetPortForwardingEnabled( tr_session * session,
|
|
|
|
tr_bool enabled )
|
2008-05-12 16:33:17 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-12-03 07:10:09 +00:00
|
|
|
tr_globalLock( session );
|
|
|
|
tr_sharedTraversalEnable( session->shared, enabled );
|
|
|
|
tr_globalUnlock( session );
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
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
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_list * l;
|
2008-06-11 16:15:45 +00:00
|
|
|
tr_blocklist * b;
|
2008-09-23 19:11:04 +00:00
|
|
|
const char * defaultName = "level1.bin";
|
2008-06-11 16:15:45 +00:00
|
|
|
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
return _tr_blocklistSetContent( b, contentFilename );
|
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 int
|
2009-01-29 16:56:43 +00:00
|
|
|
compareLookupEntries( const void * va, const void * vb )
|
2008-04-14 14:39:13 +00:00
|
|
|
{
|
|
|
|
const struct tr_metainfo_lookup * a = va;
|
|
|
|
const struct tr_metainfo_lookup * b = vb;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-04-14 14:39:13 +00:00
|
|
|
return strcmp( a->hashString, b->hashString );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-12-14 11:21:11 +00:00
|
|
|
metainfoLookupResort( tr_session * session )
|
2008-04-14 14:39:13 +00:00
|
|
|
{
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-12-14 11:21:11 +00:00
|
|
|
qsort( session->metainfoLookup,
|
|
|
|
session->metainfoLookupCount,
|
2008-04-14 14:39:13 +00:00
|
|
|
sizeof( struct tr_metainfo_lookup ),
|
|
|
|
compareLookupEntries );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-01-29 16:56:43 +00:00
|
|
|
compareHashStringToLookupEntry( const void * va, const void * vb )
|
2008-04-14 14:39:13 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
const char * a = va;
|
2008-04-14 14:39:13 +00:00
|
|
|
const struct tr_metainfo_lookup * b = vb;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-04-14 14:39:13 +00:00
|
|
|
return strcmp( a, b->hashString );
|
|
|
|
}
|
|
|
|
|
|
|
|
const char*
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_sessionFindTorrentFile( const tr_session * session,
|
|
|
|
const char * hashStr )
|
2008-04-14 14:39:13 +00:00
|
|
|
{
|
|
|
|
struct tr_metainfo_lookup * l = bsearch( hashStr,
|
2008-12-14 11:21:11 +00:00
|
|
|
session->metainfoLookup,
|
|
|
|
session->metainfoLookupCount,
|
|
|
|
sizeof( struct tr_metainfo_lookup ),
|
2008-04-14 14:39:13 +00:00
|
|
|
compareHashStringToLookupEntry );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-04-14 14:39:13 +00:00
|
|
|
return l ? l->filename : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-12-14 11:21:11 +00:00
|
|
|
metainfoLookupRescan( tr_session * session )
|
2008-04-14 14:39:13 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int i;
|
|
|
|
int n;
|
|
|
|
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;
|
|
|
|
tr_list * list = NULL;
|
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 */
|
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;
|
2008-09-23 19:11:04 +00:00
|
|
|
for( d = readdir( odir ); d != NULL; d = readdir( odir ) )
|
2008-04-14 14:39:13 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
if( d->d_name && d->d_name[0] != '.' ) /* skip dotfiles, ., and ..
|
|
|
|
*/
|
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 );
|
2008-12-14 11:21:11 +00:00
|
|
|
if( !tr_torrentParse( session, ctor, &inf ) )
|
2008-04-14 14:39:13 +00:00
|
|
|
{
|
|
|
|
tr_list_append( &list, tr_strdup( inf.hashString ) );
|
2008-10-14 03:03:29 +00:00
|
|
|
tr_list_append( &list, tr_strdup( path ) );
|
2008-04-14 14:39:13 +00:00
|
|
|
tr_metainfoFree( &inf );
|
|
|
|
}
|
2008-10-14 03:03:29 +00:00
|
|
|
tr_free( path );
|
2008-04-14 14:39:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir( odir );
|
|
|
|
}
|
|
|
|
tr_ctorFree( ctor );
|
|
|
|
|
|
|
|
n = tr_list_size( list ) / 2;
|
2008-12-14 11:21:11 +00:00
|
|
|
session->metainfoLookup = tr_new0( struct tr_metainfo_lookup, n );
|
|
|
|
session->metainfoLookupCount = n;
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < n; ++i )
|
2008-04-14 14:39:13 +00:00
|
|
|
{
|
|
|
|
char * hashString = tr_list_pop_front( &list );
|
|
|
|
char * filename = tr_list_pop_front( &list );
|
|
|
|
|
2008-12-14 11:21:11 +00:00
|
|
|
memcpy( session->metainfoLookup[i].hashString, hashString,
|
2008-09-23 19:11:04 +00:00
|
|
|
2 * SHA_DIGEST_LENGTH + 1 );
|
2008-04-14 14:39:13 +00:00
|
|
|
tr_free( hashString );
|
2008-12-14 11:21:11 +00:00
|
|
|
session->metainfoLookup[i].filename = filename;
|
2008-04-14 14:39:13 +00:00
|
|
|
}
|
|
|
|
|
2008-12-14 11:21:11 +00:00
|
|
|
metainfoLookupResort( session );
|
2008-04-14 14:39:13 +00:00
|
|
|
tr_dbg( "Found %d torrents in \"%s\"", n, dirname );
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
struct tr_metainfo_lookup * l = bsearch( hashString,
|
2008-12-14 11:21:11 +00:00
|
|
|
session->metainfoLookup,
|
|
|
|
session->metainfoLookupCount,
|
|
|
|
sizeof( struct tr_metainfo_lookup ),
|
2008-04-14 14:39:13 +00:00
|
|
|
compareHashStringToLookupEntry );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-08-01 16:43:22 +00:00
|
|
|
if( l )
|
2008-04-14 14:39:13 +00:00
|
|
|
{
|
|
|
|
if( l->filename != filename )
|
|
|
|
{
|
|
|
|
tr_free( l->filename );
|
|
|
|
l->filename = tr_strdup( filename );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-12-14 11:21:11 +00:00
|
|
|
const int n = session->metainfoLookupCount++;
|
2008-04-14 14:39:13 +00:00
|
|
|
struct tr_metainfo_lookup * node;
|
2008-12-14 11:21:11 +00:00
|
|
|
session->metainfoLookup = tr_renew( struct tr_metainfo_lookup,
|
|
|
|
session->metainfoLookup,
|
|
|
|
session->metainfoLookupCount );
|
|
|
|
node = session->metainfoLookup + n;
|
2008-09-23 19:11:04 +00:00
|
|
|
memcpy( node->hashString, hashString, 2 * SHA_DIGEST_LENGTH + 1 );
|
2008-04-14 14:39:13 +00:00
|
|
|
node->filename = tr_strdup( filename );
|
2008-12-14 11:21:11 +00:00
|
|
|
metainfoLookupResort( session );
|
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-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2008-05-10 00:19:00 +00:00
|
|
|
return tor ? tor->next : session->torrentList;
|
|
|
|
}
|
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
|
|
|
|
2008-06-05 16:23:03 +00:00
|
|
|
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
|
|
|
|
2008-06-05 16:23:03 +00:00
|
|
|
char*
|
2008-06-10 16:16:31 +00:00
|
|
|
tr_sessionGetRPCPassword( 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_rpcGetPassword( 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_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
|
|
|
|
2008-06-05 16:23:03 +00:00
|
|
|
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
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
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 ) );
|
|
|
|
|
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 ) );
|
|
|
|
|
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;
|
|
|
|
}
|