source: trunk/libtransmission/session.c @ 8132

Last change on this file since 8132 was 8132, checked in by charles, 13 years ago

(trunk)

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