Changeset 12024
- Timestamp:
- Feb 24, 2011, 3:01:26 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.2x/libtransmission/blocklist.c
r11709 r12024 12 12 13 13 #include <stdio.h> 14 #include <stdlib.h> /* free() */14 #include <stdlib.h> /* qsort(), free() */ 15 15 #include <string.h> 16 16 … … 47 47 ***/ 48 48 49 struct tr_ip _range49 struct tr_ipv4_range 50 50 { 51 51 uint32_t begin; … … 55 55 struct tr_blocklist 56 56 { 57 tr_bool isEnabled;58 int fd;59 size_t ruleCount;60 size_t byteCount;61 char * filename;62 struct tr_ip _range *rules;57 tr_bool isEnabled; 58 int fd; 59 size_t ruleCount; 60 size_t byteCount; 61 char * filename; 62 struct tr_ipv4_range * rules; 63 63 }; 64 64 … … 108 108 b->fd = fd; 109 109 b->byteCount = byteCount; 110 b->ruleCount = byteCount / sizeof( struct tr_ip _range );110 b->ruleCount = byteCount / sizeof( struct tr_ipv4_range ); 111 111 112 112 { … … 128 128 const void * vb ) 129 129 { 130 const uint32_t * a = va;131 const struct tr_ip _range * b = vb;130 const uint32_t * a = va; 131 const struct tr_ipv4_range * b = vb; 132 132 133 133 if( *a < b->begin ) return -1; … … 207 207 const tr_address * addr ) 208 208 { 209 uint32_t needle;210 const struct tr_ip _range * range;209 uint32_t needle; 210 const struct tr_ipv4_range * range; 211 211 212 212 assert( tr_isAddress( addr ) ); … … 225 225 b->rules, 226 226 b->ruleCount, 227 sizeof( struct tr_ip _range ),227 sizeof( struct tr_ipv4_range ), 228 228 compareAddressToRange ); 229 229 … … 237 237 */ 238 238 static tr_bool 239 parseLine1( const char * line, struct tr_ip _range * range )239 parseLine1( const char * line, struct tr_ipv4_range * range ) 240 240 { 241 241 char * walk; … … 273 273 */ 274 274 static tr_bool 275 parseLine2( const char * line, struct tr_ip _range * range )275 parseLine2( const char * line, struct tr_ipv4_range * range ) 276 276 { 277 277 int unk; … … 301 301 302 302 static int 303 parseLine( const char * line, struct tr_ip _range * range )303 parseLine( const char * line, struct tr_ipv4_range * range ) 304 304 { 305 305 return parseLine1( line, range ) … … 307 307 } 308 308 309 static int 310 compareAddressRangesByFirstAddress( const void * va, const void * vb ) 311 { 312 const struct tr_ipv4_range * a = va; 313 const struct tr_ipv4_range * b = vb; 314 if( a->begin != b->begin ) 315 return a->begin < b->begin ? -1 : 1; 316 return 0; 317 } 318 309 319 int 310 _tr_blocklistSetContent( tr_blocklist * b, 311 const char * filename ) 320 _tr_blocklistSetContent( tr_blocklist * b, const char * filename ) 312 321 { 313 322 FILE * in; 314 323 FILE * out; 315 324 int inCount = 0; 316 int outCount = 0;317 325 char line[2048]; 318 326 const char * err_fmt = _( "Couldn't read \"%1$s\": %2$s" ); 327 struct tr_ipv4_range * ranges = NULL; 328 size_t ranges_alloc = 0; 329 size_t ranges_count = 0; 319 330 320 331 if( !filename ) … … 341 352 } 342 353 354 /* load the rules into memory */ 343 355 while( fgets( line, sizeof( line ), in ) != NULL ) 344 356 { 345 357 char * walk; 346 struct tr_ip _range range;358 struct tr_ipv4_range range; 347 359 348 360 ++inCount; … … 359 371 } 360 372 361 if( fwrite( &range, sizeof( struct tr_ip_range ), 1, out ) != 1)373 if( ranges_alloc == ranges_count ) 362 374 { 363 tr_err( _( "Couldn't save file \"%1$s\": %2$s" ), b->filename, 364 tr_strerror( errno ) ); 365 break; 375 ranges_alloc += 4096; /* arbitrary */ 376 ranges = tr_renew( struct tr_ipv4_range, ranges, ranges_alloc ); 366 377 } 367 378 368 ++outCount; 369 } 370 371 { 379 ranges[ranges_count++] = range; 380 } 381 382 if( ranges_count > 0 ) /* sort and merge */ 383 { 384 struct tr_ipv4_range * r; 385 struct tr_ipv4_range * keep = ranges; 386 const struct tr_ipv4_range * end; 387 388 /* sort */ 389 qsort( ranges, ranges_count, sizeof( struct tr_ipv4_range ), 390 compareAddressRangesByFirstAddress ); 391 392 /* merge */ 393 for( r=ranges+1, end=ranges+ranges_count; r!=end; ++r ) { 394 if( keep->end < r->begin ) 395 *++keep = *r; 396 else if( keep->end < r->end ) 397 keep->end = r->end; 398 } 399 400 ranges_count = keep + 1 - ranges; 401 402 #ifndef NDEBUG 403 /* sanity checks: make sure the rules are sorted 404 * in ascending order and don't overlap */ 405 { 406 size_t i; 407 408 for( i=0; i<ranges_count; ++i ) 409 assert( ranges[i].begin <= ranges[i].end ); 410 411 for( i=1; i<ranges_count; ++i ) 412 assert( ranges[i-1].end < ranges[i].begin ); 413 } 414 #endif 415 } 416 417 if( fwrite( ranges, sizeof( struct tr_ipv4_range ), ranges_count, out ) != ranges_count ) 418 tr_err( _( "Couldn't save file \"%1$s\": %2$s" ), b->filename, tr_strerror( errno ) ); 419 else { 372 420 char * base = tr_basename( b->filename ); 373 tr_inf( _( "Blocklist \"%s\" updated with % d entries" ), base, outCount );421 tr_inf( _( "Blocklist \"%s\" updated with %zu entries" ), base, ranges_count ); 374 422 tr_free( base ); 375 423 } 376 424 425 tr_free( ranges ); 377 426 fclose( out ); 378 427 fclose( in ); … … 380 429 blocklistLoad( b ); 381 430 382 return outCount; 383 } 384 431 return ranges_count; 432 }
Note: See TracChangeset
for help on using the changeset viewer.