2006-07-16 19:39:23 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
2008-01-01 17:20:20 +00:00
|
|
|
* Copyright (c) 2005-2008 Transmission authors and contributors
|
2006-07-16 19:39:23 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2007-11-09 20:07:52 +00:00
|
|
|
#include <assert.h>
|
2007-07-29 18:11:21 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2007-07-12 17:51:45 +00:00
|
|
|
#include <signal.h>
|
2007-07-21 18:46:54 +00:00
|
|
|
#include <sys/types.h> /* stat */
|
|
|
|
#include <sys/stat.h> /* stat */
|
|
|
|
#include <unistd.h> /* stat */
|
|
|
|
#include <dirent.h> /* opendir */
|
2007-07-29 18:11:21 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#include "transmission.h"
|
2007-07-09 20:10:42 +00:00
|
|
|
#include "fdlimit.h"
|
2007-07-21 18:46:54 +00:00
|
|
|
#include "list.h"
|
2007-07-09 20:10:42 +00:00
|
|
|
#include "net.h"
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "peer-mgr.h"
|
2007-07-31 14:26:44 +00:00
|
|
|
#include "platform.h"
|
2007-07-30 18:04:10 +00:00
|
|
|
#include "ratecontrol.h"
|
2007-01-19 04:42:31 +00:00
|
|
|
#include "shared.h"
|
2007-11-21 20:03:53 +00:00
|
|
|
#include "stats.h"
|
2007-12-25 05:37:32 +00:00
|
|
|
#include "torrent.h"
|
2007-11-29 02:31:21 +00:00
|
|
|
#include "tracker.h"
|
2007-08-18 03:02:32 +00:00
|
|
|
#include "trevent.h"
|
2007-07-30 18:04:10 +00:00
|
|
|
#include "utils.h"
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-07-16 21:57:22 +00:00
|
|
|
/* Generate a peer id : "-TRxyzb-" + 12 random alphanumeric
|
|
|
|
characters, where x is the major version number, y is the
|
|
|
|
minor version number, z is the maintenance number, and b
|
|
|
|
designates beta (Azureus-style) */
|
2008-01-07 06:19:34 +00:00
|
|
|
uint8_t*
|
|
|
|
tr_peerIdNew( void )
|
2007-07-16 11:19:44 +00:00
|
|
|
{
|
|
|
|
int i;
|
2008-01-07 17:52:50 +00:00
|
|
|
int val;
|
2008-01-07 06:19:34 +00:00
|
|
|
int total = 0;
|
|
|
|
uint8_t * buf = tr_new( uint8_t, 21 );
|
|
|
|
const char * pool = "0123456789abcdefghijklmnopqrstuvwxyz";
|
2008-01-07 17:52:50 +00:00
|
|
|
const int base = 36;
|
2008-01-07 06:19:34 +00:00
|
|
|
|
|
|
|
memcpy( buf, PEERID_PREFIX, 8 );
|
|
|
|
|
|
|
|
for( i=8; i<19; ++i ) {
|
2008-01-07 17:52:50 +00:00
|
|
|
val = tr_rand( base );
|
2008-01-07 06:19:34 +00:00
|
|
|
total += val;
|
|
|
|
buf[i] = pool[val];
|
|
|
|
}
|
2007-07-28 04:10:09 +00:00
|
|
|
|
2008-01-07 17:52:50 +00:00
|
|
|
val = total % base ? base - (total % base) : 0;
|
|
|
|
total += val;
|
|
|
|
buf[19] = pool[val];
|
2008-01-07 06:19:34 +00:00
|
|
|
buf[20] = '\0';
|
2008-01-07 17:52:50 +00:00
|
|
|
|
2008-01-07 06:19:34 +00:00
|
|
|
return buf;
|
2007-07-16 11:19:44 +00:00
|
|
|
}
|
|
|
|
|
2008-01-07 06:19:34 +00:00
|
|
|
const uint8_t*
|
|
|
|
tr_getPeerId( void )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-01-07 06:19:34 +00:00
|
|
|
static uint8_t * id = NULL;
|
|
|
|
if( id == NULL )
|
|
|
|
id = tr_peerIdNew( );
|
|
|
|
return id;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
tr_encryption_mode
|
|
|
|
tr_getEncryptionMode( tr_handle * handle )
|
|
|
|
{
|
|
|
|
assert( handle != NULL );
|
|
|
|
|
|
|
|
return handle->encryptionMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_setEncryptionMode( tr_handle * handle, tr_encryption_mode mode )
|
|
|
|
{
|
|
|
|
assert( handle != NULL );
|
2007-11-09 01:25:34 +00:00
|
|
|
assert( mode==TR_ENCRYPTION_PREFERRED
|
|
|
|
|| mode==TR_ENCRYPTION_REQUIRED
|
|
|
|
|| mode==TR_PLAINTEXT_PREFERRED );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
handle->encryptionMode = mode;
|
|
|
|
}
|
|
|
|
|
2007-08-18 03:02:32 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2007-08-14 20:45:23 +00:00
|
|
|
|
2007-12-24 07:02:40 +00:00
|
|
|
tr_handle *
|
|
|
|
tr_initFull( const char * tag,
|
|
|
|
int isPexEnabled,
|
|
|
|
int isNatEnabled,
|
|
|
|
int publicPort,
|
|
|
|
int encryptionMode,
|
|
|
|
int isUploadLimitEnabled,
|
|
|
|
int uploadLimit,
|
|
|
|
int isDownloadLimitEnabled,
|
|
|
|
int downloadLimit,
|
2007-12-24 16:02:36 +00:00
|
|
|
int globalPeerLimit,
|
2007-12-24 16:12:23 +00:00
|
|
|
int messageLevel,
|
|
|
|
int isMessageQueueingEnabled )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_handle * h;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
#ifndef WIN32
|
|
|
|
/* Don't exit when writing on a broken socket */
|
|
|
|
signal( SIGPIPE, SIG_IGN );
|
|
|
|
#endif
|
|
|
|
|
2007-12-24 07:02:40 +00:00
|
|
|
tr_msgInit( );
|
2007-12-24 16:02:36 +00:00
|
|
|
tr_setMessageLevel( messageLevel );
|
2007-12-24 16:12:23 +00:00
|
|
|
tr_setMessageQueuing( isMessageQueueingEnabled );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-09-20 20:14:13 +00:00
|
|
|
h = tr_new0( tr_handle, 1 );
|
2007-10-01 15:17:15 +00:00
|
|
|
h->lock = tr_lockNew( );
|
2007-12-24 07:02:40 +00:00
|
|
|
h->isPexEnabled = isPexEnabled ? 1 : 0;
|
|
|
|
h->encryptionMode = encryptionMode;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2007-09-27 17:25:27 +00:00
|
|
|
tr_netInit(); /* must go before tr_eventInit */
|
|
|
|
|
2007-08-18 03:02:32 +00:00
|
|
|
tr_eventInit( h );
|
2007-09-20 16:32:01 +00:00
|
|
|
while( !h->events )
|
|
|
|
tr_wait( 50 );
|
|
|
|
|
2007-03-13 06:56:50 +00:00
|
|
|
h->tag = strdup( tag );
|
2007-08-16 20:00:06 +00:00
|
|
|
if( !h->tag ) {
|
2007-03-13 06:56:50 +00:00
|
|
|
free( h );
|
|
|
|
return NULL;
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
h->peerMgr = tr_peerMgrNew( h );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
/* Initialize rate and file descripts controls */
|
2007-12-24 07:02:40 +00:00
|
|
|
|
|
|
|
h->upload = tr_rcInit();
|
|
|
|
tr_rcSetLimit( h->upload, uploadLimit );
|
|
|
|
h->useUploadLimit = isUploadLimitEnabled;
|
|
|
|
|
2007-05-16 02:03:18 +00:00
|
|
|
h->download = tr_rcInit();
|
2007-12-24 07:02:40 +00:00
|
|
|
tr_rcSetLimit( h->download, downloadLimit );
|
|
|
|
h->useDownloadLimit = isDownloadLimitEnabled;
|
2007-05-16 02:03:18 +00:00
|
|
|
|
2007-12-24 07:02:40 +00:00
|
|
|
tr_fdInit( globalPeerLimit );
|
|
|
|
h->shared = tr_sharedInit( h, isNatEnabled, publicPort );
|
|
|
|
h->isPortSet = publicPort >= 0;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-07-10 19:40:21 +00:00
|
|
|
tr_inf( TR_NAME " " LONG_VERSION_STRING " started" );
|
2007-06-22 20:59:23 +00:00
|
|
|
|
2007-11-21 20:03:53 +00:00
|
|
|
tr_statsInit( h );
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
2007-12-24 07:02:40 +00:00
|
|
|
tr_handle * tr_init( const char * tag )
|
|
|
|
{
|
|
|
|
return tr_initFull( tag,
|
|
|
|
TRUE, /* pex enabled */
|
|
|
|
FALSE, /* nat enabled */
|
|
|
|
-1, /* public port */
|
|
|
|
TR_ENCRYPTION_PREFERRED, /* encryption mode */
|
|
|
|
FALSE, /* use upload speed limit? */
|
|
|
|
-1, /* upload speed limit */
|
|
|
|
FALSE, /* use download speed limit? */
|
|
|
|
-1, /* download speed limit */
|
2008-01-20 01:50:51 +00:00
|
|
|
200, /* globalPeerLimit */
|
2007-12-24 16:12:23 +00:00
|
|
|
TR_MSG_INF, /* message level */
|
|
|
|
FALSE ); /* is message queueing enabled? */
|
2007-12-24 07:02:40 +00:00
|
|
|
}
|
|
|
|
|
2007-10-01 15:17:15 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_globalLock( struct tr_handle * handle )
|
|
|
|
{
|
|
|
|
tr_lockLock( handle->lock );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_globalUnlock( struct tr_handle * handle )
|
|
|
|
{
|
|
|
|
tr_lockUnlock( handle->lock );
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
tr_globalIsLocked( const struct tr_handle * handle )
|
|
|
|
{
|
|
|
|
return tr_lockHave( handle->lock );
|
|
|
|
}
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
/***********************************************************************
|
|
|
|
* tr_setBindPort
|
|
|
|
***********************************************************************
|
|
|
|
*
|
|
|
|
**********************************************************************/
|
2007-11-22 06:13:57 +00:00
|
|
|
|
|
|
|
struct bind_port_data
|
|
|
|
{
|
|
|
|
tr_handle * handle;
|
|
|
|
int port;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
tr_setBindPortImpl( void * vdata )
|
|
|
|
{
|
|
|
|
struct bind_port_data * data = vdata;
|
|
|
|
tr_handle * handle = data->handle;
|
|
|
|
const int port = data->port;
|
|
|
|
|
|
|
|
handle->isPortSet = 1;
|
|
|
|
tr_sharedSetPort( handle->shared, port );
|
|
|
|
|
|
|
|
tr_free( data );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_setBindPort( tr_handle * handle, int port )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-11-22 06:13:57 +00:00
|
|
|
struct bind_port_data * data = tr_new( struct bind_port_data, 1 );
|
|
|
|
data->handle = handle;
|
|
|
|
data->port = port;
|
|
|
|
tr_runInEventThread( handle, tr_setBindPortImpl, data );
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
int
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_getPublicPort( const tr_handle * h )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
|
|
|
assert( h != NULL );
|
|
|
|
return tr_sharedGetPublicPort( h->shared );
|
|
|
|
}
|
|
|
|
|
2007-09-20 20:14:13 +00:00
|
|
|
void tr_natTraversalEnable( tr_handle * h, int enable )
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2007-10-01 15:17:15 +00:00
|
|
|
tr_globalLock( h );
|
2007-01-19 04:42:31 +00:00
|
|
|
tr_sharedTraversalEnable( h->shared, enable );
|
2007-10-01 15:17:15 +00:00
|
|
|
tr_globalUnlock( h );
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
2008-01-21 18:24:37 +00:00
|
|
|
const tr_handle_status *
|
|
|
|
tr_handleStatus( tr_handle * h )
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_handle_status * s;
|
2007-02-06 04:26:40 +00:00
|
|
|
|
|
|
|
h->statCur = ( h->statCur + 1 ) % 2;
|
|
|
|
s = &h->stats[h->statCur];
|
|
|
|
|
2007-10-01 15:17:15 +00:00
|
|
|
tr_globalLock( h );
|
2007-02-06 04:26:40 +00:00
|
|
|
|
|
|
|
s->natTraversalStatus = tr_sharedTraversalStatus( h->shared );
|
|
|
|
s->publicPort = tr_sharedGetPublicPort( h->shared );
|
|
|
|
|
2007-10-01 15:17:15 +00:00
|
|
|
tr_globalUnlock( h );
|
2007-02-06 04:26:40 +00:00
|
|
|
|
|
|
|
return s;
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
2007-07-20 08:33:59 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
void
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_setUseGlobalSpeedLimit( tr_handle * h,
|
|
|
|
int up_or_down,
|
|
|
|
int use_flag )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-07-20 08:33:59 +00:00
|
|
|
char * ch = up_or_down==TR_UP ? &h->useUploadLimit
|
|
|
|
: &h->useDownloadLimit;
|
|
|
|
*ch = use_flag;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-07-20 08:33:59 +00:00
|
|
|
void
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_setGlobalSpeedLimit( tr_handle * h,
|
|
|
|
int up_or_down,
|
|
|
|
int KiB_sec )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-07-20 08:33:59 +00:00
|
|
|
if( up_or_down == TR_DOWN )
|
|
|
|
tr_rcSetLimit( h->download, KiB_sec );
|
2007-09-20 16:32:01 +00:00
|
|
|
else
|
2007-07-20 08:33:59 +00:00
|
|
|
tr_rcSetLimit( h->upload, KiB_sec );
|
2007-01-08 21:53:55 +00:00
|
|
|
}
|
|
|
|
|
2007-07-20 08:33:59 +00:00
|
|
|
void
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_getGlobalSpeedLimit( tr_handle * h,
|
|
|
|
int up_or_down,
|
|
|
|
int * setme_enabled,
|
2007-07-20 08:33:59 +00:00
|
|
|
int * setme_KiBsec )
|
|
|
|
{
|
|
|
|
if( setme_enabled != NULL )
|
|
|
|
*setme_enabled = up_or_down==TR_UP ? h->useUploadLimit
|
|
|
|
: h->useDownloadLimit;
|
|
|
|
if( setme_KiBsec != NULL )
|
|
|
|
*setme_KiBsec = tr_rcGetLimit( up_or_down==TR_UP ? h->upload
|
|
|
|
: h->download );
|
|
|
|
}
|
|
|
|
|
2007-12-20 21:44:16 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
tr_setGlobalPeerLimit( tr_handle * handle UNUSED,
|
2008-01-21 02:11:57 +00:00
|
|
|
uint16_t maxGlobalPeers )
|
2007-12-20 21:44:16 +00:00
|
|
|
{
|
|
|
|
tr_fdSetPeerLimit( maxGlobalPeers );
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t
|
|
|
|
tr_getGlobalPeerLimit( const tr_handle * handle UNUSED )
|
|
|
|
{
|
|
|
|
return tr_fdGetPeerLimit( );
|
|
|
|
}
|
|
|
|
|
2007-09-20 23:33:46 +00:00
|
|
|
void
|
2007-10-21 15:47:26 +00:00
|
|
|
tr_torrentRates( tr_handle * h, float * toClient, float * toPeer )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-10-21 15:47:26 +00:00
|
|
|
const tr_torrent * tor;
|
2007-10-01 15:17:15 +00:00
|
|
|
tr_globalLock( h );
|
2007-10-19 23:23:21 +00:00
|
|
|
|
2007-10-21 15:47:26 +00:00
|
|
|
*toClient = *toPeer = 0.0;
|
2006-07-16 19:39:23 +00:00
|
|
|
for( tor = h->torrentList; tor; tor = tor->next )
|
|
|
|
{
|
2007-10-21 15:47:26 +00:00
|
|
|
float c, p;
|
|
|
|
tr_torrentGetRates( tor, &c, &p );
|
|
|
|
*toClient += c;
|
|
|
|
*toPeer += p;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2007-10-19 23:23:21 +00:00
|
|
|
|
2007-10-01 15:17:15 +00:00
|
|
|
tr_globalUnlock( h );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 23:33:46 +00:00
|
|
|
int
|
2008-01-15 22:40:58 +00:00
|
|
|
tr_torrentCount( const tr_handle * h )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
return h->torrentCount;
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
static void
|
|
|
|
tr_closeImpl( void * vh )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_handle * h = vh;
|
2007-11-10 22:29:28 +00:00
|
|
|
tr_torrent * t;
|
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
tr_sharedShuttingDown( h->shared );
|
2007-11-29 02:31:21 +00:00
|
|
|
tr_trackerShuttingDown( h );
|
|
|
|
|
2007-11-10 22:29:28 +00:00
|
|
|
for( t=h->torrentList; t!=NULL; t=t->next )
|
|
|
|
tr_torrentClose( t );
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_peerMgrFree( h->peerMgr );
|
|
|
|
|
2007-05-16 02:03:18 +00:00
|
|
|
tr_rcClose( h->upload );
|
|
|
|
tr_rcClose( h->download );
|
2007-11-09 15:06:32 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
h->isClosed = TRUE;
|
|
|
|
}
|
2007-11-05 05:42:25 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
deadlineReached( const uint64_t deadline )
|
|
|
|
{
|
|
|
|
return tr_date( ) >= deadline;
|
|
|
|
}
|
|
|
|
|
2007-11-29 00:43:58 +00:00
|
|
|
#define SHUTDOWN_MAX_SECONDS 30
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
void
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_close( tr_handle * h )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2007-11-29 00:43:58 +00:00
|
|
|
const int maxwait_msec = SHUTDOWN_MAX_SECONDS * 1000;
|
2007-11-05 05:42:25 +00:00
|
|
|
const uint64_t deadline = tr_date( ) + maxwait_msec;
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_runInEventThread( h, tr_closeImpl, h );
|
2007-11-05 05:42:25 +00:00
|
|
|
while( !h->isClosed && !deadlineReached( deadline ) )
|
2007-10-06 18:20:52 +00:00
|
|
|
tr_wait( 100 );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
tr_eventClose( h );
|
2007-11-05 05:42:25 +00:00
|
|
|
while( h->events && !deadlineReached( deadline ) )
|
2007-10-06 18:20:52 +00:00
|
|
|
tr_wait( 100 );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
tr_fdClose( );
|
2007-11-21 20:03:53 +00:00
|
|
|
tr_statsClose( h );
|
2007-10-01 15:17:15 +00:00
|
|
|
tr_lockFree( h->lock );
|
2007-06-06 00:30:13 +00:00
|
|
|
free( h->tag );
|
2006-07-16 19:39:23 +00:00
|
|
|
free( h );
|
|
|
|
}
|
2007-07-21 18:46:54 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrent **
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_loadTorrents ( tr_handle * h,
|
2007-12-21 22:18:40 +00:00
|
|
|
tr_ctor * ctor,
|
2007-09-20 20:14:13 +00:00
|
|
|
int * setmeCount )
|
2007-07-21 18:46:54 +00:00
|
|
|
{
|
|
|
|
int i, n = 0;
|
|
|
|
struct stat sb;
|
|
|
|
DIR * odir = NULL;
|
2007-12-21 22:18:40 +00:00
|
|
|
const char * dirname = tr_getTorrentsDirectory( );
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrent ** torrents;
|
|
|
|
tr_list *l=NULL, *list=NULL;
|
2007-07-21 18:46:54 +00:00
|
|
|
|
2007-12-22 17:30:31 +00:00
|
|
|
tr_ctorSetSave( ctor, FALSE ); /* since we already have them */
|
|
|
|
|
2007-12-21 22:18:40 +00:00
|
|
|
if( !stat( dirname, &sb )
|
2007-07-21 18:46:54 +00:00
|
|
|
&& S_ISDIR( sb.st_mode )
|
2007-12-21 22:18:40 +00:00
|
|
|
&& (( odir = opendir ( dirname ) )) )
|
2007-07-21 18:46:54 +00:00
|
|
|
{
|
|
|
|
struct dirent *d;
|
|
|
|
for (d = readdir( odir ); d!=NULL; d=readdir( odir ) )
|
|
|
|
{
|
|
|
|
if( d->d_name && d->d_name[0]!='.' ) /* skip dotfiles, ., and .. */
|
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrent * tor;
|
2007-12-21 22:18:40 +00:00
|
|
|
char filename[MAX_PATH_LENGTH];
|
|
|
|
tr_buildPath( filename, sizeof(filename), dirname, d->d_name, NULL );
|
|
|
|
tr_ctorSetMetainfoFromFile( ctor, filename );
|
|
|
|
tor = tr_torrentNew( h, ctor, NULL );
|
2007-07-21 18:46:54 +00:00
|
|
|
if( tor != NULL ) {
|
2007-08-14 14:18:54 +00:00
|
|
|
tr_list_append( &list, tor );
|
2007-07-21 18:46:54 +00:00
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir( odir );
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
torrents = tr_new( tr_torrent*, n );
|
2007-07-21 18:46:54 +00:00
|
|
|
for( i=0, l=list; l!=NULL; l=l->next )
|
2007-09-20 16:32:01 +00:00
|
|
|
torrents[i++] = (tr_torrent*) l->data;
|
2007-07-21 18:46:54 +00:00
|
|
|
assert( i==n );
|
|
|
|
|
2007-09-28 14:27:56 +00:00
|
|
|
tr_list_free( &list, NULL );
|
2007-07-21 18:46:54 +00:00
|
|
|
|
|
|
|
*setmeCount = n;
|
|
|
|
tr_inf( "Loaded %d torrents from disk", *setmeCount );
|
|
|
|
return torrents;
|
|
|
|
}
|
2007-12-24 05:03:40 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_setPexEnabled( tr_handle * handle, int isPexEnabled )
|
|
|
|
{
|
|
|
|
handle->isPexEnabled = isPexEnabled ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
tr_isPexEnabled( const tr_handle * handle )
|
|
|
|
{
|
|
|
|
return handle->isPexEnabled;
|
|
|
|
}
|