1 | /* |
---|
2 | * This file Copyright (C) 2012-2014 Mnemosyne LLC |
---|
3 | * |
---|
4 | * It may be used under the GNU GPL versions 2 or 3 |
---|
5 | * or any future license endorsed by Mnemosyne LLC. |
---|
6 | * |
---|
7 | * $Id: torrent-magnet.c 14525 2015-05-09 08:37:55Z mikedld $ |
---|
8 | */ |
---|
9 | |
---|
10 | #include <assert.h> |
---|
11 | #include <string.h> /* memcpy (), memset (), memcmp () */ |
---|
12 | |
---|
13 | #include <event2/buffer.h> |
---|
14 | |
---|
15 | #include "transmission.h" |
---|
16 | #include "crypto-utils.h" /* tr_sha1 () */ |
---|
17 | #include "file.h" |
---|
18 | #include "log.h" |
---|
19 | #include "magnet.h" |
---|
20 | #include "metainfo.h" |
---|
21 | #include "resume.h" |
---|
22 | #include "torrent.h" |
---|
23 | #include "torrent-magnet.h" |
---|
24 | #include "utils.h" |
---|
25 | #include "variant.h" |
---|
26 | #include "web.h" |
---|
27 | |
---|
28 | #define dbgmsg(tor, ...) \ |
---|
29 | do \ |
---|
30 | { \ |
---|
31 | if (tr_logGetDeepEnabled ()) \ |
---|
32 | tr_logAddDeep (__FILE__, __LINE__, tr_torrentName (tor), __VA_ARGS__); \ |
---|
33 | } \ |
---|
34 | while (0) |
---|
35 | |
---|
36 | /*** |
---|
37 | **** |
---|
38 | ***/ |
---|
39 | |
---|
40 | enum |
---|
41 | { |
---|
42 | /* don't ask for the same metadata piece more than this often */ |
---|
43 | MIN_REPEAT_INTERVAL_SECS = 3 |
---|
44 | }; |
---|
45 | |
---|
46 | struct metadata_node |
---|
47 | { |
---|
48 | time_t requestedAt; |
---|
49 | int piece; |
---|
50 | }; |
---|
51 | |
---|
52 | struct tr_incomplete_metadata |
---|
53 | { |
---|
54 | uint8_t * metadata; |
---|
55 | int metadata_size; |
---|
56 | int pieceCount; |
---|
57 | |
---|
58 | /** sorted from least to most recently requested */ |
---|
59 | struct metadata_node * piecesNeeded; |
---|
60 | int piecesNeededCount; |
---|
61 | }; |
---|
62 | |
---|
63 | static void |
---|
64 | incompleteMetadataFree (struct tr_incomplete_metadata * m) |
---|
65 | { |
---|
66 | tr_free (m->metadata); |
---|
67 | tr_free (m->piecesNeeded); |
---|
68 | tr_free (m); |
---|
69 | } |
---|
70 | |
---|
71 | void |
---|
72 | tr_torrentSetMetadataSizeHint (tr_torrent * tor, int size) |
---|
73 | { |
---|
74 | if (!tr_torrentHasMetadata (tor)) |
---|
75 | { |
---|
76 | if (tor->incompleteMetadata == NULL) |
---|
77 | { |
---|
78 | int i; |
---|
79 | struct tr_incomplete_metadata * m; |
---|
80 | const int n = (size + (METADATA_PIECE_SIZE - 1)) / METADATA_PIECE_SIZE; |
---|
81 | dbgmsg (tor, "metadata is %d bytes in %d pieces", size, n); |
---|
82 | |
---|
83 | m = tr_new (struct tr_incomplete_metadata, 1); |
---|
84 | m->pieceCount = n; |
---|
85 | m->metadata = tr_new (uint8_t, size); |
---|
86 | m->metadata_size = size; |
---|
87 | m->piecesNeededCount = n; |
---|
88 | m->piecesNeeded = tr_new (struct metadata_node, n); |
---|
89 | |
---|
90 | for (i=0; i<n; ++i) |
---|
91 | { |
---|
92 | m->piecesNeeded[i].piece = i; |
---|
93 | m->piecesNeeded[i].requestedAt = 0; |
---|
94 | } |
---|
95 | |
---|
96 | tor->incompleteMetadata = m; |
---|
97 | } |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | static int |
---|
102 | findInfoDictOffset (const tr_torrent * tor) |
---|
103 | { |
---|
104 | size_t fileLen; |
---|
105 | uint8_t * fileContents; |
---|
106 | int offset = 0; |
---|
107 | |
---|
108 | /* load the file, and find the info dict's offset inside the file */ |
---|
109 | if ((fileContents = tr_loadFile (tor->info.torrent, &fileLen, NULL))) |
---|
110 | { |
---|
111 | tr_variant top; |
---|
112 | |
---|
113 | if (!tr_variantFromBenc (&top, fileContents, fileLen)) |
---|
114 | { |
---|
115 | tr_variant * infoDict; |
---|
116 | |
---|
117 | if (tr_variantDictFindDict (&top, TR_KEY_info, &infoDict)) |
---|
118 | { |
---|
119 | int infoLen; |
---|
120 | char * infoContents = tr_variantToStr (infoDict, TR_VARIANT_FMT_BENC, &infoLen); |
---|
121 | const uint8_t * i = (const uint8_t*) tr_memmem ((char*)fileContents, fileLen, infoContents, infoLen); |
---|
122 | offset = i != NULL ? i - fileContents : 0; |
---|
123 | tr_free (infoContents); |
---|
124 | } |
---|
125 | |
---|
126 | tr_variantFree (&top); |
---|
127 | } |
---|
128 | |
---|
129 | tr_free (fileContents); |
---|
130 | } |
---|
131 | |
---|
132 | return offset; |
---|
133 | } |
---|
134 | |
---|
135 | static void |
---|
136 | ensureInfoDictOffsetIsCached (tr_torrent * tor) |
---|
137 | { |
---|
138 | assert (tr_torrentHasMetadata (tor)); |
---|
139 | |
---|
140 | if (!tor->infoDictOffsetIsCached) |
---|
141 | { |
---|
142 | tor->infoDictOffset = findInfoDictOffset (tor); |
---|
143 | tor->infoDictOffsetIsCached = true; |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | void* |
---|
148 | tr_torrentGetMetadataPiece (tr_torrent * tor, int piece, int * len) |
---|
149 | { |
---|
150 | char * ret = NULL; |
---|
151 | |
---|
152 | assert (tr_isTorrent (tor)); |
---|
153 | assert (piece >= 0); |
---|
154 | assert (len != NULL); |
---|
155 | |
---|
156 | if (tr_torrentHasMetadata (tor)) |
---|
157 | { |
---|
158 | tr_sys_file_t fd; |
---|
159 | |
---|
160 | ensureInfoDictOffsetIsCached (tor); |
---|
161 | |
---|
162 | assert (tor->infoDictLength > 0); |
---|
163 | assert (tor->infoDictOffset >= 0); |
---|
164 | |
---|
165 | fd = tr_sys_file_open (tor->info.torrent, TR_SYS_FILE_READ, 0, NULL); |
---|
166 | if (fd != TR_BAD_SYS_FILE) |
---|
167 | { |
---|
168 | const int o = piece * METADATA_PIECE_SIZE; |
---|
169 | |
---|
170 | if (tr_sys_file_seek (fd, tor->infoDictOffset + o, TR_SEEK_SET, NULL, NULL)) |
---|
171 | { |
---|
172 | const int l = o + METADATA_PIECE_SIZE <= tor->infoDictLength |
---|
173 | ? METADATA_PIECE_SIZE |
---|
174 | : tor->infoDictLength - o; |
---|
175 | |
---|
176 | if (0<l && l<=METADATA_PIECE_SIZE) |
---|
177 | { |
---|
178 | char * buf = tr_new (char, l); |
---|
179 | uint64_t n; |
---|
180 | if (tr_sys_file_read (fd, buf, l, &n, NULL) && n == (unsigned int) l) |
---|
181 | { |
---|
182 | *len = l; |
---|
183 | ret = buf; |
---|
184 | buf = NULL; |
---|
185 | } |
---|
186 | |
---|
187 | tr_free (buf); |
---|
188 | } |
---|
189 | } |
---|
190 | |
---|
191 | tr_sys_file_close (fd, NULL); |
---|
192 | } |
---|
193 | } |
---|
194 | |
---|
195 | assert (ret == NULL || *len > 0); |
---|
196 | |
---|
197 | return ret; |
---|
198 | } |
---|
199 | |
---|
200 | void |
---|
201 | tr_torrentSetMetadataPiece (tr_torrent * tor, int piece, const void * data, int len) |
---|
202 | { |
---|
203 | int i; |
---|
204 | struct tr_incomplete_metadata * m; |
---|
205 | const int offset = piece * METADATA_PIECE_SIZE; |
---|
206 | |
---|
207 | assert (tr_isTorrent (tor)); |
---|
208 | |
---|
209 | dbgmsg (tor, "got metadata piece %d", piece); |
---|
210 | |
---|
211 | /* are we set up to download metadata? */ |
---|
212 | m = tor->incompleteMetadata; |
---|
213 | if (m == NULL) |
---|
214 | return; |
---|
215 | |
---|
216 | /* does this data pass the smell test? */ |
---|
217 | if (offset + len > m->metadata_size) |
---|
218 | return; |
---|
219 | |
---|
220 | /* do we need this piece? */ |
---|
221 | for (i=0; i<m->piecesNeededCount; ++i) |
---|
222 | if (m->piecesNeeded[i].piece == piece) |
---|
223 | break; |
---|
224 | if (i==m->piecesNeededCount) |
---|
225 | return; |
---|
226 | |
---|
227 | memcpy (m->metadata + offset, data, len); |
---|
228 | |
---|
229 | tr_removeElementFromArray (m->piecesNeeded, i, |
---|
230 | sizeof (struct metadata_node), |
---|
231 | m->piecesNeededCount--); |
---|
232 | |
---|
233 | dbgmsg (tor, "saving metainfo piece %d... %d remain", piece, m->piecesNeededCount); |
---|
234 | |
---|
235 | /* are we done? */ |
---|
236 | if (m->piecesNeededCount == 0) |
---|
237 | { |
---|
238 | bool success = false; |
---|
239 | bool checksumPassed = false; |
---|
240 | bool metainfoParsed = false; |
---|
241 | uint8_t sha1[SHA_DIGEST_LENGTH]; |
---|
242 | |
---|
243 | /* we've got a complete set of metainfo... see if it passes the checksum test */ |
---|
244 | dbgmsg (tor, "metainfo piece %d was the last one", piece); |
---|
245 | tr_sha1 (sha1, m->metadata, m->metadata_size, NULL); |
---|
246 | if ((checksumPassed = !memcmp (sha1, tor->info.hash, SHA_DIGEST_LENGTH))) |
---|
247 | { |
---|
248 | /* checksum passed; now try to parse it as benc */ |
---|
249 | tr_variant infoDict; |
---|
250 | const int err = tr_variantFromBenc (&infoDict, m->metadata, m->metadata_size); |
---|
251 | dbgmsg (tor, "err is %d", err); |
---|
252 | if ((metainfoParsed = !err)) |
---|
253 | { |
---|
254 | /* yay we have bencoded metainfo... merge it into our .torrent file */ |
---|
255 | tr_variant newMetainfo; |
---|
256 | char * path = tr_strdup (tor->info.torrent); |
---|
257 | |
---|
258 | if (tr_variantFromFile (&newMetainfo, TR_VARIANT_FMT_BENC, path, NULL)) |
---|
259 | { |
---|
260 | bool hasInfo; |
---|
261 | tr_info info; |
---|
262 | int infoDictLength; |
---|
263 | |
---|
264 | /* remove any old .torrent and .resume files */ |
---|
265 | tr_sys_path_remove (path, NULL); |
---|
266 | tr_torrentRemoveResume (tor); |
---|
267 | |
---|
268 | dbgmsg (tor, "Saving completed metadata to \"%s\"", path); |
---|
269 | tr_variantMergeDicts (tr_variantDictAddDict (&newMetainfo, TR_KEY_info, 0), &infoDict); |
---|
270 | |
---|
271 | memset (&info, 0, sizeof (tr_info)); |
---|
272 | success = tr_metainfoParse (tor->session, &newMetainfo, &info, &hasInfo, &infoDictLength); |
---|
273 | |
---|
274 | if (success && !tr_getBlockSize (info.pieceSize)) |
---|
275 | { |
---|
276 | tr_torrentSetLocalError (tor, "%s", _("Magnet torrent's metadata is not usable")); |
---|
277 | success = false; |
---|
278 | } |
---|
279 | |
---|
280 | if (success) |
---|
281 | { |
---|
282 | /* keep the new info */ |
---|
283 | tor->info = info; |
---|
284 | tor->infoDictLength = infoDictLength; |
---|
285 | |
---|
286 | /* save the new .torrent file */ |
---|
287 | tr_variantToFile (&newMetainfo, TR_VARIANT_FMT_BENC, tor->info.torrent); |
---|
288 | tr_sessionSetTorrentFile (tor->session, tor->info.hashString, tor->info.torrent); |
---|
289 | tr_torrentGotNewInfoDict (tor); |
---|
290 | tr_torrentSetDirty (tor); |
---|
291 | } |
---|
292 | |
---|
293 | tr_variantFree (&newMetainfo); |
---|
294 | } |
---|
295 | |
---|
296 | tr_variantFree (&infoDict); |
---|
297 | tr_free (path); |
---|
298 | } |
---|
299 | } |
---|
300 | |
---|
301 | if (success) |
---|
302 | { |
---|
303 | incompleteMetadataFree (tor->incompleteMetadata); |
---|
304 | tor->incompleteMetadata = NULL; |
---|
305 | tor->isStopping = true; |
---|
306 | tor->magnetVerify = true; |
---|
307 | tor->startAfterVerify = true; |
---|
308 | } |
---|
309 | else /* drat. */ |
---|
310 | { |
---|
311 | const int n = m->pieceCount; |
---|
312 | for (i=0; i<n; ++i) |
---|
313 | { |
---|
314 | m->piecesNeeded[i].piece = i; |
---|
315 | m->piecesNeeded[i].requestedAt = 0; |
---|
316 | } |
---|
317 | m->piecesNeededCount = n; |
---|
318 | dbgmsg (tor, "metadata error; trying again. %d pieces left", n); |
---|
319 | |
---|
320 | tr_logAddError ("magnet status: checksum passed %d, metainfo parsed %d", |
---|
321 | (int)checksumPassed, (int)metainfoParsed); |
---|
322 | } |
---|
323 | } |
---|
324 | } |
---|
325 | |
---|
326 | bool |
---|
327 | tr_torrentGetNextMetadataRequest (tr_torrent * tor, time_t now, int * setme_piece) |
---|
328 | { |
---|
329 | bool have_request = false; |
---|
330 | struct tr_incomplete_metadata * m; |
---|
331 | |
---|
332 | assert (tr_isTorrent (tor)); |
---|
333 | |
---|
334 | m = tor->incompleteMetadata; |
---|
335 | |
---|
336 | if ((m != NULL) && (m->piecesNeededCount > 0) |
---|
337 | && (m->piecesNeeded[0].requestedAt + MIN_REPEAT_INTERVAL_SECS < now)) |
---|
338 | { |
---|
339 | int i; |
---|
340 | const int piece = m->piecesNeeded[0].piece; |
---|
341 | |
---|
342 | tr_removeElementFromArray (m->piecesNeeded, 0, |
---|
343 | sizeof (struct metadata_node), |
---|
344 | m->piecesNeededCount--); |
---|
345 | |
---|
346 | i = m->piecesNeededCount++; |
---|
347 | m->piecesNeeded[i].piece = piece; |
---|
348 | m->piecesNeeded[i].requestedAt = now; |
---|
349 | |
---|
350 | dbgmsg (tor, "next piece to request: %d", piece); |
---|
351 | *setme_piece = piece; |
---|
352 | have_request = true; |
---|
353 | } |
---|
354 | |
---|
355 | return have_request; |
---|
356 | } |
---|
357 | |
---|
358 | double |
---|
359 | tr_torrentGetMetadataPercent (const tr_torrent * tor) |
---|
360 | { |
---|
361 | double ret; |
---|
362 | |
---|
363 | if (tr_torrentHasMetadata (tor)) |
---|
364 | { |
---|
365 | ret = 1.0; |
---|
366 | } |
---|
367 | else |
---|
368 | { |
---|
369 | const struct tr_incomplete_metadata * m = tor->incompleteMetadata; |
---|
370 | |
---|
371 | if (!m || !m->pieceCount) |
---|
372 | ret = 0.0; |
---|
373 | else |
---|
374 | ret = (m->pieceCount - m->piecesNeededCount) / (double)m->pieceCount; |
---|
375 | } |
---|
376 | |
---|
377 | return ret; |
---|
378 | } |
---|
379 | |
---|
380 | /* FIXME: this should be renamed tr_metainfoGetMagnetLink() and moved to metainfo.c for consistency */ |
---|
381 | char * |
---|
382 | tr_torrentInfoGetMagnetLink (const tr_info * inf) |
---|
383 | { |
---|
384 | unsigned int i; |
---|
385 | const char * name; |
---|
386 | struct evbuffer * s = evbuffer_new (); |
---|
387 | |
---|
388 | evbuffer_add_printf (s, "magnet:?xt=urn:btih:%s", inf->hashString); |
---|
389 | |
---|
390 | name = inf->name; |
---|
391 | if (name && *name) |
---|
392 | { |
---|
393 | evbuffer_add_printf (s, "%s", "&dn="); |
---|
394 | tr_http_escape (s, name, -1, true); |
---|
395 | } |
---|
396 | |
---|
397 | for (i=0; i<inf->trackerCount; ++i) |
---|
398 | { |
---|
399 | evbuffer_add_printf (s, "%s", "&tr="); |
---|
400 | tr_http_escape (s, inf->trackers[i].announce, -1, true); |
---|
401 | } |
---|
402 | |
---|
403 | for (i=0; i<inf->webseedCount; i++) |
---|
404 | { |
---|
405 | evbuffer_add_printf (s, "%s", "&ws="); |
---|
406 | tr_http_escape (s, inf->webseeds[i], -1, true); |
---|
407 | } |
---|
408 | |
---|
409 | return evbuffer_free_to_str (s); |
---|
410 | } |
---|