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