Changeset 5432


Ignore:
Timestamp:
Mar 29, 2008, 9:05:51 PM (14 years ago)
Author:
charles
Message:

get the blocklist code working & add a unit test for it.

Location:
trunk/libtransmission
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/Makefile.am

    r5429 r5432  
    7979
    8080TESTS = \
     81    blocklist-test \
    8182    bencode-test \
    8283    test-fastset \
     
    9697bencode_test_SOURCES = bencode-test.c
    9798bencode_test_LDADD = $(APPS_LDADD)
     99blocklist_test_SOURCES = blocklist-test.c
     100blocklist_test_LDADD = $(APPS_LDADD)
    98101test_fastset_SOURCES = test-fastset.c
    99102test_fastset_LDADD = $(APPS_LDADD)
  • trunk/libtransmission/blocklist.c

    r5429 r5432  
    2525
    2626#include "transmission.h"
    27 #include "net.h" /* inet_aton() */
     27#include "internal.h"
     28#include "net.h" /* tr_netResolve */
    2829#include "platform.h" /* tr_getPrefsDirectory() */
    2930#include "utils.h" /* tr_buildPath() */
    3031
    31 static void * blocklist = NULL;
    32 static size_t blocklistSize = 0;
     32struct tr_ip_range
     33{
     34  uint32_t begin;
     35  uint32_t end;
     36};
     37
     38static struct tr_ip_range * blocklist = NULL;
     39static size_t blocklistItemCount = 0;
     40static size_t blocklistByteCount = 0;
    3341static int blocklistFd = -1;
    3442
    35 static void
    36 getFilename( char * buf, size_t buflen )
     43void
     44tr_getBlocklistFilename( char * buf, size_t buflen )
    3745{
    3846    tr_buildPath( buf, buflen, tr_getPrefsDirectory(), "blocklist", NULL );
     
    4452    if( blocklist )
    4553    {
    46         munmap( blocklist, blocklistSize );
     54        munmap( blocklist, blocklistByteCount );
    4755        close( blocklistFd );
    4856        blocklist = NULL;
    49         blocklistSize = 0;
     57        blocklistItemCount = 0;
     58        blocklistByteCount = 0;
    5059        blocklistFd  = -1;
    5160    }
     
    5867    struct stat st;
    5968    char filename[MAX_PATH_LENGTH];
    60     getFilename( filename, sizeof( filename ) );
     69    tr_getBlocklistFilename( filename, sizeof( filename ) );
    6170
    6271    closeBlocklist( );
     
    7988    }
    8089
    81     blocklistSize = st.st_size;
     90    blocklistByteCount = st.st_size;
     91    blocklistItemCount = blocklistByteCount / sizeof( struct tr_ip_range );
    8292    blocklistFd = fd;
     93    tr_inf( _( "Blocklist contains %'d IP ranges" ), blocklistItemCount );
     94}
     95
     96static int
     97compareAddressToRange( 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;
    83104}
    84105
    85106int
    86 tr_isInBlocklist( const struct in_addr * addr UNUSED )
    87 {
    88     if( !blocklist )
     107tr_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 ) {
    89113        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}
    94128
    95129static void
     
    97131{
    98132    char filename[MAX_PATH_LENGTH];
    99     getFilename( filename, sizeof( filename ) );
     133    tr_getBlocklistFilename( filename, sizeof( filename ) );
    100134    closeBlocklist( );
    101135    unlink( filename );
     
    110144    char * line;
    111145    char outfile[MAX_PATH_LENGTH];
     146    int lineCount = 0;
    112147
    113148    if( filename == NULL ) {
     
    124159    closeBlocklist( );
    125160 
    126     getFilename( outfile, sizeof( outfile ) );
    127 fprintf( stderr, "outfile is [%s]\n", outfile );
     161    tr_getBlocklistFilename( outfile, sizeof( outfile ) );
    128162    out = fopen( outfile, "wb+" );
    129163    if( !out ) {
     
    138172        char * rangeEnd;
    139173        struct in_addr in_addr;
    140         uint32_t range[2];
    141 //fprintf( stderr, "got line [%s]\n", line );
     174        struct tr_ip_range range;
    142175
    143176        rangeBegin = strrchr( line, ':' );
    144         if( !rangeBegin ) continue;
     177        if( !rangeBegin ) { free(line); continue; }
    145178        ++rangeBegin;
    146179
    147180        rangeEnd = strchr( rangeBegin, '-' );
    148         if( !rangeEnd ) continue;
     181        if( !rangeEnd ) { free(line); continue; }
    149182        *rangeEnd++ = '\0';
    150183
    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, "skipping invalid 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 );
    158191
    159192        free( line );
    160193
    161         if( fwrite( range, sizeof(uint32_t), 2, out ) != 2 ) {
     194        if( fwrite( &range, sizeof(struct tr_ip_range), 1, out ) != 1 ) {
    162195          tr_err( _( "Couldn't save file \"%s\": %s" ), outfile, tr_strerror( errno ) );
    163196          break;
    164197        }
    165     }
     198
     199        ++lineCount;
     200    }
     201
     202    tr_inf( _( "Blocklist updated with %'d IP ranges" ), lineCount );
    166203
    167204    fclose( out );
    168205    fclose( in );
    169 }
     206
     207    loadBlocklist( );
     208}
  • trunk/libtransmission/blocklist.h

    r5429 r5432  
    1414#define TR_BLOCKLIST_H
    1515
     16struct tr_handle;
    1617struct in_addr;
    1718
    18 int tr_isInBlocklist( const struct in_addr * );
     19int tr_peerIsBlocked( const struct tr_handle *,
     20                      const struct in_addr   * );
    1921
    2022#endif
  • trunk/libtransmission/peer-mgr.c

    r5329 r5432  
    2222
    2323#include "transmission.h"
     24#include "blocklist.h"
    2425#include "clients.h"
    2526#include "completion.h"
     
    10811082    managerLock( manager );
    10821083
    1083     if( getExistingHandshake( manager->incomingHandshakes, addr ) )
     1084    if( tr_peerIsBlocked( manager->handle, addr )
     1085        || getExistingHandshake( manager->incomingHandshakes, addr ) )
    10841086    {
    10851087        tr_netClose( socket );
     
    18651867        }
    18661868
     1869        /* Don't connect to peers in our blocklist */
     1870        if( tr_peerIsBlocked( t->manager->handle, &atom->addr ) )
     1871            continue;
     1872
    18671873        ret[retCount++] = atom;
    18681874    }
Note: See TracChangeset for help on using the changeset viewer.