source: trunk/libtransmission/torrent.h @ 11781

Last change on this file since 11781 was 11781, checked in by jordan, 11 years ago

(trunk libT) #3955 "tr_torrentNext() should be inlined" -- fixed.

  • Property svn:keywords set to Date Rev Author Id
File size: 13.4 KB
Line 
1/*
2 * This file Copyright (C) Mnemosyne LLC
3 *
4 * This file is licensed by the GPL version 2. Works owned by the
5 * Transmission project are granted a special exemption to clause 2(b)
6 * so that the bulk of its code can remain under the MIT license.
7 * This exemption does not extend to derived works not owned by
8 * the Transmission project.
9 *
10 * $Id: torrent.h 11781 2011-01-29 18:54:43Z jordan $
11 */
12
13#ifndef __TRANSMISSION__
14 #error only libtransmission should #include this header.
15#endif
16
17#ifndef TR_TORRENT_H
18#define TR_TORRENT_H 1
19
20#include "completion.h" /* tr_completion */
21#include "session.h" /* tr_sessionLock(), tr_sessionUnlock() */
22#include "utils.h" /* TR_GNUC_PRINTF */
23
24struct tr_bandwidth;
25struct tr_torrent_tiers;
26struct tr_magnet_info;
27
28/**
29***  Package-visible ctor API
30**/
31
32void        tr_ctorSetSave( tr_ctor * ctor,
33                            tr_bool   saveMetadataInOurTorrentsDir );
34
35int         tr_ctorGetSave( const tr_ctor * ctor );
36
37void        tr_ctorInitTorrentPriorities( const tr_ctor * ctor, tr_torrent * tor );
38
39void        tr_ctorInitTorrentWanted( const tr_ctor * ctor, tr_torrent * tor );
40
41/**
42***
43**/
44
45/* just like tr_torrentSetFileDLs but doesn't trigger a fastresume save */
46void        tr_torrentInitFileDLs( tr_torrent              * tor,
47                                   const tr_file_index_t   * files,
48                                   tr_file_index_t          fileCount,
49                                   tr_bool                  do_download );
50
51void        tr_torrentRecheckCompleteness( tr_torrent * );
52
53void        tr_torrentSetHasPiece( tr_torrent *     tor,
54                                   tr_piece_index_t pieceIndex,
55                                   tr_bool          has );
56
57void        tr_torrentChangeMyPort( tr_torrent * session );
58
59tr_torrent* tr_torrentFindFromHashString( tr_session * session,
60                                          const char * hashString );
61
62tr_torrent* tr_torrentFindFromObfuscatedHash( tr_session    * session,
63                                              const uint8_t * hash );
64
65tr_bool     tr_torrentIsPieceTransferAllowed( const tr_torrent * torrent,
66                                              tr_direction       direction );
67
68
69
70#define tr_block( a, b ) _tr_block( tor, a, b )
71tr_block_index_t _tr_block( const tr_torrent * tor,
72                            tr_piece_index_t   index,
73                            uint32_t           offset );
74
75tr_bool          tr_torrentReqIsValid( const tr_torrent * tor,
76                                       tr_piece_index_t   index,
77                                       uint32_t           offset,
78                                       uint32_t           length );
79
80uint64_t         tr_pieceOffset( const tr_torrent * tor,
81                                 tr_piece_index_t   index,
82                                 uint32_t           offset,
83                                 uint32_t           length );
84
85void             tr_torrentInitFilePriority( tr_torrent       * tor,
86                                             tr_file_index_t    fileIndex,
87                                             tr_priority_t      priority );
88
89void             tr_torrentSetPieceChecked( tr_torrent       * tor,
90                                            tr_piece_index_t   piece );
91
92void             tr_torrentSetChecked( tr_torrent * tor, time_t when );
93
94void             tr_torrentCheckSeedLimit( tr_torrent * tor );
95
96/** save a torrent's .resume file if it's changed since the last time it was saved */
97void             tr_torrentSave( tr_torrent * tor );
98
99void             tr_torrentSetLocalError( tr_torrent * tor, const char * fmt, ... ) TR_GNUC_PRINTF( 2, 3 );
100
101
102
103typedef enum
104{
105    TR_VERIFY_NONE,
106    TR_VERIFY_WAIT,
107    TR_VERIFY_NOW
108}
109tr_verify_state;
110
111void             tr_torrentSetVerifyState( tr_torrent      * tor,
112                                           tr_verify_state   state );
113
114tr_torrent_activity tr_torrentGetActivity( tr_torrent * tor );
115
116struct tr_incomplete_metadata;
117
118/** @brief Torrent object */
119struct tr_torrent
120{
121    tr_session *             session;
122    tr_info                  info;
123
124    int                      magicNumber;
125
126    tr_stat_errtype          error;
127    char                     errorString[128];
128    char                     errorTracker[128];
129
130    uint8_t                  obfuscatedHash[SHA_DIGEST_LENGTH];
131
132    /* Used when the torrent has been created with a magnet link
133     * and we're in the process of downloading the metainfo from
134     * other peers */
135    struct tr_incomplete_metadata  * incompleteMetadata;
136
137    /* If the initiator of the connection receives a handshake in which the
138     * peer_id does not match the expected peerid, then the initiator is
139     * expected to drop the connection. Note that the initiator presumably
140     * received the peer information from the tracker, which includes the
141     * peer_id that was registered by the peer. The peer_id from the tracker
142     * and in the handshake are expected to match.
143     */
144    uint8_t * peer_id;
145
146    /* Where the files will be when it's complete */
147    char * downloadDir;
148
149    /* Where the files are when the torrent is incomplete */
150    char * incompleteDir;
151
152    /* Length, in bytes, of the "info" dict in the .torrent file. */
153    int infoDictLength;
154
155    /* Offset, in bytes, of the beginning of the "info" dict in the .torrent file.
156     *
157     * Used by the torrent-magnet code for serving metainfo to peers.
158     * This field is lazy-generated and might not be initialized yet. */
159    int infoDictOffset;
160
161    /* Where the files are now.
162     * This pointer will be equal to downloadDir or incompleteDir */
163    const char * currentDir;
164
165    /* How many bytes we ask for per request */
166    uint32_t                   blockSize;
167    tr_block_index_t           blockCount;
168
169    uint32_t                   lastBlockSize;
170    uint32_t                   lastPieceSize;
171
172    uint16_t                   blockCountInPiece;
173    uint16_t                   blockCountInLastPiece;
174
175    struct tr_completion       completion;
176
177    tr_completeness            completeness;
178
179    struct tr_torrent_tiers  * tiers;
180
181    time_t                     dhtAnnounceAt;
182    time_t                     dhtAnnounce6At;
183    tr_bool                    dhtAnnounceInProgress;
184    tr_bool                    dhtAnnounce6InProgress;
185
186    time_t                     lpdAnnounceAt;
187
188    uint64_t                   downloadedCur;
189    uint64_t                   downloadedPrev;
190    uint64_t                   uploadedCur;
191    uint64_t                   uploadedPrev;
192    uint64_t                   corruptCur;
193    uint64_t                   corruptPrev;
194
195    uint64_t                   etaDLSpeedCalculatedAt;
196    double                     etaDLSpeed_KBps;
197    uint64_t                   etaULSpeedCalculatedAt;
198    double                     etaULSpeed_KBps;
199
200    time_t                     addedDate;
201    time_t                     activityDate;
202    time_t                     doneDate;
203    time_t                     startDate;
204    time_t                     anyDate;
205
206    time_t                     secondsDownloading;
207    time_t                     secondsSeeding;
208
209    tr_torrent_metadata_func  * metadata_func;
210    void                      * metadata_func_user_data;
211
212    tr_torrent_completeness_func  * completeness_func;
213    void                          *  completeness_func_user_data;
214
215    tr_torrent_ratio_limit_hit_func  * ratio_limit_hit_func;
216    void                             * ratio_limit_hit_func_user_data;
217
218    tr_torrent_idle_limit_hit_func  * idle_limit_hit_func;
219    void                            * idle_limit_hit_func_user_data;
220
221    tr_bool                    isRunning;
222    tr_bool                    isStopping;
223    tr_bool                    isDeleting;
224    tr_bool                    startAfterVerify;
225    tr_bool                    isDirty;
226
227    tr_bool                    infoDictOffsetIsCached;
228
229    uint16_t                   maxConnectedPeers;
230
231    tr_verify_state            verifyState;
232
233    time_t                     lastStatTime;
234    tr_stat                    stats;
235
236    tr_torrent *               next;
237
238    int                        uniqueId;
239
240    struct tr_bandwidth      * bandwidth;
241
242    struct tr_torrent_peers  * torrentPeers;
243
244    double                     desiredRatio;
245    tr_ratiolimit              ratioLimitMode;
246
247    uint16_t                   idleLimitMinutes;
248    tr_idlelimit               idleLimitMode;
249    tr_bool                    finishedSeedingByIdle;
250};
251
252static inline tr_torrent*
253tr_torrentNext( tr_session * session, tr_torrent * current )
254{
255    return current ? current->next : session->torrentList;
256}
257
258/* get the index of this piece's first block */
259static inline tr_block_index_t
260tr_torPieceFirstBlock( const tr_torrent * tor, const tr_piece_index_t piece )
261{
262    return piece * tor->blockCountInPiece;
263}
264
265/* what piece index is this block in? */
266static inline tr_piece_index_t
267tr_torBlockPiece( const tr_torrent * tor, const tr_block_index_t block )
268{
269    return block / tor->blockCountInPiece;
270}
271
272/* how many blocks are in this piece? */
273static inline uint16_t
274tr_torPieceCountBlocks( const tr_torrent * tor, const tr_piece_index_t piece )
275{
276    if( piece + 1 == tor->info.pieceCount )
277        return tor->blockCountInLastPiece;
278    else
279        return tor->blockCountInPiece;
280}
281
282/* how many bytes are in this piece? */
283static inline uint32_t
284tr_torPieceCountBytes( const tr_torrent * tor, const tr_piece_index_t piece )
285{
286    return piece == tor->info.pieceCount - 1 ? tor->lastPieceSize
287                                             : tor->info.pieceSize;
288}
289
290/* how many bytes are in this block? */
291static inline uint32_t
292tr_torBlockCountBytes( const tr_torrent * tor, const tr_block_index_t block )
293{
294    return block == tor->blockCount - 1 ? tor->lastBlockSize
295                                        : tor->blockSize;
296}
297
298static inline void tr_torrentLock( const tr_torrent * tor )
299{
300    tr_sessionLock( tor->session );
301}
302
303static inline tr_bool tr_torrentIsLocked( const tr_torrent * tor )
304{
305    return tr_sessionIsLocked( tor->session );
306}
307
308static inline void tr_torrentUnlock( const tr_torrent * tor )
309{
310    tr_sessionUnlock( tor->session );
311}
312
313static inline tr_bool
314tr_torrentExists( const tr_session * session, const uint8_t *   torrentHash )
315{
316    return tr_torrentFindFromHash( (tr_session*)session, torrentHash ) != NULL;
317}
318
319static inline tr_bool
320tr_torrentIsSeed( const tr_torrent * tor )
321{
322    return tor->completeness != TR_LEECH;
323}
324
325static inline tr_bool tr_torrentIsPrivate( const tr_torrent * tor )
326{
327    return ( tor != NULL ) && tor->info.isPrivate;
328}
329
330static inline tr_bool tr_torrentAllowsPex( const tr_torrent * tor )
331{
332    return ( tor != NULL )
333        && ( tor->session->isPexEnabled )
334        && ( !tr_torrentIsPrivate( tor ) );
335}
336
337static inline tr_bool tr_torrentAllowsDHT( const tr_torrent * tor )
338{
339    return ( tor != NULL )
340        && ( tr_sessionAllowsDHT( tor->session ) )
341        && ( !tr_torrentIsPrivate( tor ) );
342}
343
344static inline tr_bool tr_torrentAllowsLPD( const tr_torrent * tor )
345{
346    return ( tor != NULL )
347        && ( tr_sessionAllowsLPD( tor->session ) )
348        && ( !tr_torrentIsPrivate( tor ) );
349}
350
351/***
352****
353***/
354
355enum
356{
357    TORRENT_MAGIC_NUMBER = 95549
358};
359
360static inline tr_bool tr_isTorrent( const tr_torrent * tor )
361{
362    return ( tor != NULL )
363        && ( tor->magicNumber == TORRENT_MAGIC_NUMBER )
364        && ( tr_isSession( tor->session ) );
365}
366
367/* set a flag indicating that the torrent's .resume file
368 * needs to be saved when the torrent is closed */
369static inline
370void tr_torrentSetDirty( tr_torrent * tor )
371{
372    assert( tr_isTorrent( tor ) );
373
374    tor->isDirty = TRUE;
375}
376
377static inline
378const char * tr_torrentName( const tr_torrent * tor )
379{
380    assert( tr_isTorrent( tor ) );
381
382    return tor->info.name;
383}
384
385uint32_t tr_getBlockSize( uint32_t pieceSize );
386
387/**
388 * Tell the tr_torrent that one of its files has become complete
389 */
390void tr_torrentFileCompleted( tr_torrent * tor, tr_file_index_t fileNo );
391
392
393/**
394 * @brief Like tr_torrentFindFile(), but splits the filename into base and subpath;
395 *
396 * If the file is found, "tr_buildPath( base, subpath, NULL )"
397 * will generate the complete filename.
398 *
399 * @return true if the file is found, false otherwise.
400 *
401 * @param base if the torrent is found, this will be either
402 *             tor->downloadDir or tor->incompleteDir
403 * @param subpath on success, this pointer is assigned a newly-allocated
404 *                string holding the second half of the filename.
405 */
406tr_bool tr_torrentFindFile2( const tr_torrent *, tr_file_index_t fileNo,
407                             const char ** base, char ** subpath );
408
409
410/* Returns a newly-allocated version of the tr_file.name string
411 * that's been modified to denote that it's not a complete file yet.
412 * In the current implementation this is done by appending ".part"
413 * a la Firefox. */
414char* tr_torrentBuildPartial( const tr_torrent *, tr_file_index_t fileNo );
415
416/* for when the info dict has been fundamentally changed wrt files,
417 * piece size, etc. such as in BEP 9 where peers exchange metadata */
418void tr_torrentGotNewInfoDict( tr_torrent * tor );
419
420void tr_torrentSetSpeedLimit_Bps  ( tr_torrent *, tr_direction, int Bps );
421int tr_torrentGetSpeedLimit_Bps  ( const tr_torrent *, tr_direction );
422
423/**
424 * @return true if this piece needs to be tested
425 */
426tr_bool tr_torrentPieceNeedsCheck( const tr_torrent * tor, tr_piece_index_t pieceIndex );
427
428/**
429 * @brief Test a piece against its info dict checksum
430 * @return true if the piece's passes the checksum test
431 */
432tr_bool tr_torrentCheckPiece( tr_torrent * tor, tr_piece_index_t pieceIndex );
433
434uint64_t tr_torrentGetCurrentSizeOnDisk( const tr_torrent * tor );
435
436
437#endif
Note: See TracBrowser for help on using the repository browser.