1 | /****************************************************************************** |
---|
2 | * $Id: transmission.c 525 2006-07-04 22:31:14Z joshe $ |
---|
3 | * |
---|
4 | * Copyright (c) 2005-2006 Transmission authors and contributors |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
7 | * copy of this software and associated documentation files (the "Software"), |
---|
8 | * to deal in the Software without restriction, including without limitation |
---|
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
10 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
11 | * Software is furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
22 | * DEALINGS IN THE SOFTWARE. |
---|
23 | *****************************************************************************/ |
---|
24 | |
---|
25 | #include "transmission.h" |
---|
26 | |
---|
27 | /*********************************************************************** |
---|
28 | * Local prototypes |
---|
29 | **********************************************************************/ |
---|
30 | static tr_torrent_t * torrentRealInit( tr_handle_t *, tr_torrent_t * tor, |
---|
31 | int flags, int * error ); |
---|
32 | static void torrentReallyStop( tr_torrent_t * ); |
---|
33 | static void downloadLoop( void * ); |
---|
34 | static void acceptLoop( void * ); |
---|
35 | static void acceptStop( tr_handle_t * h ); |
---|
36 | |
---|
37 | /*********************************************************************** |
---|
38 | * tr_init |
---|
39 | *********************************************************************** |
---|
40 | * Allocates a tr_handle_t structure and initializes a few things |
---|
41 | **********************************************************************/ |
---|
42 | tr_handle_t * tr_init() |
---|
43 | { |
---|
44 | tr_handle_t * h; |
---|
45 | int i, r; |
---|
46 | |
---|
47 | tr_netResolveThreadInit(); |
---|
48 | |
---|
49 | h = calloc( sizeof( tr_handle_t ), 1 ); |
---|
50 | |
---|
51 | /* Generate a peer id : "-TRxxyy-" + 12 random alphanumeric |
---|
52 | characters, where xx is the major version number and yy the |
---|
53 | minor version number (Azureus-style) */ |
---|
54 | sprintf( h->id, "-TR%02d%02d-", VERSION_MAJOR, VERSION_MINOR ); |
---|
55 | for( i = 8; i < 20; i++ ) |
---|
56 | { |
---|
57 | r = tr_rand( 36 ); |
---|
58 | h->id[i] = ( r < 26 ) ? ( 'a' + r ) : ( '0' + r - 26 ) ; |
---|
59 | } |
---|
60 | |
---|
61 | /* Random key */ |
---|
62 | for( i = 0; i < 20; i++ ) |
---|
63 | { |
---|
64 | r = tr_rand( 36 ); |
---|
65 | h->key[i] = ( r < 26 ) ? ( 'a' + r ) : ( '0' + r - 26 ) ; |
---|
66 | } |
---|
67 | |
---|
68 | /* Don't exit when writing on a broken socket */ |
---|
69 | signal( SIGPIPE, SIG_IGN ); |
---|
70 | |
---|
71 | /* Initialize rate and file descripts controls */ |
---|
72 | h->upload = tr_rcInit(); |
---|
73 | h->download = tr_rcInit(); |
---|
74 | h->fdlimit = tr_fdInit(); |
---|
75 | h->choking = tr_chokingInit( h ); |
---|
76 | |
---|
77 | h->bindPort = -1; |
---|
78 | h->bindSocket = -1; |
---|
79 | |
---|
80 | h->acceptDie = 0; |
---|
81 | tr_lockInit( &h->acceptLock ); |
---|
82 | tr_threadCreate( &h->acceptThread, acceptLoop, h ); |
---|
83 | |
---|
84 | return h; |
---|
85 | } |
---|
86 | |
---|
87 | /*********************************************************************** |
---|
88 | * tr_setBindPort |
---|
89 | *********************************************************************** |
---|
90 | * |
---|
91 | **********************************************************************/ |
---|
92 | void tr_setBindPort( tr_handle_t * h, int port ) |
---|
93 | { |
---|
94 | int sock = -1; |
---|
95 | tr_torrent_t * tor; |
---|
96 | |
---|
97 | if( h->bindPort == port ) |
---|
98 | return; |
---|
99 | |
---|
100 | #ifndef BEOS_NETSERVER |
---|
101 | /* BeOS net_server seems to be unable to set incoming connections to |
---|
102 | non-blocking. Too bad. */ |
---|
103 | if( !tr_fdSocketWillCreate( h->fdlimit, 0 ) ) |
---|
104 | { |
---|
105 | /* XXX should handle failure here in a better way */ |
---|
106 | sock = tr_netBind( port ); |
---|
107 | } |
---|
108 | #else |
---|
109 | return; |
---|
110 | #endif |
---|
111 | |
---|
112 | tr_lockLock( &h->acceptLock ); |
---|
113 | |
---|
114 | h->bindPort = port; |
---|
115 | |
---|
116 | for( tor = h->torrentList; tor; tor = tor->next ) |
---|
117 | { |
---|
118 | tr_lockLock( &tor->lock ); |
---|
119 | if( NULL != tor->tracker ) |
---|
120 | { |
---|
121 | tr_trackerChangePort( tor->tracker, port ); |
---|
122 | } |
---|
123 | tr_lockUnlock( &tor->lock ); |
---|
124 | } |
---|
125 | |
---|
126 | if( h->bindSocket > -1 ) |
---|
127 | { |
---|
128 | tr_netClose( h->bindSocket ); |
---|
129 | tr_fdSocketClosed( h->fdlimit, 0 ); |
---|
130 | } |
---|
131 | |
---|
132 | h->bindSocket = sock; |
---|
133 | |
---|
134 | tr_lockUnlock( &h->acceptLock ); |
---|
135 | } |
---|
136 | |
---|
137 | /*********************************************************************** |
---|
138 | * tr_setUploadLimit |
---|
139 | *********************************************************************** |
---|
140 | * |
---|
141 | **********************************************************************/ |
---|
142 | void tr_setUploadLimit( tr_handle_t * h, int limit ) |
---|
143 | { |
---|
144 | tr_rcSetLimit( h->upload, limit ); |
---|
145 | tr_chokingSetLimit( h->choking, limit ); |
---|
146 | } |
---|
147 | |
---|
148 | /*********************************************************************** |
---|
149 | * tr_setDownloadLimit |
---|
150 | *********************************************************************** |
---|
151 | * |
---|
152 | **********************************************************************/ |
---|
153 | void tr_setDownloadLimit( tr_handle_t * h, int limit ) |
---|
154 | { |
---|
155 | tr_rcSetLimit( h->download, limit ); |
---|
156 | } |
---|
157 | |
---|
158 | /*********************************************************************** |
---|
159 | * tr_torrentRates |
---|
160 | *********************************************************************** |
---|
161 | * |
---|
162 | **********************************************************************/ |
---|
163 | void tr_torrentRates( tr_handle_t * h, float * dl, float * ul ) |
---|
164 | { |
---|
165 | tr_torrent_t * tor; |
---|
166 | |
---|
167 | *dl = 0.0; |
---|
168 | *ul = 0.0; |
---|
169 | for( tor = h->torrentList; tor; tor = tor->next ) |
---|
170 | { |
---|
171 | tr_lockLock( &tor->lock ); |
---|
172 | if( tor->status & TR_STATUS_DOWNLOAD ) |
---|
173 | *dl += tr_rcRate( tor->download ); |
---|
174 | *ul += tr_rcRate( tor->upload ); |
---|
175 | tr_lockUnlock( &tor->lock ); |
---|
176 | } |
---|
177 | } |
---|
178 | |
---|
179 | tr_torrent_t * tr_torrentInit( tr_handle_t * h, const char * path, |
---|
180 | int flags, int * error ) |
---|
181 | { |
---|
182 | tr_torrent_t * tor = calloc( sizeof( tr_torrent_t ), 1 ); |
---|
183 | int saveCopy = ( TR_FSAVEPRIVATE & flags ); |
---|
184 | |
---|
185 | /* Parse torrent file */ |
---|
186 | if( tr_metainfoParse( &tor->info, path, NULL, saveCopy ) ) |
---|
187 | { |
---|
188 | *error = TR_EINVALID; |
---|
189 | free( tor ); |
---|
190 | return NULL; |
---|
191 | } |
---|
192 | |
---|
193 | return torrentRealInit( h, tor, flags, error ); |
---|
194 | } |
---|
195 | |
---|
196 | tr_torrent_t * tr_torrentInitSaved( tr_handle_t * h, const char * hashStr, |
---|
197 | int flags, int * error ) |
---|
198 | { |
---|
199 | tr_torrent_t * tor = calloc( sizeof( tr_torrent_t ), 1 ); |
---|
200 | |
---|
201 | /* Parse torrent file */ |
---|
202 | if( tr_metainfoParse( &tor->info, NULL, hashStr, 0 ) ) |
---|
203 | { |
---|
204 | *error = TR_EINVALID; |
---|
205 | free( tor ); |
---|
206 | return NULL; |
---|
207 | } |
---|
208 | |
---|
209 | return torrentRealInit( h, tor, ( TR_FSAVEPRIVATE | flags ), error ); |
---|
210 | } |
---|
211 | |
---|
212 | /*********************************************************************** |
---|
213 | * tr_torrentInit |
---|
214 | *********************************************************************** |
---|
215 | * Allocates a tr_torrent_t structure, then relies on tr_metainfoParse |
---|
216 | * to fill it. |
---|
217 | **********************************************************************/ |
---|
218 | static tr_torrent_t * torrentRealInit( tr_handle_t * h, tr_torrent_t * tor, |
---|
219 | int flags, int * error ) |
---|
220 | { |
---|
221 | tr_torrent_t * tor_tmp; |
---|
222 | tr_info_t * inf; |
---|
223 | int i; |
---|
224 | char * s1, * s2; |
---|
225 | |
---|
226 | inf = &tor->info; |
---|
227 | inf->flags = flags; |
---|
228 | |
---|
229 | /* Make sure this torrent is not already open */ |
---|
230 | for( tor_tmp = h->torrentList; tor_tmp; tor_tmp = tor_tmp->next ) |
---|
231 | { |
---|
232 | if( !memcmp( tor->info.hash, tor_tmp->info.hash, |
---|
233 | SHA_DIGEST_LENGTH ) ) |
---|
234 | { |
---|
235 | *error = TR_EDUPLICATE; |
---|
236 | free( tor ); |
---|
237 | return NULL; |
---|
238 | } |
---|
239 | } |
---|
240 | |
---|
241 | tor->status = TR_STATUS_PAUSE; |
---|
242 | tor->id = h->id; |
---|
243 | tor->key = h->key; |
---|
244 | tor->bindPort = &h->bindPort; |
---|
245 | tor->finished = 0; |
---|
246 | |
---|
247 | |
---|
248 | /* Guess scrape URL */ |
---|
249 | s1 = strchr( inf->trackerAnnounce, '/' ); |
---|
250 | while( ( s2 = strchr( s1 + 1, '/' ) ) ) |
---|
251 | { |
---|
252 | s1 = s2; |
---|
253 | } |
---|
254 | s1++; |
---|
255 | if( !strncmp( s1, "announce", 8 ) ) |
---|
256 | { |
---|
257 | int pre = (long) s1 - (long) inf->trackerAnnounce; |
---|
258 | int post = strlen( inf->trackerAnnounce ) - pre - 8; |
---|
259 | memcpy( tor->scrape, inf->trackerAnnounce, pre ); |
---|
260 | sprintf( &tor->scrape[pre], "scrape" ); |
---|
261 | memcpy( &tor->scrape[pre+6], &inf->trackerAnnounce[pre+8], post ); |
---|
262 | } |
---|
263 | |
---|
264 | /* Escaped info hash for HTTP queries */ |
---|
265 | for( i = 0; i < SHA_DIGEST_LENGTH; i++ ) |
---|
266 | { |
---|
267 | sprintf( &tor->hashString[3*i], "%%%02x", inf->hash[i] ); |
---|
268 | } |
---|
269 | |
---|
270 | /* Block size: usually 16 ko, or less if we have to */ |
---|
271 | tor->blockSize = MIN( inf->pieceSize, 1 << 14 ); |
---|
272 | tor->blockCount = ( inf->totalSize + tor->blockSize - 1 ) / |
---|
273 | tor->blockSize; |
---|
274 | tor->completion = tr_cpInit( tor ); |
---|
275 | |
---|
276 | tr_lockInit( &tor->lock ); |
---|
277 | |
---|
278 | tor->globalUpload = h->upload; |
---|
279 | tor->globalDownload = h->download; |
---|
280 | tor->fdlimit = h->fdlimit; |
---|
281 | tor->upload = tr_rcInit(); |
---|
282 | tor->download = tr_rcInit(); |
---|
283 | |
---|
284 | /* We have a new torrent */ |
---|
285 | tr_lockLock( &h->acceptLock ); |
---|
286 | tor->prev = NULL; |
---|
287 | tor->next = h->torrentList; |
---|
288 | if( tor->next ) |
---|
289 | { |
---|
290 | tor->next->prev = tor; |
---|
291 | } |
---|
292 | h->torrentList = tor; |
---|
293 | (h->torrentCount)++; |
---|
294 | tr_lockUnlock( &h->acceptLock ); |
---|
295 | |
---|
296 | if( 0 > h->bindPort ) |
---|
297 | { |
---|
298 | tr_setBindPort( h, TR_DEFAULT_PORT ); |
---|
299 | } |
---|
300 | |
---|
301 | return tor; |
---|
302 | } |
---|
303 | |
---|
304 | tr_info_t * tr_torrentInfo( tr_torrent_t * tor ) |
---|
305 | { |
---|
306 | return &tor->info; |
---|
307 | } |
---|
308 | |
---|
309 | /*********************************************************************** |
---|
310 | * tr_torrentScrape |
---|
311 | **********************************************************************/ |
---|
312 | int tr_torrentScrape( tr_torrent_t * tor, int * s, int * l ) |
---|
313 | { |
---|
314 | return tr_trackerScrape( tor, s, l ); |
---|
315 | } |
---|
316 | |
---|
317 | void tr_torrentSetFolder( tr_torrent_t * tor, const char * path ) |
---|
318 | { |
---|
319 | tor->destination = strdup( path ); |
---|
320 | tr_ioLoadResume( tor ); |
---|
321 | } |
---|
322 | |
---|
323 | char * tr_torrentGetFolder( tr_torrent_t * tor ) |
---|
324 | { |
---|
325 | return tor->destination; |
---|
326 | } |
---|
327 | |
---|
328 | void tr_torrentStart( tr_torrent_t * tor ) |
---|
329 | { |
---|
330 | if( tor->status & ( TR_STATUS_STOPPING | TR_STATUS_STOPPED ) ) |
---|
331 | { |
---|
332 | /* Join the thread first */ |
---|
333 | torrentReallyStop( tor ); |
---|
334 | } |
---|
335 | |
---|
336 | tor->status = TR_STATUS_CHECK; |
---|
337 | tor->tracker = tr_trackerInit( tor ); |
---|
338 | |
---|
339 | tor->date = tr_date(); |
---|
340 | tor->die = 0; |
---|
341 | tr_threadCreate( &tor->thread, downloadLoop, tor ); |
---|
342 | } |
---|
343 | |
---|
344 | void tr_torrentStop( tr_torrent_t * tor ) |
---|
345 | { |
---|
346 | tr_lockLock( &tor->lock ); |
---|
347 | tr_trackerStopped( tor->tracker ); |
---|
348 | tr_rcReset( tor->download ); |
---|
349 | tr_rcReset( tor->upload ); |
---|
350 | tor->status = TR_STATUS_STOPPING; |
---|
351 | tor->stopDate = tr_date(); |
---|
352 | tr_lockUnlock( &tor->lock ); |
---|
353 | } |
---|
354 | |
---|
355 | /*********************************************************************** |
---|
356 | * torrentReallyStop |
---|
357 | *********************************************************************** |
---|
358 | * Joins the download thread and frees/closes everything related to it. |
---|
359 | **********************************************************************/ |
---|
360 | static void torrentReallyStop( tr_torrent_t * tor ) |
---|
361 | { |
---|
362 | tor->die = 1; |
---|
363 | tr_threadJoin( &tor->thread ); |
---|
364 | tr_dbg( "Thread joined" ); |
---|
365 | |
---|
366 | tr_trackerClose( tor->tracker ); |
---|
367 | tor->tracker = NULL; |
---|
368 | |
---|
369 | while( tor->peerCount > 0 ) |
---|
370 | { |
---|
371 | tr_peerRem( tor, 0 ); |
---|
372 | } |
---|
373 | } |
---|
374 | |
---|
375 | /*********************************************************************** |
---|
376 | * tr_torrentCount |
---|
377 | *********************************************************************** |
---|
378 | * |
---|
379 | **********************************************************************/ |
---|
380 | int tr_torrentCount( tr_handle_t * h ) |
---|
381 | { |
---|
382 | return h->torrentCount; |
---|
383 | } |
---|
384 | |
---|
385 | void tr_torrentIterate( tr_handle_t * h, tr_callback_t func, void * d ) |
---|
386 | { |
---|
387 | tr_torrent_t * tor; |
---|
388 | |
---|
389 | for( tor = h->torrentList; tor; tor = tor->next ) |
---|
390 | { |
---|
391 | func( tor, d ); |
---|
392 | } |
---|
393 | } |
---|
394 | |
---|
395 | int tr_getFinished( tr_torrent_t * tor ) |
---|
396 | { |
---|
397 | if( tor->finished ) |
---|
398 | { |
---|
399 | tor->finished = 0; |
---|
400 | return 1; |
---|
401 | } |
---|
402 | return 0; |
---|
403 | } |
---|
404 | |
---|
405 | tr_stat_t * tr_torrentStat( tr_torrent_t * tor ) |
---|
406 | { |
---|
407 | tr_stat_t * s; |
---|
408 | tr_info_t * inf = &tor->info; |
---|
409 | int i; |
---|
410 | |
---|
411 | tor->statCur = ( tor->statCur + 1 ) % 2; |
---|
412 | s = &tor->stats[tor->statCur]; |
---|
413 | |
---|
414 | if( ( tor->status & TR_STATUS_STOPPED ) || |
---|
415 | ( ( tor->status & TR_STATUS_STOPPING ) && |
---|
416 | tr_date() > tor->stopDate + 60000 ) ) |
---|
417 | { |
---|
418 | torrentReallyStop( tor ); |
---|
419 | tor->status = TR_STATUS_PAUSE; |
---|
420 | } |
---|
421 | |
---|
422 | tr_lockLock( &tor->lock ); |
---|
423 | |
---|
424 | s->status = tor->status; |
---|
425 | s->error = tor->error; |
---|
426 | memcpy( s->trackerError, tor->trackerError, |
---|
427 | sizeof( s->trackerError ) ); |
---|
428 | |
---|
429 | s->peersTotal = 0; |
---|
430 | s->peersUploading = 0; |
---|
431 | s->peersDownloading = 0; |
---|
432 | |
---|
433 | for( i = 0; i < tor->peerCount; i++ ) |
---|
434 | { |
---|
435 | if( tr_peerIsConnected( tor->peers[i] ) ) |
---|
436 | { |
---|
437 | (s->peersTotal)++; |
---|
438 | if( tr_peerIsUploading( tor->peers[i] ) ) |
---|
439 | { |
---|
440 | (s->peersUploading)++; |
---|
441 | } |
---|
442 | if( tr_peerIsDownloading( tor->peers[i] ) ) |
---|
443 | { |
---|
444 | (s->peersDownloading)++; |
---|
445 | } |
---|
446 | } |
---|
447 | } |
---|
448 | |
---|
449 | s->progress = tr_cpCompletionAsFloat( tor->completion ); |
---|
450 | if( tor->status & TR_STATUS_DOWNLOAD ) |
---|
451 | s->rateDownload = tr_rcRate( tor->download ); |
---|
452 | else |
---|
453 | /* tr_rcRate() doesn't make the difference between 'piece' |
---|
454 | messages and other messages, which causes a non-zero |
---|
455 | download rate even tough we are not downloading. So we |
---|
456 | force it to zero not to confuse the user. */ |
---|
457 | s->rateDownload = 0.0; |
---|
458 | s->rateUpload = tr_rcRate( tor->upload ); |
---|
459 | |
---|
460 | s->seeders = tr_trackerSeeders(tor->tracker); |
---|
461 | s->leechers = tr_trackerLeechers(tor->tracker); |
---|
462 | |
---|
463 | if( s->rateDownload < 0.1 ) |
---|
464 | { |
---|
465 | s->eta = -1; |
---|
466 | } |
---|
467 | else |
---|
468 | { |
---|
469 | s->eta = (float) ( 1.0 - s->progress ) * |
---|
470 | (float) inf->totalSize / s->rateDownload / 1024.0; |
---|
471 | } |
---|
472 | |
---|
473 | s->downloaded = tor->downloaded; |
---|
474 | s->uploaded = tor->uploaded; |
---|
475 | |
---|
476 | tr_lockUnlock( &tor->lock ); |
---|
477 | |
---|
478 | return s; |
---|
479 | } |
---|
480 | |
---|
481 | void tr_torrentAvailability( tr_torrent_t * tor, int8_t * tab, int size ) |
---|
482 | { |
---|
483 | int i, j, piece; |
---|
484 | |
---|
485 | tr_lockLock( &tor->lock ); |
---|
486 | for( i = 0; i < size; i++ ) |
---|
487 | { |
---|
488 | piece = i * tor->info.pieceCount / size; |
---|
489 | |
---|
490 | if( tr_cpPieceIsComplete( tor->completion, piece ) ) |
---|
491 | { |
---|
492 | tab[i] = -1; |
---|
493 | continue; |
---|
494 | } |
---|
495 | |
---|
496 | tab[i] = 0; |
---|
497 | for( j = 0; j < tor->peerCount; j++ ) |
---|
498 | { |
---|
499 | if( tr_peerBitfield( tor->peers[j] ) && |
---|
500 | tr_bitfieldHas( tr_peerBitfield( tor->peers[j] ), piece ) ) |
---|
501 | { |
---|
502 | (tab[i])++; |
---|
503 | } |
---|
504 | } |
---|
505 | } |
---|
506 | tr_lockUnlock( &tor->lock ); |
---|
507 | } |
---|
508 | |
---|
509 | void tr_torrentRemoveSaved( tr_torrent_t * tor ) { |
---|
510 | tr_metainfoRemoveSaved( tor->info.hashString ); |
---|
511 | } |
---|
512 | |
---|
513 | /*********************************************************************** |
---|
514 | * tr_torrentClose |
---|
515 | *********************************************************************** |
---|
516 | * Frees memory allocated by tr_torrentInit. |
---|
517 | **********************************************************************/ |
---|
518 | void tr_torrentClose( tr_handle_t * h, tr_torrent_t * tor ) |
---|
519 | { |
---|
520 | tr_info_t * inf = &tor->info; |
---|
521 | |
---|
522 | if( tor->status & ( TR_STATUS_STOPPING | TR_STATUS_STOPPED ) ) |
---|
523 | { |
---|
524 | /* Join the thread first */ |
---|
525 | torrentReallyStop( tor ); |
---|
526 | } |
---|
527 | |
---|
528 | tr_lockLock( &h->acceptLock ); |
---|
529 | |
---|
530 | h->torrentCount--; |
---|
531 | |
---|
532 | tr_lockClose( &tor->lock ); |
---|
533 | tr_cpClose( tor->completion ); |
---|
534 | |
---|
535 | tr_rcClose( tor->upload ); |
---|
536 | tr_rcClose( tor->download ); |
---|
537 | |
---|
538 | if( tor->destination ) |
---|
539 | { |
---|
540 | free( tor->destination ); |
---|
541 | } |
---|
542 | free( inf->pieces ); |
---|
543 | free( inf->files ); |
---|
544 | |
---|
545 | if( tor->prev ) |
---|
546 | { |
---|
547 | tor->prev->next = tor->next; |
---|
548 | } |
---|
549 | else |
---|
550 | { |
---|
551 | h->torrentList = tor->next; |
---|
552 | } |
---|
553 | if( tor->next ) |
---|
554 | { |
---|
555 | tor->next->prev = tor->prev; |
---|
556 | } |
---|
557 | free( tor ); |
---|
558 | |
---|
559 | tr_lockUnlock( &h->acceptLock ); |
---|
560 | } |
---|
561 | |
---|
562 | void tr_close( tr_handle_t * h ) |
---|
563 | { |
---|
564 | acceptStop( h ); |
---|
565 | tr_chokingClose( h->choking ); |
---|
566 | tr_fdClose( h->fdlimit ); |
---|
567 | tr_rcClose( h->upload ); |
---|
568 | tr_rcClose( h->download ); |
---|
569 | free( h ); |
---|
570 | |
---|
571 | tr_netResolveThreadClose(); |
---|
572 | } |
---|
573 | |
---|
574 | /*********************************************************************** |
---|
575 | * downloadLoop |
---|
576 | **********************************************************************/ |
---|
577 | static void downloadLoop( void * _tor ) |
---|
578 | { |
---|
579 | tr_torrent_t * tor = _tor; |
---|
580 | uint64_t date1, date2; |
---|
581 | |
---|
582 | tr_dbg( "Thread started" ); |
---|
583 | |
---|
584 | #ifdef SYS_BEOS |
---|
585 | /* This is required because on BeOS, SIGINT is sent to each thread, |
---|
586 | which kills them not nicely */ |
---|
587 | signal( SIGINT, SIG_IGN ); |
---|
588 | #endif |
---|
589 | |
---|
590 | tr_lockLock( &tor->lock ); |
---|
591 | |
---|
592 | tr_cpReset( tor->completion ); |
---|
593 | tor->io = tr_ioInit( tor ); |
---|
594 | tor->status = tr_cpIsSeeding( tor->completion ) ? |
---|
595 | TR_STATUS_SEED : TR_STATUS_DOWNLOAD; |
---|
596 | |
---|
597 | while( !tor->die ) |
---|
598 | { |
---|
599 | date1 = tr_date(); |
---|
600 | |
---|
601 | /* Are we finished ? */ |
---|
602 | if( ( tor->status & TR_STATUS_DOWNLOAD ) && |
---|
603 | tr_cpIsSeeding( tor->completion ) ) |
---|
604 | { |
---|
605 | /* Done */ |
---|
606 | tor->status = TR_STATUS_SEED; |
---|
607 | tor->finished = 1; |
---|
608 | tr_trackerCompleted( tor->tracker ); |
---|
609 | tr_ioSaveResume( tor->io ); |
---|
610 | sync(); /* KLUDGE: all files should be closed and |
---|
611 | re-opened in read-only mode instead */ |
---|
612 | } |
---|
613 | |
---|
614 | /* Receive/send messages */ |
---|
615 | tr_peerPulse( tor ); |
---|
616 | |
---|
617 | /* Try to get new peers or to send a message to the tracker */ |
---|
618 | tr_trackerPulse( tor->tracker ); |
---|
619 | |
---|
620 | if( tor->status & TR_STATUS_STOPPED ) |
---|
621 | { |
---|
622 | break; |
---|
623 | } |
---|
624 | |
---|
625 | /* Wait up to 20 ms */ |
---|
626 | date2 = tr_date(); |
---|
627 | if( date2 < date1 + 20 ) |
---|
628 | { |
---|
629 | tr_lockUnlock( &tor->lock ); |
---|
630 | tr_wait( date1 + 20 - date2 ); |
---|
631 | tr_lockLock( &tor->lock ); |
---|
632 | } |
---|
633 | } |
---|
634 | |
---|
635 | tr_lockUnlock( &tor->lock ); |
---|
636 | |
---|
637 | tr_ioClose( tor->io ); |
---|
638 | |
---|
639 | tor->status = TR_STATUS_STOPPED; |
---|
640 | |
---|
641 | tr_dbg( "Thread exited" ); |
---|
642 | } |
---|
643 | |
---|
644 | /*********************************************************************** |
---|
645 | * acceptLoop |
---|
646 | **********************************************************************/ |
---|
647 | static void acceptLoop( void * _h ) |
---|
648 | { |
---|
649 | tr_handle_t * h = _h; |
---|
650 | uint64_t date1, date2, lastchoke = 0; |
---|
651 | int ii; |
---|
652 | uint8_t * hash; |
---|
653 | tr_torrent_t * tor; |
---|
654 | |
---|
655 | tr_dbg( "Accept thread started" ); |
---|
656 | |
---|
657 | #ifdef SYS_BEOS |
---|
658 | /* This is required because on BeOS, SIGINT is sent to each thread, |
---|
659 | which kills them not nicely */ |
---|
660 | signal( SIGINT, SIG_IGN ); |
---|
661 | #endif |
---|
662 | |
---|
663 | tr_lockLock( &h->acceptLock ); |
---|
664 | |
---|
665 | while( !h->acceptDie ) |
---|
666 | { |
---|
667 | date1 = tr_date(); |
---|
668 | |
---|
669 | /* Check for incoming connections */ |
---|
670 | if( h->bindSocket > -1 && |
---|
671 | h->acceptPeerCount < TR_MAX_PEER_COUNT && |
---|
672 | !tr_fdSocketWillCreate( h->fdlimit, 0 ) ) |
---|
673 | { |
---|
674 | int s; |
---|
675 | struct in_addr addr; |
---|
676 | in_port_t port; |
---|
677 | s = tr_netAccept( h->bindSocket, &addr, &port ); |
---|
678 | if( s > -1 ) |
---|
679 | { |
---|
680 | h->acceptPeers[h->acceptPeerCount++] = tr_peerInit( addr, port, s ); |
---|
681 | } |
---|
682 | else |
---|
683 | { |
---|
684 | tr_fdSocketClosed( h->fdlimit, 0 ); |
---|
685 | } |
---|
686 | } |
---|
687 | |
---|
688 | for( ii = 0; ii < h->acceptPeerCount; ) |
---|
689 | { |
---|
690 | if( tr_peerRead( NULL, h->acceptPeers[ii] ) ) |
---|
691 | { |
---|
692 | tr_peerDestroy( h->fdlimit, h->acceptPeers[ii] ); |
---|
693 | goto removePeer; |
---|
694 | } |
---|
695 | if( NULL != ( hash = tr_peerHash( h->acceptPeers[ii] ) ) ) |
---|
696 | { |
---|
697 | for( tor = h->torrentList; tor; tor = tor->next ) |
---|
698 | { |
---|
699 | tr_lockLock( &tor->lock ); |
---|
700 | if( 0 == memcmp( tor->info.hash, hash, |
---|
701 | SHA_DIGEST_LENGTH ) ) |
---|
702 | { |
---|
703 | tr_peerAttach( tor, h->acceptPeers[ii] ); |
---|
704 | tr_lockUnlock( &tor->lock ); |
---|
705 | goto removePeer; |
---|
706 | } |
---|
707 | tr_lockUnlock( &tor->lock ); |
---|
708 | } |
---|
709 | tr_peerDestroy( h->fdlimit, h->acceptPeers[ii] ); |
---|
710 | goto removePeer; |
---|
711 | } |
---|
712 | if( date1 > tr_peerDate( h->acceptPeers[ii] ) + 10000 ) |
---|
713 | { |
---|
714 | /* Give them 10 seconds to send the handshake */ |
---|
715 | tr_peerDestroy( h->fdlimit, h->acceptPeers[ii] ); |
---|
716 | goto removePeer; |
---|
717 | } |
---|
718 | ii++; |
---|
719 | continue; |
---|
720 | removePeer: |
---|
721 | h->acceptPeerCount--; |
---|
722 | memmove( &h->acceptPeers[ii], &h->acceptPeers[ii+1], |
---|
723 | ( h->acceptPeerCount - ii ) * sizeof( tr_peer_t * ) ); |
---|
724 | } |
---|
725 | |
---|
726 | if( date1 > lastchoke + 2000 ) |
---|
727 | { |
---|
728 | tr_chokingPulse( h->choking ); |
---|
729 | lastchoke = date1; |
---|
730 | } |
---|
731 | |
---|
732 | /* Wait up to 20 ms */ |
---|
733 | date2 = tr_date(); |
---|
734 | if( date2 < date1 + 20 ) |
---|
735 | { |
---|
736 | tr_lockUnlock( &h->acceptLock ); |
---|
737 | tr_wait( date1 + 20 - date2 ); |
---|
738 | tr_lockLock( &h->acceptLock ); |
---|
739 | } |
---|
740 | } |
---|
741 | |
---|
742 | tr_lockUnlock( &h->acceptLock ); |
---|
743 | |
---|
744 | tr_dbg( "Accept thread exited" ); |
---|
745 | } |
---|
746 | |
---|
747 | /*********************************************************************** |
---|
748 | * acceptStop |
---|
749 | *********************************************************************** |
---|
750 | * Joins the accept thread and frees/closes everything related to it. |
---|
751 | **********************************************************************/ |
---|
752 | static void acceptStop( tr_handle_t * h ) |
---|
753 | { |
---|
754 | int ii; |
---|
755 | |
---|
756 | h->acceptDie = 1; |
---|
757 | tr_threadJoin( &h->acceptThread ); |
---|
758 | tr_lockClose( &h->acceptLock ); |
---|
759 | tr_dbg( "Accept thread joined" ); |
---|
760 | |
---|
761 | for( ii = 0; ii < h->acceptPeerCount; ii++ ) |
---|
762 | { |
---|
763 | tr_peerDestroy( h->fdlimit, h->acceptPeers[ii] ); |
---|
764 | } |
---|
765 | |
---|
766 | if( h->bindSocket > -1 ) |
---|
767 | { |
---|
768 | tr_netClose( h->bindSocket ); |
---|
769 | tr_fdSocketClosed( h->fdlimit, 0 ); |
---|
770 | } |
---|
771 | } |
---|