1 | /****************************************************************************** |
---|
2 | * $Id: transmissioncli.c 4795 2008-01-22 20:11:28Z charles $ |
---|
3 | * |
---|
4 | * Copyright (c) 2005-2006 Transmission authors and contributors |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
7 | * copy of this software and associated documentation files (the "Software"), |
---|
8 | * to deal in the Software without restriction, including without limitation |
---|
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
10 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
11 | * Software is furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
22 | * DEALINGS IN THE SOFTWARE. |
---|
23 | *****************************************************************************/ |
---|
24 | |
---|
25 | #include <stdio.h> |
---|
26 | #include <stdlib.h> |
---|
27 | #include <string.h> |
---|
28 | #include <unistd.h> |
---|
29 | #include <getopt.h> |
---|
30 | #include <signal.h> |
---|
31 | |
---|
32 | #include <libtransmission/transmission.h> |
---|
33 | #include <libtransmission/makemeta.h> |
---|
34 | #include <libtransmission/metainfo.h> /* tr_metainfoFree */ |
---|
35 | #include <libtransmission/utils.h> /* tr_wait */ |
---|
36 | |
---|
37 | |
---|
38 | /* macro to shut up "unused parameter" warnings */ |
---|
39 | #ifdef __GNUC__ |
---|
40 | #define UNUSED __attribute__((unused)) |
---|
41 | #else |
---|
42 | #define UNUSED |
---|
43 | #endif |
---|
44 | |
---|
45 | const char * USAGE = |
---|
46 | "Usage: %s [-car[-m]] [-dfinpsuv] [-h] file.torrent [output-dir]\n\n" |
---|
47 | "Options:\n" |
---|
48 | " -c, --create-from <file> Create torrent from the specified source file.\n" |
---|
49 | " -a, --announce <url> Used in conjunction with -c.\n" |
---|
50 | " -r, --private Used in conjunction with -c.\n" |
---|
51 | " -m, --comment <text> Adds an optional comment when creating a torrent.\n" |
---|
52 | " -d, --download <int> Maximum download rate (-1 = no limit, default = -1)\n" |
---|
53 | " -f, --finish <shell script> Command you wish to run on completion\n" |
---|
54 | " -h, --help Print this help and exit\n" |
---|
55 | " -i, --info Print metainfo and exit\n" |
---|
56 | " -n --nat-traversal Attempt NAT traversal using NAT-PMP or UPnP IGD\n" |
---|
57 | " -p, --port <int> Port we should listen on (default = %d)\n" |
---|
58 | " -s, --scrape Print counts of seeders/leechers and exit\n" |
---|
59 | " -u, --upload <int> Maximum upload rate (-1 = no limit, default = 20)\n" |
---|
60 | " -v, --verbose <int> Verbose level (0 to 2, default = 0)\n" |
---|
61 | " -V, --version Print the version number and exit\n" |
---|
62 | " -y, --recheck Force a recheck of the torrent data\n"; |
---|
63 | |
---|
64 | static int showHelp = 0; |
---|
65 | static int showInfo = 0; |
---|
66 | static int showScrape = 0; |
---|
67 | static int showVersion = 0; |
---|
68 | static int isPrivate = 0; |
---|
69 | static int verboseLevel = 0; |
---|
70 | static int bindPort = TR_DEFAULT_PORT; |
---|
71 | static int uploadLimit = 20; |
---|
72 | static int downloadLimit = -1; |
---|
73 | static char * torrentPath = NULL; |
---|
74 | static char * savePath = "."; |
---|
75 | static int natTraversal = 0; |
---|
76 | static int recheckData = 0; |
---|
77 | static sig_atomic_t gotsig = 0; |
---|
78 | static sig_atomic_t manualUpdate = 0; |
---|
79 | |
---|
80 | static char * finishCall = NULL; |
---|
81 | static char * announce = NULL; |
---|
82 | static char * sourceFile = NULL; |
---|
83 | static char * comment = NULL; |
---|
84 | |
---|
85 | static int parseCommandLine ( int argc, char ** argv ); |
---|
86 | static void sigHandler ( int signal ); |
---|
87 | |
---|
88 | static char * |
---|
89 | getStringRatio( float ratio ) |
---|
90 | { |
---|
91 | static char string[20]; |
---|
92 | |
---|
93 | if( ratio == TR_RATIO_NA ) |
---|
94 | return "n/a"; |
---|
95 | snprintf( string, sizeof string, "%.3f", ratio ); |
---|
96 | return string; |
---|
97 | } |
---|
98 | |
---|
99 | #define LINEWIDTH 80 |
---|
100 | |
---|
101 | static void |
---|
102 | torrentStateChanged( tr_torrent * torrent UNUSED, |
---|
103 | cp_status_t status UNUSED, |
---|
104 | void * user_data UNUSED ) |
---|
105 | { |
---|
106 | system( finishCall ); |
---|
107 | } |
---|
108 | |
---|
109 | int |
---|
110 | main( int argc, char ** argv ) |
---|
111 | { |
---|
112 | int i, error; |
---|
113 | tr_handle * h; |
---|
114 | tr_ctor * ctor; |
---|
115 | tr_torrent * tor = NULL; |
---|
116 | |
---|
117 | printf( "Transmission %s - http://www.transmissionbt.com/\n", |
---|
118 | LONG_VERSION_STRING ); |
---|
119 | |
---|
120 | /* Get options */ |
---|
121 | if( parseCommandLine( argc, argv ) ) |
---|
122 | { |
---|
123 | printf( USAGE, argv[0], TR_DEFAULT_PORT ); |
---|
124 | return EXIT_FAILURE; |
---|
125 | } |
---|
126 | |
---|
127 | if( showVersion ) |
---|
128 | return EXIT_SUCCESS; |
---|
129 | |
---|
130 | if( showHelp ) |
---|
131 | { |
---|
132 | printf( USAGE, argv[0], TR_DEFAULT_PORT ); |
---|
133 | return EXIT_SUCCESS; |
---|
134 | } |
---|
135 | |
---|
136 | if( bindPort < 1 || bindPort > 65535 ) |
---|
137 | { |
---|
138 | printf( "Invalid port '%d'\n", bindPort ); |
---|
139 | return EXIT_FAILURE; |
---|
140 | } |
---|
141 | |
---|
142 | /* Initialize libtransmission */ |
---|
143 | h = tr_initFull( "cli", |
---|
144 | 1, /* pex enabled */ |
---|
145 | natTraversal, /* nat enabled */ |
---|
146 | bindPort, /* public port */ |
---|
147 | TR_ENCRYPTION_PREFERRED, /* encryption mode */ |
---|
148 | uploadLimit >= 0, /* use upload speed limit? */ |
---|
149 | uploadLimit, /* upload speed limit */ |
---|
150 | downloadLimit >= 0, /* use download speed limit? */ |
---|
151 | downloadLimit, /* download speed limit */ |
---|
152 | 512, /* globalPeerLimit */ |
---|
153 | verboseLevel + 1, /* messageLevel */ |
---|
154 | 0 ); /* is message queueing enabled? */ |
---|
155 | |
---|
156 | if( sourceFile && *sourceFile ) /* creating a torrent */ |
---|
157 | { |
---|
158 | int ret; |
---|
159 | tr_metainfo_builder * builder = tr_metaInfoBuilderCreate( h, sourceFile ); |
---|
160 | tr_makeMetaInfo( builder, torrentPath, announce, comment, isPrivate ); |
---|
161 | while( !builder->isDone ) { |
---|
162 | tr_wait( 1000 ); |
---|
163 | printf( "." ); |
---|
164 | } |
---|
165 | ret = !builder->failed; |
---|
166 | tr_metaInfoBuilderFree( builder ); |
---|
167 | return ret; |
---|
168 | } |
---|
169 | |
---|
170 | ctor = tr_ctorNew( h ); |
---|
171 | tr_ctorSetMetainfoFromFile( ctor, torrentPath ); |
---|
172 | tr_ctorSetPaused( ctor, TR_FORCE, 0 ); |
---|
173 | tr_ctorSetDestination( ctor, TR_FORCE, savePath ); |
---|
174 | |
---|
175 | if( showInfo ) |
---|
176 | { |
---|
177 | tr_info info; |
---|
178 | |
---|
179 | if( !tr_torrentParse( h, ctor, &info ) ) |
---|
180 | { |
---|
181 | printf( "hash:\t" ); |
---|
182 | for( i=0; i<SHA_DIGEST_LENGTH; ++i ) |
---|
183 | printf( "%02x", info.hash[i] ); |
---|
184 | printf( "\n" ); |
---|
185 | |
---|
186 | printf( "name:\t%s\n", info.name ); |
---|
187 | |
---|
188 | for( i=0; i<info.trackerTiers; ++i ) { |
---|
189 | int j; |
---|
190 | printf( "tracker tier #%d:\n", ( i+1 ) ); |
---|
191 | for( j=0; j<info.trackerList[i].count; ++j ) { |
---|
192 | const tr_tracker_info * tracker = &info.trackerList[i].list[j]; |
---|
193 | printf( "\taddress:\t%s:%d\n", tracker->address, tracker->port ); |
---|
194 | printf( "\tannounce:\t%s\n", tracker->announce ); |
---|
195 | printf( "\n" ); |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | printf( "size:\t%"PRIu64" (%"PRIu64" * %d + %"PRIu64")\n", |
---|
200 | info.totalSize, info.totalSize / info.pieceSize, |
---|
201 | info.pieceSize, info.totalSize % info.pieceSize ); |
---|
202 | |
---|
203 | if( info.comment[0] ) |
---|
204 | printf( "comment:\t%s\n", info.comment ); |
---|
205 | if( info.creator[0] ) |
---|
206 | printf( "creator:\t%s\n", info.creator ); |
---|
207 | if( info.isPrivate ) |
---|
208 | printf( "private flag set\n" ); |
---|
209 | |
---|
210 | printf( "file(s):\n" ); |
---|
211 | for( i=0; i<info.fileCount; ++i ) |
---|
212 | printf( "\t%s (%"PRIu64")\n", info.files[i].name, info.files[i].length ); |
---|
213 | |
---|
214 | tr_metainfoFree( &info ); |
---|
215 | } |
---|
216 | |
---|
217 | tr_ctorFree( ctor ); |
---|
218 | goto cleanup; |
---|
219 | } |
---|
220 | |
---|
221 | |
---|
222 | tor = tr_torrentNew( h, ctor, &error ); |
---|
223 | tr_ctorFree( ctor ); |
---|
224 | if( tor == NULL ) |
---|
225 | { |
---|
226 | printf( "Failed opening torrent file `%s'\n", torrentPath ); |
---|
227 | tr_close( h ); |
---|
228 | return EXIT_FAILURE; |
---|
229 | } |
---|
230 | |
---|
231 | if( showScrape ) |
---|
232 | { |
---|
233 | printf( "Scraping, Please wait...\n" ); |
---|
234 | const tr_stat * stats; |
---|
235 | |
---|
236 | uint64_t start = tr_date(); |
---|
237 | |
---|
238 | do |
---|
239 | { |
---|
240 | stats = tr_torrentStat( tor ); |
---|
241 | if( stats == NULL || tr_date() - start > 20000 ) |
---|
242 | { |
---|
243 | printf( "Scrape failed.\n" ); |
---|
244 | goto cleanup; |
---|
245 | } |
---|
246 | tr_wait( 2000 ); |
---|
247 | } |
---|
248 | while( stats->completedFromTracker == -1 || stats->leechers == -1 || stats->seeders == -1 ); |
---|
249 | |
---|
250 | printf( "%d seeder(s), %d leecher(s), %d download(s).\n", |
---|
251 | stats->seeders, stats->leechers, stats->completedFromTracker ); |
---|
252 | |
---|
253 | goto cleanup; |
---|
254 | } |
---|
255 | |
---|
256 | signal( SIGINT, sigHandler ); |
---|
257 | signal( SIGHUP, sigHandler ); |
---|
258 | |
---|
259 | tr_torrentSetStatusCallback( tor, torrentStateChanged, NULL ); |
---|
260 | tr_torrentStart( tor ); |
---|
261 | |
---|
262 | for( ;; ) |
---|
263 | { |
---|
264 | char string[LINEWIDTH]; |
---|
265 | int chars = 0; |
---|
266 | const tr_stat * s; |
---|
267 | |
---|
268 | tr_wait( 1000 ); |
---|
269 | |
---|
270 | if( gotsig ) |
---|
271 | { |
---|
272 | gotsig = 0; |
---|
273 | tr_torrentStop( tor ); |
---|
274 | tr_natTraversalEnable( h, 0 ); |
---|
275 | } |
---|
276 | |
---|
277 | if( manualUpdate ) |
---|
278 | { |
---|
279 | manualUpdate = 0; |
---|
280 | if ( !tr_torrentCanManualUpdate( tor ) ) |
---|
281 | fprintf( stderr, "\rReceived SIGHUP, but can't send a manual update now\n" ); |
---|
282 | else { |
---|
283 | fprintf( stderr, "\rReceived SIGHUP: manual update scheduled\n" ); |
---|
284 | tr_manualUpdate( tor ); |
---|
285 | } |
---|
286 | } |
---|
287 | |
---|
288 | if( recheckData ) |
---|
289 | { |
---|
290 | recheckData = 0; |
---|
291 | tr_torrentRecheck( tor ); |
---|
292 | } |
---|
293 | |
---|
294 | s = tr_torrentStat( tor ); |
---|
295 | |
---|
296 | if( s->status & TR_STATUS_CHECK_WAIT ) |
---|
297 | { |
---|
298 | chars = snprintf( string, sizeof string, |
---|
299 | "Waiting to verify local files..." ); |
---|
300 | } |
---|
301 | else if( s->status & TR_STATUS_CHECK ) |
---|
302 | { |
---|
303 | chars = snprintf( string, sizeof string, |
---|
304 | "Verifying local files... %.2f%%, found %.2f%% valid", 100 * s->recheckProgress, 100.0 * s->percentDone ); |
---|
305 | } |
---|
306 | else if( s->status & TR_STATUS_DOWNLOAD ) |
---|
307 | { |
---|
308 | chars = snprintf( string, sizeof string, |
---|
309 | "Progress: %.2f %%, %d peer%s, dl from %d (%.2f KB/s), " |
---|
310 | "ul to %d (%.2f KB/s) [%s]", 100.0 * s->percentDone, |
---|
311 | s->peersConnected, ( s->peersConnected == 1 ) ? "" : "s", |
---|
312 | s->peersSendingToUs, s->rateDownload, |
---|
313 | s->peersGettingFromUs, s->rateUpload, |
---|
314 | getStringRatio(s->ratio) ); |
---|
315 | } |
---|
316 | else if( s->status & TR_STATUS_SEED ) |
---|
317 | { |
---|
318 | chars = snprintf( string, sizeof string, |
---|
319 | "Seeding, uploading to %d of %d peer(s), %.2f KB/s [%s]", |
---|
320 | s->peersGettingFromUs, s->peersConnected, |
---|
321 | s->rateUpload, getStringRatio(s->ratio) ); |
---|
322 | } |
---|
323 | else if( s->status & TR_STATUS_STOPPED ) |
---|
324 | { |
---|
325 | break; |
---|
326 | } |
---|
327 | if( ( signed )sizeof string > chars ) |
---|
328 | { |
---|
329 | memset( &string[chars], ' ', sizeof string - 1 - chars ); |
---|
330 | } |
---|
331 | string[sizeof string - 1] = '\0'; |
---|
332 | fprintf( stderr, "\r%s", string ); |
---|
333 | |
---|
334 | if( s->error ) |
---|
335 | { |
---|
336 | fprintf( stderr, "\n%s\n", s->errorString ); |
---|
337 | } |
---|
338 | else if( verboseLevel > 0 ) |
---|
339 | { |
---|
340 | fprintf( stderr, "\n" ); |
---|
341 | } |
---|
342 | } |
---|
343 | fprintf( stderr, "\n" ); |
---|
344 | |
---|
345 | /* Try for 5 seconds to delete any port mappings for nat traversal */ |
---|
346 | tr_natTraversalEnable( h, 0 ); |
---|
347 | for( i = 0; i < 10; i++ ) |
---|
348 | { |
---|
349 | const tr_handle_status * hstat = tr_handleStatus( h ); |
---|
350 | if( TR_NAT_TRAVERSAL_UNMAPPED == hstat->natTraversalStatus ) |
---|
351 | { |
---|
352 | /* Port mappings were deleted */ |
---|
353 | break; |
---|
354 | } |
---|
355 | tr_wait( 500 ); |
---|
356 | } |
---|
357 | |
---|
358 | cleanup: |
---|
359 | tr_torrentClose( tor ); |
---|
360 | tr_close( h ); |
---|
361 | |
---|
362 | return EXIT_SUCCESS; |
---|
363 | } |
---|
364 | |
---|
365 | static int |
---|
366 | parseCommandLine( int argc, char ** argv ) |
---|
367 | { |
---|
368 | for( ;; ) |
---|
369 | { |
---|
370 | static struct option long_options[] = |
---|
371 | { { "help", no_argument, NULL, 'h' }, |
---|
372 | { "info", no_argument, NULL, 'i' }, |
---|
373 | { "scrape", no_argument, NULL, 's' }, |
---|
374 | { "private", no_argument, NULL, 'r' }, |
---|
375 | { "version", no_argument, NULL, 'V' }, |
---|
376 | { "verbose", required_argument, NULL, 'v' }, |
---|
377 | { "port", required_argument, NULL, 'p' }, |
---|
378 | { "upload", required_argument, NULL, 'u' }, |
---|
379 | { "download", required_argument, NULL, 'd' }, |
---|
380 | { "finish", required_argument, NULL, 'f' }, |
---|
381 | { "create", required_argument, NULL, 'c' }, |
---|
382 | { "comment", required_argument, NULL, 'm' }, |
---|
383 | { "announce", required_argument, NULL, 'a' }, |
---|
384 | { "nat-traversal", no_argument, NULL, 'n' }, |
---|
385 | { "recheck", no_argument, NULL, 'y' }, |
---|
386 | { "output-dir", required_argument, NULL, 'o' }, |
---|
387 | { 0, 0, 0, 0} }; |
---|
388 | |
---|
389 | int c, optind = 0; |
---|
390 | c = getopt_long( argc, argv, "hisrVv:p:u:d:f:c:m:a:no:y", |
---|
391 | long_options, &optind ); |
---|
392 | if( c < 0 ) |
---|
393 | { |
---|
394 | break; |
---|
395 | } |
---|
396 | switch( c ) |
---|
397 | { |
---|
398 | case 'h': |
---|
399 | showHelp = 1; |
---|
400 | break; |
---|
401 | case 'i': |
---|
402 | showInfo = 1; |
---|
403 | break; |
---|
404 | case 's': |
---|
405 | showScrape = 1; |
---|
406 | break; |
---|
407 | case 'r': |
---|
408 | isPrivate = 1; |
---|
409 | break; |
---|
410 | case 'v': |
---|
411 | verboseLevel = atoi( optarg ); |
---|
412 | break; |
---|
413 | case 'V': |
---|
414 | showVersion = 1; |
---|
415 | break; |
---|
416 | case 'p': |
---|
417 | bindPort = atoi( optarg ); |
---|
418 | break; |
---|
419 | case 'u': |
---|
420 | uploadLimit = atoi( optarg ); |
---|
421 | break; |
---|
422 | case 'd': |
---|
423 | downloadLimit = atoi( optarg ); |
---|
424 | break; |
---|
425 | case 'f': |
---|
426 | finishCall = optarg; |
---|
427 | break; |
---|
428 | case 'c': |
---|
429 | sourceFile = optarg; |
---|
430 | break; |
---|
431 | case 'm': |
---|
432 | comment = optarg; |
---|
433 | break; |
---|
434 | case 'a': |
---|
435 | announce = optarg; |
---|
436 | break; |
---|
437 | case 'n': |
---|
438 | natTraversal = 1; |
---|
439 | break; |
---|
440 | case 'y': |
---|
441 | recheckData = 1; |
---|
442 | break; |
---|
443 | case 'o': |
---|
444 | savePath = optarg; |
---|
445 | default: |
---|
446 | return 1; |
---|
447 | } |
---|
448 | } |
---|
449 | |
---|
450 | if( showHelp || showVersion ) |
---|
451 | return 0; |
---|
452 | |
---|
453 | if( optind >= argc ) |
---|
454 | return 1; |
---|
455 | |
---|
456 | torrentPath = argv[optind]; |
---|
457 | return 0; |
---|
458 | } |
---|
459 | |
---|
460 | static void sigHandler( int signal ) |
---|
461 | { |
---|
462 | switch( signal ) |
---|
463 | { |
---|
464 | case SIGINT: |
---|
465 | gotsig = 1; |
---|
466 | break; |
---|
467 | |
---|
468 | case SIGHUP: |
---|
469 | manualUpdate = 1; |
---|
470 | break; |
---|
471 | |
---|
472 | default: |
---|
473 | break; |
---|
474 | } |
---|
475 | } |
---|