diff options
-rw-r--r-- | ot_stats.h | 10 | ||||
-rw-r--r-- | ot_sync.h | 4 | ||||
-rw-r--r-- | ot_udp.c | 10 | ||||
-rw-r--r-- | ot_vector.c | 12 | ||||
-rw-r--r-- | trackerlogic.c | 21 | ||||
-rw-r--r-- | trackerlogic.h | 22 |
6 files changed, 40 insertions, 39 deletions
@@ -10,13 +10,21 @@ typedef enum { | |||
10 | EVENT_CONNECT, /* UDP only */ | 10 | EVENT_CONNECT, /* UDP only */ |
11 | EVENT_ANNOUNCE, | 11 | EVENT_ANNOUNCE, |
12 | EVENT_SCRAPE, | 12 | EVENT_SCRAPE, |
13 | EVENT_FULLSCRAPE_REQUEST, | ||
14 | EVENT_FULLSCRAPE_REQUEST_GZIP, | ||
13 | EVENT_FULLSCRAPE, /* TCP only */ | 15 | EVENT_FULLSCRAPE, /* TCP only */ |
16 | EVENT_SYNC_IN_REQUEST, | ||
17 | EVENT_SYNC_IN, | ||
18 | EVENT_SYNC_OUT_REQUEST, | ||
19 | EVENT_SYNC_OUT, | ||
14 | EVENT_FAILED_400, | 20 | EVENT_FAILED_400, |
15 | EVENT_FAILED_404, | 21 | EVENT_FAILED_404, |
16 | EVENT_FAILED_505 | 22 | EVENT_FAILED_505 |
17 | } ot_status_event; | 23 | } ot_status_event; |
18 | 24 | ||
25 | void stats_issue_event( ot_status_event event, int is_tcp, size_t event_data ); | ||
19 | size_t return_stats_for_tracker( char *reply, int mode, int format ); | 26 | size_t return_stats_for_tracker( char *reply, int mode, int format ); |
20 | void stats_issue_event( ot_status_event event, int is_tcp, size_t event_data ); | 27 | void stats_init( ); |
28 | void stats_deinit( ); | ||
21 | 29 | ||
22 | #endif | 30 | #endif |
@@ -4,8 +4,6 @@ | |||
4 | #ifndef __OT_SYNC_H__ | 4 | #ifndef __OT_SYNC_H__ |
5 | #define __OT_SYNC_H__ | 5 | #define __OT_SYNC_H__ |
6 | 6 | ||
7 | #include "trackerlogic.h" | ||
8 | |||
9 | #ifdef WANT_TRACKER_SYNC | 7 | #ifdef WANT_TRACKER_SYNC |
10 | enum { SYNC_IN, SYNC_OUT }; | 8 | enum { SYNC_IN, SYNC_OUT }; |
11 | 9 | ||
@@ -13,7 +11,7 @@ void sync_init( ); | |||
13 | void sync_deinit( ); | 11 | void sync_deinit( ); |
14 | void sync_deliver( int64 socket ); | 12 | void sync_deliver( int64 socket ); |
15 | 13 | ||
16 | int add_changeset_to_tracker( ot_byte *data, size_t len ); | 14 | int add_changeset_to_tracker( uint8_t *data, size_t len ); |
17 | #endif | 15 | #endif |
18 | 16 | ||
19 | #endif | 17 | #endif |
@@ -23,10 +23,10 @@ void handle_udp4( int64 serversocket ) { | |||
23 | ot_torrent *torrent; | 23 | ot_torrent *torrent; |
24 | ot_hash *hash = NULL; | 24 | ot_hash *hash = NULL; |
25 | char remoteip[4]; | 25 | char remoteip[4]; |
26 | ot_dword *inpacket = (ot_dword*)static_inbuf; | 26 | uint32_t *inpacket = (uint32_t*)static_inbuf; |
27 | ot_dword *outpacket = (ot_dword*)static_outbuf; | 27 | uint32_t *outpacket = (uint32_t*)static_outbuf; |
28 | ot_dword numwant, left, event; | 28 | uint32_t numwant, left, event; |
29 | ot_word port, remoteport; | 29 | uint16_t port, remoteport; |
30 | size_t r, r_out; | 30 | size_t r, r_out; |
31 | 31 | ||
32 | r = socket_recv4( serversocket, static_inbuf, sizeof( static_inbuf ), remoteip, &remoteport); | 32 | r = socket_recv4( serversocket, static_inbuf, sizeof( static_inbuf ), remoteip, &remoteport); |
@@ -59,7 +59,7 @@ void handle_udp4( int64 serversocket ) { | |||
59 | left = inpacket[64/4] | inpacket[68/4]; | 59 | left = inpacket[64/4] | inpacket[68/4]; |
60 | 60 | ||
61 | event = ntohl( inpacket[80/4] ); | 61 | event = ntohl( inpacket[80/4] ); |
62 | port = *(ot_word*)( static_inbuf + 96 ); | 62 | port = *(uint16_t*)( static_inbuf + 96 ); |
63 | hash = (ot_hash*)( static_inbuf + 16 ); | 63 | hash = (ot_hash*)( static_inbuf + 16 ); |
64 | 64 | ||
65 | OT_SETIP( &peer, remoteip ); | 65 | OT_SETIP( &peer, remoteip ); |
diff --git a/ot_vector.c b/ot_vector.c index aa71279..1cfa4df 100644 --- a/ot_vector.c +++ b/ot_vector.c | |||
@@ -16,7 +16,7 @@ | |||
16 | void *binary_search( const void * const key, const void * base, const size_t member_count, const size_t member_size, | 16 | void *binary_search( const void * const key, const void * base, const size_t member_count, const size_t member_size, |
17 | size_t compare_size, int *exactmatch ) { | 17 | size_t compare_size, int *exactmatch ) { |
18 | size_t mc = member_count; | 18 | size_t mc = member_count; |
19 | ot_byte *lookat = ((ot_byte*)base) + member_size * (member_count >> 1); | 19 | uint8_t *lookat = ((uint8_t*)base) + member_size * (member_count >> 1); |
20 | *exactmatch = 1; | 20 | *exactmatch = 1; |
21 | 21 | ||
22 | while( mc ) { | 22 | while( mc ) { |
@@ -27,7 +27,7 @@ void *binary_search( const void * const key, const void * base, const size_t mem | |||
27 | --mc; | 27 | --mc; |
28 | } | 28 | } |
29 | mc >>= 1; | 29 | mc >>= 1; |
30 | lookat = ((ot_byte*)base) + member_size * (mc >> 1); | 30 | lookat = ((uint8_t*)base) + member_size * (mc >> 1); |
31 | } | 31 | } |
32 | *exactmatch = 0; | 32 | *exactmatch = 0; |
33 | return (void*)lookat; | 33 | return (void*)lookat; |
@@ -41,22 +41,22 @@ void *binary_search( const void * const key, const void * base, const size_t mem | |||
41 | took place. If resizing the vector failed, NULL is returned, else the pointer to the object in vector. | 41 | took place. If resizing the vector failed, NULL is returned, else the pointer to the object in vector. |
42 | */ | 42 | */ |
43 | void *vector_find_or_insert( ot_vector *vector, void *key, size_t member_size, size_t compare_size, int *exactmatch ) { | 43 | void *vector_find_or_insert( ot_vector *vector, void *key, size_t member_size, size_t compare_size, int *exactmatch ) { |
44 | ot_byte *match = binary_search( key, vector->data, vector->size, member_size, compare_size, exactmatch ); | 44 | uint8_t *match = binary_search( key, vector->data, vector->size, member_size, compare_size, exactmatch ); |
45 | 45 | ||
46 | if( *exactmatch ) return match; | 46 | if( *exactmatch ) return match; |
47 | 47 | ||
48 | if( vector->size + 1 >= vector->space ) { | 48 | if( vector->size + 1 >= vector->space ) { |
49 | size_t new_space = vector->space ? OT_VECTOR_GROW_RATIO * vector->space : OT_VECTOR_MIN_MEMBERS; | 49 | size_t new_space = vector->space ? OT_VECTOR_GROW_RATIO * vector->space : OT_VECTOR_MIN_MEMBERS; |
50 | ot_byte *new_data = realloc( vector->data, new_space * member_size ); | 50 | uint8_t *new_data = realloc( vector->data, new_space * member_size ); |
51 | if( !new_data ) return NULL; | 51 | if( !new_data ) return NULL; |
52 | 52 | ||
53 | /* Adjust pointer if it moved by realloc */ | 53 | /* Adjust pointer if it moved by realloc */ |
54 | match = new_data + (match - (ot_byte*)vector->data); | 54 | match = new_data + (match - (uint8_t*)vector->data); |
55 | 55 | ||
56 | vector->data = new_data; | 56 | vector->data = new_data; |
57 | vector->space = new_space; | 57 | vector->space = new_space; |
58 | } | 58 | } |
59 | memmove( match + member_size, match, ((ot_byte*)vector->data) + member_size * vector->size - match ); | 59 | memmove( match + member_size, match, ((uint8_t*)vector->data) + member_size * vector->size - match ); |
60 | vector->size++; | 60 | vector->size++; |
61 | return match; | 61 | return match; |
62 | } | 62 | } |
diff --git a/trackerlogic.c b/trackerlogic.c index 756ed0f..47548fa 100644 --- a/trackerlogic.c +++ b/trackerlogic.c | |||
@@ -15,6 +15,7 @@ | |||
15 | /* Libowfat */ | 15 | /* Libowfat */ |
16 | #include "scan.h" | 16 | #include "scan.h" |
17 | #include "byte.h" | 17 | #include "byte.h" |
18 | #include "io.h" | ||
18 | 19 | ||
19 | /* Opentracker */ | 20 | /* Opentracker */ |
20 | #include "trackerlogic.h" | 21 | #include "trackerlogic.h" |
@@ -163,9 +164,9 @@ size_t return_peers_for_torrent( ot_hash *hash, size_t amount, char *reply, int | |||
163 | if( is_tcp ) | 164 | if( is_tcp ) |
164 | r += sprintf( r, "d8:completei%zde10:incompletei%zde8:intervali%ie5:peers%zd:", peer_list->seed_count, peer_list->peer_count-peer_list->seed_count, OT_CLIENT_REQUEST_INTERVAL_RANDOM, 6*amount ); | 165 | r += sprintf( r, "d8:completei%zde10:incompletei%zde8:intervali%ie5:peers%zd:", peer_list->seed_count, peer_list->peer_count-peer_list->seed_count, OT_CLIENT_REQUEST_INTERVAL_RANDOM, 6*amount ); |
165 | else { | 166 | else { |
166 | *(ot_dword*)(r+0) = htonl( OT_CLIENT_REQUEST_INTERVAL_RANDOM ); | 167 | *(uint32_t*)(r+0) = htonl( OT_CLIENT_REQUEST_INTERVAL_RANDOM ); |
167 | *(ot_dword*)(r+4) = htonl( peer_list->peer_count ); | 168 | *(uint32_t*)(r+4) = htonl( peer_list->peer_count ); |
168 | *(ot_dword*)(r+8) = htonl( peer_list->seed_count ); | 169 | *(uint32_t*)(r+8) = htonl( peer_list->seed_count ); |
169 | r += 12; | 170 | r += 12; |
170 | } | 171 | } |
171 | 172 | ||
@@ -216,7 +217,7 @@ size_t return_udp_scrape_for_torrent( ot_hash *hash, char *reply ) { | |||
216 | if( !exactmatch ) { | 217 | if( !exactmatch ) { |
217 | memset( reply, 0, 12); | 218 | memset( reply, 0, 12); |
218 | } else { | 219 | } else { |
219 | ot_dword *r = (ot_dword*) reply; | 220 | uint32_t *r = (uint32_t*) reply; |
220 | 221 | ||
221 | if( clean_single_torrent( torrent ) ) { | 222 | if( clean_single_torrent( torrent ) ) { |
222 | vector_remove_torrent( torrents_list, torrent ); | 223 | vector_remove_torrent( torrents_list, torrent ); |
@@ -273,8 +274,8 @@ size_t remove_peer_from_torrent( ot_hash *hash, ot_peer *peer, char *reply, int | |||
273 | return sprintf( reply, "d8:completei0e10:incompletei0e8:intervali%ie5:peers0:e", OT_CLIENT_REQUEST_INTERVAL_RANDOM ); | 274 | return sprintf( reply, "d8:completei0e10:incompletei0e8:intervali%ie5:peers0:e", OT_CLIENT_REQUEST_INTERVAL_RANDOM ); |
274 | 275 | ||
275 | /* Create fake packet to satisfy parser on the other end */ | 276 | /* Create fake packet to satisfy parser on the other end */ |
276 | ((ot_dword*)reply)[2] = htonl( OT_CLIENT_REQUEST_INTERVAL_RANDOM ); | 277 | ((uint32_t*)reply)[2] = htonl( OT_CLIENT_REQUEST_INTERVAL_RANDOM ); |
277 | ((ot_dword*)reply)[3] = ((ot_dword*)reply)[4] = 0; | 278 | ((uint32_t*)reply)[3] = ((uint32_t*)reply)[4] = 0; |
278 | return (size_t)20; | 279 | return (size_t)20; |
279 | } | 280 | } |
280 | 281 | ||
@@ -299,9 +300,9 @@ exit_loop: | |||
299 | } | 300 | } |
300 | 301 | ||
301 | /* else { Handle UDP reply */ | 302 | /* else { Handle UDP reply */ |
302 | ((ot_dword*)reply)[2] = htonl( OT_CLIENT_REQUEST_INTERVAL_RANDOM ); | 303 | ((uint32_t*)reply)[2] = htonl( OT_CLIENT_REQUEST_INTERVAL_RANDOM ); |
303 | ((ot_dword*)reply)[3] = peer_list->peer_count - peer_list->seed_count; | 304 | ((uint32_t*)reply)[3] = peer_list->peer_count - peer_list->seed_count; |
304 | ((ot_dword*)reply)[4] = peer_list->seed_count; | 305 | ((uint32_t*)reply)[4] = peer_list->seed_count; |
305 | 306 | ||
306 | mutex_bucket_unlock_by_hash( hash ); | 307 | mutex_bucket_unlock_by_hash( hash ); |
307 | return (size_t)20; | 308 | return (size_t)20; |
@@ -322,6 +323,7 @@ int trackerlogic_init( const char * const serverdir ) { | |||
322 | #ifdef WANT_TRACKER_SYNC | 323 | #ifdef WANT_TRACKER_SYNC |
323 | sync_init( ); | 324 | sync_init( ); |
324 | #endif | 325 | #endif |
326 | stats_init( ); | ||
325 | return 0; | 327 | return 0; |
326 | } | 328 | } |
327 | 329 | ||
@@ -343,6 +345,7 @@ void trackerlogic_deinit( void ) { | |||
343 | } | 345 | } |
344 | 346 | ||
345 | /* Deinitialise background worker threads */ | 347 | /* Deinitialise background worker threads */ |
348 | stats_deinit( ); | ||
346 | #ifdef WANT_TRACKER_SYNC | 349 | #ifdef WANT_TRACKER_SYNC |
347 | sync_deinit( ); | 350 | sync_deinit( ); |
348 | #endif | 351 | #endif |
diff --git a/trackerlogic.h b/trackerlogic.h index 1eff6df..eefc038 100644 --- a/trackerlogic.h +++ b/trackerlogic.h | |||
@@ -9,14 +9,7 @@ | |||
9 | #include <time.h> | 9 | #include <time.h> |
10 | #include <stdint.h> | 10 | #include <stdint.h> |
11 | 11 | ||
12 | /* Should be called BYTE, WORD, DWORD - but some OSs already have that and there's no #iftypedef */ | 12 | typedef uint8_t ot_hash[20]; |
13 | /* They mark memory used as data instead of integer or human readable string - | ||
14 | they should be cast before used as integer/text */ | ||
15 | typedef uint8_t ot_byte; | ||
16 | typedef uint16_t ot_word; | ||
17 | typedef uint32_t ot_dword; | ||
18 | |||
19 | typedef ot_byte ot_hash[20]; | ||
20 | typedef time_t ot_time; | 13 | typedef time_t ot_time; |
21 | 14 | ||
22 | /* Some tracker behaviour tunable */ | 15 | /* Some tracker behaviour tunable */ |
@@ -46,20 +39,19 @@ typedef time_t ot_time; | |||
46 | #define OT_POOLS_TIMEOUT (60*5) | 39 | #define OT_POOLS_TIMEOUT (60*5) |
47 | 40 | ||
48 | /* From opentracker.c */ | 41 | /* From opentracker.c */ |
49 | extern time_t ot_start_time; | ||
50 | extern time_t g_now; | 42 | extern time_t g_now; |
51 | #define NOW (g_now/OT_POOLS_TIMEOUT) | 43 | #define NOW (g_now/OT_POOLS_TIMEOUT) |
52 | 44 | ||
53 | typedef struct { | 45 | typedef struct { |
54 | ot_byte data[8]; | 46 | uint8_t data[8]; |
55 | } ot_peer; | 47 | } ot_peer; |
56 | static const ot_byte PEER_FLAG_SEEDING = 0x80; | 48 | static const uint8_t PEER_FLAG_SEEDING = 0x80; |
57 | static const ot_byte PEER_FLAG_COMPLETED = 0x40; | 49 | static const uint8_t PEER_FLAG_COMPLETED = 0x40; |
58 | static const ot_byte PEER_FLAG_STOPPED = 0x20; | 50 | static const uint8_t PEER_FLAG_STOPPED = 0x20; |
59 | 51 | ||
60 | #define OT_SETIP( peer, ip ) memmove((peer),(ip),4); | 52 | #define OT_SETIP( peer, ip ) memmove((peer),(ip),4); |
61 | #define OT_SETPORT( peer, port ) memmove(((ot_byte*)peer)+4,(port),2); | 53 | #define OT_SETPORT( peer, port ) memmove(((uint8_t*)peer)+4,(port),2); |
62 | #define OT_FLAG(peer) (((ot_byte*)(peer))[6]) | 54 | #define OT_FLAG(peer) (((uint8_t*)(peer))[6]) |
63 | 55 | ||
64 | #define OT_PEER_COMPARE_SIZE ((size_t)6) | 56 | #define OT_PEER_COMPARE_SIZE ((size_t)6) |
65 | #define OT_HASH_COMPARE_SIZE (sizeof(ot_hash)) | 57 | #define OT_HASH_COMPARE_SIZE (sizeof(ot_hash)) |