2006-07-16 19:39:23 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
2007-01-19 08:36:49 +00:00
|
|
|
* Copyright (c) 2005-2007 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-07-29 18:11:21 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#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-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-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) */
|
2007-07-16 11:19:44 +00:00
|
|
|
void
|
|
|
|
tr_peerIdNew ( char * buf, int buflen )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
assert( buflen == TR_ID_LEN + 1 );
|
2007-07-28 04:10:09 +00:00
|
|
|
|
|
|
|
snprintf( buf, TR_ID_LEN, "%s", PEERID_PREFIX );
|
2007-07-16 11:19:44 +00:00
|
|
|
assert( strlen(buf) == 8 );
|
|
|
|
for( i=8; i<TR_ID_LEN; ++i ) {
|
|
|
|
const int r = tr_rand( 36 );
|
|
|
|
buf[i] = ( r < 26 ) ? ( 'a' + r ) : ( '0' + r - 26 ) ;
|
|
|
|
}
|
|
|
|
buf[TR_ID_LEN] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
/***********************************************************************
|
|
|
|
* tr_init
|
|
|
|
***********************************************************************
|
|
|
|
* Allocates a tr_handle_t structure and initializes a few things
|
|
|
|
**********************************************************************/
|
2007-03-13 06:56:50 +00:00
|
|
|
tr_handle_t * tr_init( const char * tag )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
tr_handle_t * h;
|
2007-07-09 05:40:35 +00:00
|
|
|
int i;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2006-08-22 02:12:58 +00:00
|
|
|
tr_msgInit();
|
2007-08-02 19:43:29 +00:00
|
|
|
tr_netInit();
|
2006-07-16 19:39:23 +00:00
|
|
|
tr_netResolveThreadInit();
|
|
|
|
|
2007-06-09 15:36:46 +00:00
|
|
|
h = calloc( 1, sizeof( tr_handle_t ) );
|
2007-03-13 06:56:50 +00:00
|
|
|
if( NULL == h )
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
h->tag = strdup( tag );
|
|
|
|
if( NULL == h->tag )
|
|
|
|
{
|
|
|
|
free( h );
|
|
|
|
return NULL;
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
/* Random key */
|
2007-07-09 05:40:35 +00:00
|
|
|
for( i=0; i < TR_KEY_LEN; ++i )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-07-09 05:40:35 +00:00
|
|
|
const int r = tr_rand( 36 );
|
2006-07-16 19:39:23 +00:00
|
|
|
h->key[i] = ( r < 26 ) ? ( 'a' + r ) : ( '0' + r - 26 ) ;
|
|
|
|
}
|
|
|
|
|
2007-03-23 08:28:01 +00:00
|
|
|
/* Azureus identity */
|
2007-07-09 05:40:35 +00:00
|
|
|
for( i=0; i < TR_AZ_ID_LEN; ++i )
|
2007-03-23 08:28:01 +00:00
|
|
|
{
|
|
|
|
h->azId[i] = tr_rand( 0xff );
|
|
|
|
}
|
|
|
|
|
2007-08-02 19:43:29 +00:00
|
|
|
#ifndef WIN32
|
2006-07-16 19:39:23 +00:00
|
|
|
/* Don't exit when writing on a broken socket */
|
|
|
|
signal( SIGPIPE, SIG_IGN );
|
2007-08-02 19:43:29 +00:00
|
|
|
#endif
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
/* Initialize rate and file descripts controls */
|
2007-05-16 02:03:18 +00:00
|
|
|
h->upload = tr_rcInit();
|
|
|
|
h->download = tr_rcInit();
|
|
|
|
|
2007-01-21 07:16:18 +00:00
|
|
|
tr_fdInit();
|
2007-01-19 04:42:31 +00:00
|
|
|
h->shared = tr_sharedInit( h );
|
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
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* tr_setBindPort
|
|
|
|
***********************************************************************
|
|
|
|
*
|
|
|
|
**********************************************************************/
|
|
|
|
void tr_setBindPort( tr_handle_t * h, int port )
|
|
|
|
{
|
2007-01-27 21:17:10 +00:00
|
|
|
h->isPortSet = 1;
|
2007-01-19 04:42:31 +00:00
|
|
|
tr_sharedSetPort( h->shared, port );
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
2007-01-19 04:42:31 +00:00
|
|
|
void tr_natTraversalEnable( tr_handle_t * h, int enable )
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2007-02-06 05:37:48 +00:00
|
|
|
tr_sharedLock( h->shared );
|
2007-01-19 04:42:31 +00:00
|
|
|
tr_sharedTraversalEnable( h->shared, enable );
|
2007-02-06 05:37:48 +00:00
|
|
|
tr_sharedUnlock( h->shared );
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
2007-02-06 04:26:40 +00:00
|
|
|
tr_handle_status_t * tr_handleStatus( tr_handle_t * h )
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2007-02-06 04:26:40 +00:00
|
|
|
tr_handle_status_t * s;
|
|
|
|
|
|
|
|
h->statCur = ( h->statCur + 1 ) % 2;
|
|
|
|
s = &h->stats[h->statCur];
|
|
|
|
|
|
|
|
tr_sharedLock( h->shared );
|
|
|
|
|
|
|
|
s->natTraversalStatus = tr_sharedTraversalStatus( h->shared );
|
|
|
|
s->publicPort = tr_sharedGetPublicPort( h->shared );
|
|
|
|
|
|
|
|
tr_sharedUnlock( h->shared );
|
|
|
|
|
|
|
|
return s;
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
2007-07-20 08:33:59 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_setUseGlobalSpeedLimit( tr_handle_t * 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
|
|
|
|
tr_setGlobalSpeedLimit( tr_handle_t * 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 );
|
|
|
|
else {
|
|
|
|
tr_rcSetLimit( h->upload, KiB_sec );
|
|
|
|
tr_sharedSetLimit( h->shared, KiB_sec );
|
|
|
|
}
|
2007-01-08 21:53:55 +00:00
|
|
|
}
|
|
|
|
|
2007-07-20 08:33:59 +00:00
|
|
|
void
|
|
|
|
tr_getGlobalSpeedLimit( tr_handle_t * h,
|
|
|
|
int up_or_down,
|
|
|
|
int * setme_enabled,
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
void tr_torrentRates( tr_handle_t * h, float * dl, float * ul )
|
|
|
|
{
|
|
|
|
tr_torrent_t * tor;
|
|
|
|
|
|
|
|
*dl = 0.0;
|
|
|
|
*ul = 0.0;
|
2007-01-29 08:24:09 +00:00
|
|
|
tr_sharedLock( h->shared );
|
2006-07-16 19:39:23 +00:00
|
|
|
for( tor = h->torrentList; tor; tor = tor->next )
|
|
|
|
{
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_torrentReaderLock( tor );
|
|
|
|
if( tor->cpStatus == TR_CP_INCOMPLETE )
|
2006-07-16 19:39:23 +00:00
|
|
|
*dl += tr_rcRate( tor->download );
|
|
|
|
*ul += tr_rcRate( tor->upload );
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_torrentReaderUnlock( tor );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2007-01-29 08:24:09 +00:00
|
|
|
tr_sharedUnlock( h->shared );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int tr_torrentCount( tr_handle_t * h )
|
|
|
|
{
|
|
|
|
return h->torrentCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_torrentIterate( tr_handle_t * h, tr_callback_t func, void * d )
|
|
|
|
{
|
2006-08-09 22:53:27 +00:00
|
|
|
tr_torrent_t * tor, * next;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2006-08-09 22:53:27 +00:00
|
|
|
for( tor = h->torrentList; tor; tor = next )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2006-08-09 22:53:27 +00:00
|
|
|
next = tor->next;
|
2006-07-16 19:39:23 +00:00
|
|
|
func( tor, d );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_close( tr_handle_t * h )
|
|
|
|
{
|
2007-05-16 02:03:18 +00:00
|
|
|
tr_rcClose( h->upload );
|
|
|
|
tr_rcClose( h->download );
|
|
|
|
|
2007-01-19 04:42:31 +00:00
|
|
|
tr_sharedClose( h->shared );
|
2007-01-21 07:16:18 +00:00
|
|
|
tr_fdClose();
|
2007-06-06 00:30:13 +00:00
|
|
|
free( h->tag );
|
2006-07-16 19:39:23 +00:00
|
|
|
free( h );
|
|
|
|
|
|
|
|
tr_netResolveThreadClose();
|
|
|
|
}
|
2007-07-21 18:46:54 +00:00
|
|
|
|
|
|
|
tr_torrent_t **
|
|
|
|
tr_loadTorrents ( tr_handle_t * h,
|
|
|
|
const char * destination,
|
|
|
|
int flags,
|
|
|
|
int * setmeCount )
|
|
|
|
{
|
|
|
|
int i, n = 0;
|
|
|
|
struct stat sb;
|
|
|
|
DIR * odir = NULL;
|
|
|
|
const char * torrentDir = tr_getTorrentsDirectory( );
|
|
|
|
tr_torrent_t ** torrents;
|
|
|
|
tr_list_t *l=NULL, *list=NULL;
|
|
|
|
|
|
|
|
if( !stat( torrentDir, &sb )
|
|
|
|
&& S_ISDIR( sb.st_mode )
|
|
|
|
&& (( odir = opendir ( torrentDir ) )) )
|
|
|
|
{
|
|
|
|
struct dirent *d;
|
|
|
|
for (d = readdir( odir ); d!=NULL; d=readdir( odir ) )
|
|
|
|
{
|
|
|
|
if( d->d_name && d->d_name[0]!='.' ) /* skip dotfiles, ., and .. */
|
|
|
|
{
|
|
|
|
tr_torrent_t * tor;
|
|
|
|
char path[MAX_PATH_LENGTH];
|
|
|
|
tr_buildPath( path, sizeof(path), torrentDir, d->d_name, NULL );
|
|
|
|
tor = tr_torrentInit( h, path, destination, flags, NULL );
|
|
|
|
if( tor != NULL ) {
|
2007-08-14 14:18:54 +00:00
|
|
|
tr_list_append( &list, tor );
|
2007-07-21 18:46:54 +00:00
|
|
|
//fprintf (stderr, "#%d - %s\n", n, tor->info.name );
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir( odir );
|
|
|
|
}
|
|
|
|
|
|
|
|
torrents = tr_new( tr_torrent_t*, n );
|
|
|
|
for( i=0, l=list; l!=NULL; l=l->next )
|
|
|
|
torrents[i++] = (tr_torrent_t*) l->data;
|
|
|
|
assert( i==n );
|
|
|
|
|
2007-08-14 14:18:54 +00:00
|
|
|
tr_list_free( &list );
|
2007-07-21 18:46:54 +00:00
|
|
|
|
|
|
|
*setmeCount = n;
|
|
|
|
tr_inf( "Loaded %d torrents from disk", *setmeCount );
|
|
|
|
return torrents;
|
|
|
|
}
|