source: trunk/libtransmission/session.c @ 10129

Last change on this file since 10129 was 10129, checked in by charles, 12 years ago

(trunk libT) #2874 "Since 10094 The Schedule Speed Limit Not Working" -- fixed in trunk for 1.90

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