1 | /* |
---|
2 | * This file Copyright (C) 2008 Charles Kerr <charles@rebelbase.com> |
---|
3 | * |
---|
4 | * This file is licensed by the GPL version 2. Works owned by the |
---|
5 | * Transmission project are granted a special exemption to clause 2(b) |
---|
6 | * so that the bulk of its code can remain under the MIT license. |
---|
7 | * This exemption does not extend to derived works not owned by |
---|
8 | * the Transmission project. |
---|
9 | * |
---|
10 | * $Id: remote.c 6238 2008-06-21 15:16:16Z charles $ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <stdio.h> |
---|
14 | #include <stdlib.h> |
---|
15 | #include <string.h> /* strcmp */ |
---|
16 | |
---|
17 | #include <getopt.h> |
---|
18 | #include <unistd.h> /* getcwd */ |
---|
19 | |
---|
20 | #include <libevent/event.h> |
---|
21 | #include <curl/curl.h> |
---|
22 | |
---|
23 | #include <libtransmission/transmission.h> |
---|
24 | #include <libtransmission/bencode.h> |
---|
25 | #include <libtransmission/rpc.h> |
---|
26 | #include <libtransmission/json.h> |
---|
27 | #include <libtransmission/utils.h> |
---|
28 | #include <libtransmission/version.h> |
---|
29 | |
---|
30 | #define MY_NAME "transmission-remote" |
---|
31 | #define DEFAULT_HOST "localhost" |
---|
32 | #define DEFAULT_PORT TR_DEFAULT_RPC_PORT |
---|
33 | |
---|
34 | enum { TAG_LIST }; |
---|
35 | |
---|
36 | static void |
---|
37 | showUsage( void ) |
---|
38 | { |
---|
39 | puts( "Transmission "LONG_VERSION_STRING" http://www.transmissionbt.com/\n" |
---|
40 | "A fast and easy BitTorrent client\n" |
---|
41 | "\n" |
---|
42 | "Usage: "MY_NAME" [host] [options]\n" |
---|
43 | " "MY_NAME" [port] [options]\n" |
---|
44 | " "MY_NAME" [host:port] [options]\n" |
---|
45 | "\n" |
---|
46 | "Options:\n" |
---|
47 | " -a --add <torrent> Add a torrent\n" |
---|
48 | " -c --encryption required Require encryption for all peers\n" |
---|
49 | " -c --encryption preferred Prefer peers to use encryption\n" |
---|
50 | " -c --encryption tolerated Prefer peers to use plaintext\n" |
---|
51 | " -d --download-limit <int> Max download rate in KiB/s\n" |
---|
52 | " -D --download-unlimited No download rate limit\n" |
---|
53 | " -e --enable-pex Enable peer exchange\n" |
---|
54 | " -E --disable-pex Disable peer exchange\n" |
---|
55 | " -f --folder <path> Folder to set for new torrents\n" |
---|
56 | " -g --debug Print debugging information\n" |
---|
57 | " -h --help Display this message and exit\n" |
---|
58 | " -l --list Long list of all torrent and status\n" |
---|
59 | " -m --port-mapping Automatic port mapping via NAT-PMP or UPnP\n" |
---|
60 | " -M --no-port-mapping Disable automatic port mapping\n" |
---|
61 | " -p --port <int> Port to listen for incoming peers\n" |
---|
62 | " -r --remove <int> Remove the torrent with the given ID\n" |
---|
63 | " -r --remove all Remove all torrents\n" |
---|
64 | " -s --start <int> Start the torrent with the given ID\n" |
---|
65 | " -s --start all Start all stopped torrents\n" |
---|
66 | " -S --stop <int> Stop the torrent with the given ID\n" |
---|
67 | " -S --stop all Stop all running torrents\n" |
---|
68 | " -t --auth <user>:<pass> Username and password for authentication\n" |
---|
69 | " -u --upload-limit <int> Max upload rate in KiB/s\n" |
---|
70 | " -U --upload-unlimited No upload rate limit\n" |
---|
71 | " -v --verify <id> Verify the torrent's local data\n" ); |
---|
72 | exit( 0 ); |
---|
73 | } |
---|
74 | |
---|
75 | static int |
---|
76 | numarg( const char * arg ) |
---|
77 | { |
---|
78 | char * end = NULL; |
---|
79 | const long num = strtol( arg, &end, 10 ); |
---|
80 | if( *end ) { |
---|
81 | fprintf( stderr, "Not a number: \"%s\"\n", arg ); |
---|
82 | showUsage( ); |
---|
83 | } |
---|
84 | return num; |
---|
85 | } |
---|
86 | |
---|
87 | static char * reqs[256]; /* arbitrary max */ |
---|
88 | static int reqCount = 0; |
---|
89 | static int debug = 0; |
---|
90 | static char * auth = NULL; |
---|
91 | |
---|
92 | static char* |
---|
93 | absolutify( char * buf, size_t len, const char * path ) |
---|
94 | { |
---|
95 | if( *path == '/' ) |
---|
96 | tr_strlcpy( buf, path, len ); |
---|
97 | else { |
---|
98 | char cwd[MAX_PATH_LENGTH]; |
---|
99 | getcwd( cwd, sizeof( cwd ) ); |
---|
100 | tr_buildPath( buf, len, cwd, path, NULL ); |
---|
101 | } |
---|
102 | return buf; |
---|
103 | } |
---|
104 | |
---|
105 | static char* |
---|
106 | getEncodedMetainfo( const char * filename ) |
---|
107 | { |
---|
108 | size_t len = 0; |
---|
109 | uint8_t * buf = tr_loadFile( filename, &len ); |
---|
110 | char * b64 = tr_base64_encode( buf, len, NULL ); |
---|
111 | tr_free( buf ); |
---|
112 | return b64; |
---|
113 | } |
---|
114 | |
---|
115 | static void |
---|
116 | readargs( int argc, char ** argv ) |
---|
117 | { |
---|
118 | int opt; |
---|
119 | char optstr[] = "a:c:d:DeEf:ghlmMp:r:s:S:t:u:Uv:"; |
---|
120 | |
---|
121 | const struct option longopts[] = |
---|
122 | { |
---|
123 | { "add", required_argument, NULL, 'a' }, |
---|
124 | { "encryption", required_argument, NULL, 'c' }, |
---|
125 | { "download-limit", required_argument, NULL, 'd' }, |
---|
126 | { "download-unlimited", no_argument, NULL, 'D' }, |
---|
127 | { "enable-pex", no_argument, NULL, 'e' }, |
---|
128 | { "disable-pex", no_argument, NULL, 'E' }, |
---|
129 | { "folder", required_argument, NULL, 'f' }, |
---|
130 | { "debug", no_argument, NULL, 'g' }, |
---|
131 | { "help", no_argument, NULL, 'h' }, |
---|
132 | { "list", no_argument, NULL, 'l' }, |
---|
133 | { "port-mapping", no_argument, NULL, 'm' }, |
---|
134 | { "no-port-mapping", no_argument, NULL, 'M' }, |
---|
135 | { "port", required_argument, NULL, 'p' }, |
---|
136 | { "remove", required_argument, NULL, 'r' }, |
---|
137 | { "start", required_argument, NULL, 's' }, |
---|
138 | { "stop", required_argument, NULL, 'S' }, |
---|
139 | { "auth", required_argument, NULL, 't' }, |
---|
140 | { "upload-limit", required_argument, NULL, 'u' }, |
---|
141 | { "upload-unlimited", no_argument, NULL, 'U' }, |
---|
142 | { "verify", required_argument, NULL, 'v' }, |
---|
143 | { NULL, 0, NULL, 0 } |
---|
144 | }; |
---|
145 | |
---|
146 | while((( opt = getopt_long( argc, argv, optstr, longopts, NULL ))) != -1 ) |
---|
147 | { |
---|
148 | char * tmp; |
---|
149 | char buf[MAX_PATH_LENGTH]; |
---|
150 | int addArg = TRUE; |
---|
151 | int64_t fields = 0; |
---|
152 | tr_benc top, *args; |
---|
153 | tr_bencInitDict( &top, 3 ); |
---|
154 | args = tr_bencDictAddDict( &top, "arguments", 0 ); |
---|
155 | |
---|
156 | switch( opt ) |
---|
157 | { |
---|
158 | case 'g': debug = 1; |
---|
159 | addArg = FALSE; |
---|
160 | break; |
---|
161 | case 't': auth = tr_strdup( optarg ); |
---|
162 | addArg = FALSE; |
---|
163 | break; |
---|
164 | case 'h': showUsage( ); |
---|
165 | addArg = FALSE; |
---|
166 | break; |
---|
167 | case 'a': tr_bencDictAddStr( &top, "method", "torrent-add" ); |
---|
168 | tr_bencDictAddStr( args, "metainfo", ((tmp=getEncodedMetainfo(optarg))) ); |
---|
169 | tr_free( tmp ); |
---|
170 | break; |
---|
171 | case 'c': tr_bencDictAddStr( &top, "method", "session-set" ); |
---|
172 | tr_bencDictAddStr( args, "encryption", optarg ); |
---|
173 | break; |
---|
174 | case 'd': tr_bencDictAddStr( &top, "method", "session-set" ); |
---|
175 | tr_bencDictAddInt( args, "speed-limit-down", numarg( optarg ) ); |
---|
176 | tr_bencDictAddInt( args, "speed-limit-down-enabled", 1 ); |
---|
177 | break; |
---|
178 | case 'D': tr_bencDictAddStr( &top, "method", "session-set" ); |
---|
179 | tr_bencDictAddInt( args, "speed-limit-down-enabled", 0 ); |
---|
180 | break; |
---|
181 | case 'u': tr_bencDictAddStr( &top, "method", "session-set" ); |
---|
182 | tr_bencDictAddInt( args, "speed-limit-up", numarg( optarg ) ); |
---|
183 | tr_bencDictAddInt( args, "speed-limit-up-enabled", 1 ); |
---|
184 | break; |
---|
185 | case 'U': tr_bencDictAddStr( &top, "method", "session-set" ); |
---|
186 | tr_bencDictAddInt( args, "speed-limit-up-enabled", 0 ); |
---|
187 | break; |
---|
188 | case 'e': tr_bencDictAddStr( &top, "method", "session-set" ); |
---|
189 | tr_bencDictAddInt( args, "pex-allowed", 1 ); |
---|
190 | break; |
---|
191 | case 'E': tr_bencDictAddStr( &top, "method", "session-set" ); |
---|
192 | tr_bencDictAddInt( args, "pex-allowed", 0 ); |
---|
193 | break; |
---|
194 | case 'f': tr_bencDictAddStr( &top, "method", "session-set" ); |
---|
195 | tr_bencDictAddStr( args, "download-dir", absolutify(buf,sizeof(buf),optarg) ); |
---|
196 | break; |
---|
197 | case 'l': tr_bencDictAddStr( &top, "method", "torrent-get" ); |
---|
198 | tr_bencDictAddInt( &top, "tag", TAG_LIST ); |
---|
199 | fields = TR_RPC_TORRENT_FIELD_ID |
---|
200 | | TR_RPC_TORRENT_FIELD_ACTIVITY; |
---|
201 | tr_bencDictAddInt( args, "fields", fields ); |
---|
202 | break; |
---|
203 | case 'm': tr_bencDictAddStr( &top, "method", "session-set" ); |
---|
204 | tr_bencDictAddInt( args, "port-forwarding-enabled", 1 ); |
---|
205 | break; |
---|
206 | case 'M': tr_bencDictAddStr( &top, "method", "session-set" ); |
---|
207 | tr_bencDictAddInt( args, "port-forwarding-enabled", 0 ); |
---|
208 | break; |
---|
209 | case 'p': tr_bencDictAddStr( &top, "method", "session-set" ); |
---|
210 | tr_bencDictAddInt( args, "port", numarg( optarg ) ); |
---|
211 | break; |
---|
212 | case 'r': tr_bencDictAddStr( &top, "method", "torrent-remove" ); |
---|
213 | if( strcmp( optarg, "all" ) ) |
---|
214 | tr_rpc_parse_list_str( tr_bencDictAdd( args, "ids" ), optarg, strlen(optarg) ); |
---|
215 | break; |
---|
216 | case 's': tr_bencDictAddStr( &top, "method", "torrent-start" ); |
---|
217 | if( strcmp( optarg, "all" ) ) |
---|
218 | tr_rpc_parse_list_str( tr_bencDictAdd( args, "ids" ), optarg, strlen(optarg) ); |
---|
219 | break; |
---|
220 | case 'S': tr_bencDictAddStr( &top, "method", "torrent-stop" ); |
---|
221 | if( strcmp( optarg, "all" ) ) |
---|
222 | tr_rpc_parse_list_str( tr_bencDictAdd( args, "ids" ), optarg, strlen(optarg) ); |
---|
223 | break; |
---|
224 | case 'v': tr_bencDictAddStr( &top, "method", "torrent-verify" ); |
---|
225 | if( strcmp( optarg, "all" ) ) |
---|
226 | tr_rpc_parse_list_str( tr_bencDictAdd( args, "ids" ), optarg, strlen(optarg) ); |
---|
227 | break; |
---|
228 | default: |
---|
229 | showUsage( ); |
---|
230 | addArg = FALSE; |
---|
231 | break; |
---|
232 | } |
---|
233 | |
---|
234 | if( addArg ) |
---|
235 | reqs[reqCount++] = tr_bencSaveAsJSON( &top, NULL ); |
---|
236 | tr_bencFree( &top ); |
---|
237 | } |
---|
238 | } |
---|
239 | |
---|
240 | /* [host:port] or [host] or [port] */ |
---|
241 | static void |
---|
242 | getHostAndPort( int * argc, char ** argv, char ** host, int * port ) |
---|
243 | { |
---|
244 | if( *argv[1] != '-' ) |
---|
245 | { |
---|
246 | int i; |
---|
247 | const char * s = argv[1]; |
---|
248 | const char * delim = strchr( s, ':' ); |
---|
249 | if( delim ) { /* user passed in both host and port */ |
---|
250 | *host = tr_strndup( s, delim-s ); |
---|
251 | *port = atoi( delim+1 ); |
---|
252 | } else { |
---|
253 | char * end; |
---|
254 | const int i = strtol( s, &end, 10 ); |
---|
255 | if( !*end ) /* user passed in a port */ |
---|
256 | *port = i; |
---|
257 | else /* user passed in a host */ |
---|
258 | *host = tr_strdup( s ); |
---|
259 | } |
---|
260 | |
---|
261 | *argc -= 1; |
---|
262 | for( i=1; i<*argc; ++i ) |
---|
263 | argv[i] = argv[i+1]; |
---|
264 | } |
---|
265 | } |
---|
266 | |
---|
267 | static size_t |
---|
268 | writeFunc( void * ptr, size_t size, size_t nmemb, void * buf ) |
---|
269 | { |
---|
270 | const size_t byteCount = size * nmemb; |
---|
271 | evbuffer_add( buf, ptr, byteCount ); |
---|
272 | return byteCount; |
---|
273 | } |
---|
274 | |
---|
275 | static const char* |
---|
276 | torrentStatusToString( int i ) |
---|
277 | { |
---|
278 | switch( i ) |
---|
279 | { |
---|
280 | case TR_STATUS_CHECK_WAIT: return "Will Verify"; |
---|
281 | case TR_STATUS_CHECK: return "Verifying"; |
---|
282 | case TR_STATUS_DOWNLOAD: return "Downloading"; |
---|
283 | case TR_STATUS_SEED: return "Seeding"; |
---|
284 | case TR_STATUS_STOPPED: return "Stopped"; |
---|
285 | default: return "Error"; |
---|
286 | } |
---|
287 | } |
---|
288 | |
---|
289 | static void |
---|
290 | processResponse( const char * host, int port, |
---|
291 | const void * response, size_t len ) |
---|
292 | { |
---|
293 | tr_benc top; |
---|
294 | |
---|
295 | if( debug ) |
---|
296 | fprintf( stderr, "got response: [%*.*s]\n", |
---|
297 | (int)len, (int)len, (const char*) response ); |
---|
298 | |
---|
299 | if( tr_jsonParse( response, len, &top, NULL ) ) |
---|
300 | tr_nerr( MY_NAME, "Unable to parse response \"%*.*s\"", (int)len, (int)len, (char*)response ); |
---|
301 | else |
---|
302 | { |
---|
303 | tr_benc *args, *list; |
---|
304 | int64_t tag = -1; |
---|
305 | const char * str; |
---|
306 | tr_bencDictFindInt( &top, "tag", &tag ); |
---|
307 | |
---|
308 | if( tr_bencDictFindStr( &top, "result", &str ) ) |
---|
309 | printf( "%s:%d responded: \"%s\"\n", host, port, str ); |
---|
310 | |
---|
311 | if( ( tag == TAG_LIST ) && |
---|
312 | ( tr_bencDictFindDict( &top, "arguments", &args ) ) && |
---|
313 | ( tr_bencDictFindList( args, "torrents", &list ) ) ) |
---|
314 | { |
---|
315 | int i, n; |
---|
316 | for( i=0, n=tr_bencListSize( list ); i<n; ++i ) |
---|
317 | { |
---|
318 | int64_t id, status, up, down; |
---|
319 | const char *name, *ratiostr; |
---|
320 | tr_benc * d = tr_bencListChild( list, i ); |
---|
321 | if( tr_bencDictFindInt( d, "id", &id ) |
---|
322 | && tr_bencDictFindStr( d, "name", &name ) |
---|
323 | && tr_bencDictFindInt( d, "rateDownload", &down ) |
---|
324 | && tr_bencDictFindInt( d, "rateUpload", &up ) |
---|
325 | && tr_bencDictFindStr( d, "ratio", &ratiostr ) |
---|
326 | && tr_bencDictFindInt( d, "status", &status ) ) |
---|
327 | { |
---|
328 | printf( "%4d. Up: %5.1f Down: %5.1f Ratio: %4.1f %-15s %s\n", |
---|
329 | (int)id, |
---|
330 | up / 1024.0, |
---|
331 | down / 1024.0, |
---|
332 | strtod( ratiostr, NULL ), |
---|
333 | torrentStatusToString( status ), |
---|
334 | name ); |
---|
335 | } |
---|
336 | } |
---|
337 | } |
---|
338 | |
---|
339 | tr_bencFree( &top ); |
---|
340 | } |
---|
341 | } |
---|
342 | |
---|
343 | static void |
---|
344 | processRequests( const char * host, int port, |
---|
345 | const char ** reqs, int reqCount ) |
---|
346 | { |
---|
347 | int i; |
---|
348 | CURL * curl; |
---|
349 | struct evbuffer * buf = evbuffer_new( ); |
---|
350 | char * url = tr_strdup_printf( "http://%s:%d/transmission/rpc", host, port ); |
---|
351 | |
---|
352 | curl = curl_easy_init( ); |
---|
353 | curl_easy_setopt( curl, CURLOPT_VERBOSE, debug ); |
---|
354 | curl_easy_setopt( curl, CURLOPT_USERAGENT, MY_NAME"/"LONG_VERSION_STRING ); |
---|
355 | curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, writeFunc ); |
---|
356 | curl_easy_setopt( curl, CURLOPT_WRITEDATA, buf ); |
---|
357 | curl_easy_setopt( curl, CURLOPT_POST, 1 ); |
---|
358 | curl_easy_setopt( curl, CURLOPT_URL, url ); |
---|
359 | if( auth ) { |
---|
360 | curl_easy_setopt( curl, CURLOPT_USERPWD, auth ); |
---|
361 | curl_easy_setopt( curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY ); |
---|
362 | } |
---|
363 | |
---|
364 | for( i=0; i<reqCount; ++i ) |
---|
365 | { |
---|
366 | CURLcode res; |
---|
367 | curl_easy_setopt( curl, CURLOPT_POSTFIELDS, reqs[i] ); |
---|
368 | if( debug ) |
---|
369 | tr_ninf( MY_NAME, "posting [%s]\n", reqs[i] ); |
---|
370 | if(( res = curl_easy_perform( curl ))) |
---|
371 | tr_nerr( MY_NAME, "(%s:%d) %s", host, port, curl_easy_strerror( res ) ); |
---|
372 | else |
---|
373 | processResponse( host, port, EVBUFFER_DATA( buf ), EVBUFFER_LENGTH( buf ) ); |
---|
374 | |
---|
375 | evbuffer_drain( buf, EVBUFFER_LENGTH( buf ) ); |
---|
376 | } |
---|
377 | |
---|
378 | /* cleanup */ |
---|
379 | tr_free( url ); |
---|
380 | evbuffer_free( buf ); |
---|
381 | curl_easy_cleanup( curl ); |
---|
382 | } |
---|
383 | |
---|
384 | int |
---|
385 | main( int argc, char ** argv ) |
---|
386 | { |
---|
387 | int i; |
---|
388 | int port = DEFAULT_PORT; |
---|
389 | char * host = NULL; |
---|
390 | |
---|
391 | if( argc < 2 ) |
---|
392 | showUsage( ); |
---|
393 | |
---|
394 | getHostAndPort( &argc, argv, &host, &port ); |
---|
395 | if( host == NULL ) |
---|
396 | host = tr_strdup( DEFAULT_HOST ); |
---|
397 | |
---|
398 | readargs( argc, argv ); |
---|
399 | if( reqCount ) |
---|
400 | processRequests( host, port, (const char**)reqs, reqCount ); |
---|
401 | else |
---|
402 | showUsage( ); |
---|
403 | |
---|
404 | for( i=0; i<reqCount; ++i ) |
---|
405 | tr_free( reqs[i] ); |
---|
406 | |
---|
407 | tr_free( host ); |
---|
408 | return 0; |
---|
409 | } |
---|