diff options
Diffstat (limited to 'src/trackerlogic.h')
| -rw-r--r-- | src/trackerlogic.h | 343 |
1 files changed, 343 insertions, 0 deletions
diff --git a/src/trackerlogic.h b/src/trackerlogic.h new file mode 100644 index 0000000..a05c790 --- /dev/null +++ b/src/trackerlogic.h | |||
| @@ -0,0 +1,343 @@ | |||
| 1 | /* This software was written by Dirk Engling <erdgeist@erdgeist.org> | ||
| 2 | It is considered beerware. Prost. Skol. Cheers or whatever. */ | ||
| 3 | |||
| 4 | #ifndef OT_TRACKERLOGIC_H__ | ||
| 5 | #define OT_TRACKERLOGIC_H__ | ||
| 6 | |||
| 7 | #include <arpa/inet.h> | ||
| 8 | #include <signal.h> | ||
| 9 | #include <stdatomic.h> | ||
| 10 | #include <stdint.h> | ||
| 11 | #include <stdlib.h> | ||
| 12 | #include <string.h> | ||
| 13 | #include <sys/time.h> | ||
| 14 | #include <sys/types.h> | ||
| 15 | #include <time.h> | ||
| 16 | |||
| 17 | typedef uint8_t ot_hash[20]; | ||
| 18 | typedef time_t ot_time; | ||
| 19 | typedef char ot_ip6[16]; | ||
| 20 | typedef struct { | ||
| 21 | ot_ip6 address; | ||
| 22 | int bits; | ||
| 23 | } ot_net; | ||
| 24 | /* List of peers should fit in a single UDP packet (around 1200 bytes) */ | ||
| 25 | #define OT_MAX_PEERS_UDP6 66 | ||
| 26 | #define OT_MAX_PEERS_UDP4 200 | ||
| 27 | |||
| 28 | #define OT_IP_SIZE6 16 | ||
| 29 | #define OT_IP_SIZE4 4 | ||
| 30 | #define OT_PORT_SIZE 2 | ||
| 31 | #define OT_FLAG_SIZE 1 | ||
| 32 | #define OT_TIME_SIZE 1 | ||
| 33 | |||
| 34 | /* Some tracker behaviour tunable */ | ||
| 35 | #ifndef OT_CLIENT_TIMEOUT | ||
| 36 | #define OT_CLIENT_TIMEOUT 30 | ||
| 37 | #endif | ||
| 38 | #ifndef OT_CLIENT_TIMEOUT_CHECKINTERVAL | ||
| 39 | #define OT_CLIENT_TIMEOUT_CHECKINTERVAL 10 | ||
| 40 | #endif | ||
| 41 | #define OT_CLIENT_TIMEOUT_SEND (60 * 15) | ||
| 42 | #define OT_CLIENT_REQUEST_INTERVAL (60 * 30) | ||
| 43 | #define OT_CLIENT_REQUEST_VARIATION (60 * 6) | ||
| 44 | #define OT_CLIENT_REJECT_INTERVAL (60 * 120) | ||
| 45 | |||
| 46 | #define OT_TORRENT_TIMEOUT_HOURS 24 | ||
| 47 | #define OT_TORRENT_TIMEOUT (60 * OT_TORRENT_TIMEOUT_HOURS) | ||
| 48 | |||
| 49 | /* How long a torrent kept only for its download count, with no peers in either | ||
| 50 | family, is held, in minutes. Defaults to OT_TORRENT_TIMEOUT and is lowered by | ||
| 51 | tracker.empty_torrent_timeout. */ | ||
| 52 | extern ot_time g_empty_torrent_timeout; | ||
| 53 | |||
| 54 | #define OT_CLIENT_REQUEST_INTERVAL_RANDOM \ | ||
| 55 | (OT_CLIENT_REQUEST_INTERVAL - OT_CLIENT_REQUEST_VARIATION / 2 + (int)(nrand48(ws->rand48_state) % OT_CLIENT_REQUEST_VARIATION)) | ||
| 56 | #define OT_CLIENT_REJECT_INTERVAL_RANDOM \ | ||
| 57 | (OT_CLIENT_REJECT_INTERVAL - OT_CLIENT_REQUEST_VARIATION / 2 + (int)(nrand48(ws->rand48_state) % OT_CLIENT_REQUEST_VARIATION)) | ||
| 58 | |||
| 59 | /* If WANT_MODEST_FULLSCRAPES is on, ip addresses may not | ||
| 60 | fullscrape more frequently than this amount in seconds */ | ||
| 61 | #define OT_MODEST_PEER_TIMEOUT (60 * 5) | ||
| 62 | |||
| 63 | /* If peers come back before 10 minutes, don't live sync them */ | ||
| 64 | #define OT_CLIENT_SYNC_RENEW_BOUNDARY 10 | ||
| 65 | |||
| 66 | /* Number of tracker admin ip addresses allowed */ | ||
| 67 | #define OT_ADMINIP_MAX 64 | ||
| 68 | #define OT_MAX_THREADS 64 | ||
| 69 | |||
| 70 | /* Number of minutes after announce before peer is removed */ | ||
| 71 | #define OT_PEER_TIMEOUT 45 | ||
| 72 | |||
| 73 | /* We maintain a list of 1024 pointers to sorted list of ot_torrent structs | ||
| 74 | Sort key is, of course, its hash. | ||
| 75 | Can be tweked from the Makefile, sensible values are between 8 in 16. More | ||
| 76 | buckets means shorter vectors and a cheaper insert, which only pays for a | ||
| 77 | tracker carrying millions of torrents. Consult the DESIGN document. */ | ||
| 78 | #ifndef OT_BUCKET_COUNT_BITS | ||
| 79 | #define OT_BUCKET_COUNT_BITS 10 | ||
| 80 | #endif | ||
| 81 | |||
| 82 | #define OT_BUCKET_COUNT (1 << OT_BUCKET_COUNT_BITS) | ||
| 83 | #define OT_BUCKET_COUNT_SHIFT (32 - OT_BUCKET_COUNT_BITS) | ||
| 84 | |||
| 85 | /* if _DEBUG_RANDOMTORRENTS is set, this is the amount of torrents to create | ||
| 86 | on startup */ | ||
| 87 | #define RANDOMTORRENTS (1024 * 1024 * 1) | ||
| 88 | |||
| 89 | #if defined(CLOCK_MONOTONIC_COARSE) | ||
| 90 | #define OT_CLOCK_CHEAP CLOCK_MONOTONIC_COARSE /* Linux and FreeBSD */ | ||
| 91 | #elif defined(CLOCK_MONOTONIC_FAST) | ||
| 92 | #define OT_CLOCK_CHEAP CLOCK_MONOTONIC_FAST /* the other BSDs */ | ||
| 93 | #else | ||
| 94 | #define OT_CLOCK_CHEAP CLOCK_MONOTONIC /* macOS and whatever else */ | ||
| 95 | #endif | ||
| 96 | |||
| 97 | static inline ot_time time_now_seconds(void) { | ||
| 98 | struct timespec now; | ||
| 99 | clock_gettime(OT_CLOCK_CHEAP, &now); | ||
| 100 | return (ot_time)now.tv_sec; | ||
| 101 | } | ||
| 102 | |||
| 103 | static inline ot_time time_now_minutes(void) { return time_now_seconds() / 60; } | ||
| 104 | |||
| 105 | /* For the deadlines the tracker sets on itself, which read better as the | ||
| 106 | span they allow than as an addition to the current second */ | ||
| 107 | static inline ot_time time_future_seconds(int span) { return time_now_seconds() + span; } | ||
| 108 | |||
| 109 | /* How long ago a stamp was taken. */ | ||
| 110 | _Static_assert((ot_time)-1 < 0, "ot_time has to be signed for time_since_* to clamp"); | ||
| 111 | |||
| 112 | static inline ot_time time_since_seconds(ot_time then) { | ||
| 113 | ot_time now = time_now_seconds(); | ||
| 114 | return now > then ? now - then : 0; | ||
| 115 | } | ||
| 116 | |||
| 117 | static inline ot_time time_since_minutes(ot_time then) { | ||
| 118 | ot_time now = time_now_minutes(); | ||
| 119 | return now > then ? now - then : 0; | ||
| 120 | } | ||
| 121 | |||
| 122 | /* Has a deadline the tracker set on itself gone by? */ | ||
| 123 | static inline int time_is_past(ot_time deadline) { return time_now_seconds() > deadline; } | ||
| 124 | |||
| 125 | /* The wall clock, which the state file uses and nothing else does: it has to | ||
| 126 | survive a restart, and the clock above counts from boot. */ | ||
| 127 | static inline ot_time time_wall_minutes(void) { return time(NULL) / 60; } | ||
| 128 | |||
| 129 | /* Appends a 32 bit value in network byte order to a binary reply and | ||
| 130 | advances the cursor. The caller has made sure of the room. */ | ||
| 131 | static inline void pack_uint32(char **cursor, uint32_t value) { | ||
| 132 | value = htonl(value); | ||
| 133 | memcpy(*cursor, &value, sizeof(value)); | ||
| 134 | *cursor += sizeof(value); | ||
| 135 | } | ||
| 136 | |||
| 137 | /* Cleared from the interrupt handler and read by every worker */ | ||
| 138 | extern _Atomic int g_opentracker_running; | ||
| 139 | |||
| 140 | /* Sent to a thread parked in a blocking read */ | ||
| 141 | #define OT_SIGNAL_WAKE SIGUSR1 | ||
| 142 | |||
| 143 | |||
| 144 | extern uint32_t g_tracker_id; | ||
| 145 | typedef enum { FLAG_TCP, FLAG_UDP, FLAG_MCA, FLAG_SELFPIPE } PROTO_FLAG; | ||
| 146 | |||
| 147 | #define OT_PEER_COMPARE_SIZE6 ((OT_IP_SIZE6) + (OT_PORT_SIZE)) | ||
| 148 | #define OT_PEER_COMPARE_SIZE4 ((OT_IP_SIZE4) + (OT_PORT_SIZE)) | ||
| 149 | #define OT_PEER_COMPARE_SIZE_FROM_PEER_SIZE(PEER_SIZE) ((PEER_SIZE) - (OT_TIME_SIZE) - (OT_FLAG_SIZE)) | ||
| 150 | |||
| 151 | #define OT_PEER_SIZE6 ((OT_TIME_SIZE) + (OT_FLAG_SIZE) + (OT_PEER_COMPARE_SIZE6)) | ||
| 152 | #define OT_PEER_SIZE4 ((OT_TIME_SIZE) + (OT_FLAG_SIZE) + (OT_PEER_COMPARE_SIZE4)) | ||
| 153 | |||
| 154 | typedef uint8_t ot_peer; /* Generic pointer to a v6 or v4 peer */ | ||
| 155 | typedef uint8_t ot_peer6[OT_PEER_SIZE6]; | ||
| 156 | typedef uint8_t ot_peer4[OT_PEER_SIZE4]; | ||
| 157 | static const uint8_t PEER_FLAG_SEEDING = 0x80; | ||
| 158 | static const uint8_t PEER_FLAG_COMPLETED = 0x40; | ||
| 159 | static const uint8_t PEER_FLAG_STOPPED = 0x20; | ||
| 160 | static const uint8_t PEER_FLAG_FROM_SYNC = 0x10; | ||
| 161 | static const uint8_t PEER_FLAG_LEECHING = 0x00; | ||
| 162 | |||
| 163 | /* Takes an ot_peer6 and returns the proper pointer to the peer and sets peer_size */ | ||
| 164 | ot_peer *peer_from_peer6(ot_peer6 *peer, size_t *peer_size); | ||
| 165 | size_t peer_size_from_peer6(ot_peer6 *peer); | ||
| 166 | |||
| 167 | /* New style */ | ||
| 168 | #define OT_SETIP(peer, ip) memcpy((uint8_t *)(peer), (ip), OT_IP_SIZE6) | ||
| 169 | #define OT_SETPORT(peer, port) memcpy(((uint8_t *)(peer)) + (OT_IP_SIZE6), (port), 2) | ||
| 170 | #define OT_PEERFLAG(peer) (((uint8_t *)(peer))[(OT_IP_SIZE6) + 2]) | ||
| 171 | #define OT_PEERFLAG_D(peer, peersize) (((uint8_t *)(peer))[(peersize) - 2]) | ||
| 172 | #define OT_PEERTIME(peer, peersize) (((uint8_t *)(peer))[(peersize) - 1]) | ||
| 173 | |||
| 174 | #define PEERS_BENCODED6 "6:peers6" | ||
| 175 | #define PEERS_BENCODED4 "5:peers" | ||
| 176 | |||
| 177 | #define OT_HASH_COMPARE_SIZE (sizeof(ot_hash)) | ||
| 178 | |||
| 179 | struct ot_peerlist; | ||
| 180 | typedef struct ot_peerlist ot_peerlist; | ||
| 181 | typedef struct { | ||
| 182 | ot_hash hash; | ||
| 183 | ot_peerlist *peer_list; /* NULL until the torrent has a peer in either family */ | ||
| 184 | uint32_t base; /* epoch minutes, when the torrent was last cleaned */ | ||
| 185 | uint32_t down_count; /* completed events */ | ||
| 186 | } ot_torrent; | ||
| 187 | |||
| 188 | #include "ot_vector.h" | ||
| 189 | |||
| 190 | struct ot_peerlist { | ||
| 191 | uint32_t seed_count; | ||
| 192 | /* each family's peers: a normal vector, or a pointer to ot_vector[] buckets | ||
| 193 | when data != NULL and space == 0 (size then holds the bucket count) */ | ||
| 194 | ot_vector peers6; | ||
| 195 | ot_vector peers4; | ||
| 196 | }; | ||
| 197 | |||
| 198 | /* A peer vector becomes a bucket list of peer vectors, marker is size > space (usually 0) */ | ||
| 199 | #define OT_VECTOR_HASBUCKETS(v) ((v)->size > (v)->space) | ||
| 200 | |||
| 201 | /* Peers in one family: a plain vector's size is the count, a bucket list's is | ||
| 202 | the sum of its buckets. O(1) for the small torrents that dominate a table. */ | ||
| 203 | static inline size_t vector_peer_count(const ot_vector *v) { | ||
| 204 | const ot_vector *b; | ||
| 205 | size_t n, total = 0; | ||
| 206 | if (!OT_VECTOR_HASBUCKETS(v)) | ||
| 207 | return v->size; | ||
| 208 | b = (const ot_vector *)v->data; | ||
| 209 | for (n = 0; n < v->size; ++n) | ||
| 210 | total += b[n].size; | ||
| 211 | return total; | ||
| 212 | } | ||
| 213 | static inline size_t torrent_peer_count(const ot_torrent *t) { | ||
| 214 | return t->peer_list ? vector_peer_count(&t->peer_list->peers6) + vector_peer_count(&t->peer_list->peers4) : 0; | ||
| 215 | } | ||
| 216 | static inline uint32_t torrent_seed_count(const ot_torrent *t) { return t->peer_list ? t->peer_list->seed_count : 0; } | ||
| 217 | |||
| 218 | /* The http replies the tracker counts when it has to refuse a request. */ | ||
| 219 | enum { | ||
| 220 | CODE_HTTPERROR_302, | ||
| 221 | CODE_HTTPERROR_400, | ||
| 222 | CODE_HTTPERROR_400_PARAM, | ||
| 223 | CODE_HTTPERROR_400_COMPACT, | ||
| 224 | CODE_HTTPERROR_402_NOTMODEST, | ||
| 225 | CODE_HTTPERROR_402_PAYMENT_REQUIRED, | ||
| 226 | CODE_HTTPERROR_403_IP, | ||
| 227 | CODE_HTTPERROR_404, | ||
| 228 | CODE_HTTPERROR_500, | ||
| 229 | |||
| 230 | CODE_HTTPERROR_COUNT | ||
| 231 | }; | ||
| 232 | |||
| 233 | /* One statistic. Used when workers report stats to their workstruct */ | ||
| 234 | typedef _Atomic unsigned long ot_counter; | ||
| 235 | |||
| 236 | /* What each thread counts for itself. The two runs at the end are indexed by | ||
| 237 | the http error code and by the age in minutes of a renewed peer */ | ||
| 238 | typedef enum { | ||
| 239 | OT_COUNTER_TCP_CONNECTIONS, | ||
| 240 | OT_COUNTER_UDP_CONNECTIONS, | ||
| 241 | OT_COUNTER_TCP_ANNOUNCES, | ||
| 242 | OT_COUNTER_UDP_ANNOUNCES, | ||
| 243 | OT_COUNTER_TCP_SCRAPES, | ||
| 244 | OT_COUNTER_UDP_SCRAPES, | ||
| 245 | OT_COUNTER_TCP_CONNECTS, | ||
| 246 | OT_COUNTER_UDP_CONNECTS, | ||
| 247 | OT_COUNTER_UDP_CONNID_MISSMATCH, | ||
| 248 | OT_COUNTER_COMPLETED, | ||
| 249 | OT_COUNTER_FULLSCRAPES, | ||
| 250 | OT_COUNTER_FULLSCRAPE_REQUESTS, | ||
| 251 | OT_COUNTER_FULLSCRAPE_BYTES, | ||
| 252 | OT_COUNTER_SYNCS, | ||
| 253 | OT_COUNTER_STALLS, | ||
| 254 | |||
| 255 | OT_COUNTER_FAILED, /* CODE_HTTPERROR_COUNT of them */ | ||
| 256 | OT_COUNTER_RENEWED = OT_COUNTER_FAILED + CODE_HTTPERROR_COUNT, /* OT_PEER_TIMEOUT of them */ | ||
| 257 | |||
| 258 | OT_COUNTER_COUNT = OT_COUNTER_RENEWED + OT_PEER_TIMEOUT | ||
| 259 | } ot_counter_index; | ||
| 260 | |||
| 261 | struct ot_workstruct { | ||
| 262 | /* Thread specific, static */ | ||
| 263 | |||
| 264 | /* This thread's own statistics, and the link ot_stats keeps them on */ | ||
| 265 | ot_counter counters[OT_COUNTER_COUNT]; | ||
| 266 | struct ot_workstruct *counters_next; | ||
| 267 | |||
| 268 | char *inbuf; | ||
| 269 | #define G_INBUF_SIZE 8192 | ||
| 270 | char *outbuf; | ||
| 271 | #define G_OUTBUF_SIZE 8192 | ||
| 272 | #ifdef _DEBUG_HTTPERROR | ||
| 273 | char *debugbuf; | ||
| 274 | #define G_DEBUGBUF_SIZE 8192 | ||
| 275 | #endif | ||
| 276 | |||
| 277 | /* The peer currently in the working */ | ||
| 278 | ot_peer6 peer; /* Can fit v6 and v4 peers */ | ||
| 279 | |||
| 280 | /* Pointers into the request buffer */ | ||
| 281 | ot_hash *hash; | ||
| 282 | char *peer_id; | ||
| 283 | |||
| 284 | /* HTTP specific, non static */ | ||
| 285 | char *request; | ||
| 286 | ssize_t request_size; | ||
| 287 | ssize_t header_size; | ||
| 288 | char *reply; | ||
| 289 | ssize_t reply_size; | ||
| 290 | |||
| 291 | /* Entropy state for rand48 function so that threads don't need to acquire mutexes for | ||
| 292 | global random() or arc4random() state, which causes heavy load on linuxes */ | ||
| 293 | uint16_t rand48_state[3]; | ||
| 294 | |||
| 295 | int keep_alive; | ||
| 296 | |||
| 297 | /* Set when a request path gave the connection to a worker thread. */ | ||
| 298 | int handed_off; | ||
| 299 | }; | ||
| 300 | |||
| 301 | /* Everything a thread that serves requests needs for itself. Set up and torn | ||
| 302 | down in one place so that a new kind of worker cannot miss a piece. | ||
| 303 | Returns -1 if the buffers could not be had. */ | ||
| 304 | int workstruct_init(struct ot_workstruct *ws, size_t inbuf_size); | ||
| 305 | void workstruct_deinit(struct ot_workstruct *ws); | ||
| 306 | |||
| 307 | /* | ||
| 308 | Exported functions | ||
| 309 | */ | ||
| 310 | |||
| 311 | #ifdef WANT_SYNC_LIVE | ||
| 312 | #define WANT_SYNC | ||
| 313 | #endif | ||
| 314 | |||
| 315 | #ifdef WANT_SYNC | ||
| 316 | #define WANT_SYNC_PARAM(param) , param | ||
| 317 | #else | ||
| 318 | #define WANT_SYNC_PARAM(param) | ||
| 319 | #endif | ||
| 320 | |||
| 321 | void trackerlogic_init(void); | ||
| 322 | void trackerlogic_deinit(void); | ||
| 323 | void exerr(const char *message); | ||
| 324 | |||
| 325 | /* add_peer_to_torrent does only release the torrent bucket if from_sync is set, | ||
| 326 | otherwise it is released in return_peers_for_torrent */ | ||
| 327 | size_t add_peer_to_torrent_and_return_peers(PROTO_FLAG proto, struct ot_workstruct *ws, size_t amount); | ||
| 328 | size_t remove_peer_from_torrent(PROTO_FLAG proto, struct ot_workstruct *ws); | ||
| 329 | void return_tcp_scrape_for_torrent(ot_hash const *hash_list, size_t amount, char **reply, const char *reply_end); | ||
| 330 | size_t return_udp_scrape_for_torrent(ot_hash const hash, char *reply); | ||
| 331 | void add_torrent_from_saved_state(ot_hash const hash, ot_time age, size_t down_count); | ||
| 332 | #ifdef _DEBUG_RANDOMTORRENTS | ||
| 333 | void trackerlogic_add_random_torrents(size_t amount); | ||
| 334 | #endif | ||
| 335 | |||
| 336 | /* torrent iterator */ | ||
| 337 | void iterate_all_torrents(int (*for_each)(ot_torrent *torrent, uintptr_t data), uintptr_t data); | ||
| 338 | |||
| 339 | /* Helper, before it moves to its own object */ | ||
| 340 | void free_peerlist(ot_peerlist *peer_list); | ||
| 341 | void free_peer_vector(ot_vector *peer_vector); | ||
| 342 | |||
| 343 | #endif | ||
