2008-05-18 16:44:30 +00:00
|
|
|
/*
|
|
|
|
* This file Copyright (C) 2008 Charles Kerr <charles@rebelbase.com>
|
2007-04-18 16:39:10 +00:00
|
|
|
*
|
2008-05-18 16:44:30 +00:00
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
|
|
|
* Transmission project are granted a special exemption to clause 2(b)
|
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
2007-04-18 16:39:10 +00:00
|
|
|
*
|
2008-05-19 00:12:31 +00:00
|
|
|
* $Id$
|
2008-05-18 16:44:30 +00:00
|
|
|
*/
|
2007-04-18 16:39:10 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2008-05-18 16:44:30 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h> /* strcmp */
|
2007-07-18 23:04:26 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
#include <getopt.h>
|
|
|
|
#include <unistd.h> /* getcwd */
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-05-19 00:21:16 +00:00
|
|
|
#include <libevent/event.h>
|
2008-05-18 16:44:30 +00:00
|
|
|
#include <curl/curl.h>
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
|
|
|
#include <libtransmission/bencode.h>
|
|
|
|
#include <libtransmission/rpc.h>
|
|
|
|
#include <libtransmission/json.h>
|
|
|
|
#include <libtransmission/utils.h>
|
|
|
|
#include <libtransmission/version.h>
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
#define MY_NAME "transmission-remote"
|
|
|
|
#define DEFAULT_HOST "localhost"
|
|
|
|
#define DEFAULT_PORT TR_DEFAULT_RPC_PORT
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
enum { TAG_LIST };
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
static void
|
|
|
|
showUsage( void )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
puts( "Transmission "LONG_VERSION_STRING" http://www.transmissionbt.com/\n"
|
|
|
|
"A fast and easy BitTorrent client\n"
|
|
|
|
"\n"
|
|
|
|
"Usage: "MY_NAME" [host] [options]\n"
|
|
|
|
" "MY_NAME" [port] [options]\n"
|
|
|
|
" "MY_NAME" [host:port] [options]\n"
|
|
|
|
"\n"
|
|
|
|
"Options:\n"
|
|
|
|
" -a --add <torrent> Add a torrent\n"
|
|
|
|
" -c --encryption required Require encryption for all peers\n"
|
|
|
|
" -c --encryption preferred Prefer peers to use encryption\n"
|
|
|
|
" -c --encryption tolerated Prefer peers to use plaintext\n"
|
|
|
|
" -d --download-limit <int> Max download rate in KiB/s\n"
|
|
|
|
" -D --download-unlimited No download rate limit\n"
|
|
|
|
" -e --enable-pex Enable peer exchange\n"
|
|
|
|
" -E --disable-pex Disable peer exchange\n"
|
|
|
|
" -f --folder <path> Folder to set for new torrents\n"
|
|
|
|
" -g --debug Print debugging information\n"
|
|
|
|
" -h --help Display this message and exit\n"
|
|
|
|
" -l --list Long list of all torrent and status\n"
|
|
|
|
" -m --port-mapping Automatic port mapping via NAT-PMP or UPnP\n"
|
|
|
|
" -M --no-port-mapping Disable automatic port mapping\n"
|
|
|
|
" -p --port <int> Port to listen for incoming peers\n"
|
|
|
|
" -r --remove <int> Remove the torrent with the given ID\n"
|
|
|
|
" -r --remove all Remove all torrents\n"
|
|
|
|
" -s --start <int> Start the torrent with the given ID\n"
|
|
|
|
" -s --start all Start all stopped torrents\n"
|
|
|
|
" -S --stop <int> Stop the torrent with the given ID\n"
|
|
|
|
" -S --stop all Stop all running torrents\n"
|
2008-06-05 18:16:59 +00:00
|
|
|
" -t --auth <user>:<pass> Username and password for authentication\n"
|
2008-05-18 16:44:30 +00:00
|
|
|
" -u --upload-limit <int> Max upload rate in KiB/s\n"
|
|
|
|
" -U --upload-unlimited No upload rate limit\n"
|
|
|
|
" -v --verify <id> Verify the torrent's local data\n" );
|
|
|
|
exit( 0 );
|
|
|
|
}
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
static int
|
|
|
|
numarg( const char * arg )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
char * end = NULL;
|
|
|
|
const long num = strtol( arg, &end, 10 );
|
|
|
|
if( *end ) {
|
|
|
|
fprintf( stderr, "Not a number: \"%s\"\n", arg );
|
|
|
|
showUsage( );
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
return num;
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
static char * reqs[256]; /* arbitrary max */
|
|
|
|
static int reqCount = 0;
|
|
|
|
static int debug = 0;
|
2008-06-05 18:16:59 +00:00
|
|
|
static char * auth = NULL;
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
static char*
|
|
|
|
absolutify( char * buf, size_t len, const char * path )
|
|
|
|
{
|
|
|
|
if( *path == '/' )
|
|
|
|
tr_strlcpy( buf, path, len );
|
|
|
|
else {
|
|
|
|
char cwd[MAX_PATH_LENGTH];
|
|
|
|
getcwd( cwd, sizeof( cwd ) );
|
|
|
|
tr_buildPath( buf, len, cwd, path, NULL );
|
|
|
|
}
|
|
|
|
return buf;
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
|
|
|
|
2008-05-20 17:33:54 +00:00
|
|
|
static char*
|
|
|
|
getEncodedMetainfo( const char * filename )
|
|
|
|
{
|
2008-06-09 23:34:52 +00:00
|
|
|
size_t len = 0;
|
2008-05-20 17:33:54 +00:00
|
|
|
uint8_t * buf = tr_loadFile( filename, &len );
|
|
|
|
char * b64 = tr_base64_encode( buf, len, NULL );
|
|
|
|
tr_free( buf );
|
|
|
|
return b64;
|
|
|
|
}
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
static void
|
|
|
|
readargs( int argc, char ** argv )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
int opt;
|
2008-06-05 18:16:59 +00:00
|
|
|
char optstr[] = "a:c:d:DeEf:ghlmMp:r:s:S:t:u:Uv:";
|
2008-05-18 16:44:30 +00:00
|
|
|
|
|
|
|
const struct option longopts[] =
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
|
|
|
{ "add", required_argument, NULL, 'a' },
|
2007-10-26 03:43:27 +00:00
|
|
|
{ "encryption", required_argument, NULL, 'c' },
|
2007-04-18 16:39:10 +00:00
|
|
|
{ "download-limit", required_argument, NULL, 'd' },
|
|
|
|
{ "download-unlimited", no_argument, NULL, 'D' },
|
|
|
|
{ "enable-pex", no_argument, NULL, 'e' },
|
|
|
|
{ "disable-pex", no_argument, NULL, 'E' },
|
|
|
|
{ "folder", required_argument, NULL, 'f' },
|
2008-05-18 16:44:30 +00:00
|
|
|
{ "debug", no_argument, NULL, 'g' },
|
2007-04-18 16:39:10 +00:00
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "list", no_argument, NULL, 'l' },
|
|
|
|
{ "port-mapping", no_argument, NULL, 'm' },
|
|
|
|
{ "no-port-mapping", no_argument, NULL, 'M' },
|
|
|
|
{ "port", required_argument, NULL, 'p' },
|
|
|
|
{ "remove", required_argument, NULL, 'r' },
|
|
|
|
{ "start", required_argument, NULL, 's' },
|
|
|
|
{ "stop", required_argument, NULL, 'S' },
|
2008-06-05 18:16:59 +00:00
|
|
|
{ "auth", required_argument, NULL, 't' },
|
2007-04-18 16:39:10 +00:00
|
|
|
{ "upload-limit", required_argument, NULL, 'u' },
|
|
|
|
{ "upload-unlimited", no_argument, NULL, 'U' },
|
2008-02-27 17:38:39 +00:00
|
|
|
{ "verify", required_argument, NULL, 'v' },
|
2007-04-18 16:39:10 +00:00
|
|
|
{ NULL, 0, NULL, 0 }
|
|
|
|
};
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
while((( opt = getopt_long( argc, argv, optstr, longopts, NULL ))) != -1 )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-05-20 17:33:54 +00:00
|
|
|
char * tmp;
|
2008-05-18 16:44:30 +00:00
|
|
|
char buf[MAX_PATH_LENGTH];
|
2008-05-27 21:47:07 +00:00
|
|
|
int addArg = TRUE;
|
2008-06-16 03:47:50 +00:00
|
|
|
int64_t fields = 0;
|
2008-05-19 18:16:58 +00:00
|
|
|
tr_benc top, *args;
|
|
|
|
tr_bencInitDict( &top, 3 );
|
2008-05-20 17:33:54 +00:00
|
|
|
args = tr_bencDictAddDict( &top, "arguments", 0 );
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
switch( opt )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-05-27 21:47:07 +00:00
|
|
|
case 'g': debug = 1;
|
|
|
|
addArg = FALSE;
|
|
|
|
break;
|
2008-06-05 18:16:59 +00:00
|
|
|
case 't': auth = tr_strdup( optarg );
|
|
|
|
addArg = FALSE;
|
|
|
|
break;
|
2008-05-27 21:47:07 +00:00
|
|
|
case 'h': showUsage( );
|
|
|
|
addArg = FALSE;
|
|
|
|
break;
|
2008-05-19 18:16:58 +00:00
|
|
|
case 'a': tr_bencDictAddStr( &top, "method", "torrent-add" );
|
2008-05-20 17:33:54 +00:00
|
|
|
tr_bencDictAddStr( args, "metainfo", ((tmp=getEncodedMetainfo(optarg))) );
|
|
|
|
tr_free( tmp );
|
2008-05-19 18:16:58 +00:00
|
|
|
break;
|
|
|
|
case 'c': tr_bencDictAddStr( &top, "method", "session-set" );
|
|
|
|
tr_bencDictAddStr( args, "encryption", optarg );
|
|
|
|
break;
|
|
|
|
case 'd': tr_bencDictAddStr( &top, "method", "session-set" );
|
|
|
|
tr_bencDictAddInt( args, "speed-limit-down", numarg( optarg ) );
|
|
|
|
tr_bencDictAddInt( args, "speed-limit-down-enabled", 1 );
|
|
|
|
break;
|
|
|
|
case 'D': tr_bencDictAddStr( &top, "method", "session-set" );
|
|
|
|
tr_bencDictAddInt( args, "speed-limit-down-enabled", 0 );
|
|
|
|
break;
|
|
|
|
case 'u': tr_bencDictAddStr( &top, "method", "session-set" );
|
|
|
|
tr_bencDictAddInt( args, "speed-limit-up", numarg( optarg ) );
|
|
|
|
tr_bencDictAddInt( args, "speed-limit-up-enabled", 1 );
|
|
|
|
break;
|
|
|
|
case 'U': tr_bencDictAddStr( &top, "method", "session-set" );
|
|
|
|
tr_bencDictAddInt( args, "speed-limit-up-enabled", 0 );
|
|
|
|
break;
|
|
|
|
case 'e': tr_bencDictAddStr( &top, "method", "session-set" );
|
|
|
|
tr_bencDictAddInt( args, "pex-allowed", 1 );
|
|
|
|
break;
|
|
|
|
case 'E': tr_bencDictAddStr( &top, "method", "session-set" );
|
|
|
|
tr_bencDictAddInt( args, "pex-allowed", 0 );
|
|
|
|
break;
|
|
|
|
case 'f': tr_bencDictAddStr( &top, "method", "session-set" );
|
|
|
|
tr_bencDictAddStr( args, "download-dir", absolutify(buf,sizeof(buf),optarg) );
|
|
|
|
break;
|
2008-06-16 03:47:50 +00:00
|
|
|
case 'l': tr_bencDictAddStr( &top, "method", "torrent-info" );
|
2008-05-19 18:16:58 +00:00
|
|
|
tr_bencDictAddInt( &top, "tag", TAG_LIST );
|
2008-06-16 03:47:50 +00:00
|
|
|
fields = TR_RPC_TORRENT_FIELD_ID
|
|
|
|
| TR_RPC_TORRENT_FIELD_ACTIVITY
|
|
|
|
| TR_RPC_TORRENT_FIELD_SIZE;
|
|
|
|
tr_bencDictAddInt( args, "fields", fields );
|
2008-06-16 22:11:50 +00:00
|
|
|
tr_bencDictAddStr( args, "sort", "name" );
|
2008-05-19 18:16:58 +00:00
|
|
|
break;
|
|
|
|
case 'm': tr_bencDictAddStr( &top, "method", "session-set" );
|
|
|
|
tr_bencDictAddInt( args, "port-forwarding-enabled", 1 );
|
|
|
|
break;
|
|
|
|
case 'M': tr_bencDictAddStr( &top, "method", "session-set" );
|
|
|
|
tr_bencDictAddInt( args, "port-forwarding-enabled", 0 );
|
|
|
|
break;
|
|
|
|
case 'p': tr_bencDictAddStr( &top, "method", "session-set" );
|
|
|
|
tr_bencDictAddInt( args, "port", numarg( optarg ) );
|
|
|
|
break;
|
|
|
|
case 'r': tr_bencDictAddStr( &top, "method", "torrent-remove" );
|
|
|
|
if( strcmp( optarg, "all" ) )
|
2008-05-24 18:22:16 +00:00
|
|
|
tr_rpc_parse_list_str( tr_bencDictAdd( args, "ids" ), optarg, strlen(optarg) );
|
2008-05-19 18:16:58 +00:00
|
|
|
break;
|
|
|
|
case 's': tr_bencDictAddStr( &top, "method", "torrent-start" );
|
|
|
|
if( strcmp( optarg, "all" ) )
|
2008-05-24 18:22:16 +00:00
|
|
|
tr_rpc_parse_list_str( tr_bencDictAdd( args, "ids" ), optarg, strlen(optarg) );
|
2008-05-19 18:16:58 +00:00
|
|
|
break;
|
|
|
|
case 'S': tr_bencDictAddStr( &top, "method", "torrent-stop" );
|
|
|
|
if( strcmp( optarg, "all" ) )
|
2008-05-24 18:22:16 +00:00
|
|
|
tr_rpc_parse_list_str( tr_bencDictAdd( args, "ids" ), optarg, strlen(optarg) );
|
2008-05-19 18:16:58 +00:00
|
|
|
break;
|
|
|
|
case 'v': tr_bencDictAddStr( &top, "method", "torrent-verify" );
|
|
|
|
if( strcmp( optarg, "all" ) )
|
2008-05-24 18:22:16 +00:00
|
|
|
tr_rpc_parse_list_str( tr_bencDictAdd( args, "ids" ), optarg, strlen(optarg) );
|
2008-05-19 18:16:58 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
showUsage( );
|
2008-05-27 21:47:07 +00:00
|
|
|
addArg = FALSE;
|
2008-05-19 18:16:58 +00:00
|
|
|
break;
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
|
|
|
|
2008-05-27 21:47:07 +00:00
|
|
|
if( addArg )
|
|
|
|
reqs[reqCount++] = tr_bencSaveAsJSON( &top, NULL );
|
2008-05-19 18:16:58 +00:00
|
|
|
tr_bencFree( &top );
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
/* [host:port] or [host] or [port] */
|
|
|
|
static void
|
|
|
|
getHostAndPort( int * argc, char ** argv, char ** host, int * port )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
if( *argv[1] != '-' )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
const char * s = argv[1];
|
|
|
|
const char * delim = strchr( s, ':' );
|
|
|
|
if( delim ) { /* user passed in both host and port */
|
|
|
|
*host = tr_strndup( s, delim-s );
|
|
|
|
*port = atoi( delim+1 );
|
|
|
|
} else {
|
|
|
|
char * end;
|
|
|
|
const int i = strtol( s, &end, 10 );
|
|
|
|
if( !*end ) /* user passed in a port */
|
|
|
|
*port = i;
|
|
|
|
else /* user passed in a host */
|
|
|
|
*host = tr_strdup( s );
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
*argc -= 1;
|
|
|
|
for( i=1; i<*argc; ++i )
|
|
|
|
argv[i] = argv[i+1];
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
static size_t
|
|
|
|
writeFunc( void * ptr, size_t size, size_t nmemb, void * buf )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
const size_t byteCount = size * nmemb;
|
|
|
|
evbuffer_add( buf, ptr, byteCount );
|
|
|
|
return byteCount;
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
static const char*
|
|
|
|
torrentStatusToString( int i )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
switch( i )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
case TR_STATUS_CHECK_WAIT: return "Will Verify";
|
|
|
|
case TR_STATUS_CHECK: return "Verifying";
|
|
|
|
case TR_STATUS_DOWNLOAD: return "Downloading";
|
|
|
|
case TR_STATUS_SEED: return "Seeding";
|
|
|
|
case TR_STATUS_STOPPED: return "Stopped";
|
|
|
|
default: return "Error";
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
static void
|
2008-05-20 14:01:15 +00:00
|
|
|
processResponse( const char * host, int port,
|
|
|
|
const void * response, size_t len )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_benc top;
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-05-27 21:47:07 +00:00
|
|
|
if( debug )
|
|
|
|
fprintf( stderr, "got response: [%*.*s]\n",
|
|
|
|
(int)len, (int)len, (const char*) response );
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
if( tr_jsonParse( response, len, &top, NULL ) )
|
2008-06-05 18:16:59 +00:00
|
|
|
tr_nerr( MY_NAME, "Unable to parse response \"%*.*s\"", (int)len, (int)len, (char*)response );
|
2008-05-18 16:44:30 +00:00
|
|
|
else
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_benc *args, *list;
|
|
|
|
int64_t tag = -1;
|
|
|
|
const char * str;
|
|
|
|
tr_bencDictFindInt( &top, "tag", &tag );
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
if( tr_bencDictFindStr( &top, "result", &str ) )
|
2008-05-20 14:01:15 +00:00
|
|
|
printf( "%s:%d responded: \"%s\"\n", host, port, str );
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
if( ( tag == TAG_LIST ) &&
|
|
|
|
( tr_bencDictFindDict( &top, "arguments", &args ) ) &&
|
2008-06-16 03:47:50 +00:00
|
|
|
( tr_bencDictFindList( args, "torrent-info", &list ) ) )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
int i, n;
|
|
|
|
for( i=0, n=tr_bencListSize( list ); i<n; ++i )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
int64_t id, status;
|
|
|
|
const char *name, *ratiostr, *upstr, *dnstr;
|
|
|
|
tr_benc * d = tr_bencListChild( list, i );
|
|
|
|
if( tr_bencDictFindInt( d, "id", &id )
|
|
|
|
&& tr_bencDictFindStr( d, "name", &name )
|
2008-06-16 03:47:50 +00:00
|
|
|
&& tr_bencDictFindStr( d, "rateDownload", &dnstr )
|
2008-05-18 16:44:30 +00:00
|
|
|
&& tr_bencDictFindStr( d, "rateUpload", &upstr )
|
2008-06-16 03:47:50 +00:00
|
|
|
&& tr_bencDictFindStr( d, "ratio", &ratiostr )
|
|
|
|
&& tr_bencDictFindInt( d, "status", &status ) )
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
|
|
|
printf( "%4d. Up: %5.1f Down: %5.1f Ratio: %4.1f %-15s %s\n",
|
|
|
|
(int)id,
|
|
|
|
strtod( upstr, NULL ),
|
|
|
|
strtod( dnstr, NULL ),
|
|
|
|
strtod( ratiostr, NULL ),
|
|
|
|
torrentStatusToString( status ),
|
|
|
|
name );
|
|
|
|
}
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_bencFree( &top );
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
static void
|
|
|
|
processRequests( const char * host, int port,
|
|
|
|
const char ** reqs, int reqCount )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
int i;
|
|
|
|
CURL * curl;
|
|
|
|
struct evbuffer * buf = evbuffer_new( );
|
2008-05-19 18:16:58 +00:00
|
|
|
char * url = tr_strdup_printf( "http://%s:%d/transmission", host, port );
|
2008-05-18 16:44:30 +00:00
|
|
|
|
|
|
|
curl = curl_easy_init( );
|
|
|
|
curl_easy_setopt( curl, CURLOPT_VERBOSE, debug );
|
|
|
|
curl_easy_setopt( curl, CURLOPT_USERAGENT, MY_NAME"/"LONG_VERSION_STRING );
|
2008-05-19 00:12:31 +00:00
|
|
|
curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, writeFunc );
|
|
|
|
curl_easy_setopt( curl, CURLOPT_WRITEDATA, buf );
|
2008-05-19 18:16:58 +00:00
|
|
|
curl_easy_setopt( curl, CURLOPT_POST, 1 );
|
|
|
|
curl_easy_setopt( curl, CURLOPT_URL, url );
|
2008-06-05 18:16:59 +00:00
|
|
|
if( auth ) {
|
|
|
|
curl_easy_setopt( curl, CURLOPT_USERPWD, auth );
|
|
|
|
curl_easy_setopt( curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY );
|
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
|
|
|
|
for( i=0; i<reqCount; ++i )
|
|
|
|
{
|
|
|
|
CURLcode res;
|
2008-05-19 18:16:58 +00:00
|
|
|
curl_easy_setopt( curl, CURLOPT_POSTFIELDS, reqs[i] );
|
2008-05-24 18:22:16 +00:00
|
|
|
if( debug )
|
|
|
|
tr_ninf( MY_NAME, "posting [%s]\n", reqs[i] );
|
2008-05-18 16:44:30 +00:00
|
|
|
if(( res = curl_easy_perform( curl )))
|
2008-05-20 14:01:15 +00:00
|
|
|
tr_nerr( MY_NAME, "(%s:%d) %s", host, port, curl_easy_strerror( res ) );
|
2007-04-18 16:39:10 +00:00
|
|
|
else
|
2008-05-20 14:01:15 +00:00
|
|
|
processResponse( host, port, EVBUFFER_DATA( buf ), EVBUFFER_LENGTH( buf ) );
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
evbuffer_drain( buf, EVBUFFER_LENGTH( buf ) );
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
|
|
|
|
/* cleanup */
|
2008-05-19 18:16:58 +00:00
|
|
|
tr_free( url );
|
2008-05-18 16:44:30 +00:00
|
|
|
evbuffer_free( buf );
|
|
|
|
curl_easy_cleanup( curl );
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-05-18 16:44:30 +00:00
|
|
|
main( int argc, char ** argv )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
int i;
|
|
|
|
int port = DEFAULT_PORT;
|
|
|
|
char * host = NULL;
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
if( argc < 2 )
|
|
|
|
showUsage( );
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
getHostAndPort( &argc, argv, &host, &port );
|
|
|
|
if( host == NULL )
|
|
|
|
host = tr_strdup( DEFAULT_HOST );
|
|
|
|
|
|
|
|
readargs( argc, argv );
|
|
|
|
if( reqCount )
|
|
|
|
processRequests( host, port, (const char**)reqs, reqCount );
|
2007-04-18 16:39:10 +00:00
|
|
|
else
|
2008-05-18 16:44:30 +00:00
|
|
|
showUsage( );
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
for( i=0; i<reqCount; ++i )
|
|
|
|
tr_free( reqs[i] );
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-05-19 18:16:58 +00:00
|
|
|
tr_free( host );
|
2008-05-18 16:44:30 +00:00
|
|
|
return 0;
|
|
|
|
}
|