1 | /****************************************************************************** |
---|
2 | * $Id: metainfo.c 5065 2008-02-19 04:16:04Z 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, benc_val_t * meta ); |
---|
73 | static char * announceToScrape( const char * announce ); |
---|
74 | static int parseFiles( tr_info * inf, benc_val_t * name, |
---|
75 | benc_val_t * files, benc_val_t * 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 benc_val_t * meta_in, const char * tag ) |
---|
173 | { |
---|
174 | int i; |
---|
175 | benc_val_t * beInfo, * val, * val2; |
---|
176 | benc_val_t * meta = (benc_val_t *) 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 | for( i = 0; i < SHA_DIGEST_LENGTH; i++ ) |
---|
196 | { |
---|
197 | snprintf( inf->hashString + i * 2, sizeof( inf->hashString ) - i * 2, |
---|
198 | "%02x", inf->hash[i] ); |
---|
199 | } |
---|
200 | savedname( inf->torrent, sizeof( inf->torrent ), inf->hashString, tag ); |
---|
201 | |
---|
202 | /* comment */ |
---|
203 | memset( buf, '\0', sizeof( buf ) ); |
---|
204 | val = tr_bencDictFindFirst( meta, "comment.utf-8", "comment", NULL ); |
---|
205 | if( val && val->type == TYPE_STR ) |
---|
206 | strlcat_utf8( buf, val->val.s.s, sizeof( buf ), 0 ); |
---|
207 | tr_free( inf->comment ); |
---|
208 | inf->comment = tr_strdup( buf ); |
---|
209 | |
---|
210 | /* creator */ |
---|
211 | memset( buf, '\0', sizeof( buf ) ); |
---|
212 | val = tr_bencDictFindFirst( meta, "created by.utf-8", "created by", NULL ); |
---|
213 | if( val && val->type == TYPE_STR ) |
---|
214 | strlcat_utf8( buf, val->val.s.s, sizeof( buf ), 0 ); |
---|
215 | tr_free( inf->creator ); |
---|
216 | inf->creator = tr_strdup( buf ); |
---|
217 | |
---|
218 | /* Date created */ |
---|
219 | inf->dateCreated = 0; |
---|
220 | val = tr_bencDictFind( meta, "creation date" ); |
---|
221 | if( NULL != val && TYPE_INT == val->type ) |
---|
222 | { |
---|
223 | inf->dateCreated = val->val.i; |
---|
224 | } |
---|
225 | |
---|
226 | /* Private torrent */ |
---|
227 | val = tr_bencDictFind( beInfo, "private" ); |
---|
228 | val2 = tr_bencDictFind( meta, "private" ); |
---|
229 | if( ( NULL != val && ( TYPE_INT != val->type || 0 != val->val.i ) ) || |
---|
230 | ( NULL != val2 && ( TYPE_INT != val2->type || 0 != val2->val.i ) ) ) |
---|
231 | { |
---|
232 | inf->isPrivate = 1; |
---|
233 | } |
---|
234 | |
---|
235 | /* Piece length */ |
---|
236 | val = tr_bencDictFind( beInfo, "piece length" ); |
---|
237 | if( NULL == val || TYPE_INT != val->type ) |
---|
238 | { |
---|
239 | tr_err( "%s \"piece length\" entry", ( val ? "Invalid" : "Missing" ) ); |
---|
240 | goto fail; |
---|
241 | } |
---|
242 | inf->pieceSize = val->val.i; |
---|
243 | |
---|
244 | /* Hashes */ |
---|
245 | val = tr_bencDictFind( beInfo, "pieces" ); |
---|
246 | if( NULL == val || TYPE_STR != val->type ) |
---|
247 | { |
---|
248 | tr_err( "%s \"pieces\" entry", ( val ? "Invalid" : "Missing" ) ); |
---|
249 | goto fail; |
---|
250 | } |
---|
251 | if( val->val.s.i % SHA_DIGEST_LENGTH ) |
---|
252 | { |
---|
253 | tr_err( "Invalid \"piece\" string (size is %d)", val->val.s.i ); |
---|
254 | goto fail; |
---|
255 | } |
---|
256 | inf->pieceCount = val->val.s.i / SHA_DIGEST_LENGTH; |
---|
257 | |
---|
258 | inf->pieces = calloc ( inf->pieceCount, sizeof(tr_piece) ); |
---|
259 | |
---|
260 | for ( i=0; i<inf->pieceCount; ++i ) |
---|
261 | { |
---|
262 | memcpy (inf->pieces[i].hash, &val->val.s.s[i*SHA_DIGEST_LENGTH], SHA_DIGEST_LENGTH); |
---|
263 | } |
---|
264 | |
---|
265 | /* get file or top directory name */ |
---|
266 | val = tr_bencDictFindFirst( beInfo, "name.utf-8", "name", NULL ); |
---|
267 | if( parseFiles( inf, tr_bencDictFindFirst( beInfo, |
---|
268 | "name.utf-8", "name", NULL ), |
---|
269 | tr_bencDictFind( beInfo, "files" ), |
---|
270 | tr_bencDictFind( beInfo, "length" ) ) ) |
---|
271 | { |
---|
272 | goto fail; |
---|
273 | } |
---|
274 | |
---|
275 | if( !inf->fileCount ) |
---|
276 | { |
---|
277 | tr_err( "Torrent has no files." ); |
---|
278 | goto fail; |
---|
279 | } |
---|
280 | |
---|
281 | if( !inf->totalSize ) |
---|
282 | { |
---|
283 | tr_err( "Torrent is zero bytes long." ); |
---|
284 | goto fail; |
---|
285 | } |
---|
286 | |
---|
287 | /* TODO add more tests so we don't crash on weird files */ |
---|
288 | |
---|
289 | if( (uint64_t) inf->pieceCount != |
---|
290 | ( inf->totalSize + inf->pieceSize - 1 ) / inf->pieceSize ) |
---|
291 | { |
---|
292 | tr_err( "Size of hashes and files don't match" ); |
---|
293 | goto fail; |
---|
294 | } |
---|
295 | |
---|
296 | /* get announce or announce-list */ |
---|
297 | if( getannounce( inf, meta ) ) |
---|
298 | { |
---|
299 | goto fail; |
---|
300 | } |
---|
301 | |
---|
302 | return TR_OK; |
---|
303 | |
---|
304 | fail: |
---|
305 | tr_metainfoFree( inf ); |
---|
306 | return TR_EINVALID; |
---|
307 | } |
---|
308 | |
---|
309 | void tr_metainfoFree( tr_info * inf ) |
---|
310 | { |
---|
311 | int i, j; |
---|
312 | |
---|
313 | for( i=0; i<inf->fileCount; ++i ) |
---|
314 | tr_free( inf->files[i].name ); |
---|
315 | |
---|
316 | tr_free( inf->pieces ); |
---|
317 | tr_free( inf->files ); |
---|
318 | tr_free( inf->comment ); |
---|
319 | tr_free( inf->creator ); |
---|
320 | tr_free( inf->primaryAddress ); |
---|
321 | |
---|
322 | for( i=0; i<inf->trackerTiers; ++i ) { |
---|
323 | for( j=0; j<inf->trackerList[i].count; ++j ) |
---|
324 | tr_trackerInfoClear( &inf->trackerList[i].list[j] ); |
---|
325 | tr_free( inf->trackerList[i].list ); |
---|
326 | } |
---|
327 | tr_free( inf->trackerList ); |
---|
328 | |
---|
329 | memset( inf, '\0', sizeof(tr_info) ); |
---|
330 | } |
---|
331 | |
---|
332 | static int getfile( char ** setme, |
---|
333 | const char * prefix, benc_val_t * name ) |
---|
334 | { |
---|
335 | benc_val_t * dir; |
---|
336 | const char ** list; |
---|
337 | int ii, jj; |
---|
338 | char buf[4096]; |
---|
339 | |
---|
340 | if( TYPE_LIST != name->type ) |
---|
341 | { |
---|
342 | return TR_EINVALID; |
---|
343 | } |
---|
344 | |
---|
345 | list = calloc( name->val.l.count, sizeof( list[0] ) ); |
---|
346 | if( NULL == list ) |
---|
347 | { |
---|
348 | return TR_EINVALID; |
---|
349 | } |
---|
350 | |
---|
351 | for( ii = jj = 0; name->val.l.count > ii; ii++ ) |
---|
352 | { |
---|
353 | dir = &name->val.l.vals[ii]; |
---|
354 | if( TYPE_STR != dir->type ) |
---|
355 | { |
---|
356 | continue; |
---|
357 | } |
---|
358 | if( 0 == strcmp( "..", dir->val.s.s ) ) |
---|
359 | { |
---|
360 | if( 0 < jj ) |
---|
361 | { |
---|
362 | jj--; |
---|
363 | } |
---|
364 | } |
---|
365 | else if( 0 != strcmp( ".", dir->val.s.s ) ) |
---|
366 | { |
---|
367 | list[jj] = dir->val.s.s; |
---|
368 | jj++; |
---|
369 | } |
---|
370 | } |
---|
371 | |
---|
372 | if( 0 == jj ) |
---|
373 | { |
---|
374 | free( list ); |
---|
375 | return TR_EINVALID; |
---|
376 | } |
---|
377 | |
---|
378 | memset( buf, 0, sizeof( buf ) ); |
---|
379 | strlcat_utf8( buf, prefix, sizeof(buf), 0 ); |
---|
380 | for( ii = 0; jj > ii; ii++ ) |
---|
381 | { |
---|
382 | strlcat_utf8( buf, TR_PATH_DELIMITER_STR, sizeof(buf), 0 ); |
---|
383 | strlcat_utf8( buf, list[ii], sizeof(buf), TR_PATH_DELIMITER ); |
---|
384 | } |
---|
385 | free( list ); |
---|
386 | |
---|
387 | tr_free( *setme ); |
---|
388 | *setme = tr_strdup( buf ); |
---|
389 | |
---|
390 | return TR_OK; |
---|
391 | } |
---|
392 | |
---|
393 | static int getannounce( tr_info * inf, benc_val_t * meta ) |
---|
394 | { |
---|
395 | benc_val_t * val, * subval, * urlval; |
---|
396 | char * address, * announce; |
---|
397 | int ii, jj, port, random, subcount; |
---|
398 | tr_tracker_info * sublist; |
---|
399 | void * swapping; |
---|
400 | |
---|
401 | /* Announce-list */ |
---|
402 | val = tr_bencDictFind( meta, "announce-list" ); |
---|
403 | if( NULL != val && TYPE_LIST == val->type && 0 < val->val.l.count ) |
---|
404 | { |
---|
405 | inf->trackerTiers = 0; |
---|
406 | inf->trackerList = calloc( val->val.l.count, |
---|
407 | sizeof( inf->trackerList[0] ) ); |
---|
408 | |
---|
409 | /* iterate through the announce-list's tiers */ |
---|
410 | for( ii = 0; ii < val->val.l.count; ii++ ) |
---|
411 | { |
---|
412 | subval = &val->val.l.vals[ii]; |
---|
413 | if( TYPE_LIST != subval->type || 0 >= subval->val.l.count ) |
---|
414 | { |
---|
415 | continue; |
---|
416 | } |
---|
417 | subcount = 0; |
---|
418 | sublist = calloc( subval->val.l.count, sizeof( sublist[0] ) ); |
---|
419 | |
---|
420 | /* iterate through the tier's items */ |
---|
421 | for( jj = 0; jj < subval->val.l.count; jj++ ) |
---|
422 | { |
---|
423 | tr_tracker_info tmp; |
---|
424 | |
---|
425 | urlval = &subval->val.l.vals[jj]; |
---|
426 | if( TYPE_STR != urlval->type || |
---|
427 | tr_trackerInfoInit( &tmp, urlval->val.s.s, urlval->val.s.i ) ) |
---|
428 | { |
---|
429 | continue; |
---|
430 | } |
---|
431 | |
---|
432 | if( !inf->primaryAddress ) { |
---|
433 | char buf[1024]; |
---|
434 | snprintf( buf, sizeof(buf), "%s:%d", tmp.address, tmp.port ); |
---|
435 | inf->primaryAddress = tr_strdup( buf ); |
---|
436 | } |
---|
437 | |
---|
438 | /* place the item info in a random location in the sublist */ |
---|
439 | random = tr_rand( subcount + 1 ); |
---|
440 | if( random != subcount ) |
---|
441 | sublist[subcount] = sublist[random]; |
---|
442 | sublist[random] = tmp; |
---|
443 | subcount++; |
---|
444 | } |
---|
445 | |
---|
446 | /* just use sublist as-is if it's full */ |
---|
447 | if( subcount == subval->val.l.count ) |
---|
448 | { |
---|
449 | inf->trackerList[inf->trackerTiers].list = sublist; |
---|
450 | inf->trackerList[inf->trackerTiers].count = subcount; |
---|
451 | inf->trackerTiers++; |
---|
452 | } |
---|
453 | /* if we skipped some of the tier's items then trim the sublist */ |
---|
454 | else if( 0 < subcount ) |
---|
455 | { |
---|
456 | inf->trackerList[inf->trackerTiers].list = calloc( subcount, sizeof( sublist[0] ) ); |
---|
457 | memcpy( inf->trackerList[inf->trackerTiers].list, sublist, |
---|
458 | sizeof( sublist[0] ) * subcount ); |
---|
459 | inf->trackerList[inf->trackerTiers].count = subcount; |
---|
460 | inf->trackerTiers++; |
---|
461 | free( sublist ); |
---|
462 | } |
---|
463 | /* drop the whole sublist if we didn't use any items at all */ |
---|
464 | else |
---|
465 | { |
---|
466 | free( sublist ); |
---|
467 | } |
---|
468 | } |
---|
469 | |
---|
470 | /* did we use any of the tiers? */ |
---|
471 | if( 0 == inf->trackerTiers ) |
---|
472 | { |
---|
473 | tr_inf( "No valid entries in \"announce-list\"" ); |
---|
474 | free( inf->trackerList ); |
---|
475 | inf->trackerList = NULL; |
---|
476 | } |
---|
477 | /* trim unused sublist pointers */ |
---|
478 | else if( inf->trackerTiers < val->val.l.count ) |
---|
479 | { |
---|
480 | swapping = inf->trackerList; |
---|
481 | inf->trackerList = calloc( inf->trackerTiers, |
---|
482 | sizeof( inf->trackerList[0] ) ); |
---|
483 | memcpy( inf->trackerList, swapping, |
---|
484 | sizeof( inf->trackerList[0] ) * inf->trackerTiers ); |
---|
485 | free( swapping ); |
---|
486 | } |
---|
487 | } |
---|
488 | |
---|
489 | /* Regular announce value */ |
---|
490 | val = tr_bencDictFind( meta, "announce" ); |
---|
491 | if( NULL == val || TYPE_STR != val->type ) |
---|
492 | { |
---|
493 | tr_err( "No \"announce\" entry" ); |
---|
494 | return TR_EINVALID; |
---|
495 | } |
---|
496 | |
---|
497 | if( !inf->trackerTiers ) |
---|
498 | { |
---|
499 | char buf[4096], *pch; |
---|
500 | strlcpy( buf, val->val.s.s, sizeof( buf ) ); |
---|
501 | pch = buf; |
---|
502 | while( isspace( *pch ) ) |
---|
503 | ++pch; |
---|
504 | |
---|
505 | if( tr_httpParseUrl( pch, -1, &address, &port, &announce ) ) |
---|
506 | { |
---|
507 | tr_err( "Invalid announce URL (%s)", val->val.s.s ); |
---|
508 | return TR_EINVALID; |
---|
509 | } |
---|
510 | sublist = calloc( 1, sizeof( sublist[0] ) ); |
---|
511 | sublist[0].address = address; |
---|
512 | sublist[0].port = port; |
---|
513 | sublist[0].announce = announce; |
---|
514 | sublist[0].scrape = announceToScrape( announce ); |
---|
515 | inf->trackerList = calloc( 1, sizeof( inf->trackerList[0] ) ); |
---|
516 | inf->trackerList[0].list = sublist; |
---|
517 | inf->trackerList[0].count = 1; |
---|
518 | inf->trackerTiers = 1; |
---|
519 | |
---|
520 | if( !inf->primaryAddress ) { |
---|
521 | char buf[1024]; |
---|
522 | snprintf( buf, sizeof(buf), "%s:%d", sublist[0].address, sublist[0].port ); |
---|
523 | inf->primaryAddress = tr_strdup( buf ); |
---|
524 | } |
---|
525 | |
---|
526 | } |
---|
527 | |
---|
528 | return TR_OK; |
---|
529 | } |
---|
530 | |
---|
531 | static char * announceToScrape( const char * announce ) |
---|
532 | { |
---|
533 | char old[] = "announce"; |
---|
534 | int oldlen = 8; |
---|
535 | char new[] = "scrape"; |
---|
536 | int newlen = 6; |
---|
537 | char * slash, * scrape; |
---|
538 | size_t scrapelen, used; |
---|
539 | |
---|
540 | slash = strrchr( announce, '/' ); |
---|
541 | if( NULL == slash ) |
---|
542 | { |
---|
543 | return NULL; |
---|
544 | } |
---|
545 | slash++; |
---|
546 | |
---|
547 | if( 0 != strncmp( slash, old, oldlen ) ) |
---|
548 | { |
---|
549 | return NULL; |
---|
550 | } |
---|
551 | |
---|
552 | scrapelen = strlen( announce ) - oldlen + newlen; |
---|
553 | scrape = calloc( scrapelen + 1, 1 ); |
---|
554 | if( NULL == scrape ) |
---|
555 | { |
---|
556 | return NULL; |
---|
557 | } |
---|
558 | assert( ( size_t )( slash - announce ) < scrapelen ); |
---|
559 | memcpy( scrape, announce, slash - announce ); |
---|
560 | used = slash - announce; |
---|
561 | strncat( scrape, new, scrapelen - used ); |
---|
562 | used += newlen; |
---|
563 | assert( strlen( scrape ) == used ); |
---|
564 | if( used < scrapelen ) |
---|
565 | { |
---|
566 | assert( strlen( slash + oldlen ) == scrapelen - used ); |
---|
567 | strncat( scrape, slash + oldlen, scrapelen - used ); |
---|
568 | } |
---|
569 | |
---|
570 | return scrape; |
---|
571 | } |
---|
572 | |
---|
573 | int |
---|
574 | tr_trackerInfoInit( tr_tracker_info * info, |
---|
575 | const char * address, |
---|
576 | int address_len ) |
---|
577 | { |
---|
578 | int ret = tr_httpParseUrl( address, address_len, |
---|
579 | &info->address, |
---|
580 | &info->port, |
---|
581 | &info->announce ); |
---|
582 | if( !ret ) |
---|
583 | info->scrape = announceToScrape( info->announce ); |
---|
584 | |
---|
585 | return ret; |
---|
586 | } |
---|
587 | |
---|
588 | void |
---|
589 | tr_trackerInfoClear( tr_tracker_info * info ) |
---|
590 | { |
---|
591 | tr_free( info->address ); |
---|
592 | tr_free( info->announce ); |
---|
593 | tr_free( info->scrape ); |
---|
594 | memset( info, '\0', sizeof(tr_tracker_info) ); |
---|
595 | } |
---|
596 | |
---|
597 | void |
---|
598 | tr_metainfoRemoveSaved( const char * hashString, const char * tag ) |
---|
599 | { |
---|
600 | char file[MAX_PATH_LENGTH]; |
---|
601 | savedname( file, sizeof file, hashString, tag ); |
---|
602 | unlink( file ); |
---|
603 | } |
---|
604 | |
---|
605 | /* Save a copy of the torrent file in the saved torrent directory */ |
---|
606 | int |
---|
607 | tr_metainfoSave( const char * hash, const char * tag, |
---|
608 | const uint8_t * buf, size_t buflen ) |
---|
609 | { |
---|
610 | char path[MAX_PATH_LENGTH]; |
---|
611 | FILE * file; |
---|
612 | |
---|
613 | savedname( path, sizeof path, hash, tag ); |
---|
614 | file = fopen( path, "wb+" ); |
---|
615 | if( !file ) |
---|
616 | { |
---|
617 | tr_err( "Could not open file (%s) (%s)", path, tr_strerror( errno ) ); |
---|
618 | return TR_EINVALID; |
---|
619 | } |
---|
620 | fseek( file, 0, SEEK_SET ); |
---|
621 | if( fwrite( buf, 1, buflen, file ) != buflen ) |
---|
622 | { |
---|
623 | tr_err( "Could not write file (%s) (%s)", path, tr_strerror( errno ) ); |
---|
624 | fclose( file ); |
---|
625 | return TR_EINVALID; |
---|
626 | } |
---|
627 | fclose( file ); |
---|
628 | |
---|
629 | return TR_OK; |
---|
630 | } |
---|
631 | |
---|
632 | static int |
---|
633 | parseFiles( tr_info * inf, benc_val_t * name, |
---|
634 | benc_val_t * files, benc_val_t * length ) |
---|
635 | { |
---|
636 | benc_val_t * item, * path; |
---|
637 | int ii; |
---|
638 | |
---|
639 | if( NULL == name || TYPE_STR != name->type ) |
---|
640 | { |
---|
641 | tr_err( "%s \"name\" string", ( name ? "Invalid" : "Missing" ) ); |
---|
642 | return TR_EINVALID; |
---|
643 | } |
---|
644 | |
---|
645 | strlcat_utf8( inf->name, name->val.s.s, sizeof( inf->name ), |
---|
646 | TR_PATH_DELIMITER ); |
---|
647 | if( '\0' == inf->name[0] ) |
---|
648 | { |
---|
649 | tr_err( "Invalid \"name\" string" ); |
---|
650 | return TR_EINVALID; |
---|
651 | } |
---|
652 | inf->totalSize = 0; |
---|
653 | |
---|
654 | if( files && TYPE_LIST == files->type ) |
---|
655 | { |
---|
656 | /* Multi-file mode */ |
---|
657 | inf->isMultifile = 1; |
---|
658 | inf->fileCount = files->val.l.count; |
---|
659 | inf->files = calloc( inf->fileCount, sizeof( inf->files[0] ) ); |
---|
660 | |
---|
661 | if( !inf->files ) |
---|
662 | return TR_EINVALID; |
---|
663 | |
---|
664 | for( ii = 0; files->val.l.count > ii; ii++ ) |
---|
665 | { |
---|
666 | item = &files->val.l.vals[ii]; |
---|
667 | path = tr_bencDictFindFirst( item, "path.utf-8", "path", NULL ); |
---|
668 | if( getfile( &inf->files[ii].name, inf->name, path ) ) |
---|
669 | { |
---|
670 | tr_err( "%s \"path\" entry", |
---|
671 | ( path ? "Invalid" : "Missing" ) ); |
---|
672 | return TR_EINVALID; |
---|
673 | } |
---|
674 | length = tr_bencDictFind( item, "length" ); |
---|
675 | if( NULL == length || TYPE_INT != length->type ) |
---|
676 | { |
---|
677 | tr_err( "%s \"length\" entry", |
---|
678 | ( length ? "Invalid" : "Missing" ) ); |
---|
679 | return TR_EINVALID; |
---|
680 | } |
---|
681 | inf->files[ii].length = length->val.i; |
---|
682 | inf->totalSize += length->val.i; |
---|
683 | } |
---|
684 | } |
---|
685 | else if( NULL != length && TYPE_INT == length->type ) |
---|
686 | { |
---|
687 | char buf[4096]; |
---|
688 | |
---|
689 | /* Single-file mode */ |
---|
690 | inf->isMultifile = 0; |
---|
691 | inf->fileCount = 1; |
---|
692 | inf->files = calloc( 1, sizeof( inf->files[0] ) ); |
---|
693 | |
---|
694 | if( !inf->files ) |
---|
695 | return TR_EINVALID; |
---|
696 | |
---|
697 | memset( buf, 0, sizeof( buf ) ); |
---|
698 | strlcat_utf8( buf, name->val.s.s, sizeof(buf), TR_PATH_DELIMITER ); |
---|
699 | tr_free( inf->files[0].name ); |
---|
700 | inf->files[0].name = tr_strdup( buf ); |
---|
701 | |
---|
702 | inf->files[0].length = length->val.i; |
---|
703 | inf->totalSize += length->val.i; |
---|
704 | } |
---|
705 | else |
---|
706 | { |
---|
707 | tr_err( "%s \"files\" entry and %s \"length\" entry", |
---|
708 | ( files ? "Invalid" : "Missing" ), |
---|
709 | ( length ? "invalid" : "missing" ) ); |
---|
710 | } |
---|
711 | |
---|
712 | return TR_OK; |
---|
713 | } |
---|