1 | /****************************************************************************** |
---|
2 | * $Id: metainfo.c 3473 2007-10-20 15:17:36Z charles $ |
---|
3 | * |
---|
4 | * Copyright (c) 2005-2007 Transmission authors and contributors |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
7 | * copy of this software and associated documentation files (the "Software"), |
---|
8 | * to deal in the Software without restriction, including without limitation |
---|
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
10 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
11 | * Software is furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
22 | * DEALINGS IN THE SOFTWARE. |
---|
23 | *****************************************************************************/ |
---|
24 | |
---|
25 | #include <assert.h> |
---|
26 | #include <errno.h> |
---|
27 | #include <stdio.h> |
---|
28 | #include <stdlib.h> |
---|
29 | |
---|
30 | #include <sys/types.h> |
---|
31 | #include <sys/stat.h> |
---|
32 | #include <unistd.h> /* unlink, stat */ |
---|
33 | |
---|
34 | #include "transmission.h" |
---|
35 | #include "bencode.h" |
---|
36 | #include "crypto.h" /* tr_sha1 */ |
---|
37 | #include "http.h" /* tr_httpParseUrl */ |
---|
38 | #include "metainfo.h" |
---|
39 | #include "platform.h" |
---|
40 | #include "utils.h" |
---|
41 | |
---|
42 | /*********************************************************************** |
---|
43 | * Local prototypes |
---|
44 | **********************************************************************/ |
---|
45 | static int realparse( tr_info * inf, const uint8_t * buf, size_t len ); |
---|
46 | static void savedname( char * name, size_t len, const char * hash, |
---|
47 | const char * tag ); |
---|
48 | static uint8_t * readtorrent( const char * path, size_t * len ); |
---|
49 | static int savetorrent( const char * hash, const char * tag, |
---|
50 | const uint8_t * buf, size_t buflen ); |
---|
51 | static int getfile( char * buf, int size, |
---|
52 | const char * prefix, benc_val_t * name ); |
---|
53 | static int getannounce( tr_info * inf, benc_val_t * meta ); |
---|
54 | static char * announceToScrape( const char * announce ); |
---|
55 | static int parseFiles( tr_info * inf, benc_val_t * name, |
---|
56 | benc_val_t * files, benc_val_t * length ); |
---|
57 | |
---|
58 | /*** |
---|
59 | **** |
---|
60 | ***/ |
---|
61 | |
---|
62 | #define WANTBYTES( want, got ) \ |
---|
63 | if( (want) > (got) ) { return; } else { (got) -= (want); } |
---|
64 | static void |
---|
65 | strlcat_utf8( void * dest, const void * src, size_t len, char skip ) |
---|
66 | { |
---|
67 | char * s = dest; |
---|
68 | const char * append = src; |
---|
69 | const char * p; |
---|
70 | |
---|
71 | /* don't overwrite the nul at the end */ |
---|
72 | len--; |
---|
73 | |
---|
74 | /* Go to the end of the destination string */ |
---|
75 | while( s[0] ) |
---|
76 | { |
---|
77 | s++; |
---|
78 | len--; |
---|
79 | } |
---|
80 | |
---|
81 | /* Now start appending, converting on the fly if necessary */ |
---|
82 | for( p = append; p[0]; ) |
---|
83 | { |
---|
84 | /* skip over the requested character */ |
---|
85 | if( skip == p[0] ) |
---|
86 | { |
---|
87 | p++; |
---|
88 | continue; |
---|
89 | } |
---|
90 | |
---|
91 | if( !( p[0] & 0x80 ) ) |
---|
92 | { |
---|
93 | /* ASCII character */ |
---|
94 | WANTBYTES( 1, len ); |
---|
95 | *(s++) = *(p++); |
---|
96 | continue; |
---|
97 | } |
---|
98 | |
---|
99 | if( ( p[0] & 0xE0 ) == 0xC0 && ( p[1] & 0xC0 ) == 0x80 ) |
---|
100 | { |
---|
101 | /* 2-bytes UTF-8 character */ |
---|
102 | WANTBYTES( 2, len ); |
---|
103 | *(s++) = *(p++); *(s++) = *(p++); |
---|
104 | continue; |
---|
105 | } |
---|
106 | |
---|
107 | if( ( p[0] & 0xF0 ) == 0xE0 && ( p[1] & 0xC0 ) == 0x80 && |
---|
108 | ( p[2] & 0xC0 ) == 0x80 ) |
---|
109 | { |
---|
110 | /* 3-bytes UTF-8 character */ |
---|
111 | WANTBYTES( 3, len ); |
---|
112 | *(s++) = *(p++); *(s++) = *(p++); |
---|
113 | *(s++) = *(p++); |
---|
114 | continue; |
---|
115 | } |
---|
116 | |
---|
117 | if( ( p[0] & 0xF8 ) == 0xF0 && ( p[1] & 0xC0 ) == 0x80 && |
---|
118 | ( p[2] & 0xC0 ) == 0x80 && ( p[3] & 0xC0 ) == 0x80 ) |
---|
119 | { |
---|
120 | /* 4-bytes UTF-8 character */ |
---|
121 | WANTBYTES( 4, len ); |
---|
122 | *(s++) = *(p++); *(s++) = *(p++); |
---|
123 | *(s++) = *(p++); *(s++) = *(p++); |
---|
124 | continue; |
---|
125 | } |
---|
126 | |
---|
127 | /* ISO 8859-1 -> UTF-8 conversion */ |
---|
128 | WANTBYTES( 2, len ); |
---|
129 | *(s++) = 0xC0 | ( ( *p & 0xFF ) >> 6 ); |
---|
130 | *(s++) = 0x80 | ( *(p++) & 0x3F ); |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | /*********************************************************************** |
---|
135 | * tr_metainfoParse |
---|
136 | *********************************************************************** |
---|
137 | * |
---|
138 | **********************************************************************/ |
---|
139 | int |
---|
140 | tr_metainfoParseFile( tr_info * inf, const char * tag, |
---|
141 | const char * path, int save ) |
---|
142 | { |
---|
143 | uint8_t * buf; |
---|
144 | size_t size; |
---|
145 | |
---|
146 | /* read the torrent data */ |
---|
147 | buf = readtorrent( path, &size ); |
---|
148 | if( NULL == buf ) |
---|
149 | { |
---|
150 | return TR_EINVALID; |
---|
151 | } |
---|
152 | |
---|
153 | if( realparse( inf, buf, size ) ) |
---|
154 | { |
---|
155 | free( buf ); |
---|
156 | return TR_EINVALID; |
---|
157 | } |
---|
158 | |
---|
159 | if( save ) |
---|
160 | { |
---|
161 | if( savetorrent( inf->hashString, tag, buf, size ) ) |
---|
162 | { |
---|
163 | free( buf ); |
---|
164 | return TR_EINVALID; |
---|
165 | } |
---|
166 | savedname( inf->torrent, sizeof inf->torrent, inf->hashString, tag ); |
---|
167 | } |
---|
168 | else |
---|
169 | { |
---|
170 | snprintf( inf->torrent, sizeof inf->torrent, "%s", path ); |
---|
171 | } |
---|
172 | |
---|
173 | free( buf ); |
---|
174 | |
---|
175 | return TR_OK; |
---|
176 | } |
---|
177 | |
---|
178 | int |
---|
179 | tr_metainfoParseData( tr_info * inf, const char * tag, |
---|
180 | const uint8_t * data, size_t size, int save ) |
---|
181 | { |
---|
182 | if( realparse( inf, data, size ) ) |
---|
183 | { |
---|
184 | return TR_EINVALID; |
---|
185 | } |
---|
186 | |
---|
187 | if( save ) |
---|
188 | { |
---|
189 | if( savetorrent( inf->hashString, tag, data, size ) ) |
---|
190 | { |
---|
191 | return TR_EINVALID; |
---|
192 | } |
---|
193 | savedname( inf->torrent, sizeof inf->torrent, inf->hashString, tag ); |
---|
194 | } |
---|
195 | |
---|
196 | return TR_OK; |
---|
197 | } |
---|
198 | |
---|
199 | int |
---|
200 | tr_metainfoParseHash( tr_info * inf, const char * tag, const char * hash ) |
---|
201 | { |
---|
202 | struct stat sb; |
---|
203 | uint8_t * buf; |
---|
204 | size_t size; |
---|
205 | int save; |
---|
206 | |
---|
207 | /* check it we should use an old file without a tag */ |
---|
208 | /* XXX this should go away at some point */ |
---|
209 | save = 0; |
---|
210 | savedname( inf->torrent, sizeof inf->torrent, hash, tag ); |
---|
211 | if( 0 > stat( inf->torrent, &sb ) && ENOENT == errno ) |
---|
212 | { |
---|
213 | savedname( inf->torrent, sizeof inf->torrent, hash, NULL ); |
---|
214 | if( 0 == stat( inf->torrent, &sb )) |
---|
215 | { |
---|
216 | save = 1; |
---|
217 | } |
---|
218 | } |
---|
219 | |
---|
220 | buf = readtorrent( inf->torrent, &size ); |
---|
221 | if( NULL == buf ) |
---|
222 | { |
---|
223 | return TR_EINVALID; |
---|
224 | } |
---|
225 | |
---|
226 | if( realparse( inf, buf, size ) ) |
---|
227 | { |
---|
228 | free( buf ); |
---|
229 | return TR_EINVALID; |
---|
230 | } |
---|
231 | |
---|
232 | /* save a new tagged copy of the old untagged torrent */ |
---|
233 | if( save ) |
---|
234 | { |
---|
235 | if( savetorrent( hash, tag, buf, size ) ) |
---|
236 | { |
---|
237 | free( buf ); |
---|
238 | return TR_EINVALID; |
---|
239 | } |
---|
240 | savedname( inf->torrent, sizeof inf->torrent, hash, tag ); |
---|
241 | } |
---|
242 | |
---|
243 | free( buf ); |
---|
244 | |
---|
245 | return TR_OK; |
---|
246 | } |
---|
247 | |
---|
248 | static int |
---|
249 | realparse( tr_info * inf, const uint8_t * buf, size_t size ) |
---|
250 | { |
---|
251 | benc_val_t meta, * beInfo, * val, * val2; |
---|
252 | int i; |
---|
253 | |
---|
254 | /* Parse bencoded infos */ |
---|
255 | if( tr_bencLoad( buf, size, &meta, NULL ) ) |
---|
256 | { |
---|
257 | tr_err( "Error while parsing bencoded data [%*.*s]", (int)size, (int)size, (char*)buf ); |
---|
258 | return TR_EINVALID; |
---|
259 | } |
---|
260 | |
---|
261 | /* Get info hash */ |
---|
262 | beInfo = tr_bencDictFind( &meta, "info" ); |
---|
263 | if( NULL == beInfo || TYPE_DICT != beInfo->type ) |
---|
264 | { |
---|
265 | tr_err( "%s \"info\" dictionary", ( beInfo ? "Invalid" : "Missing" ) ); |
---|
266 | tr_bencFree( &meta ); |
---|
267 | return TR_EINVALID; |
---|
268 | } |
---|
269 | |
---|
270 | tr_sha1( inf->hash, beInfo->begin, beInfo->end - beInfo->begin, NULL ); |
---|
271 | |
---|
272 | for( i = 0; i < SHA_DIGEST_LENGTH; i++ ) |
---|
273 | { |
---|
274 | snprintf( inf->hashString + i * 2, sizeof( inf->hashString ) - i * 2, |
---|
275 | "%02x", inf->hash[i] ); |
---|
276 | } |
---|
277 | |
---|
278 | /* Comment info */ |
---|
279 | val = tr_bencDictFindFirst( &meta, "comment.utf-8", "comment", NULL ); |
---|
280 | if( NULL != val && TYPE_STR == val->type ) |
---|
281 | { |
---|
282 | strlcat_utf8( inf->comment, val->val.s.s, sizeof( inf->comment ), 0 ); |
---|
283 | } |
---|
284 | |
---|
285 | /* Creator info */ |
---|
286 | val = tr_bencDictFindFirst( &meta, "created by.utf-8", "created by", NULL ); |
---|
287 | if( NULL != val && TYPE_STR == val->type ) |
---|
288 | { |
---|
289 | strlcat_utf8( inf->creator, val->val.s.s, sizeof( inf->creator ), 0 ); |
---|
290 | } |
---|
291 | |
---|
292 | /* Date created */ |
---|
293 | inf->dateCreated = 0; |
---|
294 | val = tr_bencDictFind( &meta, "creation date" ); |
---|
295 | if( NULL != val && TYPE_INT == val->type ) |
---|
296 | { |
---|
297 | inf->dateCreated = val->val.i; |
---|
298 | } |
---|
299 | |
---|
300 | /* Private torrent */ |
---|
301 | val = tr_bencDictFind( beInfo, "private" ); |
---|
302 | val2 = tr_bencDictFind( &meta, "private" ); |
---|
303 | if( ( NULL != val && ( TYPE_INT != val->type || 0 != val->val.i ) ) || |
---|
304 | ( NULL != val2 && ( TYPE_INT != val2->type || 0 != val2->val.i ) ) ) |
---|
305 | { |
---|
306 | inf->isPrivate = 1; |
---|
307 | } |
---|
308 | |
---|
309 | /* Piece length */ |
---|
310 | val = tr_bencDictFind( beInfo, "piece length" ); |
---|
311 | if( NULL == val || TYPE_INT != val->type ) |
---|
312 | { |
---|
313 | tr_err( "%s \"piece length\" entry", ( val ? "Invalid" : "Missing" ) ); |
---|
314 | goto fail; |
---|
315 | } |
---|
316 | inf->pieceSize = val->val.i; |
---|
317 | |
---|
318 | /* Hashes */ |
---|
319 | val = tr_bencDictFind( beInfo, "pieces" ); |
---|
320 | if( NULL == val || TYPE_STR != val->type ) |
---|
321 | { |
---|
322 | tr_err( "%s \"pieces\" entry", ( val ? "Invalid" : "Missing" ) ); |
---|
323 | goto fail; |
---|
324 | } |
---|
325 | if( val->val.s.i % SHA_DIGEST_LENGTH ) |
---|
326 | { |
---|
327 | tr_err( "Invalid \"piece\" string (size is %d)", val->val.s.i ); |
---|
328 | goto fail; |
---|
329 | } |
---|
330 | inf->pieceCount = val->val.s.i / SHA_DIGEST_LENGTH; |
---|
331 | |
---|
332 | inf->pieces = calloc ( inf->pieceCount, sizeof(tr_piece) ); |
---|
333 | |
---|
334 | for ( i=0; i<inf->pieceCount; ++i ) |
---|
335 | { |
---|
336 | memcpy (inf->pieces[i].hash, &val->val.s.s[i*SHA_DIGEST_LENGTH], SHA_DIGEST_LENGTH); |
---|
337 | } |
---|
338 | |
---|
339 | /* get file or top directory name */ |
---|
340 | val = tr_bencDictFindFirst( beInfo, "name.utf-8", "name", NULL ); |
---|
341 | if( parseFiles( inf, tr_bencDictFindFirst( beInfo, |
---|
342 | "name.utf-8", "name", NULL ), |
---|
343 | tr_bencDictFind( beInfo, "files" ), |
---|
344 | tr_bencDictFind( beInfo, "length" ) ) ) |
---|
345 | { |
---|
346 | goto fail; |
---|
347 | } |
---|
348 | |
---|
349 | if( !inf->fileCount ) |
---|
350 | { |
---|
351 | tr_err( "Torrent has no files." ); |
---|
352 | goto fail; |
---|
353 | } |
---|
354 | |
---|
355 | if( !inf->totalSize ) |
---|
356 | { |
---|
357 | tr_err( "Torrent is zero bytes long." ); |
---|
358 | goto fail; |
---|
359 | } |
---|
360 | |
---|
361 | /* TODO add more tests so we don't crash on weird files */ |
---|
362 | |
---|
363 | if( (uint64_t) inf->pieceCount != |
---|
364 | ( inf->totalSize + inf->pieceSize - 1 ) / inf->pieceSize ) |
---|
365 | { |
---|
366 | tr_err( "Size of hashes and files don't match" ); |
---|
367 | goto fail; |
---|
368 | } |
---|
369 | |
---|
370 | /* get announce or announce-list */ |
---|
371 | if( getannounce( inf, &meta ) ) |
---|
372 | { |
---|
373 | goto fail; |
---|
374 | } |
---|
375 | |
---|
376 | tr_bencFree( &meta ); |
---|
377 | return TR_OK; |
---|
378 | |
---|
379 | fail: |
---|
380 | tr_metainfoFree( inf ); |
---|
381 | tr_bencFree( &meta ); |
---|
382 | return TR_EINVALID; |
---|
383 | } |
---|
384 | |
---|
385 | void tr_metainfoFree( tr_info * inf ) |
---|
386 | { |
---|
387 | int i, j; |
---|
388 | |
---|
389 | tr_free( inf->pieces ); |
---|
390 | tr_free( inf->files ); |
---|
391 | tr_free( inf->primaryAddress ); |
---|
392 | |
---|
393 | for( i=0; i<inf->trackerTiers; ++i ) { |
---|
394 | for( j=0; j<inf->trackerList[i].count; ++j ) |
---|
395 | tr_trackerInfoClear( &inf->trackerList[i].list[j] ); |
---|
396 | tr_free( inf->trackerList[i].list ); |
---|
397 | } |
---|
398 | tr_free( inf->trackerList ); |
---|
399 | |
---|
400 | memset( inf, '\0', sizeof(tr_info) ); |
---|
401 | } |
---|
402 | |
---|
403 | static int getfile( char * buf, int size, |
---|
404 | const char * prefix, benc_val_t * name ) |
---|
405 | { |
---|
406 | benc_val_t * dir; |
---|
407 | const char ** list; |
---|
408 | int ii, jj; |
---|
409 | |
---|
410 | if( TYPE_LIST != name->type ) |
---|
411 | { |
---|
412 | return TR_EINVALID; |
---|
413 | } |
---|
414 | |
---|
415 | list = calloc( name->val.l.count, sizeof( list[0] ) ); |
---|
416 | if( NULL == list ) |
---|
417 | { |
---|
418 | return TR_EINVALID; |
---|
419 | } |
---|
420 | |
---|
421 | for( ii = jj = 0; name->val.l.count > ii; ii++ ) |
---|
422 | { |
---|
423 | dir = &name->val.l.vals[ii]; |
---|
424 | if( TYPE_STR != dir->type ) |
---|
425 | { |
---|
426 | continue; |
---|
427 | } |
---|
428 | if( 0 == strcmp( "..", dir->val.s.s ) ) |
---|
429 | { |
---|
430 | if( 0 < jj ) |
---|
431 | { |
---|
432 | jj--; |
---|
433 | } |
---|
434 | } |
---|
435 | else if( 0 != strcmp( ".", dir->val.s.s ) ) |
---|
436 | { |
---|
437 | list[jj] = dir->val.s.s; |
---|
438 | jj++; |
---|
439 | } |
---|
440 | } |
---|
441 | |
---|
442 | if( 0 == jj ) |
---|
443 | { |
---|
444 | free( list ); |
---|
445 | return TR_EINVALID; |
---|
446 | } |
---|
447 | |
---|
448 | strlcat_utf8( buf, prefix, size, 0 ); |
---|
449 | for( ii = 0; jj > ii; ii++ ) |
---|
450 | { |
---|
451 | strlcat_utf8( buf, TR_PATH_DELIMITER_STR, size, 0 ); |
---|
452 | strlcat_utf8( buf, list[ii], size, TR_PATH_DELIMITER ); |
---|
453 | } |
---|
454 | free( list ); |
---|
455 | |
---|
456 | return TR_OK; |
---|
457 | } |
---|
458 | |
---|
459 | static int getannounce( tr_info * inf, benc_val_t * meta ) |
---|
460 | { |
---|
461 | benc_val_t * val, * subval, * urlval; |
---|
462 | char * address, * announce; |
---|
463 | int ii, jj, port, random, subcount; |
---|
464 | tr_tracker_info * sublist; |
---|
465 | void * swapping; |
---|
466 | |
---|
467 | /* Announce-list */ |
---|
468 | val = tr_bencDictFind( meta, "announce-list" ); |
---|
469 | if( NULL != val && TYPE_LIST == val->type && 0 < val->val.l.count ) |
---|
470 | { |
---|
471 | inf->trackerTiers = 0; |
---|
472 | inf->trackerList = calloc( val->val.l.count, |
---|
473 | sizeof( inf->trackerList[0] ) ); |
---|
474 | |
---|
475 | /* iterate through the announce-list's tiers */ |
---|
476 | for( ii = 0; ii < val->val.l.count; ii++ ) |
---|
477 | { |
---|
478 | subval = &val->val.l.vals[ii]; |
---|
479 | if( TYPE_LIST != subval->type || 0 >= subval->val.l.count ) |
---|
480 | { |
---|
481 | continue; |
---|
482 | } |
---|
483 | subcount = 0; |
---|
484 | sublist = calloc( subval->val.l.count, sizeof( sublist[0] ) ); |
---|
485 | |
---|
486 | /* iterate through the tier's items */ |
---|
487 | for( jj = 0; jj < subval->val.l.count; jj++ ) |
---|
488 | { |
---|
489 | tr_tracker_info tmp; |
---|
490 | |
---|
491 | urlval = &subval->val.l.vals[jj]; |
---|
492 | if( TYPE_STR != urlval->type || |
---|
493 | tr_trackerInfoInit( &tmp, urlval->val.s.s, urlval->val.s.i ) ) |
---|
494 | { |
---|
495 | continue; |
---|
496 | } |
---|
497 | |
---|
498 | if( !inf->primaryAddress ) { |
---|
499 | char buf[1024]; |
---|
500 | snprintf( buf, sizeof(buf), "%s:%d", tmp.address, tmp.port ); |
---|
501 | inf->primaryAddress = tr_strdup( buf ); |
---|
502 | } |
---|
503 | |
---|
504 | /* place the item info in a random location in the sublist */ |
---|
505 | random = tr_rand( subcount + 1 ); |
---|
506 | if( random != subcount ) |
---|
507 | sublist[subcount] = sublist[random]; |
---|
508 | sublist[random] = tmp; |
---|
509 | subcount++; |
---|
510 | } |
---|
511 | |
---|
512 | /* just use sublist as-is if it's full */ |
---|
513 | if( subcount == subval->val.l.count ) |
---|
514 | { |
---|
515 | inf->trackerList[inf->trackerTiers].list = sublist; |
---|
516 | inf->trackerList[inf->trackerTiers].count = subcount; |
---|
517 | inf->trackerTiers++; |
---|
518 | } |
---|
519 | /* if we skipped some of the tier's items then trim the sublist */ |
---|
520 | else if( 0 < subcount ) |
---|
521 | { |
---|
522 | inf->trackerList[inf->trackerTiers].list = calloc( subcount, sizeof( sublist[0] ) ); |
---|
523 | memcpy( inf->trackerList[inf->trackerTiers].list, sublist, |
---|
524 | sizeof( sublist[0] ) * subcount ); |
---|
525 | inf->trackerList[inf->trackerTiers].count = subcount; |
---|
526 | inf->trackerTiers++; |
---|
527 | free( sublist ); |
---|
528 | } |
---|
529 | /* drop the whole sublist if we didn't use any items at all */ |
---|
530 | else |
---|
531 | { |
---|
532 | free( sublist ); |
---|
533 | } |
---|
534 | } |
---|
535 | |
---|
536 | /* did we use any of the tiers? */ |
---|
537 | if( 0 == inf->trackerTiers ) |
---|
538 | { |
---|
539 | tr_inf( "No valid entries in \"announce-list\"" ); |
---|
540 | free( inf->trackerList ); |
---|
541 | inf->trackerList = NULL; |
---|
542 | } |
---|
543 | /* trim unused sublist pointers */ |
---|
544 | else if( inf->trackerTiers < val->val.l.count ) |
---|
545 | { |
---|
546 | swapping = inf->trackerList; |
---|
547 | inf->trackerList = calloc( inf->trackerTiers, |
---|
548 | sizeof( inf->trackerList[0] ) ); |
---|
549 | memcpy( inf->trackerList, swapping, |
---|
550 | sizeof( inf->trackerList[0] ) * inf->trackerTiers ); |
---|
551 | free( swapping ); |
---|
552 | } |
---|
553 | } |
---|
554 | |
---|
555 | /* Regular announce value */ |
---|
556 | val = tr_bencDictFind( meta, "announce" ); |
---|
557 | if( NULL == val || TYPE_STR != val->type ) |
---|
558 | { |
---|
559 | tr_err( "No \"announce\" entry" ); |
---|
560 | return TR_EINVALID; |
---|
561 | } |
---|
562 | |
---|
563 | if( !inf->trackerTiers ) |
---|
564 | { |
---|
565 | |
---|
566 | if( tr_httpParseUrl( val->val.s.s, val->val.s.i, |
---|
567 | &address, &port, &announce ) ) |
---|
568 | { |
---|
569 | tr_err( "Invalid announce URL (%s)", val->val.s.s ); |
---|
570 | return TR_EINVALID; |
---|
571 | } |
---|
572 | sublist = calloc( 1, sizeof( sublist[0] ) ); |
---|
573 | sublist[0].address = address; |
---|
574 | sublist[0].port = port; |
---|
575 | sublist[0].announce = announce; |
---|
576 | sublist[0].scrape = announceToScrape( announce ); |
---|
577 | inf->trackerList = calloc( 1, sizeof( inf->trackerList[0] ) ); |
---|
578 | inf->trackerList[0].list = sublist; |
---|
579 | inf->trackerList[0].count = 1; |
---|
580 | inf->trackerTiers = 1; |
---|
581 | |
---|
582 | if( !inf->primaryAddress ) { |
---|
583 | char buf[1024]; |
---|
584 | snprintf( buf, sizeof(buf), "%s:%d", sublist[0].address, sublist[0].port ); |
---|
585 | inf->primaryAddress = tr_strdup( buf ); |
---|
586 | } |
---|
587 | |
---|
588 | } |
---|
589 | |
---|
590 | return TR_OK; |
---|
591 | } |
---|
592 | |
---|
593 | static char * announceToScrape( const char * announce ) |
---|
594 | { |
---|
595 | char old[] = "announce"; |
---|
596 | int oldlen = 8; |
---|
597 | char new[] = "scrape"; |
---|
598 | int newlen = 6; |
---|
599 | char * slash, * scrape; |
---|
600 | size_t scrapelen, used; |
---|
601 | |
---|
602 | slash = strrchr( announce, '/' ); |
---|
603 | if( NULL == slash ) |
---|
604 | { |
---|
605 | return NULL; |
---|
606 | } |
---|
607 | slash++; |
---|
608 | |
---|
609 | if( 0 != strncmp( slash, old, oldlen ) ) |
---|
610 | { |
---|
611 | return NULL; |
---|
612 | } |
---|
613 | |
---|
614 | scrapelen = strlen( announce ) - oldlen + newlen; |
---|
615 | scrape = calloc( scrapelen + 1, 1 ); |
---|
616 | if( NULL == scrape ) |
---|
617 | { |
---|
618 | return NULL; |
---|
619 | } |
---|
620 | assert( ( size_t )( slash - announce ) < scrapelen ); |
---|
621 | memcpy( scrape, announce, slash - announce ); |
---|
622 | used = slash - announce; |
---|
623 | strncat( scrape, new, scrapelen - used ); |
---|
624 | used += newlen; |
---|
625 | assert( strlen( scrape ) == used ); |
---|
626 | if( used < scrapelen ) |
---|
627 | { |
---|
628 | assert( strlen( slash + oldlen ) == scrapelen - used ); |
---|
629 | strncat( scrape, slash + oldlen, scrapelen - used ); |
---|
630 | } |
---|
631 | |
---|
632 | return scrape; |
---|
633 | } |
---|
634 | |
---|
635 | int |
---|
636 | tr_trackerInfoInit( tr_tracker_info * info, |
---|
637 | const char * address, |
---|
638 | int address_len ) |
---|
639 | { |
---|
640 | int ret = tr_httpParseUrl( address, address_len, |
---|
641 | &info->address, |
---|
642 | &info->port, |
---|
643 | &info->announce ); |
---|
644 | if( !ret ) |
---|
645 | info->scrape = announceToScrape( info->announce ); |
---|
646 | |
---|
647 | return ret; |
---|
648 | } |
---|
649 | |
---|
650 | void |
---|
651 | tr_trackerInfoClear( tr_tracker_info * info ) |
---|
652 | { |
---|
653 | tr_free( info->address ); |
---|
654 | tr_free( info->announce ); |
---|
655 | tr_free( info->scrape ); |
---|
656 | memset( info, '\0', sizeof(tr_tracker_info) ); |
---|
657 | } |
---|
658 | |
---|
659 | |
---|
660 | static void |
---|
661 | savedname( char * name, size_t len, const char * hash, const char * tag ) |
---|
662 | { |
---|
663 | const char * torDir = tr_getTorrentsDirectory (); |
---|
664 | |
---|
665 | if( tag == NULL ) |
---|
666 | { |
---|
667 | tr_buildPath( name, len, torDir, hash, NULL ); |
---|
668 | } |
---|
669 | else |
---|
670 | { |
---|
671 | char base[1024]; |
---|
672 | snprintf( base, sizeof(base), "%s-%s", hash, tag ); |
---|
673 | tr_buildPath( name, len, torDir, base, NULL ); |
---|
674 | } |
---|
675 | } |
---|
676 | |
---|
677 | void tr_metainfoRemoveSaved( const char * hashString, const char * tag ) |
---|
678 | { |
---|
679 | char file[MAX_PATH_LENGTH]; |
---|
680 | |
---|
681 | savedname( file, sizeof file, hashString, tag ); |
---|
682 | unlink( file ); |
---|
683 | } |
---|
684 | |
---|
685 | static uint8_t * |
---|
686 | readtorrent( const char * path, size_t * size ) |
---|
687 | { |
---|
688 | return tr_loadFile( path, size ); |
---|
689 | } |
---|
690 | |
---|
691 | /* Save a copy of the torrent file in the saved torrent directory */ |
---|
692 | static int |
---|
693 | savetorrent( const char * hash, const char * tag, |
---|
694 | const uint8_t * buf, size_t buflen ) |
---|
695 | { |
---|
696 | char path[MAX_PATH_LENGTH]; |
---|
697 | FILE * file; |
---|
698 | |
---|
699 | savedname( path, sizeof path, hash, tag ); |
---|
700 | file = fopen( path, "wb+" ); |
---|
701 | if( !file ) |
---|
702 | { |
---|
703 | tr_err( "Could not open file (%s) (%s)", path, strerror( errno ) ); |
---|
704 | return TR_EINVALID; |
---|
705 | } |
---|
706 | fseek( file, 0, SEEK_SET ); |
---|
707 | if( fwrite( buf, 1, buflen, file ) != buflen ) |
---|
708 | { |
---|
709 | tr_err( "Could not write file (%s) (%s)", path, strerror( errno ) ); |
---|
710 | fclose( file ); |
---|
711 | return TR_EINVALID; |
---|
712 | } |
---|
713 | fclose( file ); |
---|
714 | |
---|
715 | return TR_OK; |
---|
716 | } |
---|
717 | |
---|
718 | static int |
---|
719 | parseFiles( tr_info * inf, benc_val_t * name, |
---|
720 | benc_val_t * files, benc_val_t * length ) |
---|
721 | { |
---|
722 | benc_val_t * item, * path; |
---|
723 | int ii; |
---|
724 | |
---|
725 | if( NULL == name || TYPE_STR != name->type ) |
---|
726 | { |
---|
727 | tr_err( "%s \"name\" string", ( name ? "Invalid" : "Missing" ) ); |
---|
728 | return TR_EINVALID; |
---|
729 | } |
---|
730 | |
---|
731 | strlcat_utf8( inf->name, name->val.s.s, sizeof( inf->name ), |
---|
732 | TR_PATH_DELIMITER ); |
---|
733 | if( '\0' == inf->name[0] ) |
---|
734 | { |
---|
735 | tr_err( "Invalid \"name\" string" ); |
---|
736 | return TR_EINVALID; |
---|
737 | } |
---|
738 | inf->totalSize = 0; |
---|
739 | |
---|
740 | if( files && TYPE_LIST == files->type ) |
---|
741 | { |
---|
742 | /* Multi-file mode */ |
---|
743 | inf->isMultifile = 1; |
---|
744 | inf->fileCount = files->val.l.count; |
---|
745 | inf->files = calloc( inf->fileCount, sizeof( inf->files[0] ) ); |
---|
746 | |
---|
747 | if( NULL == inf->files ) |
---|
748 | { |
---|
749 | return TR_EINVALID; |
---|
750 | } |
---|
751 | |
---|
752 | for( ii = 0; files->val.l.count > ii; ii++ ) |
---|
753 | { |
---|
754 | item = &files->val.l.vals[ii]; |
---|
755 | path = tr_bencDictFindFirst( item, "path.utf-8", "path", NULL ); |
---|
756 | if( getfile( inf->files[ii].name, sizeof( inf->files[0].name ), |
---|
757 | inf->name, path ) ) |
---|
758 | { |
---|
759 | tr_err( "%s \"path\" entry", |
---|
760 | ( path ? "Invalid" : "Missing" ) ); |
---|
761 | return TR_EINVALID; |
---|
762 | } |
---|
763 | length = tr_bencDictFind( item, "length" ); |
---|
764 | if( NULL == length || TYPE_INT != length->type ) |
---|
765 | { |
---|
766 | tr_err( "%s \"length\" entry", |
---|
767 | ( length ? "Invalid" : "Missing" ) ); |
---|
768 | return TR_EINVALID; |
---|
769 | } |
---|
770 | inf->files[ii].length = length->val.i; |
---|
771 | inf->totalSize += length->val.i; |
---|
772 | } |
---|
773 | } |
---|
774 | else if( NULL != length && TYPE_INT == length->type ) |
---|
775 | { |
---|
776 | /* Single-file mode */ |
---|
777 | inf->isMultifile = 0; |
---|
778 | inf->fileCount = 1; |
---|
779 | inf->files = calloc( 1, sizeof( inf->files[0] ) ); |
---|
780 | |
---|
781 | if( NULL == inf->files ) |
---|
782 | { |
---|
783 | return TR_EINVALID; |
---|
784 | } |
---|
785 | |
---|
786 | strlcat_utf8( inf->files[0].name, name->val.s.s, |
---|
787 | sizeof( inf->files[0].name ), TR_PATH_DELIMITER ); |
---|
788 | |
---|
789 | inf->files[0].length = length->val.i; |
---|
790 | inf->totalSize += length->val.i; |
---|
791 | } |
---|
792 | else |
---|
793 | { |
---|
794 | tr_err( "%s \"files\" entry and %s \"length\" entry", |
---|
795 | ( files ? "Invalid" : "Missing" ), |
---|
796 | ( length ? "invalid" : "missing" ) ); |
---|
797 | } |
---|
798 | |
---|
799 | return TR_OK; |
---|
800 | } |
---|