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