2006-07-16 19:39:23 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (c) 2005-2006 Transmission authors and contributors
|
|
|
|
*
|
|
|
|
* 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 <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <signal.h>
|
2007-07-18 23:04:26 +00:00
|
|
|
|
|
|
|
#include <libtransmission/transmission.h>
|
2008-06-12 02:11:54 +00:00
|
|
|
#include <libtransmission/bencode.h>
|
2007-07-18 23:04:26 +00:00
|
|
|
#include <libtransmission/makemeta.h>
|
2008-01-16 16:47:58 +00:00
|
|
|
#include <libtransmission/metainfo.h> /* tr_metainfoFree */
|
2007-11-12 15:15:51 +00:00
|
|
|
#include <libtransmission/utils.h> /* tr_wait */
|
2008-06-12 02:11:54 +00:00
|
|
|
#include <libtransmission/web.h> /* tr_webRun */
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-01-16 16:47:58 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
/* macro to shut up "unused parameter" warnings */
|
|
|
|
#ifdef __GNUC__
|
|
|
|
#define UNUSED __attribute__((unused))
|
|
|
|
#else
|
|
|
|
#define UNUSED
|
|
|
|
#endif
|
|
|
|
|
|
|
|
const char * USAGE =
|
2008-04-19 00:41:32 +00:00
|
|
|
"Usage: %s [-car[-m]] [-dfginpsuv] [-h] file.torrent [output-dir]\n\n"
|
2007-06-18 03:40:41 +00:00
|
|
|
"Options:\n"
|
2008-04-19 00:41:32 +00:00
|
|
|
" -h, --help Print this help and exit\n"
|
|
|
|
" -i, --info Print metainfo and exit\n"
|
|
|
|
" -s, --scrape Print counts of seeders/leechers and exit\n"
|
|
|
|
" -V, --version Print the version number and exit\n"
|
2007-06-18 03:40:41 +00:00
|
|
|
" -c, --create-from <file> Create torrent from the specified source file.\n"
|
2008-04-19 00:41:32 +00:00
|
|
|
" -a, --announce <url> Used in conjunction with -c.\n"
|
|
|
|
" -g, --config-dir <path> Where to look for configuration files\n"
|
2008-05-29 17:33:07 +00:00
|
|
|
" -o, --output-dir <path> Where to save downloaded data\n"
|
2008-04-19 00:41:32 +00:00
|
|
|
" -r, --private Used in conjunction with -c.\n"
|
|
|
|
" -m, --comment <text> Adds an optional comment when creating a torrent.\n"
|
|
|
|
" -d, --download <int> Max download rate (-1 = no limit, default = -1)\n"
|
|
|
|
" -f, --finish <script> Command you wish to run on completion\n"
|
|
|
|
" -n --nat-traversal Attempt NAT traversal using NAT-PMP or UPnP IGD\n"
|
|
|
|
" -p, --port <int> Port we should listen on (default = %d)\n"
|
2008-06-08 23:59:19 +00:00
|
|
|
" -t, --tos <int> Peer socket TOS (0 to 255, default = 8)\n"
|
2008-04-19 00:41:32 +00:00
|
|
|
" -u, --upload <int> Maximum upload rate (-1 = no limit, default = 20)\n"
|
|
|
|
" -v, --verbose <int> Verbose level (0 to 2, default = 0)\n"
|
|
|
|
" -y, --recheck Force a recheck of the torrent data\n";
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
static int showHelp = 0;
|
|
|
|
static int showInfo = 0;
|
|
|
|
static int showScrape = 0;
|
2008-01-01 18:14:05 +00:00
|
|
|
static int showVersion = 0;
|
2007-06-18 03:40:41 +00:00
|
|
|
static int isPrivate = 0;
|
2006-07-16 19:39:23 +00:00
|
|
|
static int verboseLevel = 0;
|
2008-05-18 16:44:30 +00:00
|
|
|
static int peerPort = TR_DEFAULT_PORT;
|
2008-06-04 20:04:19 +00:00
|
|
|
static int peerSocketTOS = TR_DEFAULT_PEER_SOCKET_TOS;
|
2006-07-16 19:39:23 +00:00
|
|
|
static int uploadLimit = 20;
|
|
|
|
static int downloadLimit = -1;
|
2007-11-26 16:22:36 +00:00
|
|
|
static char * torrentPath = NULL;
|
2006-09-25 18:37:45 +00:00
|
|
|
static int natTraversal = 0;
|
2007-11-26 16:39:11 +00:00
|
|
|
static int recheckData = 0;
|
2007-02-13 05:20:52 +00:00
|
|
|
static sig_atomic_t gotsig = 0;
|
2007-10-19 21:53:35 +00:00
|
|
|
static sig_atomic_t manualUpdate = 0;
|
2008-05-18 16:44:30 +00:00
|
|
|
static char downloadDir[MAX_PATH_LENGTH] = { '\0' };
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
static char * finishCall = NULL;
|
2007-06-18 03:40:41 +00:00
|
|
|
static char * announce = NULL;
|
2008-04-19 00:41:32 +00:00
|
|
|
static char * configdir = NULL;
|
2007-06-18 03:40:41 +00:00
|
|
|
static char * sourceFile = NULL;
|
|
|
|
static char * comment = NULL;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
static int parseCommandLine ( int argc, char ** argv );
|
|
|
|
static void sigHandler ( int signal );
|
|
|
|
|
2008-01-16 16:47:58 +00:00
|
|
|
static char *
|
|
|
|
getStringRatio( float ratio )
|
2007-02-15 20:56:25 +00:00
|
|
|
{
|
|
|
|
static char string[20];
|
|
|
|
|
|
|
|
if( ratio == TR_RATIO_NA )
|
|
|
|
return "n/a";
|
|
|
|
snprintf( string, sizeof string, "%.3f", ratio );
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2008-06-12 02:11:54 +00:00
|
|
|
static int
|
|
|
|
is_rfc2396_alnum( char ch )
|
|
|
|
{
|
|
|
|
return ( '0' <= ch && ch <= '9' )
|
|
|
|
|| ( 'A' <= ch && ch <= 'Z' )
|
|
|
|
|| ( 'a' <= ch && ch <= 'z' );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
escape( char * out, const uint8_t * in, int in_len ) /* rfc2396 */
|
|
|
|
{
|
|
|
|
const uint8_t *end = in + in_len;
|
|
|
|
while( in != end )
|
|
|
|
if( is_rfc2396_alnum(*in) )
|
|
|
|
*out++ = (char) *in++;
|
|
|
|
else
|
|
|
|
out += snprintf( out, 4, "%%%02X", (unsigned int)*in++ );
|
|
|
|
*out = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-02-15 20:56:25 +00:00
|
|
|
#define LINEWIDTH 80
|
|
|
|
|
2007-09-28 15:12:21 +00:00
|
|
|
static void
|
|
|
|
torrentStateChanged( tr_torrent * torrent UNUSED,
|
|
|
|
cp_status_t status UNUSED,
|
|
|
|
void * user_data UNUSED )
|
|
|
|
{
|
|
|
|
system( finishCall );
|
|
|
|
}
|
|
|
|
|
2008-06-12 02:11:54 +00:00
|
|
|
static int leftToScrape = 0;
|
|
|
|
|
|
|
|
static void
|
|
|
|
scrapeDoneFunc( struct tr_handle * session UNUSED,
|
|
|
|
long response_code,
|
|
|
|
const void * response,
|
|
|
|
size_t response_byte_count,
|
|
|
|
void * host )
|
|
|
|
{
|
|
|
|
tr_benc top, *files;
|
|
|
|
|
|
|
|
if( !tr_bencLoad( response, response_byte_count, &top, NULL )
|
|
|
|
&& tr_bencDictFindDict( &top, "files", &files )
|
|
|
|
&& files->val.l.count >= 2 )
|
|
|
|
{
|
|
|
|
int64_t complete=-1, incomplete=-1, downloaded=-1;
|
|
|
|
tr_benc * hash = &files->val.l.vals[1];
|
|
|
|
tr_bencDictFindInt( hash, "complete", &complete );
|
|
|
|
tr_bencDictFindInt( hash, "incomplete", &incomplete );
|
|
|
|
tr_bencDictFindInt( hash, "downloaded", &downloaded );
|
|
|
|
printf( "%4d seeders, %4d leechers, %5d downloads at %s\n",
|
|
|
|
(int)complete, (int)incomplete, (int)downloaded, (char*)host );
|
|
|
|
tr_bencFree( &top );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
printf( "unable to parse response (http code %lu) at %s", response_code, (char*)host );
|
|
|
|
|
|
|
|
--leftToScrape;
|
|
|
|
}
|
|
|
|
|
2008-01-16 16:47:58 +00:00
|
|
|
int
|
|
|
|
main( int argc, char ** argv )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-08-05 03:18:23 +00:00
|
|
|
int i, error;
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_handle * h;
|
2007-12-22 03:51:12 +00:00
|
|
|
tr_ctor * ctor;
|
2008-01-16 16:47:58 +00:00
|
|
|
tr_torrent * tor = NULL;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-01-01 18:14:05 +00:00
|
|
|
printf( "Transmission %s - http://www.transmissionbt.com/\n",
|
2007-07-10 19:40:21 +00:00
|
|
|
LONG_VERSION_STRING );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
/* Get options */
|
|
|
|
if( parseCommandLine( argc, argv ) )
|
|
|
|
{
|
|
|
|
printf( USAGE, argv[0], TR_DEFAULT_PORT );
|
2007-02-06 22:47:55 +00:00
|
|
|
return EXIT_FAILURE;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2008-01-01 18:14:05 +00:00
|
|
|
if( showVersion )
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
if( showHelp )
|
|
|
|
{
|
|
|
|
printf( USAGE, argv[0], TR_DEFAULT_PORT );
|
2007-02-06 22:47:55 +00:00
|
|
|
return EXIT_SUCCESS;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
if( peerPort < 1 || peerPort > 65535 )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
printf( "Invalid port '%d'\n", peerPort );
|
2007-02-06 22:47:55 +00:00
|
|
|
return EXIT_FAILURE;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2008-06-04 20:04:19 +00:00
|
|
|
if( peerSocketTOS < 0 || peerSocketTOS > 255 )
|
|
|
|
{
|
|
|
|
printf( "Invalid TOS '%d'\n", peerSocketTOS );
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2008-04-06 16:50:41 +00:00
|
|
|
/* don't bind the port if we're just running the CLI
|
|
|
|
* to get metainfo or to create a torrent */
|
2008-04-06 16:53:26 +00:00
|
|
|
if( showInfo || showScrape || ( sourceFile != NULL ) )
|
2008-05-18 16:44:30 +00:00
|
|
|
peerPort = -1;
|
2008-04-06 16:50:41 +00:00
|
|
|
|
2008-04-19 00:41:32 +00:00
|
|
|
if( configdir == NULL )
|
|
|
|
configdir = strdup( tr_getDefaultConfigDir( ) );
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
/* Initialize libtransmission */
|
2008-05-18 16:44:30 +00:00
|
|
|
h = tr_sessionInitFull(
|
|
|
|
configdir,
|
|
|
|
"cli", /* tag */
|
|
|
|
downloadDir, /* where to download torrents */
|
|
|
|
TR_DEFAULT_PEX_ENABLED,
|
|
|
|
natTraversal, /* nat enabled */
|
|
|
|
peerPort,
|
|
|
|
TR_ENCRYPTION_PREFERRED,
|
|
|
|
uploadLimit >= 0,
|
|
|
|
uploadLimit,
|
|
|
|
downloadLimit >= 0,
|
|
|
|
downloadLimit,
|
|
|
|
TR_DEFAULT_GLOBAL_PEER_LIMIT,
|
|
|
|
verboseLevel + 1, /* messageLevel */
|
|
|
|
0, /* is message queueing enabled? */
|
|
|
|
TR_DEFAULT_BLOCKLIST_ENABLED,
|
2008-06-04 20:04:19 +00:00
|
|
|
peerSocketTOS,
|
2008-05-18 16:44:30 +00:00
|
|
|
TR_DEFAULT_RPC_ENABLED,
|
|
|
|
TR_DEFAULT_RPC_PORT,
|
2008-06-05 20:26:46 +00:00
|
|
|
TR_DEFAULT_RPC_ACL,
|
2008-06-10 16:16:31 +00:00
|
|
|
FALSE, "fnord", "potzrebie",
|
|
|
|
TR_DEFAULT_PROXY_ENABLED,
|
|
|
|
TR_DEFAULT_PROXY,
|
2008-06-11 20:45:53 +00:00
|
|
|
TR_DEFAULT_PROXY_TYPE,
|
2008-06-10 16:16:31 +00:00
|
|
|
TR_DEFAULT_PROXY_AUTH_ENABLED,
|
|
|
|
TR_DEFAULT_PROXY_USERNAME,
|
|
|
|
TR_DEFAULT_PROXY_PASSWORD );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
if( sourceFile && *sourceFile ) /* creating a torrent */
|
|
|
|
{
|
2008-03-24 17:33:08 +00:00
|
|
|
int err;
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_metainfo_builder * builder = tr_metaInfoBuilderCreate( h, sourceFile );
|
2008-06-01 05:36:23 +00:00
|
|
|
tr_tracker_info ti;
|
|
|
|
ti.tier = 0;
|
|
|
|
ti.announce = announce;
|
|
|
|
tr_makeMetaInfo( builder, torrentPath, &ti, 1, comment, isPrivate );
|
2007-06-18 03:40:41 +00:00
|
|
|
while( !builder->isDone ) {
|
2007-11-12 15:15:51 +00:00
|
|
|
tr_wait( 1000 );
|
2007-06-18 03:40:41 +00:00
|
|
|
printf( "." );
|
|
|
|
}
|
2008-03-24 17:33:08 +00:00
|
|
|
err = builder->result;
|
2007-06-18 03:40:41 +00:00
|
|
|
tr_metaInfoBuilderFree( builder );
|
2008-03-24 17:33:08 +00:00
|
|
|
return err;
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2007-12-22 03:51:12 +00:00
|
|
|
ctor = tr_ctorNew( h );
|
|
|
|
tr_ctorSetMetainfoFromFile( ctor, torrentPath );
|
2008-06-12 02:11:54 +00:00
|
|
|
tr_ctorSetPaused( ctor, TR_FORCE, showScrape );
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_ctorSetDownloadDir( ctor, TR_FORCE, downloadDir );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-06-12 02:11:54 +00:00
|
|
|
if( showScrape )
|
|
|
|
{
|
|
|
|
tr_info info;
|
|
|
|
|
|
|
|
if( !tr_torrentParse( h, ctor, &info ) )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
const time_t start = time( NULL );
|
|
|
|
for( i=0; i<info.trackerCount; ++i )
|
|
|
|
{
|
|
|
|
if( info.trackers[i].scrape )
|
|
|
|
{
|
|
|
|
const char * scrape = info.trackers[i].scrape;
|
|
|
|
char escaped[SHA_DIGEST_LENGTH*3 + 1];
|
|
|
|
char *url, *host;
|
|
|
|
escape( escaped, info.hash, SHA_DIGEST_LENGTH );
|
|
|
|
url = tr_strdup_printf( "%s%cinfo_hash=%s",
|
|
|
|
scrape,
|
|
|
|
strchr(scrape,'?')?'&':'?',
|
|
|
|
escaped );
|
|
|
|
tr_httpParseURL( scrape, -1, &host, NULL, NULL );
|
|
|
|
++leftToScrape;
|
|
|
|
tr_webRun( h, url, NULL, scrapeDoneFunc, host );
|
|
|
|
tr_free( url );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf( stderr, "scraping %d trackers:\n", leftToScrape );
|
|
|
|
|
|
|
|
while( leftToScrape>0 && ((time(NULL)-start)<20) )
|
|
|
|
tr_wait( 250 );
|
|
|
|
}
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
if( showInfo )
|
|
|
|
{
|
2008-01-16 16:47:58 +00:00
|
|
|
tr_info info;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-01-16 16:47:58 +00:00
|
|
|
if( !tr_torrentParse( h, ctor, &info ) )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2008-04-24 02:00:38 +00:00
|
|
|
int prevTier = -1;
|
2008-03-22 18:10:59 +00:00
|
|
|
tr_file_index_t ff;
|
|
|
|
|
2008-01-16 16:47:58 +00:00
|
|
|
printf( "hash:\t" );
|
|
|
|
for( i=0; i<SHA_DIGEST_LENGTH; ++i )
|
|
|
|
printf( "%02x", info.hash[i] );
|
|
|
|
printf( "\n" );
|
|
|
|
|
|
|
|
printf( "name:\t%s\n", info.name );
|
|
|
|
|
2008-04-24 02:00:38 +00:00
|
|
|
for( i=0; i<info.trackerCount; ++i ) {
|
|
|
|
if( prevTier != info.trackers[i].tier ) {
|
|
|
|
prevTier = info.trackers[i].tier;
|
|
|
|
printf( "\ntracker tier #%d:\n", (prevTier+1) );
|
2008-01-16 16:47:58 +00:00
|
|
|
}
|
2008-04-24 02:00:38 +00:00
|
|
|
printf( "\tannounce:\t%s\n", info.trackers[i].announce );
|
2008-01-16 16:47:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
printf( "size:\t%"PRIu64" (%"PRIu64" * %d + %"PRIu64")\n",
|
|
|
|
info.totalSize, info.totalSize / info.pieceSize,
|
|
|
|
info.pieceSize, info.totalSize % info.pieceSize );
|
|
|
|
|
|
|
|
if( info.comment[0] )
|
|
|
|
printf( "comment:\t%s\n", info.comment );
|
|
|
|
if( info.creator[0] )
|
|
|
|
printf( "creator:\t%s\n", info.creator );
|
|
|
|
if( info.isPrivate )
|
|
|
|
printf( "private flag set\n" );
|
|
|
|
|
|
|
|
printf( "file(s):\n" );
|
2008-03-22 18:10:59 +00:00
|
|
|
for( ff=0; ff<info.fileCount; ++ff )
|
|
|
|
printf( "\t%s (%"PRIu64")\n", info.files[ff].name, info.files[ff].length );
|
2008-01-16 16:47:58 +00:00
|
|
|
|
|
|
|
tr_metainfoFree( &info );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2008-01-16 16:47:58 +00:00
|
|
|
tr_ctorFree( ctor );
|
2006-07-16 19:39:23 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2008-01-16 16:47:58 +00:00
|
|
|
|
|
|
|
tor = tr_torrentNew( h, ctor, &error );
|
|
|
|
tr_ctorFree( ctor );
|
|
|
|
if( tor == NULL )
|
|
|
|
{
|
|
|
|
printf( "Failed opening torrent file `%s'\n", torrentPath );
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_sessionClose( h );
|
2008-01-16 16:47:58 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
signal( SIGINT, sigHandler );
|
2007-10-19 21:53:35 +00:00
|
|
|
signal( SIGHUP, sigHandler );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-09-28 15:12:21 +00:00
|
|
|
tr_torrentSetStatusCallback( tor, torrentStateChanged, NULL );
|
2006-07-16 19:39:23 +00:00
|
|
|
tr_torrentStart( tor );
|
|
|
|
|
2007-01-14 12:00:21 +00:00
|
|
|
for( ;; )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-02-15 20:56:25 +00:00
|
|
|
char string[LINEWIDTH];
|
2006-07-16 19:39:23 +00:00
|
|
|
int chars = 0;
|
2008-05-06 16:32:45 +00:00
|
|
|
const struct tr_stat * s;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-11-12 15:15:51 +00:00
|
|
|
tr_wait( 1000 );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-02-13 05:20:52 +00:00
|
|
|
if( gotsig )
|
|
|
|
{
|
|
|
|
gotsig = 0;
|
|
|
|
tr_torrentStop( tor );
|
2008-05-12 16:33:17 +00:00
|
|
|
tr_sessionSetPortForwardingEnabled( h, 0 );
|
2007-02-13 05:20:52 +00:00
|
|
|
}
|
2007-10-19 21:53:35 +00:00
|
|
|
|
|
|
|
if( manualUpdate )
|
|
|
|
{
|
|
|
|
manualUpdate = 0;
|
2007-10-19 23:21:32 +00:00
|
|
|
if ( !tr_torrentCanManualUpdate( tor ) )
|
2007-10-19 21:53:35 +00:00
|
|
|
fprintf( stderr, "\rReceived SIGHUP, but can't send a manual update now\n" );
|
|
|
|
else {
|
|
|
|
fprintf( stderr, "\rReceived SIGHUP: manual update scheduled\n" );
|
2008-05-22 20:44:41 +00:00
|
|
|
tr_torrentManualUpdate( tor );
|
2007-10-19 21:53:35 +00:00
|
|
|
}
|
|
|
|
}
|
2007-11-26 16:39:11 +00:00
|
|
|
|
|
|
|
if( recheckData )
|
|
|
|
{
|
|
|
|
recheckData = 0;
|
2008-02-27 17:38:39 +00:00
|
|
|
tr_torrentVerify( tor );
|
2007-11-26 16:39:11 +00:00
|
|
|
}
|
2007-02-13 05:20:52 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
s = tr_torrentStat( tor );
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
if( s->status & TR_STATUS_CHECK_WAIT )
|
2007-05-28 18:37:47 +00:00
|
|
|
{
|
|
|
|
chars = snprintf( string, sizeof string,
|
2007-11-12 15:15:51 +00:00
|
|
|
"Waiting to verify local files..." );
|
2007-05-28 18:37:47 +00:00
|
|
|
}
|
2007-01-14 12:00:21 +00:00
|
|
|
else if( s->status & TR_STATUS_CHECK )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-02-15 20:56:25 +00:00
|
|
|
chars = snprintf( string, sizeof string,
|
2007-11-12 15:15:51 +00:00
|
|
|
"Verifying local files... %.2f%%, found %.2f%% valid", 100 * s->recheckProgress, 100.0 * s->percentDone );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
else if( s->status & TR_STATUS_DOWNLOAD )
|
|
|
|
{
|
2007-02-15 20:56:25 +00:00
|
|
|
chars = snprintf( string, sizeof string,
|
2006-07-16 19:39:23 +00:00
|
|
|
"Progress: %.2f %%, %d peer%s, dl from %d (%.2f KB/s), "
|
2007-06-18 03:40:41 +00:00
|
|
|
"ul to %d (%.2f KB/s) [%s]", 100.0 * s->percentDone,
|
2007-07-15 17:19:07 +00:00
|
|
|
s->peersConnected, ( s->peersConnected == 1 ) ? "" : "s",
|
2007-07-16 00:50:43 +00:00
|
|
|
s->peersSendingToUs, s->rateDownload,
|
|
|
|
s->peersGettingFromUs, s->rateUpload,
|
2007-02-15 20:56:25 +00:00
|
|
|
getStringRatio(s->ratio) );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
else if( s->status & TR_STATUS_SEED )
|
|
|
|
{
|
2007-02-15 20:56:25 +00:00
|
|
|
chars = snprintf( string, sizeof string,
|
|
|
|
"Seeding, uploading to %d of %d peer(s), %.2f KB/s [%s]",
|
2007-10-01 15:51:54 +00:00
|
|
|
s->peersGettingFromUs, s->peersConnected,
|
2007-02-15 20:56:25 +00:00
|
|
|
s->rateUpload, getStringRatio(s->ratio) );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2007-10-20 22:07:21 +00:00
|
|
|
else if( s->status & TR_STATUS_STOPPED )
|
2007-07-18 17:31:00 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2007-02-15 20:56:25 +00:00
|
|
|
if( ( signed )sizeof string > chars )
|
2007-02-13 04:21:29 +00:00
|
|
|
{
|
2007-02-15 20:56:25 +00:00
|
|
|
memset( &string[chars], ' ', sizeof string - 1 - chars );
|
2007-02-13 04:21:29 +00:00
|
|
|
}
|
2007-02-15 20:56:25 +00:00
|
|
|
string[sizeof string - 1] = '\0';
|
2006-10-05 18:37:38 +00:00
|
|
|
fprintf( stderr, "\r%s", string );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-01-14 12:00:21 +00:00
|
|
|
if( s->error )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-01-14 12:00:21 +00:00
|
|
|
fprintf( stderr, "\n%s\n", s->errorString );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
else if( verboseLevel > 0 )
|
|
|
|
{
|
|
|
|
fprintf( stderr, "\n" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fprintf( stderr, "\n" );
|
|
|
|
|
2008-05-22 12:31:47 +00:00
|
|
|
/* try for 5 seconds to delete any port mappings for nat traversal */
|
2008-05-12 16:33:17 +00:00
|
|
|
tr_sessionSetPortForwardingEnabled( h, 0 );
|
2008-05-22 12:31:47 +00:00
|
|
|
for( i=0; i<10; ++i ) {
|
|
|
|
const tr_port_forwarding f = tr_sessionGetPortForwarding( h );
|
|
|
|
if( f == TR_PORT_UNMAPPED )
|
2006-07-16 19:39:23 +00:00
|
|
|
break;
|
2007-11-12 15:15:51 +00:00
|
|
|
tr_wait( 500 );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_sessionClose( h );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-02-06 22:47:55 +00:00
|
|
|
return EXIT_SUCCESS;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2008-01-01 18:14:05 +00:00
|
|
|
static int
|
|
|
|
parseCommandLine( int argc, char ** argv )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
for( ;; )
|
|
|
|
{
|
2008-04-19 00:41:32 +00:00
|
|
|
static const struct option long_options[] = {
|
|
|
|
{ "announce", required_argument, NULL, 'a' },
|
|
|
|
{ "create-from", required_argument, NULL, 'c' },
|
|
|
|
{ "download", required_argument, NULL, 'd' },
|
|
|
|
{ "finish", required_argument, NULL, 'f' },
|
|
|
|
{ "config-dir", required_argument, NULL, 'g' },
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "info", no_argument, NULL, 'i' },
|
|
|
|
{ "comment", required_argument, NULL, 'm' },
|
|
|
|
{ "nat-traversal", no_argument, NULL, 'n' },
|
|
|
|
{ "output-dir", required_argument, NULL, 'o' },
|
|
|
|
{ "port", required_argument, NULL, 'p' },
|
|
|
|
{ "private", no_argument, NULL, 'r' },
|
|
|
|
{ "scrape", no_argument, NULL, 's' },
|
2008-06-04 20:04:19 +00:00
|
|
|
{ "tos", required_argument, NULL, 't' },
|
2008-04-19 00:41:32 +00:00
|
|
|
{ "upload", required_argument, NULL, 'u' },
|
|
|
|
{ "verbose", required_argument, NULL, 'v' },
|
|
|
|
{ "version", no_argument, NULL, 'V' },
|
|
|
|
{ "recheck", no_argument, NULL, 'y' },
|
2006-07-16 19:39:23 +00:00
|
|
|
{ 0, 0, 0, 0} };
|
2008-04-19 00:41:32 +00:00
|
|
|
int optind = 0;
|
|
|
|
int c = getopt_long( argc, argv,
|
2008-06-04 20:04:19 +00:00
|
|
|
"a:c:d:f:g:him:no:p:rst:u:v:Vy",
|
2008-04-19 00:41:32 +00:00
|
|
|
long_options, &optind );
|
2006-07-16 19:39:23 +00:00
|
|
|
if( c < 0 )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch( c )
|
|
|
|
{
|
2008-04-19 00:41:32 +00:00
|
|
|
case 'a': announce = optarg; break;
|
|
|
|
case 'c': sourceFile = optarg; break;
|
|
|
|
case 'd': downloadLimit = atoi( optarg ); break;
|
|
|
|
case 'f': finishCall = optarg; break;
|
|
|
|
case 'g': configdir = strdup( optarg ); break;
|
|
|
|
case 'h': showHelp = 1; break;
|
|
|
|
case 'i': showInfo = 1; break;
|
|
|
|
case 'm': comment = optarg; break;
|
|
|
|
case 'n': natTraversal = 1; break;
|
2008-05-18 16:44:30 +00:00
|
|
|
case 'o': tr_strlcpy( downloadDir, optarg, sizeof( downloadDir ) ); break;
|
|
|
|
case 'p': peerPort = atoi( optarg ); break;
|
2008-04-19 00:41:32 +00:00
|
|
|
case 'r': isPrivate = 1; break;
|
|
|
|
case 's': showScrape = 1; break;
|
2008-06-04 20:04:19 +00:00
|
|
|
case 't': peerSocketTOS = atoi( optarg ); break;
|
2008-04-19 00:41:32 +00:00
|
|
|
case 'u': uploadLimit = atoi( optarg ); break;
|
|
|
|
case 'v': verboseLevel = atoi( optarg ); break;
|
|
|
|
case 'V': showVersion = 1; break;
|
|
|
|
case 'y': recheckData = 1; break;
|
|
|
|
default: return 1;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
if( !*downloadDir )
|
|
|
|
getcwd( downloadDir, sizeof( downloadDir ) );
|
|
|
|
|
2008-01-01 18:14:05 +00:00
|
|
|
if( showHelp || showVersion )
|
|
|
|
return 0;
|
|
|
|
|
2007-11-26 16:22:36 +00:00
|
|
|
if( optind >= argc )
|
2008-01-01 18:14:05 +00:00
|
|
|
return 1;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
torrentPath = argv[optind];
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sigHandler( int signal )
|
|
|
|
{
|
|
|
|
switch( signal )
|
|
|
|
{
|
|
|
|
case SIGINT:
|
2007-02-13 05:20:52 +00:00
|
|
|
gotsig = 1;
|
2006-07-16 19:39:23 +00:00
|
|
|
break;
|
2007-10-19 21:53:35 +00:00
|
|
|
|
|
|
|
case SIGHUP:
|
|
|
|
manualUpdate = 1;
|
|
|
|
break;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|