source: trunk/libtransmission/torrent.h @ 9671

Last change on this file since 9671 was 9671, checked in by charles, 12 years ago

(trunk) update the copyright notices

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