1 | /****************************************************************************** |
---|
2 | * Copyright (c) 2005 Eric Petit |
---|
3 | * |
---|
4 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
5 | * copy of this software and associated documentation files (the "Software"), |
---|
6 | * to deal in the Software without restriction, including without limitation |
---|
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
8 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
9 | * Software is furnished to do so, subject to the following conditions: |
---|
10 | * |
---|
11 | * The above copyright notice and this permission notice shall be included in |
---|
12 | * all copies or substantial portions of the Software. |
---|
13 | * |
---|
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
20 | * DEALINGS IN THE SOFTWARE. |
---|
21 | *****************************************************************************/ |
---|
22 | |
---|
23 | #include "transmission.h" |
---|
24 | |
---|
25 | struct tr_tracker_s |
---|
26 | { |
---|
27 | tr_torrent_t * tor; |
---|
28 | |
---|
29 | char * id; |
---|
30 | |
---|
31 | char started; |
---|
32 | char completed; |
---|
33 | char stopped; |
---|
34 | |
---|
35 | int interval; |
---|
36 | int seeders; |
---|
37 | int leechers; |
---|
38 | int hasManyPeers; |
---|
39 | |
---|
40 | uint64_t dateTry; |
---|
41 | uint64_t dateOk; |
---|
42 | |
---|
43 | #define TC_STATUS_IDLE 1 |
---|
44 | #define TC_STATUS_RESOLVE 2 |
---|
45 | #define TC_STATUS_CONNECT 4 |
---|
46 | #define TC_STATUS_RECV 8 |
---|
47 | char status; |
---|
48 | |
---|
49 | tr_resolve_t * resolve; |
---|
50 | int socket; |
---|
51 | uint8_t * buf; |
---|
52 | int size; |
---|
53 | int pos; |
---|
54 | |
---|
55 | int bindPort; |
---|
56 | int newPort; |
---|
57 | |
---|
58 | uint64_t download; |
---|
59 | uint64_t upload; |
---|
60 | }; |
---|
61 | |
---|
62 | static void sendQuery ( tr_tracker_t * tc ); |
---|
63 | static void recvAnswer ( tr_tracker_t * tc ); |
---|
64 | |
---|
65 | tr_tracker_t * tr_trackerInit( tr_torrent_t * tor ) |
---|
66 | { |
---|
67 | tr_tracker_t * tc; |
---|
68 | |
---|
69 | tc = calloc( 1, sizeof( tr_tracker_t ) ); |
---|
70 | tc->tor = tor; |
---|
71 | tc->id = tor->id; |
---|
72 | |
---|
73 | tc->started = 1; |
---|
74 | |
---|
75 | tc->seeders = -1; |
---|
76 | tc->leechers = -1; |
---|
77 | |
---|
78 | tc->status = TC_STATUS_IDLE; |
---|
79 | tc->size = 1024; |
---|
80 | tc->buf = malloc( tc->size ); |
---|
81 | |
---|
82 | tc->bindPort = *(tor->bindPort); |
---|
83 | tc->newPort = -1; |
---|
84 | |
---|
85 | tc->download = tor->downloaded; |
---|
86 | tc->upload = tor->uploaded; |
---|
87 | |
---|
88 | return tc; |
---|
89 | } |
---|
90 | |
---|
91 | static int shouldConnect( tr_tracker_t * tc ) |
---|
92 | { |
---|
93 | uint64_t now = tr_date(); |
---|
94 | |
---|
95 | /* In any case, always wait 5 seconds between two requests */ |
---|
96 | if( now < tc->dateTry + 5000 ) |
---|
97 | { |
---|
98 | return 0; |
---|
99 | } |
---|
100 | |
---|
101 | /* Do we need to send an event? */ |
---|
102 | if( tc->started || tc->completed || tc->stopped || 0 < tc->newPort ) |
---|
103 | { |
---|
104 | return 1; |
---|
105 | } |
---|
106 | |
---|
107 | /* Should we try and get more peers? */ |
---|
108 | if( now > tc->dateOk + 1000 * tc->interval ) |
---|
109 | { |
---|
110 | return 1; |
---|
111 | } |
---|
112 | |
---|
113 | /* If there is quite a lot of people on this torrent, stress |
---|
114 | the tracker a bit until we get a decent number of peers */ |
---|
115 | if( tc->hasManyPeers ) |
---|
116 | { |
---|
117 | if( tc->tor->peerCount < 5 && now > tc->dateOk + 10000 ) |
---|
118 | { |
---|
119 | return 1; |
---|
120 | } |
---|
121 | if( tc->tor->peerCount < 10 && now > tc->dateOk + 20000 ) |
---|
122 | { |
---|
123 | return 1; |
---|
124 | } |
---|
125 | if( tc->tor->peerCount < 15 && now > tc->dateOk + 30000 ) |
---|
126 | { |
---|
127 | return 1; |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | return 0; |
---|
132 | } |
---|
133 | |
---|
134 | void tr_trackerChangePort( tr_tracker_t * tc, int port ) |
---|
135 | { |
---|
136 | tc->newPort = port; |
---|
137 | } |
---|
138 | |
---|
139 | int tr_trackerPulse( tr_tracker_t * tc ) |
---|
140 | { |
---|
141 | tr_torrent_t * tor = tc->tor; |
---|
142 | tr_info_t * inf = &tor->info; |
---|
143 | uint64_t now = tr_date(); |
---|
144 | |
---|
145 | if( ( tc->status & TC_STATUS_IDLE ) && shouldConnect( tc ) ) |
---|
146 | { |
---|
147 | tc->resolve = tr_netResolveInit( inf->trackerAddress ); |
---|
148 | |
---|
149 | tr_inf( "Tracker: connecting to %s:%d (%s)", |
---|
150 | inf->trackerAddress, inf->trackerPort, |
---|
151 | tc->started ? "sending 'started'" : |
---|
152 | ( tc->completed ? "sending 'completed'" : |
---|
153 | ( tc->stopped ? "sending 'stopped'" : |
---|
154 | ( 0 < tc->newPort ? "sending 'stopped' to change port" : |
---|
155 | "getting peers" ) ) ) ); |
---|
156 | |
---|
157 | tc->status = TC_STATUS_RESOLVE; |
---|
158 | tc->dateTry = tr_date(); |
---|
159 | } |
---|
160 | |
---|
161 | if( tc->status & TC_STATUS_RESOLVE ) |
---|
162 | { |
---|
163 | int ret; |
---|
164 | struct in_addr addr; |
---|
165 | |
---|
166 | ret = tr_netResolvePulse( tc->resolve, &addr ); |
---|
167 | if( ret == TR_RESOLVE_WAIT ) |
---|
168 | { |
---|
169 | return 0; |
---|
170 | } |
---|
171 | else |
---|
172 | { |
---|
173 | tr_netResolveClose( tc->resolve ); |
---|
174 | } |
---|
175 | |
---|
176 | if( ret == TR_RESOLVE_ERROR ) |
---|
177 | { |
---|
178 | tc->status = TC_STATUS_IDLE; |
---|
179 | return 0; |
---|
180 | } |
---|
181 | |
---|
182 | if( tr_fdSocketWillCreate( tor->fdlimit, 1 ) ) |
---|
183 | { |
---|
184 | tc->status = TC_STATUS_IDLE; |
---|
185 | return 0; |
---|
186 | } |
---|
187 | |
---|
188 | tc->socket = tr_netOpen( addr, htons( inf->trackerPort ) ); |
---|
189 | if( tc->socket < 0 ) |
---|
190 | { |
---|
191 | tr_fdSocketClosed( tor->fdlimit, 1 ); |
---|
192 | tc->status = TC_STATUS_IDLE; |
---|
193 | return 0; |
---|
194 | } |
---|
195 | |
---|
196 | tc->status = TC_STATUS_CONNECT; |
---|
197 | } |
---|
198 | |
---|
199 | if( tc->status & TC_STATUS_CONNECT ) |
---|
200 | { |
---|
201 | /* We are connecting to the tracker. Try to send the query */ |
---|
202 | sendQuery( tc ); |
---|
203 | } |
---|
204 | |
---|
205 | if( tc->status & TC_STATUS_RECV ) |
---|
206 | { |
---|
207 | /* Try to get something */ |
---|
208 | recvAnswer( tc ); |
---|
209 | } |
---|
210 | |
---|
211 | if( tc->status > TC_STATUS_IDLE && now > tc->dateTry + 60000 ) |
---|
212 | { |
---|
213 | /* Give up if the request wasn't successful within 60 seconds */ |
---|
214 | tr_inf( "Tracker: timeout reached (60 s)" ); |
---|
215 | |
---|
216 | tr_netClose( tc->socket ); |
---|
217 | tr_fdSocketClosed( tor->fdlimit, 1 ); |
---|
218 | |
---|
219 | tc->status = TC_STATUS_IDLE; |
---|
220 | tc->dateTry = tr_date(); |
---|
221 | } |
---|
222 | |
---|
223 | return 0; |
---|
224 | } |
---|
225 | |
---|
226 | void tr_trackerCompleted( tr_tracker_t * tc ) |
---|
227 | { |
---|
228 | tc->started = 0; |
---|
229 | tc->completed = 1; |
---|
230 | tc->stopped = 0; |
---|
231 | } |
---|
232 | |
---|
233 | void tr_trackerStopped( tr_tracker_t * tc ) |
---|
234 | { |
---|
235 | tr_torrent_t * tor = tc->tor; |
---|
236 | |
---|
237 | if( tc->status > TC_STATUS_CONNECT ) |
---|
238 | { |
---|
239 | /* If we are already sendy a query at the moment, we need to |
---|
240 | reconnect */ |
---|
241 | tr_netClose( tc->socket ); |
---|
242 | tr_fdSocketClosed( tor->fdlimit, 1 ); |
---|
243 | tc->status = TC_STATUS_IDLE; |
---|
244 | } |
---|
245 | |
---|
246 | tc->started = 0; |
---|
247 | tc->completed = 0; |
---|
248 | tc->stopped = 1; |
---|
249 | |
---|
250 | /* Even if we have connected recently, reconnect right now */ |
---|
251 | if( tc->status & TC_STATUS_IDLE ) |
---|
252 | { |
---|
253 | tc->dateTry = 0; |
---|
254 | } |
---|
255 | } |
---|
256 | |
---|
257 | void tr_trackerClose( tr_tracker_t * tc ) |
---|
258 | { |
---|
259 | tr_torrent_t * tor = tc->tor; |
---|
260 | |
---|
261 | if( tc->status == TC_STATUS_RESOLVE ) |
---|
262 | { |
---|
263 | tr_netResolveClose( tc->resolve ); |
---|
264 | } |
---|
265 | else if( tc->status > TC_STATUS_RESOLVE ) |
---|
266 | { |
---|
267 | tr_netClose( tc->socket ); |
---|
268 | tr_fdSocketClosed( tor->fdlimit, 1 ); |
---|
269 | } |
---|
270 | free( tc->buf ); |
---|
271 | free( tc ); |
---|
272 | } |
---|
273 | |
---|
274 | static void sendQuery( tr_tracker_t * tc ) |
---|
275 | { |
---|
276 | tr_torrent_t * tor = tc->tor; |
---|
277 | tr_info_t * inf = &tor->info; |
---|
278 | |
---|
279 | char * event; |
---|
280 | uint64_t left; |
---|
281 | int ret; |
---|
282 | uint64_t down; |
---|
283 | uint64_t up; |
---|
284 | |
---|
285 | down = tor->downloaded - tc->download; |
---|
286 | up = tor->uploaded - tc->upload; |
---|
287 | if( tc->started ) |
---|
288 | { |
---|
289 | event = "&event=started"; |
---|
290 | down = up = 0; |
---|
291 | |
---|
292 | if( 0 < tc->newPort ) |
---|
293 | { |
---|
294 | tc->bindPort = tc->newPort; |
---|
295 | tc->newPort = -1; |
---|
296 | } |
---|
297 | } |
---|
298 | else if( tc->completed ) |
---|
299 | { |
---|
300 | event = "&event=completed"; |
---|
301 | } |
---|
302 | else if( tc->stopped || 0 < tc->newPort ) |
---|
303 | { |
---|
304 | event = "&event=stopped"; |
---|
305 | } |
---|
306 | else |
---|
307 | { |
---|
308 | event = ""; |
---|
309 | } |
---|
310 | |
---|
311 | left = tr_cpLeftBytes( tor->completion ); |
---|
312 | |
---|
313 | ret = snprintf( (char *) tc->buf, tc->size, |
---|
314 | "GET %s?" |
---|
315 | "info_hash=%s&" |
---|
316 | "peer_id=%s&" |
---|
317 | "port=%d&" |
---|
318 | "uploaded=%lld&" |
---|
319 | "downloaded=%lld&" |
---|
320 | "left=%lld&" |
---|
321 | "compact=1&" |
---|
322 | "numwant=50&" |
---|
323 | "key=%s" |
---|
324 | "%s " |
---|
325 | "HTTP/1.1\r\n" |
---|
326 | "Host: %s\r\n" |
---|
327 | "User-Agent: Transmission/%d.%d\r\n" |
---|
328 | "Connection: close\r\n\r\n", |
---|
329 | inf->trackerAnnounce, tor->hashString, tc->id, |
---|
330 | tc->bindPort, up, down, |
---|
331 | left, tor->key, event, inf->trackerAddress, |
---|
332 | VERSION_MAJOR, VERSION_MINOR ); |
---|
333 | |
---|
334 | ret = tr_netSend( tc->socket, tc->buf, ret ); |
---|
335 | if( ret & TR_NET_CLOSE ) |
---|
336 | { |
---|
337 | tr_inf( "Tracker: connection failed" ); |
---|
338 | tr_netClose( tc->socket ); |
---|
339 | tr_fdSocketClosed( tor->fdlimit, 1 ); |
---|
340 | tc->status = TC_STATUS_IDLE; |
---|
341 | tc->dateTry = tr_date(); |
---|
342 | } |
---|
343 | else if( !( ret & TR_NET_BLOCK ) ) |
---|
344 | { |
---|
345 | // printf( "Tracker: sent %s", tc->buf ); |
---|
346 | tc->status = TC_STATUS_RECV; |
---|
347 | tc->pos = 0; |
---|
348 | } |
---|
349 | } |
---|
350 | |
---|
351 | static void recvAnswer( tr_tracker_t * tc ) |
---|
352 | { |
---|
353 | tr_torrent_t * tor = tc->tor; |
---|
354 | int ret; |
---|
355 | int i; |
---|
356 | benc_val_t beAll; |
---|
357 | benc_val_t * bePeers, * beFoo; |
---|
358 | uint8_t * body; |
---|
359 | int bodylen; |
---|
360 | |
---|
361 | if( tc->pos == tc->size ) |
---|
362 | { |
---|
363 | tc->size *= 2; |
---|
364 | tc->buf = realloc( tc->buf, tc->size ); |
---|
365 | } |
---|
366 | |
---|
367 | ret = tr_netRecv( tc->socket, &tc->buf[tc->pos], |
---|
368 | tc->size - tc->pos ); |
---|
369 | |
---|
370 | if( ret & TR_NET_BLOCK ) |
---|
371 | { |
---|
372 | return; |
---|
373 | } |
---|
374 | if( !( ret & TR_NET_CLOSE ) ) |
---|
375 | { |
---|
376 | // printf( "got %d bytes\n", ret ); |
---|
377 | tc->pos += ret; |
---|
378 | return; |
---|
379 | } |
---|
380 | |
---|
381 | tr_netClose( tc->socket ); |
---|
382 | tr_fdSocketClosed( tor->fdlimit, 1 ); |
---|
383 | // printf( "connection closed, got total %d bytes\n", tc->pos ); |
---|
384 | |
---|
385 | tc->status = TC_STATUS_IDLE; |
---|
386 | tc->dateTry = tr_date(); |
---|
387 | |
---|
388 | if( tc->pos < 12 || ( 0 != memcmp( tc->buf, "HTTP/1.0 ", 9 ) && |
---|
389 | 0 != memcmp( tc->buf, "HTTP/1.1 ", 9 ) ) ) |
---|
390 | { |
---|
391 | /* We don't have a complete HTTP status line */ |
---|
392 | tr_inf( "Tracker: incomplete HTTP status line" ); |
---|
393 | return; |
---|
394 | } |
---|
395 | |
---|
396 | if( '2' != tc->buf[9] ) |
---|
397 | { |
---|
398 | /* we didn't get a 2xx status code */ |
---|
399 | tr_err( "Tracker: invalid HTTP status code: %c%c%c", |
---|
400 | tc->buf[9], tc->buf[10], tc->buf[11] ); |
---|
401 | return; |
---|
402 | } |
---|
403 | |
---|
404 | /* find the end of the http headers */ |
---|
405 | body = tr_memmem( tc->buf, tc->pos, "\015\012\015\012", 4 ); |
---|
406 | if( NULL != body ) |
---|
407 | { |
---|
408 | body += 4; |
---|
409 | } |
---|
410 | /* hooray for trackers that violate the HTTP spec */ |
---|
411 | else if( NULL != ( body = tr_memmem( tc->buf, tc->pos, "\015\015", 2 ) ) || |
---|
412 | NULL != ( body = tr_memmem( tc->buf, tc->pos, "\012\012", 2 ) ) ) |
---|
413 | { |
---|
414 | body += 2; |
---|
415 | } |
---|
416 | else |
---|
417 | { |
---|
418 | tr_err( "Tracker: could not find end of HTTP headers" ); |
---|
419 | return; |
---|
420 | } |
---|
421 | bodylen = tc->pos - (body - tc->buf); |
---|
422 | |
---|
423 | /* Find the beginning of the dictionary */ |
---|
424 | for( i = 0; i < bodylen; i++ ) |
---|
425 | { |
---|
426 | if( body[i] == 'd' ) |
---|
427 | { |
---|
428 | /* This must be it */ |
---|
429 | break; |
---|
430 | } |
---|
431 | } |
---|
432 | |
---|
433 | if( i >= bodylen ) |
---|
434 | { |
---|
435 | if( tc->stopped || 0 < tc->newPort ) |
---|
436 | { |
---|
437 | goto nodict; |
---|
438 | } |
---|
439 | tr_err( "Tracker: no dictionary in answer" ); |
---|
440 | // printf( "%s\n", body ); |
---|
441 | return; |
---|
442 | } |
---|
443 | |
---|
444 | if( tr_bencLoad( &body[i], &beAll, NULL ) ) |
---|
445 | { |
---|
446 | tr_err( "Tracker: error parsing bencoded data" ); |
---|
447 | return; |
---|
448 | } |
---|
449 | |
---|
450 | // tr_bencPrint( &beAll ); |
---|
451 | |
---|
452 | if( ( bePeers = tr_bencDictFind( &beAll, "failure reason" ) ) ) |
---|
453 | { |
---|
454 | tr_err( "Tracker: %s", bePeers->val.s.s ); |
---|
455 | tor->error |= TR_ETRACKER; |
---|
456 | snprintf( tor->trackerError, sizeof( tor->trackerError ), |
---|
457 | "%s", bePeers->val.s.s ); |
---|
458 | goto cleanup; |
---|
459 | } |
---|
460 | tor->error &= ~TR_ETRACKER; |
---|
461 | |
---|
462 | if( !tc->interval ) |
---|
463 | { |
---|
464 | /* Get the tracker interval, ignore it if it is not between |
---|
465 | 10 sec and 5 mins */ |
---|
466 | if( !( beFoo = tr_bencDictFind( &beAll, "interval" ) ) || |
---|
467 | !( beFoo->type & TYPE_INT ) ) |
---|
468 | { |
---|
469 | tr_err( "Tracker: no 'interval' field" ); |
---|
470 | goto cleanup; |
---|
471 | } |
---|
472 | |
---|
473 | tc->interval = beFoo->val.i; |
---|
474 | tc->interval = MIN( tc->interval, 300 ); |
---|
475 | tc->interval = MAX( 10, tc->interval ); |
---|
476 | |
---|
477 | tr_inf( "Tracker: interval = %d seconds", tc->interval ); |
---|
478 | } |
---|
479 | |
---|
480 | if( ( beFoo = tr_bencDictFind( &beAll, "complete" ) ) && |
---|
481 | ( beFoo->type & TYPE_INT ) ) |
---|
482 | { |
---|
483 | tc->seeders = beFoo->val.i; |
---|
484 | } |
---|
485 | if( ( beFoo = tr_bencDictFind( &beAll, "incomplete" ) ) && |
---|
486 | ( beFoo->type & TYPE_INT ) ) |
---|
487 | { |
---|
488 | tc->leechers = beFoo->val.i; |
---|
489 | } |
---|
490 | if( tc->seeders + tc->leechers >= 50 ) |
---|
491 | { |
---|
492 | tc->hasManyPeers = 1; |
---|
493 | } |
---|
494 | |
---|
495 | if( !( bePeers = tr_bencDictFind( &beAll, "peers" ) ) ) |
---|
496 | { |
---|
497 | tr_err( "Tracker: no \"peers\" field" ); |
---|
498 | goto cleanup; |
---|
499 | } |
---|
500 | |
---|
501 | if( bePeers->type & TYPE_LIST ) |
---|
502 | { |
---|
503 | char * ip; |
---|
504 | int port; |
---|
505 | |
---|
506 | /* Original protocol */ |
---|
507 | tr_inf( "Tracker: got %d peers", bePeers->val.l.count ); |
---|
508 | |
---|
509 | for( i = 0; i < bePeers->val.l.count; i++ ) |
---|
510 | { |
---|
511 | beFoo = tr_bencDictFind( &bePeers->val.l.vals[i], "ip" ); |
---|
512 | if( !beFoo ) |
---|
513 | continue; |
---|
514 | ip = beFoo->val.s.s; |
---|
515 | beFoo = tr_bencDictFind( &bePeers->val.l.vals[i], "port" ); |
---|
516 | if( !beFoo ) |
---|
517 | continue; |
---|
518 | port = beFoo->val.i; |
---|
519 | |
---|
520 | tr_peerAddOld( tor, ip, port ); |
---|
521 | } |
---|
522 | |
---|
523 | if( bePeers->val.l.count >= 50 ) |
---|
524 | { |
---|
525 | tc->hasManyPeers = 1; |
---|
526 | } |
---|
527 | } |
---|
528 | else if( bePeers->type & TYPE_STR ) |
---|
529 | { |
---|
530 | struct in_addr addr; |
---|
531 | in_port_t port; |
---|
532 | |
---|
533 | /* "Compact" extension */ |
---|
534 | if( bePeers->val.s.i % 6 ) |
---|
535 | { |
---|
536 | tr_err( "Tracker: \"peers\" of size %d", |
---|
537 | bePeers->val.s.i ); |
---|
538 | tr_lockUnlock( &tor->lock ); |
---|
539 | goto cleanup; |
---|
540 | } |
---|
541 | |
---|
542 | tr_inf( "Tracker: got %d peers", bePeers->val.s.i / 6 ); |
---|
543 | for( i = 0; i < bePeers->val.s.i / 6; i++ ) |
---|
544 | { |
---|
545 | memcpy( &addr, &bePeers->val.s.s[6*i], 4 ); |
---|
546 | memcpy( &port, &bePeers->val.s.s[6*i+4], 2 ); |
---|
547 | |
---|
548 | tr_peerAddCompact( tor, addr, port ); |
---|
549 | } |
---|
550 | |
---|
551 | if( bePeers->val.s.i / 6 >= 50 ) |
---|
552 | { |
---|
553 | tc->hasManyPeers = 1; |
---|
554 | } |
---|
555 | } |
---|
556 | |
---|
557 | nodict: |
---|
558 | /* Success */ |
---|
559 | tc->started = 0; |
---|
560 | tc->completed = 0; |
---|
561 | tc->dateOk = tr_date(); |
---|
562 | |
---|
563 | if( tc->stopped ) |
---|
564 | { |
---|
565 | tor->status = TR_STATUS_STOPPED; |
---|
566 | tc->stopped = 0; |
---|
567 | } |
---|
568 | else if( 0 < tc->newPort ) |
---|
569 | { |
---|
570 | tc->started = 1; |
---|
571 | } |
---|
572 | |
---|
573 | cleanup: |
---|
574 | tr_bencFree( &beAll ); |
---|
575 | } |
---|
576 | |
---|
577 | int tr_trackerScrape( tr_torrent_t * tor, int * seeders, int * leechers ) |
---|
578 | { |
---|
579 | tr_info_t * inf = &tor->info; |
---|
580 | |
---|
581 | int s, i, ret; |
---|
582 | uint8_t buf[1024]; |
---|
583 | benc_val_t scrape, * val1, * val2; |
---|
584 | struct in_addr addr; |
---|
585 | uint64_t date; |
---|
586 | int pos, len; |
---|
587 | |
---|
588 | if( !tor->scrape[0] ) |
---|
589 | { |
---|
590 | /* scrape not supported */ |
---|
591 | return 1; |
---|
592 | } |
---|
593 | |
---|
594 | if( tr_netResolve( inf->trackerAddress, &addr ) ) |
---|
595 | { |
---|
596 | return 0; |
---|
597 | } |
---|
598 | s = tr_netOpen( addr, htons( inf->trackerPort ) ); |
---|
599 | if( s < 0 ) |
---|
600 | { |
---|
601 | return 1; |
---|
602 | } |
---|
603 | |
---|
604 | len = snprintf( (char *) buf, sizeof( buf ), |
---|
605 | "GET %s?info_hash=%s HTTP/1.1\r\n" |
---|
606 | "Host: %s\r\n" |
---|
607 | "Connection: close\r\n\r\n", |
---|
608 | tor->scrape, tor->hashString, |
---|
609 | inf->trackerAddress ); |
---|
610 | |
---|
611 | for( date = tr_date();; ) |
---|
612 | { |
---|
613 | ret = tr_netSend( s, buf, len ); |
---|
614 | if( ret & TR_NET_CLOSE ) |
---|
615 | { |
---|
616 | fprintf( stderr, "Could not connect to tracker\n" ); |
---|
617 | tr_netClose( s ); |
---|
618 | return 1; |
---|
619 | } |
---|
620 | else if( ret & TR_NET_BLOCK ) |
---|
621 | { |
---|
622 | if( tr_date() > date + 10000 ) |
---|
623 | { |
---|
624 | fprintf( stderr, "Could not connect to tracker\n" ); |
---|
625 | tr_netClose( s ); |
---|
626 | return 1; |
---|
627 | } |
---|
628 | } |
---|
629 | else |
---|
630 | { |
---|
631 | break; |
---|
632 | } |
---|
633 | tr_wait( 10 ); |
---|
634 | } |
---|
635 | |
---|
636 | pos = 0; |
---|
637 | for( date = tr_date();; ) |
---|
638 | { |
---|
639 | ret = tr_netRecv( s, &buf[pos], sizeof( buf ) - pos ); |
---|
640 | if( ret & TR_NET_CLOSE ) |
---|
641 | { |
---|
642 | break; |
---|
643 | } |
---|
644 | else if( ret & TR_NET_BLOCK ) |
---|
645 | { |
---|
646 | if( tr_date() > date + 10000 ) |
---|
647 | { |
---|
648 | fprintf( stderr, "Could not read from tracker\n" ); |
---|
649 | tr_netClose( s ); |
---|
650 | return 1; |
---|
651 | } |
---|
652 | } |
---|
653 | else |
---|
654 | { |
---|
655 | pos += ret; |
---|
656 | } |
---|
657 | tr_wait( 10 ); |
---|
658 | } |
---|
659 | |
---|
660 | if( pos < 1 ) |
---|
661 | { |
---|
662 | fprintf( stderr, "Could not read from tracker\n" ); |
---|
663 | tr_netClose( s ); |
---|
664 | return 1; |
---|
665 | } |
---|
666 | |
---|
667 | for( i = 0; i < pos - 8; i++ ) |
---|
668 | { |
---|
669 | if( !memcmp( &buf[i], "d5:files", 8 ) ) |
---|
670 | { |
---|
671 | break; |
---|
672 | } |
---|
673 | } |
---|
674 | if( i >= pos - 8 ) |
---|
675 | { |
---|
676 | return 1; |
---|
677 | } |
---|
678 | if( tr_bencLoad( &buf[i], &scrape, NULL ) ) |
---|
679 | { |
---|
680 | return 1; |
---|
681 | } |
---|
682 | |
---|
683 | val1 = tr_bencDictFind( &scrape, "files" ); |
---|
684 | if( !val1 ) |
---|
685 | { |
---|
686 | return 1; |
---|
687 | } |
---|
688 | val1 = &val1->val.l.vals[1]; |
---|
689 | if( !val1 ) |
---|
690 | { |
---|
691 | return 1; |
---|
692 | } |
---|
693 | val2 = tr_bencDictFind( val1, "complete" ); |
---|
694 | if( !val2 ) |
---|
695 | { |
---|
696 | return 1; |
---|
697 | } |
---|
698 | *seeders = val2->val.i; |
---|
699 | val2 = tr_bencDictFind( val1, "incomplete" ); |
---|
700 | if( !val2 ) |
---|
701 | { |
---|
702 | return 1; |
---|
703 | } |
---|
704 | *leechers = val2->val.i; |
---|
705 | tr_bencFree( &scrape ); |
---|
706 | |
---|
707 | return 0; |
---|
708 | } |
---|
709 | |
---|
710 | int tr_trackerSeeders( tr_tracker_t * tc ) |
---|
711 | { |
---|
712 | if( !tc ) |
---|
713 | { |
---|
714 | return -1; |
---|
715 | } |
---|
716 | return tc->seeders; |
---|
717 | } |
---|
718 | |
---|
719 | int tr_trackerLeechers( tr_tracker_t * tc ) |
---|
720 | { |
---|
721 | if( !tc ) |
---|
722 | { |
---|
723 | return -1; |
---|
724 | } |
---|
725 | return tc->leechers; |
---|
726 | } |
---|