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