source: trunk/libtransmission/session.c @ 8242

Last change on this file since 8242 was 8242, checked in by jhujhiti, 13 years ago

(trunk) #1497 Options to listen on specific network sockets

  • Property svn:keywords set to Date Rev Author Id
File size: 58.2 KB
Line 
1/*
2 * This file Copyright (C) 2008-2009 Charles Kerr <charles@transmissionbt.com>
3 *
4 * This file is licensed by the GPL version 2.  Works owned by the
5 * Transmission project are granted a special exemption to clause 2(b)
6 * so that the bulk of its code can remain under the MIT license.
7 * This exemption does not extend to derived works not owned by
8 * the Transmission project.
9 *
10 * $Id: session.c 8242 2009-04-15 21:05:58Z jhujhiti $
11 */
12
13#include <assert.h>
14#include <stdlib.h>
15#include <string.h> /* memcpy */
16
17#include <signal.h>
18#include <sys/types.h> /* stat */
19#include <sys/stat.h> /* stat */
20#include <unistd.h> /* stat */
21#include <dirent.h> /* opendir */
22
23#include <event.h>
24
25#include "transmission.h"
26#include "session.h"
27#include "bandwidth.h"
28#include "bencode.h"
29#include "blocklist.h"
30#include "crypto.h"
31#include "fdlimit.h"
32#include "list.h"
33#include "metainfo.h" /* tr_metainfoFree */
34#include "net.h"
35#include "peer-mgr.h"
36#include "platform.h" /* tr_lock */
37#include "port-forwarding.h"
38#include "rpc-server.h"
39#include "stats.h"
40#include "torrent.h"
41#include "tracker.h"
42#include "trevent.h"
43#include "utils.h"
44#include "version.h"
45#include "web.h"
46
47#define dbgmsg( ... ) \
48    do { \
49        if( tr_deepLoggingIsActive( ) ) \
50            tr_deepLog( __FILE__, __LINE__, NULL, __VA_ARGS__ ); \
51    } while( 0 )
52
53static tr_port
54getRandomPort( tr_session * s )
55{
56    return tr_cryptoWeakRandInt( s->randomPortHigh - s->randomPortLow + 1) + s->randomPortLow;
57}
58
59/* Generate a peer id : "-TRxyzb-" + 12 random alphanumeric
60   characters, where x is the major version number, y is the
61   minor version number, z is the maintenance number, and b
62   designates beta (Azureus-style) */
63uint8_t*
64tr_peerIdNew( void )
65{
66    int          i;
67    int          val;
68    int          total = 0;
69    uint8_t *    buf = tr_new( uint8_t, 21 );
70    const char * pool = "0123456789abcdefghijklmnopqrstuvwxyz";
71    const int    base = 36;
72
73    memcpy( buf, PEERID_PREFIX, 8 );
74
75    for( i = 8; i < 19; ++i )
76    {
77        val = tr_cryptoRandInt( base );
78        total += val;
79        buf[i] = pool[val];
80    }
81
82    val = total % base ? base - ( total % base ) : 0;
83    buf[19] = pool[val];
84    buf[20] = '\0';
85
86    return buf;
87}
88
89const uint8_t*
90tr_getPeerId( void )
91{
92    static uint8_t * id = NULL;
93
94    if( id == NULL )
95        id = tr_peerIdNew( );
96    return id;
97}
98
99/***
100****
101***/
102
103tr_encryption_mode
104tr_sessionGetEncryption( tr_session * session )
105{
106    assert( session );
107
108    return session->encryptionMode;
109}
110
111void
112tr_sessionSetEncryption( tr_session *       session,
113                         tr_encryption_mode mode )
114{
115    assert( session );
116    assert( mode == TR_ENCRYPTION_PREFERRED
117          || mode == TR_ENCRYPTION_REQUIRED
118          || mode == TR_CLEAR_PREFERRED );
119
120    session->encryptionMode = mode;
121}
122
123/***
124****
125***/
126
127static int
128tr_stringEndsWith( const char * str,
129                   const char * end )
130{
131    const size_t slen = strlen( str );
132    const size_t elen = strlen( end );
133
134    return slen >= elen && !memcmp( &str[slen - elen], end, elen );
135}
136
137static void
138loadBlocklists( tr_session * session )
139{
140    int         binCount = 0;
141    int         newCount = 0;
142    struct stat sb;
143    char      * dirname;
144    DIR *       odir = NULL;
145    tr_list *   list = NULL;
146    const tr_bool   isEnabled = session->isBlocklistEnabled;
147
148    /* walk through the directory and find blocklists */
149    dirname = tr_buildPath( session->configDir, "blocklists", NULL );
150    if( !stat( dirname,
151               &sb ) && S_ISDIR( sb.st_mode )
152      && ( ( odir = opendir( dirname ) ) ) )
153    {
154        struct dirent *d;
155        for( d = readdir( odir ); d; d = readdir( odir ) )
156        {
157            char * filename;
158
159            if( !d->d_name || d->d_name[0] == '.' ) /* skip dotfiles, ., and ..
160                                                      */
161                continue;
162
163            filename = tr_buildPath( dirname, d->d_name, NULL );
164
165            if( tr_stringEndsWith( filename, ".bin" ) )
166            {
167                /* if we don't already have this blocklist, add it */
168                if( !tr_list_find( list, filename,
169                                   (TrListCompareFunc)strcmp ) )
170                {
171                    tr_list_append( &list,
172                                   _tr_blocklistNew( filename, isEnabled ) );
173                    ++binCount;
174                }
175            }
176            else
177            {
178                /* strip out the file suffix, if there is one, and add ".bin"
179                  instead */
180                tr_blocklist * b;
181                const char *   dot = strrchr( d->d_name, '.' );
182                const int      len = dot ? dot - d->d_name
183                                         : (int)strlen( d->d_name );
184                char         * tmp = tr_strdup_printf(
185                                        "%s" TR_PATH_DELIMITER_STR "%*.*s.bin",
186                                        dirname, len, len, d->d_name );
187                b = _tr_blocklistNew( tmp, isEnabled );
188                _tr_blocklistSetContent( b, filename );
189                tr_list_append( &list, b );
190                ++newCount;
191                tr_free( tmp );
192            }
193
194            tr_free( filename );
195        }
196
197        closedir( odir );
198    }
199
200    session->blocklists = list;
201
202    if( binCount )
203        tr_dbg( "Found %d blocklists in \"%s\"", binCount, dirname );
204    if( newCount )
205        tr_dbg( "Found %d new blocklists in \"%s\"", newCount, dirname );
206
207    tr_free( dirname );
208}
209
210static tr_bool
211isAltTime( const tr_session * s )
212{
213    int minutes, day;
214    tr_bool withinTime;
215    struct tm tm;
216    const time_t now = time( NULL );
217    const int begin = s->altSpeedTimeBegin;
218    const int end = s->altSpeedTimeEnd;
219    const tr_bool toNextDay = begin > end;
220
221    tr_localtime_r( &now, &tm );
222    minutes = tm.tm_hour*60 + tm.tm_min;
223    day = tm.tm_wday;
224   
225    if( !toNextDay )
226        withinTime = ( begin <= minutes ) && ( minutes < end );
227    else /* goes past midnight */
228        withinTime = ( begin <= minutes ) || ( minutes < end );
229   
230    if( !withinTime )
231        return FALSE;
232   
233    if( toNextDay && (minutes < end) )
234        day = (day - 1) % 7;
235
236    return ((1<<day) & s->altSpeedTimeDay) != 0;
237}
238
239/***
240****
241***/
242
243#ifdef TR_EMBEDDED
244 #define TR_DEFAULT_ENCRYPTION   TR_CLEAR_PREFERRED
245#else
246 #define TR_DEFAULT_ENCRYPTION   TR_ENCRYPTION_PREFERRED
247#endif
248
249void
250tr_sessionGetDefaultSettings( tr_benc * d )
251{
252    assert( tr_bencIsDict( d ) );
253
254    tr_bencDictReserve( d, 35 );
255    tr_bencDictAddBool( d, TR_PREFS_KEY_BLOCKLIST_ENABLED,        FALSE );
256    tr_bencDictAddStr ( d, TR_PREFS_KEY_DOWNLOAD_DIR,             tr_getDefaultDownloadDir( ) );
257    tr_bencDictAddInt ( d, TR_PREFS_KEY_DSPEED,                   100 );
258    tr_bencDictAddBool( d, TR_PREFS_KEY_DSPEED_ENABLED,           FALSE );
259    tr_bencDictAddInt ( d, TR_PREFS_KEY_ENCRYPTION,               TR_DEFAULT_ENCRYPTION );
260    tr_bencDictAddBool( d, TR_PREFS_KEY_LAZY_BITFIELD,            TRUE );
261    tr_bencDictAddInt ( d, TR_PREFS_KEY_MSGLEVEL,                 TR_MSG_INF );
262    tr_bencDictAddInt ( d, TR_PREFS_KEY_OPEN_FILE_LIMIT,          atoi( TR_DEFAULT_OPEN_FILE_LIMIT_STR ) );
263    tr_bencDictAddInt ( d, TR_PREFS_KEY_PEER_LIMIT_GLOBAL,        atoi( TR_DEFAULT_PEER_LIMIT_GLOBAL_STR ) );
264    tr_bencDictAddInt ( d, TR_PREFS_KEY_PEER_LIMIT_TORRENT,       atoi( TR_DEFAULT_PEER_LIMIT_TORRENT_STR ) );
265    tr_bencDictAddInt ( d, TR_PREFS_KEY_PEER_PORT,                atoi( TR_DEFAULT_PEER_PORT_STR ) );
266    tr_bencDictAddBool( d, TR_PREFS_KEY_PEER_PORT_RANDOM_ON_START, FALSE );
267    tr_bencDictAddInt ( d, TR_PREFS_KEY_PEER_PORT_RANDOM_LOW,     1024 );
268    tr_bencDictAddInt ( d, TR_PREFS_KEY_PEER_PORT_RANDOM_HIGH,    65535 );
269    tr_bencDictAddInt ( d, TR_PREFS_KEY_PEER_SOCKET_TOS,          atoi( TR_DEFAULT_PEER_SOCKET_TOS_STR ) );
270    tr_bencDictAddBool( d, TR_PREFS_KEY_PEX_ENABLED,              TRUE );
271    tr_bencDictAddBool( d, TR_PREFS_KEY_PORT_FORWARDING,          TRUE );
272    tr_bencDictAddInt ( d, TR_PREFS_KEY_PREALLOCATION,            TR_PREALLOCATE_FULL );
273    /* tr_bencDictAddInt ( d, TR_PREFS_KEY_PREALLOCATION,            TR_PREALLOCATE_SPARSE ); */
274    tr_bencDictAddStr ( d, TR_PREFS_KEY_PROXY,                    "" );
275    tr_bencDictAddBool( d, TR_PREFS_KEY_PROXY_AUTH_ENABLED,       FALSE );
276    tr_bencDictAddBool( d, TR_PREFS_KEY_PROXY_ENABLED,            FALSE );
277    tr_bencDictAddStr ( d, TR_PREFS_KEY_PROXY_PASSWORD,           "" );
278    tr_bencDictAddInt ( d, TR_PREFS_KEY_PROXY_PORT,               80 );
279    tr_bencDictAddInt ( d, TR_PREFS_KEY_PROXY_TYPE,               TR_PROXY_HTTP );
280    tr_bencDictAddStr ( d, TR_PREFS_KEY_PROXY_USERNAME,           "" );
281    tr_bencDictAddReal( d, TR_PREFS_KEY_RATIO,                    2.0 );
282    tr_bencDictAddBool( d, TR_PREFS_KEY_RATIO_ENABLED,            FALSE );
283    tr_bencDictAddBool( d, TR_PREFS_KEY_RPC_AUTH_REQUIRED,        FALSE );
284    tr_bencDictAddStr ( d, TR_PREFS_KEY_RPC_BIND_ADDRESS,         "0.0.0.0" );
285    tr_bencDictAddBool( d, TR_PREFS_KEY_RPC_ENABLED,              TRUE );
286    tr_bencDictAddStr ( d, TR_PREFS_KEY_RPC_PASSWORD,             "" );
287    tr_bencDictAddStr ( d, TR_PREFS_KEY_RPC_USERNAME,             "" );
288    tr_bencDictAddStr ( d, TR_PREFS_KEY_RPC_WHITELIST,            TR_DEFAULT_RPC_WHITELIST );
289    tr_bencDictAddBool( d, TR_PREFS_KEY_RPC_WHITELIST_ENABLED,    TRUE );
290    tr_bencDictAddInt ( d, TR_PREFS_KEY_RPC_PORT,                 atoi( TR_DEFAULT_RPC_PORT_STR ) );
291    tr_bencDictAddBool( d, TR_PREFS_KEY_ALT_SPEED_ENABLED,        FALSE );
292    tr_bencDictAddInt ( d, TR_PREFS_KEY_ALT_SPEED_UP,             50 ); /* half the regular */
293    tr_bencDictAddInt ( d, TR_PREFS_KEY_ALT_SPEED_DOWN,           50 ); /* half the regular */
294    tr_bencDictAddInt ( d, TR_PREFS_KEY_ALT_SPEED_TIME_BEGIN,     540 ); /* 9am */
295    tr_bencDictAddBool( d, TR_PREFS_KEY_ALT_SPEED_TIME_ENABLED,   FALSE );
296    tr_bencDictAddInt ( d, TR_PREFS_KEY_ALT_SPEED_TIME_END,       1020 ); /* 5pm */
297    tr_bencDictAddInt ( d, TR_PREFS_KEY_ALT_SPEED_TIME_DAY,       TR_SCHED_ALL );
298    tr_bencDictAddInt ( d, TR_PREFS_KEY_USPEED,                   100 );
299    tr_bencDictAddBool( d, TR_PREFS_KEY_USPEED_ENABLED,           FALSE );
300    tr_bencDictAddInt ( d, TR_PREFS_KEY_UPLOAD_SLOTS_PER_TORRENT, 14 );
301    tr_bencDictAddStr ( d, TR_PREFS_KEY_BIND_ADDRESS_IPV4,        TR_DEFAULT_BIND_ADDRESS_IPV4 );
302    tr_bencDictAddStr ( d, TR_PREFS_KEY_BIND_ADDRESS_IPV6,        TR_DEFAULT_BIND_ADDRESS_IPV6 );
303}
304
305const tr_socketList * tr_getSessionBindSockets( const tr_session * session );
306
307void
308tr_sessionGetSettings( tr_session * s, struct tr_benc * d )
309{
310    int i, n=0;
311    char * freeme[16];
312
313    assert( tr_bencIsDict( d ) );
314
315    tr_bencDictReserve( d, 30 );
316    tr_bencDictAddBool( d, TR_PREFS_KEY_BLOCKLIST_ENABLED,        tr_blocklistIsEnabled( s ) );
317    tr_bencDictAddStr ( d, TR_PREFS_KEY_DOWNLOAD_DIR,             s->downloadDir );
318    tr_bencDictAddInt ( d, TR_PREFS_KEY_DSPEED,                   tr_sessionGetSpeedLimit( s, TR_DOWN ) );
319    tr_bencDictAddBool( d, TR_PREFS_KEY_DSPEED_ENABLED,           tr_sessionIsSpeedLimited( s, TR_DOWN ) );
320    tr_bencDictAddInt ( d, TR_PREFS_KEY_ENCRYPTION,               s->encryptionMode );
321    tr_bencDictAddBool( d, TR_PREFS_KEY_LAZY_BITFIELD,            s->useLazyBitfield );
322    tr_bencDictAddInt ( d, TR_PREFS_KEY_MSGLEVEL,                 tr_getMessageLevel( ) );
323    tr_bencDictAddInt ( d, TR_PREFS_KEY_OPEN_FILE_LIMIT,          s->openFileLimit );
324    tr_bencDictAddInt ( d, TR_PREFS_KEY_PEER_LIMIT_GLOBAL,        tr_sessionGetPeerLimit( s ) );
325    tr_bencDictAddInt ( d, TR_PREFS_KEY_PEER_LIMIT_TORRENT,       s->peerLimitPerTorrent );
326    tr_bencDictAddInt ( d, TR_PREFS_KEY_PEER_PORT,                tr_sessionGetPeerPort( s ) );
327    tr_bencDictAddBool( d, TR_PREFS_KEY_PEER_PORT_RANDOM_ON_START, s->isPortRandom );
328    tr_bencDictAddInt ( d, TR_PREFS_KEY_PEER_PORT_RANDOM_LOW,     s->randomPortLow );
329    tr_bencDictAddInt ( d, TR_PREFS_KEY_PEER_PORT_RANDOM_HIGH,    s->randomPortHigh );
330    tr_bencDictAddInt ( d, TR_PREFS_KEY_PEER_SOCKET_TOS,          s->peerSocketTOS );
331    tr_bencDictAddBool( d, TR_PREFS_KEY_PEX_ENABLED,              s->isPexEnabled );
332    tr_bencDictAddBool( d, TR_PREFS_KEY_PORT_FORWARDING,          tr_sessionIsPortForwardingEnabled( s ) );
333    tr_bencDictAddInt ( d, TR_PREFS_KEY_PREALLOCATION,            s->preallocationMode );
334    tr_bencDictAddStr ( d, TR_PREFS_KEY_PROXY,                    s->proxy );
335    tr_bencDictAddBool( d, TR_PREFS_KEY_PROXY_AUTH_ENABLED,       s->isProxyAuthEnabled );
336    tr_bencDictAddBool( d, TR_PREFS_KEY_PROXY_ENABLED,            s->isProxyEnabled );
337    tr_bencDictAddStr ( d, TR_PREFS_KEY_PROXY_PASSWORD,           s->proxyPassword );
338    tr_bencDictAddInt ( d, TR_PREFS_KEY_PROXY_PORT,               s->proxyPort );
339    tr_bencDictAddInt ( d, TR_PREFS_KEY_PROXY_TYPE,               s->proxyType );
340    tr_bencDictAddStr ( d, TR_PREFS_KEY_PROXY_USERNAME,           s->proxyUsername );
341    tr_bencDictAddReal( d, TR_PREFS_KEY_RATIO,                    s->desiredRatio );
342    tr_bencDictAddBool( d, TR_PREFS_KEY_RATIO_ENABLED,            s->isRatioLimited );
343    tr_bencDictAddBool( d, TR_PREFS_KEY_RPC_AUTH_REQUIRED,        tr_sessionIsRPCPasswordEnabled( s ) );
344    tr_bencDictAddStr ( d, TR_PREFS_KEY_RPC_BIND_ADDRESS,         tr_sessionGetRPCBindAddress( s ) );
345    tr_bencDictAddBool( d, TR_PREFS_KEY_RPC_ENABLED,              tr_sessionIsRPCEnabled( s ) );
346    tr_bencDictAddStr ( d, TR_PREFS_KEY_RPC_PASSWORD,             freeme[n++] = tr_sessionGetRPCPassword( s ) );
347    tr_bencDictAddInt ( d, TR_PREFS_KEY_RPC_PORT,                 tr_sessionGetRPCPort( s ) );
348    tr_bencDictAddStr ( d, TR_PREFS_KEY_RPC_USERNAME,             freeme[n++] = tr_sessionGetRPCUsername( s ) );
349    tr_bencDictAddStr ( d, TR_PREFS_KEY_RPC_WHITELIST,            freeme[n++] = tr_sessionGetRPCWhitelist( s ) );
350    tr_bencDictAddBool( d, TR_PREFS_KEY_RPC_WHITELIST_ENABLED,    tr_sessionGetRPCWhitelistEnabled( s ) );
351    tr_bencDictAddBool( d, TR_PREFS_KEY_ALT_SPEED_ENABLED,        tr_sessionUsesAltSpeed( s ) );
352    tr_bencDictAddInt ( d, TR_PREFS_KEY_ALT_SPEED_UP,             tr_sessionGetAltSpeed( s, TR_UP ) );
353    tr_bencDictAddInt ( d, TR_PREFS_KEY_ALT_SPEED_DOWN,           tr_sessionGetAltSpeed( s, TR_DOWN ) );
354    tr_bencDictAddInt ( d, TR_PREFS_KEY_ALT_SPEED_TIME_BEGIN,     tr_sessionGetAltSpeedBegin( s ) );
355    tr_bencDictAddBool( d, TR_PREFS_KEY_ALT_SPEED_TIME_ENABLED,   tr_sessionUsesAltSpeedTime( s ) );
356    tr_bencDictAddInt ( d, TR_PREFS_KEY_ALT_SPEED_TIME_END,       tr_sessionGetAltSpeedEnd( s ) );
357    tr_bencDictAddInt ( d, TR_PREFS_KEY_ALT_SPEED_TIME_DAY,       tr_sessionGetAltSpeedDay( s ) );
358    tr_bencDictAddInt ( d, TR_PREFS_KEY_USPEED,                   tr_sessionGetSpeedLimit( s, TR_UP ) );
359    tr_bencDictAddBool( d, TR_PREFS_KEY_USPEED_ENABLED,           tr_sessionIsSpeedLimited( s, TR_UP ) );
360    tr_bencDictAddInt ( d, TR_PREFS_KEY_UPLOAD_SLOTS_PER_TORRENT, s->uploadSlotsPerTorrent );
361    tr_bencDictAddStr ( d, TR_PREFS_KEY_BIND_ADDRESS_IPV4,
362                        tr_ntop_non_ts( tr_socketListGetType( tr_getSessionBindSockets( s ), TR_AF_INET ) ) );
363    tr_bencDictAddStr ( d, TR_PREFS_KEY_BIND_ADDRESS_IPV6,
364                        tr_ntop_non_ts( tr_socketListGetType( tr_getSessionBindSockets( s ), TR_AF_INET6 ) ) );
365
366    for( i=0; i<n; ++i )
367        tr_free( freeme[i] );
368}
369
370void
371tr_sessionLoadSettings( tr_benc * d, const char * configDir, const char * appName )
372{
373    char * filename;
374    tr_benc fileSettings;
375    tr_benc sessionDefaults;
376    tr_benc tmp;
377
378    assert( tr_bencIsDict( d ) );
379
380    /* initializing the defaults: caller may have passed in some app-level defaults.
381     * preserve those and use the session defaults to fill in any missing gaps. */
382    tr_bencInitDict( &sessionDefaults, 0 );
383    tr_sessionGetDefaultSettings( &sessionDefaults );
384    tr_bencMergeDicts( &sessionDefaults, d );
385    tmp = *d; *d = sessionDefaults; sessionDefaults = tmp;
386
387    /* if caller didn't specify a config dir, use the default */
388    if( !configDir || !*configDir )
389        configDir = tr_getDefaultConfigDir( appName );
390
391    /* file settings override the defaults */
392    filename = tr_buildPath( configDir, "settings.json", NULL );
393    if( !tr_bencLoadJSONFile( filename, &fileSettings ) ) {
394        tr_bencMergeDicts( d, &fileSettings );
395        tr_bencFree( &fileSettings );
396    }
397
398    /* cleanup */
399    tr_bencFree( &sessionDefaults );
400    tr_free( filename );
401}
402
403void
404tr_sessionSaveSettings( tr_session    * session,
405                        const char    * configDir,
406                        const tr_benc * clientSettings )
407{
408    tr_benc settings;
409    char * filename = tr_buildPath( configDir, "settings.json", NULL );
410
411    assert( tr_bencIsDict( clientSettings ) );
412
413    tr_bencInitDict( &settings, 0 );
414
415    /* the existing file settings are the fallback values */
416    {
417        tr_benc fileSettings;
418        if( !tr_bencLoadJSONFile( filename, &fileSettings ) )
419        {
420            tr_bencMergeDicts( &settings, &fileSettings );
421            tr_bencFree( &fileSettings );
422        }
423    }
424
425    /* the client's settings override the file settings */
426    tr_bencMergeDicts( &settings, clientSettings );
427
428    /* the session's true values override the file & client settings */
429    {
430        tr_benc sessionSettings;
431        tr_bencInitDict( &sessionSettings, 0 );
432        tr_sessionGetSettings( session, &sessionSettings );
433        tr_bencMergeDicts( &settings, &sessionSettings );
434        tr_bencFree( &sessionSettings );
435    }
436
437    /* save the result */
438    tr_bencSaveJSONFile( filename, &settings );
439    tr_inf( "Saved \"%s\"", filename );
440
441    /* cleanup */
442    tr_free( filename );
443    tr_bencFree( &settings );
444}
445
446static void metainfoLookupRescan( tr_session * );
447static void tr_sessionInitImpl( void * );
448static void onAltTimer( int, short, void* );
449static void setAltTimer( tr_session * session );
450
451struct init_data
452{
453    tr_session  * session;
454    const char  * configDir;
455    tr_bool       messageQueuingEnabled;
456    tr_benc     * clientSettings;
457};
458
459tr_session *
460tr_sessionInit( const char  * tag,
461                const char  * configDir,
462                tr_bool       messageQueuingEnabled,
463                tr_benc     * clientSettings )
464{
465    tr_session * session;
466    struct init_data data;
467
468    assert( tr_bencIsDict( clientSettings ) );
469
470    /* initialize the bare skeleton of the session object */
471    session = tr_new0( tr_session, 1 );
472    session->bandwidth = tr_bandwidthNew( session, NULL );
473    session->lock = tr_lockNew( );
474    session->tag = tr_strdup( tag );
475    session->magicNumber = SESSION_MAGIC_NUMBER;
476    tr_bencInitList( &session->removedTorrents, 0 );
477
478    /* start the libtransmission thread */
479    tr_netInit( ); /* must go before tr_eventInit */
480    tr_eventInit( session );
481    assert( session->events != NULL );
482
483    /* run the rest in the libtransmission thread */
484    session->isWaiting = TRUE;
485    data.session = session;
486    data.configDir = configDir;
487    data.messageQueuingEnabled = messageQueuingEnabled;
488    data.clientSettings = clientSettings;
489    tr_runInEventThread( session, tr_sessionInitImpl, &data );
490    while( session->isWaiting )
491        tr_wait( 100 );
492
493    return session;
494}
495
496static void useAltSpeed( tr_session * session, tr_bool enabled, tr_bool byUser );
497static void useAltSpeedTime( tr_session * session, tr_bool enabled, tr_bool byUser );
498
499static void
500tr_sessionInitImpl( void * vdata )
501{
502    int64_t i;
503    int64_t j;
504    double  d;
505    tr_bool found;
506    tr_bool boolVal;
507    const char * str;
508    tr_benc settings;
509    char * filename;
510    struct init_data * data = vdata;
511    tr_benc * clientSettings = data->clientSettings;
512    tr_session * session = data->session;
513    tr_address address;
514    tr_socketList * socketList;
515
516    assert( tr_amInEventThread( session ) );
517    assert( tr_bencIsDict( clientSettings ) );
518
519    dbgmsg( "tr_sessionInit: the session's top-level bandwidth object is %p", session->bandwidth );
520
521    tr_bencInitDict( &settings, 0 );
522    tr_sessionGetDefaultSettings( &settings );
523    tr_bencMergeDicts( &settings, clientSettings );
524
525#ifndef WIN32
526    /* Don't exit when writing on a broken socket */
527    signal( SIGPIPE, SIG_IGN );
528#endif
529
530    found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_PEER_LIMIT_TORRENT, &i );
531    assert( found );
532    session->peerLimitPerTorrent = i;
533
534    found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_MSGLEVEL, &i );
535    assert( found );
536    tr_setMessageLevel( i );
537    tr_setMessageQueuing( data->messageQueuingEnabled );
538
539
540    found = tr_bencDictFindBool( &settings, TR_PREFS_KEY_PEX_ENABLED, &boolVal );
541    assert( found );
542    session->isPexEnabled = boolVal;
543
544    found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_ENCRYPTION, &i );
545    assert( found );
546    assert( tr_isEncryptionMode( i ) );
547    session->encryptionMode = i;
548
549    found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_PREALLOCATION, &i );
550    assert( found );
551    assert( tr_isPreallocationMode( i ) );
552    session->preallocationMode = i;
553
554    found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_PEER_SOCKET_TOS, &i );
555    assert( found );
556    session->peerSocketTOS = i;
557
558    found = tr_bencDictFindStr( &settings, TR_PREFS_KEY_DOWNLOAD_DIR, &str );
559    assert( found );
560    session->downloadDir = tr_strdup( str );
561
562    found = tr_bencDictFindBool( &settings, TR_PREFS_KEY_PROXY_ENABLED, &boolVal );
563    assert( found );
564    session->isProxyEnabled = boolVal;
565
566    found = tr_bencDictFindStr( &settings, TR_PREFS_KEY_PROXY, &str );
567    assert( found );
568    session->proxy = tr_strdup( str );
569
570    found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_PROXY_PORT, &i );
571    assert( found );
572    session->proxyPort = i;
573
574    found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_PROXY_TYPE, &i );
575    assert( found );
576    session->proxyType = i;
577
578    found = tr_bencDictFindBool( &settings, TR_PREFS_KEY_PROXY_AUTH_ENABLED, &boolVal );
579    assert( found );
580    session->isProxyAuthEnabled = boolVal;
581
582    found = tr_bencDictFindStr( &settings, TR_PREFS_KEY_PROXY_USERNAME, &str );
583    assert( found );
584    session->proxyUsername = tr_strdup( str );
585
586    found = tr_bencDictFindStr( &settings, TR_PREFS_KEY_PROXY_PASSWORD, &str );
587    assert( found );
588    session->proxyPassword = tr_strdup( str );
589
590    session->so_sndbuf = 1500 * 3; /* 3x MTU for most ethernet/wireless */
591    session->so_rcvbuf = 8192;
592
593    tr_setConfigDir( session, data->configDir );
594
595    tr_trackerSessionInit( session );
596    assert( session->tracker != NULL );
597
598    session->peerMgr = tr_peerMgrNew( session );
599
600    found = tr_bencDictFindBool( &settings, TR_PREFS_KEY_LAZY_BITFIELD, &boolVal );
601    assert( found );
602    session->useLazyBitfield = boolVal;
603
604    /* Initialize rate and file descripts controls */
605
606    found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_OPEN_FILE_LIMIT, &i );
607    assert( found );
608    session->openFileLimit = i;
609    found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_PEER_LIMIT_GLOBAL, &j );
610    assert( found );
611    tr_fdInit( session->openFileLimit, j );
612
613    /**
614    *** random port
615    **/
616
617    found = tr_bencDictFindBool( &settings, TR_PREFS_KEY_PEER_PORT_RANDOM_ON_START, &boolVal );
618    assert( found );
619    session->isPortRandom = boolVal;
620
621    found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_PEER_PORT_RANDOM_LOW, &i );
622    assert( found );
623    session->randomPortLow = i;
624
625    found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_PEER_PORT_RANDOM_HIGH, &i );
626    assert( found );
627    session->randomPortHigh = i;
628
629    found = tr_bencDictFindBool( &settings, TR_PREFS_KEY_PORT_FORWARDING, &boolVal )
630         && tr_bencDictFindInt( &settings, TR_PREFS_KEY_PEER_PORT, &j );
631    assert( found );
632    session->peerPort = session->isPortRandom ? getRandomPort( session ) : j;
633
634    /* bind addresses */
635
636    found = tr_bencDictFindStr( &settings, TR_PREFS_KEY_BIND_ADDRESS_IPV4,
637                                &str );
638    assert( found );
639    if( tr_pton( str, &address ) == NULL ) {
640        tr_err( _( "%s is not a valid address" ), str );
641        socketList = tr_socketListNew( &tr_inaddr_any );
642    } else if( address.type != TR_AF_INET ) {
643        tr_err( _( "%s is not an IPv4 address" ), str );
644        socketList = tr_socketListNew( &tr_inaddr_any );
645    } else
646        socketList = tr_socketListNew( &address );
647
648    found = tr_bencDictFindStr( &settings, TR_PREFS_KEY_BIND_ADDRESS_IPV6,
649                                &str );
650    assert( found );
651    if( tr_pton( str, &address ) == NULL ) {
652        tr_err( _( "%s is not a valid address" ), str );
653        address = tr_in6addr_any;
654    } else if( address.type != TR_AF_INET6 ) {
655        tr_err( _( "%s is not an IPv6 address" ), str );
656        address = tr_in6addr_any;
657    }
658    if( tr_net_hasIPv6( session->peerPort ) )
659        tr_socketListAppend( socketList, &address );
660    else
661        tr_inf( _( "System does not seem to support IPv6. Not listening on"
662                   "an IPv6 address" ) );
663
664    session->shared = tr_sharedInit( session, boolVal, session->peerPort,
665                                     socketList );
666    session->isPortSet = session->peerPort > 0;
667
668    /**
669    **/
670
671    found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_UPLOAD_SLOTS_PER_TORRENT, &i );
672    assert( found );
673    session->uploadSlotsPerTorrent = i;
674
675    found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_USPEED, &i )
676         && tr_bencDictFindBool( &settings, TR_PREFS_KEY_USPEED_ENABLED, &boolVal );
677    assert( found );
678    tr_sessionSetSpeedLimit( session, TR_UP, i );
679    tr_sessionLimitSpeed( session, TR_UP, boolVal );
680
681    found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_DSPEED, &i )
682         && tr_bencDictFindBool( &settings, TR_PREFS_KEY_DSPEED_ENABLED, &boolVal );
683    assert( found );
684    tr_sessionSetSpeedLimit( session, TR_DOWN, i );
685    tr_sessionLimitSpeed( session, TR_DOWN, boolVal );
686
687    found = tr_bencDictFindReal( &settings, TR_PREFS_KEY_RATIO, &d )
688         && tr_bencDictFindBool( &settings, TR_PREFS_KEY_RATIO_ENABLED, &boolVal );
689    assert( found );
690    tr_sessionSetRatioLimit( session, d );
691    tr_sessionSetRatioLimited( session, boolVal );
692
693    /**
694    ***  Alternate speed limits
695    **/
696
697    found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_ALT_SPEED_UP, &i );
698    assert( found );
699    session->altSpeed[TR_UP] = i;
700
701    found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_ALT_SPEED_DOWN, &i );
702    assert( found );
703    session->altSpeed[TR_DOWN] = i;
704
705    found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_ALT_SPEED_TIME_BEGIN, &i );
706    assert( found );
707    session->altSpeedTimeBegin = i;
708
709    found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_ALT_SPEED_TIME_END, &i );
710    assert( found );
711    session->altSpeedTimeEnd = i;
712   
713    found = tr_bencDictFindInt( &settings, TR_PREFS_KEY_ALT_SPEED_TIME_DAY, &i );
714    assert( found );
715    session->altSpeedTimeDay = i;
716
717    found = tr_bencDictFindBool( &settings, TR_PREFS_KEY_ALT_SPEED_TIME_ENABLED, &boolVal );
718    assert( found );
719    useAltSpeedTime( session, boolVal, FALSE );
720
721    if( !boolVal )
722    {
723        found = tr_bencDictFindBool( &settings, TR_PREFS_KEY_ALT_SPEED_ENABLED, &boolVal );
724        assert( found );
725        useAltSpeed( session, boolVal, FALSE );
726    }
727    else
728        useAltSpeed( session, isAltTime( session ), FALSE );
729
730    /**
731    ***  Blocklist
732    **/
733
734    filename = tr_buildPath( session->configDir, "blocklists", NULL );
735    tr_mkdirp( filename, 0777 );
736    tr_free( filename );
737    found = tr_bencDictFindBool( &settings, TR_PREFS_KEY_BLOCKLIST_ENABLED, &boolVal );
738    assert( found );
739    session->isBlocklistEnabled = boolVal;
740    loadBlocklists( session );
741
742    session->rpcServer = tr_rpcInit( session, &settings );
743
744    tr_bencFree( &settings );
745
746    assert( tr_isSession( session ) );
747
748    session->altTimer = tr_new0( struct event, 1 );
749    evtimer_set( session->altTimer, onAltTimer, session );
750    setAltTimer( session );
751
752    /* first %s is the application name
753       second %s is the version number */
754    tr_inf( _( "%s %s started" ), TR_NAME, LONG_VERSION_STRING );
755
756    tr_statsInit( session );
757    session->web = tr_webInit( session );
758    metainfoLookupRescan( session );
759    session->isWaiting = FALSE;
760    dbgmsg( "returning session %p; session->tracker is %p", session, session->tracker );
761}
762
763/***
764****
765***/
766
767void
768tr_sessionSetDownloadDir( tr_session * session, const char * dir )
769{
770    assert( tr_isSession( session ) );
771
772    if( session->downloadDir != dir )
773    {
774        tr_free( session->downloadDir );
775        session->downloadDir = tr_strdup( dir );
776    }
777}
778
779const char *
780tr_sessionGetDownloadDir( const tr_session * session )
781{
782    assert( tr_isSession( session ) );
783
784    return session->downloadDir;
785}
786
787/***
788****
789***/
790
791void
792tr_globalLock( tr_session * session )
793{
794    assert( tr_isSession( session ) );
795
796    tr_lockLock( session->lock );
797}
798
799void
800tr_globalUnlock( tr_session * session )
801{
802    assert( tr_isSession( session ) );
803
804    tr_lockUnlock( session->lock );
805}
806
807tr_bool
808tr_globalIsLocked( const tr_session * session )
809{
810    return tr_isSession( session ) && tr_lockHave( session->lock );
811}
812
813/***********************************************************************
814 * tr_setBindPort
815 ***********************************************************************
816 *
817 **********************************************************************/
818
819struct bind_port_data
820{
821    tr_session * session;
822    tr_port      port;
823};
824
825static void
826tr_setBindPortImpl( void * vdata )
827{
828    struct bind_port_data * data = vdata;
829    tr_session * session = data->session;
830    const tr_port port = data->port;
831
832    session->isPortSet = 1;
833    tr_sharedSetPort( session->shared, port );
834
835    tr_free( data );
836}
837
838static void
839setPortImpl( tr_session * session, tr_port port )
840{
841    struct bind_port_data * data;
842
843    assert( tr_isSession( session ) );
844
845    data = tr_new( struct bind_port_data, 1 );
846    data->session = session;
847    data->port = port;
848    tr_runInEventThread( session, tr_setBindPortImpl, data );
849}
850
851void
852tr_sessionSetPeerPort( tr_session * session,
853                       tr_port      port )
854{
855    assert( tr_isSession( session ) );
856
857    session->peerPort = port;
858    setPortImpl( session, session->peerPort );
859}
860
861tr_port
862tr_sessionGetPeerPort( const tr_session * session )
863{
864    assert( tr_isSession( session ) );
865
866    return session->peerPort;
867}
868
869tr_port
870tr_sessionSetPeerPortRandom( tr_session * session )
871{
872    assert( tr_isSession( session ) );
873
874    session->peerPort = getRandomPort( session );
875    setPortImpl( session, session->peerPort );
876    return session->peerPort;
877}
878
879void
880tr_sessionSetPeerPortRandomOnStart( tr_session * session,
881                                    tr_bool random )
882{
883    assert( tr_isSession( session ) );
884
885    session->isPortRandom = random;
886}
887
888tr_bool
889tr_sessionGetPeerPortRandomOnStart( tr_session * session )
890{
891    assert( tr_isSession( session ) );
892
893    return session->isPortRandom;
894}
895
896tr_port_forwarding
897tr_sessionGetPortForwarding( const tr_session * session )
898{
899    assert( tr_isSession( session ) );
900
901    return tr_sharedTraversalStatus( session->shared );
902}
903
904/***
905****
906***/
907
908static void
909updateSeedRatio( tr_session * session )
910{
911    tr_torrent * tor = NULL;
912
913    while(( tor = tr_torrentNext( session, tor )))
914        tor->needsSeedRatioCheck = TRUE;
915}
916
917void
918tr_sessionSetRatioLimited( tr_session * session, tr_bool isLimited )
919{
920    assert( tr_isSession( session ) );
921
922    session->isRatioLimited = isLimited;
923    updateSeedRatio( session );
924}
925
926void
927tr_sessionSetRatioLimit( tr_session * session, double desiredRatio )
928{
929    assert( tr_isSession( session ) );
930
931    session->desiredRatio = desiredRatio;
932    updateSeedRatio( session );
933}
934
935tr_bool
936tr_sessionIsRatioLimited( const tr_session  * session )
937{
938    assert( tr_isSession( session ) );
939
940    return session->isRatioLimited;
941}
942
943double
944tr_sessionGetRatioLimit( const tr_session * session )
945{
946    assert( tr_isSession( session ) );
947
948    return session->desiredRatio;
949}
950
951/***
952****  SPEED LIMITS
953***/
954
955tr_bool
956tr_sessionGetActiveSpeedLimit( const tr_session * session, tr_direction dir, int * setme )
957{
958    int isLimited = TRUE;
959
960    if( tr_sessionUsesAltSpeed( session ) )
961        *setme = tr_sessionGetAltSpeed( session, dir );
962    else if( tr_sessionIsSpeedLimited( session, dir ) )
963        *setme = tr_sessionGetSpeedLimit( session, dir );
964    else
965        isLimited = FALSE;
966
967    return isLimited;
968}
969
970static void
971updateBandwidth( tr_session * session, tr_direction dir )
972{
973    int limit;
974    const tr_bool isLimited = tr_sessionGetActiveSpeedLimit( session, dir, &limit );
975    const tr_bool zeroCase = isLimited && !limit;
976
977    tr_bandwidthSetLimited( session->bandwidth, dir, isLimited && !zeroCase );
978
979    tr_bandwidthSetDesiredSpeed( session->bandwidth, dir, limit );
980}
981
982static void
983altSpeedToggled( void * vsession )
984{
985    tr_session * session = vsession;
986
987    assert( tr_isSession( session ) );
988
989    updateBandwidth( session, TR_UP );
990    updateBandwidth( session, TR_DOWN );
991
992    if( session->altCallback != NULL )
993        (*session->altCallback)( session, session->altSpeedEnabled, session->altSpeedChangedByUser, session->altCallbackUserData );
994}
995
996/* tell the alt speed limit timer to fire again at the top of the minute */
997static void
998setAltTimer( tr_session * session )
999{
1000    const time_t now = time( NULL );
1001    struct tm tm;
1002    struct timeval tv;
1003
1004    assert( tr_isSession( session ) );
1005    assert( session->altTimer != NULL );
1006
1007    tr_localtime_r( &now, &tm );
1008    tv.tv_sec = 60 - tm.tm_sec;
1009    tv.tv_usec = 0;
1010    evtimer_add( session->altTimer, &tv );
1011}
1012
1013/* this is called once a minute to:
1014 * (1) update session->isAltTime
1015 * (2) alter the speed limits when the alt limits go on and off */
1016static void
1017onAltTimer( int foo UNUSED, short bar UNUSED, void * vsession )
1018{
1019    tr_session * session = vsession;
1020
1021    assert( tr_isSession( session ) );
1022
1023    if( session->altSpeedTimeEnabled )
1024    {
1025        const time_t now = time( NULL );
1026        struct tm tm;
1027        int currentMinute, day;
1028        tr_bool isBeginTime, isEndTime, isDay;
1029        tr_localtime_r( &now, &tm );
1030        currentMinute = tm.tm_hour*60 + tm.tm_min;
1031        day = tm.tm_wday;
1032       
1033        isBeginTime = currentMinute == session->altSpeedTimeBegin;
1034        isEndTime = currentMinute == session->altSpeedTimeEnd;
1035        if( isBeginTime || isEndTime )
1036        {
1037            /* if looking at the end date, look at the next day if end time is before begin time */
1038            if( isEndTime && !isBeginTime && session->altSpeedTimeEnd < session->altSpeedTimeBegin )
1039                day = (day - 1) % 7;
1040           
1041            isDay = ((1<<day) & session->altSpeedTimeDay) != 0;
1042
1043            if( isDay )
1044                useAltSpeed( session, isBeginTime, FALSE );
1045        }
1046    }
1047
1048    setAltTimer( session );
1049}
1050
1051/***
1052****  Primary session speed limits
1053***/
1054
1055void
1056tr_sessionSetSpeedLimit( tr_session * s, tr_direction d, int KB_s )
1057{
1058    assert( tr_isSession( s ) );
1059    assert( tr_isDirection( d ) );
1060    assert( KB_s >= 0 );
1061
1062    s->speedLimit[d] = KB_s;
1063
1064    updateBandwidth( s, d );
1065}
1066
1067int
1068tr_sessionGetSpeedLimit( const tr_session * s, tr_direction d )
1069{
1070    assert( tr_isSession( s ) );
1071    assert( tr_isDirection( d ) );
1072
1073    return s->speedLimit[d];
1074}
1075
1076void
1077tr_sessionLimitSpeed( tr_session * s, tr_direction d, tr_bool b )
1078{
1079    assert( tr_isSession( s ) );
1080    assert( tr_isDirection( d ) );
1081    assert( tr_isBool( b ) );
1082
1083    s->speedLimitEnabled[d] = b;
1084
1085    updateBandwidth( s, d );
1086}
1087
1088tr_bool
1089tr_sessionIsSpeedLimited( const tr_session * s, tr_direction d )
1090{
1091    assert( tr_isSession( s ) );
1092    assert( tr_isDirection( d ) );
1093
1094    return s->speedLimitEnabled[d];
1095}
1096
1097/***
1098****  Alternative speed limits that are used during scheduled times
1099***/
1100
1101void
1102tr_sessionSetAltSpeed( tr_session * s, tr_direction d, int KB_s )
1103{
1104    assert( tr_isSession( s ) );
1105    assert( tr_isDirection( d ) );
1106    assert( KB_s >= 0 );
1107
1108    s->altSpeed[d] = KB_s;
1109
1110    updateBandwidth( s, d );
1111}
1112
1113int
1114tr_sessionGetAltSpeed( const tr_session * s, tr_direction d )
1115{
1116    assert( tr_isSession( s ) );
1117    assert( tr_isDirection( d ) );
1118
1119    return s->altSpeed[d]; 
1120}
1121
1122void
1123useAltSpeedTime( tr_session * session, tr_bool enabled, tr_bool byUser )
1124{
1125    assert( tr_isSession( session ) );
1126    assert( tr_isBool( enabled ) );
1127    assert( tr_isBool( byUser ) );
1128
1129    if( session->altSpeedTimeEnabled != enabled )
1130    {
1131        const tr_bool isAlt = isAltTime( session );
1132
1133        session->altSpeedTimeEnabled = enabled;
1134
1135        if( enabled && session->altSpeedEnabled != isAlt )
1136            useAltSpeed( session, isAlt, byUser );
1137    }
1138}
1139void
1140tr_sessionUseAltSpeedTime( tr_session * s, tr_bool b )
1141{
1142    useAltSpeedTime( s, b, TRUE );
1143}
1144
1145tr_bool
1146tr_sessionUsesAltSpeedTime( const tr_session * s )
1147{
1148    assert( tr_isSession( s ) );
1149
1150    return s->altSpeedTimeEnabled;
1151}
1152
1153void
1154tr_sessionSetAltSpeedBegin( tr_session * s, int minutes )
1155{
1156    assert( tr_isSession( s ) );
1157    assert( 0<=minutes && minutes<(60*24) );
1158
1159    if( s->altSpeedTimeBegin != minutes )
1160    {
1161        s->altSpeedTimeBegin = minutes;
1162
1163        if( tr_sessionUsesAltSpeedTime( s ) )
1164            useAltSpeed( s, isAltTime( s ), TRUE );
1165    }
1166}
1167
1168int
1169tr_sessionGetAltSpeedBegin( const tr_session * s )
1170{
1171    assert( tr_isSession( s ) );
1172
1173    return s->altSpeedTimeBegin;
1174}
1175
1176void
1177tr_sessionSetAltSpeedEnd( tr_session * s, int minutes )
1178{
1179    assert( tr_isSession( s ) );
1180    assert( 0<=minutes && minutes<(60*24) );
1181
1182    if( s->altSpeedTimeEnd != minutes )
1183    {
1184        s->altSpeedTimeEnd = minutes;
1185
1186        if( tr_sessionUsesAltSpeedTime( s ) )
1187            useAltSpeed( s, isAltTime( s ), TRUE );
1188    }
1189}
1190
1191int
1192tr_sessionGetAltSpeedEnd( const tr_session * s )
1193{
1194    assert( tr_isSession( s ) );
1195
1196    return s->altSpeedTimeEnd;
1197}
1198
1199void
1200tr_sessionSetAltSpeedDay( tr_session * s, tr_sched_day day )
1201{
1202    assert( tr_isSession( s ) );
1203
1204    if( s->altSpeedTimeDay != day )
1205    {
1206        s->altSpeedTimeDay = day;
1207
1208        if( tr_sessionUsesAltSpeedTime( s ) )
1209            useAltSpeed( s, isAltTime( s ), TRUE );
1210    }
1211}
1212
1213tr_sched_day
1214tr_sessionGetAltSpeedDay( const tr_session * s )
1215{
1216    assert( tr_isSession( s ) );
1217
1218    return s->altSpeedTimeDay;
1219}
1220
1221void
1222useAltSpeed( tr_session * s, tr_bool enabled, tr_bool byUser )
1223{
1224    assert( tr_isSession( s ) );
1225    assert( tr_isBool( enabled ) );
1226    assert( tr_isBool( byUser ) );
1227
1228    if( s->altSpeedEnabled != enabled)
1229    {
1230        s->altSpeedEnabled = enabled;
1231        s->altSpeedChangedByUser = byUser;
1232   
1233        tr_runInEventThread( s, altSpeedToggled, s );
1234    }
1235}
1236void
1237tr_sessionUseAltSpeed( tr_session * session, tr_bool enabled )
1238{
1239    useAltSpeed( session, enabled, TRUE );
1240}
1241
1242tr_bool
1243tr_sessionUsesAltSpeed( const tr_session * s )
1244{
1245    assert( tr_isSession( s ) );
1246
1247    return s->altSpeedEnabled;
1248}
1249
1250void
1251tr_sessionSetAltSpeedFunc( tr_session       * session,
1252                           tr_altSpeedFunc    func,
1253                           void             * userData )
1254{
1255    assert( tr_isSession( session ) );
1256
1257    session->altCallback = func;
1258    session->altCallbackUserData = userData;
1259}
1260
1261void
1262tr_sessionClearAltSpeedFunc( tr_session * session )
1263{
1264    tr_sessionSetAltSpeedFunc( session, NULL, NULL );
1265}
1266
1267/***
1268****
1269***/
1270
1271void
1272tr_sessionSetPeerLimit( tr_session * session, uint16_t maxGlobalPeers )
1273{
1274    assert( tr_isSession( session ) );
1275
1276    tr_fdSetPeerLimit( maxGlobalPeers );
1277}
1278
1279uint16_t
1280tr_sessionGetPeerLimit( const tr_session * session )
1281{
1282    assert( tr_isSession( session ) );
1283
1284    return tr_fdGetPeerLimit( );
1285}
1286
1287void
1288tr_sessionSetPeerLimitPerTorrent( tr_session  * session, uint16_t n )
1289{
1290    assert( tr_isSession( session ) );
1291
1292    session->peerLimitPerTorrent = n;
1293}
1294
1295uint16_t
1296tr_sessionGetPeerLimitPerTorrent( const tr_session * session )
1297{
1298    assert( tr_isSession( session ) );
1299
1300    return session->peerLimitPerTorrent;
1301}
1302
1303/***
1304****
1305***/
1306
1307double
1308tr_sessionGetPieceSpeed( const tr_session * session, tr_direction dir )
1309{
1310    return tr_isSession( session ) ? tr_bandwidthGetPieceSpeed( session->bandwidth, 0, dir ) : 0.0;
1311}
1312
1313double
1314tr_sessionGetRawSpeed( const tr_session * session, tr_direction dir )
1315{
1316    return tr_isSession( session ) ? tr_bandwidthGetPieceSpeed( session->bandwidth, 0, dir ) : 0.0;
1317}
1318
1319int
1320tr_sessionCountTorrents( const tr_session * session )
1321{
1322    return tr_isSession( session ) ? session->torrentCount : 0;
1323}
1324
1325static int
1326compareTorrentByCur( const void * va, const void * vb )
1327{
1328    const tr_torrent * a = *(const tr_torrent**)va;
1329    const tr_torrent * b = *(const tr_torrent**)vb;
1330    const uint64_t     aCur = a->downloadedCur + a->uploadedCur;
1331    const uint64_t     bCur = b->downloadedCur + b->uploadedCur;
1332
1333    if( aCur != bCur )
1334        return aCur > bCur ? -1 : 1; /* close the biggest torrents first */
1335
1336    return 0;
1337}
1338
1339static void
1340tr_closeAllConnections( void * vsession )
1341{
1342    tr_session *  session = vsession;
1343    tr_torrent *  tor;
1344    int           i, n;
1345    tr_torrent ** torrents;
1346
1347    assert( tr_isSession( session ) );
1348
1349    evtimer_del( session->altTimer );
1350    tr_free( session->altTimer );
1351    session->altTimer = NULL;
1352
1353    tr_statsClose( session );
1354    tr_sharedShuttingDown( session->shared );
1355    tr_rpcClose( &session->rpcServer );
1356
1357    /* close the torrents.  get the most active ones first so that
1358     * if we can't get them all closed in a reasonable amount of time,
1359     * at least we get the most important ones first. */
1360    tor = NULL;
1361    n = session->torrentCount;
1362    torrents = tr_new( tr_torrent *, session->torrentCount );
1363    for( i = 0; i < n; ++i )
1364        torrents[i] = tor = tr_torrentNext( session, tor );
1365    qsort( torrents, n, sizeof( tr_torrent* ), compareTorrentByCur );
1366    for( i = 0; i < n; ++i )
1367        tr_torrentFree( torrents[i] );
1368    tr_free( torrents );
1369
1370    tr_peerMgrFree( session->peerMgr );
1371
1372    tr_trackerSessionClose( session );
1373    tr_list_free( &session->blocklists,
1374                  (TrListForeachFunc)_tr_blocklistFree );
1375    tr_webClose( &session->web );
1376
1377    session->isClosed = TRUE;
1378}
1379
1380static int
1381deadlineReached( const uint64_t deadline )
1382{
1383    return tr_date( ) >= deadline;
1384}
1385
1386#define SHUTDOWN_MAX_SECONDS 30
1387
1388void
1389tr_sessionClose( tr_session * session )
1390{
1391    int            i;
1392    const int      maxwait_msec = SHUTDOWN_MAX_SECONDS * 1000;
1393    const uint64_t deadline = tr_date( ) + maxwait_msec;
1394
1395    assert( tr_isSession( session ) );
1396
1397    dbgmsg( "shutting down transmission session %p", session );
1398
1399    /* close the session */
1400    tr_runInEventThread( session, tr_closeAllConnections, session );
1401    while( !session->isClosed && !deadlineReached( deadline ) )
1402    {
1403        dbgmsg(
1404            "waiting for the shutdown commands to run in the main thread" );
1405        tr_wait( 100 );
1406    }
1407
1408    /* "shared" and "tracker" have live sockets,
1409     * so we need to keep the transmission thread alive
1410     * for a bit while they tell the router & tracker
1411     * that we're closing now */
1412    while( ( session->shared
1413           || session->tracker ) && !deadlineReached( deadline ) )
1414    {
1415        dbgmsg( "waiting on port unmap (%p) or tracker (%p)",
1416                session->shared, session->tracker );
1417        tr_wait( 100 );
1418    }
1419
1420    tr_fdClose( );
1421
1422    /* close the libtransmission thread */
1423    tr_eventClose( session );
1424    while( session->events && !deadlineReached( deadline ) )
1425    {
1426        dbgmsg( "waiting for the libevent thread to shutdown cleanly" );
1427        tr_wait( 100 );
1428    }
1429
1430    /* free the session memory */
1431    tr_bencFree( &session->removedTorrents );
1432    tr_bandwidthFree( session->bandwidth );
1433    tr_lockFree( session->lock );
1434    for( i = 0; i < session->metainfoLookupCount; ++i )
1435        tr_free( session->metainfoLookup[i].filename );
1436    tr_free( session->metainfoLookup );
1437    tr_free( session->tag );
1438    tr_free( session->configDir );
1439    tr_free( session->resumeDir );
1440    tr_free( session->torrentDir );
1441    tr_free( session->downloadDir );
1442    tr_free( session->proxy );
1443    tr_free( session->proxyUsername );
1444    tr_free( session->proxyPassword );
1445    tr_free( session );
1446}
1447
1448tr_torrent **
1449tr_sessionLoadTorrents( tr_session * session,
1450                        tr_ctor    * ctor,
1451                        int        * setmeCount )
1452{
1453    int           i, n = 0;
1454    struct stat   sb;
1455    DIR *         odir = NULL;
1456    const char *  dirname = tr_getTorrentDir( session );
1457    tr_torrent ** torrents;
1458    tr_list *     l = NULL, *list = NULL;
1459
1460    assert( tr_isSession( session ) );
1461
1462    tr_ctorSetSave( ctor, FALSE ); /* since we already have them */
1463
1464    if( !stat( dirname, &sb )
1465      && S_ISDIR( sb.st_mode )
1466      && ( ( odir = opendir ( dirname ) ) ) )
1467    {
1468        struct dirent *d;
1469        for( d = readdir( odir ); d != NULL; d = readdir( odir ) )
1470        {
1471            if( d->d_name && d->d_name[0] != '.' ) /* skip dotfiles, ., and ..
1472                                                     */
1473            {
1474                tr_torrent * tor;
1475                char * path = tr_buildPath( dirname, d->d_name, NULL );
1476                tr_ctorSetMetainfoFromFile( ctor, path );
1477                if(( tor = tr_torrentNew( ctor, NULL )))
1478                {
1479                    tr_list_append( &list, tor );
1480                    ++n;
1481                }
1482                tr_free( path );
1483            }
1484        }
1485        closedir( odir );
1486    }
1487
1488    torrents = tr_new( tr_torrent *, n );
1489    for( i = 0, l = list; l != NULL; l = l->next )
1490        torrents[i++] = (tr_torrent*) l->data;
1491    assert( i == n );
1492
1493    tr_list_free( &list, NULL );
1494
1495    if( n )
1496        tr_inf( _( "Loaded %d torrents" ), n );
1497
1498    if( setmeCount )
1499        *setmeCount = n;
1500    return torrents;
1501}
1502
1503/***
1504****
1505***/
1506
1507void
1508tr_sessionSetPexEnabled( tr_session * session,
1509                         tr_bool      enabled )
1510{
1511    assert( tr_isSession( session ) );
1512
1513    session->isPexEnabled = enabled != 0;
1514}
1515
1516tr_bool
1517tr_sessionIsPexEnabled( const tr_session * session )
1518{
1519    assert( tr_isSession( session ) );
1520
1521    return session->isPexEnabled;
1522}
1523
1524/***
1525****
1526***/
1527
1528void
1529tr_sessionSetLazyBitfieldEnabled( tr_session * session,
1530                                  tr_bool      enabled )
1531{
1532    assert( tr_isSession( session ) );
1533
1534    session->useLazyBitfield = enabled != 0;
1535}
1536
1537tr_bool
1538tr_sessionIsLazyBitfieldEnabled( const tr_session * session )
1539{
1540    assert( tr_isSession( session ) );
1541
1542    return session->useLazyBitfield;
1543}
1544
1545/***
1546****
1547***/
1548
1549void
1550tr_sessionSetPortForwardingEnabled( tr_session  * session,
1551                                    tr_bool       enabled )
1552{
1553    assert( tr_isSession( session ) );
1554
1555    tr_globalLock( session );
1556    tr_sharedTraversalEnable( session->shared, enabled );
1557    tr_globalUnlock( session );
1558}
1559
1560tr_bool
1561tr_sessionIsPortForwardingEnabled( const tr_session * session )
1562{
1563    assert( tr_isSession( session ) );
1564
1565    return tr_sharedTraversalIsEnabled( session->shared );
1566}
1567
1568/***
1569****
1570***/
1571
1572int
1573tr_blocklistGetRuleCount( const tr_session * session )
1574{
1575    int       n = 0;
1576    tr_list * l;
1577
1578    assert( tr_isSession( session ) );
1579
1580    for( l = session->blocklists; l; l = l->next )
1581        n += _tr_blocklistGetRuleCount( l->data );
1582    return n;
1583}
1584
1585tr_bool
1586tr_blocklistIsEnabled( const tr_session * session )
1587{
1588    assert( tr_isSession( session ) );
1589
1590    return session->isBlocklistEnabled;
1591}
1592
1593void
1594tr_blocklistSetEnabled( tr_session * session,
1595                        tr_bool      isEnabled )
1596{
1597    tr_list * l;
1598
1599    assert( tr_isSession( session ) );
1600
1601    session->isBlocklistEnabled = isEnabled != 0;
1602
1603    for( l=session->blocklists; l!=NULL; l=l->next )
1604        _tr_blocklistSetEnabled( l->data, isEnabled );
1605}
1606
1607tr_bool
1608tr_blocklistExists( const tr_session * session )
1609{
1610    assert( tr_isSession( session ) );
1611
1612    return session->blocklists != NULL;
1613}
1614
1615int
1616tr_blocklistSetContent( tr_session * session,
1617                        const char * contentFilename )
1618{
1619    tr_list *      l;
1620    tr_blocklist * b;
1621    const char *   defaultName = "level1.bin";
1622
1623    assert( tr_isSession( session ) );
1624
1625    for( b = NULL, l = session->blocklists; !b && l; l = l->next )
1626        if( tr_stringEndsWith( _tr_blocklistGetFilename( l->data ),
1627                               defaultName ) )
1628            b = l->data;
1629
1630    if( !b )
1631    {
1632        char * path = tr_buildPath( session->configDir, "blocklists", defaultName, NULL );
1633        b = _tr_blocklistNew( path, session->isBlocklistEnabled );
1634        tr_list_append( &session->blocklists, b );
1635        tr_free( path );
1636    }
1637
1638    return _tr_blocklistSetContent( b, contentFilename );
1639}
1640
1641tr_bool
1642tr_sessionIsAddressBlocked( const tr_session * session,
1643                            const tr_address * addr )
1644{
1645    tr_list * l;
1646
1647    assert( tr_isSession( session ) );
1648
1649    for( l = session->blocklists; l; l = l->next )
1650        if( _tr_blocklistHasAddress( l->data, addr ) )
1651            return TRUE;
1652    return FALSE;
1653}
1654
1655/***
1656****
1657***/
1658
1659static int
1660compareLookupEntries( const void * va, const void * vb )
1661{
1662    const struct tr_metainfo_lookup * a = va;
1663    const struct tr_metainfo_lookup * b = vb;
1664
1665    return strcmp( a->hashString, b->hashString );
1666}
1667
1668static void
1669metainfoLookupResort( tr_session * session )
1670{
1671    assert( tr_isSession( session ) );
1672
1673    qsort( session->metainfoLookup,
1674           session->metainfoLookupCount,
1675           sizeof( struct tr_metainfo_lookup ),
1676           compareLookupEntries );
1677}
1678
1679static int
1680compareHashStringToLookupEntry( const void * va, const void * vb )
1681{
1682    const char *                      a = va;
1683    const struct tr_metainfo_lookup * b = vb;
1684
1685    return strcmp( a, b->hashString );
1686}
1687
1688const char*
1689tr_sessionFindTorrentFile( const tr_session * session,
1690                           const char       * hashStr )
1691{
1692    struct tr_metainfo_lookup * l = bsearch( hashStr,
1693                                             session->metainfoLookup,
1694                                             session->metainfoLookupCount,
1695                                             sizeof( struct tr_metainfo_lookup ),
1696                                             compareHashStringToLookupEntry );
1697
1698    return l ? l->filename : NULL;
1699}
1700
1701static void
1702metainfoLookupRescan( tr_session * session )
1703{
1704    int          i;
1705    int          n;
1706    struct stat  sb;
1707    const char * dirname = tr_getTorrentDir( session );
1708    DIR *        odir = NULL;
1709    tr_ctor *    ctor = NULL;
1710    tr_list *    list = NULL;
1711
1712    assert( tr_isSession( session ) );
1713
1714    /* walk through the directory and find the mappings */
1715    ctor = tr_ctorNew( session );
1716    tr_ctorSetSave( ctor, FALSE ); /* since we already have them */
1717    if( !stat( dirname, &sb ) && S_ISDIR( sb.st_mode ) && ( ( odir = opendir( dirname ) ) ) )
1718    {
1719        struct dirent *d;
1720        for( d = readdir( odir ); d != NULL; d = readdir( odir ) )
1721        {
1722            if( d->d_name && d->d_name[0] != '.' ) /* skip dotfiles, ., and ..
1723                                                     */
1724            {
1725                tr_info inf;
1726                char * path = tr_buildPath( dirname, d->d_name, NULL );
1727                tr_ctorSetMetainfoFromFile( ctor, path );
1728                if( !tr_torrentParse( ctor, &inf ) )
1729                {
1730                    tr_list_append( &list, tr_strdup( inf.hashString ) );
1731                    tr_list_append( &list, tr_strdup( path ) );
1732                    tr_metainfoFree( &inf );
1733                }
1734                tr_free( path );
1735            }
1736        }
1737        closedir( odir );
1738    }
1739    tr_ctorFree( ctor );
1740
1741    n = tr_list_size( list ) / 2;
1742    session->metainfoLookup = tr_new0( struct tr_metainfo_lookup, n );
1743    session->metainfoLookupCount = n;
1744    for( i = 0; i < n; ++i )
1745    {
1746        char * hashString = tr_list_pop_front( &list );
1747        char * filename = tr_list_pop_front( &list );
1748
1749        memcpy( session->metainfoLookup[i].hashString, hashString,
1750                2 * SHA_DIGEST_LENGTH + 1 );
1751        tr_free( hashString );
1752        session->metainfoLookup[i].filename = filename;
1753    }
1754
1755    metainfoLookupResort( session );
1756    tr_dbg( "Found %d torrents in \"%s\"", n, dirname );
1757}
1758
1759void
1760tr_sessionSetTorrentFile( tr_session * session,
1761                          const char * hashString,
1762                          const char * filename )
1763{
1764    struct tr_metainfo_lookup * l = bsearch( hashString,
1765                                             session->metainfoLookup,
1766                                             session->metainfoLookupCount,
1767                                             sizeof( struct tr_metainfo_lookup ),
1768                                             compareHashStringToLookupEntry );
1769
1770    if( l )
1771    {
1772        if( l->filename != filename )
1773        {
1774            tr_free( l->filename );
1775            l->filename = tr_strdup( filename );
1776        }
1777    }
1778    else
1779    {
1780        const int n = session->metainfoLookupCount++;
1781        struct tr_metainfo_lookup * node;
1782        session->metainfoLookup = tr_renew( struct tr_metainfo_lookup,
1783                                            session->metainfoLookup,
1784                                            session->metainfoLookupCount );
1785        node = session->metainfoLookup + n;
1786        memcpy( node->hashString, hashString, 2 * SHA_DIGEST_LENGTH + 1 );
1787        node->filename = tr_strdup( filename );
1788        metainfoLookupResort( session );
1789    }
1790}
1791
1792tr_torrent*
1793tr_torrentNext( tr_session * session,
1794                tr_torrent * tor )
1795{
1796    assert( tr_isSession( session ) );
1797
1798    return tor ? tor->next : session->torrentList;
1799}
1800
1801/***
1802****
1803***/
1804
1805void
1806tr_sessionSetRPCEnabled( tr_session * session,
1807                         tr_bool      isEnabled )
1808{
1809    assert( tr_isSession( session ) );
1810
1811    tr_rpcSetEnabled( session->rpcServer, isEnabled );
1812}
1813
1814tr_bool
1815tr_sessionIsRPCEnabled( const tr_session * session )
1816{
1817    assert( tr_isSession( session ) );
1818
1819    return tr_rpcIsEnabled( session->rpcServer );
1820}
1821
1822void
1823tr_sessionSetRPCPort( tr_session * session,
1824                      tr_port      port )
1825{
1826    assert( tr_isSession( session ) );
1827
1828    tr_rpcSetPort( session->rpcServer, port );
1829}
1830
1831tr_port
1832tr_sessionGetRPCPort( const tr_session * session )
1833{
1834    assert( tr_isSession( session ) );
1835
1836    return tr_rpcGetPort( session->rpcServer );
1837}
1838
1839void
1840tr_sessionSetRPCCallback( tr_session * session,
1841                          tr_rpc_func  func,
1842                          void *       user_data )
1843{
1844    assert( tr_isSession( session ) );
1845
1846    session->rpc_func = func;
1847    session->rpc_func_user_data = user_data;
1848}
1849
1850void
1851tr_sessionSetRPCWhitelist( tr_session * session,
1852                           const char * whitelist )
1853{
1854    assert( tr_isSession( session ) );
1855
1856    tr_rpcSetWhitelist( session->rpcServer, whitelist );
1857}
1858
1859char*
1860tr_sessionGetRPCWhitelist( const tr_session * session )
1861{
1862    assert( tr_isSession( session ) );
1863
1864    return tr_rpcGetWhitelist( session->rpcServer );
1865}
1866
1867void
1868tr_sessionSetRPCWhitelistEnabled( tr_session * session,
1869                                  tr_bool      isEnabled )
1870{
1871    assert( tr_isSession( session ) );
1872
1873    tr_rpcSetWhitelistEnabled( session->rpcServer, isEnabled );
1874}
1875
1876tr_bool
1877tr_sessionGetRPCWhitelistEnabled( const tr_session * session )
1878{
1879    assert( tr_isSession( session ) );
1880
1881    return tr_rpcGetWhitelistEnabled( session->rpcServer );
1882}
1883
1884
1885void
1886tr_sessionSetRPCPassword( tr_session * session,
1887                          const char * password )
1888{
1889    assert( tr_isSession( session ) );
1890
1891    tr_rpcSetPassword( session->rpcServer, password );
1892}
1893
1894char*
1895tr_sessionGetRPCPassword( const tr_session * session )
1896{
1897    assert( tr_isSession( session ) );
1898
1899    return tr_rpcGetPassword( session->rpcServer );
1900}
1901
1902void
1903tr_sessionSetRPCUsername( tr_session * session,
1904                          const char * username )
1905{
1906    assert( tr_isSession( session ) );
1907
1908    tr_rpcSetUsername( session->rpcServer, username );
1909}
1910
1911char*
1912tr_sessionGetRPCUsername( const tr_session * session )
1913{
1914    assert( tr_isSession( session ) );
1915
1916    return tr_rpcGetUsername( session->rpcServer );
1917}
1918
1919void
1920tr_sessionSetRPCPasswordEnabled( tr_session * session,
1921                                 tr_bool      isEnabled )
1922{
1923    assert( tr_isSession( session ) );
1924
1925    tr_rpcSetPasswordEnabled( session->rpcServer, isEnabled );
1926}
1927
1928tr_bool
1929tr_sessionIsRPCPasswordEnabled( const tr_session * session )
1930{
1931    assert( tr_isSession( session ) );
1932
1933    return tr_rpcIsPasswordEnabled( session->rpcServer );
1934}
1935
1936const char *
1937tr_sessionGetRPCBindAddress( const tr_session * session )
1938{
1939    assert( tr_isSession( session ) );
1940
1941    return tr_rpcGetBindAddress( session->rpcServer );
1942}
1943
1944/***
1945****
1946***/
1947
1948tr_bool
1949tr_sessionIsProxyEnabled( const tr_session * session )
1950{
1951    assert( tr_isSession( session ) );
1952
1953    return session->isProxyEnabled;
1954}
1955
1956void
1957tr_sessionSetProxyEnabled( tr_session * session,
1958                           tr_bool      isEnabled )
1959{
1960    assert( tr_isSession( session ) );
1961
1962    session->isProxyEnabled = isEnabled != 0;
1963}
1964
1965tr_proxy_type
1966tr_sessionGetProxyType( const tr_session * session )
1967{
1968    assert( tr_isSession( session ) );
1969
1970    return session->proxyType;
1971}
1972
1973void
1974tr_sessionSetProxyType( tr_session *  session,
1975                        tr_proxy_type type )
1976{
1977    assert( tr_isSession( session ) );
1978
1979    session->proxyType = type;
1980}
1981
1982const char*
1983tr_sessionGetProxy( const tr_session * session )
1984{
1985    assert( tr_isSession( session ) );
1986
1987    return session->proxy;
1988}
1989
1990tr_port
1991tr_sessionGetProxyPort( const tr_session * session )
1992{
1993    assert( tr_isSession( session ) );
1994
1995    return session->proxyPort;
1996}
1997
1998void
1999tr_sessionSetProxy( tr_session * session,
2000                    const char * proxy )
2001{
2002    assert( tr_isSession( session ) );
2003
2004    if( proxy != session->proxy )
2005    {
2006        tr_free( session->proxy );
2007        session->proxy = tr_strdup( proxy );
2008    }
2009}
2010
2011void
2012tr_sessionSetProxyPort( tr_session * session,
2013                        tr_port      port )
2014{
2015    assert( tr_isSession( session ) );
2016
2017    session->proxyPort = port;
2018}
2019
2020tr_bool
2021tr_sessionIsProxyAuthEnabled( const tr_session * session )
2022{
2023    assert( tr_isSession( session ) );
2024
2025    return session->isProxyAuthEnabled;
2026}
2027
2028void
2029tr_sessionSetProxyAuthEnabled( tr_session * session,
2030                               tr_bool      isEnabled )
2031{
2032    assert( tr_isSession( session ) );
2033
2034    session->isProxyAuthEnabled = isEnabled != 0;
2035}
2036
2037const char*
2038tr_sessionGetProxyUsername( const tr_session * session )
2039{
2040    assert( tr_isSession( session ) );
2041
2042    return session->proxyUsername;
2043}
2044
2045void
2046tr_sessionSetProxyUsername( tr_session * session,
2047                            const char * username )
2048{
2049    assert( tr_isSession( session ) );
2050
2051    if( username != session->proxyUsername )
2052    {
2053        tr_free( session->proxyUsername );
2054        session->proxyUsername = tr_strdup( username );
2055    }
2056}
2057
2058const char*
2059tr_sessionGetProxyPassword( const tr_session * session )
2060{
2061    assert( tr_isSession( session ) );
2062
2063    return session->proxyPassword;
2064}
2065
2066void
2067tr_sessionSetProxyPassword( tr_session * session,
2068                            const char * password )
2069{
2070    assert( tr_isSession( session ) );
2071
2072    if( password != session->proxyPassword )
2073    {
2074        tr_free( session->proxyPassword );
2075        session->proxyPassword = tr_strdup( password );
2076    }
2077}
2078
2079int
2080tr_sessionGetActiveTorrentCount( tr_session * session )
2081{
2082    int ret = 0;
2083    tr_torrent * tor = NULL;
2084
2085    assert( tr_isSession( session ) );
2086
2087    while(( tor = tr_torrentNext( session, tor )))
2088        if( tr_torrentGetActivity( tor ) != TR_STATUS_STOPPED )
2089            ++ret;
2090
2091    return ret;
2092}
2093
2094const tr_socketList *
2095tr_getSessionBindSockets( const tr_session * session )
2096{
2097    return tr_sharedGetBindSockets( session->shared );
2098}
Note: See TracBrowser for help on using the repository browser.