Browse Source

Reloading peer_list files works in theory

dynamic-accesslists
erdgeist 18 years ago
parent
commit
a364c3f010
  1. 177
      trackerlogic.c

177
trackerlogic.c

@ -1,6 +1,5 @@ @@ -1,6 +1,5 @@
// THIS REALLY BELONGS INTO A HEADER FILE
//
//
#include "trackerlogic.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
@ -9,77 +8,51 @@ @@ -9,77 +8,51 @@
#include <sys/mman.h>
#include <unistd.h>
#include <time.h>
#include <glob.h>
/* Should be called BYTE, WORD, DWORD - but some OSs already have that and there's no #iftypedef */
/* They mark memory used as data instead of integer or human readable string -
they should be cast before used as integer/text */
typedef unsigned char ot_byte;
typedef unsigned short ot_word;
typedef unsigned long ot_dword;
typedef unsigned long ot_time;
typedef ot_byte ot_hash[20];
typedef ot_byte ot_ip[ 4/*0*/ ];
// tunables
const unsigned long OT_TIMEOUT = 2700;
const unsigned long OT_HUGE_FILESIZE = 1024*1024*256; // Thats 256MB per file, enough for 204800 peers of 128 bytes
#define OT_COMPACT_ONLY
#define MEMMOVE memmove
#define BZERO bzero
#define FORMAT_FIXED_STRING sprintf
#define FORMAT_FORMAT_STRING sprintf
#define BINARY_FIND binary_search
#define NOW time(NULL)
typedef struct ot_peer {
#ifndef OT_COMPACT_ONLY
ot_hash id;
ot_hash key;
#endif
ot_ip ip;
ot_word port;
ot_time death;
ot_byte flags;
} *ot_peer;
ot_byte PEER_FLAG_SEEDING = 0x80;
ot_byte PEER_IP_LENGTH_MASK = 0x3f;
typedef struct {
ot_hash hash;
ot_peer peer_list;
unsigned long peer_count;
unsigned long seed_count;
} *ot_torrent;
void *map_file( char *file_name );
void unmap_file( char *file_name, void *map, unsigned long real_size );
// This behaves quite like bsearch but allows to find
// the insertion point for inserts after unsuccessful searches
// in this case exactmatch is 0 on exit
// Helper functions for binary_find
//
int compare_hash( const void *hash1, const void *hash2 ) { return memcmp( hash1, hash2, sizeof( ot_hash )); }
int compare_ip_port( const void *peer1, const void *peer2 ) { return memcmp( &((ot_peer)peer1)->ip, &((ot_peer)peer2)->ip, 6); }
void *binary_search( const void *key, const void *base,
const unsigned long member_count, const unsigned long member_size,
unsigned long member_count, const unsigned long member_size,
int (*compar) (const void *, const void *),
int *exactmatch );
int *exactmatch ) {
ot_byte *lookat = ((ot_byte*)base) + member_size * (member_count >> 1);
*exactmatch = 1;
int compare_hash( const void *hash1, const void *hash2 ) { return memcmp( hash1, hash2, sizeof( ot_hash )); }
int compare_ip_port( const void *peer1, const void *peer2 ) { return memcmp( peer1, peer2, 6); }
while( member_count ) {
int cmp = compar((void*)lookat, key);
if (cmp == 0) return (void *)lookat;
if (cmp < 0) {
base = (void*)(lookat + member_size);
--member_count;
}
member_count >>= 1;
lookat = ((ot_byte*)base) + member_size * (member_count >> 1);
}
*exactmatch = 0;
return (void*)lookat;
}
// Converter function from memory to human readable hex strings
// * definitely not thread safe!!!
//
//
// END OF STUFF THAT BELONGS INTO A HEADER FILE
char ths[1+2*20];char *to_hex(ot_byte*s){char*m="0123456789ABCDEF";char*e=ths+40;char*t=ths;while(t<e){*t++=m[*s>>4];*t++=m[*s++&15];}*t=0;return ths;}
// GLOBAL VARIABLES
//
unsigned long torrents_count = 0;
ot_torrent torrents_list = 0;
ot_byte *scratch_space = 0;
// Converter function from memory to human readable hex strings
// * definitely not thread safe!!!
//
char ths[1+2*20];char *to_hex(ot_byte*s){char*m="0123456789ABCDEF";char*e=ths+40;char*t=ths;while(t<e){*t++=m[*s>>4];*t++=m[*s++&15];}*t=0;return ths;}
#define SETINVALID( i ) (scratch_space[index] = 3);
#define SETSELECTED( i ) (scratch_space[index] = 1);
#define TESTSELECTED( i ) (scratch_space[index] == 1 )
#define TESTSET( i ) (scratch_space[index])
#define RANDOM random()
ot_torrent add_peer_to_torrent( ot_hash hash, ot_peer peer ) {
ot_torrent torrent;
@ -122,12 +95,6 @@ ot_torrent add_peer_to_torrent( ot_hash hash, ot_peer peer ) { @@ -122,12 +95,6 @@ ot_torrent add_peer_to_torrent( ot_hash hash, ot_peer peer ) {
return torrent;
}
#define SETINVALID( i ) (scratch_space[index] = 3);
#define SETSELECTED( i ) (scratch_space[index] = 1);
#define TESTSELECTED( i ) (scratch_space[index] == 1 )
#define TESTSET( i ) (scratch_space[index])
#define RANDOM random()
inline int TESTVALIDPEER( ot_peer p ) { return p->death > NOW; }
// Compiles a list of random peers for a torrent
@ -249,30 +216,8 @@ void dispose_torrent( ot_torrent torrent ) { @@ -249,30 +216,8 @@ void dispose_torrent( ot_torrent torrent ) {
torrents_count--;
}
void *binary_search( const void *key, const void *base,
unsigned long member_count, const unsigned long member_size,
int (*compar) (const void *, const void *),
int *exactmatch ) {
ot_byte *lookat = ((ot_byte*)base) + member_size * (member_count >> 1);
*exactmatch = 1;
while( member_count ) {
int cmp = compar((void*)lookat, key);
if (cmp == 0) return (void *)lookat;
if (cmp < 0) {
base = (void*)(lookat + member_size);
--member_count;
}
member_count >>= 1;
lookat = ((ot_byte*)base) + member_size * (member_count >> 1);
}
*exactmatch = 0;
return (void*)lookat;
}
// This function maps a "huge" file into process space
// * no name will aqcuire anonymous growable memory
// * giving no name will aqcuire anonymous growable memory
// * memory will not be "freed" from systems vm if once used, until unmap_file
// * I guess, we should be checking for more errors...
//
@ -281,6 +226,8 @@ void *map_file( char *file_name ) { @@ -281,6 +226,8 @@ void *map_file( char *file_name ) {
if( file_name ) {
int file_desc=open(file_name,O_RDWR|O_CREAT|O_NDELAY,0644);
if( file_desc < 0) return 0;
lseek( file_desc, OT_HUGE_FILESIZE, SEEK_SET );
map=mmap(0,OT_HUGE_FILESIZE,PROT_READ|PROT_WRITE,MAP_SHARED,file_desc,0);
close(file_desc);
} else
@ -295,14 +242,60 @@ void unmap_file( char *file_name, void *map, unsigned long real_size ) { @@ -295,14 +242,60 @@ void unmap_file( char *file_name, void *map, unsigned long real_size ) {
truncate( file_name, real_size );
}
void count_peers_and_seeds( ot_peer peer_list, unsigned long *peers, unsigned long *seeds ) {
*peers = *seeds = 0;
if( peer_list[*peers].ip )
do {
*seeds += peer_list[*peers++].flags & PEER_FLAG_SEEDING ? 1 : 0;
} while( compare_ip_port( peer_list + *peers, peer_list + *peers - 1 ) < 0 );
}
int init_logic( ) {
glob_t globber;
int i;
scratch_space = map_file( "" );
torrents_list = map_file( "" );
torrents_count = 0;
if( !scratch_space || !torrents_list ) {
if( scratch_space || torrents_list )
unmap_file( "", scratch_space ? (void*)scratch_space : (void*)torrents_list, 0 );
return -1;
}
// Scan directory for filenames in the form [0-9A-F]{20}
// ...
// * I know this looks ugly, but I've seen A-F to match umlauts as well in strange locales
// * lower case for .. better being safe than sorry, this is not expansive here :)
if( !glob(
"[0-9ABCDEFabcdef][0-9ABCDEFabcdef][0-9ABCDEFabcdef][0-9ABCDEFabcdef]"
"[0-9ABCDEFabcdef][0-9ABCDEFabcdef][0-9ABCDEFabcdef][0-9ABCDEFabcdef]"
"[0-9ABCDEFabcdef][0-9ABCDEFabcdef][0-9ABCDEFabcdef][0-9ABCDEFabcdef]"
"[0-9ABCDEFabcdef][0-9ABCDEFabcdef][0-9ABCDEFabcdef][0-9ABCDEFabcdef]"
"[0-9ABCDEFabcdef][0-9ABCDEFabcdef][0-9ABCDEFabcdef][0-9ABCDEFabcdef]"
"[0-9ABCDEFabcdef][0-9ABCDEFabcdef][0-9ABCDEFabcdef][0-9ABCDEFabcdef]"
"[0-9ABCDEFabcdef][0-9ABCDEFabcdef][0-9ABCDEFabcdef][0-9ABCDEFabcdef]"
"[0-9ABCDEFabcdef][0-9ABCDEFabcdef][0-9ABCDEFabcdef][0-9ABCDEFabcdef]"
"[0-9ABCDEFabcdef][0-9ABCDEFabcdef][0-9ABCDEFabcdef][0-9ABCDEFabcdef]"
"[0-9ABCDEFabcdef][0-9ABCDEFabcdef][0-9ABCDEFabcdef][0-9ABCDEFabcdef]"
, GLOB_NOCHECK, 0, &globber) )
{
for( i=0; i<globber.gl_matchc; ++i ) {
#ifdef _DEBUG
printf( "Found dir: %s\n", globber.gl_pathv[i] );
#endif
if( ( torrents_list[torrents_count].peer_list = map_file( globber.gl_pathv[i] ) ) ) {
MEMMOVE( &torrents_list[torrents_count].hash, globber.gl_pathv[i], sizeof( ot_hash ) );
count_peers_and_seeds( torrents_list[torrents_count].peer_list,
&torrents_list[torrents_count].peer_count,
&torrents_list[torrents_count].seed_count );
torrents_count++;
}
}
}
globfree( &globber );
return 0;
}

Loading…
Cancel
Save