summaryrefslogtreecommitdiff
path: root/trackerlogic.h
diff options
context:
space:
mode:
Diffstat (limited to 'trackerlogic.h')
-rw-r--r--trackerlogic.h168
1 files changed, 97 insertions, 71 deletions
diff --git a/trackerlogic.h b/trackerlogic.h
index 87b9138..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,118 +22,139 @@
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#ifdef WANT_V6 27 int bits;
28#define OT_IP_SIZE 16 28} ot_net;
29#define PEERS_BENCODED "6:peers6" 29/* List of peers should fit in a single UDP packet (around 1200 bytes) */
30#else 30#define OT_MAX_PEERS_UDP6 66
31#define OT_IP_SIZE 4 31#define OT_MAX_PEERS_UDP4 200
32#define PEERS_BENCODED "5:peers" 32
33#endif 33#define OT_IP_SIZE6 16
34#define OT_IP_SIZE4 4
35#define OT_PORT_SIZE 2
36#define OT_FLAG_SIZE 1
37#define OT_TIME_SIZE 1
34 38
35/* Some tracker behaviour tunable */ 39/* Some tracker behaviour tunable */
36#define OT_CLIENT_TIMEOUT 30 40#define OT_CLIENT_TIMEOUT 30
37#define OT_CLIENT_TIMEOUT_CHECKINTERVAL 10 41#define OT_CLIENT_TIMEOUT_CHECKINTERVAL 10
38#define OT_CLIENT_TIMEOUT_SEND (60*15) 42#define OT_CLIENT_TIMEOUT_SEND (60 * 15)
39#define OT_CLIENT_REQUEST_INTERVAL (60*30) 43#define OT_CLIENT_REQUEST_INTERVAL (60 * 30)
40#define OT_CLIENT_REQUEST_VARIATION (60*6) 44#define OT_CLIENT_REQUEST_VARIATION (60 * 6)
41 45
42#define OT_TORRENT_TIMEOUT_HOURS 24 46#define OT_TORRENT_TIMEOUT_HOURS 24
43#define OT_TORRENT_TIMEOUT (60*OT_TORRENT_TIMEOUT_HOURS) 47#define OT_TORRENT_TIMEOUT (60 * OT_TORRENT_TIMEOUT_HOURS)
44 48
45#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))
46 51
47/* If WANT_MODEST_FULLSCRAPES is on, ip addresses may not 52/* If WANT_MODEST_FULLSCRAPES is on, ip addresses may not
48 fullscrape more frequently than this amount in seconds */ 53 fullscrape more frequently than this amount in seconds */
49#define OT_MODEST_PEER_TIMEOUT (60*5) 54#define OT_MODEST_PEER_TIMEOUT (60 * 5)
50 55
51/* 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 */
52#define OT_CLIENT_SYNC_RENEW_BOUNDARY 10 57#define OT_CLIENT_SYNC_RENEW_BOUNDARY 10
53 58
54/* Number of tracker admin ip addresses allowed */ 59/* Number of tracker admin ip addresses allowed */
55#define OT_ADMINIP_MAX 64 60#define OT_ADMINIP_MAX 64
56#define OT_MAX_THREADS 64 61#define OT_MAX_THREADS 64
57 62
58#define OT_PEER_TIMEOUT 45 63/* Number of minutes after announce before peer is removed */
64#define OT_PEER_TIMEOUT 45
59 65
60/* 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
61 Sort key is, of course, its hash */ 67 Sort key is, of course, its hash */
62#define OT_BUCKET_COUNT_BITS 10 68#define OT_BUCKET_COUNT_BITS 10
63 69
64#define OT_BUCKET_COUNT (1<<OT_BUCKET_COUNT_BITS) 70#define OT_BUCKET_COUNT (1 << OT_BUCKET_COUNT_BITS)
65#define OT_BUCKET_COUNT_SHIFT (32-OT_BUCKET_COUNT_BITS) 71#define OT_BUCKET_COUNT_SHIFT (32 - OT_BUCKET_COUNT_BITS)
72
73/* if _DEBUG_RANDOMTORRENTS is set, this is the amount of torrents to create
74 on startup */
75#define RANDOMTORRENTS (1024 * 1024 * 1)
66 76
67/* From opentracker.c */ 77/* From opentracker.c */
68extern time_t g_now_seconds; 78extern time_t g_now_seconds;
69extern volatile int g_opentracker_running; 79extern volatile int g_opentracker_running;
70#define g_now_minutes (g_now_seconds/60) 80#define g_now_minutes (g_now_seconds / 60)
71 81
72extern uint32_t g_tracker_id; 82extern uint32_t g_tracker_id;
73typedef enum { FLAG_TCP, FLAG_UDP, FLAG_MCA, FLAG_SELFPIPE } PROTO_FLAG; 83typedef enum { FLAG_TCP, FLAG_UDP, FLAG_MCA, FLAG_SELFPIPE } PROTO_FLAG;
74 84
75typedef struct { 85#define OT_PEER_COMPARE_SIZE6 ((OT_IP_SIZE6) + (OT_PORT_SIZE))
76 uint8_t data[OT_IP_SIZE+2+2]; 86#define OT_PEER_COMPARE_SIZE4 ((OT_IP_SIZE4) + (OT_PORT_SIZE))
77} ot_peer; 87#define OT_PEER_COMPARE_SIZE_FROM_PEER_SIZE(PEER_SIZE) ((PEER_SIZE) - (OT_TIME_SIZE) - (OT_FLAG_SIZE))
88
89#define OT_PEER_SIZE6 ((OT_TIME_SIZE) + (OT_FLAG_SIZE) + (OT_PEER_COMPARE_SIZE6))
90#define OT_PEER_SIZE4 ((OT_TIME_SIZE) + (OT_FLAG_SIZE) + (OT_PEER_COMPARE_SIZE4))
91
92typedef uint8_t ot_peer; /* Generic pointer to a v6 or v4 peer */
93typedef uint8_t ot_peer6[OT_PEER_SIZE6];
94typedef uint8_t ot_peer4[OT_PEER_SIZE4];
78static const uint8_t PEER_FLAG_SEEDING = 0x80; 95static const uint8_t PEER_FLAG_SEEDING = 0x80;
79static const uint8_t PEER_FLAG_COMPLETED = 0x40; 96static const uint8_t PEER_FLAG_COMPLETED = 0x40;
80static const uint8_t PEER_FLAG_STOPPED = 0x20; 97static const uint8_t PEER_FLAG_STOPPED = 0x20;
81static const uint8_t PEER_FLAG_FROM_SYNC = 0x10; 98static const uint8_t PEER_FLAG_FROM_SYNC = 0x10;
82static const uint8_t PEER_FLAG_LEECHING = 0x00; 99static const uint8_t PEER_FLAG_LEECHING = 0x00;
83 100
84#ifdef WANT_V6 101/* Takes an ot_peer6 and returns the proper pointer to the peer and sets peer_size */
85#define OT_SETIP(peer,ip) memcpy((peer),(ip),(OT_IP_SIZE)) 102ot_peer *peer_from_peer6(ot_peer6 *peer, size_t *peer_size);
86#else 103size_t peer_size_from_peer6(ot_peer6 *peer);
87#define OT_SETIP(peer,ip) memcpy((peer),(((uint8_t*)ip)+12),(OT_IP_SIZE)) 104
88#endif 105/* New style */
89#define OT_SETPORT(peer,port) memcpy(((uint8_t*)(peer))+(OT_IP_SIZE),(port),2) 106#define OT_SETIP(peer, ip) memcpy((uint8_t *)(peer), (ip), OT_IP_SIZE6)
90#define OT_PEERFLAG(peer) (((uint8_t*)(peer))[(OT_IP_SIZE)+2]) 107#define OT_SETPORT(peer, port) memcpy(((uint8_t *)(peer)) + (OT_IP_SIZE6), (port), 2)
91#define OT_PEERTIME(peer) (((uint8_t*)(peer))[(OT_IP_SIZE)+3]) 108#define OT_PEERFLAG(peer) (((uint8_t *)(peer))[(OT_IP_SIZE6) + 2])
109#define OT_PEERFLAG_D(peer, peersize) (((uint8_t *)(peer))[(peersize) - 2])
110#define OT_PEERTIME(peer, peersize) (((uint8_t *)(peer))[(peersize) - 1])
111
112#define PEERS_BENCODED6 "6:peers6"
113#define PEERS_BENCODED4 "5:peers"
92 114
93#define OT_HASH_COMPARE_SIZE (sizeof(ot_hash)) 115#define OT_HASH_COMPARE_SIZE (sizeof(ot_hash))
94#define OT_PEER_COMPARE_SIZE ((OT_IP_SIZE)+2)
95 116
96struct ot_peerlist; 117struct ot_peerlist;
97typedef struct ot_peerlist ot_peerlist; 118typedef struct ot_peerlist ot_peerlist;
98typedef struct { 119typedef struct {
99 ot_hash hash; 120 ot_hash hash;
100 ot_peerlist *peer_list; 121 ot_peerlist *peer_list6;
122 ot_peerlist *peer_list4;
101} ot_torrent; 123} ot_torrent;
102 124
103#include "ot_vector.h" 125#include "ot_vector.h"
104 126
105struct ot_peerlist { 127struct ot_peerlist {
106 ot_time base; 128 ot_time base;
107 size_t seed_count; 129 size_t seed_count;
108 size_t peer_count; 130 size_t peer_count;
109 size_t down_count; 131 size_t down_count;
110/* normal peers vector or 132 /* normal peers vector or
111 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
112*/ 134 */
113 ot_vector peers; 135 ot_vector peers;
114}; 136};
115#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)
116 138
117struct ot_workstruct { 139struct ot_workstruct {
118 /* Thread specific, static */ 140 /* Thread specific, static */
119 char *inbuf; 141 char *inbuf;
120#define G_INBUF_SIZE 8192 142#define G_INBUF_SIZE 8192
121 char *outbuf; 143 char *outbuf;
122#define G_OUTBUF_SIZE 8192 144#define G_OUTBUF_SIZE 8192
123#ifdef _DEBUG_HTTPERROR 145#ifdef _DEBUG_HTTPERROR
124 char *debugbuf; 146 char *debugbuf;
125#define G_DEBUGBUF_SIZE 8192 147#define G_DEBUGBUF_SIZE 8192
126#endif 148#endif
127 149
128 /* The peer currently in the working */ 150 /* The peer currently in the working */
129 ot_peer peer; 151 ot_peer6 peer; /* Can fit v6 and v4 peers */
130 152
131 /* Pointers into the request buffer */ 153 /* Pointers into the request buffer */
132 ot_hash *hash; 154 ot_hash *hash;
133 char *peer_id; 155 char *peer_id;
134 156
135 /* HTTP specific, non static */ 157 /* HTTP specific, non static */
136 int keep_alive;
137 char *request; 158 char *request;
138 ssize_t request_size; 159 ssize_t request_size;
139 ssize_t header_size; 160 ssize_t header_size;
@@ -143,6 +164,8 @@ struct ot_workstruct {
143 /* Entropy state for rand48 function so that threads don't need to acquire mutexes for 164 /* Entropy state for rand48 function so that threads don't need to acquire mutexes for
144 global random() or arc4random() state, which causes heavy load on linuxes */ 165 global random() or arc4random() state, which causes heavy load on linuxes */
145 uint16_t rand48_state[3]; 166 uint16_t rand48_state[3];
167
168 int keep_alive;
146}; 169};
147 170
148/* 171/*
@@ -154,31 +177,34 @@ struct ot_workstruct {
154#endif 177#endif
155 178
156#ifdef WANT_SYNC 179#ifdef WANT_SYNC
157#define WANT_SYNC_PARAM( param ) , param 180#define WANT_SYNC_PARAM(param) , param
158#else 181#else
159#define WANT_SYNC_PARAM( param ) 182#define WANT_SYNC_PARAM(param)
160#endif 183#endif
161 184
162#ifdef WANT_LOG_NETWORKS 185#ifdef WANT_LOG_NETWORKS
163#error Live logging networks disabled at the moment. 186#error Live logging networks disabled at the moment.
164#endif 187#endif
165 188
166void trackerlogic_init( ); 189void trackerlogic_init(void);
167void trackerlogic_deinit( void ); 190void trackerlogic_deinit(void);
168void exerr( char * message ); 191void exerr(char *message);
169 192
170/* 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,
171 otherwise it is released in return_peers_for_torrent */ 194 otherwise it is released in return_peers_for_torrent */
172size_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);
173size_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);
174size_t return_tcp_scrape_for_torrent( ot_hash *hash, int amount, char *reply ); 197size_t return_tcp_scrape_for_torrent(ot_hash const *hash_list, int amount, char *reply);
175size_t return_udp_scrape_for_torrent( ot_hash hash, char *reply ); 198size_t return_udp_scrape_for_torrent(ot_hash const hash, char *reply);
176void add_torrent_from_saved_state( ot_hash 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);
200#ifdef _DEBUG_RANDOMTORRENTS
201void trackerlogic_add_random_torrents(size_t amount);
202#endif
177 203
178/* torrent iterator */ 204/* torrent iterator */
179void 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);
180 206
181/* Helper, before it moves to its own object */ 207/* Helper, before it moves to its own object */
182void free_peerlist( ot_peerlist *peer_list ); 208void free_peerlist(ot_peerlist *peer_list);
183 209
184#endif 210#endif