Changeset 6140
- Timestamp:
- Jun 11, 2008, 4:15:45 PM (15 years ago)
- Location:
- trunk/libtransmission
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/blocklist.c
r5959 r6140 15 15 #include <string.h> 16 16 17 #include <libgen.h> /* basename */ 17 18 #include <sys/mman.h> 18 19 #include <sys/types.h> … … 90 91 b->ruleCount = st.st_size / sizeof( struct tr_ip_range ); 91 92 b->fd = fd; 92 tr_inf( _( "Blocklist contains %'u entries" ), (unsigned int)b->ruleCount ); 93 94 { 95 char * name; 96 char buf[MAX_PATH_LENGTH]; 97 tr_strlcpy( buf, b->filename, sizeof( buf ) ); 98 name = basename( buf ); 99 tr_inf( _( "Blocklist \"%s\" contains %'u entries" ), name, (unsigned int)b->ruleCount ); 100 } 93 101 } 94 102 … … 134 142 } 135 143 144 const char* 145 _tr_blocklistGetFilename( const tr_blocklist * b ) 146 { 147 return b->filename; 148 } 149 136 150 void 137 151 _tr_blocklistFree( tr_blocklist * b ) … … 143 157 144 158 int 145 _tr_blocklistGetRuleCount( tr_blocklist * b ) 146 { 147 blocklistEnsureLoaded( b ); 159 _tr_blocklistExists( const tr_blocklist * b ) 160 { 161 struct stat st; 162 return !stat( b->filename, &st ); 163 } 164 165 int 166 _tr_blocklistGetRuleCount( const tr_blocklist * b ) 167 { 168 blocklistEnsureLoaded( (tr_blocklist*)b ); 148 169 149 170 return b->ruleCount; … … 187 208 188 209 int 189 _tr_blocklistExists( const tr_blocklist * b )190 {191 struct stat st;192 return !stat( b->filename, &st );193 }194 195 int196 210 _tr_blocklistSetContent( tr_blocklist * b, 197 211 const char * filename ) … … 256 270 } 257 271 258 tr_inf( _( "Blocklist updated with %'d entries" ), lineCount ); 272 { 273 char * name; 274 char buf[MAX_PATH_LENGTH]; 275 tr_strlcpy( buf, b->filename, sizeof( buf ) ); 276 name = basename( buf ); 277 tr_inf( _( "Blocklist \"%s\" updated with %'d entries" ), name, lineCount ); 278 } 279 259 280 260 281 fclose( out ); -
trunk/libtransmission/blocklist.h
r5959 r6140 18 18 19 19 tr_blocklist* _tr_blocklistNew ( const char * filename, int isEnabled ); 20 void _tr_blocklistFree ( tr_blocklist * b ); 21 int _tr_blocklistGetRuleCount( tr_blocklist * b ); 22 int _tr_blocklistIsEnabled ( tr_blocklist * b ); 23 void _tr_blocklistSetEnabled ( tr_blocklist * b, int isEnabled ); 24 int _tr_blocklistHasAddress ( tr_blocklist * b, const struct in_addr * addr ); 25 int _tr_blocklistExists ( const tr_blocklist * b ); 26 int _tr_blocklistSetContent ( tr_blocklist * b, const char * filename ); 20 int _tr_blocklistExists ( const tr_blocklist * ); 21 const char* _tr_blocklistGetFilename ( const tr_blocklist * ); 22 int _tr_blocklistGetRuleCount( const tr_blocklist * ); 23 void _tr_blocklistFree ( tr_blocklist * ); 24 int _tr_blocklistIsEnabled ( tr_blocklist * ); 25 void _tr_blocklistSetEnabled ( tr_blocklist *, int isEnabled ); 26 int _tr_blocklistHasAddress ( tr_blocklist *, const struct in_addr * addr ); 27 int _tr_blocklistSetContent ( tr_blocklist *, const char * filename ); 27 28 28 29 #endif -
trunk/libtransmission/peer-mgr.c
r6115 r6140 1016 1016 managerLock( manager ); 1017 1017 1018 if( tr_ blocklistHasAddress( manager->handle, addr ) )1018 if( tr_sessionIsAddressBlocked( manager->handle, addr ) ) 1019 1019 { 1020 1020 tr_dbg( "Banned IP address \"%s\" tried to connect to us", … … 1054 1054 1055 1055 t = getExistingTorrent( manager, torrentHash ); 1056 if( !tr_ blocklistHasAddress( t->manager->handle, &pex->in_addr ) )1056 if( !tr_sessionIsAddressBlocked( t->manager->handle, &pex->in_addr ) ) 1057 1057 ensureAtomExists( t, &pex->in_addr, pex->port, pex->flags, from ); 1058 1058 … … 1800 1800 1801 1801 /* Don't connect to peers in our blocklist */ 1802 if( tr_ blocklistHasAddress( t->manager->handle, &atom->addr ) )1802 if( tr_sessionIsAddressBlocked( t->manager->handle, &atom->addr ) ) 1803 1803 continue; 1804 1804 -
trunk/libtransmission/rpc-test.c
r6139 r6140 31 31 int ok; 32 32 char * str = cidrize( in ); 33 fprintf( stderr, "in [%s] out [%s] should be [%s]\n", in, str, expected ); 33 /* fprintf( stderr, "in [%s] out [%s] should be [%s]\n", in, str, expected ); */ 34 34 ok = expected ? !strcmp( expected, str ) : !str; 35 35 tr_free( str ); -
trunk/libtransmission/session.c
r6120 r6140 111 111 112 112 session->encryptionMode = mode; 113 } 114 115 /*** 116 **** 117 ***/ 118 119 static void 120 loadBlocklists( tr_session * session ) 121 { 122 int binCount = 0; 123 int newCount = 0; 124 struct stat sb; 125 char dirname[MAX_PATH_LENGTH]; 126 DIR * odir = NULL; 127 tr_list * list = NULL; 128 const int isEnabled = session->isBlocklistEnabled; 129 130 /* walk through the directory and find blocklists */ 131 tr_buildPath( dirname, sizeof( dirname ), session->configDir, "blocklists", NULL ); 132 if( !stat( dirname, &sb ) && S_ISDIR( sb.st_mode ) && (( odir = opendir( dirname )))) 133 { 134 struct dirent *d; 135 for( d=readdir( odir ); d; d=readdir( odir ) ) 136 { 137 char filename[MAX_PATH_LENGTH]; 138 139 if( !d->d_name || d->d_name[0]=='.' ) /* skip dotfiles, ., and .. */ 140 continue; 141 142 tr_buildPath( filename, sizeof(filename), dirname, d->d_name, NULL ); 143 144 if( tr_stringEndsWith( filename, ".bin" ) ) 145 { 146 /* if we don't already have this blocklist, add it */ 147 if( !tr_list_find( list, filename, (TrListCompareFunc)strcmp ) ) 148 { 149 tr_list_append( &list, _tr_blocklistNew( filename, isEnabled ) ); 150 ++binCount; 151 } 152 } 153 else 154 { 155 /* strip out the file suffix, if there is one, and add ".bin" instead */ 156 tr_blocklist * b; 157 const char * dot = strrchr( d->d_name, '.' ); 158 const int len = dot ? dot - d->d_name : (int)strlen( d->d_name ); 159 char tmp[MAX_PATH_LENGTH]; 160 snprintf( tmp, sizeof( tmp ), 161 "%s%c%*.*s.bin", dirname, TR_PATH_DELIMITER, len, len, d->d_name ); 162 b = _tr_blocklistNew( tmp, isEnabled ); 163 _tr_blocklistSetContent( b, filename ); 164 tr_list_append( &list, b ); 165 ++newCount; 166 } 167 } 168 169 closedir( odir ); 170 } 171 172 session->blocklists = list; 173 174 if( binCount ) 175 tr_dbg( "Found %d blocklists in \"%s\"", binCount, dirname ); 176 if( newCount ) 177 tr_dbg( "Found %d new blocklists in \"%s\"", newCount, dirname ); 113 178 } 114 179 … … 207 272 tr_buildPath( filename, sizeof( filename ), h->configDir, "blocklists", NULL ); 208 273 tr_mkdirp( filename, 0777 ); 209 tr_buildPath( filename, sizeof( filename ), h->configDir, "blocklists", "level1.bin", NULL );210 h->blocklist = _tr_blocklistNew( filename, isBlocklistEnabled);274 h->isBlocklistEnabled = isBlocklistEnabled; 275 loadBlocklists( h ); 211 276 212 277 tr_statsInit( h ); … … 486 551 tr_wait( 100 ); 487 552 488 _tr_blocklistFree( h->blocklist ); 489 h->blocklist = NULL; 553 tr_list_free( &h->blocklists, (TrListForeachFunc)_tr_blocklistFree ); 490 554 tr_webClose( &h->web ); 491 555 … … 536 600 char filename[MAX_PATH_LENGTH]; 537 601 tr_buildPath( filename, sizeof(filename), dirname, d->d_name, NULL ); 538 tr_ctorSetMetainfoFromFile( ctor, filename ); 539 tor = tr_torrentNew( h, ctor, NULL ); 540 if( tor ) { 541 tr_list_append( &list, tor ); 542 n++; 602 if( tr_stringEndsWith( filename, ".torrent" ) ) 603 { 604 tr_ctorSetMetainfoFromFile( ctor, filename ); 605 tor = tr_torrentNew( h, ctor, NULL ); 606 if( tor ) { 607 tr_list_append( &list, tor ); 608 ++n; 609 } 543 610 } 544 611 } … … 601 668 602 669 int 603 tr_blocklistGetRuleCount( tr_handle * handle ) 604 { 605 return _tr_blocklistGetRuleCount( handle->blocklist ); 606 } 607 608 int 609 tr_blocklistIsEnabled( const tr_handle * handle ) 610 { 611 return _tr_blocklistIsEnabled( handle->blocklist ); 612 } 613 614 void 615 tr_blocklistSetEnabled( tr_handle * handle, int isEnabled ) 616 { 617 _tr_blocklistSetEnabled( handle->blocklist, isEnabled ); 618 } 619 620 int 621 tr_blocklistExists( const tr_handle * handle ) 622 { 623 return _tr_blocklistExists( handle->blocklist ); 624 } 625 626 int 627 tr_blocklistSetContent( tr_handle * handle, const char * filename ) 628 { 629 return _tr_blocklistSetContent( handle->blocklist, filename ); 630 } 631 632 int 633 tr_blocklistHasAddress( tr_handle * handle, const struct in_addr * addr ) 634 { 635 return _tr_blocklistHasAddress( handle->blocklist, addr ); 670 tr_blocklistGetRuleCount( const tr_session * session ) 671 { 672 int n = 0; 673 tr_list * l; 674 for( l=session->blocklists; l; l=l->next ) 675 n += _tr_blocklistGetRuleCount( l->data ); 676 return n; 677 } 678 679 int 680 tr_blocklistIsEnabled( const tr_session * session ) 681 { 682 return session->isBlocklistEnabled; 683 } 684 685 void 686 tr_blocklistSetEnabled( tr_session * session, int isEnabled ) 687 { 688 tr_list * l; 689 session->isBlocklistEnabled = isEnabled ? 1 : 0; 690 for( l=session->blocklists; l; l=l->next ) 691 _tr_blocklistSetEnabled( l->data, isEnabled ); 692 } 693 694 int 695 tr_blocklistExists( const tr_session * session ) 696 { 697 return session->blocklists != NULL; 698 } 699 700 int 701 tr_blocklistSetContent( tr_session * session, const char * contentFilename ) 702 { 703 tr_list * l; 704 tr_blocklist * b; 705 const char * defaultName = "level1.bin"; 706 707 for( b=NULL, l=session->blocklists; !b && l; l=l->next ) 708 if( tr_stringEndsWith( _tr_blocklistGetFilename( l->data ), defaultName ) ) 709 b = l->data; 710 711 if( !b ) { 712 char filename[MAX_PATH_LENGTH]; 713 tr_buildPath( filename, sizeof( filename ), session->configDir, "blocklists", defaultName, NULL ); 714 b = _tr_blocklistNew( filename, session->isBlocklistEnabled ); 715 tr_list_append( &session->blocklists, b ); 716 } 717 718 return _tr_blocklistSetContent( b, contentFilename ); 719 } 720 721 int 722 tr_sessionIsAddressBlocked( const tr_session * session, 723 const struct in_addr * addr ) 724 { 725 tr_list * l; 726 for( l=session->blocklists; l; l=l->next ) 727 if( _tr_blocklistHasAddress( l->data, addr ) ) 728 return TRUE; 729 return FALSE; 636 730 } 637 731 -
trunk/libtransmission/session.h
r6120 r6140 52 52 unsigned int isPortSet : 1; 53 53 unsigned int isPexEnabled : 1; 54 unsigned int isBlocklistEnabled : 1; 54 55 unsigned int isProxyEnabled : 1; 55 56 unsigned int isProxyAuthEnabled : 1; … … 81 82 struct tr_ratecontrol * download; 82 83 83 struct tr_ blocklist * blocklist;84 struct tr_list * blocklists; 84 85 struct tr_peerMgr * peerMgr; 85 86 struct tr_shared * shared; … … 107 108 const char * filename ); 108 109 110 struct in_addr; 111 112 int tr_sessionIsAddressBlocked( const tr_session * session, 113 const struct in_addr * addr ); 114 115 109 116 void tr_globalLock ( tr_session * ); 110 117 void tr_globalUnlock ( tr_session * ); -
trunk/libtransmission/transmission.h
r6120 r6140 606 606 * Passing NULL for a filename will clear the blocklist. 607 607 */ 608 int tr_blocklistSetContent( tr_handle * handle, 609 const char * filename ); 610 611 int tr_blocklistGetRuleCount( tr_handle * handle ); 612 613 int tr_blocklistExists( const tr_handle * handle ); 614 615 int tr_blocklistIsEnabled( const tr_handle * handle ); 616 617 void tr_blocklistSetEnabled( tr_handle * handle, 618 int isEnabled ); 619 620 struct in_addr; 621 622 int tr_blocklistHasAddress( tr_handle * handle, 623 const struct in_addr * addr); 608 int tr_blocklistSetContent ( tr_session * session, 609 const char * filename ); 610 611 int tr_blocklistGetRuleCount ( const tr_session * session ); 612 613 int tr_blocklistExists ( const tr_session * session ); 614 615 int tr_blocklistIsEnabled ( const tr_session * session ); 616 617 void tr_blocklistSetEnabled ( tr_session * session, 618 int isEnabled ); 619 624 620 625 621 /** @} */ -
trunk/libtransmission/utils-test.c
r6101 r6140 94 94 tr_free( out ); 95 95 96 /* tr_stringEndsWith */ 97 check( tr_stringEndsWith( "the", "the" ) ); 98 check( tr_stringEndsWith( "dress", "dress" ) ); 99 check( tr_stringEndsWith( "address", "dress" ) ); 100 check( !tr_stringEndsWith( "foo.bin", "gfoo.bin" ) ); 101 check( !tr_stringEndsWith( "xyz", "xyw" ) ); 102 96 103 /* simple bitfield tests */ 97 104 for( l=0; l<NUM_LOOPS; ++l ) -
trunk/libtransmission/utils.c
r6101 r6140 922 922 ***/ 923 923 924 int 925 tr_stringEndsWith( const char * str, const char * end ) 926 { 927 const size_t slen = strlen( str ); 928 const size_t elen = strlen( end ); 929 return slen>=elen && !memcmp( &str[slen-elen], end, elen ); 930 } 931 924 932 /* 925 933 * Copy src to string dst of size siz. At most siz-1 characters -
trunk/libtransmission/utils.h
r6101 r6140 198 198 size_t tr_strlcpy( char * dst, const char * src, size_t siz ); 199 199 200 200 int tr_stringEndsWith( const char * string, const char * end ); 201 201 202 202 const char* tr_strerror( int );
Note: See TracChangeset
for help on using the changeset viewer.