2008-03-29 18:37:07 +00:00
|
|
|
/*
|
2010-01-04 21:00:47 +00:00
|
|
|
* This file Copyright (C) 2008-2010 Mnemosyne LLC
|
2008-03-29 18:37:07 +00:00
|
|
|
*
|
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
|
|
|
* Transmission project are granted a special exemption to clause 2(b)
|
2008-09-23 19:11:04 +00:00
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
2008-03-29 18:37:07 +00:00
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
|
|
|
*
|
2008-05-28 17:17:12 +00:00
|
|
|
* $Id$
|
2008-03-29 18:37:07 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h> /* free */
|
|
|
|
#include <string.h>
|
|
|
|
|
2008-09-05 14:31:52 +00:00
|
|
|
#ifdef WIN32
|
2008-09-23 19:11:04 +00:00
|
|
|
#include <windows.h>
|
2008-09-05 14:31:52 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef WIN32
|
2008-09-23 19:11:04 +00:00
|
|
|
#include <sys/mman.h>
|
2008-09-05 14:31:52 +00:00
|
|
|
#endif
|
2008-03-29 18:37:07 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2008-12-20 02:56:44 +00:00
|
|
|
#include <assert.h>
|
2008-03-29 18:37:07 +00:00
|
|
|
|
|
|
|
#include "ggets.h"
|
|
|
|
|
|
|
|
#include "transmission.h"
|
2008-09-05 14:31:52 +00:00
|
|
|
#include "platform.h"
|
2008-03-30 13:22:45 +00:00
|
|
|
#include "blocklist.h"
|
2008-12-02 03:41:58 +00:00
|
|
|
#include "net.h"
|
2008-04-01 19:20:21 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
2008-12-09 21:39:05 +00:00
|
|
|
|
2008-04-01 19:20:21 +00:00
|
|
|
/***
|
|
|
|
**** PRIVATE
|
|
|
|
***/
|
2008-03-29 18:37:07 +00:00
|
|
|
|
2008-03-29 21:05:51 +00:00
|
|
|
struct tr_ip_range
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
uint32_t begin;
|
|
|
|
uint32_t end;
|
2008-03-29 21:05:51 +00:00
|
|
|
};
|
|
|
|
|
2008-03-30 13:22:45 +00:00
|
|
|
struct tr_blocklist
|
|
|
|
{
|
2008-11-28 22:11:41 +00:00
|
|
|
tr_bool isEnabled;
|
2008-09-23 19:11:04 +00:00
|
|
|
int fd;
|
|
|
|
size_t ruleCount;
|
|
|
|
size_t byteCount;
|
|
|
|
char * filename;
|
|
|
|
struct tr_ip_range * rules;
|
2008-03-30 13:22:45 +00:00
|
|
|
};
|
2008-03-29 18:37:07 +00:00
|
|
|
|
|
|
|
static void
|
2008-03-30 13:22:45 +00:00
|
|
|
blocklistClose( tr_blocklist * b )
|
2008-03-29 18:37:07 +00:00
|
|
|
{
|
2008-03-30 13:22:45 +00:00
|
|
|
if( b->rules )
|
2008-03-29 18:37:07 +00:00
|
|
|
{
|
2008-03-30 13:22:45 +00:00
|
|
|
munmap( b->rules, b->byteCount );
|
|
|
|
close( b->fd );
|
|
|
|
b->rules = NULL;
|
|
|
|
b->ruleCount = 0;
|
|
|
|
b->byteCount = 0;
|
|
|
|
b->fd = -1;
|
2008-03-29 18:37:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-03-30 13:22:45 +00:00
|
|
|
blocklistLoad( tr_blocklist * b )
|
2008-03-29 18:37:07 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int fd;
|
|
|
|
struct stat st;
|
2008-03-30 13:52:55 +00:00
|
|
|
const char * err_fmt = _( "Couldn't read \"%1$s\": %2$s" );
|
2008-03-29 18:37:07 +00:00
|
|
|
|
2008-03-30 13:22:45 +00:00
|
|
|
blocklistClose( b );
|
2008-03-29 18:37:07 +00:00
|
|
|
|
2008-04-05 16:45:35 +00:00
|
|
|
if( stat( b->filename, &st ) == -1 )
|
|
|
|
return;
|
|
|
|
|
2008-03-30 13:22:45 +00:00
|
|
|
fd = open( b->filename, O_RDONLY );
|
2008-09-23 19:11:04 +00:00
|
|
|
if( fd == -1 )
|
|
|
|
{
|
|
|
|
tr_err( err_fmt, b->filename, tr_strerror( errno ) );
|
2008-03-29 18:37:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2008-04-05 16:45:35 +00:00
|
|
|
|
2008-09-05 14:31:52 +00:00
|
|
|
#ifndef WIN32
|
2008-09-23 19:11:04 +00:00
|
|
|
b->rules = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0 );
|
2008-09-05 14:31:52 +00:00
|
|
|
#else
|
|
|
|
b->rules = mmap( NULL, st.st_size, 0, 0, fd, 0 );
|
|
|
|
#endif
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !b->rules )
|
|
|
|
{
|
|
|
|
tr_err( err_fmt, b->filename, tr_strerror( errno ) );
|
2008-03-29 18:37:07 +00:00
|
|
|
close( fd );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-03-30 13:22:45 +00:00
|
|
|
b->byteCount = st.st_size;
|
|
|
|
b->ruleCount = st.st_size / sizeof( struct tr_ip_range );
|
|
|
|
b->fd = fd;
|
2008-06-11 16:15:45 +00:00
|
|
|
|
|
|
|
{
|
2008-10-14 03:39:16 +00:00
|
|
|
char * base = tr_basename( b->filename );
|
|
|
|
tr_inf( _( "Blocklist \"%s\" contains %'zu entries" ), base, b->ruleCount );
|
|
|
|
tr_free( base );
|
2008-06-11 16:15:45 +00:00
|
|
|
}
|
2008-03-29 21:05:51 +00:00
|
|
|
}
|
|
|
|
|
2008-03-30 00:00:28 +00:00
|
|
|
static void
|
2008-04-01 19:20:21 +00:00
|
|
|
blocklistEnsureLoaded( tr_blocklist * b )
|
2008-03-30 00:00:28 +00:00
|
|
|
{
|
2008-03-30 13:22:45 +00:00
|
|
|
if( !b->rules )
|
|
|
|
blocklistLoad( b );
|
2008-03-30 00:00:28 +00:00
|
|
|
}
|
|
|
|
|
2008-03-29 21:05:51 +00:00
|
|
|
static int
|
2008-09-23 19:11:04 +00:00
|
|
|
compareAddressToRange( const void * va,
|
|
|
|
const void * vb )
|
2008-03-29 21:05:51 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
const uint32_t * a = va;
|
2008-03-29 21:05:51 +00:00
|
|
|
const struct tr_ip_range * b = vb;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-03-29 21:05:51 +00:00
|
|
|
if( *a < b->begin ) return -1;
|
|
|
|
if( *a > b->end ) return 1;
|
|
|
|
return 0;
|
2008-03-29 18:37:07 +00:00
|
|
|
}
|
|
|
|
|
2008-03-30 13:22:45 +00:00
|
|
|
static void
|
|
|
|
blocklistDelete( tr_blocklist * b )
|
|
|
|
{
|
|
|
|
blocklistClose( b );
|
|
|
|
unlink( b->filename );
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
2008-04-01 19:20:21 +00:00
|
|
|
**** PACKAGE-VISIBLE
|
2008-03-30 13:22:45 +00:00
|
|
|
***/
|
|
|
|
|
|
|
|
tr_blocklist *
|
2008-09-23 19:11:04 +00:00
|
|
|
_tr_blocklistNew( const char * filename,
|
|
|
|
int isEnabled )
|
2008-03-30 13:22:45 +00:00
|
|
|
{
|
|
|
|
tr_blocklist * b;
|
|
|
|
|
|
|
|
b = tr_new0( tr_blocklist, 1 );
|
|
|
|
b->fd = -1;
|
|
|
|
b->filename = tr_strdup( filename );
|
|
|
|
b->isEnabled = isEnabled;
|
|
|
|
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2008-06-11 16:15:45 +00:00
|
|
|
const char*
|
|
|
|
_tr_blocklistGetFilename( const tr_blocklist * b )
|
|
|
|
{
|
|
|
|
return b->filename;
|
|
|
|
}
|
|
|
|
|
2008-03-30 13:22:45 +00:00
|
|
|
void
|
2008-04-01 19:20:21 +00:00
|
|
|
_tr_blocklistFree( tr_blocklist * b )
|
2008-03-30 13:22:45 +00:00
|
|
|
{
|
|
|
|
blocklistClose( b );
|
|
|
|
tr_free( b->filename );
|
|
|
|
tr_free( b );
|
|
|
|
}
|
|
|
|
|
2008-03-29 23:12:34 +00:00
|
|
|
int
|
2008-06-11 16:15:45 +00:00
|
|
|
_tr_blocklistExists( const tr_blocklist * b )
|
2008-03-29 23:12:34 +00:00
|
|
|
{
|
2008-06-11 16:15:45 +00:00
|
|
|
struct stat st;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-06-11 16:15:45 +00:00
|
|
|
return !stat( b->filename, &st );
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
_tr_blocklistGetRuleCount( const tr_blocklist * b )
|
|
|
|
{
|
|
|
|
blocklistEnsureLoaded( (tr_blocklist*)b );
|
2008-03-30 13:22:45 +00:00
|
|
|
|
|
|
|
return b->ruleCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-04-01 19:20:21 +00:00
|
|
|
_tr_blocklistIsEnabled( tr_blocklist * b )
|
2008-03-30 13:22:45 +00:00
|
|
|
{
|
2008-04-01 19:20:21 +00:00
|
|
|
return b->isEnabled;
|
2008-03-29 23:12:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
_tr_blocklistSetEnabled( tr_blocklist * b,
|
|
|
|
int isEnabled )
|
2008-03-29 23:12:34 +00:00
|
|
|
{
|
2008-04-01 19:20:21 +00:00
|
|
|
b->isEnabled = isEnabled ? 1 : 0;
|
2008-03-29 23:12:34 +00:00
|
|
|
}
|
|
|
|
|
2008-03-29 18:37:07 +00:00
|
|
|
int
|
2008-12-03 05:28:09 +00:00
|
|
|
_tr_blocklistHasAddress( tr_blocklist * b,
|
|
|
|
const tr_address * addr )
|
2008-03-29 18:37:07 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
uint32_t needle;
|
2008-03-29 21:05:51 +00:00
|
|
|
const struct tr_ip_range * range;
|
|
|
|
|
2008-12-20 02:56:44 +00:00
|
|
|
assert( tr_isAddress( addr ) );
|
|
|
|
|
|
|
|
if( !b->isEnabled || addr->type == TR_AF_INET6 )
|
2008-03-30 13:22:45 +00:00
|
|
|
return 0;
|
2008-03-29 23:12:34 +00:00
|
|
|
|
2008-04-01 19:20:21 +00:00
|
|
|
blocklistEnsureLoaded( b );
|
2009-12-14 18:47:45 +00:00
|
|
|
|
|
|
|
if( !b->rules || !b->ruleCount )
|
2008-03-30 13:22:45 +00:00
|
|
|
return 0;
|
2008-03-29 18:37:07 +00:00
|
|
|
|
2008-12-02 03:41:58 +00:00
|
|
|
needle = ntohl( addr->addr.addr4.s_addr );
|
2008-03-29 18:37:07 +00:00
|
|
|
|
2008-03-29 21:05:51 +00:00
|
|
|
range = bsearch( &needle,
|
2008-03-30 13:22:45 +00:00
|
|
|
b->rules,
|
|
|
|
b->ruleCount,
|
2008-09-23 19:11:04 +00:00
|
|
|
sizeof( struct tr_ip_range ),
|
2008-03-29 21:05:51 +00:00
|
|
|
compareAddressToRange );
|
|
|
|
|
|
|
|
return range != NULL;
|
|
|
|
}
|
2008-03-29 18:37:07 +00:00
|
|
|
|
2008-03-30 00:00:28 +00:00
|
|
|
int
|
2008-04-01 19:20:21 +00:00
|
|
|
_tr_blocklistSetContent( tr_blocklist * b,
|
2008-09-23 19:11:04 +00:00
|
|
|
const char * filename )
|
2008-03-29 18:37:07 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
FILE * in;
|
|
|
|
FILE * out;
|
|
|
|
char * line;
|
|
|
|
int lineCount = 0;
|
2008-03-30 13:52:55 +00:00
|
|
|
const char * err_fmt = _( "Couldn't read \"%1$s\": %2$s" );
|
2008-03-29 18:37:07 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !filename )
|
|
|
|
{
|
2008-03-30 13:22:45 +00:00
|
|
|
blocklistDelete( b );
|
2008-03-30 00:00:28 +00:00
|
|
|
return 0;
|
2008-03-29 18:37:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
in = fopen( filename, "r" );
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !in )
|
|
|
|
{
|
|
|
|
tr_err( err_fmt, filename, tr_strerror( errno ) );
|
2008-03-30 00:00:28 +00:00
|
|
|
return 0;
|
2008-03-29 18:37:07 +00:00
|
|
|
}
|
|
|
|
|
2008-03-30 13:22:45 +00:00
|
|
|
blocklistClose( b );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-03-30 13:22:45 +00:00
|
|
|
out = fopen( b->filename, "wb+" );
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !out )
|
|
|
|
{
|
2008-03-30 13:52:55 +00:00
|
|
|
tr_err( err_fmt, b->filename, tr_strerror( errno ) );
|
2008-03-29 18:37:07 +00:00
|
|
|
fclose( in );
|
2008-03-30 00:00:28 +00:00
|
|
|
return 0;
|
2008-03-29 18:37:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while( !fggets( &line, in ) )
|
|
|
|
{
|
2008-12-03 05:28:09 +00:00
|
|
|
char * rangeBegin;
|
|
|
|
char * rangeEnd;
|
|
|
|
char * crpos;
|
|
|
|
tr_address addr;
|
2008-03-29 21:05:51 +00:00
|
|
|
struct tr_ip_range range;
|
2008-03-29 18:37:07 +00:00
|
|
|
|
|
|
|
rangeBegin = strrchr( line, ':' );
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !rangeBegin ){ free( line ); continue; }
|
2008-03-29 18:37:07 +00:00
|
|
|
++rangeBegin;
|
|
|
|
|
|
|
|
rangeEnd = strchr( rangeBegin, '-' );
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !rangeEnd ){ free( line ); continue; }
|
2008-03-29 18:37:07 +00:00
|
|
|
*rangeEnd++ = '\0';
|
2008-12-03 05:28:09 +00:00
|
|
|
if(( crpos = strchr( rangeEnd, '\r' )))
|
|
|
|
*crpos = '\0';
|
2008-03-29 18:37:07 +00:00
|
|
|
|
2008-12-02 03:41:58 +00:00
|
|
|
if( !tr_pton( rangeBegin, &addr ) )
|
2008-03-29 21:05:51 +00:00
|
|
|
tr_err( "blocklist skipped invalid address [%s]\n", rangeBegin );
|
2008-12-02 03:41:58 +00:00
|
|
|
range.begin = ntohl( addr.addr.addr4.s_addr );
|
2008-03-29 21:05:51 +00:00
|
|
|
|
2008-12-02 03:41:58 +00:00
|
|
|
if( !tr_pton( rangeEnd, &addr ) )
|
2008-03-29 21:05:51 +00:00
|
|
|
tr_err( "blocklist skipped invalid address [%s]\n", rangeEnd );
|
2008-12-02 03:41:58 +00:00
|
|
|
range.end = ntohl( addr.addr.addr4.s_addr );
|
2008-03-29 18:37:07 +00:00
|
|
|
|
|
|
|
free( line );
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( fwrite( &range, sizeof( struct tr_ip_range ), 1, out ) != 1 )
|
|
|
|
{
|
|
|
|
tr_err( _(
|
|
|
|
"Couldn't save file \"%1$s\": %2$s" ), b->filename,
|
|
|
|
tr_strerror( errno ) );
|
|
|
|
break;
|
2008-03-29 18:37:07 +00:00
|
|
|
}
|
2008-03-29 21:05:51 +00:00
|
|
|
|
|
|
|
++lineCount;
|
2008-03-29 18:37:07 +00:00
|
|
|
}
|
|
|
|
|
2008-06-11 16:15:45 +00:00
|
|
|
{
|
2008-10-14 03:39:16 +00:00
|
|
|
char * base = tr_basename( b->filename );
|
|
|
|
tr_inf( _( "Blocklist \"%1$s\" updated with %2$'d entries" ), base, lineCount );
|
|
|
|
tr_free( base );
|
2008-06-11 16:15:45 +00:00
|
|
|
}
|
|
|
|
|
2008-03-29 21:05:51 +00:00
|
|
|
|
2008-03-29 18:37:07 +00:00
|
|
|
fclose( out );
|
|
|
|
fclose( in );
|
2008-03-29 21:05:51 +00:00
|
|
|
|
2008-03-30 13:22:45 +00:00
|
|
|
blocklistLoad( b );
|
2008-03-30 00:00:28 +00:00
|
|
|
|
|
|
|
return lineCount;
|
2008-03-29 18:37:07 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|