1 | /****************************************************************************** |
---|
2 | * $Id: torrent.c 2355 2007-07-15 15:45:08Z livings124 $ |
---|
3 | * |
---|
4 | * Copyright (c) 2005-2007 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 <sys/types.h> |
---|
26 | #include <sys/socket.h> |
---|
27 | #include <netinet/in.h> |
---|
28 | #include <arpa/inet.h> |
---|
29 | |
---|
30 | #include "transmission.h" |
---|
31 | #include "fastresume.h" |
---|
32 | #include "trcompat.h" /* for strlcpy */ |
---|
33 | #include "metainfo.h" |
---|
34 | #include "net.h" /* tr_netNtop */ |
---|
35 | #include "shared.h" |
---|
36 | |
---|
37 | /*** |
---|
38 | **** LOCKS |
---|
39 | ***/ |
---|
40 | |
---|
41 | void |
---|
42 | tr_torrentReaderLock( const tr_torrent_t * tor ) |
---|
43 | { |
---|
44 | tr_rwReaderLock ( (tr_rwlock_t*)&tor->lock ); |
---|
45 | } |
---|
46 | |
---|
47 | void |
---|
48 | tr_torrentReaderUnlock( const tr_torrent_t * tor ) |
---|
49 | { |
---|
50 | tr_rwReaderUnlock ( (tr_rwlock_t*)&tor->lock ); |
---|
51 | } |
---|
52 | |
---|
53 | void |
---|
54 | tr_torrentWriterLock( tr_torrent_t * tor ) |
---|
55 | { |
---|
56 | tr_rwWriterLock ( &tor->lock ); |
---|
57 | } |
---|
58 | |
---|
59 | void |
---|
60 | tr_torrentWriterUnlock( tr_torrent_t * tor ) |
---|
61 | { |
---|
62 | tr_rwWriterUnlock ( &tor->lock ); |
---|
63 | } |
---|
64 | |
---|
65 | /*** |
---|
66 | **** PER-TORRENT UL / DL SPEEDS |
---|
67 | ***/ |
---|
68 | |
---|
69 | void |
---|
70 | tr_setUseCustomUpload( tr_torrent_t * tor, int limit ) |
---|
71 | { |
---|
72 | tr_torrentWriterLock( tor ); |
---|
73 | tor->customUploadLimit = limit; |
---|
74 | tr_torrentWriterUnlock( tor ); |
---|
75 | } |
---|
76 | |
---|
77 | void |
---|
78 | tr_setUseCustomDownload( tr_torrent_t * tor, int limit ) |
---|
79 | { |
---|
80 | tr_torrentWriterLock( tor ); |
---|
81 | tor->customDownloadLimit = limit; |
---|
82 | tr_torrentWriterUnlock( tor ); |
---|
83 | } |
---|
84 | |
---|
85 | void |
---|
86 | tr_setUploadLimit( tr_torrent_t * tor, int limit ) |
---|
87 | { |
---|
88 | tr_torrentWriterLock( tor ); |
---|
89 | tr_rcSetLimit( tor->upload, limit ); |
---|
90 | tr_torrentWriterUnlock( tor ); |
---|
91 | } |
---|
92 | |
---|
93 | void |
---|
94 | tr_setDownloadLimit( tr_torrent_t * tor, int limit ) |
---|
95 | { |
---|
96 | tr_torrentWriterLock( tor ); |
---|
97 | tr_rcSetLimit( tor->download, limit ); |
---|
98 | tr_torrentWriterUnlock( tor ); |
---|
99 | } |
---|
100 | |
---|
101 | /*** |
---|
102 | **** |
---|
103 | **** TORRENT INSTANTIATION |
---|
104 | **** |
---|
105 | ***/ |
---|
106 | |
---|
107 | static int |
---|
108 | getBytePiece( const tr_info_t * info, uint64_t byteOffset ) |
---|
109 | { |
---|
110 | assert( info != NULL ); |
---|
111 | assert( info->pieceSize != 0 ); |
---|
112 | |
---|
113 | return byteOffset / info->pieceSize; |
---|
114 | } |
---|
115 | |
---|
116 | static void |
---|
117 | initFilePieces ( tr_info_t * info, int fileIndex ) |
---|
118 | { |
---|
119 | tr_file_t * file = &info->files[fileIndex]; |
---|
120 | uint64_t firstByte, lastByte; |
---|
121 | |
---|
122 | assert( info != NULL ); |
---|
123 | assert( 0<=fileIndex && fileIndex<info->fileCount ); |
---|
124 | |
---|
125 | file = &info->files[fileIndex]; |
---|
126 | firstByte = file->offset; |
---|
127 | lastByte = firstByte + (file->length ? file->length-1 : 0); |
---|
128 | file->firstPiece = getBytePiece( info, firstByte ); |
---|
129 | file->lastPiece = getBytePiece( info, lastByte ); |
---|
130 | tr_dbg( "file #%d is in pieces [%d...%d] (%s)", fileIndex, file->firstPiece, file->lastPiece, file->name ); |
---|
131 | } |
---|
132 | |
---|
133 | static tr_priority_t |
---|
134 | calculatePiecePriority ( const tr_torrent_t * tor, |
---|
135 | int piece ) |
---|
136 | { |
---|
137 | int i; |
---|
138 | tr_priority_t priority = TR_PRI_NORMAL; |
---|
139 | |
---|
140 | for( i=0; i<tor->info.fileCount; ++i ) |
---|
141 | { |
---|
142 | const tr_file_t * file = &tor->info.files[i]; |
---|
143 | if ( file->firstPiece <= piece |
---|
144 | && file->lastPiece >= piece |
---|
145 | && file->priority > priority) |
---|
146 | priority = file->priority; |
---|
147 | } |
---|
148 | |
---|
149 | return priority; |
---|
150 | } |
---|
151 | |
---|
152 | static void |
---|
153 | tr_torrentInitFilePieces( tr_torrent_t * tor ) |
---|
154 | { |
---|
155 | int i; |
---|
156 | uint64_t offset = 0; |
---|
157 | |
---|
158 | assert( tor != NULL ); |
---|
159 | |
---|
160 | for( i=0; i<tor->info.fileCount; ++i ) { |
---|
161 | tor->info.files[i].offset = offset; |
---|
162 | offset += tor->info.files[i].length; |
---|
163 | initFilePieces( &tor->info, i ); |
---|
164 | } |
---|
165 | |
---|
166 | for( i=0; i<tor->info.pieceCount; ++i ) |
---|
167 | tor->info.pieces[i].priority = calculatePiecePriority( tor, i ); |
---|
168 | } |
---|
169 | |
---|
170 | static void torrentThreadLoop( void * ); |
---|
171 | |
---|
172 | static void |
---|
173 | torrentRealInit( tr_handle_t * h, |
---|
174 | tr_torrent_t * tor, |
---|
175 | const char * destination, |
---|
176 | int flags ) |
---|
177 | { |
---|
178 | int i; |
---|
179 | char name[512]; |
---|
180 | |
---|
181 | tor->info.flags |= flags; |
---|
182 | |
---|
183 | tr_sharedLock( h->shared ); |
---|
184 | |
---|
185 | tor->destination = tr_strdup( destination ); |
---|
186 | |
---|
187 | tr_torrentInitFilePieces( tor ); |
---|
188 | |
---|
189 | tor->handle = h; |
---|
190 | tor->id = h->id; |
---|
191 | tor->key = h->key; |
---|
192 | tor->azId = h->azId; |
---|
193 | tor->hasChangedState = -1; |
---|
194 | |
---|
195 | /* Escaped info hash for HTTP queries */ |
---|
196 | for( i = 0; i < SHA_DIGEST_LENGTH; i++ ) |
---|
197 | { |
---|
198 | snprintf( &tor->escapedHashString[3*i], |
---|
199 | sizeof( tor->escapedHashString ) - 3 * i, |
---|
200 | "%%%02x", tor->info.hash[i] ); |
---|
201 | } |
---|
202 | |
---|
203 | tor->pexDisabled = 0; |
---|
204 | |
---|
205 | /* Block size: usually 16 ko, or less if we have to */ |
---|
206 | tor->blockSize = MIN( tor->info.pieceSize, 1 << 14 ); |
---|
207 | tor->blockCount = ( tor->info.totalSize + tor->blockSize - 1 ) / |
---|
208 | tor->blockSize; |
---|
209 | tor->completion = tr_cpInit( tor ); |
---|
210 | |
---|
211 | tor->thread = THREAD_EMPTY; |
---|
212 | tr_rwInit( &tor->lock ); |
---|
213 | |
---|
214 | tor->upload = tr_rcInit(); |
---|
215 | tor->download = tr_rcInit(); |
---|
216 | tor->swarmspeed = tr_rcInit(); |
---|
217 | |
---|
218 | /* We have a new torrent */ |
---|
219 | tor->publicPort = tr_sharedGetPublicPort( h->shared ); |
---|
220 | |
---|
221 | tr_sharedUnlock( h->shared ); |
---|
222 | |
---|
223 | if( !h->isPortSet ) |
---|
224 | tr_setBindPort( h, TR_DEFAULT_PORT ); |
---|
225 | |
---|
226 | assert( !tor->downloadedCur ); |
---|
227 | assert( !tor->uploadedCur ); |
---|
228 | |
---|
229 | tor->error = TR_OK; |
---|
230 | tor->runStatus = flags & TR_FLAG_PAUSED ? TR_RUN_STOPPED : TR_RUN_RUNNING; |
---|
231 | tor->recheckFlag = tr_ioCheckFiles( tor, TR_RECHECK_FAST ); |
---|
232 | tor->cpStatus = tr_cpGetStatus( tor->completion ); |
---|
233 | |
---|
234 | tr_sharedLock( h->shared ); |
---|
235 | tor->next = h->torrentList; |
---|
236 | h->torrentList = tor; |
---|
237 | h->torrentCount++; |
---|
238 | tr_sharedUnlock( h->shared ); |
---|
239 | |
---|
240 | snprintf( name, sizeof( name ), "torrent %p (%s)", tor, tor->info.name ); |
---|
241 | tr_threadCreate( &tor->thread, torrentThreadLoop, tor, name ); |
---|
242 | } |
---|
243 | |
---|
244 | static int |
---|
245 | pathIsInUse ( const tr_handle_t * h, |
---|
246 | const char * destination, |
---|
247 | const char * name ) |
---|
248 | { |
---|
249 | const tr_torrent_t * tor; |
---|
250 | |
---|
251 | for( tor=h->torrentList; tor; tor=tor->next ) |
---|
252 | if( !strcmp( destination, tor->destination ) |
---|
253 | && !strcmp( name, tor->info.name ) ) |
---|
254 | return TRUE; |
---|
255 | |
---|
256 | return FALSE; |
---|
257 | } |
---|
258 | |
---|
259 | static int |
---|
260 | hashExists( const tr_handle_t * h, |
---|
261 | const uint8_t * hash ) |
---|
262 | { |
---|
263 | const tr_torrent_t * tor; |
---|
264 | |
---|
265 | for( tor=h->torrentList; tor; tor=tor->next ) |
---|
266 | if( !memcmp( hash, tor->info.hash, SHA_DIGEST_LENGTH ) ) |
---|
267 | return TRUE; |
---|
268 | |
---|
269 | return FALSE; |
---|
270 | } |
---|
271 | |
---|
272 | static int |
---|
273 | infoCanAdd( const tr_handle_t * h, |
---|
274 | const char * destination, |
---|
275 | const tr_info_t * info ) |
---|
276 | { |
---|
277 | if( hashExists( h, info->hash ) ) |
---|
278 | return TR_EDUPLICATE; |
---|
279 | |
---|
280 | if( destination && pathIsInUse( h, destination, info->name ) ) |
---|
281 | return TR_EDUPLICATE; |
---|
282 | |
---|
283 | return TR_OK; |
---|
284 | } |
---|
285 | |
---|
286 | int |
---|
287 | tr_torrentParse( const tr_handle_t * h, |
---|
288 | const char * path, |
---|
289 | const char * destination, |
---|
290 | tr_info_t * setme_info ) |
---|
291 | { |
---|
292 | int ret; |
---|
293 | tr_info_t tmp; |
---|
294 | |
---|
295 | if( setme_info == NULL ) |
---|
296 | setme_info = &tmp; |
---|
297 | |
---|
298 | memset( setme_info, 0, sizeof( tr_info_t ) ); |
---|
299 | ret = tr_metainfoParseFile( setme_info, h->tag, path, FALSE ); |
---|
300 | |
---|
301 | if( ret == TR_OK ) |
---|
302 | ret = infoCanAdd( h, destination, setme_info ); |
---|
303 | |
---|
304 | if( setme_info == &tmp ) |
---|
305 | tr_metainfoFree( &tmp ); |
---|
306 | |
---|
307 | return ret; |
---|
308 | } |
---|
309 | |
---|
310 | tr_torrent_t * |
---|
311 | tr_torrentInit( tr_handle_t * h, |
---|
312 | const char * path, |
---|
313 | const char * destination, |
---|
314 | int flags, |
---|
315 | int * error ) |
---|
316 | { |
---|
317 | int val; |
---|
318 | tr_torrent_t * tor = NULL; |
---|
319 | |
---|
320 | if(( val = tr_torrentParse( h, path, destination, NULL ))) |
---|
321 | *error = val; |
---|
322 | else if(!(( tor = tr_new0( tr_torrent_t, 1 )))) |
---|
323 | *error = TR_EOTHER; |
---|
324 | else { |
---|
325 | tr_metainfoParseFile( &tor->info, h->tag, path, TR_FLAG_SAVE & flags ); |
---|
326 | torrentRealInit( h, tor, destination, flags ); |
---|
327 | } |
---|
328 | |
---|
329 | return tor; |
---|
330 | } |
---|
331 | |
---|
332 | int |
---|
333 | tr_torrentParseHash( const tr_handle_t * h, |
---|
334 | const char * hashStr, |
---|
335 | const char * destination, |
---|
336 | tr_info_t * setme_info ) |
---|
337 | { |
---|
338 | int ret; |
---|
339 | tr_info_t tmp; |
---|
340 | |
---|
341 | if( setme_info == NULL ) |
---|
342 | setme_info = &tmp; |
---|
343 | |
---|
344 | memset( setme_info, 0, sizeof( tr_info_t ) ); |
---|
345 | ret = tr_metainfoParseHash( setme_info, h->tag, hashStr ); |
---|
346 | |
---|
347 | if( ret == TR_OK ) |
---|
348 | ret = infoCanAdd( h, destination, setme_info ); |
---|
349 | |
---|
350 | if( setme_info == &tmp ) |
---|
351 | tr_metainfoFree( &tmp ); |
---|
352 | |
---|
353 | return ret; |
---|
354 | } |
---|
355 | |
---|
356 | |
---|
357 | tr_torrent_t * |
---|
358 | tr_torrentInitSaved( tr_handle_t * h, |
---|
359 | const char * hashStr, |
---|
360 | const char * destination, |
---|
361 | int flags, |
---|
362 | int * error ) |
---|
363 | { |
---|
364 | int val; |
---|
365 | tr_torrent_t * tor = NULL; |
---|
366 | |
---|
367 | if(( val = tr_torrentParseHash( h, hashStr, destination, NULL ))) |
---|
368 | *error = val; |
---|
369 | else if(!(( tor = tr_new0( tr_torrent_t, 1 )))) |
---|
370 | *error = TR_EOTHER; |
---|
371 | else { |
---|
372 | tr_metainfoParseHash( &tor->info, h->tag, hashStr ); |
---|
373 | torrentRealInit( h, tor, destination, (TR_FLAG_SAVE|flags) ); |
---|
374 | } |
---|
375 | |
---|
376 | return tor; |
---|
377 | } |
---|
378 | |
---|
379 | static int |
---|
380 | tr_torrentParseData( const tr_handle_t * h, |
---|
381 | const uint8_t * data, |
---|
382 | size_t size, |
---|
383 | const char * destination, |
---|
384 | tr_info_t * setme_info ) |
---|
385 | { |
---|
386 | int ret; |
---|
387 | tr_info_t tmp; |
---|
388 | |
---|
389 | if( setme_info == NULL ) |
---|
390 | setme_info = &tmp; |
---|
391 | |
---|
392 | memset( setme_info, 0, sizeof( tr_info_t ) ); |
---|
393 | ret = tr_metainfoParseData( setme_info, h->tag, data, size, FALSE ); |
---|
394 | |
---|
395 | if( ret == TR_OK ) |
---|
396 | ret = infoCanAdd( h, destination, setme_info ); |
---|
397 | |
---|
398 | if( setme_info == &tmp ) |
---|
399 | tr_metainfoFree( &tmp ); |
---|
400 | |
---|
401 | return ret; |
---|
402 | } |
---|
403 | |
---|
404 | tr_torrent_t * |
---|
405 | tr_torrentInitData( tr_handle_t * h, |
---|
406 | const uint8_t * data, |
---|
407 | size_t size, |
---|
408 | const char * destination, |
---|
409 | int flags, |
---|
410 | int * error ) |
---|
411 | { |
---|
412 | int val; |
---|
413 | tr_torrent_t * tor = NULL; |
---|
414 | |
---|
415 | if(( val = tr_torrentParseData( h, data, size, destination, NULL ))) |
---|
416 | *error = val; |
---|
417 | else if(!(( tor = tr_new0( tr_torrent_t, 1 )))) |
---|
418 | *error = TR_EOTHER; |
---|
419 | else { |
---|
420 | tr_metainfoParseData( &tor->info, h->tag, data, size, TR_FLAG_SAVE & flags ); |
---|
421 | torrentRealInit( h, tor, destination, flags ); |
---|
422 | } |
---|
423 | |
---|
424 | return tor; |
---|
425 | } |
---|
426 | |
---|
427 | const tr_info_t * |
---|
428 | tr_torrentInfo( const tr_torrent_t * tor ) |
---|
429 | { |
---|
430 | return &tor->info; |
---|
431 | } |
---|
432 | |
---|
433 | /*** |
---|
434 | **** |
---|
435 | ***/ |
---|
436 | |
---|
437 | int tr_torrentScrape( tr_torrent_t * tor, int * s, int * l, int * d ) |
---|
438 | { |
---|
439 | return tr_trackerScrape( tor, s, l, d ); |
---|
440 | } |
---|
441 | |
---|
442 | void tr_torrentSetFolder( tr_torrent_t * tor, const char * path ) |
---|
443 | { |
---|
444 | tr_free( tor->destination ); |
---|
445 | tor->destination = tr_strdup( path ); |
---|
446 | |
---|
447 | if( !tor->ioLoaded ) |
---|
448 | tor->ioLoaded = tr_ioLoadResume( tor ) == TR_OK; |
---|
449 | } |
---|
450 | |
---|
451 | const char* tr_torrentGetFolder( const tr_torrent_t * tor ) |
---|
452 | { |
---|
453 | return tor->destination; |
---|
454 | } |
---|
455 | |
---|
456 | |
---|
457 | /*********************************************************************** |
---|
458 | * torrentReallyStop |
---|
459 | *********************************************************************** |
---|
460 | * Joins the download thread and frees/closes everything related to it. |
---|
461 | **********************************************************************/ |
---|
462 | |
---|
463 | void tr_torrentDisablePex( tr_torrent_t * tor, int disable ) |
---|
464 | { |
---|
465 | tr_torrentWriterLock( tor ); |
---|
466 | |
---|
467 | if( ! ( TR_FLAG_PRIVATE & tor->info.flags ) ) |
---|
468 | { |
---|
469 | if( tor->pexDisabled != disable ) |
---|
470 | { |
---|
471 | int i; |
---|
472 | tor->pexDisabled = disable; |
---|
473 | for( i=0; i<tor->peerCount; ++i ) |
---|
474 | tr_peerSetPrivate( tor->peers[i], disable ); |
---|
475 | } |
---|
476 | } |
---|
477 | |
---|
478 | tr_torrentWriterUnlock( tor ); |
---|
479 | } |
---|
480 | |
---|
481 | static int tr_didStateChangeTo ( tr_torrent_t * tor, int status ) |
---|
482 | { |
---|
483 | int ret; |
---|
484 | |
---|
485 | tr_torrentWriterLock( tor ); |
---|
486 | if (( ret = tor->hasChangedState == status )) |
---|
487 | tor->hasChangedState = -1; |
---|
488 | tr_torrentWriterUnlock( tor ); |
---|
489 | |
---|
490 | return ret; |
---|
491 | } |
---|
492 | |
---|
493 | int tr_getIncomplete( tr_torrent_t * tor ) |
---|
494 | { |
---|
495 | return tr_didStateChangeTo( tor, TR_CP_INCOMPLETE ); |
---|
496 | } |
---|
497 | int tr_getDone( tr_torrent_t * tor ) |
---|
498 | { |
---|
499 | return tr_didStateChangeTo( tor, TR_CP_DONE ); |
---|
500 | } |
---|
501 | int tr_getComplete( tr_torrent_t * tor ) |
---|
502 | { |
---|
503 | return tr_didStateChangeTo( tor, TR_CP_COMPLETE ); |
---|
504 | } |
---|
505 | |
---|
506 | void tr_manualUpdate( tr_torrent_t * tor UNUSED ) |
---|
507 | { |
---|
508 | #if 0 |
---|
509 | int peerCount, new; |
---|
510 | uint8_t * peerCompact; |
---|
511 | |
---|
512 | if( tor->status != TR_RUN_RUNNING ) |
---|
513 | return; |
---|
514 | |
---|
515 | tr_torrentWriterLock( tor ); |
---|
516 | tr_trackerAnnouncePulse( tor->tracker, &peerCount, &peerCompact, 1 ); |
---|
517 | new = 0; |
---|
518 | if( peerCount > 0 ) |
---|
519 | { |
---|
520 | new = tr_torrentAddCompact( tor, TR_PEER_FROM_TRACKER, |
---|
521 | peerCompact, peerCount ); |
---|
522 | free( peerCompact ); |
---|
523 | } |
---|
524 | tr_dbg( "got %i peers from manual announce, used %i", peerCount, new ); |
---|
525 | tr_torrentWriterUnlock( tor ); |
---|
526 | #endif |
---|
527 | } |
---|
528 | |
---|
529 | tr_stat_t * tr_torrentStat( tr_torrent_t * tor ) |
---|
530 | { |
---|
531 | tr_stat_t * s; |
---|
532 | tr_tracker_t * tc; |
---|
533 | int i; |
---|
534 | |
---|
535 | tr_torrentReaderLock( tor ); |
---|
536 | |
---|
537 | tor->statCur = ( tor->statCur + 1 ) % 2; |
---|
538 | s = &tor->stats[tor->statCur]; |
---|
539 | |
---|
540 | s->error = tor->error; |
---|
541 | memcpy( s->errorString, tor->errorString, |
---|
542 | sizeof( s->errorString ) ); |
---|
543 | |
---|
544 | tc = tor->tracker; |
---|
545 | s->cannotConnect = tr_trackerCannotConnect( tc ); |
---|
546 | s->tracker = tc |
---|
547 | ? tr_trackerGet( tc ) |
---|
548 | : &tor->info.trackerList[0].list[0]; |
---|
549 | |
---|
550 | /* peers... */ |
---|
551 | memset( s->peersFrom, 0, sizeof( s->peersFrom ) ); |
---|
552 | s->peersTotal = 0; |
---|
553 | s->peersUploading = 0; |
---|
554 | s->peersDownloading = 0; |
---|
555 | for( i=0; i<tor->peerCount; ++i ) |
---|
556 | { |
---|
557 | const tr_peer_t * peer = tor->peers[i]; |
---|
558 | ++s->peersTotal; |
---|
559 | if( tr_peerIsConnected( peer ) ) |
---|
560 | { |
---|
561 | ++s->peersFrom[tr_peerIsFrom(peer)]; |
---|
562 | |
---|
563 | /*if( tr_peerIsInterested( peer ) && !tr_peerIsChokedByUs( peer ) )*/ |
---|
564 | if( tr_peerDownloadRate( peer ) > 0.01 ) |
---|
565 | ++s->peersUploading; |
---|
566 | |
---|
567 | /*if( tr_peerIsInteresting( peer ) && !tr_peerIsChokingUs( peer ) )*/ |
---|
568 | if( tr_peerUploadRate( peer ) > 0.01 ) |
---|
569 | ++s->peersDownloading; |
---|
570 | } |
---|
571 | } |
---|
572 | |
---|
573 | s->percentDone = tr_cpPercentDone( tor->completion ); |
---|
574 | s->percentComplete = tr_cpPercentComplete( tor->completion ); |
---|
575 | s->left = tr_cpLeftUntilDone( tor->completion ); |
---|
576 | |
---|
577 | |
---|
578 | if( tor->recheckFlag ) |
---|
579 | s->status = TR_STATUS_CHECK_WAIT; |
---|
580 | else switch( tor->runStatus ) { |
---|
581 | case TR_RUN_STOPPING: /* fallthrough */ |
---|
582 | case TR_RUN_STOPPING_NET_WAIT: s->status = TR_STATUS_STOPPING; break; |
---|
583 | case TR_RUN_STOPPED: s->status = TR_STATUS_STOPPED; break; |
---|
584 | case TR_RUN_CHECKING: s->status = TR_STATUS_CHECK; break; |
---|
585 | case TR_RUN_RUNNING: switch( tor->cpStatus ) { |
---|
586 | case TR_CP_INCOMPLETE: s->status = TR_STATUS_DOWNLOAD; break; |
---|
587 | case TR_CP_DONE: s->status = TR_STATUS_DONE; break; |
---|
588 | case TR_CP_COMPLETE: s->status = TR_STATUS_SEED; break; |
---|
589 | } |
---|
590 | } |
---|
591 | |
---|
592 | s->cpStatus = tor->cpStatus; |
---|
593 | |
---|
594 | /* tr_rcRate() doesn't make the difference between 'piece' |
---|
595 | messages and other messages, which causes a non-zero |
---|
596 | download rate even tough we are not downloading. So we |
---|
597 | force it to zero not to confuse the user. */ |
---|
598 | s->rateDownload = tor->runStatus==TR_RUN_RUNNING |
---|
599 | ? tr_rcRate( tor->download ) |
---|
600 | : 0.0; |
---|
601 | s->rateUpload = tr_rcRate( tor->upload ); |
---|
602 | |
---|
603 | s->seeders = tr_trackerSeeders( tc ); |
---|
604 | s->leechers = tr_trackerLeechers( tc ); |
---|
605 | s->completedFromTracker = tr_trackerDownloaded( tc ); |
---|
606 | |
---|
607 | s->swarmspeed = tr_rcRate( tor->swarmspeed ); |
---|
608 | |
---|
609 | s->startDate = tor->startDate; |
---|
610 | s->activityDate = tor->activityDate; |
---|
611 | |
---|
612 | s->eta = s->rateDownload < 0.1 |
---|
613 | ? -1.0f |
---|
614 | : (s->left / s->rateDownload / 1024.0); |
---|
615 | |
---|
616 | s->uploaded = tor->uploadedCur + tor->uploadedPrev; |
---|
617 | s->downloaded = tor->downloadedCur + tor->downloadedPrev; |
---|
618 | s->downloadedValid = tr_cpDownloadedValid( tor->completion ); |
---|
619 | |
---|
620 | s->ratio = s->downloaded || s->downloadedValid |
---|
621 | ? (float)s->uploaded / (float)MAX(s->downloaded, s->downloadedValid) |
---|
622 | : TR_RATIO_NA; |
---|
623 | |
---|
624 | tr_torrentReaderUnlock( tor ); |
---|
625 | |
---|
626 | return s; |
---|
627 | } |
---|
628 | |
---|
629 | tr_peer_stat_t * |
---|
630 | tr_torrentPeers( const tr_torrent_t * tor, int * peerCount ) |
---|
631 | { |
---|
632 | tr_peer_stat_t * peers; |
---|
633 | |
---|
634 | tr_torrentReaderLock( tor ); |
---|
635 | |
---|
636 | *peerCount = tor->peerCount; |
---|
637 | |
---|
638 | peers = tr_new0( tr_peer_stat_t, tor->peerCount ); |
---|
639 | if (peers != NULL) |
---|
640 | { |
---|
641 | tr_peer_t * peer; |
---|
642 | struct in_addr * addr; |
---|
643 | int i; |
---|
644 | for( i=0; i<tor->peerCount; ++i ) |
---|
645 | { |
---|
646 | peer = tor->peers[i]; |
---|
647 | |
---|
648 | addr = tr_peerAddress( peer ); |
---|
649 | if( NULL != addr ) |
---|
650 | { |
---|
651 | tr_netNtop( addr, peers[i].addr, |
---|
652 | sizeof( peers[i].addr ) ); |
---|
653 | } |
---|
654 | |
---|
655 | peers[i].client = tr_peerClient( peer ); |
---|
656 | peers[i].isConnected = tr_peerIsConnected( peer ); |
---|
657 | peers[i].from = tr_peerIsFrom( peer ); |
---|
658 | peers[i].progress = tr_peerProgress( peer ); |
---|
659 | peers[i].port = tr_peerPort( peer ); |
---|
660 | |
---|
661 | peers[i].uploadToRate = tr_peerUploadRate( peer ); |
---|
662 | peers[i].downloadFromRate = tr_peerDownloadRate( peer ); |
---|
663 | |
---|
664 | peers[i].isDownloading = peers[i].uploadToRate > 0.01; |
---|
665 | peers[i].isUploading = peers[i].downloadFromRate > 0.01; |
---|
666 | } |
---|
667 | } |
---|
668 | |
---|
669 | tr_torrentReaderUnlock( tor ); |
---|
670 | |
---|
671 | return peers; |
---|
672 | } |
---|
673 | |
---|
674 | void tr_torrentPeersFree( tr_peer_stat_t * peers, int peerCount UNUSED ) |
---|
675 | { |
---|
676 | tr_free( peers ); |
---|
677 | } |
---|
678 | |
---|
679 | void tr_torrentAvailability( const tr_torrent_t * tor, int8_t * tab, int size ) |
---|
680 | { |
---|
681 | int i, j, piece; |
---|
682 | float interval; |
---|
683 | |
---|
684 | tr_torrentReaderLock( tor ); |
---|
685 | |
---|
686 | interval = (float)tor->info.pieceCount / (float)size; |
---|
687 | for( i = 0; i < size; i++ ) |
---|
688 | { |
---|
689 | piece = i * interval; |
---|
690 | |
---|
691 | if( tr_cpPieceIsComplete( tor->completion, piece ) ) |
---|
692 | { |
---|
693 | tab[i] = -1; |
---|
694 | continue; |
---|
695 | } |
---|
696 | |
---|
697 | tab[i] = 0; |
---|
698 | for( j = 0; j < tor->peerCount; j++ ) |
---|
699 | { |
---|
700 | if( tr_peerHasPiece( tor->peers[j], piece ) ) |
---|
701 | { |
---|
702 | (tab[i])++; |
---|
703 | } |
---|
704 | } |
---|
705 | } |
---|
706 | |
---|
707 | tr_torrentReaderUnlock( tor ); |
---|
708 | } |
---|
709 | |
---|
710 | uint64_t |
---|
711 | tr_torrentFileBytesCompleted ( const tr_torrent_t * tor, int fileIndex ) |
---|
712 | { |
---|
713 | const tr_file_t * file = &tor->info.files[fileIndex]; |
---|
714 | const uint64_t firstBlock = file->offset / tor->blockSize; |
---|
715 | const uint64_t firstBlockOffset = file->offset % tor->blockSize; |
---|
716 | const uint64_t lastOffset = file->length ? (file->length-1) : 0; |
---|
717 | const uint64_t lastBlock = (file->offset + lastOffset) / tor->blockSize; |
---|
718 | const uint64_t lastBlockOffset = (file->offset + lastOffset) % tor->blockSize; |
---|
719 | uint64_t haveBytes = 0; |
---|
720 | |
---|
721 | assert( tor != NULL ); |
---|
722 | assert( 0<=fileIndex && fileIndex<tor->info.fileCount ); |
---|
723 | assert( file->offset + file->length <= tor->info.totalSize ); |
---|
724 | assert( (int)firstBlock < tor->blockCount ); |
---|
725 | assert( (int)lastBlock < tor->blockCount ); |
---|
726 | assert( firstBlock <= lastBlock ); |
---|
727 | assert( tr_blockPiece( firstBlock ) == file->firstPiece ); |
---|
728 | assert( tr_blockPiece( lastBlock ) == file->lastPiece ); |
---|
729 | |
---|
730 | if( firstBlock == lastBlock ) |
---|
731 | { |
---|
732 | if( tr_cpBlockIsComplete( tor->completion, firstBlock ) ) |
---|
733 | haveBytes += lastBlockOffset + 1 - firstBlockOffset; |
---|
734 | } |
---|
735 | else |
---|
736 | { |
---|
737 | uint64_t i; |
---|
738 | |
---|
739 | if( tr_cpBlockIsComplete( tor->completion, firstBlock ) ) |
---|
740 | haveBytes += tor->blockSize - firstBlockOffset; |
---|
741 | |
---|
742 | for( i=firstBlock+1; i<lastBlock; ++i ) |
---|
743 | if( tr_cpBlockIsComplete( tor->completion, i ) ) |
---|
744 | haveBytes += tor->blockSize; |
---|
745 | |
---|
746 | if( tr_cpBlockIsComplete( tor->completion, lastBlock ) ) |
---|
747 | haveBytes += lastBlockOffset + 1; |
---|
748 | } |
---|
749 | |
---|
750 | return haveBytes; |
---|
751 | } |
---|
752 | |
---|
753 | float |
---|
754 | tr_torrentFileCompletion ( const tr_torrent_t * tor, int fileIndex ) |
---|
755 | { |
---|
756 | const uint64_t c = tr_torrentFileBytesCompleted ( tor, fileIndex ); |
---|
757 | uint64_t length = tor->info.files[fileIndex].length; |
---|
758 | |
---|
759 | if( !length ) |
---|
760 | return 1.0; |
---|
761 | return (double)c / length; |
---|
762 | } |
---|
763 | |
---|
764 | float* |
---|
765 | tr_torrentCompletion( const tr_torrent_t * tor ) |
---|
766 | { |
---|
767 | int i; |
---|
768 | float * f; |
---|
769 | tr_torrentReaderLock( tor ); |
---|
770 | |
---|
771 | f = tr_new0( float, tor->info.fileCount ); |
---|
772 | for( i=0; i<tor->info.fileCount; ++i ) |
---|
773 | f[i] = tr_torrentFileCompletion ( tor, i ); |
---|
774 | |
---|
775 | tr_torrentReaderUnlock( tor ); |
---|
776 | return f; |
---|
777 | } |
---|
778 | |
---|
779 | void tr_torrentAmountFinished( const tr_torrent_t * tor, float * tab, int size ) |
---|
780 | { |
---|
781 | int i; |
---|
782 | float interval; |
---|
783 | tr_torrentReaderLock( tor ); |
---|
784 | |
---|
785 | interval = (float)tor->info.pieceCount / (float)size; |
---|
786 | for( i = 0; i < size; i++ ) |
---|
787 | { |
---|
788 | int piece = i * interval; |
---|
789 | tab[i] = tr_cpPercentBlocksInPiece( tor->completion, piece ); |
---|
790 | } |
---|
791 | |
---|
792 | tr_torrentReaderUnlock( tor ); |
---|
793 | } |
---|
794 | |
---|
795 | void |
---|
796 | tr_torrentResetTransferStats( tr_torrent_t * tor ) |
---|
797 | { |
---|
798 | tr_torrentWriterLock( tor ); |
---|
799 | |
---|
800 | tor->downloadedPrev += tor->downloadedCur; |
---|
801 | tor->downloadedCur = 0; |
---|
802 | tor->uploadedPrev += tor->uploadedCur; |
---|
803 | tor->uploadedCur = 0; |
---|
804 | |
---|
805 | tr_torrentWriterUnlock( tor ); |
---|
806 | } |
---|
807 | |
---|
808 | |
---|
809 | void |
---|
810 | tr_torrentSetHasPiece( tr_torrent_t * tor, int pieceIndex, int has ) |
---|
811 | { |
---|
812 | tr_torrentWriterLock( tor ); |
---|
813 | |
---|
814 | if( has ) |
---|
815 | tr_cpPieceAdd( tor->completion, pieceIndex ); |
---|
816 | else |
---|
817 | tr_cpPieceRem( tor->completion, pieceIndex ); |
---|
818 | |
---|
819 | tr_torrentWriterUnlock( tor ); |
---|
820 | } |
---|
821 | |
---|
822 | void tr_torrentRemoveSaved( tr_torrent_t * tor ) |
---|
823 | { |
---|
824 | tr_metainfoRemoveSaved( tor->info.hashString, tor->handle->tag ); |
---|
825 | } |
---|
826 | |
---|
827 | void tr_torrentRecheck( tr_torrent_t * tor ) |
---|
828 | { |
---|
829 | tor->recheckFlag = TRUE; |
---|
830 | } |
---|
831 | |
---|
832 | |
---|
833 | int tr_torrentAttachPeer( tr_torrent_t * tor, tr_peer_t * peer ) |
---|
834 | { |
---|
835 | int i; |
---|
836 | tr_peer_t * otherPeer; |
---|
837 | |
---|
838 | assert( tor != NULL ); |
---|
839 | assert( peer != NULL ); |
---|
840 | |
---|
841 | if( tor->peerCount >= TR_MAX_PEER_COUNT ) |
---|
842 | { |
---|
843 | tr_peerDestroy( peer ); |
---|
844 | return 0; |
---|
845 | } |
---|
846 | |
---|
847 | /* Don't accept two connections from the same IP */ |
---|
848 | for( i = 0; i < tor->peerCount; i++ ) |
---|
849 | { |
---|
850 | otherPeer = tor->peers[i]; |
---|
851 | if( !memcmp( tr_peerAddress( peer ), tr_peerAddress( otherPeer ), 4 ) ) |
---|
852 | { |
---|
853 | tr_peerDestroy( peer ); |
---|
854 | return 0; |
---|
855 | } |
---|
856 | } |
---|
857 | |
---|
858 | tr_peerSetPrivate( peer, tor->info.flags & TR_FLAG_PRIVATE || |
---|
859 | tor->pexDisabled ); |
---|
860 | tr_peerSetTorrent( peer, tor ); |
---|
861 | tor->peers[tor->peerCount++] = peer; |
---|
862 | |
---|
863 | return 1; |
---|
864 | } |
---|
865 | |
---|
866 | int tr_torrentAddCompact( tr_torrent_t * tor, int from, |
---|
867 | uint8_t * buf, int count ) |
---|
868 | { |
---|
869 | struct in_addr addr; |
---|
870 | in_port_t port; |
---|
871 | int i, added; |
---|
872 | tr_peer_t * peer; |
---|
873 | |
---|
874 | added = 0; |
---|
875 | for( i = 0; i < count; i++ ) |
---|
876 | { |
---|
877 | memcpy( &addr, buf, 4 ); buf += 4; |
---|
878 | memcpy( &port, buf, 2 ); buf += 2; |
---|
879 | |
---|
880 | peer = tr_peerInit( &addr, port, -1, from ); |
---|
881 | added += tr_torrentAttachPeer( tor, peer ); |
---|
882 | } |
---|
883 | |
---|
884 | return added; |
---|
885 | } |
---|
886 | |
---|
887 | /*** |
---|
888 | **** |
---|
889 | ***/ |
---|
890 | |
---|
891 | static void setRunState( tr_torrent_t * tor, run_status_t run ) |
---|
892 | { |
---|
893 | tr_torrentWriterLock( tor ); |
---|
894 | tor->runStatus = run; |
---|
895 | tr_torrentWriterUnlock( tor ); |
---|
896 | } |
---|
897 | |
---|
898 | void tr_torrentStart( tr_torrent_t * tor ) |
---|
899 | { |
---|
900 | setRunState( tor, TR_RUN_RUNNING ); |
---|
901 | } |
---|
902 | |
---|
903 | void tr_torrentStop( tr_torrent_t * tor ) |
---|
904 | { |
---|
905 | setRunState( tor, TR_RUN_STOPPING ); |
---|
906 | } |
---|
907 | |
---|
908 | void tr_torrentClose( tr_torrent_t * tor ) |
---|
909 | { |
---|
910 | tr_torrentStop( tor ); |
---|
911 | tor->dieFlag = TRUE; |
---|
912 | } |
---|
913 | |
---|
914 | static void |
---|
915 | tr_torrentFree( tr_torrent_t * tor ) |
---|
916 | { |
---|
917 | tr_torrent_t * t; |
---|
918 | tr_handle_t * h = tor->handle; |
---|
919 | tr_info_t * inf = &tor->info; |
---|
920 | |
---|
921 | tr_sharedLock( h->shared ); |
---|
922 | |
---|
923 | tr_rwClose( &tor->lock ); |
---|
924 | tr_cpClose( tor->completion ); |
---|
925 | |
---|
926 | tr_rcClose( tor->upload ); |
---|
927 | tr_rcClose( tor->download ); |
---|
928 | tr_rcClose( tor->swarmspeed ); |
---|
929 | |
---|
930 | tr_free( tor->destination ); |
---|
931 | |
---|
932 | tr_metainfoFree( inf ); |
---|
933 | |
---|
934 | if( tor == h->torrentList ) |
---|
935 | h->torrentList = tor->next; |
---|
936 | else for( t=h->torrentList; t!=NULL; t=t->next ) { |
---|
937 | if( t->next == tor ) { |
---|
938 | t->next = tor->next; |
---|
939 | break; |
---|
940 | } |
---|
941 | } |
---|
942 | |
---|
943 | tr_free( tor ); |
---|
944 | |
---|
945 | h->torrentCount--; |
---|
946 | |
---|
947 | tr_sharedUnlock( h->shared ); |
---|
948 | } |
---|
949 | |
---|
950 | static void |
---|
951 | torrentThreadLoop ( void * _tor ) |
---|
952 | { |
---|
953 | static tr_lock_t checkFilesLock; |
---|
954 | static int checkFilesLockInited = FALSE; |
---|
955 | tr_torrent_t * tor = _tor; |
---|
956 | |
---|
957 | /* create the check-files mutex */ |
---|
958 | if( !checkFilesLockInited ) { |
---|
959 | checkFilesLockInited = TRUE; |
---|
960 | tr_lockInit( &checkFilesLock ); |
---|
961 | } |
---|
962 | |
---|
963 | /* loop until the torrent is being deleted */ |
---|
964 | while( ! ( tor->dieFlag && (tor->runStatus == TR_RUN_STOPPED) ) ) |
---|
965 | { |
---|
966 | cp_status_t cpStatus; |
---|
967 | |
---|
968 | /* sleep a little while */ |
---|
969 | tr_wait( tor->runStatus == TR_RUN_STOPPED ? 1600 : 600 ); |
---|
970 | |
---|
971 | /* if we're stopping... */ |
---|
972 | if( tor->runStatus == TR_RUN_STOPPING ) |
---|
973 | { |
---|
974 | int i; |
---|
975 | int peerCount; |
---|
976 | uint8_t * peerCompact; |
---|
977 | tr_torrentWriterLock( tor ); |
---|
978 | |
---|
979 | /* close the IO */ |
---|
980 | tr_ioClose( tor->io ); |
---|
981 | tor->io = NULL; |
---|
982 | |
---|
983 | /* close the peers */ |
---|
984 | for( i=0; i<tor->peerCount; ++i ) |
---|
985 | tr_peerDestroy( tor->peers[i] ); |
---|
986 | tor->peerCount = 0; |
---|
987 | |
---|
988 | /* resest the transfer rates */ |
---|
989 | tr_rcReset( tor->download ); |
---|
990 | tr_rcReset( tor->upload ); |
---|
991 | tr_rcReset( tor->swarmspeed ); |
---|
992 | |
---|
993 | /* tell the tracker we're stopping */ |
---|
994 | tr_trackerStopped( tor->tracker ); |
---|
995 | tr_trackerPulse( tor->tracker, &peerCount, &peerCompact ); |
---|
996 | tor->runStatus = TR_RUN_STOPPING_NET_WAIT; |
---|
997 | tor->stopDate = tr_date(); |
---|
998 | tr_torrentWriterUnlock( tor ); |
---|
999 | } |
---|
1000 | |
---|
1001 | if( tor->runStatus == TR_RUN_STOPPING_NET_WAIT ) |
---|
1002 | { |
---|
1003 | uint64_t date; |
---|
1004 | int peerCount; |
---|
1005 | uint8_t * peerCompact; |
---|
1006 | tr_trackerPulse( tor->tracker, &peerCount, &peerCompact ); |
---|
1007 | |
---|
1008 | /* have we finished telling the tracker that we're stopping? */ |
---|
1009 | date = tr_trackerLastResponseDate( tor->tracker ); |
---|
1010 | if( date > tor->stopDate ) |
---|
1011 | { |
---|
1012 | tr_torrentWriterLock( tor ); |
---|
1013 | tr_trackerClose( tor->tracker ); |
---|
1014 | tor->tracker = NULL; |
---|
1015 | tor->runStatus = TR_RUN_STOPPED; |
---|
1016 | tr_torrentWriterUnlock( tor ); |
---|
1017 | } |
---|
1018 | continue; |
---|
1019 | } |
---|
1020 | |
---|
1021 | /* do we need to check files? */ |
---|
1022 | if( tor->recheckFlag ) |
---|
1023 | { |
---|
1024 | if( !tr_lockTryLock( &checkFilesLock ) ) |
---|
1025 | { |
---|
1026 | run_status_t realStatus; |
---|
1027 | |
---|
1028 | tr_torrentWriterLock( tor ); |
---|
1029 | realStatus = tor->runStatus; |
---|
1030 | tor->recheckFlag = FALSE; |
---|
1031 | tor->runStatus = TR_RUN_CHECKING; |
---|
1032 | tr_torrentWriterUnlock( tor ); |
---|
1033 | |
---|
1034 | tr_ioCheckFiles( tor, TR_RECHECK_FORCE ); |
---|
1035 | setRunState( tor, realStatus ); |
---|
1036 | |
---|
1037 | tr_torrentWriterLock( tor ); |
---|
1038 | tor->cpStatus = tr_cpGetStatus( tor->completion ); |
---|
1039 | tr_torrentWriterUnlock( tor ); |
---|
1040 | |
---|
1041 | tr_lockUnlock( &checkFilesLock ); |
---|
1042 | } |
---|
1043 | continue; |
---|
1044 | } |
---|
1045 | |
---|
1046 | /* if we're paused or stopped, not much to do... */ |
---|
1047 | if( tor->runStatus == TR_RUN_STOPPED ) |
---|
1048 | continue; |
---|
1049 | |
---|
1050 | /* ping our peers if we're running... */ |
---|
1051 | if( tor->runStatus == TR_RUN_RUNNING ) |
---|
1052 | { |
---|
1053 | int i; |
---|
1054 | int peerCount; |
---|
1055 | uint8_t * peerCompact; |
---|
1056 | |
---|
1057 | /* starting to run... */ |
---|
1058 | if( tor->io == NULL ) { |
---|
1059 | *tor->errorString = '\0'; |
---|
1060 | tr_torrentResetTransferStats( tor ); |
---|
1061 | tor->io = tr_ioInitFast( tor ); |
---|
1062 | if( tor->io == NULL ) { |
---|
1063 | tor->recheckFlag = TRUE; |
---|
1064 | continue; |
---|
1065 | } |
---|
1066 | tor->tracker = tr_trackerInit( tor ); |
---|
1067 | tor->startDate = tr_date(); |
---|
1068 | } |
---|
1069 | |
---|
1070 | /* refresh our completion state */ |
---|
1071 | tr_torrentWriterLock( tor ); |
---|
1072 | cpStatus = tr_cpGetStatus( tor->completion ); |
---|
1073 | if( cpStatus != tor->cpStatus ) { |
---|
1074 | tor->cpStatus = cpStatus; |
---|
1075 | if( (cpStatus == TR_CP_COMPLETE) /* if we're complete */ |
---|
1076 | && tor->tracker!=NULL /* and we have a tracker */ |
---|
1077 | && tor->downloadedCur ) { /* and it just happened */ |
---|
1078 | tr_trackerCompleted( tor->tracker ); /* tell the tracker */ |
---|
1079 | tor->hasChangedState = tor->cpStatus; /* and the client */ |
---|
1080 | } |
---|
1081 | tr_ioSync( tor->io ); |
---|
1082 | } |
---|
1083 | tr_torrentWriterUnlock( tor ); |
---|
1084 | |
---|
1085 | /* ping the tracker... */ |
---|
1086 | tr_trackerPulse( tor->tracker, &peerCount, &peerCompact ); |
---|
1087 | if( peerCount > 0 ) { |
---|
1088 | int used = tr_torrentAddCompact( tor, TR_PEER_FROM_TRACKER, |
---|
1089 | peerCompact, peerCount ); |
---|
1090 | tr_dbg( "got %i peers from announce, used %i", peerCount, used ); |
---|
1091 | free( peerCompact ); |
---|
1092 | } |
---|
1093 | |
---|
1094 | /* Shuffle peers */ |
---|
1095 | if ( tor->peerCount > 1 ) { |
---|
1096 | tr_peer_t * tmp = tor->peers[0]; |
---|
1097 | memmove( tor->peers, tor->peers+1, |
---|
1098 | (tor->peerCount-1) * sizeof(void*) ); |
---|
1099 | tor->peers[tor->peerCount - 1] = tmp; |
---|
1100 | } |
---|
1101 | |
---|
1102 | /* receive/send messages */ |
---|
1103 | tr_torrentWriterLock( tor ); |
---|
1104 | for( i=0; i<tor->peerCount; ) { |
---|
1105 | tr_peer_t * peer = tor->peers[i]; |
---|
1106 | int ret = tr_peerPulse( peer ); |
---|
1107 | if( ret & TR_ERROR_IO_MASK ) { |
---|
1108 | tr_err( "Fatal error, stopping download (%d)", ret ); |
---|
1109 | tor->runStatus = TR_RUN_STOPPING; |
---|
1110 | tor->error = ret; |
---|
1111 | strlcpy( tor->errorString, |
---|
1112 | tr_errorString(ret), |
---|
1113 | sizeof(tor->errorString) ); |
---|
1114 | break; |
---|
1115 | } |
---|
1116 | if( ret ) { |
---|
1117 | tr_peerDestroy( peer ); |
---|
1118 | tor->peerCount--; |
---|
1119 | memmove( &tor->peers[i], &tor->peers[i+1], |
---|
1120 | (tor->peerCount-i)*sizeof(void*) ); |
---|
1121 | continue; |
---|
1122 | } |
---|
1123 | i++; |
---|
1124 | } |
---|
1125 | tr_torrentWriterUnlock( tor ); |
---|
1126 | } |
---|
1127 | } |
---|
1128 | |
---|
1129 | tr_ioClose( tor->io ); |
---|
1130 | tr_torrentFree( tor ); |
---|
1131 | } |
---|
1132 | |
---|
1133 | |
---|
1134 | /*** |
---|
1135 | **** |
---|
1136 | **** File prioritization |
---|
1137 | **** |
---|
1138 | ***/ |
---|
1139 | |
---|
1140 | static void |
---|
1141 | tr_torrentSetFilePriorityImpl( tr_torrent_t * tor, |
---|
1142 | int fileIndex, |
---|
1143 | tr_priority_t priority, |
---|
1144 | int doSave ) |
---|
1145 | { |
---|
1146 | int i; |
---|
1147 | tr_file_t * file; |
---|
1148 | |
---|
1149 | tr_torrentWriterLock( tor ); |
---|
1150 | |
---|
1151 | assert( tor != NULL ); |
---|
1152 | assert( 0<=fileIndex && fileIndex<tor->info.fileCount ); |
---|
1153 | assert( priority==TR_PRI_LOW || priority==TR_PRI_NORMAL || priority==TR_PRI_HIGH ); |
---|
1154 | |
---|
1155 | file = &tor->info.files[fileIndex]; |
---|
1156 | file->priority = priority; |
---|
1157 | for( i=file->firstPiece; i<=file->lastPiece; ++i ) |
---|
1158 | tor->info.pieces[i].priority = calculatePiecePriority( tor, i ); |
---|
1159 | |
---|
1160 | tr_dbg ( "Setting file #%d (pieces %d-%d) priority to %d (%s)", |
---|
1161 | fileIndex, file->firstPiece, file->lastPiece, |
---|
1162 | priority, tor->info.files[fileIndex].name ); |
---|
1163 | |
---|
1164 | if( doSave ) |
---|
1165 | fastResumeSave( tor ); |
---|
1166 | |
---|
1167 | tr_torrentWriterUnlock( tor ); |
---|
1168 | } |
---|
1169 | |
---|
1170 | void |
---|
1171 | tr_torrentSetFilePriority( tr_torrent_t * tor, |
---|
1172 | int fileIndex, |
---|
1173 | tr_priority_t priority ) |
---|
1174 | { |
---|
1175 | tr_torrentSetFilePriorityImpl( tor, fileIndex, priority, TRUE ); |
---|
1176 | } |
---|
1177 | |
---|
1178 | void |
---|
1179 | tr_torrentSetFilePriorities( tr_torrent_t * tor, |
---|
1180 | int * files, |
---|
1181 | int fileCount, |
---|
1182 | tr_priority_t priority ) |
---|
1183 | { |
---|
1184 | int i; |
---|
1185 | for( i=0; i<fileCount; ++i ) { |
---|
1186 | const int fileIndex = files[i]; |
---|
1187 | tr_torrentSetFilePriorityImpl( tor, fileIndex, priority, FALSE ); |
---|
1188 | } |
---|
1189 | fastResumeSave( tor ); |
---|
1190 | } |
---|
1191 | |
---|
1192 | tr_priority_t |
---|
1193 | tr_torrentGetFilePriority( const tr_torrent_t * tor, int file ) |
---|
1194 | { |
---|
1195 | tr_priority_t ret; |
---|
1196 | |
---|
1197 | tr_torrentReaderLock( tor ); |
---|
1198 | assert( tor != NULL ); |
---|
1199 | assert( 0<=file && file<tor->info.fileCount ); |
---|
1200 | ret = tor->info.files[file].priority; |
---|
1201 | tr_torrentReaderUnlock( tor ); |
---|
1202 | |
---|
1203 | return ret; |
---|
1204 | } |
---|
1205 | |
---|
1206 | |
---|
1207 | tr_priority_t* |
---|
1208 | tr_torrentGetFilePriorities( const tr_torrent_t * tor ) |
---|
1209 | { |
---|
1210 | int i; |
---|
1211 | tr_priority_t * p; |
---|
1212 | |
---|
1213 | tr_torrentReaderLock( tor ); |
---|
1214 | p = tr_new0( tr_priority_t, tor->info.fileCount ); |
---|
1215 | for( i=0; i<tor->info.fileCount; ++i ) |
---|
1216 | p[i] = tor->info.files[i].priority; |
---|
1217 | tr_torrentReaderUnlock( tor ); |
---|
1218 | |
---|
1219 | return p; |
---|
1220 | } |
---|
1221 | |
---|
1222 | int |
---|
1223 | tr_torrentGetFileDL( const tr_torrent_t * tor, |
---|
1224 | int file ) |
---|
1225 | { |
---|
1226 | int do_download; |
---|
1227 | tr_torrentReaderLock( tor ); |
---|
1228 | |
---|
1229 | assert( 0<=file && file<tor->info.fileCount ); |
---|
1230 | do_download = !tor->info.files[file].dnd; |
---|
1231 | |
---|
1232 | tr_torrentReaderUnlock( tor ); |
---|
1233 | return do_download != 0; |
---|
1234 | } |
---|
1235 | |
---|
1236 | void |
---|
1237 | tr_torrentSetFileDL( tr_torrent_t * tor, |
---|
1238 | int fileIndex, |
---|
1239 | int do_download ) |
---|
1240 | { |
---|
1241 | int i; |
---|
1242 | tr_file_t * file; |
---|
1243 | const int dnd = !do_download; |
---|
1244 | |
---|
1245 | tr_torrentWriterLock( tor ); |
---|
1246 | |
---|
1247 | assert( 0<=fileIndex && fileIndex<tor->info.fileCount ); |
---|
1248 | file = &tor->info.files[fileIndex]; |
---|
1249 | file->dnd = dnd; |
---|
1250 | for( i=file->firstPiece; i<=file->lastPiece; ++i ) |
---|
1251 | tor->info.pieces[i].dnd = dnd; |
---|
1252 | fastResumeSave( tor ); |
---|
1253 | |
---|
1254 | tr_torrentWriterUnlock( tor ); |
---|
1255 | } |
---|
1256 | |
---|
1257 | void |
---|
1258 | tr_torrentSetFileDLs ( tr_torrent_t * tor, |
---|
1259 | int * files, |
---|
1260 | int fileCount, |
---|
1261 | int do_download ) |
---|
1262 | { |
---|
1263 | int i, j; |
---|
1264 | const int dnd = !do_download; |
---|
1265 | |
---|
1266 | tr_torrentWriterLock( tor ); |
---|
1267 | |
---|
1268 | for( i=0; i<fileCount; ++i ) { |
---|
1269 | const int fileIndex = files[i]; |
---|
1270 | tr_file_t * file = &tor->info.files[fileIndex]; |
---|
1271 | file->dnd = dnd; |
---|
1272 | for( j=file->firstPiece; j<=file->lastPiece; ++j ) |
---|
1273 | tor->info.pieces[j].dnd = dnd; |
---|
1274 | } |
---|
1275 | |
---|
1276 | fastResumeSave( tor ); |
---|
1277 | |
---|
1278 | tr_torrentWriterUnlock( tor ); |
---|
1279 | } |
---|
1280 | |
---|
1281 | cp_status_t |
---|
1282 | tr_torrentGetFileStatus( const tr_torrent_t * tor, int fileIndex ) |
---|
1283 | { |
---|
1284 | int i; |
---|
1285 | int isComplete; |
---|
1286 | const tr_file_t * file; |
---|
1287 | |
---|
1288 | assert( tor != NULL ); |
---|
1289 | assert( 0<=fileIndex ); |
---|
1290 | assert( fileIndex<tor->info.fileCount ); |
---|
1291 | |
---|
1292 | file = &tor->info.files[fileIndex]; |
---|
1293 | |
---|
1294 | isComplete = TRUE; |
---|
1295 | for( i=file->firstPiece; isComplete && i<=file->lastPiece; ++i ) |
---|
1296 | if( !tr_cpPieceIsComplete( tor->completion, i ) ) |
---|
1297 | isComplete = FALSE; |
---|
1298 | |
---|
1299 | if( isComplete ) |
---|
1300 | return TR_CP_COMPLETE; |
---|
1301 | |
---|
1302 | if( file->dnd ) |
---|
1303 | return TR_CP_DONE; |
---|
1304 | |
---|
1305 | return TR_CP_INCOMPLETE; |
---|
1306 | } |
---|