source: trunk/libtransmission/session.c @ 8233

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

(trunk) make it possible to #include "version.h" without having to add -I${TOP}/libtransmission/ to your CFLAGS

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