transmission/daemon/remote.c

371 lines
14 KiB
C
Raw Normal View History

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
*
* $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 */
2008-05-18 16:44:30 +00:00
#include <getopt.h>
#include <unistd.h> /* getcwd */
2007-04-18 16:39:10 +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"
" -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;
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-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;
char optstr[] = "a:c:d:DeEf:ghlmMp:r:s:S:u:Uv:";
const struct option longopts[] =
2007-04-18 16:39:10 +00:00
{
{ "add", required_argument, NULL, 'a' },
{ "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' },
{ "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-18 16:44:30 +00:00
char buf[MAX_PATH_LENGTH];
tr_benc top, *args;
tr_bencInitDict( &top, 3 );
args = tr_bencDictAddDict( &top, "args", 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-18 16:44:30 +00:00
case 'g': debug = 1; break;
case 'h': showUsage( ); break;
case 'a': tr_bencDictAddStr( &top, "method", "torrent-add" );
tr_bencDictAddStr( args, "filename", optarg );
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;
case 'l': tr_bencDictAddStr( &top, "method", "torrent-list" );
tr_bencDictAddInt( &top, "tag", TAG_LIST );
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" ) )
tr_bencDictAddStr( args, "ids", optarg );
break;
case 's': tr_bencDictAddStr( &top, "method", "torrent-start" );
if( strcmp( optarg, "all" ) )
tr_bencDictAddStr( args, "ids", optarg );
break;
case 'S': tr_bencDictAddStr( &top, "method", "torrent-stop" );
if( strcmp( optarg, "all" ) )
tr_bencDictAddStr( args, "ids", optarg );
break;
case 'v': tr_bencDictAddStr( &top, "method", "torrent-verify" );
if( strcmp( optarg, "all" ) )
tr_bencDictAddStr( args, "ids", optarg );
break;
default:
showUsage( );
break;
2007-04-18 16:39:10 +00:00
}
reqs[reqCount++] = tr_bencSaveAsJSON( &top, NULL );
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
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-18 16:44:30 +00:00
if( tr_jsonParse( response, len, &top, NULL ) )
tr_nerr( MY_NAME, "Unable to parse 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 ) )
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 ) ) &&
( tr_bencDictFindList( args, "list", &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_bencDictFindInt( d, "status", &status )
&& tr_bencDictFindStr( d, "name", &name )
&& tr_bencDictFindStr( d, "ratio", &ratiostr )
&& tr_bencDictFindStr( d, "rateUpload", &upstr )
&& tr_bencDictFindStr( d, "rateDownload", &dnstr ) )
{
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( );
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 );
curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, writeFunc );
curl_easy_setopt( curl, CURLOPT_WRITEDATA, buf );
curl_easy_setopt( curl, CURLOPT_POST, 1 );
curl_easy_setopt( curl, CURLOPT_URL, url );
2008-05-18 16:44:30 +00:00
for( i=0; i<reqCount; ++i )
{
CURLcode res;
curl_easy_setopt( curl, CURLOPT_POSTFIELDS, reqs[i] );
2008-05-18 16:44:30 +00:00
if(( res = curl_easy_perform( curl )))
tr_nerr( MY_NAME, "(%s:%d) %s", host, port, curl_easy_strerror( res ) );
2007-04-18 16:39:10 +00:00
else
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 */
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
tr_free( host );
2008-05-18 16:44:30 +00:00
return 0;
}