mirror of
https://github.com/transmission/transmission
synced 2025-03-15 16:29:34 +00:00
get the blocklist code working & add a unit test for it.
This commit is contained in:
parent
1d10d55bc2
commit
a20bb8d569
5 changed files with 176 additions and 30 deletions
|
@ -78,6 +78,7 @@ noinst_HEADERS = \
|
|||
bin_PROGRAMS = benc2php
|
||||
|
||||
TESTS = \
|
||||
blocklist-test \
|
||||
bencode-test \
|
||||
test-fastset \
|
||||
test-peer-id
|
||||
|
@ -95,6 +96,8 @@ benc2php_SOURCES = benc2php.c
|
|||
benc2php_LDADD = $(APPS_LDADD)
|
||||
bencode_test_SOURCES = bencode-test.c
|
||||
bencode_test_LDADD = $(APPS_LDADD)
|
||||
blocklist_test_SOURCES = blocklist-test.c
|
||||
blocklist_test_LDADD = $(APPS_LDADD)
|
||||
test_fastset_SOURCES = test-fastset.c
|
||||
test_fastset_LDADD = $(APPS_LDADD)
|
||||
test_peer_id_SOURCES = test-peer-id.c
|
||||
|
|
96
libtransmission/blocklist-test.c
Normal file
96
libtransmission/blocklist-test.c
Normal file
|
@ -0,0 +1,96 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "transmission.h"
|
||||
#include "blocklist.h"
|
||||
#include "net.h"
|
||||
#include "utils.h"
|
||||
|
||||
#define VERBOSE 1
|
||||
|
||||
#define check(A) { \
|
||||
++test; \
|
||||
if (A) { \
|
||||
if( VERBOSE ) \
|
||||
fprintf( stderr, "PASS test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \
|
||||
} else { \
|
||||
if( VERBOSE ) \
|
||||
fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \
|
||||
return test; \
|
||||
} \
|
||||
}
|
||||
|
||||
extern void tr_getBlocklistFilename( char * buf, size_t buflen );
|
||||
|
||||
static void
|
||||
createTestBlocklist( const char * tmpfile )
|
||||
{
|
||||
const char * lines[] = { "Austin Law Firm:216.16.1.144-216.16.1.151",
|
||||
"Sargent Controls and Aerospace:216.19.18.0-216.19.18.255",
|
||||
"Corel Corporation:216.21.157.192-216.21.157.223",
|
||||
"Fox Speed Channel:216.79.131.192-216.79.131.223" };
|
||||
FILE * out;
|
||||
int i;
|
||||
const int lineCount = sizeof(lines) / sizeof(lines[0]);
|
||||
|
||||
/* create the ascii file to feed to libtransmission */
|
||||
out = fopen( tmpfile, "w+" );
|
||||
for( i=0; i<lineCount; ++i )
|
||||
fprintf( out, "%s\n", lines[i] );
|
||||
fclose( out );
|
||||
}
|
||||
|
||||
int
|
||||
main( void )
|
||||
{
|
||||
tr_handle * handle;
|
||||
char fname[MAX_PATH_LENGTH];
|
||||
char bak[MAX_PATH_LENGTH];
|
||||
char * tmpfile = "/tmp/transmission-blocklist-test.txt";
|
||||
struct in_addr addr;
|
||||
int test = 0;
|
||||
|
||||
handle = tr_init( "unit-tests" );
|
||||
|
||||
/* backup the real blocklist */
|
||||
tr_getBlocklistFilename( fname, sizeof( fname ) );
|
||||
snprintf( bak, sizeof( bak ), "%s.bak", fname );
|
||||
rename( fname, bak );
|
||||
|
||||
/* create our own dummy blocklist */
|
||||
createTestBlocklist( tmpfile );
|
||||
tr_setBlocklist( handle, tmpfile );
|
||||
|
||||
/* now run some tests */
|
||||
check( !tr_netResolve( "216.16.1.143", &addr ) );
|
||||
check( !tr_peerIsBlocked( handle, &addr ) );
|
||||
check( !tr_netResolve( "216.16.1.144", &addr ) );
|
||||
check( tr_peerIsBlocked( handle, &addr ) );
|
||||
check( !tr_netResolve( "216.16.1.145", &addr ) );
|
||||
check( tr_peerIsBlocked( handle, &addr ) );
|
||||
check( !tr_netResolve( "216.16.1.146", &addr ) );
|
||||
check( tr_peerIsBlocked( handle, &addr ) );
|
||||
check( !tr_netResolve( "216.16.1.147", &addr ) );
|
||||
check( tr_peerIsBlocked( handle, &addr ) );
|
||||
check( !tr_netResolve( "216.16.1.148", &addr ) );
|
||||
check( tr_peerIsBlocked( handle, &addr ) );
|
||||
check( !tr_netResolve( "216.16.1.149", &addr ) );
|
||||
check( tr_peerIsBlocked( handle, &addr ) );
|
||||
check( !tr_netResolve( "216.16.1.150", &addr ) );
|
||||
check( tr_peerIsBlocked( handle, &addr ) );
|
||||
check( !tr_netResolve( "216.16.1.151", &addr ) );
|
||||
check( tr_peerIsBlocked( handle, &addr ) );
|
||||
check( !tr_netResolve( "216.16.1.152", &addr ) );
|
||||
check( !tr_peerIsBlocked( handle, &addr ) );
|
||||
check( !tr_netResolve( "216.16.1.153", &addr ) );
|
||||
check( !tr_peerIsBlocked( handle, &addr ) );
|
||||
check( !tr_netResolve( "217.0.0.1", &addr ) );
|
||||
check( !tr_peerIsBlocked( handle, &addr ) );
|
||||
check( !tr_netResolve( "255.0.0.1", &addr ) );
|
||||
|
||||
/* restore the real blocklist */
|
||||
remove( tmpfile );
|
||||
remove( fname );
|
||||
rename( bak, fname );
|
||||
return 0;
|
||||
}
|
|
@ -24,16 +24,24 @@
|
|||
#include "ggets.h"
|
||||
|
||||
#include "transmission.h"
|
||||
#include "net.h" /* inet_aton() */
|
||||
#include "internal.h"
|
||||
#include "net.h" /* tr_netResolve */
|
||||
#include "platform.h" /* tr_getPrefsDirectory() */
|
||||
#include "utils.h" /* tr_buildPath() */
|
||||
|
||||
static void * blocklist = NULL;
|
||||
static size_t blocklistSize = 0;
|
||||
struct tr_ip_range
|
||||
{
|
||||
uint32_t begin;
|
||||
uint32_t end;
|
||||
};
|
||||
|
||||
static struct tr_ip_range * blocklist = NULL;
|
||||
static size_t blocklistItemCount = 0;
|
||||
static size_t blocklistByteCount = 0;
|
||||
static int blocklistFd = -1;
|
||||
|
||||
static void
|
||||
getFilename( char * buf, size_t buflen )
|
||||
void
|
||||
tr_getBlocklistFilename( char * buf, size_t buflen )
|
||||
{
|
||||
tr_buildPath( buf, buflen, tr_getPrefsDirectory(), "blocklist", NULL );
|
||||
}
|
||||
|
@ -43,10 +51,11 @@ closeBlocklist( void )
|
|||
{
|
||||
if( blocklist )
|
||||
{
|
||||
munmap( blocklist, blocklistSize );
|
||||
munmap( blocklist, blocklistByteCount );
|
||||
close( blocklistFd );
|
||||
blocklist = NULL;
|
||||
blocklistSize = 0;
|
||||
blocklistItemCount = 0;
|
||||
blocklistByteCount = 0;
|
||||
blocklistFd = -1;
|
||||
}
|
||||
}
|
||||
|
@ -57,7 +66,7 @@ loadBlocklist( void )
|
|||
int fd;
|
||||
struct stat st;
|
||||
char filename[MAX_PATH_LENGTH];
|
||||
getFilename( filename, sizeof( filename ) );
|
||||
tr_getBlocklistFilename( filename, sizeof( filename ) );
|
||||
|
||||
closeBlocklist( );
|
||||
|
||||
|
@ -78,25 +87,50 @@ loadBlocklist( void )
|
|||
return;
|
||||
}
|
||||
|
||||
blocklistSize = st.st_size;
|
||||
blocklistByteCount = st.st_size;
|
||||
blocklistItemCount = blocklistByteCount / sizeof( struct tr_ip_range );
|
||||
blocklistFd = fd;
|
||||
tr_inf( _( "Blocklist contains %'d IP ranges" ), blocklistItemCount );
|
||||
}
|
||||
|
||||
static int
|
||||
compareAddressToRange( const void * va, const void * vb )
|
||||
{
|
||||
const uint32_t * a = va;
|
||||
const struct tr_ip_range * b = vb;
|
||||
if( *a < b->begin ) return -1;
|
||||
if( *a > b->end ) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
tr_isInBlocklist( const struct in_addr * addr UNUSED )
|
||||
tr_peerIsBlocked( tr_handle * handle UNUSED, const struct in_addr * addr )
|
||||
{
|
||||
if( !blocklist )
|
||||
uint32_t needle;
|
||||
const struct tr_ip_range * range;
|
||||
|
||||
if( !blocklist ) {
|
||||
loadBlocklist( );
|
||||
if( !blocklist )
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return FALSE; /* FIXME */
|
||||
needle = ntohl( addr->s_addr );
|
||||
|
||||
range = bsearch( &needle,
|
||||
blocklist,
|
||||
blocklistItemCount,
|
||||
sizeof( struct tr_ip_range ),
|
||||
compareAddressToRange );
|
||||
|
||||
return range != NULL;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
deleteBlocklist( tr_handle * handle UNUSED )
|
||||
{
|
||||
char filename[MAX_PATH_LENGTH];
|
||||
getFilename( filename, sizeof( filename ) );
|
||||
tr_getBlocklistFilename( filename, sizeof( filename ) );
|
||||
closeBlocklist( );
|
||||
unlink( filename );
|
||||
}
|
||||
|
@ -109,6 +143,7 @@ tr_setBlocklist( tr_handle * handle,
|
|||
FILE * out;
|
||||
char * line;
|
||||
char outfile[MAX_PATH_LENGTH];
|
||||
int lineCount = 0;
|
||||
|
||||
if( filename == NULL ) {
|
||||
deleteBlocklist( handle );
|
||||
|
@ -123,8 +158,7 @@ tr_setBlocklist( tr_handle * handle,
|
|||
|
||||
closeBlocklist( );
|
||||
|
||||
getFilename( outfile, sizeof( outfile ) );
|
||||
fprintf( stderr, "outfile is [%s]\n", outfile );
|
||||
tr_getBlocklistFilename( outfile, sizeof( outfile ) );
|
||||
out = fopen( outfile, "wb+" );
|
||||
if( !out ) {
|
||||
tr_err( _( "Couldn't save file \"%s\": %s" ), outfile, tr_strerror( errno ) );
|
||||
|
@ -137,33 +171,38 @@ fprintf( stderr, "outfile is [%s]\n", outfile );
|
|||
char * rangeBegin;
|
||||
char * rangeEnd;
|
||||
struct in_addr in_addr;
|
||||
uint32_t range[2];
|
||||
//fprintf( stderr, "got line [%s]\n", line );
|
||||
struct tr_ip_range range;
|
||||
|
||||
rangeBegin = strrchr( line, ':' );
|
||||
if( !rangeBegin ) continue;
|
||||
if( !rangeBegin ) { free(line); continue; }
|
||||
++rangeBegin;
|
||||
|
||||
rangeEnd = strchr( rangeBegin, '-' );
|
||||
if( !rangeEnd ) continue;
|
||||
if( !rangeEnd ) { free(line); continue; }
|
||||
*rangeEnd++ = '\0';
|
||||
|
||||
//fprintf( stderr, "rangeBegin [%s] rangeEnd [%s]\n", rangeBegin, rangeEnd );
|
||||
if( !inet_aton( rangeBegin, &in_addr ) )
|
||||
fprintf( stderr, "skipping invalid address [%s]\n", rangeBegin );
|
||||
range[0] = ntohl( in_addr.s_addr );
|
||||
if( !inet_aton( rangeEnd, &in_addr ) )
|
||||
fprintf( stderr, "skipping invalid address [%s]\n", rangeEnd );
|
||||
range[1] = ntohl( in_addr.s_addr );
|
||||
if( tr_netResolve( rangeBegin, &in_addr ) )
|
||||
tr_err( "blocklist skipped invalid address [%s]\n", rangeBegin );
|
||||
range.begin = ntohl( in_addr.s_addr );
|
||||
|
||||
if( tr_netResolve( rangeEnd, &in_addr ) )
|
||||
tr_err( "blocklist skipped invalid address [%s]\n", rangeEnd );
|
||||
range.end = ntohl( in_addr.s_addr );
|
||||
|
||||
free( line );
|
||||
|
||||
if( fwrite( range, sizeof(uint32_t), 2, out ) != 2 ) {
|
||||
if( fwrite( &range, sizeof(struct tr_ip_range), 1, out ) != 1 ) {
|
||||
tr_err( _( "Couldn't save file \"%s\": %s" ), outfile, tr_strerror( errno ) );
|
||||
break;
|
||||
}
|
||||
|
||||
++lineCount;
|
||||
}
|
||||
|
||||
tr_inf( _( "Blocklist updated with %'d IP ranges" ), lineCount );
|
||||
|
||||
fclose( out );
|
||||
fclose( in );
|
||||
|
||||
loadBlocklist( );
|
||||
}
|
||||
|
|
|
@ -13,8 +13,10 @@
|
|||
#ifndef TR_BLOCKLIST_H
|
||||
#define TR_BLOCKLIST_H
|
||||
|
||||
struct tr_handle;
|
||||
struct in_addr;
|
||||
|
||||
int tr_isInBlocklist( const struct in_addr * );
|
||||
int tr_peerIsBlocked( const struct tr_handle *,
|
||||
const struct in_addr * );
|
||||
|
||||
#endif
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
#include <event.h>
|
||||
|
||||
#include "transmission.h"
|
||||
#include "blocklist.h"
|
||||
#include "clients.h"
|
||||
#include "completion.h"
|
||||
#include "crypto.h"
|
||||
|
@ -1080,7 +1081,8 @@ tr_peerMgrAddIncoming( tr_peerMgr * manager,
|
|||
{
|
||||
managerLock( manager );
|
||||
|
||||
if( getExistingHandshake( manager->incomingHandshakes, addr ) )
|
||||
if( tr_peerIsBlocked( manager->handle, addr )
|
||||
|| getExistingHandshake( manager->incomingHandshakes, addr ) )
|
||||
{
|
||||
tr_netClose( socket );
|
||||
}
|
||||
|
@ -1864,6 +1866,10 @@ getPeerCandidates( Torrent * t, int * setmeSize )
|
|||
}
|
||||
}
|
||||
|
||||
/* Don't connect to peers in our blocklist */
|
||||
if( tr_peerIsBlocked( t->manager->handle, &atom->addr ) )
|
||||
continue;
|
||||
|
||||
ret[retCount++] = atom;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue