/* Libowfat */
#include "byte.h"
+#include "io.h"
/* Opentracker */
#include "trackerlogic.h"
#include "ot_mutex.h"
#include "ot_stats.h"
+#ifndef NO_FULLSCRAPE_LOGGING
+#define LOG_TO_STDERR( ... ) fprintf( stderr, __VA_ARGS__ )
+#else
+#define LOG_TO_STDERR( ... )
+#endif
+
/* Clumsy counters... to be rethought */
static unsigned long long ot_overall_tcp_connections = 0;
static unsigned long long ot_overall_udp_connections = 0;
static unsigned long long ot_overall_tcp_connects = 0;
static unsigned long long ot_overall_udp_connects = 0;
static unsigned long long ot_full_scrape_count = 0;
+static unsigned long long ot_full_scrape_request_count = 0;
static unsigned long long ot_full_scrape_size = 0;
+static time_t ot_start_time;
+
/* Converter function from memory to human readable hex strings */
-static char*to_hex(char*d,ot_byte*s){char*m="0123456789ABCDEF";char *t=d;char*e=d+40;while(d<e){*d++=m[*s>>4];*d++=m[*s++&15];}*d=0;return t;}
+static char*to_hex(char*d,uint8_t*s){char*m="0123456789ABCDEF";char *t=d;char*e=d+40;while(d<e){*d++=m[*s>>4];*d++=m[*s++&15];}*d=0;return t;}
typedef struct { size_t val; ot_torrent * torrent; } ot_record;
/* This function collects 4096 /24s in 4096 possible
malloc blocks
*/
-static size_t stats_slash24s_txt( char * reply, size_t amount, ot_dword thresh ) {
+static size_t stats_slash24s_txt( char * reply, size_t amount, uint32_t thresh ) {
#define NUM_TOPBITS 12
#define NUM_LOWBITS (24-NUM_TOPBITS)
#define NUM_S24S (1<<NUM_LOWBITS)
#define MSK_S24S (NUM_S24S-1)
- ot_dword *counts[ NUM_BUFS ];
- ot_dword slash24s[amount*2]; /* first dword amount, second dword subnet */
+ uint32_t *counts[ NUM_BUFS ];
+ uint32_t slash24s[amount*2]; /* first dword amount, second dword subnet */
int bucket;
size_t i, j, k, l;
char *r = reply;
byte_zero( counts, sizeof( counts ) );
- byte_zero( slash24s, amount * 2 * sizeof(ot_dword) );
+ byte_zero( slash24s, amount * 2 * sizeof(uint32_t) );
r += sprintf( r, "Stats for all /24s with more than %u announced torrents:\n\n", thresh );
ot_peer *peers = peer_list->peers[k].data;
size_t numpeers = peer_list->peers[k].size;
for( l=0; l<numpeers; ++l ) {
- ot_dword s24 = ntohl(*(ot_dword*)(peers+l)) >> 8;
- ot_dword *count = counts[ s24 >> NUM_LOWBITS ];
+ uint32_t s24 = ntohl(*(uint32_t*)(peers+l)) >> 8;
+ uint32_t *count = counts[ s24 >> NUM_LOWBITS ];
if( !count ) {
- count = malloc( sizeof(ot_dword) * NUM_S24S );
+ count = malloc( sizeof(uint32_t) * NUM_S24S );
if( !count )
goto bailout_cleanup;
- byte_zero( count, sizeof( ot_dword ) * NUM_S24S );
+ byte_zero( count, sizeof( uint32_t ) * NUM_S24S );
counts[ s24 >> NUM_LOWBITS ] = count;
}
count[ s24 & MSK_S24S ]++;
k = l = 0; /* Debug: count allocated bufs */
for( i=0; i < NUM_BUFS; ++i ) {
- ot_dword *count = counts[i];
+ uint32_t *count = counts[i];
if( !counts[i] )
continue;
++k; /* Debug: count allocated bufs */
while( ( insert_pos >= 0 ) && ( count[j] > slash24s[ 2 * insert_pos ] ) )
--insert_pos;
++insert_pos;
- memmove( slash24s + 2 * ( insert_pos + 1 ), slash24s + 2 * ( insert_pos ), 2 * sizeof( ot_dword ) * ( amount - insert_pos - 1 ) );
+ memmove( slash24s + 2 * ( insert_pos + 1 ), slash24s + 2 * ( insert_pos ), 2 * sizeof( uint32_t ) * ( amount - insert_pos - 1 ) );
slash24s[ 2 * insert_pos ] = count[j];
slash24s[ 2 * insert_pos + 1 ] = ( i << NUM_TOPBITS ) + j;
if( slash24s[ 2 * amount - 2 ] > thresh )
for( i=0; i < amount; ++i )
if( slash24s[ 2*i ] >= thresh ) {
- ot_dword ip = slash24s[ 2*i +1 ];
+ uint32_t ip = slash24s[ 2*i +1 ];
r += sprintf( r, "% 10ld %d.%d.%d.0/24\n", (long)slash24s[ 2*i ], (int)(ip >> 16), (int)(255 & ( ip >> 8 )), (int)(ip & 255) );
}
case EVENT_ANNOUNCE:
if( is_tcp ) ot_overall_tcp_successfulannounces++; else ot_overall_udp_successfulannounces++;
break;
+ case EVENT_CONNECT:
+ if( is_tcp ) ot_overall_tcp_connects++; else ot_overall_udp_connects++;
+ break;
case EVENT_SCRAPE:
if( is_tcp ) ot_overall_tcp_successfulscrapes++; else ot_overall_udp_successfulscrapes++;
case EVENT_CONNECT:
ot_full_scrape_count++;
ot_full_scrape_size += event_data;
break;
+ case EVENT_FULLSCRAPE_REQUEST:
+ {
+ char ip[4]; *(int*)ip = is_tcp; /* ugly hack to transfer ip to stats */
+ LOG_TO_STDERR( "[%08d] scrp: %d.%d.%d.%d - FULL SCRAPE\n", (unsigned int)(g_now - ot_start_time), ip[0], ip[1], ip[2], ip[3] );
+ ot_full_scrape_request_count++;
+ }
+ break;
+ case EVENT_FULLSCRAPE_REQUEST_GZIP:
+ {
+ char ip[4]; *(int*)ip = is_tcp; /* ugly hack to transfer ip to stats */
+ LOG_TO_STDERR( "[%08d] scrp: %d.%d.%d.%d - FULL SCRAPE GZIP\n", (unsigned int)(g_now - ot_start_time), ip[0], ip[1], ip[2], ip[3] );
+ ot_full_scrape_request_count++;
+ }
+ break;
+ case EVENT_SYNC_IN_REQUEST:
+ case EVENT_SYNC_IN:
+ case EVENT_SYNC_OUT_REQUEST:
+ case EVENT_SYNC_OUT:
+ break;
default:
break;
}
}
+
+void stats_init( ) {
+ ot_start_time = g_now;
+}
+
+void stats_deinit( ) {
+
+}
+