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.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#include "transmission.h"
|
2007-01-19 04:42:31 +00:00
|
|
|
#include "shared.h"
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* tr_init
|
|
|
|
***********************************************************************
|
|
|
|
* Allocates a tr_handle_t structure and initializes a few things
|
|
|
|
**********************************************************************/
|
|
|
|
tr_handle_t * tr_init()
|
|
|
|
{
|
|
|
|
tr_handle_t * h;
|
|
|
|
int i, r;
|
|
|
|
|
2006-08-22 02:12:58 +00:00
|
|
|
tr_msgInit();
|
2006-07-16 19:39:23 +00:00
|
|
|
tr_netResolveThreadInit();
|
|
|
|
|
|
|
|
h = calloc( sizeof( tr_handle_t ), 1 );
|
|
|
|
|
|
|
|
/* Generate a peer id : "-TRxxyy-" + 12 random alphanumeric
|
|
|
|
characters, where xx is the major version number and yy the
|
|
|
|
minor version number (Azureus-style) */
|
|
|
|
sprintf( h->id, "-TR%02d%02d-", VERSION_MAJOR, VERSION_MINOR );
|
|
|
|
for( i = 8; i < 20; i++ )
|
|
|
|
{
|
|
|
|
r = tr_rand( 36 );
|
|
|
|
h->id[i] = ( r < 26 ) ? ( 'a' + r ) : ( '0' + r - 26 ) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Random key */
|
|
|
|
for( i = 0; i < 20; i++ )
|
|
|
|
{
|
|
|
|
r = tr_rand( 36 );
|
|
|
|
h->key[i] = ( r < 26 ) ? ( 'a' + r ) : ( '0' + r - 26 ) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Don't exit when writing on a broken socket */
|
|
|
|
signal( SIGPIPE, SIG_IGN );
|
|
|
|
|
|
|
|
/* Initialize rate and file descripts controls */
|
2007-01-08 21:53:55 +00:00
|
|
|
h->uploadLimit = -1;
|
|
|
|
h->downloadLimit = -1;
|
|
|
|
|
2007-01-19 04:42:31 +00:00
|
|
|
h->fdlimit = tr_fdInit();
|
|
|
|
h->shared = tr_sharedInit( h );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* tr_setBindPort
|
|
|
|
***********************************************************************
|
|
|
|
*
|
|
|
|
**********************************************************************/
|
|
|
|
void tr_setBindPort( tr_handle_t * h, int port )
|
|
|
|
{
|
|
|
|
h->bindPort = port;
|
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-01-19 04:42:31 +00:00
|
|
|
tr_sharedTraversalEnable( h->shared, enable );
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int tr_natTraversalStatus( tr_handle_t * h )
|
|
|
|
{
|
2007-01-19 04:42:31 +00:00
|
|
|
return tr_sharedTraversalStatus( h->shared );
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
2006-12-27 00:22:38 +00:00
|
|
|
void tr_setGlobalUploadLimit( tr_handle_t * h, int limit )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-01-08 21:53:55 +00:00
|
|
|
h->uploadLimit = limit;
|
2007-01-19 04:42:31 +00:00
|
|
|
tr_sharedSetLimit( h->shared, limit );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2006-12-27 00:22:38 +00:00
|
|
|
void tr_setGlobalDownloadLimit( tr_handle_t * h, int limit )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-01-08 21:53:55 +00:00
|
|
|
h->downloadLimit = limit;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
for( tor = h->torrentList; tor; tor = tor->next )
|
|
|
|
{
|
|
|
|
tr_lockLock( &tor->lock );
|
|
|
|
if( tor->status & TR_STATUS_DOWNLOAD )
|
|
|
|
*dl += tr_rcRate( tor->download );
|
|
|
|
*ul += tr_rcRate( tor->upload );
|
|
|
|
tr_lockUnlock( &tor->lock );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-01-19 04:42:31 +00:00
|
|
|
tr_sharedClose( h->shared );
|
2006-07-16 19:39:23 +00:00
|
|
|
tr_fdClose( h->fdlimit );
|
|
|
|
free( h );
|
|
|
|
|
|
|
|
tr_netResolveThreadClose();
|
|
|
|
}
|
|
|
|
|