source: trunk/libtransmission/torrent.h @ 8090

Last change on this file since 8090 was 8090, checked in by charles, 13 years ago

(trunk) rpc changes:

  1. new field "blocklist-size" to session-info, so RPC clients can know how big the blocklist is
  2. new method "torrent-reannounce" so RPC clients can ask the tracker for more peers
  • Property svn:keywords set to Date Rev Author Id
File size: 9.6 KB
Line 
1/*
2 * This file Copyright (C) 2009 Charles Kerr <charles@transmissionbt.com>
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 8090 2009-03-26 18:06:54Z 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_bitfield */
24
25struct tr_bandwidth;
26struct tr_ratecontrol;
27struct tr_torrent_peers;
28
29/**
30***  Package-visible ctor API
31**/
32
33void        tr_ctorSetSave( tr_ctor * ctor,
34                            tr_bool   saveMetadataInOurTorrentsDir );
35
36int         tr_ctorGetSave( const tr_ctor * ctor );
37
38/**
39***
40**/
41
42/* just like tr_torrentSetFileDLs but doesn't trigger a fastresume save */
43void        tr_torrentInitFileDLs( tr_torrent *      tor,
44                                   tr_file_index_t * files,
45                                   tr_file_index_t   fileCount,
46                                   tr_bool           do_download );
47
48void        tr_torrentRecheckCompleteness( tr_torrent * );
49
50void        tr_torrentResetTransferStats( tr_torrent * );
51
52void        tr_torrentSetHasPiece( tr_torrent *     tor,
53                                   tr_piece_index_t pieceIndex,
54                                   tr_bool          has );
55
56void        tr_torrentChangeMyPort( tr_torrent * session );
57
58tr_torrent* tr_torrentFindFromId( tr_session * session,
59                                  int          id );
60
61tr_torrent* tr_torrentFindFromHash( tr_session *    session,
62                                    const uint8_t * hash );
63
64tr_torrent* tr_torrentFindFromHashString( tr_session * session,
65                                          const char * hashString );
66
67tr_torrent* tr_torrentFindFromObfuscatedHash( tr_session    * session,
68                                              const uint8_t * hash );
69
70tr_bool     tr_torrentIsPieceTransferAllowed( const tr_torrent * torrent,
71                                              tr_direction       direction );
72
73
74
75#define tr_block( a, b ) _tr_block( tor, a, b )
76tr_block_index_t _tr_block( const tr_torrent * tor,
77                            tr_piece_index_t   index,
78                            uint32_t           offset );
79
80tr_bool          tr_torrentReqIsValid( const tr_torrent * tor,
81                                       tr_piece_index_t   index,
82                                       uint32_t           offset,
83                                       uint32_t           length );
84
85uint64_t         tr_pieceOffset( const tr_torrent * tor,
86                                 tr_piece_index_t   index,
87                                 uint32_t           offset,
88                                 uint32_t           length );
89
90void             tr_torrentInitFilePriority( tr_torrent       * tor,
91                                             tr_file_index_t    fileIndex,
92                                             tr_priority_t      priority );
93
94int              tr_torrentCountUncheckedPieces( const tr_torrent * );
95
96tr_bool          tr_torrentIsFileChecked( const tr_torrent  * tor,
97                                          tr_file_index_t     file );
98
99void             tr_torrentSetPieceChecked( tr_torrent       * tor,
100                                            tr_piece_index_t   piece,
101                                            tr_bool            isChecked );
102
103void             tr_torrentSetFileChecked( tr_torrent       * tor,
104                                           tr_file_index_t    file,
105                                           tr_bool            isChecked );
106
107void             tr_torrentUncheck( tr_torrent * tor );
108
109int              tr_torrentPromoteTracker( tr_torrent   * tor,
110                                           int            trackerIndex );
111
112time_t*          tr_torrentGetMTimes( const tr_torrent  * tor,
113                                      size_t            * setmeCount );
114
115tr_torrent*      tr_torrentNext( tr_session  * session,
116                                 tr_torrent  * current );
117
118void             tr_torrentCheckSeedRatio( tr_torrent * tor );
119
120
121
122typedef enum
123{
124    TR_VERIFY_NONE,
125    TR_VERIFY_WAIT,
126    TR_VERIFY_NOW
127}
128tr_verify_state;
129
130void             tr_torrentSetVerifyState( tr_torrent      * tor,
131                                           tr_verify_state   state );
132
133struct tr_torrent
134{
135    tr_session *             session;
136    tr_info                  info;
137
138    struct tr_ratecontrol    swarmSpeed;
139
140    int                      magicNumber;
141
142    int                      error;
143    char                     errorString[128];
144
145    uint8_t                  obfuscatedHash[SHA_DIGEST_LENGTH];
146
147    /* If the initiator of the connection receives a handshake in which the
148     * peer_id does not match the expected peerid, then the initiator is
149     * expected to drop the connection. Note that the initiator presumably
150     * received the peer information from the tracker, which includes the
151     * peer_id that was registered by the peer. The peer_id from the tracker
152     * and in the handshake are expected to match.
153     */
154    uint8_t * peer_id;
155
156    /* Where to download */
157    char * downloadDir;
158
159    /* How many bytes we ask for per request */
160    uint32_t                   blockSize;
161    tr_block_index_t           blockCount;
162
163    uint32_t                   lastBlockSize;
164    uint32_t                   lastPieceSize;
165
166    uint32_t                   blockCountInPiece;
167    uint32_t                   blockCountInLastPiece;
168
169    struct tr_completion       completion;
170
171    struct tr_bitfield         checkedPieces;
172    tr_completeness            completeness;
173
174    struct tr_tracker *        tracker;
175    struct tr_publisher_tag *  trackerSubscription;
176
177    uint64_t                   downloadedCur;
178    uint64_t                   downloadedPrev;
179    uint64_t                   uploadedCur;
180    uint64_t                   uploadedPrev;
181    uint64_t                   corruptCur;
182    uint64_t                   corruptPrev;
183
184    time_t                     addedDate;
185    time_t                     activityDate;
186    time_t                     doneDate;
187    time_t                     startDate;
188    time_t                     anyDate;
189
190    tr_torrent_completeness_func *   completeness_func;
191    void *                     completeness_func_user_data;
192
193    tr_torrent_ratio_limit_hit_func * ratio_limit_hit_func;
194    void *                     ratio_limit_hit_func_user_data;
195
196    tr_bool                    isRunning;
197    tr_bool                    isDeleting;
198
199    uint16_t                   maxConnectedPeers;
200
201    tr_verify_state            verifyState;
202
203    time_t                     lastStatTime;
204    tr_stat                    stats;
205
206    tr_torrent *               next;
207
208    int                        uniqueId;
209
210    struct tr_bandwidth      * bandwidth;
211
212    struct tr_torrent_peers  * torrentPeers;
213
214    double                     desiredRatio;
215    tr_ratiolimit              ratioLimitMode;
216};
217
218/* get the index of this piece's first block */
219static TR_INLINE tr_block_index_t
220tr_torPieceFirstBlock( const tr_torrent * tor, const tr_piece_index_t piece )
221{
222    return piece * tor->blockCountInPiece;
223}
224
225/* what piece index is this block in? */
226static TR_INLINE tr_piece_index_t
227tr_torBlockPiece( const tr_torrent * tor, const tr_block_index_t block )
228{
229    return block / tor->blockCountInPiece;
230}
231
232/* how many blocks are in this piece? */
233static TR_INLINE uint32_t
234tr_torPieceCountBlocks( const tr_torrent * tor, const tr_piece_index_t piece )
235{
236    return piece == tor->info.pieceCount - 1 ? tor->blockCountInLastPiece
237                                             : tor->blockCountInPiece;
238}
239
240/* how many bytes are in this piece? */
241static TR_INLINE uint32_t
242tr_torPieceCountBytes( const tr_torrent * tor, const tr_piece_index_t piece )
243{
244    return piece == tor->info.pieceCount - 1 ? tor->lastPieceSize
245                                             : tor->info.pieceSize;
246}
247
248/* how many bytes are in this block? */
249static TR_INLINE uint32_t
250tr_torBlockCountBytes( const tr_torrent * tor, const tr_block_index_t block )
251{
252    return block == tor->blockCount - 1 ? tor->lastBlockSize
253                                        : tor->blockSize;
254}
255
256static TR_INLINE void tr_torrentLock( const tr_torrent * tor )
257{
258    tr_globalLock( tor->session );
259}
260
261static TR_INLINE void tr_torrentUnlock( const tr_torrent * tor )
262{
263    tr_globalUnlock( tor->session );
264}
265
266static TR_INLINE tr_bool
267tr_torrentExists( const tr_session * session, const uint8_t *   torrentHash )
268{
269    return tr_torrentFindFromHash( (tr_session*)session, torrentHash ) != NULL;
270}
271
272static TR_INLINE tr_bool
273tr_torrentIsSeed( const tr_torrent * tor )
274{
275    return tor->completeness != TR_LEECH;
276}
277
278static TR_INLINE tr_bool tr_torrentIsPrivate( const tr_torrent * tor )
279{
280    return ( tor != NULL ) && tor->info.isPrivate;
281}
282
283static TR_INLINE tr_bool tr_torrentAllowsPex( const tr_torrent * tor )
284{
285    return ( tor != NULL  ) && tor->session->isPexEnabled && !tr_torrentIsPrivate( tor );
286}
287
288static TR_INLINE tr_bool tr_torrentIsPieceChecked( const tr_torrent  * tor, tr_piece_index_t i )
289{
290    return tr_bitfieldHas( &tor->checkedPieces, i );
291}
292
293/***
294****
295***/
296
297enum
298{
299    TORRENT_MAGIC_NUMBER = 95549
300};
301
302static inline tr_bool tr_isTorrent( const tr_torrent * tor )
303{
304    return ( tor != NULL )
305        && ( tor->magicNumber == TORRENT_MAGIC_NUMBER )
306        && ( tr_isSession( tor->session ) );
307}
308
309#endif
Note: See TracBrowser for help on using the repository browser.