source: trunk/libtransmission/torrent.h @ 9868

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

happy new year!

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