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:$ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <assert.h> |
---|
14 | #include <event.h> /* struct evbuffer */ |
---|
15 | |
---|
16 | #include "transmission.h" |
---|
17 | #include "bencode.h" |
---|
18 | #include "crypto.h" |
---|
19 | #include "magnet.h" |
---|
20 | #include "metainfo.h" |
---|
21 | #include "torrent.h" |
---|
22 | #include "torrent-magnet.h" |
---|
23 | #include "utils.h" |
---|
24 | #include "web.h" |
---|
25 | |
---|
26 | #define dbgmsg( tor, ... ) \ |
---|
27 | do { \ |
---|
28 | if( tr_deepLoggingIsActive( ) ) \ |
---|
29 | tr_deepLog( __FILE__, __LINE__, tor->info.name, __VA_ARGS__ ); \ |
---|
30 | } while( 0 ) |
---|
31 | |
---|
32 | /*** |
---|
33 | **** |
---|
34 | ***/ |
---|
35 | |
---|
36 | enum |
---|
37 | { |
---|
38 | /* don't ask for the same metadata piece more than this often */ |
---|
39 | MIN_REPEAT_INTERVAL_SECS = 3 |
---|
40 | }; |
---|
41 | |
---|
42 | struct metadata_node |
---|
43 | { |
---|
44 | time_t requestedAt; |
---|
45 | int piece; |
---|
46 | }; |
---|
47 | |
---|
48 | struct tr_incomplete_metadata |
---|
49 | { |
---|
50 | uint8_t * metadata; |
---|
51 | int metadata_size; |
---|
52 | int pieceCount; |
---|
53 | |
---|
54 | /** sorted from least to most recently requested */ |
---|
55 | struct metadata_node * piecesNeeded; |
---|
56 | int piecesNeededCount; |
---|
57 | }; |
---|
58 | |
---|
59 | static void |
---|
60 | incompleteMetadataFree( struct tr_incomplete_metadata * m ) |
---|
61 | { |
---|
62 | tr_free( m->metadata ); |
---|
63 | tr_free( m->piecesNeeded ); |
---|
64 | tr_free( m ); |
---|
65 | } |
---|
66 | |
---|
67 | void |
---|
68 | tr_torrentSetMetadataSizeHint( tr_torrent * tor, int size ) |
---|
69 | { |
---|
70 | if( !tr_torrentHasMetadata( tor ) ) |
---|
71 | { |
---|
72 | if( tor->incompleteMetadata == NULL ) |
---|
73 | { |
---|
74 | int i; |
---|
75 | struct tr_incomplete_metadata * m; |
---|
76 | int n = ( size + ( METADATA_PIECE_SIZE - 1 ) ) / METADATA_PIECE_SIZE; |
---|
77 | dbgmsg( tor, "there are %d pieces", n ); |
---|
78 | |
---|
79 | m = tr_new( struct tr_incomplete_metadata, 1 ); |
---|
80 | m->pieceCount = n; |
---|
81 | m->metadata = tr_new( uint8_t, size ); |
---|
82 | m->metadata_size = size; |
---|
83 | m->piecesNeededCount = n; |
---|
84 | m->piecesNeeded = tr_new( struct metadata_node, n ); |
---|
85 | |
---|
86 | for( i=0; i<n; ++i ) { |
---|
87 | m->piecesNeeded[i].piece = i; |
---|
88 | m->piecesNeeded[i].requestedAt = 0; |
---|
89 | } |
---|
90 | |
---|
91 | tor->incompleteMetadata = m; |
---|
92 | } |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | void* |
---|
97 | tr_torrentGetMetadataPiece( const tr_torrent * tor, int piece, int * len ) |
---|
98 | { |
---|
99 | char * ret = NULL; |
---|
100 | |
---|
101 | assert( tr_isTorrent( tor ) ); |
---|
102 | assert( piece >= 0 ); |
---|
103 | assert( len != NULL ); |
---|
104 | |
---|
105 | if( tor->infoDictLength > 0 ) |
---|
106 | { |
---|
107 | FILE * fp = fopen( tor->info.torrent, "rb" ); |
---|
108 | if( fp != NULL ) |
---|
109 | { |
---|
110 | const int offset = piece * METADATA_PIECE_SIZE; |
---|
111 | |
---|
112 | if( !fseek( fp, tor->infoDictOffset + offset, SEEK_SET ) ) |
---|
113 | { |
---|
114 | const int l = offset + METADATA_PIECE_SIZE <= tor->infoDictLength |
---|
115 | ? METADATA_PIECE_SIZE |
---|
116 | : tor->infoDictLength - offset; |
---|
117 | char * buf = tr_new( char, l ); |
---|
118 | const int n = fread( buf, 1, l, fp ); |
---|
119 | if( n != l ) |
---|
120 | { |
---|
121 | *len = l; |
---|
122 | ret = buf; |
---|
123 | buf = NULL; |
---|
124 | } |
---|
125 | |
---|
126 | tr_free( buf ); |
---|
127 | } |
---|
128 | |
---|
129 | fclose( fp ); |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | return ret; |
---|
134 | } |
---|
135 | |
---|
136 | void |
---|
137 | tr_torrentSetMetadataPiece( tr_torrent * tor, |
---|
138 | int piece, |
---|
139 | const void * data, |
---|
140 | int len ) |
---|
141 | { |
---|
142 | int i; |
---|
143 | struct tr_incomplete_metadata * m; |
---|
144 | const int offset = piece * METADATA_PIECE_SIZE; |
---|
145 | |
---|
146 | assert( tr_isTorrent( tor ) ); |
---|
147 | |
---|
148 | dbgmsg( tor, "got metadata piece %d", piece ); |
---|
149 | |
---|
150 | /* are we set up to download metadata? */ |
---|
151 | m = tor->incompleteMetadata; |
---|
152 | if( m == NULL ) |
---|
153 | return; |
---|
154 | |
---|
155 | /* does this data pass the smell test? */ |
---|
156 | if( offset + len > m->metadata_size ) |
---|
157 | return; |
---|
158 | |
---|
159 | /* do we need this piece? */ |
---|
160 | for( i=0; i<m->piecesNeededCount; ++i ) |
---|
161 | if( m->piecesNeeded[i].piece == piece ) |
---|
162 | break; |
---|
163 | if( i==m->piecesNeededCount ) |
---|
164 | return; |
---|
165 | |
---|
166 | memcpy( m->metadata + offset, data, len ); |
---|
167 | |
---|
168 | tr_removeElementFromArray( m->piecesNeeded, i, |
---|
169 | sizeof( struct metadata_node ), |
---|
170 | m->piecesNeededCount-- ); |
---|
171 | |
---|
172 | dbgmsg( tor, "saving metainfo piece %d... %d remain", piece, m->piecesNeededCount ); |
---|
173 | |
---|
174 | /* are we done? */ |
---|
175 | if( m->piecesNeededCount == 0 ) |
---|
176 | { |
---|
177 | tr_bool success = FALSE; |
---|
178 | uint8_t sha1[SHA_DIGEST_LENGTH]; |
---|
179 | dbgmsg( tor, "metainfo piece %d was the last one", piece ); |
---|
180 | tr_sha1( sha1, m->metadata, m->metadata_size, NULL ); |
---|
181 | if( !memcmp( sha1, tor->info.hash, SHA_DIGEST_LENGTH ) ) |
---|
182 | { |
---|
183 | int err; |
---|
184 | tr_benc dict; |
---|
185 | struct evbuffer * buf = evbuffer_new( ); |
---|
186 | dbgmsg( tor, "metadata checksum passed! (length: %d)", (int)m->metadata_size ); |
---|
187 | |
---|
188 | /* add a wrapper dictionary to the benc. |
---|
189 | * include the announce-list too, |
---|
190 | * so we can save it in the .torrent for future sessions */ |
---|
191 | evbuffer_add_printf( buf, "d" ); |
---|
192 | evbuffer_add_printf( buf, "13:announce-list" ); |
---|
193 | evbuffer_add_printf( buf, "l" ); |
---|
194 | for( i=0; i<tor->info.trackerCount; ++i ) { |
---|
195 | const char * url = tor->info.trackers[i].announce; |
---|
196 | evbuffer_add_printf( buf, "l%zu:%se", strlen( url ), url ); |
---|
197 | } |
---|
198 | evbuffer_add_printf( buf, "e" ); |
---|
199 | evbuffer_add_printf( buf, "4:info" ); |
---|
200 | evbuffer_add( buf, m->metadata, m->metadata_size ); |
---|
201 | evbuffer_add_printf( buf, "e" ); |
---|
202 | |
---|
203 | /* does it parse? */ |
---|
204 | err = tr_bencLoad( EVBUFFER_DATA( buf ), EVBUFFER_LENGTH( buf ), &dict, NULL ); |
---|
205 | dbgmsg( tor, "err is %d", err ); |
---|
206 | if( !err ) |
---|
207 | { |
---|
208 | if( tr_metainfoParse( tor->session, |
---|
209 | &tor->info, |
---|
210 | &tor->infoDictOffset, |
---|
211 | &tor->infoDictLength, |
---|
212 | &dict ) ) |
---|
213 | { |
---|
214 | const char * path = tor->info.torrent; |
---|
215 | dbgmsg( tor, "saving completed metadata to \"%s\"", path ); |
---|
216 | |
---|
217 | success = TRUE; |
---|
218 | tr_torrentGotNewInfoDict( tor ); |
---|
219 | |
---|
220 | tr_bencToFile( &dict, TR_FMT_BENC, path ); |
---|
221 | tr_sessionSetTorrentFile( tor->session, |
---|
222 | tor->info.hashString, path ); |
---|
223 | tr_torrentSetDirty( tor ); |
---|
224 | } |
---|
225 | |
---|
226 | tr_bencFree( &dict ); |
---|
227 | } |
---|
228 | |
---|
229 | evbuffer_free( buf ); |
---|
230 | } |
---|
231 | |
---|
232 | if( success ) |
---|
233 | { |
---|
234 | incompleteMetadataFree( tor->incompleteMetadata ); |
---|
235 | tor->incompleteMetadata = NULL; |
---|
236 | } |
---|
237 | else /* drat. */ |
---|
238 | { |
---|
239 | const int n = m->pieceCount; |
---|
240 | for( i=0; i<n; ++i ) { |
---|
241 | m->piecesNeeded[i].piece = i; |
---|
242 | m->piecesNeeded[i].requestedAt = 0; |
---|
243 | } |
---|
244 | m->piecesNeededCount = n; |
---|
245 | dbgmsg( tor, "metadata error; trying again. %d pieces left", n ); |
---|
246 | } |
---|
247 | } |
---|
248 | } |
---|
249 | |
---|
250 | tr_bool |
---|
251 | tr_torrentGetNextMetadataRequest( tr_torrent * tor, time_t now, int * setme_piece ) |
---|
252 | { |
---|
253 | tr_bool have_request = FALSE; |
---|
254 | struct tr_incomplete_metadata * m; |
---|
255 | |
---|
256 | assert( tr_isTorrent( tor ) ); |
---|
257 | |
---|
258 | m = tor->incompleteMetadata; |
---|
259 | |
---|
260 | if( ( m != NULL ) |
---|
261 | && ( m->piecesNeededCount > 0 ) |
---|
262 | && ( m->piecesNeeded[0].requestedAt + MIN_REPEAT_INTERVAL_SECS < now ) ) |
---|
263 | { |
---|
264 | int i; |
---|
265 | const int piece = m->piecesNeeded[0].piece; |
---|
266 | |
---|
267 | tr_removeElementFromArray( m->piecesNeeded, 0, |
---|
268 | sizeof( struct metadata_node ), |
---|
269 | m->piecesNeededCount-- ); |
---|
270 | |
---|
271 | i = m->piecesNeededCount++; |
---|
272 | m->piecesNeeded[i].piece = piece; |
---|
273 | m->piecesNeeded[i].requestedAt = now; |
---|
274 | |
---|
275 | dbgmsg( tor, "next piece to request: %d", piece ); |
---|
276 | *setme_piece = piece; |
---|
277 | have_request = TRUE; |
---|
278 | } |
---|
279 | |
---|
280 | return have_request; |
---|
281 | } |
---|
282 | |
---|
283 | float |
---|
284 | tr_torrentGetMetadataPercent( const tr_torrent * tor ) |
---|
285 | { |
---|
286 | float ret; |
---|
287 | |
---|
288 | if( tr_torrentHasMetadata( tor ) ) |
---|
289 | ret = 1.0; |
---|
290 | else { |
---|
291 | const struct tr_incomplete_metadata * m = tor->incompleteMetadata; |
---|
292 | if( m == NULL ) |
---|
293 | ret = 0.0; |
---|
294 | else |
---|
295 | ret = (m->pieceCount - m->piecesNeededCount) / (float)m->pieceCount; |
---|
296 | } |
---|
297 | |
---|
298 | return ret; |
---|
299 | } |
---|
300 | |
---|
301 | char* |
---|
302 | tr_torrentGetMagnetLink( const tr_torrent * tor ) |
---|
303 | { |
---|
304 | int i; |
---|
305 | char * ret; |
---|
306 | struct evbuffer * s; |
---|
307 | |
---|
308 | assert( tr_isTorrent( tor ) ); |
---|
309 | |
---|
310 | s = evbuffer_new( ); |
---|
311 | evbuffer_add_printf( s, "magnet:?xt=urn:btih:%s", tor->info.hashString ); |
---|
312 | evbuffer_add_printf( s, "%s", "&dn=" ); |
---|
313 | tr_http_escape( s, tr_torrentName( tor ), -1, TRUE ); |
---|
314 | for( i=0; i<tor->info.trackerCount; ++i ) |
---|
315 | { |
---|
316 | evbuffer_add_printf( s, "%s", "&tr=" ); |
---|
317 | tr_http_escape( s, tor->info.trackers[i].announce, -1, TRUE ); |
---|
318 | } |
---|
319 | |
---|
320 | ret = tr_strndup( EVBUFFER_DATA( s ), EVBUFFER_LENGTH( s ) ); |
---|
321 | evbuffer_free( s ); |
---|
322 | return ret; |
---|
323 | } |
---|