1 | /* |
---|
2 | * This file Copyright (C) 2007-2008 Charles Kerr <charles@transmissionbt.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: tracker.c 7541 2008-12-30 02:43:51Z charles $ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <assert.h> |
---|
14 | #include <stdlib.h> |
---|
15 | #include <string.h> /* strcmp, strchr */ |
---|
16 | |
---|
17 | #include <event.h> |
---|
18 | |
---|
19 | #include "transmission.h" |
---|
20 | #include "session.h" |
---|
21 | #include "bencode.h" |
---|
22 | #include "crypto.h" |
---|
23 | #include "completion.h" |
---|
24 | #include "net.h" |
---|
25 | #include "publish.h" |
---|
26 | #include "resume.h" |
---|
27 | #include "torrent.h" |
---|
28 | #include "tracker.h" |
---|
29 | #include "trevent.h" |
---|
30 | #include "utils.h" |
---|
31 | #include "web.h" |
---|
32 | |
---|
33 | enum |
---|
34 | { |
---|
35 | /* the announceAt fields are set to this when the action is disabled */ |
---|
36 | TR_TRACKER_STOPPED = 0, |
---|
37 | |
---|
38 | /* the announceAt fields are set to this when the action is in progress */ |
---|
39 | TR_TRACKER_BUSY = 1, |
---|
40 | |
---|
41 | HTTP_OK = 200, |
---|
42 | |
---|
43 | /* seconds between tracker pulses */ |
---|
44 | PULSE_INTERVAL_MSEC = 1000, |
---|
45 | |
---|
46 | /* unless the tracker says otherwise, rescrape this frequently */ |
---|
47 | DEFAULT_SCRAPE_INTERVAL_SEC = ( 60 * 15 ), |
---|
48 | |
---|
49 | /* unless the tracker says otherwise, this is the announce interval */ |
---|
50 | DEFAULT_ANNOUNCE_INTERVAL_SEC = ( 60 * 4 ), |
---|
51 | |
---|
52 | /* unless the tracker says otherwise, this is the announce min_interval */ |
---|
53 | DEFAULT_ANNOUNCE_MIN_INTERVAL_SEC = ( 60 * 2 ), |
---|
54 | |
---|
55 | /* how long to wait before a rescrape the first time we get an error */ |
---|
56 | FIRST_SCRAPE_RETRY_INTERVAL_SEC = 30, |
---|
57 | |
---|
58 | /* how long to wait before a reannounce the first time we get an error */ |
---|
59 | FIRST_ANNOUNCE_RETRY_INTERVAL_SEC = 30, |
---|
60 | |
---|
61 | /* the value of the 'numwant' argument passed in tracker requests. */ |
---|
62 | NUMWANT = 80, |
---|
63 | |
---|
64 | /* the length of the 'key' argument passed in tracker requests */ |
---|
65 | KEYLEN = 10 |
---|
66 | }; |
---|
67 | |
---|
68 | /** |
---|
69 | *** |
---|
70 | **/ |
---|
71 | |
---|
72 | struct tr_tracker |
---|
73 | { |
---|
74 | tr_bool isRunning; |
---|
75 | |
---|
76 | uint8_t randOffset; |
---|
77 | |
---|
78 | /* sent as the "key" argument in tracker requests |
---|
79 | to verify us if our IP address changes. |
---|
80 | This is immutable for the life of the tracker object. */ |
---|
81 | char key_param[KEYLEN + 1]; |
---|
82 | |
---|
83 | /* these are set from the latest scrape or tracker response */ |
---|
84 | int announceIntervalSec; |
---|
85 | int announceMinIntervalSec; |
---|
86 | int scrapeIntervalSec; |
---|
87 | int retryScrapeIntervalSec; |
---|
88 | int retryAnnounceIntervalSec; |
---|
89 | |
---|
90 | /* index into the torrent's tr_info.trackers array */ |
---|
91 | int trackerIndex; |
---|
92 | |
---|
93 | tr_session * session; |
---|
94 | |
---|
95 | tr_publisher publisher; |
---|
96 | |
---|
97 | /* torrent hash string */ |
---|
98 | uint8_t hash[SHA_DIGEST_LENGTH]; |
---|
99 | char escaped[SHA_DIGEST_LENGTH * 3 + 1]; |
---|
100 | char * name; |
---|
101 | int torrentId; |
---|
102 | |
---|
103 | /* corresponds to the peer_id sent as a tracker request parameter. |
---|
104 | one tracker admin says: "When the same torrent is opened and |
---|
105 | closed and opened again without quitting Transmission ... |
---|
106 | change the peerid. It would help sometimes if a stopped event |
---|
107 | was missed to ensure that we didn't think someone was cheating. */ |
---|
108 | uint8_t * peer_id; |
---|
109 | |
---|
110 | /* these are set from the latest tracker response... -1 is 'unknown' */ |
---|
111 | int timesDownloaded; |
---|
112 | int seederCount; |
---|
113 | int downloaderCount; |
---|
114 | int leecherCount; |
---|
115 | char * trackerID; |
---|
116 | |
---|
117 | time_t manualAnnounceAllowedAt; |
---|
118 | time_t reannounceAt; |
---|
119 | |
---|
120 | /* 0==never, 1==in progress, other values==when to scrape */ |
---|
121 | time_t scrapeAt; |
---|
122 | |
---|
123 | time_t lastScrapeTime; |
---|
124 | long lastScrapeResponse; |
---|
125 | |
---|
126 | time_t lastAnnounceTime; |
---|
127 | long lastAnnounceResponse; |
---|
128 | }; |
---|
129 | |
---|
130 | #define dbgmsg( name, ... ) \ |
---|
131 | do { \ |
---|
132 | if( tr_deepLoggingIsActive( ) ) \ |
---|
133 | tr_deepLog( __FILE__, __LINE__, name, __VA_ARGS__ ); \ |
---|
134 | } while( 0 ) |
---|
135 | |
---|
136 | /*** |
---|
137 | **** |
---|
138 | ***/ |
---|
139 | |
---|
140 | static const tr_tracker_info * |
---|
141 | getCurrentAddressFromTorrent( tr_tracker * t, |
---|
142 | const tr_torrent * tor ) |
---|
143 | { |
---|
144 | /* user might have removed trackers, |
---|
145 | * so check to make sure our current index is in-bounds */ |
---|
146 | if( t->trackerIndex >= tor->info.trackerCount ) |
---|
147 | t->trackerIndex = 0; |
---|
148 | |
---|
149 | assert( t->trackerIndex >= 0 ); |
---|
150 | assert( t->trackerIndex < tor->info.trackerCount ); |
---|
151 | return tor->info.trackers + t->trackerIndex; |
---|
152 | } |
---|
153 | |
---|
154 | static const tr_tracker_info * |
---|
155 | getCurrentAddress( tr_tracker * t ) |
---|
156 | { |
---|
157 | const tr_torrent * torrent; |
---|
158 | |
---|
159 | if( ( torrent = tr_torrentFindFromHash( t->session, t->hash ) ) ) |
---|
160 | return getCurrentAddressFromTorrent( t, torrent ); |
---|
161 | return NULL; |
---|
162 | } |
---|
163 | |
---|
164 | static int |
---|
165 | trackerSupportsScrape( tr_tracker * t, |
---|
166 | const tr_torrent * tor ) |
---|
167 | { |
---|
168 | const tr_tracker_info * info = getCurrentAddressFromTorrent( t, tor ); |
---|
169 | |
---|
170 | return info && info->scrape; |
---|
171 | } |
---|
172 | |
---|
173 | /*** |
---|
174 | **** |
---|
175 | ***/ |
---|
176 | |
---|
177 | static tr_tracker * |
---|
178 | findTracker( tr_session * session, int torrentId ) |
---|
179 | { |
---|
180 | tr_torrent * torrent = tr_torrentFindFromId( session, torrentId ); |
---|
181 | |
---|
182 | return torrent ? torrent->tracker : NULL; |
---|
183 | } |
---|
184 | |
---|
185 | /*** |
---|
186 | **** PUBLISH |
---|
187 | ***/ |
---|
188 | |
---|
189 | static const tr_tracker_event emptyEvent = { 0, NULL, NULL, 0, 0 }; |
---|
190 | |
---|
191 | static void |
---|
192 | publishMessage( tr_tracker * t, |
---|
193 | const char * msg, |
---|
194 | int type ) |
---|
195 | { |
---|
196 | if( t ) |
---|
197 | { |
---|
198 | tr_tracker_event event = emptyEvent; |
---|
199 | event.messageType = type; |
---|
200 | event.text = msg; |
---|
201 | tr_publisherPublish( &t->publisher, t, &event ); |
---|
202 | } |
---|
203 | } |
---|
204 | |
---|
205 | static void |
---|
206 | publishErrorClear( tr_tracker * t ) |
---|
207 | { |
---|
208 | publishMessage( t, NULL, TR_TRACKER_ERROR_CLEAR ); |
---|
209 | } |
---|
210 | |
---|
211 | static void |
---|
212 | publishErrorMessageAndStop( tr_tracker * t, |
---|
213 | const char * msg ) |
---|
214 | { |
---|
215 | t->isRunning = 0; |
---|
216 | publishMessage( t, msg, TR_TRACKER_ERROR ); |
---|
217 | } |
---|
218 | |
---|
219 | static void |
---|
220 | publishWarning( tr_tracker * t, |
---|
221 | const char * msg ) |
---|
222 | { |
---|
223 | publishMessage( t, msg, TR_TRACKER_WARNING ); |
---|
224 | } |
---|
225 | |
---|
226 | static void |
---|
227 | publishNewPeers( tr_tracker * t, |
---|
228 | int allAreSeeds, |
---|
229 | void * compact, |
---|
230 | int compactLen ) |
---|
231 | { |
---|
232 | tr_tracker_event event = emptyEvent; |
---|
233 | |
---|
234 | event.messageType = TR_TRACKER_PEERS; |
---|
235 | event.allAreSeeds = allAreSeeds; |
---|
236 | event.compact = compact; |
---|
237 | event.compactLen = compactLen; |
---|
238 | if( compactLen ) |
---|
239 | tr_publisherPublish( &t->publisher, t, &event ); |
---|
240 | } |
---|
241 | |
---|
242 | static void |
---|
243 | publishNewPeersCompact( tr_tracker * t, |
---|
244 | int allAreSeeds, |
---|
245 | void * compact, |
---|
246 | int compactLen ) |
---|
247 | { |
---|
248 | int i; |
---|
249 | uint8_t *array, *walk, *compactWalk; |
---|
250 | const int peerCount = compactLen / 6; |
---|
251 | const int arrayLen = peerCount * ( sizeof( tr_address ) + 2 ); |
---|
252 | tr_address addr; |
---|
253 | tr_port port; |
---|
254 | |
---|
255 | addr.type = TR_AF_INET; |
---|
256 | memset( &addr.addr, 0x00, sizeof( addr.addr ) ); |
---|
257 | array = tr_new( uint8_t, arrayLen ); |
---|
258 | for ( i = 0, walk = array, compactWalk = compact ; i < peerCount ; i++ ) |
---|
259 | { |
---|
260 | memcpy( &addr.addr.addr4, compactWalk, 4 ); |
---|
261 | memcpy( &port, compactWalk + 4, 2 ); |
---|
262 | tr_suspectAddress( &addr, "compact" ); |
---|
263 | |
---|
264 | memcpy( walk, &addr, sizeof( addr ) ); |
---|
265 | memcpy( walk + sizeof( addr ), &port, 2 ); |
---|
266 | |
---|
267 | walk += sizeof( tr_address ) + 2; |
---|
268 | compactWalk += 6; |
---|
269 | } |
---|
270 | publishNewPeers( t, allAreSeeds, array, arrayLen ); |
---|
271 | tr_free( array ); |
---|
272 | } |
---|
273 | |
---|
274 | static void |
---|
275 | publishNewPeersCompact6( tr_tracker * t, |
---|
276 | int allAreSeeds, |
---|
277 | void * compact, |
---|
278 | int compactLen ) |
---|
279 | { |
---|
280 | int i; |
---|
281 | uint8_t *array, *walk, *compactWalk; |
---|
282 | const int peerCount = compactLen / 18; |
---|
283 | const int arrayLen = peerCount * ( sizeof( tr_address ) + 2 ); |
---|
284 | tr_address addr; |
---|
285 | tr_port port; |
---|
286 | |
---|
287 | addr.type = TR_AF_INET6; |
---|
288 | memset( &addr.addr, 0x00, sizeof( addr.addr ) ); |
---|
289 | array = tr_new( uint8_t, arrayLen ); |
---|
290 | for ( i = 0, walk = array, compactWalk = compact ; i < peerCount ; i++ ) |
---|
291 | { |
---|
292 | memcpy( &addr.addr.addr6, compactWalk, 16 ); |
---|
293 | memcpy( &port, compactWalk + 16, 2 ); |
---|
294 | tr_suspectAddress( &addr, "compact6" ); |
---|
295 | |
---|
296 | memcpy( walk, &addr, sizeof( addr ) ); |
---|
297 | memcpy( walk + sizeof( addr ), &port, 2 ); |
---|
298 | |
---|
299 | walk += sizeof( tr_address ) + 2; |
---|
300 | compactWalk += 6; |
---|
301 | } |
---|
302 | publishNewPeers( t, allAreSeeds, array, arrayLen ); |
---|
303 | tr_free( array ); |
---|
304 | } |
---|
305 | |
---|
306 | /*** |
---|
307 | **** |
---|
308 | ***/ |
---|
309 | |
---|
310 | static void onReqDone( tr_session * session ); |
---|
311 | |
---|
312 | static int |
---|
313 | updateAddresses( tr_tracker * t, |
---|
314 | int success ) |
---|
315 | { |
---|
316 | int retry; |
---|
317 | |
---|
318 | tr_torrent * torrent = tr_torrentFindFromHash( t->session, t->hash ); |
---|
319 | |
---|
320 | if( success ) |
---|
321 | { |
---|
322 | /* multitracker spec: "if a connection with a tracker is |
---|
323 | successful, it will be moved to the front of the tier." */ |
---|
324 | t->trackerIndex = tr_torrentPromoteTracker( torrent, t->trackerIndex ); |
---|
325 | retry = FALSE; /* we succeeded; no need to retry */ |
---|
326 | } |
---|
327 | else if( ++t->trackerIndex >= torrent->info.trackerCount ) |
---|
328 | { |
---|
329 | t->trackerIndex = 0; |
---|
330 | retry = FALSE; /* we've tried them all */ |
---|
331 | } |
---|
332 | else |
---|
333 | { |
---|
334 | const tr_tracker_info * n = getCurrentAddressFromTorrent( t, torrent ); |
---|
335 | tr_ninf( t->name, _( "Trying tracker \"%s\"" ), n->announce ); |
---|
336 | retry = TRUE; |
---|
337 | } |
---|
338 | |
---|
339 | return retry; |
---|
340 | } |
---|
341 | |
---|
342 | static uint8_t * |
---|
343 | parseOldPeers( tr_benc * bePeers, |
---|
344 | size_t * byteCount ) |
---|
345 | { |
---|
346 | int i; |
---|
347 | uint8_t * array, *walk; |
---|
348 | const int peerCount = bePeers->val.l.count; |
---|
349 | |
---|
350 | assert( bePeers->type == TYPE_LIST ); |
---|
351 | |
---|
352 | array = tr_new( uint8_t, peerCount * ( sizeof( tr_address ) + 2 ) ); |
---|
353 | |
---|
354 | for( i = 0, walk = array; i < peerCount; ++i ) |
---|
355 | { |
---|
356 | const char * s; |
---|
357 | int64_t itmp; |
---|
358 | tr_address addr; |
---|
359 | tr_port port; |
---|
360 | tr_benc * peer = &bePeers->val.l.vals[i]; |
---|
361 | |
---|
362 | if( tr_bencDictFindStr( peer, "ip", &s ) ) |
---|
363 | { |
---|
364 | if( tr_pton( s, &addr ) == NULL ) |
---|
365 | continue; |
---|
366 | } |
---|
367 | if( !tr_bencDictFindInt( peer, "port", |
---|
368 | &itmp ) || itmp < 0 || itmp > 0xffff ) |
---|
369 | continue; |
---|
370 | |
---|
371 | memcpy( walk, &addr, sizeof( tr_address ) ); |
---|
372 | tr_suspectAddress( &addr, "old tracker" ); |
---|
373 | port = htons( itmp ); |
---|
374 | memcpy( walk + sizeof( tr_address ), &port, 2 ); |
---|
375 | walk += sizeof( tr_address ) + 2; |
---|
376 | } |
---|
377 | |
---|
378 | *byteCount = peerCount * sizeof( tr_address ) + 2; |
---|
379 | return array; |
---|
380 | } |
---|
381 | |
---|
382 | static void |
---|
383 | onStoppedResponse( tr_session * session, |
---|
384 | long responseCode UNUSED, |
---|
385 | const void * response UNUSED, |
---|
386 | size_t responseLen UNUSED, |
---|
387 | void * torrentId ) |
---|
388 | { |
---|
389 | tr_tracker * t = findTracker( session, tr_ptr2int( torrentId ) ); |
---|
390 | if( t ) |
---|
391 | { |
---|
392 | const time_t now = time( NULL ); |
---|
393 | |
---|
394 | t->reannounceAt = TR_TRACKER_STOPPED; |
---|
395 | t->manualAnnounceAllowedAt = TR_TRACKER_STOPPED; |
---|
396 | |
---|
397 | if( t->scrapeAt <= now ) |
---|
398 | t->scrapeAt = now + t->scrapeIntervalSec + t->randOffset; |
---|
399 | } |
---|
400 | |
---|
401 | dbgmsg( NULL, "got a response to some `stop' message" ); |
---|
402 | onReqDone( session ); |
---|
403 | } |
---|
404 | |
---|
405 | static void |
---|
406 | onTrackerResponse( tr_session * session, |
---|
407 | long responseCode, |
---|
408 | const void * response, |
---|
409 | size_t responseLen, |
---|
410 | void * torrentId ) |
---|
411 | { |
---|
412 | int retry; |
---|
413 | int success = FALSE; |
---|
414 | int scrapeFields = 0; |
---|
415 | tr_tracker * t; |
---|
416 | |
---|
417 | onReqDone( session ); |
---|
418 | t = findTracker( session, tr_ptr2int( torrentId ) ); |
---|
419 | if( !t ) /* tracker's been closed */ |
---|
420 | return; |
---|
421 | |
---|
422 | dbgmsg( t->name, "tracker response: %ld", responseCode ); |
---|
423 | tr_ndbg( t->name, "tracker response: %ld", responseCode ); |
---|
424 | t->lastAnnounceResponse = responseCode; |
---|
425 | |
---|
426 | if( responseCode == HTTP_OK ) |
---|
427 | { |
---|
428 | tr_benc benc; |
---|
429 | const int bencLoaded = !tr_bencLoad( response, responseLen, |
---|
430 | &benc, NULL ); |
---|
431 | publishErrorClear( t ); |
---|
432 | if( bencLoaded && tr_bencIsDict( &benc ) ) |
---|
433 | { |
---|
434 | tr_benc * tmp; |
---|
435 | int64_t i; |
---|
436 | int incomplete = -1; |
---|
437 | const char * str; |
---|
438 | |
---|
439 | success = TRUE; |
---|
440 | t->retryAnnounceIntervalSec = FIRST_SCRAPE_RETRY_INTERVAL_SEC; |
---|
441 | |
---|
442 | if( ( tr_bencDictFindStr( &benc, "failure reason", &str ) ) ) |
---|
443 | { |
---|
444 | publishMessage( t, str, TR_TRACKER_ERROR ); |
---|
445 | success = FALSE; |
---|
446 | } |
---|
447 | |
---|
448 | if( ( tr_bencDictFindStr( &benc, "warning message", &str ) ) ) |
---|
449 | publishWarning( t, str ); |
---|
450 | |
---|
451 | if( ( tr_bencDictFindInt( &benc, "interval", &i ) ) ) |
---|
452 | { |
---|
453 | dbgmsg( t->name, "setting interval to %d", (int)i ); |
---|
454 | t->announceIntervalSec = i; |
---|
455 | } |
---|
456 | |
---|
457 | if( ( tr_bencDictFindInt( &benc, "min interval", &i ) ) ) |
---|
458 | { |
---|
459 | dbgmsg( t->name, "setting min interval to %d", (int)i ); |
---|
460 | t->announceMinIntervalSec = i; |
---|
461 | } |
---|
462 | |
---|
463 | if( ( tr_bencDictFindStr( &benc, "tracker id", &str ) ) ) |
---|
464 | t->trackerID = tr_strdup( str ); |
---|
465 | |
---|
466 | if( ( tr_bencDictFindInt( &benc, "complete", &i ) ) ) |
---|
467 | { |
---|
468 | ++scrapeFields; |
---|
469 | t->seederCount = i; |
---|
470 | } |
---|
471 | |
---|
472 | if( ( tr_bencDictFindInt( &benc, "incomplete", &i ) ) ) |
---|
473 | { |
---|
474 | ++scrapeFields; |
---|
475 | t->leecherCount = incomplete = i; |
---|
476 | } |
---|
477 | |
---|
478 | if( ( tr_bencDictFindInt( &benc, "downloaded", &i ) ) ) |
---|
479 | { |
---|
480 | ++scrapeFields; |
---|
481 | t->timesDownloaded = i; |
---|
482 | } |
---|
483 | |
---|
484 | if( ( tmp = tr_bencDictFind( &benc, "peers" ) ) ) |
---|
485 | { |
---|
486 | const int allAreSeeds = incomplete == 0; |
---|
487 | |
---|
488 | if( tmp->type == TYPE_STR ) /* "compact" extension */ |
---|
489 | { |
---|
490 | publishNewPeersCompact( t, allAreSeeds, tmp->val.s.s, |
---|
491 | tmp->val.s.i ); |
---|
492 | } |
---|
493 | else if( tmp->type == TYPE_LIST ) /* original protocol */ |
---|
494 | { |
---|
495 | size_t byteCount = 0; |
---|
496 | uint8_t * array = parseOldPeers( tmp, &byteCount ); |
---|
497 | publishNewPeers( t, allAreSeeds, array, byteCount ); |
---|
498 | tr_free( array ); |
---|
499 | } |
---|
500 | } |
---|
501 | |
---|
502 | if( ( tmp = tr_bencDictFind( &benc, "peers6" ) ) ) |
---|
503 | { |
---|
504 | const int allAreSeeds = incomplete == 0; |
---|
505 | |
---|
506 | if( tmp->type == TYPE_STR ) /* "compact" extension */ |
---|
507 | { |
---|
508 | publishNewPeersCompact6( t, allAreSeeds, tmp->val.s.s, |
---|
509 | tmp->val.s.i ); |
---|
510 | } |
---|
511 | } |
---|
512 | } |
---|
513 | |
---|
514 | if( bencLoaded ) |
---|
515 | tr_bencFree( &benc ); |
---|
516 | } |
---|
517 | else if( responseCode ) |
---|
518 | { |
---|
519 | /* %1$ld - http status code, such as 404 |
---|
520 | * %2$s - human-readable explanation of the http status code */ |
---|
521 | char * buf = tr_strdup_printf( _( "Tracker request failed. Got HTTP Status Code %1$ld (%2$s)" ), |
---|
522 | responseCode, |
---|
523 | tr_webGetResponseStr( responseCode ) ); |
---|
524 | publishWarning( t, buf ); |
---|
525 | tr_free( buf ); |
---|
526 | } |
---|
527 | |
---|
528 | retry = updateAddresses( t, success ); |
---|
529 | |
---|
530 | if( responseCode && retry ) |
---|
531 | responseCode = 300; |
---|
532 | |
---|
533 | if( responseCode == 0 ) |
---|
534 | { |
---|
535 | dbgmsg( t->name, "No response from tracker... retrying in two minutes." ); |
---|
536 | t->manualAnnounceAllowedAt = ~(time_t)0; |
---|
537 | t->reannounceAt = time( NULL ) + t->randOffset + 120; |
---|
538 | } |
---|
539 | else if( 200 <= responseCode && responseCode <= 299 ) |
---|
540 | { |
---|
541 | const int interval = t->announceIntervalSec + t->randOffset; |
---|
542 | const time_t now = time ( NULL ); |
---|
543 | dbgmsg( t->name, "request succeeded. reannouncing in %d seconds", interval ); |
---|
544 | |
---|
545 | /* if the announce response was a superset of the scrape response, |
---|
546 | treat this as both a successful announce AND scrape. */ |
---|
547 | if( scrapeFields >= 3 ) { |
---|
548 | t->lastScrapeResponse = responseCode; |
---|
549 | t->lastScrapeTime = now; |
---|
550 | t->scrapeAt = now + t->scrapeIntervalSec + t->randOffset; |
---|
551 | } |
---|
552 | |
---|
553 | /* most trackers don't provide all the scrape responses, but do |
---|
554 | provide most of them, so don't scrape too soon anyway */ |
---|
555 | if( ( scrapeFields == 2 ) && ( t->scrapeAt <= ( now + 120 ) ) ) { |
---|
556 | t->scrapeAt = now + t->scrapeIntervalSec + t->randOffset; |
---|
557 | } |
---|
558 | |
---|
559 | t->reannounceAt = now + interval; |
---|
560 | t->manualAnnounceAllowedAt = now + t->announceMinIntervalSec; |
---|
561 | |
---|
562 | /* #319: save the .resume file after an announce so that, in case |
---|
563 | * of a crash, our stats still match up with the tracker's stats */ |
---|
564 | tr_torrentSaveResume( tr_torrentFindFromHash( t->session, t->hash ) ); |
---|
565 | } |
---|
566 | else if( 300 <= responseCode && responseCode <= 399 ) |
---|
567 | { |
---|
568 | /* it's a redirect... updateAddresses() has already |
---|
569 | * parsed the redirect, all that's left is to retry */ |
---|
570 | const int interval = 5; |
---|
571 | dbgmsg( t->name, "got a redirect. retrying in %d seconds", interval ); |
---|
572 | t->reannounceAt = time( NULL ) + interval; |
---|
573 | t->manualAnnounceAllowedAt = time( NULL ) + t->announceMinIntervalSec; |
---|
574 | } |
---|
575 | else if( 400 <= responseCode && responseCode <= 499 ) |
---|
576 | { |
---|
577 | /* The request could not be understood by the server due to |
---|
578 | * malformed syntax. The client SHOULD NOT repeat the |
---|
579 | * request without modifications. */ |
---|
580 | publishErrorMessageAndStop( t, _( "Tracker returned a 4xx message" ) ); |
---|
581 | t->manualAnnounceAllowedAt = ~(time_t)0; |
---|
582 | t->reannounceAt = TR_TRACKER_STOPPED; |
---|
583 | } |
---|
584 | else if( 500 <= responseCode && responseCode <= 599 ) |
---|
585 | { |
---|
586 | /* Response status codes beginning with the digit "5" indicate |
---|
587 | * cases in which the server is aware that it has erred or is |
---|
588 | * incapable of performing the request. So we pause a bit and |
---|
589 | * try again. */ |
---|
590 | t->manualAnnounceAllowedAt = ~(time_t)0; |
---|
591 | t->reannounceAt = time( NULL ) + t->retryAnnounceIntervalSec; |
---|
592 | t->retryAnnounceIntervalSec *= 2; |
---|
593 | } |
---|
594 | else |
---|
595 | { |
---|
596 | /* WTF did we get?? */ |
---|
597 | dbgmsg( t->name, "Invalid response from tracker... retrying in two minutes." ); |
---|
598 | t->manualAnnounceAllowedAt = ~(time_t)0; |
---|
599 | t->reannounceAt = time( NULL ) + t->randOffset + 120; |
---|
600 | } |
---|
601 | } |
---|
602 | |
---|
603 | static void |
---|
604 | onScrapeResponse( tr_session * session, |
---|
605 | long responseCode, |
---|
606 | const void * response, |
---|
607 | size_t responseLen, |
---|
608 | void * torrentId ) |
---|
609 | { |
---|
610 | int success = FALSE; |
---|
611 | int retry; |
---|
612 | tr_tracker * t; |
---|
613 | |
---|
614 | onReqDone( session ); |
---|
615 | t = findTracker( session, tr_ptr2int( torrentId ) ); |
---|
616 | if( !t ) /* tracker's been closed... */ |
---|
617 | return; |
---|
618 | |
---|
619 | dbgmsg( t->name, "scrape response: %ld\n", responseCode ); |
---|
620 | tr_ndbg( t->name, "scrape response: %ld", responseCode ); |
---|
621 | t->lastScrapeResponse = responseCode; |
---|
622 | |
---|
623 | if( responseCode == HTTP_OK ) |
---|
624 | { |
---|
625 | tr_benc benc, *files; |
---|
626 | const int bencLoaded = !tr_bencLoad( response, responseLen, |
---|
627 | &benc, NULL ); |
---|
628 | if( bencLoaded && tr_bencDictFindDict( &benc, "files", &files ) ) |
---|
629 | { |
---|
630 | size_t i; |
---|
631 | for( i = 0; i < files->val.l.count; i += 2 ) |
---|
632 | { |
---|
633 | int64_t itmp; |
---|
634 | const uint8_t* hash = |
---|
635 | (const uint8_t*) files->val.l.vals[i].val.s.s; |
---|
636 | tr_benc * flags; |
---|
637 | tr_benc * tordict = &files->val.l.vals[i + 1]; |
---|
638 | if( memcmp( t->hash, hash, SHA_DIGEST_LENGTH ) ) |
---|
639 | continue; |
---|
640 | |
---|
641 | publishErrorClear( t ); |
---|
642 | |
---|
643 | if( ( tr_bencDictFindInt( tordict, "complete", &itmp ) ) ) |
---|
644 | t->seederCount = itmp; |
---|
645 | |
---|
646 | if( ( tr_bencDictFindInt( tordict, "incomplete", &itmp ) ) ) |
---|
647 | t->leecherCount = itmp; |
---|
648 | |
---|
649 | if( ( tr_bencDictFindInt( tordict, "downloaded", &itmp ) ) ) |
---|
650 | t->timesDownloaded = itmp; |
---|
651 | |
---|
652 | if( ( tr_bencDictFindInt( tordict, "downloaders", &itmp ) ) ) |
---|
653 | t->downloaderCount = itmp; |
---|
654 | |
---|
655 | if( tr_bencDictFindDict( tordict, "flags", &flags ) ) |
---|
656 | if( ( tr_bencDictFindInt( flags, "min_request_interval", &itmp ) ) ) |
---|
657 | t->scrapeIntervalSec = i; |
---|
658 | |
---|
659 | /* as per ticket #1045, safeguard against trackers returning |
---|
660 | * a very low min_request_interval... */ |
---|
661 | if( t->scrapeIntervalSec < DEFAULT_SCRAPE_INTERVAL_SEC ) |
---|
662 | t->scrapeIntervalSec = DEFAULT_SCRAPE_INTERVAL_SEC; |
---|
663 | |
---|
664 | tr_ndbg( t->name, |
---|
665 | "Scrape successful. Rescraping in %d seconds.", |
---|
666 | t->scrapeIntervalSec ); |
---|
667 | |
---|
668 | success = TRUE; |
---|
669 | t->retryScrapeIntervalSec = FIRST_SCRAPE_RETRY_INTERVAL_SEC; |
---|
670 | } |
---|
671 | } |
---|
672 | |
---|
673 | if( bencLoaded ) |
---|
674 | tr_bencFree( &benc ); |
---|
675 | } |
---|
676 | |
---|
677 | retry = updateAddresses( t, success ); |
---|
678 | |
---|
679 | /** |
---|
680 | *** |
---|
681 | **/ |
---|
682 | |
---|
683 | if( retry ) |
---|
684 | responseCode = 300; |
---|
685 | |
---|
686 | if( 200 <= responseCode && responseCode <= 299 ) |
---|
687 | { |
---|
688 | const int interval = t->scrapeIntervalSec + t->randOffset; |
---|
689 | dbgmsg( t->name, "request succeeded. rescraping in %d seconds", |
---|
690 | interval ); |
---|
691 | tr_ndbg( t->name, "request succeeded. rescraping in %d seconds", |
---|
692 | interval ); |
---|
693 | t->scrapeAt = time( NULL ) + interval; |
---|
694 | } |
---|
695 | else if( 300 <= responseCode && responseCode <= 399 ) |
---|
696 | { |
---|
697 | const int interval = 5; |
---|
698 | dbgmsg( t->name, "got a redirect. retrying in %d seconds", interval ); |
---|
699 | t->scrapeAt = time( NULL ) + interval; |
---|
700 | } |
---|
701 | else |
---|
702 | { |
---|
703 | const int interval = t->retryScrapeIntervalSec + t->randOffset; |
---|
704 | dbgmsg( |
---|
705 | t->name, |
---|
706 | "Tracker responded to scrape with %ld. Retrying in %d seconds.", |
---|
707 | responseCode, interval ); |
---|
708 | t->retryScrapeIntervalSec *= 2; |
---|
709 | t->scrapeAt = time( NULL ) + interval; |
---|
710 | } |
---|
711 | } |
---|
712 | |
---|
713 | /*** |
---|
714 | **** |
---|
715 | ***/ |
---|
716 | |
---|
717 | enum |
---|
718 | { |
---|
719 | TR_REQ_STARTED, |
---|
720 | TR_REQ_COMPLETED, |
---|
721 | TR_REQ_STOPPED, |
---|
722 | TR_REQ_PAUSED, /* BEP 21 */ |
---|
723 | TR_REQ_REANNOUNCE, |
---|
724 | TR_REQ_SCRAPE |
---|
725 | }; |
---|
726 | |
---|
727 | struct tr_tracker_request |
---|
728 | { |
---|
729 | int reqtype; /* TR_REQ_* */ |
---|
730 | int torrentId; |
---|
731 | struct evbuffer * url; |
---|
732 | tr_web_done_func * done_func; |
---|
733 | tr_session * session; |
---|
734 | }; |
---|
735 | |
---|
736 | static void |
---|
737 | freeRequest( struct tr_tracker_request * req ) |
---|
738 | { |
---|
739 | evbuffer_free( req->url ); |
---|
740 | tr_free( req ); |
---|
741 | } |
---|
742 | |
---|
743 | static void |
---|
744 | buildTrackerRequestURI( tr_tracker * t, |
---|
745 | const tr_torrent * torrent, |
---|
746 | const char * eventName, |
---|
747 | struct evbuffer * buf ) |
---|
748 | { |
---|
749 | const int isStopping = !strcmp( eventName, "stopped" ); |
---|
750 | const int numwant = isStopping ? 0 : NUMWANT; |
---|
751 | const char * ann = getCurrentAddressFromTorrent( t, torrent )->announce; |
---|
752 | |
---|
753 | evbuffer_add_printf( buf, "%cinfo_hash=%s" |
---|
754 | "&peer_id=%s" |
---|
755 | "&port=%d" |
---|
756 | "&uploaded=%" PRIu64 |
---|
757 | "&downloaded=%" PRIu64 |
---|
758 | "&corrupt=%" PRIu64 |
---|
759 | "&left=%" PRIu64 |
---|
760 | "&compact=1" |
---|
761 | "&numwant=%d" |
---|
762 | "&key=%s", |
---|
763 | strchr( ann, '?' ) ? '&' : '?', |
---|
764 | t->escaped, |
---|
765 | t->peer_id, |
---|
766 | tr_sessionGetPeerPort( t->session ), |
---|
767 | torrent->uploadedCur, |
---|
768 | torrent->downloadedCur, |
---|
769 | torrent->corruptCur, |
---|
770 | tr_cpLeftUntilComplete( torrent->completion ), |
---|
771 | numwant, |
---|
772 | t->key_param ); |
---|
773 | |
---|
774 | if( eventName && *eventName ) |
---|
775 | evbuffer_add_printf( buf, "&event=%s", eventName ); |
---|
776 | |
---|
777 | if( t->trackerID && *t->trackerID ) |
---|
778 | evbuffer_add_printf( buf, "&trackerid=%s", t->trackerID ); |
---|
779 | |
---|
780 | } |
---|
781 | |
---|
782 | static struct tr_tracker_request* |
---|
783 | createRequest( tr_session * session, |
---|
784 | tr_tracker * tracker, |
---|
785 | int reqtype ) |
---|
786 | { |
---|
787 | static const char* strings[] = { "started", "completed", "stopped", "paused", "", "err" }; |
---|
788 | const tr_torrent * torrent = tr_torrentFindFromHash( session, tracker->hash ); |
---|
789 | const tr_tracker_info * address = getCurrentAddressFromTorrent( tracker, torrent ); |
---|
790 | int isStopping; |
---|
791 | struct tr_tracker_request * req; |
---|
792 | struct evbuffer * url; |
---|
793 | |
---|
794 | /* BEP 21: In order to tell the tracker that a peer is a partial seed, it MUST send |
---|
795 | * an event=paused parameter in every announce while it is a partial seed. */ |
---|
796 | if( tr_cpGetStatus( torrent->completion ) == TR_PARTIAL_SEED ) |
---|
797 | reqtype = TR_REQ_PAUSED; |
---|
798 | |
---|
799 | isStopping = reqtype == TR_REQ_STOPPED; |
---|
800 | |
---|
801 | url = evbuffer_new( ); |
---|
802 | evbuffer_add_printf( url, "%s", address->announce ); |
---|
803 | buildTrackerRequestURI( tracker, torrent, strings[reqtype], url ); |
---|
804 | |
---|
805 | req = tr_new0( struct tr_tracker_request, 1 ); |
---|
806 | req->session = session; |
---|
807 | req->reqtype = reqtype; |
---|
808 | req->done_func = isStopping ? onStoppedResponse : onTrackerResponse; |
---|
809 | req->url = url; |
---|
810 | req->torrentId = tracker->torrentId; |
---|
811 | |
---|
812 | return req; |
---|
813 | } |
---|
814 | |
---|
815 | static struct tr_tracker_request* |
---|
816 | createScrape( tr_session * session, |
---|
817 | tr_tracker * tracker ) |
---|
818 | { |
---|
819 | const tr_tracker_info * a = getCurrentAddress( tracker ); |
---|
820 | struct tr_tracker_request * req; |
---|
821 | struct evbuffer * url = evbuffer_new( ); |
---|
822 | |
---|
823 | evbuffer_add_printf( url, "%s%cinfo_hash=%s", |
---|
824 | a->scrape, strchr( a->scrape, '?' ) ? '&' : '?', |
---|
825 | tracker->escaped ); |
---|
826 | |
---|
827 | req = tr_new0( struct tr_tracker_request, 1 ); |
---|
828 | req->session = session; |
---|
829 | req->reqtype = TR_REQ_SCRAPE; |
---|
830 | req->url = url; |
---|
831 | req->done_func = onScrapeResponse; |
---|
832 | req->torrentId = tracker->torrentId; |
---|
833 | |
---|
834 | return req; |
---|
835 | } |
---|
836 | |
---|
837 | struct tr_tracker_handle |
---|
838 | { |
---|
839 | int runningCount; |
---|
840 | tr_timer * pulseTimer; |
---|
841 | }; |
---|
842 | |
---|
843 | static int trackerPulse( void * vsession ); |
---|
844 | |
---|
845 | static void |
---|
846 | ensureGlobalsExist( tr_session * session ) |
---|
847 | { |
---|
848 | if( session->tracker == NULL ) |
---|
849 | { |
---|
850 | session->tracker = tr_new0( struct tr_tracker_handle, 1 ); |
---|
851 | session->tracker->pulseTimer = |
---|
852 | tr_timerNew( session, trackerPulse, session, |
---|
853 | PULSE_INTERVAL_MSEC ); |
---|
854 | dbgmsg( NULL, "creating tracker timer" ); |
---|
855 | } |
---|
856 | } |
---|
857 | |
---|
858 | void |
---|
859 | tr_trackerSessionClose( tr_session * session ) |
---|
860 | { |
---|
861 | if( session && session->tracker ) |
---|
862 | { |
---|
863 | dbgmsg( NULL, "freeing tracker timer" ); |
---|
864 | tr_timerFree( &session->tracker->pulseTimer ); |
---|
865 | tr_free( session->tracker ); |
---|
866 | session->tracker = NULL; |
---|
867 | } |
---|
868 | } |
---|
869 | |
---|
870 | /*** |
---|
871 | **** |
---|
872 | ***/ |
---|
873 | |
---|
874 | static void |
---|
875 | invokeRequest( void * vreq ) |
---|
876 | { |
---|
877 | struct tr_tracker_request * req = vreq; |
---|
878 | tr_tracker * t = findTracker( req->session, req->torrentId ); |
---|
879 | |
---|
880 | if( t ) |
---|
881 | { |
---|
882 | const time_t now = time( NULL ); |
---|
883 | |
---|
884 | if( req->reqtype == TR_REQ_SCRAPE ) |
---|
885 | { |
---|
886 | t->lastScrapeTime = now; |
---|
887 | t->scrapeAt = TR_TRACKER_BUSY; |
---|
888 | } |
---|
889 | else |
---|
890 | { |
---|
891 | t->lastAnnounceTime = now; |
---|
892 | t->reannounceAt = TR_TRACKER_BUSY; |
---|
893 | t->manualAnnounceAllowedAt = TR_TRACKER_BUSY; |
---|
894 | } |
---|
895 | } |
---|
896 | |
---|
897 | ++req->session->tracker->runningCount; |
---|
898 | |
---|
899 | tr_webRun( req->session, |
---|
900 | (char*)EVBUFFER_DATA(req->url), |
---|
901 | NULL, |
---|
902 | req->done_func, tr_int2ptr( req->torrentId ) ); |
---|
903 | |
---|
904 | freeRequest( req ); |
---|
905 | } |
---|
906 | |
---|
907 | static void |
---|
908 | enqueueScrape( tr_session * session, |
---|
909 | tr_tracker * tracker ) |
---|
910 | { |
---|
911 | struct tr_tracker_request * req = createScrape( session, tracker ); |
---|
912 | tr_runInEventThread( session, invokeRequest, req ); |
---|
913 | } |
---|
914 | |
---|
915 | static void |
---|
916 | enqueueRequest( tr_session * session, |
---|
917 | tr_tracker * tracker, |
---|
918 | int reqtype ) |
---|
919 | { |
---|
920 | struct tr_tracker_request * req = createRequest( session, tracker, reqtype ); |
---|
921 | tr_runInEventThread( session, invokeRequest, req ); |
---|
922 | } |
---|
923 | |
---|
924 | static int |
---|
925 | trackerPulse( void * vsession ) |
---|
926 | { |
---|
927 | tr_session * session = vsession; |
---|
928 | struct tr_tracker_handle * th = session->tracker; |
---|
929 | tr_torrent * tor; |
---|
930 | const time_t now = time( NULL ); |
---|
931 | |
---|
932 | if( !session->tracker ) |
---|
933 | return FALSE; |
---|
934 | |
---|
935 | if( th->runningCount ) |
---|
936 | dbgmsg( NULL, "tracker pulse... %d running", th->runningCount ); |
---|
937 | |
---|
938 | /* upkeep: queue periodic rescrape / reannounce */ |
---|
939 | for( tor = session->torrentList; tor; tor = tor->next ) |
---|
940 | { |
---|
941 | tr_tracker * t = tor->tracker; |
---|
942 | |
---|
943 | if( ( t->scrapeAt > 1 ) |
---|
944 | && ( t->scrapeAt <= now ) |
---|
945 | && ( trackerSupportsScrape( t, tor ) ) ) |
---|
946 | { |
---|
947 | t->scrapeAt = TR_TRACKER_BUSY; |
---|
948 | enqueueScrape( session, t ); |
---|
949 | } |
---|
950 | |
---|
951 | if( ( t->reannounceAt > 1 ) |
---|
952 | && ( t->reannounceAt <= now ) |
---|
953 | && ( t->isRunning ) ) |
---|
954 | { |
---|
955 | t->reannounceAt = TR_TRACKER_BUSY; |
---|
956 | t->manualAnnounceAllowedAt = TR_TRACKER_BUSY; |
---|
957 | enqueueRequest( session, t, TR_REQ_REANNOUNCE ); |
---|
958 | } |
---|
959 | } |
---|
960 | |
---|
961 | if( th->runningCount ) |
---|
962 | dbgmsg( NULL, "tracker pulse after upkeep... %d running", |
---|
963 | th->runningCount ); |
---|
964 | |
---|
965 | /* free the tracker manager if no torrents are left */ |
---|
966 | if( ( session->tracker ) |
---|
967 | && ( session->tracker->runningCount < 1 ) |
---|
968 | && ( session->torrentList == NULL ) ) |
---|
969 | { |
---|
970 | tr_trackerSessionClose( session ); |
---|
971 | } |
---|
972 | |
---|
973 | /* if there are still running torrents (as indicated by |
---|
974 | * the existence of the tracker manager) then keep the |
---|
975 | * trackerPulse() timer alive */ |
---|
976 | return session->tracker != NULL; |
---|
977 | } |
---|
978 | |
---|
979 | static void |
---|
980 | onReqDone( tr_session * session ) |
---|
981 | { |
---|
982 | if( session->tracker ) |
---|
983 | { |
---|
984 | --session->tracker->runningCount; |
---|
985 | dbgmsg( NULL, "decrementing runningCount to %d", |
---|
986 | session->tracker->runningCount ); |
---|
987 | trackerPulse( session ); |
---|
988 | } |
---|
989 | } |
---|
990 | |
---|
991 | /*** |
---|
992 | **** LIFE CYCLE |
---|
993 | ***/ |
---|
994 | |
---|
995 | static void |
---|
996 | generateKeyParam( char * msg, |
---|
997 | int len ) |
---|
998 | { |
---|
999 | int i; |
---|
1000 | const char * pool = "abcdefghijklmnopqrstuvwxyz0123456789"; |
---|
1001 | const int poolSize = strlen( pool ); |
---|
1002 | |
---|
1003 | for( i = 0; i < len; ++i ) |
---|
1004 | *msg++ = pool[tr_cryptoRandInt( poolSize )]; |
---|
1005 | *msg = '\0'; |
---|
1006 | } |
---|
1007 | |
---|
1008 | static int |
---|
1009 | is_rfc2396_alnum( char ch ) |
---|
1010 | { |
---|
1011 | return ( '0' <= ch && ch <= '9' ) |
---|
1012 | || ( 'A' <= ch && ch <= 'Z' ) |
---|
1013 | || ( 'a' <= ch && ch <= 'z' ); |
---|
1014 | } |
---|
1015 | |
---|
1016 | static void |
---|
1017 | escape( char * out, |
---|
1018 | const uint8_t * in, |
---|
1019 | int in_len ) /* rfc2396 */ |
---|
1020 | { |
---|
1021 | const uint8_t *end = in + in_len; |
---|
1022 | |
---|
1023 | while( in != end ) |
---|
1024 | if( is_rfc2396_alnum( *in ) ) |
---|
1025 | *out++ = (char) *in++; |
---|
1026 | else |
---|
1027 | out += tr_snprintf( out, 4, "%%%02X", (unsigned int)*in++ ); |
---|
1028 | |
---|
1029 | *out = '\0'; |
---|
1030 | } |
---|
1031 | |
---|
1032 | tr_tracker * |
---|
1033 | tr_trackerNew( const tr_torrent * torrent ) |
---|
1034 | { |
---|
1035 | const tr_info * info = &torrent->info; |
---|
1036 | tr_tracker * t; |
---|
1037 | |
---|
1038 | ensureGlobalsExist( torrent->session ); |
---|
1039 | |
---|
1040 | t = tr_new0( tr_tracker, 1 ); |
---|
1041 | t->publisher = TR_PUBLISHER_INIT; |
---|
1042 | t->session = torrent->session; |
---|
1043 | t->scrapeIntervalSec = DEFAULT_SCRAPE_INTERVAL_SEC; |
---|
1044 | t->retryScrapeIntervalSec = FIRST_SCRAPE_RETRY_INTERVAL_SEC; |
---|
1045 | t->retryAnnounceIntervalSec = FIRST_ANNOUNCE_RETRY_INTERVAL_SEC; |
---|
1046 | t->announceIntervalSec = DEFAULT_ANNOUNCE_INTERVAL_SEC; |
---|
1047 | t->announceMinIntervalSec = DEFAULT_ANNOUNCE_MIN_INTERVAL_SEC; |
---|
1048 | t->timesDownloaded = -1; |
---|
1049 | t->seederCount = -1; |
---|
1050 | t->downloaderCount = -1; |
---|
1051 | t->leecherCount = -1; |
---|
1052 | t->lastAnnounceResponse = -1; |
---|
1053 | t->lastScrapeResponse = -1; |
---|
1054 | t->manualAnnounceAllowedAt = ~(time_t)0; |
---|
1055 | t->name = tr_strdup( info->name ); |
---|
1056 | t->torrentId = torrent->uniqueId; |
---|
1057 | t->randOffset = tr_cryptoRandInt( 30 ); |
---|
1058 | memcpy( t->hash, info->hash, SHA_DIGEST_LENGTH ); |
---|
1059 | escape( t->escaped, info->hash, SHA_DIGEST_LENGTH ); |
---|
1060 | generateKeyParam( t->key_param, KEYLEN ); |
---|
1061 | |
---|
1062 | t->trackerIndex = 0; |
---|
1063 | |
---|
1064 | if( trackerSupportsScrape( t, torrent ) ) |
---|
1065 | t->scrapeAt = time( NULL ) + t->randOffset; |
---|
1066 | |
---|
1067 | return t; |
---|
1068 | } |
---|
1069 | |
---|
1070 | static void |
---|
1071 | onTrackerFreeNow( void * vt ) |
---|
1072 | { |
---|
1073 | tr_tracker * t = vt; |
---|
1074 | |
---|
1075 | tr_publisherDestruct( &t->publisher ); |
---|
1076 | tr_free( t->name ); |
---|
1077 | tr_free( t->trackerID ); |
---|
1078 | tr_free( t->peer_id ); |
---|
1079 | |
---|
1080 | tr_free( t ); |
---|
1081 | } |
---|
1082 | |
---|
1083 | /*** |
---|
1084 | **** PUBLIC |
---|
1085 | ***/ |
---|
1086 | |
---|
1087 | void |
---|
1088 | tr_trackerFree( tr_tracker * t ) |
---|
1089 | { |
---|
1090 | if( t ) |
---|
1091 | tr_runInEventThread( t->session, onTrackerFreeNow, t ); |
---|
1092 | } |
---|
1093 | |
---|
1094 | tr_publisher_tag |
---|
1095 | tr_trackerSubscribe( tr_tracker * t, |
---|
1096 | tr_delivery_func func, |
---|
1097 | void * user_data ) |
---|
1098 | { |
---|
1099 | return tr_publisherSubscribe( &t->publisher, func, user_data ); |
---|
1100 | } |
---|
1101 | |
---|
1102 | void |
---|
1103 | tr_trackerUnsubscribe( tr_tracker * t, |
---|
1104 | tr_publisher_tag tag ) |
---|
1105 | { |
---|
1106 | if( t ) |
---|
1107 | tr_publisherUnsubscribe( &t->publisher, tag ); |
---|
1108 | } |
---|
1109 | |
---|
1110 | const tr_tracker_info * |
---|
1111 | tr_trackerGetAddress( tr_tracker * t ) |
---|
1112 | { |
---|
1113 | return getCurrentAddress( t ); |
---|
1114 | } |
---|
1115 | |
---|
1116 | time_t |
---|
1117 | tr_trackerGetManualAnnounceTime( const struct tr_tracker * t ) |
---|
1118 | { |
---|
1119 | return t->isRunning ? t->manualAnnounceAllowedAt : 0; |
---|
1120 | } |
---|
1121 | |
---|
1122 | int |
---|
1123 | tr_trackerCanManualAnnounce( const tr_tracker * t ) |
---|
1124 | { |
---|
1125 | const time_t allow = tr_trackerGetManualAnnounceTime( t ); |
---|
1126 | |
---|
1127 | return allow && ( allow <= time( NULL ) ); |
---|
1128 | } |
---|
1129 | |
---|
1130 | void |
---|
1131 | tr_trackerGetCounts( const tr_tracker * t, |
---|
1132 | int * setme_completedCount, |
---|
1133 | int * setme_leecherCount, |
---|
1134 | int * setme_seederCount, |
---|
1135 | int * setme_downloaderCount ) |
---|
1136 | { |
---|
1137 | if( setme_completedCount ) |
---|
1138 | *setme_completedCount = t->timesDownloaded; |
---|
1139 | |
---|
1140 | if( setme_leecherCount ) |
---|
1141 | *setme_leecherCount = t->leecherCount; |
---|
1142 | |
---|
1143 | if( setme_seederCount ) |
---|
1144 | *setme_seederCount = t->seederCount; |
---|
1145 | |
---|
1146 | if( setme_downloaderCount ) |
---|
1147 | *setme_downloaderCount = t->downloaderCount; |
---|
1148 | } |
---|
1149 | |
---|
1150 | void |
---|
1151 | tr_trackerStart( tr_tracker * t ) |
---|
1152 | { |
---|
1153 | if( t && !t->isRunning ) |
---|
1154 | { |
---|
1155 | tr_torrent * tor; |
---|
1156 | |
---|
1157 | /* change the peer-id */ |
---|
1158 | tr_free( t->peer_id ); |
---|
1159 | t->peer_id = tr_peerIdNew( ); |
---|
1160 | if(( tor = tr_torrentFindFromHash( t->session, t->hash ))) { |
---|
1161 | tr_free( tor->peer_id ); |
---|
1162 | tor->peer_id = (uint8_t*) tr_strdup( t->peer_id ); |
---|
1163 | } |
---|
1164 | |
---|
1165 | t->isRunning = 1; |
---|
1166 | enqueueRequest( t->session, t, TR_REQ_STARTED ); |
---|
1167 | } |
---|
1168 | } |
---|
1169 | |
---|
1170 | void |
---|
1171 | tr_trackerReannounce( tr_tracker * t ) |
---|
1172 | { |
---|
1173 | enqueueRequest( t->session, t, TR_REQ_REANNOUNCE ); |
---|
1174 | } |
---|
1175 | |
---|
1176 | void |
---|
1177 | tr_trackerCompleted( tr_tracker * t ) |
---|
1178 | { |
---|
1179 | enqueueRequest( t->session, t, TR_REQ_COMPLETED ); |
---|
1180 | } |
---|
1181 | |
---|
1182 | void |
---|
1183 | tr_trackerStop( tr_tracker * t ) |
---|
1184 | { |
---|
1185 | if( t && t->isRunning ) |
---|
1186 | { |
---|
1187 | t->isRunning = 0; |
---|
1188 | t->reannounceAt = TR_TRACKER_STOPPED; |
---|
1189 | t->manualAnnounceAllowedAt = TR_TRACKER_STOPPED; |
---|
1190 | enqueueRequest( t->session, t, TR_REQ_STOPPED ); |
---|
1191 | } |
---|
1192 | } |
---|
1193 | |
---|
1194 | void |
---|
1195 | tr_trackerChangeMyPort( tr_tracker * t ) |
---|
1196 | { |
---|
1197 | if( t->isRunning ) |
---|
1198 | tr_trackerReannounce( t ); |
---|
1199 | } |
---|
1200 | |
---|
1201 | void |
---|
1202 | tr_trackerStat( const tr_tracker * t, |
---|
1203 | struct tr_stat * setme ) |
---|
1204 | { |
---|
1205 | assert( t ); |
---|
1206 | assert( setme ); |
---|
1207 | |
---|
1208 | setme->lastScrapeTime = t->lastScrapeTime; |
---|
1209 | setme->nextScrapeTime = t->scrapeAt; |
---|
1210 | setme->lastAnnounceTime = t->lastAnnounceTime; |
---|
1211 | setme->nextAnnounceTime = t->reannounceAt; |
---|
1212 | setme->manualAnnounceTime = t->manualAnnounceAllowedAt; |
---|
1213 | |
---|
1214 | if( t->lastScrapeResponse == -1 ) /* never been scraped */ |
---|
1215 | *setme->scrapeResponse = '\0'; |
---|
1216 | else |
---|
1217 | tr_snprintf( setme->scrapeResponse, |
---|
1218 | sizeof( setme->scrapeResponse ), |
---|
1219 | "%s (%ld)", |
---|
1220 | tr_webGetResponseStr( t->lastScrapeResponse ), |
---|
1221 | t->lastScrapeResponse ); |
---|
1222 | |
---|
1223 | if( t->lastAnnounceResponse == -1 ) /* never been announced */ |
---|
1224 | *setme->announceResponse = '\0'; |
---|
1225 | else |
---|
1226 | tr_snprintf( setme->announceResponse, |
---|
1227 | sizeof( setme->announceResponse ), |
---|
1228 | "%s (%ld)", |
---|
1229 | tr_webGetResponseStr( t->lastAnnounceResponse ), |
---|
1230 | t->lastAnnounceResponse ); |
---|
1231 | } |
---|
1232 | |
---|