1 | /****************************************************************************** |
---|
2 | * $Id: metainfo.c 4404 2008-01-01 17:20:20Z charles $ |
---|
3 | * |
---|
4 | * Copyright (c) 2005-2008 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 <miniupnp/miniwget.h> /* parseURL */ |
---|
35 | |
---|
36 | #include "transmission.h" |
---|
37 | #include "bencode.h" |
---|
38 | #include "crypto.h" /* tr_sha1 */ |
---|
39 | #include "metainfo.h" |
---|
40 | #include "platform.h" |
---|
41 | #include "utils.h" |
---|
42 | |
---|
43 | |
---|
44 | static int |
---|
45 | tr_httpParseUrl( const char * url_in, int len, |
---|
46 | char ** setme_host, int * setme_port, char ** setme_path ) |
---|
47 | { |
---|
48 | char * url = tr_strndup( url_in, len ); |
---|
49 | char * path; |
---|
50 | char host[4096+1]; |
---|
51 | unsigned short port; |
---|
52 | int success; |
---|
53 | |
---|
54 | success = parseURL( url, host, &port, &path ); |
---|
55 | |
---|
56 | if( success ) { |
---|
57 | *setme_host = tr_strdup( host ); |
---|
58 | *setme_port = port; |
---|
59 | *setme_path = tr_strdup( path ); |
---|
60 | } |
---|
61 | |
---|
62 | tr_free( url ); |
---|
63 | |
---|
64 | return !success; |
---|
65 | } |
---|
66 | |
---|
67 | /*********************************************************************** |
---|
68 | * Local prototypes |
---|
69 | **********************************************************************/ |
---|
70 | static int getannounce( tr_info * inf, benc_val_t * meta ); |
---|
71 | static char * announceToScrape( const char * announce ); |
---|
72 | static int parseFiles( tr_info * inf, benc_val_t * name, |
---|
73 | benc_val_t * files, benc_val_t * length ); |
---|
74 | |
---|
75 | /*** |
---|
76 | **** |
---|
77 | ***/ |
---|
78 | |
---|
79 | #define WANTBYTES( want, got ) \ |
---|
80 | if( (want) > (got) ) { return; } else { (got) -= (want); } |
---|
81 | static void |
---|
82 | strlcat_utf8( void * dest, const void * src, size_t len, char skip ) |
---|
83 | { |
---|
84 | char * s = dest; |
---|
85 | const char * append = src; |
---|
86 | const char * p; |
---|
87 | |
---|
88 | /* don't overwrite the nul at the end */ |
---|
89 | len--; |
---|
90 | |
---|
91 | /* Go to the end of the destination string */ |
---|
92 | while( s[0] ) |
---|
93 | { |
---|
94 | s++; |
---|
95 | len--; |
---|
96 | } |
---|
97 | |
---|
98 | /* Now start appending, converting on the fly if necessary */ |
---|
99 | for( p = append; p[0]; ) |
---|
100 | { |
---|
101 | /* skip over the requested character */ |
---|
102 | if( skip == p[0] ) |
---|
103 | { |
---|
104 | p++; |
---|
105 | continue; |
---|
106 | } |
---|
107 | |
---|
108 | if( !( p[0] & 0x80 ) ) |
---|
109 | { |
---|
110 | /* ASCII character */ |
---|
111 | WANTBYTES( 1, len ); |
---|
112 | *(s++) = *(p++); |
---|
113 | continue; |
---|
114 | } |
---|
115 | |
---|
116 | if( ( p[0] & 0xE0 ) == 0xC0 && ( p[1] & 0xC0 ) == 0x80 ) |
---|
117 | { |
---|
118 | /* 2-bytes UTF-8 character */ |
---|
119 | WANTBYTES( 2, len ); |
---|
120 | *(s++) = *(p++); *(s++) = *(p++); |
---|
121 | continue; |
---|
122 | } |
---|
123 | |
---|
124 | if( ( p[0] & 0xF0 ) == 0xE0 && ( p[1] & 0xC0 ) == 0x80 && |
---|
125 | ( p[2] & 0xC0 ) == 0x80 ) |
---|
126 | { |
---|
127 | /* 3-bytes UTF-8 character */ |
---|
128 | WANTBYTES( 3, len ); |
---|
129 | *(s++) = *(p++); *(s++) = *(p++); |
---|
130 | *(s++) = *(p++); |
---|
131 | continue; |
---|
132 | } |
---|
133 | |
---|
134 | if( ( p[0] & 0xF8 ) == 0xF0 && ( p[1] & 0xC0 ) == 0x80 && |
---|
135 | ( p[2] & 0xC0 ) == 0x80 && ( p[3] & 0xC0 ) == 0x80 ) |
---|
136 | { |
---|
137 | /* 4-bytes UTF-8 character */ |
---|
138 | WANTBYTES( 4, len ); |
---|
139 | *(s++) = *(p++); *(s++) = *(p++); |
---|
140 | *(s++) = *(p++); *(s++) = *(p++); |
---|
141 | continue; |
---|
142 | } |
---|
143 | |
---|
144 | /* ISO 8859-1 -> UTF-8 conversion */ |
---|
145 | WANTBYTES( 2, len ); |
---|
146 | *(s++) = 0xC0 | ( ( *p & 0xFF ) >> 6 ); |
---|
147 | *(s++) = 0x80 | ( *(p++) & 0x3F ); |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|
151 | static void |
---|
152 | savedname( char * name, size_t len, const char * hash, const char * tag ) |
---|
153 | { |
---|
154 | const char * torDir = tr_getTorrentsDirectory (); |
---|
155 | |
---|
156 | if( tag == NULL ) |
---|
157 | { |
---|
158 | tr_buildPath( name, len, torDir, hash, NULL ); |
---|
159 | } |
---|
160 | else |
---|
161 | { |
---|
162 | char base[1024]; |
---|
163 | snprintf( base, sizeof(base), "%s-%s", hash, tag ); |
---|
164 | tr_buildPath( name, len, torDir, base, NULL ); |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | |
---|
169 | int |
---|
170 | tr_metainfoParse( tr_info * inf, const benc_val_t * meta_in, const char * tag ) |
---|
171 | { |
---|
172 | int i; |
---|
173 | benc_val_t * beInfo, * val, * val2; |
---|
174 | benc_val_t * meta = (benc_val_t *) meta_in; |
---|
175 | |
---|
176 | /* info_hash: urlencoded 20-byte SHA1 hash of the value of the info key |
---|
177 | * from the Metainfo file. Note that the value will be a bencoded |
---|
178 | * dictionary, given the definition of the info key above. */ |
---|
179 | if(( beInfo = tr_bencDictFindType( meta, "info", TYPE_DICT ))) |
---|
180 | { |
---|
181 | int len; |
---|
182 | char * str = tr_bencSave( beInfo, &len ); |
---|
183 | tr_sha1( inf->hash, str, len, NULL ); |
---|
184 | tr_free( str ); |
---|
185 | } |
---|
186 | else |
---|
187 | { |
---|
188 | tr_err( "info dictionary not found!" ); |
---|
189 | return TR_EINVALID; |
---|
190 | } |
---|
191 | |
---|
192 | for( i = 0; i < SHA_DIGEST_LENGTH; i++ ) |
---|
193 | { |
---|
194 | snprintf( inf->hashString + i * 2, sizeof( inf->hashString ) - i * 2, |
---|
195 | "%02x", inf->hash[i] ); |
---|
196 | } |
---|
197 | savedname( inf->torrent, sizeof( inf->torrent ), inf->hashString, tag ); |
---|
198 | |
---|
199 | /* Comment info */ |
---|
200 | val = tr_bencDictFindFirst( meta, "comment.utf-8", "comment", NULL ); |
---|
201 | if( NULL != val && TYPE_STR == val->type ) |
---|
202 | { |
---|
203 | strlcat_utf8( inf->comment, val->val.s.s, sizeof( inf->comment ), 0 ); |
---|
204 | } |
---|
205 | |
---|
206 | /* Creator info */ |
---|
207 | val = tr_bencDictFindFirst( meta, "created by.utf-8", "created by", NULL ); |
---|
208 | if( NULL != val && TYPE_STR == val->type ) |
---|
209 | { |
---|
210 | strlcat_utf8( inf->creator, val->val.s.s, sizeof( inf->creator ), 0 ); |
---|
211 | } |
---|
212 | |
---|
213 | /* Date created */ |
---|
214 | inf->dateCreated = 0; |
---|
215 | val = tr_bencDictFind( meta, "creation date" ); |
---|
216 | if( NULL != val && TYPE_INT == val->type ) |
---|
217 | { |
---|
218 | inf->dateCreated = val->val.i; |
---|
219 | } |
---|
220 | |
---|
221 | /* Private torrent */ |
---|
222 | val = tr_bencDictFind( beInfo, "private" ); |
---|
223 | val2 = tr_bencDictFind( meta, "private" ); |
---|
224 | if( ( NULL != val && ( TYPE_INT != val->type || 0 != val->val.i ) ) || |
---|
225 | ( NULL != val2 && ( TYPE_INT != val2->type || 0 != val2->val.i ) ) ) |
---|
226 | { |
---|
227 | inf->isPrivate = 1; |
---|
228 | } |
---|
229 | |
---|
230 | /* Piece length */ |
---|
231 | val = tr_bencDictFind( beInfo, "piece length" ); |
---|
232 | if( NULL == val || TYPE_INT != val->type ) |
---|
233 | { |
---|
234 | tr_err( "%s \"piece length\" entry", ( val ? "Invalid" : "Missing" ) ); |
---|
235 | goto fail; |
---|
236 | } |
---|
237 | inf->pieceSize = val->val.i; |
---|
238 | |
---|
239 | /* Hashes */ |
---|
240 | val = tr_bencDictFind( beInfo, "pieces" ); |
---|
241 | if( NULL == val || TYPE_STR != val->type ) |
---|
242 | { |
---|
243 | tr_err( "%s \"pieces\" entry", ( val ? "Invalid" : "Missing" ) ); |
---|
244 | goto fail; |
---|
245 | } |
---|
246 | if( val->val.s.i % SHA_DIGEST_LENGTH ) |
---|
247 | { |
---|
248 | tr_err( "Invalid \"piece\" string (size is %d)", val->val.s.i ); |
---|
249 | goto fail; |
---|
250 | } |
---|
251 | inf->pieceCount = val->val.s.i / SHA_DIGEST_LENGTH; |
---|
252 | |
---|
253 | inf->pieces = calloc ( inf->pieceCount, sizeof(tr_piece) ); |
---|
254 | |
---|
255 | for ( i=0; i<inf->pieceCount; ++i ) |
---|
256 | { |
---|
257 | memcpy (inf->pieces[i].hash, &val->val.s.s[i*SHA_DIGEST_LENGTH], SHA_DIGEST_LENGTH); |
---|
258 | } |
---|
259 | |
---|
260 | /* get file or top directory name */ |
---|
261 | val = tr_bencDictFindFirst( beInfo, "name.utf-8", "name", NULL ); |
---|
262 | if( parseFiles( inf, tr_bencDictFindFirst( beInfo, |
---|
263 | "name.utf-8", "name", NULL ), |
---|
264 | tr_bencDictFind( beInfo, "files" ), |
---|
265 | tr_bencDictFind( beInfo, "length" ) ) ) |
---|
266 | { |
---|
267 | goto fail; |
---|
268 | } |
---|
269 | |
---|
270 | if( !inf->fileCount ) |
---|
271 | { |
---|
272 | tr_err( "Torrent has no files." ); |
---|
273 | goto fail; |
---|
274 | } |
---|
275 | |
---|
276 | if( !inf->totalSize ) |
---|
277 | { |
---|
278 | tr_err( "Torrent is zero bytes long." ); |
---|
279 | goto fail; |
---|
280 | } |
---|
281 | |
---|
282 | /* TODO add more tests so we don't crash on weird files */ |
---|
283 | |
---|
284 | if( (uint64_t) inf->pieceCount != |
---|
285 | ( inf->totalSize + inf->pieceSize - 1 ) / inf->pieceSize ) |
---|
286 | { |
---|
287 | tr_err( "Size of hashes and files don't match" ); |
---|
288 | goto fail; |
---|
289 | } |
---|
290 | |
---|
291 | /* get announce or announce-list */ |
---|
292 | if( getannounce( inf, meta ) ) |
---|
293 | { |
---|
294 | goto fail; |
---|
295 | } |
---|
296 | |
---|
297 | return TR_OK; |
---|
298 | |
---|
299 | fail: |
---|
300 | tr_metainfoFree( inf ); |
---|
301 | return TR_EINVALID; |
---|
302 | } |
---|
303 | |
---|
304 | void tr_metainfoFree( tr_info * inf ) |
---|
305 | { |
---|
306 | int i, j; |
---|
307 | |
---|
308 | tr_free( inf->pieces ); |
---|
309 | tr_free( inf->files ); |
---|
310 | tr_free( inf->primaryAddress ); |
---|
311 | |
---|
312 | for( i=0; i<inf->trackerTiers; ++i ) { |
---|
313 | for( j=0; j<inf->trackerList[i].count; ++j ) |
---|
314 | tr_trackerInfoClear( &inf->trackerList[i].list[j] ); |
---|
315 | tr_free( inf->trackerList[i].list ); |
---|
316 | } |
---|
317 | tr_free( inf->trackerList ); |
---|
318 | |
---|
319 | memset( inf, '\0', sizeof(tr_info) ); |
---|
320 | } |
---|
321 | |
---|
322 | static int getfile( char * buf, int size, |
---|
323 | const char * prefix, benc_val_t * name ) |
---|
324 | { |
---|
325 | benc_val_t * dir; |
---|
326 | const char ** list; |
---|
327 | int ii, jj; |
---|
328 | |
---|
329 | if( TYPE_LIST != name->type ) |
---|
330 | { |
---|
331 | return TR_EINVALID; |
---|
332 | } |
---|
333 | |
---|
334 | list = calloc( name->val.l.count, sizeof( list[0] ) ); |
---|
335 | if( NULL == list ) |
---|
336 | { |
---|
337 | return TR_EINVALID; |
---|
338 | } |
---|
339 | |
---|
340 | for( ii = jj = 0; name->val.l.count > ii; ii++ ) |
---|
341 | { |
---|
342 | dir = &name->val.l.vals[ii]; |
---|
343 | if( TYPE_STR != dir->type ) |
---|
344 | { |
---|
345 | continue; |
---|
346 | } |
---|
347 | if( 0 == strcmp( "..", dir->val.s.s ) ) |
---|
348 | { |
---|
349 | if( 0 < jj ) |
---|
350 | { |
---|
351 | jj--; |
---|
352 | } |
---|
353 | } |
---|
354 | else if( 0 != strcmp( ".", dir->val.s.s ) ) |
---|
355 | { |
---|
356 | list[jj] = dir->val.s.s; |
---|
357 | jj++; |
---|
358 | } |
---|
359 | } |
---|
360 | |
---|
361 | if( 0 == jj ) |
---|
362 | { |
---|
363 | free( list ); |
---|
364 | return TR_EINVALID; |
---|
365 | } |
---|
366 | |
---|
367 | strlcat_utf8( buf, prefix, size, 0 ); |
---|
368 | for( ii = 0; jj > ii; ii++ ) |
---|
369 | { |
---|
370 | strlcat_utf8( buf, TR_PATH_DELIMITER_STR, size, 0 ); |
---|
371 | strlcat_utf8( buf, list[ii], size, TR_PATH_DELIMITER ); |
---|
372 | } |
---|
373 | free( list ); |
---|
374 | |
---|
375 | return TR_OK; |
---|
376 | } |
---|
377 | |
---|
378 | static int getannounce( tr_info * inf, benc_val_t * meta ) |
---|
379 | { |
---|
380 | benc_val_t * val, * subval, * urlval; |
---|
381 | char * address, * announce; |
---|
382 | int ii, jj, port, random, subcount; |
---|
383 | tr_tracker_info * sublist; |
---|
384 | void * swapping; |
---|
385 | |
---|
386 | /* Announce-list */ |
---|
387 | val = tr_bencDictFind( meta, "announce-list" ); |
---|
388 | if( NULL != val && TYPE_LIST == val->type && 0 < val->val.l.count ) |
---|
389 | { |
---|
390 | inf->trackerTiers = 0; |
---|
391 | inf->trackerList = calloc( val->val.l.count, |
---|
392 | sizeof( inf->trackerList[0] ) ); |
---|
393 | |
---|
394 | /* iterate through the announce-list's tiers */ |
---|
395 | for( ii = 0; ii < val->val.l.count; ii++ ) |
---|
396 | { |
---|
397 | subval = &val->val.l.vals[ii]; |
---|
398 | if( TYPE_LIST != subval->type || 0 >= subval->val.l.count ) |
---|
399 | { |
---|
400 | continue; |
---|
401 | } |
---|
402 | subcount = 0; |
---|
403 | sublist = calloc( subval->val.l.count, sizeof( sublist[0] ) ); |
---|
404 | |
---|
405 | /* iterate through the tier's items */ |
---|
406 | for( jj = 0; jj < subval->val.l.count; jj++ ) |
---|
407 | { |
---|
408 | tr_tracker_info tmp; |
---|
409 | |
---|
410 | urlval = &subval->val.l.vals[jj]; |
---|
411 | if( TYPE_STR != urlval->type || |
---|
412 | tr_trackerInfoInit( &tmp, urlval->val.s.s, urlval->val.s.i ) ) |
---|
413 | { |
---|
414 | continue; |
---|
415 | } |
---|
416 | |
---|
417 | if( !inf->primaryAddress ) { |
---|
418 | char buf[1024]; |
---|
419 | snprintf( buf, sizeof(buf), "%s:%d", tmp.address, tmp.port ); |
---|
420 | inf->primaryAddress = tr_strdup( buf ); |
---|
421 | } |
---|
422 | |
---|
423 | /* place the item info in a random location in the sublist */ |
---|
424 | random = tr_rand( subcount + 1 ); |
---|
425 | if( random != subcount ) |
---|
426 | sublist[subcount] = sublist[random]; |
---|
427 | sublist[random] = tmp; |
---|
428 | subcount++; |
---|
429 | } |
---|
430 | |
---|
431 | /* just use sublist as-is if it's full */ |
---|
432 | if( subcount == subval->val.l.count ) |
---|
433 | { |
---|
434 | inf->trackerList[inf->trackerTiers].list = sublist; |
---|
435 | inf->trackerList[inf->trackerTiers].count = subcount; |
---|
436 | inf->trackerTiers++; |
---|
437 | } |
---|
438 | /* if we skipped some of the tier's items then trim the sublist */ |
---|
439 | else if( 0 < subcount ) |
---|
440 | { |
---|
441 | inf->trackerList[inf->trackerTiers].list = calloc( subcount, sizeof( sublist[0] ) ); |
---|
442 | memcpy( inf->trackerList[inf->trackerTiers].list, sublist, |
---|
443 | sizeof( sublist[0] ) * subcount ); |
---|
444 | inf->trackerList[inf->trackerTiers].count = subcount; |
---|
445 | inf->trackerTiers++; |
---|
446 | free( sublist ); |
---|
447 | } |
---|
448 | /* drop the whole sublist if we didn't use any items at all */ |
---|
449 | else |
---|
450 | { |
---|
451 | free( sublist ); |
---|
452 | } |
---|
453 | } |
---|
454 | |
---|
455 | /* did we use any of the tiers? */ |
---|
456 | if( 0 == inf->trackerTiers ) |
---|
457 | { |
---|
458 | tr_inf( "No valid entries in \"announce-list\"" ); |
---|
459 | free( inf->trackerList ); |
---|
460 | inf->trackerList = NULL; |
---|
461 | } |
---|
462 | /* trim unused sublist pointers */ |
---|
463 | else if( inf->trackerTiers < val->val.l.count ) |
---|
464 | { |
---|
465 | swapping = inf->trackerList; |
---|
466 | inf->trackerList = calloc( inf->trackerTiers, |
---|
467 | sizeof( inf->trackerList[0] ) ); |
---|
468 | memcpy( inf->trackerList, swapping, |
---|
469 | sizeof( inf->trackerList[0] ) * inf->trackerTiers ); |
---|
470 | free( swapping ); |
---|
471 | } |
---|
472 | } |
---|
473 | |
---|
474 | /* Regular announce value */ |
---|
475 | val = tr_bencDictFind( meta, "announce" ); |
---|
476 | if( NULL == val || TYPE_STR != val->type ) |
---|
477 | { |
---|
478 | tr_err( "No \"announce\" entry" ); |
---|
479 | return TR_EINVALID; |
---|
480 | } |
---|
481 | |
---|
482 | if( !inf->trackerTiers ) |
---|
483 | { |
---|
484 | |
---|
485 | if( tr_httpParseUrl( val->val.s.s, val->val.s.i, |
---|
486 | &address, &port, &announce ) ) |
---|
487 | { |
---|
488 | tr_err( "Invalid announce URL (%s)", val->val.s.s ); |
---|
489 | return TR_EINVALID; |
---|
490 | } |
---|
491 | sublist = calloc( 1, sizeof( sublist[0] ) ); |
---|
492 | sublist[0].address = address; |
---|
493 | sublist[0].port = port; |
---|
494 | sublist[0].announce = announce; |
---|
495 | sublist[0].scrape = announceToScrape( announce ); |
---|
496 | inf->trackerList = calloc( 1, sizeof( inf->trackerList[0] ) ); |
---|
497 | inf->trackerList[0].list = sublist; |
---|
498 | inf->trackerList[0].count = 1; |
---|
499 | inf->trackerTiers = 1; |
---|
500 | |
---|
501 | if( !inf->primaryAddress ) { |
---|
502 | char buf[1024]; |
---|
503 | snprintf( buf, sizeof(buf), "%s:%d", sublist[0].address, sublist[0].port ); |
---|
504 | inf->primaryAddress = tr_strdup( buf ); |
---|
505 | } |
---|
506 | |
---|
507 | } |
---|
508 | |
---|
509 | return TR_OK; |
---|
510 | } |
---|
511 | |
---|
512 | static char * announceToScrape( const char * announce ) |
---|
513 | { |
---|
514 | char old[] = "announce"; |
---|
515 | int oldlen = 8; |
---|
516 | char new[] = "scrape"; |
---|
517 | int newlen = 6; |
---|
518 | char * slash, * scrape; |
---|
519 | size_t scrapelen, used; |
---|
520 | |
---|
521 | slash = strrchr( announce, '/' ); |
---|
522 | if( NULL == slash ) |
---|
523 | { |
---|
524 | return NULL; |
---|
525 | } |
---|
526 | slash++; |
---|
527 | |
---|
528 | if( 0 != strncmp( slash, old, oldlen ) ) |
---|
529 | { |
---|
530 | return NULL; |
---|
531 | } |
---|
532 | |
---|
533 | scrapelen = strlen( announce ) - oldlen + newlen; |
---|
534 | scrape = calloc( scrapelen + 1, 1 ); |
---|
535 | if( NULL == scrape ) |
---|
536 | { |
---|
537 | return NULL; |
---|
538 | } |
---|
539 | assert( ( size_t )( slash - announce ) < scrapelen ); |
---|
540 | memcpy( scrape, announce, slash - announce ); |
---|
541 | used = slash - announce; |
---|
542 | strncat( scrape, new, scrapelen - used ); |
---|
543 | used += newlen; |
---|
544 | assert( strlen( scrape ) == used ); |
---|
545 | if( used < scrapelen ) |
---|
546 | { |
---|
547 | assert( strlen( slash + oldlen ) == scrapelen - used ); |
---|
548 | strncat( scrape, slash + oldlen, scrapelen - used ); |
---|
549 | } |
---|
550 | |
---|
551 | return scrape; |
---|
552 | } |
---|
553 | |
---|
554 | int |
---|
555 | tr_trackerInfoInit( tr_tracker_info * info, |
---|
556 | const char * address, |
---|
557 | int address_len ) |
---|
558 | { |
---|
559 | int ret = tr_httpParseUrl( address, address_len, |
---|
560 | &info->address, |
---|
561 | &info->port, |
---|
562 | &info->announce ); |
---|
563 | if( !ret ) |
---|
564 | info->scrape = announceToScrape( info->announce ); |
---|
565 | |
---|
566 | return ret; |
---|
567 | } |
---|
568 | |
---|
569 | void |
---|
570 | tr_trackerInfoClear( tr_tracker_info * info ) |
---|
571 | { |
---|
572 | tr_free( info->address ); |
---|
573 | tr_free( info->announce ); |
---|
574 | tr_free( info->scrape ); |
---|
575 | memset( info, '\0', sizeof(tr_tracker_info) ); |
---|
576 | } |
---|
577 | |
---|
578 | void |
---|
579 | tr_metainfoRemoveSaved( const char * hashString, const char * tag ) |
---|
580 | { |
---|
581 | char file[MAX_PATH_LENGTH]; |
---|
582 | savedname( file, sizeof file, hashString, tag ); |
---|
583 | unlink( file ); |
---|
584 | } |
---|
585 | |
---|
586 | /* Save a copy of the torrent file in the saved torrent directory */ |
---|
587 | int |
---|
588 | tr_metainfoSave( const char * hash, const char * tag, |
---|
589 | const uint8_t * buf, size_t buflen ) |
---|
590 | { |
---|
591 | char path[MAX_PATH_LENGTH]; |
---|
592 | FILE * file; |
---|
593 | |
---|
594 | savedname( path, sizeof path, hash, tag ); |
---|
595 | file = fopen( path, "wb+" ); |
---|
596 | if( !file ) |
---|
597 | { |
---|
598 | tr_err( "Could not open file (%s) (%s)", path, strerror( errno ) ); |
---|
599 | return TR_EINVALID; |
---|
600 | } |
---|
601 | fseek( file, 0, SEEK_SET ); |
---|
602 | if( fwrite( buf, 1, buflen, file ) != buflen ) |
---|
603 | { |
---|
604 | tr_err( "Could not write file (%s) (%s)", path, strerror( errno ) ); |
---|
605 | fclose( file ); |
---|
606 | return TR_EINVALID; |
---|
607 | } |
---|
608 | fclose( file ); |
---|
609 | |
---|
610 | return TR_OK; |
---|
611 | } |
---|
612 | |
---|
613 | static int |
---|
614 | parseFiles( tr_info * inf, benc_val_t * name, |
---|
615 | benc_val_t * files, benc_val_t * length ) |
---|
616 | { |
---|
617 | benc_val_t * item, * path; |
---|
618 | int ii; |
---|
619 | |
---|
620 | if( NULL == name || TYPE_STR != name->type ) |
---|
621 | { |
---|
622 | tr_err( "%s \"name\" string", ( name ? "Invalid" : "Missing" ) ); |
---|
623 | return TR_EINVALID; |
---|
624 | } |
---|
625 | |
---|
626 | strlcat_utf8( inf->name, name->val.s.s, sizeof( inf->name ), |
---|
627 | TR_PATH_DELIMITER ); |
---|
628 | if( '\0' == inf->name[0] ) |
---|
629 | { |
---|
630 | tr_err( "Invalid \"name\" string" ); |
---|
631 | return TR_EINVALID; |
---|
632 | } |
---|
633 | inf->totalSize = 0; |
---|
634 | |
---|
635 | if( files && TYPE_LIST == files->type ) |
---|
636 | { |
---|
637 | /* Multi-file mode */ |
---|
638 | inf->isMultifile = 1; |
---|
639 | inf->fileCount = files->val.l.count; |
---|
640 | inf->files = calloc( inf->fileCount, sizeof( inf->files[0] ) ); |
---|
641 | |
---|
642 | if( NULL == inf->files ) |
---|
643 | { |
---|
644 | return TR_EINVALID; |
---|
645 | } |
---|
646 | |
---|
647 | for( ii = 0; files->val.l.count > ii; ii++ ) |
---|
648 | { |
---|
649 | item = &files->val.l.vals[ii]; |
---|
650 | path = tr_bencDictFindFirst( item, "path.utf-8", "path", NULL ); |
---|
651 | if( getfile( inf->files[ii].name, sizeof( inf->files[0].name ), |
---|
652 | inf->name, path ) ) |
---|
653 | { |
---|
654 | tr_err( "%s \"path\" entry", |
---|
655 | ( path ? "Invalid" : "Missing" ) ); |
---|
656 | return TR_EINVALID; |
---|
657 | } |
---|
658 | length = tr_bencDictFind( item, "length" ); |
---|
659 | if( NULL == length || TYPE_INT != length->type ) |
---|
660 | { |
---|
661 | tr_err( "%s \"length\" entry", |
---|
662 | ( length ? "Invalid" : "Missing" ) ); |
---|
663 | return TR_EINVALID; |
---|
664 | } |
---|
665 | inf->files[ii].length = length->val.i; |
---|
666 | inf->totalSize += length->val.i; |
---|
667 | } |
---|
668 | } |
---|
669 | else if( NULL != length && TYPE_INT == length->type ) |
---|
670 | { |
---|
671 | /* Single-file mode */ |
---|
672 | inf->isMultifile = 0; |
---|
673 | inf->fileCount = 1; |
---|
674 | inf->files = calloc( 1, sizeof( inf->files[0] ) ); |
---|
675 | |
---|
676 | if( NULL == inf->files ) |
---|
677 | { |
---|
678 | return TR_EINVALID; |
---|
679 | } |
---|
680 | |
---|
681 | strlcat_utf8( inf->files[0].name, name->val.s.s, |
---|
682 | sizeof( inf->files[0].name ), TR_PATH_DELIMITER ); |
---|
683 | |
---|
684 | inf->files[0].length = length->val.i; |
---|
685 | inf->totalSize += length->val.i; |
---|
686 | } |
---|
687 | else |
---|
688 | { |
---|
689 | tr_err( "%s \"files\" entry and %s \"length\" entry", |
---|
690 | ( files ? "Invalid" : "Missing" ), |
---|
691 | ( length ? "invalid" : "missing" ) ); |
---|
692 | } |
---|
693 | |
---|
694 | return TR_OK; |
---|
695 | } |
---|