1 | /****************************************************************************** |
---|
2 | * $Id: torrents.c 5643 2008-04-18 16:23:59Z charles $ |
---|
3 | * |
---|
4 | * Copyright (c) 2007 Joshua Elsasser |
---|
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 <sys/types.h> |
---|
26 | #include <sys/param.h> |
---|
27 | #include <sys/stat.h> |
---|
28 | #include <sys/time.h> |
---|
29 | #include <sys/uio.h> |
---|
30 | #include <assert.h> |
---|
31 | #include <ctype.h> |
---|
32 | #include <errno.h> |
---|
33 | #include <event.h> |
---|
34 | #include <fcntl.h> |
---|
35 | #include <time.h> |
---|
36 | #include <stddef.h> |
---|
37 | #include <stdio.h> |
---|
38 | #include <stdlib.h> |
---|
39 | #include <string.h> |
---|
40 | #include <unistd.h> |
---|
41 | |
---|
42 | #include <libtransmission/bencode.h> |
---|
43 | #include <libtransmission/transmission.h> |
---|
44 | #include <libtransmission/trcompat.h> |
---|
45 | |
---|
46 | #include "bsdtree.h" |
---|
47 | #include "errors.h" |
---|
48 | #include "misc.h" |
---|
49 | #include "torrents.h" |
---|
50 | |
---|
51 | #define EXIT_TIMEOUT 10 /* how many seconds to wait on exit */ |
---|
52 | #define TIMER_SECS 1 /* timer interval seconds */ |
---|
53 | #define TIMER_USECS 0 /* timer interval microseconds */ |
---|
54 | |
---|
55 | struct tor |
---|
56 | { |
---|
57 | int id; |
---|
58 | uint8_t hash[SHA_DIGEST_LENGTH]; |
---|
59 | tr_torrent * tor; |
---|
60 | RB_ENTRY( tor ) idlinks; |
---|
61 | RB_ENTRY( tor ) hashlinks; |
---|
62 | }; |
---|
63 | |
---|
64 | RB_HEAD( tortree, tor ); |
---|
65 | RB_HEAD( hashtree, tor ); |
---|
66 | |
---|
67 | static struct tor * opentor ( const char *, const char *, uint8_t *, size_t, |
---|
68 | const char * ); |
---|
69 | static void closetor ( struct tor *, int ); |
---|
70 | static void starttimer ( int ); |
---|
71 | static void timerfunc ( int, short, void * ); |
---|
72 | static int loadstate ( void ); |
---|
73 | static int savestate ( void ); |
---|
74 | |
---|
75 | static struct event_base * gl_base = NULL; |
---|
76 | static tr_handle * gl_handle = NULL; |
---|
77 | static struct tortree gl_tree = RB_INITIALIZER( &gl_tree ); |
---|
78 | static struct hashtree gl_hashes = RB_INITIALIZER( &gl_hashes ); |
---|
79 | static int gl_lastid = 0; |
---|
80 | static struct event gl_event; |
---|
81 | static time_t gl_exiting = 0; |
---|
82 | static int gl_exitval = 0; |
---|
83 | static char gl_state[MAXPATHLEN]; |
---|
84 | static char gl_newstate[MAXPATHLEN]; |
---|
85 | |
---|
86 | static int gl_autostart = 1; |
---|
87 | static int gl_pex = 1; |
---|
88 | static int gl_port = TR_DEFAULT_PORT; |
---|
89 | static int gl_mapping = 0; |
---|
90 | static int gl_uplimit = -1; |
---|
91 | static int gl_downlimit = -1; |
---|
92 | static char gl_dir[MAXPATHLEN]; |
---|
93 | static tr_encryption_mode gl_crypto = TR_ENCRYPTION_PREFERRED; |
---|
94 | |
---|
95 | static int |
---|
96 | torhashcmp( struct tor * left, struct tor * right ) |
---|
97 | { |
---|
98 | return memcmp( left->hash, right->hash, sizeof left->hash ); |
---|
99 | } |
---|
100 | |
---|
101 | RB_GENERATE_STATIC( hashtree, tor, hashlinks, torhashcmp ) |
---|
102 | |
---|
103 | INTCMP_FUNC( toridcmp, tor, id ) |
---|
104 | RB_GENERATE_STATIC( tortree, tor, idlinks, toridcmp ) |
---|
105 | |
---|
106 | void |
---|
107 | torrent_init( struct event_base * base ) |
---|
108 | { |
---|
109 | assert( NULL == gl_handle && NULL == gl_base ); |
---|
110 | |
---|
111 | gl_base = base; |
---|
112 | gl_handle = tr_init( tr_getDefaultConfigDir(), "daemon" ); |
---|
113 | |
---|
114 | confpath( gl_state, sizeof gl_state, CONF_FILE_STATE, 0 ); |
---|
115 | strlcpy( gl_newstate, gl_state, sizeof gl_state ); |
---|
116 | strlcat( gl_newstate, ".new", sizeof gl_state ); |
---|
117 | absolutify( gl_dir, sizeof gl_dir, "." ); |
---|
118 | |
---|
119 | loadstate(); |
---|
120 | } |
---|
121 | |
---|
122 | int |
---|
123 | torrent_add_file( const char * path, const char * dir, int autostart ) |
---|
124 | { |
---|
125 | struct tor * tor; |
---|
126 | |
---|
127 | assert( NULL != gl_handle ); |
---|
128 | assert( !gl_exiting ); |
---|
129 | |
---|
130 | tor = opentor( path, NULL, NULL, 0, dir ); |
---|
131 | if( NULL == tor ) |
---|
132 | { |
---|
133 | return -1; |
---|
134 | } |
---|
135 | |
---|
136 | if( 0 > autostart ) |
---|
137 | { |
---|
138 | autostart = gl_autostart; |
---|
139 | } |
---|
140 | if( autostart ) |
---|
141 | { |
---|
142 | tr_torrentStart( tor->tor ); |
---|
143 | } |
---|
144 | |
---|
145 | savestate(); |
---|
146 | |
---|
147 | return tor->id; |
---|
148 | } |
---|
149 | |
---|
150 | int |
---|
151 | torrent_add_data( uint8_t * data, size_t size, const char * dir, int autostart ) |
---|
152 | { |
---|
153 | struct tor * tor; |
---|
154 | |
---|
155 | assert( NULL != gl_handle ); |
---|
156 | assert( !gl_exiting ); |
---|
157 | |
---|
158 | tor = opentor( NULL, NULL, data, size, dir ); |
---|
159 | if( NULL == tor ) |
---|
160 | { |
---|
161 | return -1; |
---|
162 | } |
---|
163 | |
---|
164 | if( 0 > autostart ) |
---|
165 | { |
---|
166 | autostart = gl_autostart; |
---|
167 | } |
---|
168 | if( autostart ) |
---|
169 | { |
---|
170 | tr_torrentStart( tor->tor ); |
---|
171 | } |
---|
172 | |
---|
173 | savestate(); |
---|
174 | |
---|
175 | return tor->id; |
---|
176 | } |
---|
177 | |
---|
178 | static struct tor * |
---|
179 | idlookup( int id ) |
---|
180 | { |
---|
181 | struct tor * found = NULL; |
---|
182 | |
---|
183 | if( gl_handle && !gl_exiting ) |
---|
184 | { |
---|
185 | struct tor key; |
---|
186 | memset( &key, 0, sizeof key ); |
---|
187 | key.id = id; |
---|
188 | found = RB_FIND( tortree, &gl_tree, &key ); |
---|
189 | } |
---|
190 | |
---|
191 | return found; |
---|
192 | } |
---|
193 | |
---|
194 | void |
---|
195 | torrent_start( int id ) |
---|
196 | { |
---|
197 | struct tor * tor = idlookup( id ); |
---|
198 | if( tor && !TR_STATUS_IS_ACTIVE( tr_torrentStat( tor->tor )->status ) ) { |
---|
199 | tr_torrentStart( tor->tor ); |
---|
200 | savestate(); |
---|
201 | } |
---|
202 | |
---|
203 | } |
---|
204 | |
---|
205 | void |
---|
206 | torrent_stop( int id ) |
---|
207 | { |
---|
208 | struct tor * tor = idlookup( id ); |
---|
209 | if( tor && TR_STATUS_IS_ACTIVE( tr_torrentStat( tor->tor )->status ) ) { |
---|
210 | tr_torrentStop( tor->tor ); |
---|
211 | savestate( ); |
---|
212 | } |
---|
213 | } |
---|
214 | |
---|
215 | void |
---|
216 | torrent_verify( int id ) |
---|
217 | { |
---|
218 | struct tor * tor = idlookup( id ); |
---|
219 | if( tor ) |
---|
220 | tr_torrentVerify( tor->tor ); |
---|
221 | } |
---|
222 | |
---|
223 | void |
---|
224 | torrent_remove( int id ) |
---|
225 | { |
---|
226 | struct tor * tor = idlookup( id ); |
---|
227 | if( tor ) { |
---|
228 | closetor( tor, 1 ); |
---|
229 | savestate(); |
---|
230 | } |
---|
231 | } |
---|
232 | |
---|
233 | tr_torrent * |
---|
234 | torrent_handle( int id ) |
---|
235 | { |
---|
236 | const struct tor * tor = idlookup( id ); |
---|
237 | return tor ? tor->tor : NULL; |
---|
238 | } |
---|
239 | |
---|
240 | const tr_info * |
---|
241 | torrent_info( int id ) |
---|
242 | { |
---|
243 | return tr_torrentInfo( torrent_handle( id ) ); |
---|
244 | } |
---|
245 | |
---|
246 | const tr_stat * |
---|
247 | torrent_stat( int id ) |
---|
248 | { |
---|
249 | return tr_torrentStat( torrent_handle( id ) ); |
---|
250 | } |
---|
251 | |
---|
252 | static struct tor * |
---|
253 | hashlookup( const uint8_t * hash ) |
---|
254 | { |
---|
255 | struct tor key, * found; |
---|
256 | |
---|
257 | memset( &key, 0, sizeof key ); |
---|
258 | memcpy( key.hash, hash, sizeof key.hash ); |
---|
259 | found = RB_FIND( hashtree, &gl_hashes, &key ); |
---|
260 | |
---|
261 | return found; |
---|
262 | } |
---|
263 | |
---|
264 | int |
---|
265 | torrent_lookup( const uint8_t * hashstr ) |
---|
266 | { |
---|
267 | uint8_t hash[SHA_DIGEST_LENGTH]; |
---|
268 | size_t ii; |
---|
269 | struct tor * tor; |
---|
270 | char buf[3]; |
---|
271 | |
---|
272 | assert( NULL != gl_handle ); |
---|
273 | assert( !gl_exiting ); |
---|
274 | |
---|
275 | memset( buf, 0, sizeof buf ); |
---|
276 | for( ii = 0; sizeof( hash ) > ii; ii++ ) |
---|
277 | { |
---|
278 | if( !isxdigit( hashstr[2*ii] ) || !isxdigit( hashstr[1+2*ii] ) ) |
---|
279 | { |
---|
280 | return -1; |
---|
281 | } |
---|
282 | memcpy( buf, &hashstr[2*ii], 2 ); |
---|
283 | hash[ii] = strtol( buf, NULL, 16 ); |
---|
284 | } |
---|
285 | |
---|
286 | tor = hashlookup( hash ); |
---|
287 | if( NULL == tor ) |
---|
288 | { |
---|
289 | return -1; |
---|
290 | } |
---|
291 | |
---|
292 | return tor->id; |
---|
293 | } |
---|
294 | |
---|
295 | static struct tor * |
---|
296 | iterate( struct tor * tor ) |
---|
297 | { |
---|
298 | struct tor * next = NULL; |
---|
299 | |
---|
300 | if( gl_handle && !gl_exiting ) |
---|
301 | next = tor ? RB_NEXT( tortree, &gl_tree, tor ) |
---|
302 | : RB_MIN( tortree, &gl_tree ); |
---|
303 | |
---|
304 | return next; |
---|
305 | } |
---|
306 | |
---|
307 | void * |
---|
308 | torrent_iter( void * cur, int * id ) |
---|
309 | { |
---|
310 | struct tor * next = iterate( cur ); |
---|
311 | if( next ) |
---|
312 | *id = next->id; |
---|
313 | return next; |
---|
314 | } |
---|
315 | |
---|
316 | void |
---|
317 | torrent_exit( int exitval ) |
---|
318 | { |
---|
319 | struct tor * tor; |
---|
320 | |
---|
321 | assert( NULL != gl_handle ); |
---|
322 | assert( !gl_exiting ); |
---|
323 | gl_exiting = time( NULL ); |
---|
324 | gl_exitval = exitval; |
---|
325 | |
---|
326 | RB_FOREACH( tor, tortree, &gl_tree ) |
---|
327 | { |
---|
328 | closetor( tor, 0 ); |
---|
329 | } |
---|
330 | |
---|
331 | tr_natTraversalEnable( gl_handle, 0 ); |
---|
332 | starttimer( 1 ); |
---|
333 | } |
---|
334 | |
---|
335 | void |
---|
336 | torrent_set_autostart( int autostart ) |
---|
337 | { |
---|
338 | assert( NULL != gl_handle ); |
---|
339 | assert( !gl_exiting ); |
---|
340 | gl_autostart = autostart; |
---|
341 | savestate(); |
---|
342 | } |
---|
343 | |
---|
344 | int |
---|
345 | torrent_get_autostart( void ) |
---|
346 | { |
---|
347 | return gl_autostart; |
---|
348 | } |
---|
349 | |
---|
350 | void |
---|
351 | torrent_set_port( int port ) |
---|
352 | { |
---|
353 | assert( NULL != gl_handle ); |
---|
354 | assert( !gl_exiting ); |
---|
355 | if( 0 < port && 0xffff > port ) |
---|
356 | { |
---|
357 | gl_port = port; |
---|
358 | tr_setBindPort( gl_handle, port ); |
---|
359 | savestate(); |
---|
360 | } |
---|
361 | } |
---|
362 | |
---|
363 | int |
---|
364 | torrent_get_port( void ) |
---|
365 | { |
---|
366 | return gl_port; |
---|
367 | } |
---|
368 | |
---|
369 | void |
---|
370 | torrent_set_pex( int pex ) |
---|
371 | { |
---|
372 | assert( NULL != gl_handle ); |
---|
373 | assert( !gl_exiting ); |
---|
374 | |
---|
375 | if( gl_pex != pex ) |
---|
376 | { |
---|
377 | gl_pex = pex; |
---|
378 | |
---|
379 | tr_setPexEnabled( gl_handle, gl_pex ); |
---|
380 | |
---|
381 | savestate( ); |
---|
382 | } |
---|
383 | } |
---|
384 | |
---|
385 | int |
---|
386 | torrent_get_pex( void ) |
---|
387 | { |
---|
388 | return gl_pex; |
---|
389 | } |
---|
390 | |
---|
391 | void |
---|
392 | torrent_enable_port_mapping( int automap ) |
---|
393 | { |
---|
394 | assert( NULL != gl_handle ); |
---|
395 | assert( !gl_exiting ); |
---|
396 | gl_mapping = ( automap ? 1 : 0 ); |
---|
397 | tr_natTraversalEnable( gl_handle, gl_mapping ); |
---|
398 | savestate(); |
---|
399 | } |
---|
400 | |
---|
401 | int |
---|
402 | torrent_get_port_mapping( void ) |
---|
403 | { |
---|
404 | return gl_mapping; |
---|
405 | } |
---|
406 | |
---|
407 | void |
---|
408 | torrent_set_uplimit( int uplimit ) |
---|
409 | { |
---|
410 | assert( NULL != gl_handle ); |
---|
411 | assert( !gl_exiting ); |
---|
412 | gl_uplimit = uplimit; |
---|
413 | tr_setGlobalSpeedLimit ( gl_handle, TR_UP, uplimit ); |
---|
414 | tr_setUseGlobalSpeedLimit( gl_handle, TR_UP, uplimit > 0 ); |
---|
415 | savestate(); |
---|
416 | } |
---|
417 | |
---|
418 | int |
---|
419 | torrent_get_uplimit( void ) |
---|
420 | { |
---|
421 | return gl_uplimit; |
---|
422 | } |
---|
423 | |
---|
424 | void |
---|
425 | torrent_set_downlimit( int downlimit ) |
---|
426 | { |
---|
427 | assert( NULL != gl_handle ); |
---|
428 | assert( !gl_exiting ); |
---|
429 | gl_downlimit = downlimit; |
---|
430 | tr_setGlobalSpeedLimit ( gl_handle, TR_DOWN, downlimit ); |
---|
431 | tr_setUseGlobalSpeedLimit( gl_handle, TR_DOWN, downlimit > 0 ); |
---|
432 | savestate(); |
---|
433 | } |
---|
434 | |
---|
435 | int |
---|
436 | torrent_get_downlimit( void ) |
---|
437 | { |
---|
438 | return gl_downlimit; |
---|
439 | } |
---|
440 | |
---|
441 | void |
---|
442 | torrent_set_directory( const char * path ) |
---|
443 | { |
---|
444 | assert( NULL != gl_handle ); |
---|
445 | assert( !gl_exiting ); |
---|
446 | |
---|
447 | absolutify( gl_dir, sizeof gl_dir, path ); |
---|
448 | savestate(); |
---|
449 | } |
---|
450 | |
---|
451 | const char * |
---|
452 | torrent_get_directory( void ) |
---|
453 | { |
---|
454 | return gl_dir; |
---|
455 | } |
---|
456 | |
---|
457 | void |
---|
458 | torrent_set_encryption(tr_encryption_mode mode) |
---|
459 | { |
---|
460 | tr_setEncryptionMode(gl_handle, mode); |
---|
461 | gl_crypto = mode; |
---|
462 | savestate(); |
---|
463 | } |
---|
464 | |
---|
465 | tr_encryption_mode |
---|
466 | torrent_get_encryption(void) |
---|
467 | { |
---|
468 | return tr_getEncryptionMode(gl_handle); |
---|
469 | } |
---|
470 | |
---|
471 | struct tor * |
---|
472 | opentor( const char * path, const char * hash, uint8_t * data, size_t size, |
---|
473 | const char * dir ) |
---|
474 | { |
---|
475 | struct tor * tor, * found; |
---|
476 | int errcode; |
---|
477 | const tr_info * inf; |
---|
478 | tr_ctor * ctor; |
---|
479 | |
---|
480 | assert( ( NULL != path && NULL == hash && NULL == data ) || |
---|
481 | ( NULL == path && NULL != hash && NULL == data ) || |
---|
482 | ( NULL == path && NULL == hash && NULL != data ) ); |
---|
483 | |
---|
484 | /* XXX should probably wrap around back to 1 and avoid duplicates */ |
---|
485 | if( INT_MAX == gl_lastid ) |
---|
486 | { |
---|
487 | errmsg( "Congratulations, you're the %ith torrent! Your prize the " |
---|
488 | "inability to load any more torrents, enjoy!", INT_MAX ); |
---|
489 | return NULL; |
---|
490 | } |
---|
491 | |
---|
492 | tor = calloc( 1, sizeof *tor ); |
---|
493 | if( NULL == tor ) |
---|
494 | { |
---|
495 | mallocmsg( sizeof *tor ); |
---|
496 | return NULL; |
---|
497 | } |
---|
498 | |
---|
499 | if( dir == NULL ) |
---|
500 | dir = gl_dir; |
---|
501 | |
---|
502 | ctor = tr_ctorNew( gl_handle ); |
---|
503 | tr_ctorSetPaused( ctor, TR_FORCE, 1 ); |
---|
504 | tr_ctorSetDestination( ctor, TR_FORCE, dir ); |
---|
505 | if( path != NULL ) |
---|
506 | tr_ctorSetMetainfoFromFile( ctor, path ); |
---|
507 | else if( hash != NULL ) |
---|
508 | tr_ctorSetMetainfoFromHash( ctor, hash ); |
---|
509 | else |
---|
510 | tr_ctorSetMetainfo( ctor, data, size ); |
---|
511 | tor->tor = tr_torrentNew( gl_handle, ctor, &errcode ); |
---|
512 | tr_ctorFree( ctor ); |
---|
513 | |
---|
514 | if( NULL == tor->tor ) |
---|
515 | { |
---|
516 | found = NULL; |
---|
517 | switch( errcode ) |
---|
518 | { |
---|
519 | case TR_EINVALID: |
---|
520 | if( NULL == path ) |
---|
521 | { |
---|
522 | errmsg( "invalid torrent file" ); |
---|
523 | } |
---|
524 | else |
---|
525 | { |
---|
526 | errmsg( "invalid torrent file: %s", path ); |
---|
527 | } |
---|
528 | break; |
---|
529 | case TR_EUNSUPPORTED: |
---|
530 | if( NULL == path ) |
---|
531 | { |
---|
532 | errmsg( "unsupported torrent file" ); |
---|
533 | } |
---|
534 | else |
---|
535 | { |
---|
536 | errmsg( "unsupported torrent file: %s", path ); |
---|
537 | } |
---|
538 | break; |
---|
539 | case TR_EDUPLICATE: |
---|
540 | /* XXX not yet |
---|
541 | found = hashlookup( tor->hash, 1 ); |
---|
542 | assert( NULL != found ); |
---|
543 | found->deleting = 0; |
---|
544 | */ |
---|
545 | errmsg( "XXX loaded duplicate torrent" ); |
---|
546 | break; |
---|
547 | default: |
---|
548 | if( NULL == path ) |
---|
549 | { |
---|
550 | errmsg( "torrent file failed to load" ); |
---|
551 | } |
---|
552 | else |
---|
553 | { |
---|
554 | errmsg( "torrent file failed to load: %s", path ); |
---|
555 | } |
---|
556 | break; |
---|
557 | } |
---|
558 | free( tor ); |
---|
559 | return found; |
---|
560 | } |
---|
561 | gl_lastid++; |
---|
562 | tor->id = gl_lastid; |
---|
563 | |
---|
564 | assert( sizeof( inf->hash ) == sizeof( tor->hash ) ); |
---|
565 | inf = tr_torrentInfo( tor->tor ); |
---|
566 | memcpy( tor->hash, inf->hash, sizeof tor->hash ); |
---|
567 | |
---|
568 | found = RB_INSERT( tortree, &gl_tree, tor ); |
---|
569 | assert( NULL == found ); |
---|
570 | found = RB_INSERT( hashtree, &gl_hashes, tor ); |
---|
571 | assert( NULL == found ); |
---|
572 | |
---|
573 | return tor; |
---|
574 | } |
---|
575 | |
---|
576 | static void |
---|
577 | freetor( struct tor * tor ) |
---|
578 | { |
---|
579 | tr_torrentClose( tor->tor ); |
---|
580 | RB_REMOVE( tortree, &gl_tree, tor ); |
---|
581 | RB_REMOVE( hashtree, &gl_hashes, tor ); |
---|
582 | free( tor ); |
---|
583 | } |
---|
584 | |
---|
585 | void |
---|
586 | closetor( struct tor * tor, int calltimer ) |
---|
587 | { |
---|
588 | if( NULL != tor ) |
---|
589 | { |
---|
590 | freetor( tor ); |
---|
591 | |
---|
592 | starttimer( calltimer ); |
---|
593 | } |
---|
594 | } |
---|
595 | |
---|
596 | void |
---|
597 | starttimer( int callnow ) |
---|
598 | { |
---|
599 | if( !evtimer_initialized( &gl_event ) ) |
---|
600 | { |
---|
601 | evtimer_set( &gl_event, timerfunc, NULL ); |
---|
602 | event_base_set( gl_base, &gl_event ); |
---|
603 | } |
---|
604 | |
---|
605 | if( callnow ) |
---|
606 | { |
---|
607 | timerfunc( -1, EV_TIMEOUT, NULL ); |
---|
608 | } |
---|
609 | } |
---|
610 | |
---|
611 | static void |
---|
612 | timerfunc( int fd UNUSED, short event UNUSED, void * arg UNUSED ) |
---|
613 | { |
---|
614 | struct tor * tor, * next; |
---|
615 | const tr_handle_status * hs; |
---|
616 | int stillmore; |
---|
617 | struct timeval tv; |
---|
618 | |
---|
619 | /* true if we've still got live torrents... */ |
---|
620 | stillmore = tr_torrentCount( gl_handle ) != 0; |
---|
621 | |
---|
622 | if( gl_exiting ) |
---|
623 | { |
---|
624 | if( !stillmore ) |
---|
625 | { |
---|
626 | hs = tr_handleStatus( gl_handle ); |
---|
627 | if( TR_NAT_TRAVERSAL_UNMAPPED != hs->natTraversalStatus ) |
---|
628 | { |
---|
629 | stillmore = 1; |
---|
630 | } |
---|
631 | } |
---|
632 | |
---|
633 | if( !stillmore || EXIT_TIMEOUT <= time( NULL ) - gl_exiting ) |
---|
634 | { |
---|
635 | if( stillmore ) |
---|
636 | { |
---|
637 | errmsg( "timing out trackers and/or port mapping on exit" ); |
---|
638 | } |
---|
639 | for( tor = RB_MIN( tortree, &gl_tree ); NULL != tor; tor = next ) |
---|
640 | { |
---|
641 | next = RB_NEXT( tortree, &gl_tree, tor ); |
---|
642 | freetor( tor ); |
---|
643 | } |
---|
644 | tr_close( gl_handle ); |
---|
645 | exit( gl_exitval ); |
---|
646 | } |
---|
647 | } |
---|
648 | |
---|
649 | if( stillmore ) |
---|
650 | { |
---|
651 | memset( &tv, 0, sizeof tv ); |
---|
652 | tv.tv_sec = TIMER_SECS; |
---|
653 | tv.tv_usec = TIMER_USECS; |
---|
654 | evtimer_add( &gl_event, &tv ); |
---|
655 | } |
---|
656 | } |
---|
657 | |
---|
658 | int |
---|
659 | loadstate( void ) |
---|
660 | { |
---|
661 | const char * str; |
---|
662 | int64_t tmp; |
---|
663 | benc_val_t top, * list; |
---|
664 | int ii; |
---|
665 | struct tor * tor; |
---|
666 | |
---|
667 | if( tr_bencLoadFile( gl_state, &top ) ) |
---|
668 | { |
---|
669 | errmsg( "failed to load bencoded data from %s", gl_state ); |
---|
670 | return -1; |
---|
671 | } |
---|
672 | |
---|
673 | if( tr_bencDictFindInt( &top, "autostart", &tmp ) ) |
---|
674 | gl_autostart = tmp != 0; |
---|
675 | |
---|
676 | if( tr_bencDictFindInt( &top, "port", &tmp ) && ( 0 < tmp ) && ( tmp <= 0xffff ) ) |
---|
677 | gl_port = tmp; |
---|
678 | tr_setBindPort( gl_handle, gl_port ); |
---|
679 | |
---|
680 | if( tr_bencDictFindInt( &top, "default-pex", &tmp ) ) |
---|
681 | gl_pex = tmp != 0; |
---|
682 | |
---|
683 | if( tr_bencDictFindInt( &top, "port-mapping", &tmp ) ) |
---|
684 | gl_mapping = tmp != 0; |
---|
685 | tr_natTraversalEnable( gl_handle, gl_mapping ); |
---|
686 | |
---|
687 | if( tr_bencDictFindInt( &top, "upload-limit", &tmp ) ) |
---|
688 | gl_uplimit = tmp; |
---|
689 | tr_setGlobalSpeedLimit( gl_handle, TR_UP, gl_uplimit ); |
---|
690 | tr_setUseGlobalSpeedLimit( gl_handle, TR_UP, gl_uplimit > 0 ); |
---|
691 | |
---|
692 | if( tr_bencDictFindInt( &top, "download-limit", &tmp ) ) |
---|
693 | gl_downlimit = tmp; |
---|
694 | tr_setGlobalSpeedLimit( gl_handle, TR_DOWN, gl_downlimit ); |
---|
695 | tr_setUseGlobalSpeedLimit( gl_handle, TR_DOWN, gl_downlimit > 0 ); |
---|
696 | |
---|
697 | if( tr_bencDictFindStr( &top, "default-directory", &str ) ) |
---|
698 | strlcpy( gl_dir, str, sizeof gl_dir ); |
---|
699 | |
---|
700 | if( tr_bencDictFindStr( &top, "encryption-mode", &str ) ) |
---|
701 | { |
---|
702 | if(!strcasecmp( str, "preferred")) |
---|
703 | gl_crypto = TR_ENCRYPTION_PREFERRED; |
---|
704 | else if(!strcasecmp( str, "required")) |
---|
705 | gl_crypto = TR_ENCRYPTION_REQUIRED; |
---|
706 | } |
---|
707 | |
---|
708 | tr_setEncryptionMode(gl_handle, gl_crypto); |
---|
709 | |
---|
710 | list = tr_bencDictFind( &top, "torrents" ); |
---|
711 | if( !tr_bencIsList( list ) ) |
---|
712 | return 0; |
---|
713 | |
---|
714 | for( ii = 0; ii < list->val.l.count; ii++ ) |
---|
715 | { |
---|
716 | const char * directory = NULL; |
---|
717 | const char * hash = NULL; |
---|
718 | |
---|
719 | tr_benc * dict = &list->val.l.vals[ii]; |
---|
720 | if( !tr_bencIsDict( dict ) ) |
---|
721 | continue; |
---|
722 | |
---|
723 | if( !tr_bencDictFindStr( dict, "directory", &directory ) || |
---|
724 | !tr_bencDictFindStr( dict, "hash", &hash ) ) |
---|
725 | continue; |
---|
726 | |
---|
727 | tor = opentor( NULL, hash, NULL, 0, directory ); |
---|
728 | if( !tor ) |
---|
729 | continue; |
---|
730 | |
---|
731 | if( tr_bencDictFindInt( dict, "pex", &tmp ) ) |
---|
732 | fprintf( stderr, "warning: obsolete command 'pex'\n" ); |
---|
733 | |
---|
734 | if( tr_bencDictFindInt( dict, "paused", &tmp ) && !tmp ) |
---|
735 | tr_torrentStart( tor->tor ); |
---|
736 | } |
---|
737 | |
---|
738 | return 0; |
---|
739 | } |
---|
740 | |
---|
741 | int |
---|
742 | savestate( void ) |
---|
743 | { |
---|
744 | benc_val_t top, * list; |
---|
745 | struct tor * ii; |
---|
746 | int torrentCount; |
---|
747 | |
---|
748 | torrentCount = 0; |
---|
749 | RB_FOREACH( ii, tortree, &gl_tree ) |
---|
750 | ++torrentCount; |
---|
751 | |
---|
752 | tr_bencInitDict( &top, 9 ); |
---|
753 | tr_bencDictAddInt( &top, "autostart", gl_autostart ); |
---|
754 | tr_bencDictAddInt( &top, "port", gl_port ); |
---|
755 | tr_bencDictAddInt( &top, "default-pex", gl_pex ); |
---|
756 | tr_bencDictAddInt( &top, "port-mapping", gl_mapping ); |
---|
757 | tr_bencDictAddInt( &top, "upload-limit", gl_uplimit ); |
---|
758 | tr_bencDictAddInt( &top, "download-limit", gl_downlimit ); |
---|
759 | tr_bencDictAddStr( &top, "default-directory", gl_dir ); |
---|
760 | tr_bencDictAddStr( &top, "encryption-mode", TR_ENCRYPTION_REQUIRED == gl_crypto |
---|
761 | ? "required" : "preferred" ); |
---|
762 | list = tr_bencDictAddList( &top, "torrents", torrentCount ); |
---|
763 | |
---|
764 | RB_FOREACH( ii, tortree, &gl_tree ) |
---|
765 | { |
---|
766 | const tr_info * inf = tr_torrentInfo( ii->tor ); |
---|
767 | const tr_stat * st = tr_torrentStat( ii->tor ); |
---|
768 | tr_benc * tor = tr_bencListAddDict( list, 3 ); |
---|
769 | tr_bencDictAddStr( tor, "hash", inf->hashString ); |
---|
770 | tr_bencDictAddInt( tor, "paused", !TR_STATUS_IS_ACTIVE( st->status ) ); |
---|
771 | tr_bencDictAddStr( tor, "directory", tr_torrentGetFolder( ii->tor ) ); |
---|
772 | } |
---|
773 | |
---|
774 | if( tr_bencSaveFile( gl_newstate, &top ) ) |
---|
775 | { |
---|
776 | errnomsg( "failed to save state: failed to write to %s", gl_newstate ); |
---|
777 | return -1; |
---|
778 | } |
---|
779 | |
---|
780 | if( 0 > rename( gl_newstate, gl_state ) ) |
---|
781 | { |
---|
782 | errnomsg( "failed to save state: failed to rename %s to %s", |
---|
783 | gl_newstate, CONF_FILE_STATE ); |
---|
784 | return -1; |
---|
785 | } |
---|
786 | |
---|
787 | return 0; |
---|
788 | } |
---|