mirror of
https://github.com/transmission/transmission
synced 2024-12-25 09:13:06 +00:00
encapsulate the blocklist a little better so that we can add more blocklists in the future if necessary
This commit is contained in:
parent
1950b87a6c
commit
5890eaef34
6 changed files with 120 additions and 89 deletions
|
@ -20,8 +20,6 @@
|
|||
} \
|
||||
}
|
||||
|
||||
extern void blocklistFilename( char * buf, size_t buflen );
|
||||
|
||||
static void
|
||||
createTestBlocklist( const char * tmpfile )
|
||||
{
|
||||
|
@ -43,55 +41,49 @@ createTestBlocklist( const char * tmpfile )
|
|||
int
|
||||
main( void )
|
||||
{
|
||||
tr_handle * handle;
|
||||
char fname[MAX_PATH_LENGTH];
|
||||
char bak[MAX_PATH_LENGTH];
|
||||
char * tmpfile = "/tmp/transmission-blocklist-test.txt";
|
||||
char * tmpfile_txt = "/tmp/transmission-blocklist-test.txt";
|
||||
char * tmpfile_bin = "/tmp/transmission-blocklist-test.bin";
|
||||
struct in_addr addr;
|
||||
int test = 0;
|
||||
tr_blocklist * b;
|
||||
|
||||
handle = tr_init( "unit-tests" );
|
||||
remove( tmpfile_txt );
|
||||
remove( tmpfile_bin );
|
||||
|
||||
/* backup the real blocklist */
|
||||
blocklistFilename( fname, sizeof( fname ) );
|
||||
snprintf( bak, sizeof( bak ), "%s.bak", fname );
|
||||
rename( fname, bak );
|
||||
|
||||
/* create our own dummy blocklist */
|
||||
createTestBlocklist( tmpfile );
|
||||
tr_blocklistSetContent( handle, tmpfile );
|
||||
tr_blocklistSetEnabled( handle, TRUE );
|
||||
b = _tr_blocklistNew( tmpfile_bin, TRUE );
|
||||
createTestBlocklist( tmpfile_txt );
|
||||
_tr_blocklistSetContent( b, tmpfile_txt );
|
||||
|
||||
/* now run some tests */
|
||||
check( !tr_netResolve( "216.16.1.143", &addr ) );
|
||||
check( !tr_peerIsBlocked( handle, &addr ) );
|
||||
check( !_tr_blocklistHasAddress( b, &addr ) );
|
||||
check( !tr_netResolve( "216.16.1.144", &addr ) );
|
||||
check( tr_peerIsBlocked( handle, &addr ) );
|
||||
check( _tr_blocklistHasAddress( b, &addr ) );
|
||||
check( !tr_netResolve( "216.16.1.145", &addr ) );
|
||||
check( tr_peerIsBlocked( handle, &addr ) );
|
||||
check( _tr_blocklistHasAddress( b, &addr ) );
|
||||
check( !tr_netResolve( "216.16.1.146", &addr ) );
|
||||
check( tr_peerIsBlocked( handle, &addr ) );
|
||||
check( _tr_blocklistHasAddress( b, &addr ) );
|
||||
check( !tr_netResolve( "216.16.1.147", &addr ) );
|
||||
check( tr_peerIsBlocked( handle, &addr ) );
|
||||
check( _tr_blocklistHasAddress( b, &addr ) );
|
||||
check( !tr_netResolve( "216.16.1.148", &addr ) );
|
||||
check( tr_peerIsBlocked( handle, &addr ) );
|
||||
check( _tr_blocklistHasAddress( b, &addr ) );
|
||||
check( !tr_netResolve( "216.16.1.149", &addr ) );
|
||||
check( tr_peerIsBlocked( handle, &addr ) );
|
||||
check( _tr_blocklistHasAddress( b, &addr ) );
|
||||
check( !tr_netResolve( "216.16.1.150", &addr ) );
|
||||
check( tr_peerIsBlocked( handle, &addr ) );
|
||||
check( _tr_blocklistHasAddress( b, &addr ) );
|
||||
check( !tr_netResolve( "216.16.1.151", &addr ) );
|
||||
check( tr_peerIsBlocked( handle, &addr ) );
|
||||
check( _tr_blocklistHasAddress( b, &addr ) );
|
||||
check( !tr_netResolve( "216.16.1.152", &addr ) );
|
||||
check( !tr_peerIsBlocked( handle, &addr ) );
|
||||
check( !_tr_blocklistHasAddress( b, &addr ) );
|
||||
check( !tr_netResolve( "216.16.1.153", &addr ) );
|
||||
check( !tr_peerIsBlocked( handle, &addr ) );
|
||||
check( !_tr_blocklistHasAddress( b, &addr ) );
|
||||
check( !tr_netResolve( "217.0.0.1", &addr ) );
|
||||
check( !tr_peerIsBlocked( handle, &addr ) );
|
||||
check( !_tr_blocklistHasAddress( b, &addr ) );
|
||||
check( !tr_netResolve( "255.0.0.1", &addr ) );
|
||||
|
||||
/* restore the real blocklist */
|
||||
remove( tmpfile );
|
||||
remove( fname );
|
||||
rename( bak, fname );
|
||||
/* cleanup */
|
||||
_tr_blocklistFree( b );
|
||||
remove( tmpfile_txt );
|
||||
remove( tmpfile_bin );
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -15,7 +15,6 @@
|
|||
#include <string.h>
|
||||
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
|
@ -25,9 +24,12 @@
|
|||
|
||||
#include "transmission.h"
|
||||
#include "blocklist.h"
|
||||
#include "net.h" /* tr_netResolve() */
|
||||
#include "platform.h" /* tr_getPrefsDirectory() */
|
||||
#include "utils.h" /* tr_buildPath() */
|
||||
#include "net.h" /* tr_netResolve() */
|
||||
#include "utils.h"
|
||||
|
||||
/***
|
||||
**** PRIVATE
|
||||
***/
|
||||
|
||||
struct tr_ip_range
|
||||
{
|
||||
|
@ -37,20 +39,14 @@ struct tr_ip_range
|
|||
|
||||
struct tr_blocklist
|
||||
{
|
||||
struct tr_ip_range * rules;
|
||||
unsigned int isEnabled : 1;
|
||||
int fd;
|
||||
size_t ruleCount;
|
||||
size_t byteCount;
|
||||
int fd;
|
||||
int isEnabled;
|
||||
char * filename;
|
||||
struct tr_ip_range * rules;
|
||||
};
|
||||
|
||||
void
|
||||
blocklistFilename( char * buf, size_t buflen )
|
||||
{
|
||||
tr_buildPath( buf, buflen, tr_getPrefsDirectory(), "blocklist", NULL );
|
||||
}
|
||||
|
||||
static void
|
||||
blocklistClose( tr_blocklist * b )
|
||||
{
|
||||
|
@ -94,11 +90,11 @@ blocklistLoad( tr_blocklist * b )
|
|||
b->byteCount = st.st_size;
|
||||
b->ruleCount = st.st_size / sizeof( struct tr_ip_range );
|
||||
b->fd = fd;
|
||||
tr_inf( _( "Blocklist contains %'d rules" ), b->ruleCount );
|
||||
tr_inf( _( "Blocklist contains %'d entries" ), b->ruleCount );
|
||||
}
|
||||
|
||||
static void
|
||||
ensureBlocklistIsLoaded( tr_blocklist * b )
|
||||
blocklistEnsureLoaded( tr_blocklist * b )
|
||||
{
|
||||
if( !b->rules )
|
||||
blocklistLoad( b );
|
||||
|
@ -122,16 +118,13 @@ blocklistDelete( tr_blocklist * b )
|
|||
}
|
||||
|
||||
/***
|
||||
****
|
||||
**** PACKAGE-VISIBLE
|
||||
***/
|
||||
|
||||
tr_blocklist *
|
||||
tr_blocklistNew( int isEnabled )
|
||||
_tr_blocklistNew( const char * filename, int isEnabled )
|
||||
{
|
||||
tr_blocklist * b;
|
||||
char filename[MAX_PATH_LENGTH];
|
||||
|
||||
blocklistFilename( filename, sizeof( filename ) );
|
||||
|
||||
b = tr_new0( tr_blocklist, 1 );
|
||||
b->fd = -1;
|
||||
|
@ -142,47 +135,43 @@ tr_blocklistNew( int isEnabled )
|
|||
}
|
||||
|
||||
void
|
||||
tr_blocklistFree( tr_blocklist * b )
|
||||
_tr_blocklistFree( tr_blocklist * b )
|
||||
{
|
||||
blocklistClose( b );
|
||||
tr_free( b->filename );
|
||||
tr_free( b );
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
tr_blocklistGetRuleCount( tr_handle * handle )
|
||||
_tr_blocklistGetRuleCount( tr_blocklist * b )
|
||||
{
|
||||
tr_blocklist * b = handle->blocklist;
|
||||
|
||||
ensureBlocklistIsLoaded( b );
|
||||
blocklistEnsureLoaded( b );
|
||||
|
||||
return b->ruleCount;
|
||||
}
|
||||
|
||||
int
|
||||
tr_blocklistIsEnabled( const tr_handle * handle )
|
||||
_tr_blocklistIsEnabled( tr_blocklist * b )
|
||||
{
|
||||
return handle->blocklist->isEnabled;
|
||||
return b->isEnabled;
|
||||
}
|
||||
|
||||
void
|
||||
tr_blocklistSetEnabled( tr_handle * handle, int isEnabled )
|
||||
_tr_blocklistSetEnabled( tr_blocklist * b, int isEnabled )
|
||||
{
|
||||
handle->blocklist->isEnabled = isEnabled ? 1 : 0;
|
||||
b->isEnabled = isEnabled ? 1 : 0;
|
||||
}
|
||||
|
||||
int
|
||||
tr_peerIsBlocked( const tr_handle * handle, const struct in_addr * addr )
|
||||
_tr_blocklistHasAddress( tr_blocklist * b, const struct in_addr * addr )
|
||||
{
|
||||
uint32_t needle;
|
||||
const struct tr_ip_range * range;
|
||||
tr_blocklist * b = handle->blocklist;
|
||||
|
||||
if( !b->isEnabled )
|
||||
return 0;
|
||||
|
||||
ensureBlocklistIsLoaded( b );
|
||||
blocklistEnsureLoaded( b );
|
||||
if( !b->rules )
|
||||
return 0;
|
||||
|
||||
|
@ -198,17 +187,16 @@ tr_peerIsBlocked( const tr_handle * handle, const struct in_addr * addr )
|
|||
}
|
||||
|
||||
int
|
||||
tr_blocklistExists( const tr_handle * handle )
|
||||
_tr_blocklistExists( const tr_blocklist * b )
|
||||
{
|
||||
struct stat st;
|
||||
return !stat( handle->blocklist->filename, &st );
|
||||
return !stat( b->filename, &st );
|
||||
}
|
||||
|
||||
int
|
||||
tr_blocklistSetContent( tr_handle * handle,
|
||||
const char * filename )
|
||||
_tr_blocklistSetContent( tr_blocklist * b,
|
||||
const char * filename )
|
||||
{
|
||||
tr_blocklist * b = handle->blocklist;
|
||||
FILE * in;
|
||||
FILE * out;
|
||||
char * line;
|
||||
|
@ -268,7 +256,7 @@ tr_blocklistSetContent( tr_handle * handle,
|
|||
++lineCount;
|
||||
}
|
||||
|
||||
tr_inf( _( "Blocklist updated with %'d rules" ), lineCount );
|
||||
tr_inf( _( "Blocklist updated with %'d entries" ), lineCount );
|
||||
|
||||
fclose( out );
|
||||
fclose( in );
|
||||
|
|
|
@ -13,17 +13,16 @@
|
|||
#ifndef TR_BLOCKLIST_H
|
||||
#define TR_BLOCKLIST_H
|
||||
|
||||
struct in_addr;
|
||||
typedef struct tr_blocklist tr_blocklist;
|
||||
|
||||
tr_blocklist * tr_blocklistNew( int isEnabled );
|
||||
|
||||
void tr_blocklistFree( tr_blocklist * );
|
||||
|
||||
|
||||
struct tr_handle;
|
||||
struct in_addr;
|
||||
|
||||
int tr_peerIsBlocked( const struct tr_handle *,
|
||||
const struct in_addr * );
|
||||
tr_blocklist* _tr_blocklistNew ( const char * filename, int isEnabled );
|
||||
void _tr_blocklistFree ( tr_blocklist * b );
|
||||
int _tr_blocklistGetRuleCount( tr_blocklist * b );
|
||||
int _tr_blocklistIsEnabled ( tr_blocklist * b );
|
||||
void _tr_blocklistSetEnabled ( tr_blocklist * b, int isEnabled );
|
||||
int _tr_blocklistHasAddress ( tr_blocklist * b, const struct in_addr * addr );
|
||||
int _tr_blocklistExists ( const tr_blocklist * b );
|
||||
int _tr_blocklistSetContent ( tr_blocklist * b, const char * filename );
|
||||
|
||||
#endif
|
||||
|
|
|
@ -981,7 +981,7 @@ ensureAtomExists( Torrent * t, const struct in_addr * addr, uint16_t port, uint8
|
|||
static void
|
||||
maybeEnsureAtomExists( Torrent * t, const struct in_addr * addr, uint16_t port, uint8_t flags, uint8_t from )
|
||||
{
|
||||
if( tr_peerIsBlocked( t->manager->handle, addr ) )
|
||||
if( tr_blocklistHasAddress( t->manager->handle, addr ) )
|
||||
{
|
||||
char * fmt = NULL;
|
||||
switch( from ) {
|
||||
|
@ -1101,7 +1101,7 @@ tr_peerMgrAddIncoming( tr_peerMgr * manager,
|
|||
{
|
||||
managerLock( manager );
|
||||
|
||||
if( tr_peerIsBlocked( manager->handle, addr ) )
|
||||
if( tr_blocklistHasAddress( manager->handle, addr ) )
|
||||
{
|
||||
tr_inf( _( "Banned IP address \"%s\" tried to connect to us" ),
|
||||
inet_ntoa( *addr ) );
|
||||
|
@ -1892,7 +1892,7 @@ getPeerCandidates( Torrent * t, int * setmeSize )
|
|||
}
|
||||
|
||||
/* Don't connect to peers in our blocklist */
|
||||
if( tr_peerIsBlocked( t->manager->handle, &atom->addr ) )
|
||||
if( tr_blocklistHasAddress( t->manager->handle, &atom->addr ) )
|
||||
continue;
|
||||
|
||||
ret[retCount++] = atom;
|
||||
|
|
|
@ -130,6 +130,7 @@ tr_initFull( const char * tag,
|
|||
{
|
||||
tr_handle * h;
|
||||
char buf[128];
|
||||
char filename[MAX_PATH_LENGTH];
|
||||
|
||||
#ifndef WIN32
|
||||
/* Don't exit when writing on a broken socket */
|
||||
|
@ -179,7 +180,11 @@ tr_initFull( const char * tag,
|
|||
TR_NAME, LONG_VERSION_STRING );
|
||||
tr_inf( "%s", buf );
|
||||
|
||||
h->blocklist = tr_blocklistNew( isBlocklistEnabled );
|
||||
/* initialize the blocklist */
|
||||
tr_buildPath( filename, sizeof( filename ), tr_getPrefsDirectory(), "blocklists", NULL );
|
||||
tr_mkdirp( filename, 0777 );
|
||||
tr_buildPath( filename, sizeof( filename ), tr_getPrefsDirectory(), "blocklists", "level1.bin", NULL );
|
||||
h->blocklist = _tr_blocklistNew( filename, isBlocklistEnabled );
|
||||
|
||||
tr_statsInit( h );
|
||||
|
||||
|
@ -372,7 +377,7 @@ tr_closeImpl( void * vh )
|
|||
tr_sharedShuttingDown( h->shared );
|
||||
tr_trackerShuttingDown( h );
|
||||
|
||||
tr_blocklistFree( h->blocklist );
|
||||
_tr_blocklistFree( h->blocklist );
|
||||
h->blocklist = NULL;
|
||||
|
||||
for( t=h->torrentList; t!=NULL; ) {
|
||||
|
@ -485,3 +490,43 @@ tr_isPexEnabled( const tr_handle * handle )
|
|||
{
|
||||
return handle->isPexEnabled;
|
||||
}
|
||||
|
||||
/***
|
||||
****
|
||||
***/
|
||||
|
||||
int
|
||||
tr_blocklistGetRuleCount( tr_handle * handle )
|
||||
{
|
||||
return _tr_blocklistGetRuleCount( handle->blocklist );
|
||||
}
|
||||
|
||||
int
|
||||
tr_blocklistIsEnabled( const tr_handle * handle )
|
||||
{
|
||||
return _tr_blocklistIsEnabled( handle->blocklist );
|
||||
}
|
||||
|
||||
void
|
||||
tr_blocklistSetEnabled( tr_handle * handle, int isEnabled )
|
||||
{
|
||||
_tr_blocklistSetEnabled( handle->blocklist, isEnabled );
|
||||
}
|
||||
|
||||
int
|
||||
tr_blocklistExists( const tr_handle * handle )
|
||||
{
|
||||
return _tr_blocklistExists( handle->blocklist );
|
||||
}
|
||||
|
||||
int
|
||||
tr_blocklistSetContent( tr_handle * handle, const char * filename )
|
||||
{
|
||||
return _tr_blocklistSetContent( handle->blocklist, filename );
|
||||
}
|
||||
|
||||
int
|
||||
tr_blocklistHasAddress( tr_handle * handle, const struct in_addr * addr )
|
||||
{
|
||||
return _tr_blocklistHasAddress( handle->blocklist, addr );
|
||||
}
|
||||
|
|
|
@ -156,9 +156,12 @@ const char * tr_getPrefsDirectory( void );
|
|||
** Message Logging
|
||||
*/
|
||||
|
||||
#define TR_MSG_ERR 1
|
||||
#define TR_MSG_INF 2
|
||||
#define TR_MSG_DBG 3
|
||||
enum
|
||||
{
|
||||
TR_MSG_ERR = 1,
|
||||
TR_MSG_INF = 2,
|
||||
TR_MSG_DBG = 3
|
||||
};
|
||||
void tr_setMessageLevel( int );
|
||||
int tr_getMessageLevel( void );
|
||||
|
||||
|
@ -319,6 +322,10 @@ int tr_blocklistIsEnabled( const tr_handle * handle );
|
|||
void tr_blocklistSetEnabled( tr_handle * handle,
|
||||
int isEnabled );
|
||||
|
||||
struct in_addr;
|
||||
|
||||
int tr_blocklistHasAddress( tr_handle * handle,
|
||||
const struct in_addr * addr);
|
||||
|
||||
|
||||
/***********************************************************************
|
||||
|
|
Loading…
Reference in a new issue