1 | /****************************************************************************** |
---|
2 | * $Id: metainfo.c 1162 2006-12-06 21:44:15Z livings124 $ |
---|
3 | * |
---|
4 | * Copyright (c) 2005-2006 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 "transmission.h" |
---|
26 | |
---|
27 | #define TORRENT_MAX_SIZE (5*1024*1024) |
---|
28 | |
---|
29 | /*********************************************************************** |
---|
30 | * Local prototypes |
---|
31 | **********************************************************************/ |
---|
32 | #define strcatUTF8( dst, src) _strcatUTF8( (dst), sizeof( dst ) - 1, (src) ) |
---|
33 | |
---|
34 | static void _strcatUTF8( char *, int, char * ); |
---|
35 | static int parseAnnounce( char * original, char * address, int * port, char * announce ); |
---|
36 | |
---|
37 | static int parseAnnounce( char * original, char * address, int * port, char * announce ) |
---|
38 | { |
---|
39 | char * colon, * slash; |
---|
40 | |
---|
41 | /* Skip spaces */ |
---|
42 | while( *original && *original == ' ' ) |
---|
43 | { |
---|
44 | original++; |
---|
45 | } |
---|
46 | |
---|
47 | /* Parse announce URL */ |
---|
48 | if( strncmp( original, "http://", 7 ) ) |
---|
49 | { |
---|
50 | return 0; |
---|
51 | } |
---|
52 | |
---|
53 | colon = strchr( original + 7, ':' ); |
---|
54 | slash = strchr( original + 7, '/' ); |
---|
55 | if( colon && colon < slash ) |
---|
56 | { |
---|
57 | memcpy( address, original + 7, (long) colon - (long) original - 7 ); |
---|
58 | *port = atoi( colon + 1 ); |
---|
59 | snprintf( announce, MAX_PATH_LENGTH, "%s", slash ); |
---|
60 | |
---|
61 | return 1; |
---|
62 | } |
---|
63 | else if( slash ) |
---|
64 | { |
---|
65 | memcpy( address, original + 7, (long) slash - (long) original - 7 ); |
---|
66 | *port = 80; |
---|
67 | snprintf( announce, MAX_PATH_LENGTH, "%s", slash ); |
---|
68 | |
---|
69 | return 1; |
---|
70 | } |
---|
71 | else |
---|
72 | { |
---|
73 | return 0; |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | /*********************************************************************** |
---|
78 | * tr_metainfoParse |
---|
79 | *********************************************************************** |
---|
80 | * |
---|
81 | **********************************************************************/ |
---|
82 | int tr_metainfoParse( tr_info_t * inf, const char * path, |
---|
83 | const char * savedHash, int saveCopy ) |
---|
84 | { |
---|
85 | FILE * file; |
---|
86 | char * buf; |
---|
87 | benc_val_t meta, * beInfo, * list, * val, * sublist; |
---|
88 | char * address, * announce; |
---|
89 | int i, j, k, tiersSet, tiers, inTier, port, random; |
---|
90 | struct stat sb; |
---|
91 | tr_announce_list_item_t * announceItem, * prevAnnounceItem, * nextAnnounceItem; |
---|
92 | |
---|
93 | assert( NULL == path || NULL == savedHash ); |
---|
94 | /* if savedHash isn't null, saveCopy should be false */ |
---|
95 | assert( NULL == savedHash || !saveCopy ); |
---|
96 | |
---|
97 | if ( NULL != savedHash ) |
---|
98 | { |
---|
99 | snprintf( inf->torrent, MAX_PATH_LENGTH, "%s/%s", |
---|
100 | tr_getTorrentsDirectory(), savedHash ); |
---|
101 | path = inf->torrent; |
---|
102 | } |
---|
103 | |
---|
104 | if( stat( path, &sb ) ) |
---|
105 | { |
---|
106 | tr_err( "Could not stat file (%s)", path ); |
---|
107 | return 1; |
---|
108 | } |
---|
109 | if( ( sb.st_mode & S_IFMT ) != S_IFREG ) |
---|
110 | { |
---|
111 | tr_err( "Not a regular file (%s)", path ); |
---|
112 | return 1; |
---|
113 | } |
---|
114 | if( sb.st_size > TORRENT_MAX_SIZE ) |
---|
115 | { |
---|
116 | tr_err( "Torrent file is too big (%d bytes)", (int)sb.st_size ); |
---|
117 | return 1; |
---|
118 | } |
---|
119 | |
---|
120 | /* Load the torrent file into our buffer */ |
---|
121 | file = fopen( path, "rb" ); |
---|
122 | if( !file ) |
---|
123 | { |
---|
124 | tr_err( "Could not open file (%s)", path ); |
---|
125 | return 1; |
---|
126 | } |
---|
127 | buf = malloc( sb.st_size ); |
---|
128 | fseek( file, 0, SEEK_SET ); |
---|
129 | if( fread( buf, sb.st_size, 1, file ) != 1 ) |
---|
130 | { |
---|
131 | tr_err( "Read error (%s)", path ); |
---|
132 | free( buf ); |
---|
133 | fclose( file ); |
---|
134 | return 1; |
---|
135 | } |
---|
136 | fclose( file ); |
---|
137 | |
---|
138 | /* Parse bencoded infos */ |
---|
139 | if( tr_bencLoad( buf, sb.st_size, &meta, NULL ) ) |
---|
140 | { |
---|
141 | tr_err( "Error while parsing bencoded data" ); |
---|
142 | free( buf ); |
---|
143 | return 1; |
---|
144 | } |
---|
145 | |
---|
146 | /* Get info hash */ |
---|
147 | if( !( beInfo = tr_bencDictFind( &meta, "info" ) ) ) |
---|
148 | { |
---|
149 | tr_err( "Could not find \"info\" dictionary" ); |
---|
150 | tr_bencFree( &meta ); |
---|
151 | free( buf ); |
---|
152 | return 1; |
---|
153 | } |
---|
154 | SHA1( (uint8_t *) beInfo->begin, |
---|
155 | (long) beInfo->end - (long) beInfo->begin, inf->hash ); |
---|
156 | for( i = 0; i < SHA_DIGEST_LENGTH; i++ ) |
---|
157 | { |
---|
158 | sprintf( inf->hashString + i * 2, "%02x", inf->hash[i] ); |
---|
159 | } |
---|
160 | |
---|
161 | if( saveCopy ) |
---|
162 | { |
---|
163 | /* Save a copy of the torrent file in the private torrent directory */ |
---|
164 | snprintf( inf->torrent, MAX_PATH_LENGTH, "%s/%s", |
---|
165 | tr_getTorrentsDirectory(), inf->hashString ); |
---|
166 | file = fopen( inf->torrent, "wb" ); |
---|
167 | if( !file ) |
---|
168 | { |
---|
169 | tr_err( "Could not open file (%s) (%s)", |
---|
170 | inf->torrent, strerror(errno) ); |
---|
171 | tr_bencFree( &meta ); |
---|
172 | free( buf ); |
---|
173 | return 1; |
---|
174 | } |
---|
175 | fseek( file, 0, SEEK_SET ); |
---|
176 | if( fwrite( buf, sb.st_size, 1, file ) != 1 ) |
---|
177 | { |
---|
178 | tr_err( "Write error (%s)", inf->torrent ); |
---|
179 | tr_bencFree( &meta ); |
---|
180 | free( buf ); |
---|
181 | fclose( file ); |
---|
182 | return 1; |
---|
183 | } |
---|
184 | fclose( file ); |
---|
185 | } |
---|
186 | else |
---|
187 | { |
---|
188 | snprintf( inf->torrent, MAX_PATH_LENGTH, "%s", path ); |
---|
189 | } |
---|
190 | |
---|
191 | /* We won't need this anymore */ |
---|
192 | free( buf ); |
---|
193 | |
---|
194 | address = calloc( sizeof( char ), 256 ); |
---|
195 | announce = calloc( sizeof( char ), MAX_PATH_LENGTH ); |
---|
196 | |
---|
197 | tiersSet = 0; |
---|
198 | if( ( val = tr_bencDictFind( &meta, "announce-list" ) ) ) |
---|
199 | { |
---|
200 | list = val->val.l.vals; |
---|
201 | |
---|
202 | inf->trackerAnnounceList = calloc( sizeof( int ), val->val.l.count ); |
---|
203 | tiersSet = 1; |
---|
204 | |
---|
205 | tiers = 0; |
---|
206 | for( i = 0; i < val->val.l.count; i++ ) |
---|
207 | { |
---|
208 | sublist = list[i].val.l.vals; |
---|
209 | |
---|
210 | inTier = 0; |
---|
211 | for( j = 0; j < list[i].val.l.count; j++ ) |
---|
212 | { |
---|
213 | if( !parseAnnounce( sublist[j].val.s.s, address, &port, announce ) ) |
---|
214 | { |
---|
215 | continue; |
---|
216 | } |
---|
217 | |
---|
218 | /* Shuffle order of sublist */ |
---|
219 | random = tr_rand( inTier+1 ); |
---|
220 | |
---|
221 | announceItem = calloc( sizeof( tr_announce_list_item_t ), 1 ); |
---|
222 | prevAnnounceItem = 0; |
---|
223 | nextAnnounceItem = inf->trackerAnnounceList[tiers]; |
---|
224 | for( k = 0; k < random; k++ ) |
---|
225 | { |
---|
226 | prevAnnounceItem = nextAnnounceItem; |
---|
227 | nextAnnounceItem = nextAnnounceItem->nextItem; |
---|
228 | } |
---|
229 | |
---|
230 | announceItem->nextItem = nextAnnounceItem; |
---|
231 | if( prevAnnounceItem ) |
---|
232 | { |
---|
233 | prevAnnounceItem->nextItem = announceItem; |
---|
234 | } |
---|
235 | else |
---|
236 | { |
---|
237 | inf->trackerAnnounceList[tiers] = announceItem; |
---|
238 | } |
---|
239 | |
---|
240 | /* Set values */ |
---|
241 | snprintf( announceItem->address, 256, "%s", address ); |
---|
242 | announceItem->port = port; |
---|
243 | snprintf( announceItem->announce, MAX_PATH_LENGTH, "%s", announce ); |
---|
244 | |
---|
245 | inTier++; |
---|
246 | } |
---|
247 | |
---|
248 | /* Only use tier if there are useable addresses */ |
---|
249 | if( inTier > 0 ) |
---|
250 | { |
---|
251 | tiers++; |
---|
252 | } |
---|
253 | } |
---|
254 | |
---|
255 | inf->trackerAnnounceTiers = tiers; |
---|
256 | } |
---|
257 | |
---|
258 | tr_err( "announce-list:" ); |
---|
259 | for( i = 0; i < inf->trackerAnnounceTiers; i++ ) |
---|
260 | { |
---|
261 | tr_err( "list %d:", i ); |
---|
262 | for (announceItem = inf->trackerAnnounceList[i]; announceItem != NULL; announceItem = announceItem->nextItem) |
---|
263 | { |
---|
264 | tr_err( "%s:%d%s", announceItem->address, announceItem->port, announceItem->announce ); |
---|
265 | } |
---|
266 | } |
---|
267 | |
---|
268 | /* Regular announce value */ |
---|
269 | if ( !inf->trackerAnnounceTiers ) |
---|
270 | { |
---|
271 | if( !( val = tr_bencDictFind( &meta, "announce" ) ) ) |
---|
272 | { |
---|
273 | tr_err( "No \"announce\" entry" ); |
---|
274 | free( address ); |
---|
275 | free( announce ); |
---|
276 | |
---|
277 | tr_bencFree( &meta ); |
---|
278 | return 1; |
---|
279 | } |
---|
280 | |
---|
281 | if ( !parseAnnounce( val->val.s.s, address, &port, announce ) ) |
---|
282 | { |
---|
283 | tr_err( "Invalid announce URL (%s)", val->val.s.s ); |
---|
284 | free( address ); |
---|
285 | free( announce ); |
---|
286 | |
---|
287 | tr_bencFree( &meta ); |
---|
288 | return 1; |
---|
289 | } |
---|
290 | |
---|
291 | if ( !tiersSet ) |
---|
292 | inf->trackerAnnounceList = calloc( sizeof( int ), 1 ); |
---|
293 | inf->trackerAnnounceList[0] = calloc( sizeof( tr_announce_list_item_t ), 1 ); |
---|
294 | |
---|
295 | inf->trackerAnnounceTiers = 1; |
---|
296 | snprintf( inf->trackerAnnounceList[0]->address, 256, "%s", address ); |
---|
297 | inf->trackerAnnounceList[0]->port = port; |
---|
298 | snprintf( inf->trackerAnnounceList[0]->announce, MAX_PATH_LENGTH, "%s", announce ); |
---|
299 | } |
---|
300 | |
---|
301 | tr_setTorrentAnnounce( inf, inf->trackerAnnounceList[0] ); |
---|
302 | |
---|
303 | free( address ); |
---|
304 | free( announce ); |
---|
305 | |
---|
306 | /* Comment info */ |
---|
307 | if( ( val = tr_bencDictFind( &meta, "comment.utf-8" ) ) || ( val = tr_bencDictFind( &meta, "comment" ) ) ) |
---|
308 | { |
---|
309 | strcatUTF8( inf->comment, val->val.s.s ); |
---|
310 | } |
---|
311 | |
---|
312 | /* Creator info */ |
---|
313 | if( ( val = tr_bencDictFind( &meta, "created by.utf-8" ) ) || ( val = tr_bencDictFind( &meta, "created by" ) ) ) |
---|
314 | { |
---|
315 | strcatUTF8( inf->creator, val->val.s.s ); |
---|
316 | } |
---|
317 | |
---|
318 | /* Date created */ |
---|
319 | if( ( val = tr_bencDictFind( &meta, "creation date" ) ) ) |
---|
320 | { |
---|
321 | inf->dateCreated = val->val.i; |
---|
322 | } |
---|
323 | else |
---|
324 | { |
---|
325 | inf->dateCreated = 0; |
---|
326 | } |
---|
327 | |
---|
328 | /* Piece length */ |
---|
329 | if( !( val = tr_bencDictFind( beInfo, "piece length" ) ) ) |
---|
330 | { |
---|
331 | tr_err( "No \"piece length\" entry" ); |
---|
332 | tr_bencFree( &meta ); |
---|
333 | return 1; |
---|
334 | } |
---|
335 | inf->pieceSize = val->val.i; |
---|
336 | |
---|
337 | /* Hashes */ |
---|
338 | val = tr_bencDictFind( beInfo, "pieces" ); |
---|
339 | if( val->val.s.i % SHA_DIGEST_LENGTH ) |
---|
340 | { |
---|
341 | tr_err( "Invalid \"piece\" string (size is %d)", val->val.s.i ); |
---|
342 | tr_bencFree( &meta ); |
---|
343 | return 1; |
---|
344 | } |
---|
345 | inf->pieceCount = val->val.s.i / SHA_DIGEST_LENGTH; |
---|
346 | inf->pieces = (uint8_t *) val->val.s.s; /* Ugly, but avoids a memcpy */ |
---|
347 | val->val.s.s = NULL; |
---|
348 | |
---|
349 | /* TODO add more tests so we don't crash on weird files */ |
---|
350 | |
---|
351 | inf->totalSize = 0; |
---|
352 | if( ( list = tr_bencDictFind( beInfo, "files" ) ) ) |
---|
353 | { |
---|
354 | /* Multi-file mode */ |
---|
355 | int j; |
---|
356 | |
---|
357 | val = tr_bencDictFind( beInfo, "name.utf-8" ); |
---|
358 | if( NULL == val ) |
---|
359 | { |
---|
360 | val = tr_bencDictFind( beInfo, "name" ); |
---|
361 | } |
---|
362 | strcatUTF8( inf->name, val->val.s.s ); |
---|
363 | |
---|
364 | inf->multifile = 1; |
---|
365 | inf->fileCount = list->val.l.count; |
---|
366 | inf->files = calloc( inf->fileCount * sizeof( tr_file_t ), 1 ); |
---|
367 | |
---|
368 | for( i = 0; i < list->val.l.count; i++ ) |
---|
369 | { |
---|
370 | val = tr_bencDictFind( &list->val.l.vals[i], "path.utf-8" ); |
---|
371 | if( NULL == val ) |
---|
372 | { |
---|
373 | val = tr_bencDictFind( &list->val.l.vals[i], "path" ); |
---|
374 | } |
---|
375 | strcatUTF8( inf->files[i].name, inf->name ); |
---|
376 | for( j = 0; j < val->val.l.count; j++ ) |
---|
377 | { |
---|
378 | strcatUTF8( inf->files[i].name, "/" ); |
---|
379 | strcatUTF8( inf->files[i].name, |
---|
380 | val->val.l.vals[j].val.s.s ); |
---|
381 | } |
---|
382 | val = tr_bencDictFind( &list->val.l.vals[i], "length" ); |
---|
383 | inf->files[i].length = val->val.i; |
---|
384 | inf->totalSize += val->val.i; |
---|
385 | } |
---|
386 | |
---|
387 | } |
---|
388 | else |
---|
389 | { |
---|
390 | /* Single-file mode */ |
---|
391 | inf->multifile = 0; |
---|
392 | inf->fileCount = 1; |
---|
393 | inf->files = calloc( sizeof( tr_file_t ), 1 ); |
---|
394 | |
---|
395 | val = tr_bencDictFind( beInfo, "name.utf-8" ); |
---|
396 | if( NULL == val ) |
---|
397 | { |
---|
398 | val = tr_bencDictFind( beInfo, "name" ); |
---|
399 | } |
---|
400 | strcatUTF8( inf->files[0].name, val->val.s.s ); |
---|
401 | strcatUTF8( inf->name, val->val.s.s ); |
---|
402 | |
---|
403 | val = tr_bencDictFind( beInfo, "length" ); |
---|
404 | inf->files[0].length = val->val.i; |
---|
405 | inf->totalSize += val->val.i; |
---|
406 | } |
---|
407 | |
---|
408 | if( (uint64_t) inf->pieceCount != |
---|
409 | ( inf->totalSize + inf->pieceSize - 1 ) / inf->pieceSize ) |
---|
410 | { |
---|
411 | tr_err( "Size of hashes and files don't match" ); |
---|
412 | free( inf->pieces ); |
---|
413 | tr_bencFree( &meta ); |
---|
414 | return 1; |
---|
415 | } |
---|
416 | |
---|
417 | tr_bencFree( &meta ); |
---|
418 | return 0; |
---|
419 | } |
---|
420 | |
---|
421 | void tr_metainfoRemoveSaved( const char * hashString ) |
---|
422 | { |
---|
423 | char file[MAX_PATH_LENGTH]; |
---|
424 | |
---|
425 | snprintf( file, MAX_PATH_LENGTH, "%s/%s", |
---|
426 | tr_getTorrentsDirectory(), hashString ); |
---|
427 | unlink(file); |
---|
428 | } |
---|
429 | |
---|
430 | /*********************************************************************** |
---|
431 | * strcatUTF8 |
---|
432 | *********************************************************************** |
---|
433 | * According to the official specification, all strings in the torrent |
---|
434 | * file are supposed to be UTF-8 encoded. However, there are |
---|
435 | * non-compliant torrents around... If we encounter an invalid UTF-8 |
---|
436 | * character, we assume it is ISO 8859-1 and convert it to UTF-8. |
---|
437 | **********************************************************************/ |
---|
438 | #define WANTBYTES( want, got ) \ |
---|
439 | if( (want) > (got) ) { return; } else { (got) -= (want); } |
---|
440 | static void _strcatUTF8( char * s, int len, char * append ) |
---|
441 | { |
---|
442 | char * p; |
---|
443 | |
---|
444 | /* Go to the end of the destination string */ |
---|
445 | while( s[0] ) |
---|
446 | { |
---|
447 | s++; |
---|
448 | len--; |
---|
449 | } |
---|
450 | |
---|
451 | /* Now start appending, converting on the fly if necessary */ |
---|
452 | for( p = append; p[0]; ) |
---|
453 | { |
---|
454 | if( !( p[0] & 0x80 ) ) |
---|
455 | { |
---|
456 | /* ASCII character */ |
---|
457 | WANTBYTES( 1, len ); |
---|
458 | *(s++) = *(p++); |
---|
459 | continue; |
---|
460 | } |
---|
461 | |
---|
462 | if( ( p[0] & 0xE0 ) == 0xC0 && ( p[1] & 0xC0 ) == 0x80 ) |
---|
463 | { |
---|
464 | /* 2-bytes UTF-8 character */ |
---|
465 | WANTBYTES( 2, len ); |
---|
466 | *(s++) = *(p++); *(s++) = *(p++); |
---|
467 | continue; |
---|
468 | } |
---|
469 | |
---|
470 | if( ( p[0] & 0xF0 ) == 0xE0 && ( p[1] & 0xC0 ) == 0x80 && |
---|
471 | ( p[2] & 0xC0 ) == 0x80 ) |
---|
472 | { |
---|
473 | /* 3-bytes UTF-8 character */ |
---|
474 | WANTBYTES( 3, len ); |
---|
475 | *(s++) = *(p++); *(s++) = *(p++); |
---|
476 | *(s++) = *(p++); |
---|
477 | continue; |
---|
478 | } |
---|
479 | |
---|
480 | if( ( p[0] & 0xF8 ) == 0xF0 && ( p[1] & 0xC0 ) == 0x80 && |
---|
481 | ( p[2] & 0xC0 ) == 0x80 && ( p[3] & 0xC0 ) == 0x80 ) |
---|
482 | { |
---|
483 | /* 4-bytes UTF-8 character */ |
---|
484 | WANTBYTES( 4, len ); |
---|
485 | *(s++) = *(p++); *(s++) = *(p++); |
---|
486 | *(s++) = *(p++); *(s++) = *(p++); |
---|
487 | continue; |
---|
488 | } |
---|
489 | |
---|
490 | /* ISO 8859-1 -> UTF-8 conversion */ |
---|
491 | WANTBYTES( 2, len ); |
---|
492 | *(s++) = 0xC0 | ( ( *p & 0xFF ) >> 6 ); |
---|
493 | *(s++) = 0x80 | ( *(p++) & 0x3F ); |
---|
494 | } |
---|
495 | } |
---|