summaryrefslogtreecommitdiff
path: root/trackerlogic.h
diff options
context:
space:
mode:
Diffstat (limited to 'trackerlogic.h')
-rw-r--r--trackerlogic.h153
1 files changed, 78 insertions, 75 deletions
diff --git a/trackerlogic.h b/trackerlogic.h
index e43fcdb..022184d 100644
--- a/trackerlogic.h
+++ b/trackerlogic.h
@@ -6,11 +6,11 @@
6#ifndef OT_TRACKERLOGIC_H__ 6#ifndef OT_TRACKERLOGIC_H__
7#define OT_TRACKERLOGIC_H__ 7#define OT_TRACKERLOGIC_H__
8 8
9#include <sys/types.h>
10#include <sys/time.h>
11#include <time.h>
12#include <stdint.h> 9#include <stdint.h>
13#include <stdlib.h> 10#include <stdlib.h>
11#include <sys/time.h>
12#include <sys/types.h>
13#include <time.h>
14 14
15#if defined(__linux__) && defined(WANT_ARC4RANDOM) 15#if defined(__linux__) && defined(WANT_ARC4RANDOM)
16#include <bsd/stdlib.h> 16#include <bsd/stdlib.h>
@@ -22,73 +22,76 @@
22typedef uint8_t ot_hash[20]; 22typedef uint8_t ot_hash[20];
23typedef time_t ot_time; 23typedef time_t ot_time;
24typedef char ot_ip6[16]; 24typedef char ot_ip6[16];
25typedef struct { ot_ip6 address; int bits; } 25typedef struct {
26 ot_net; 26 ot_ip6 address;
27 int bits;
28} ot_net;
27/* List of peers should fit in a single UDP packet (around 1200 bytes) */ 29/* List of peers should fit in a single UDP packet (around 1200 bytes) */
28#define OT_MAX_PEERS_UDP6 66 30#define OT_MAX_PEERS_UDP6 66
29#define OT_MAX_PEERS_UDP4 200 31#define OT_MAX_PEERS_UDP4 200
30 32
31#define OT_IP_SIZE6 16 33#define OT_IP_SIZE6 16
32#define OT_IP_SIZE4 4 34#define OT_IP_SIZE4 4
33#define OT_PORT_SIZE 2 35#define OT_PORT_SIZE 2
34#define OT_FLAG_SIZE 1 36#define OT_FLAG_SIZE 1
35#define OT_TIME_SIZE 1 37#define OT_TIME_SIZE 1
36 38
37/* Some tracker behaviour tunable */ 39/* Some tracker behaviour tunable */
38#define OT_CLIENT_TIMEOUT 30 40#define OT_CLIENT_TIMEOUT 30
39#define OT_CLIENT_TIMEOUT_CHECKINTERVAL 10 41#define OT_CLIENT_TIMEOUT_CHECKINTERVAL 10
40#define OT_CLIENT_TIMEOUT_SEND (60*15) 42#define OT_CLIENT_TIMEOUT_SEND (60 * 15)
41#define OT_CLIENT_REQUEST_INTERVAL (60*30) 43#define OT_CLIENT_REQUEST_INTERVAL (60 * 30)
42#define OT_CLIENT_REQUEST_VARIATION (60*6) 44#define OT_CLIENT_REQUEST_VARIATION (60 * 6)
43 45
44#define OT_TORRENT_TIMEOUT_HOURS 24 46#define OT_TORRENT_TIMEOUT_HOURS 24
45#define OT_TORRENT_TIMEOUT (60*OT_TORRENT_TIMEOUT_HOURS) 47#define OT_TORRENT_TIMEOUT (60 * OT_TORRENT_TIMEOUT_HOURS)
46 48
47#define OT_CLIENT_REQUEST_INTERVAL_RANDOM ( OT_CLIENT_REQUEST_INTERVAL - OT_CLIENT_REQUEST_VARIATION/2 + (int)( nrand48(ws->rand48_state) % OT_CLIENT_REQUEST_VARIATION ) ) 49#define OT_CLIENT_REQUEST_INTERVAL_RANDOM \
50 (OT_CLIENT_REQUEST_INTERVAL - OT_CLIENT_REQUEST_VARIATION / 2 + (int)(nrand48(ws->rand48_state) % OT_CLIENT_REQUEST_VARIATION))
48 51
49/* If WANT_MODEST_FULLSCRAPES is on, ip addresses may not 52/* If WANT_MODEST_FULLSCRAPES is on, ip addresses may not
50 fullscrape more frequently than this amount in seconds */ 53 fullscrape more frequently than this amount in seconds */
51#define OT_MODEST_PEER_TIMEOUT (60*5) 54#define OT_MODEST_PEER_TIMEOUT (60 * 5)
52 55
53/* If peers come back before 10 minutes, don't live sync them */ 56/* If peers come back before 10 minutes, don't live sync them */
54#define OT_CLIENT_SYNC_RENEW_BOUNDARY 10 57#define OT_CLIENT_SYNC_RENEW_BOUNDARY 10
55 58
56/* Number of tracker admin ip addresses allowed */ 59/* Number of tracker admin ip addresses allowed */
57#define OT_ADMINIP_MAX 64 60#define OT_ADMINIP_MAX 64
58#define OT_MAX_THREADS 64 61#define OT_MAX_THREADS 64
59 62
60/* Number of minutes after announce before peer is removed */ 63/* Number of minutes after announce before peer is removed */
61#define OT_PEER_TIMEOUT 45 64#define OT_PEER_TIMEOUT 45
62 65
63/* We maintain a list of 1024 pointers to sorted list of ot_torrent structs 66/* We maintain a list of 1024 pointers to sorted list of ot_torrent structs
64 Sort key is, of course, its hash */ 67 Sort key is, of course, its hash */
65#define OT_BUCKET_COUNT_BITS 10 68#define OT_BUCKET_COUNT_BITS 10
66 69
67#define OT_BUCKET_COUNT (1<<OT_BUCKET_COUNT_BITS) 70#define OT_BUCKET_COUNT (1 << OT_BUCKET_COUNT_BITS)
68#define OT_BUCKET_COUNT_SHIFT (32-OT_BUCKET_COUNT_BITS) 71#define OT_BUCKET_COUNT_SHIFT (32 - OT_BUCKET_COUNT_BITS)
69 72
70/* if _DEBUG_RANDOMTORRENTS is set, this is the amount of torrents to create 73/* if _DEBUG_RANDOMTORRENTS is set, this is the amount of torrents to create
71 on startup */ 74 on startup */
72#define RANDOMTORRENTS (1024*1024*1) 75#define RANDOMTORRENTS (1024 * 1024 * 1)
73 76
74/* From opentracker.c */ 77/* From opentracker.c */
75extern time_t g_now_seconds; 78extern time_t g_now_seconds;
76extern volatile int g_opentracker_running; 79extern volatile int g_opentracker_running;
77#define g_now_minutes (g_now_seconds/60) 80#define g_now_minutes (g_now_seconds / 60)
78 81
79extern uint32_t g_tracker_id; 82extern uint32_t g_tracker_id;
80typedef enum { FLAG_TCP, FLAG_UDP, FLAG_MCA, FLAG_SELFPIPE } PROTO_FLAG; 83typedef enum { FLAG_TCP, FLAG_UDP, FLAG_MCA, FLAG_SELFPIPE } PROTO_FLAG;
81 84
82#define OT_PEER_COMPARE_SIZE6 ((OT_IP_SIZE6)+(OT_PORT_SIZE)) 85#define OT_PEER_COMPARE_SIZE6 ((OT_IP_SIZE6) + (OT_PORT_SIZE))
83#define OT_PEER_COMPARE_SIZE4 ((OT_IP_SIZE4)+(OT_PORT_SIZE)) 86#define OT_PEER_COMPARE_SIZE4 ((OT_IP_SIZE4) + (OT_PORT_SIZE))
84#define OT_PEER_COMPARE_SIZE_FROM_PEER_SIZE(PEER_SIZE) ((PEER_SIZE)-(OT_TIME_SIZE)-(OT_FLAG_SIZE)) 87#define OT_PEER_COMPARE_SIZE_FROM_PEER_SIZE(PEER_SIZE) ((PEER_SIZE) - (OT_TIME_SIZE) - (OT_FLAG_SIZE))
85 88
86#define OT_PEER_SIZE6 ((OT_TIME_SIZE)+(OT_FLAG_SIZE)+(OT_PEER_COMPARE_SIZE6)) 89#define OT_PEER_SIZE6 ((OT_TIME_SIZE) + (OT_FLAG_SIZE) + (OT_PEER_COMPARE_SIZE6))
87#define OT_PEER_SIZE4 ((OT_TIME_SIZE)+(OT_FLAG_SIZE)+(OT_PEER_COMPARE_SIZE4)) 90#define OT_PEER_SIZE4 ((OT_TIME_SIZE) + (OT_FLAG_SIZE) + (OT_PEER_COMPARE_SIZE4))
88 91
89typedef uint8_t ot_peer; /* Generic pointer to a v6 or v4 peer */ 92typedef uint8_t ot_peer; /* Generic pointer to a v6 or v4 peer */
90typedef uint8_t ot_peer6[OT_PEER_SIZE6]; 93typedef uint8_t ot_peer6[OT_PEER_SIZE6];
91typedef uint8_t ot_peer4[OT_PEER_SIZE4]; 94typedef uint8_t ot_peer4[OT_PEER_SIZE4];
92static const uint8_t PEER_FLAG_SEEDING = 0x80; 95static const uint8_t PEER_FLAG_SEEDING = 0x80;
93static const uint8_t PEER_FLAG_COMPLETED = 0x40; 96static const uint8_t PEER_FLAG_COMPLETED = 0x40;
94static const uint8_t PEER_FLAG_STOPPED = 0x20; 97static const uint8_t PEER_FLAG_STOPPED = 0x20;
@@ -96,20 +99,20 @@ static const uint8_t PEER_FLAG_FROM_SYNC = 0x10;
96static const uint8_t PEER_FLAG_LEECHING = 0x00; 99static const uint8_t PEER_FLAG_LEECHING = 0x00;
97 100
98/* Takes an ot_peer6 and returns the proper pointer to the peer and sets peer_size */ 101/* Takes an ot_peer6 and returns the proper pointer to the peer and sets peer_size */
99ot_peer *peer_from_peer6(ot_peer6 *peer, size_t *peer_size); 102ot_peer *peer_from_peer6(ot_peer6 *peer, size_t *peer_size);
100size_t peer_size_from_peer6(ot_peer6 *peer); 103size_t peer_size_from_peer6(ot_peer6 *peer);
101 104
102/* New style */ 105/* New style */
103#define OT_SETIP(peer,ip) memcpy((uint8_t*)(peer),(ip),OT_IP_SIZE6) 106#define OT_SETIP(peer, ip) memcpy((uint8_t *)(peer), (ip), OT_IP_SIZE6)
104#define OT_SETPORT(peer,port) memcpy(((uint8_t*)(peer))+(OT_IP_SIZE6),(port),2) 107#define OT_SETPORT(peer, port) memcpy(((uint8_t *)(peer)) + (OT_IP_SIZE6), (port), 2)
105#define OT_PEERFLAG(peer) (((uint8_t*)(peer))[(OT_IP_SIZE6)+2]) 108#define OT_PEERFLAG(peer) (((uint8_t *)(peer))[(OT_IP_SIZE6) + 2])
106#define OT_PEERFLAG_D(peer,peersize) (((uint8_t*)(peer))[(peersize)-2]) 109#define OT_PEERFLAG_D(peer, peersize) (((uint8_t *)(peer))[(peersize) - 2])
107#define OT_PEERTIME(peer,peersize) (((uint8_t*)(peer))[(peersize)-1]) 110#define OT_PEERTIME(peer, peersize) (((uint8_t *)(peer))[(peersize) - 1])
108 111
109#define PEERS_BENCODED6 "6:peers6" 112#define PEERS_BENCODED6 "6:peers6"
110#define PEERS_BENCODED4 "5:peers" 113#define PEERS_BENCODED4 "5:peers"
111 114
112#define OT_HASH_COMPARE_SIZE (sizeof(ot_hash)) 115#define OT_HASH_COMPARE_SIZE (sizeof(ot_hash))
113 116
114struct ot_peerlist; 117struct ot_peerlist;
115typedef struct ot_peerlist ot_peerlist; 118typedef struct ot_peerlist ot_peerlist;
@@ -122,26 +125,26 @@ typedef struct {
122#include "ot_vector.h" 125#include "ot_vector.h"
123 126
124struct ot_peerlist { 127struct ot_peerlist {
125 ot_time base; 128 ot_time base;
126 size_t seed_count; 129 size_t seed_count;
127 size_t peer_count; 130 size_t peer_count;
128 size_t down_count; 131 size_t down_count;
129/* normal peers vector or 132 /* normal peers vector or
130 pointer to ot_vector[32] buckets if data != NULL and space == 0 133 pointer to ot_vector[32] buckets if data != NULL and space == 0
131*/ 134 */
132 ot_vector peers; 135 ot_vector peers;
133}; 136};
134#define OT_PEERLIST_HASBUCKETS(peer_list) ((peer_list)->peers.size > (peer_list)->peers.space) 137#define OT_PEERLIST_HASBUCKETS(peer_list) ((peer_list)->peers.size > (peer_list)->peers.space)
135 138
136struct ot_workstruct { 139struct ot_workstruct {
137 /* Thread specific, static */ 140 /* Thread specific, static */
138 char *inbuf; 141 char *inbuf;
139#define G_INBUF_SIZE 8192 142#define G_INBUF_SIZE 8192
140 char *outbuf; 143 char *outbuf;
141#define G_OUTBUF_SIZE 8192 144#define G_OUTBUF_SIZE 8192
142#ifdef _DEBUG_HTTPERROR 145#ifdef _DEBUG_HTTPERROR
143 char *debugbuf; 146 char *debugbuf;
144#define G_DEBUGBUF_SIZE 8192 147#define G_DEBUGBUF_SIZE 8192
145#endif 148#endif
146 149
147 /* The peer currently in the working */ 150 /* The peer currently in the working */
@@ -174,34 +177,34 @@ struct ot_workstruct {
174#endif 177#endif
175 178
176#ifdef WANT_SYNC 179#ifdef WANT_SYNC
177#define WANT_SYNC_PARAM( param ) , param 180#define WANT_SYNC_PARAM(param) , param
178#else 181#else
179#define WANT_SYNC_PARAM( param ) 182#define WANT_SYNC_PARAM(param)
180#endif 183#endif
181 184
182#ifdef WANT_LOG_NETWORKS 185#ifdef WANT_LOG_NETWORKS
183#error Live logging networks disabled at the moment. 186#error Live logging networks disabled at the moment.
184#endif 187#endif
185 188
186void trackerlogic_init( void ); 189void trackerlogic_init(void);
187void trackerlogic_deinit( void ); 190void trackerlogic_deinit(void);
188void exerr( char * message ); 191void exerr(char *message);
189 192
190/* add_peer_to_torrent does only release the torrent bucket if from_sync is set, 193/* add_peer_to_torrent does only release the torrent bucket if from_sync is set,
191 otherwise it is released in return_peers_for_torrent */ 194 otherwise it is released in return_peers_for_torrent */
192size_t add_peer_to_torrent_and_return_peers( PROTO_FLAG proto, struct ot_workstruct *ws, size_t amount ); 195size_t add_peer_to_torrent_and_return_peers(PROTO_FLAG proto, struct ot_workstruct *ws, size_t amount);
193size_t remove_peer_from_torrent( PROTO_FLAG proto, struct ot_workstruct *ws ); 196size_t remove_peer_from_torrent(PROTO_FLAG proto, struct ot_workstruct *ws);
194size_t return_tcp_scrape_for_torrent( ot_hash const *hash_list, int amount, char *reply ); 197size_t return_tcp_scrape_for_torrent(ot_hash const *hash_list, int amount, char *reply);
195size_t return_udp_scrape_for_torrent( ot_hash const hash, char *reply ); 198size_t return_udp_scrape_for_torrent(ot_hash const hash, char *reply);
196void add_torrent_from_saved_state( ot_hash const hash, ot_time base, size_t down_count ); 199void add_torrent_from_saved_state(ot_hash const hash, ot_time base, size_t down_count);
197#ifdef _DEBUG_RANDOMTORRENTS 200#ifdef _DEBUG_RANDOMTORRENTS
198void trackerlogic_add_random_torrents(size_t amount); 201void trackerlogic_add_random_torrents(size_t amount);
199#endif 202#endif
200 203
201/* torrent iterator */ 204/* torrent iterator */
202void iterate_all_torrents( int (*for_each)( ot_torrent* torrent, uintptr_t data ), uintptr_t data ); 205void iterate_all_torrents(int (*for_each)(ot_torrent *torrent, uintptr_t data), uintptr_t data);
203 206
204/* Helper, before it moves to its own object */ 207/* Helper, before it moves to its own object */
205void free_peerlist( ot_peerlist *peer_list ); 208void free_peerlist(ot_peerlist *peer_list);
206 209
207#endif 210#endif