Changeset 5432
- Timestamp:
- Mar 29, 2008, 9:05:51 PM (14 years ago)
- Location:
- trunk/libtransmission
- Files:
-
- 1 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/Makefile.am
r5429 r5432 79 79 80 80 TESTS = \ 81 blocklist-test \ 81 82 bencode-test \ 82 83 test-fastset \ … … 96 97 bencode_test_SOURCES = bencode-test.c 97 98 bencode_test_LDADD = $(APPS_LDADD) 99 blocklist_test_SOURCES = blocklist-test.c 100 blocklist_test_LDADD = $(APPS_LDADD) 98 101 test_fastset_SOURCES = test-fastset.c 99 102 test_fastset_LDADD = $(APPS_LDADD) -
trunk/libtransmission/blocklist.c
r5429 r5432 25 25 26 26 #include "transmission.h" 27 #include "net.h" /* inet_aton() */ 27 #include "internal.h" 28 #include "net.h" /* tr_netResolve */ 28 29 #include "platform.h" /* tr_getPrefsDirectory() */ 29 30 #include "utils.h" /* tr_buildPath() */ 30 31 31 static void * blocklist = NULL; 32 static size_t blocklistSize = 0; 32 struct tr_ip_range 33 { 34 uint32_t begin; 35 uint32_t end; 36 }; 37 38 static struct tr_ip_range * blocklist = NULL; 39 static size_t blocklistItemCount = 0; 40 static size_t blocklistByteCount = 0; 33 41 static int blocklistFd = -1; 34 42 35 staticvoid36 getFilename( char * buf, size_t buflen )43 void 44 tr_getBlocklistFilename( char * buf, size_t buflen ) 37 45 { 38 46 tr_buildPath( buf, buflen, tr_getPrefsDirectory(), "blocklist", NULL ); … … 44 52 if( blocklist ) 45 53 { 46 munmap( blocklist, blocklist Size);54 munmap( blocklist, blocklistByteCount ); 47 55 close( blocklistFd ); 48 56 blocklist = NULL; 49 blocklistSize = 0; 57 blocklistItemCount = 0; 58 blocklistByteCount = 0; 50 59 blocklistFd = -1; 51 60 } … … 58 67 struct stat st; 59 68 char filename[MAX_PATH_LENGTH]; 60 getFilename( filename, sizeof( filename ) );69 tr_getBlocklistFilename( filename, sizeof( filename ) ); 61 70 62 71 closeBlocklist( ); … … 79 88 } 80 89 81 blocklistSize = st.st_size; 90 blocklistByteCount = st.st_size; 91 blocklistItemCount = blocklistByteCount / sizeof( struct tr_ip_range ); 82 92 blocklistFd = fd; 93 tr_inf( _( "Blocklist contains %'d IP ranges" ), blocklistItemCount ); 94 } 95 96 static int 97 compareAddressToRange( const void * va, const void * vb ) 98 { 99 const uint32_t * a = va; 100 const struct tr_ip_range * b = vb; 101 if( *a < b->begin ) return -1; 102 if( *a > b->end ) return 1; 103 return 0; 83 104 } 84 105 85 106 int 86 tr_isInBlocklist( const struct in_addr * addr UNUSED ) 87 { 88 if( !blocklist ) 107 tr_peerIsBlocked( tr_handle * handle UNUSED, const struct in_addr * addr ) 108 { 109 uint32_t needle; 110 const struct tr_ip_range * range; 111 112 if( !blocklist ) { 89 113 loadBlocklist( ); 90 91 return FALSE; /* FIXME */ 92 } 93 114 if( !blocklist ) 115 return FALSE; 116 } 117 118 needle = ntohl( addr->s_addr ); 119 120 range = bsearch( &needle, 121 blocklist, 122 blocklistItemCount, 123 sizeof( struct tr_ip_range ), 124 compareAddressToRange ); 125 126 return range != NULL; 127 } 94 128 95 129 static void … … 97 131 { 98 132 char filename[MAX_PATH_LENGTH]; 99 getFilename( filename, sizeof( filename ) );133 tr_getBlocklistFilename( filename, sizeof( filename ) ); 100 134 closeBlocklist( ); 101 135 unlink( filename ); … … 110 144 char * line; 111 145 char outfile[MAX_PATH_LENGTH]; 146 int lineCount = 0; 112 147 113 148 if( filename == NULL ) { … … 124 159 closeBlocklist( ); 125 160 126 getFilename( outfile, sizeof( outfile ) ); 127 fprintf( stderr, "outfile is [%s]\n", outfile ); 161 tr_getBlocklistFilename( outfile, sizeof( outfile ) ); 128 162 out = fopen( outfile, "wb+" ); 129 163 if( !out ) { … … 138 172 char * rangeEnd; 139 173 struct in_addr in_addr; 140 uint32_t range[2]; 141 //fprintf( stderr, "got line [%s]\n", line ); 174 struct tr_ip_range range; 142 175 143 176 rangeBegin = strrchr( line, ':' ); 144 if( !rangeBegin ) continue;177 if( !rangeBegin ) { free(line); continue; } 145 178 ++rangeBegin; 146 179 147 180 rangeEnd = strchr( rangeBegin, '-' ); 148 if( !rangeEnd ) continue;181 if( !rangeEnd ) { free(line); continue; } 149 182 *rangeEnd++ = '\0'; 150 183 151 //fprintf( stderr, "rangeBegin [%s] rangeEnd [%s]\n", rangeBegin, rangeEnd );152 if( !inet_aton( rangeBegin, &in_addr ) )153 fprintf( stderr, "skipping invalid address [%s]\n", rangeBegin);154 range[0] = ntohl( in_addr.s_addr ); 155 if( !inet_aton( rangeEnd, &in_addr ) )156 fprintf( stderr, "skippinginvalid address [%s]\n", rangeEnd );157 range [1]= ntohl( in_addr.s_addr );184 if( tr_netResolve( rangeBegin, &in_addr ) ) 185 tr_err( "blocklist skipped invalid address [%s]\n", rangeBegin ); 186 range.begin = ntohl( in_addr.s_addr ); 187 188 if( tr_netResolve( rangeEnd, &in_addr ) ) 189 tr_err( "blocklist skipped invalid address [%s]\n", rangeEnd ); 190 range.end = ntohl( in_addr.s_addr ); 158 191 159 192 free( line ); 160 193 161 if( fwrite( range, sizeof(uint32_t), 2, out ) != 2) {194 if( fwrite( &range, sizeof(struct tr_ip_range), 1, out ) != 1 ) { 162 195 tr_err( _( "Couldn't save file \"%s\": %s" ), outfile, tr_strerror( errno ) ); 163 196 break; 164 197 } 165 } 198 199 ++lineCount; 200 } 201 202 tr_inf( _( "Blocklist updated with %'d IP ranges" ), lineCount ); 166 203 167 204 fclose( out ); 168 205 fclose( in ); 169 } 206 207 loadBlocklist( ); 208 } -
trunk/libtransmission/blocklist.h
r5429 r5432 14 14 #define TR_BLOCKLIST_H 15 15 16 struct tr_handle; 16 17 struct in_addr; 17 18 18 int tr_isInBlocklist( const struct in_addr * ); 19 int tr_peerIsBlocked( const struct tr_handle *, 20 const struct in_addr * ); 19 21 20 22 #endif -
trunk/libtransmission/peer-mgr.c
r5329 r5432 22 22 23 23 #include "transmission.h" 24 #include "blocklist.h" 24 25 #include "clients.h" 25 26 #include "completion.h" … … 1081 1082 managerLock( manager ); 1082 1083 1083 if( getExistingHandshake( manager->incomingHandshakes, addr ) ) 1084 if( tr_peerIsBlocked( manager->handle, addr ) 1085 || getExistingHandshake( manager->incomingHandshakes, addr ) ) 1084 1086 { 1085 1087 tr_netClose( socket ); … … 1865 1867 } 1866 1868 1869 /* Don't connect to peers in our blocklist */ 1870 if( tr_peerIsBlocked( t->manager->handle, &atom->addr ) ) 1871 continue; 1872 1867 1873 ret[retCount++] = atom; 1868 1874 }
Note: See TracChangeset
for help on using the changeset viewer.