diff options
Diffstat (limited to 'src/trackerlogic.h')
| -rw-r--r-- | src/trackerlogic.h | 104 |
1 files changed, 82 insertions, 22 deletions
diff --git a/src/trackerlogic.h b/src/trackerlogic.h index 692b6af..a05c790 100644 --- a/src/trackerlogic.h +++ b/src/trackerlogic.h | |||
| @@ -4,10 +4,12 @@ | |||
| 4 | #ifndef OT_TRACKERLOGIC_H__ | 4 | #ifndef OT_TRACKERLOGIC_H__ |
| 5 | #define OT_TRACKERLOGIC_H__ | 5 | #define OT_TRACKERLOGIC_H__ |
| 6 | 6 | ||
| 7 | #include <arpa/inet.h> | ||
| 7 | #include <signal.h> | 8 | #include <signal.h> |
| 8 | #include <stdatomic.h> | 9 | #include <stdatomic.h> |
| 9 | #include <stdint.h> | 10 | #include <stdint.h> |
| 10 | #include <stdlib.h> | 11 | #include <stdlib.h> |
| 12 | #include <string.h> | ||
| 11 | #include <sys/time.h> | 13 | #include <sys/time.h> |
| 12 | #include <sys/types.h> | 14 | #include <sys/types.h> |
| 13 | #include <time.h> | 15 | #include <time.h> |
| @@ -39,12 +41,20 @@ typedef struct { | |||
| 39 | #define OT_CLIENT_TIMEOUT_SEND (60 * 15) | 41 | #define OT_CLIENT_TIMEOUT_SEND (60 * 15) |
| 40 | #define OT_CLIENT_REQUEST_INTERVAL (60 * 30) | 42 | #define OT_CLIENT_REQUEST_INTERVAL (60 * 30) |
| 41 | #define OT_CLIENT_REQUEST_VARIATION (60 * 6) | 43 | #define OT_CLIENT_REQUEST_VARIATION (60 * 6) |
| 44 | #define OT_CLIENT_REJECT_INTERVAL (60 * 120) | ||
| 42 | 45 | ||
| 43 | #define OT_TORRENT_TIMEOUT_HOURS 24 | 46 | #define OT_TORRENT_TIMEOUT_HOURS 24 |
| 44 | #define OT_TORRENT_TIMEOUT (60 * OT_TORRENT_TIMEOUT_HOURS) | 47 | #define OT_TORRENT_TIMEOUT (60 * OT_TORRENT_TIMEOUT_HOURS) |
| 45 | 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 | |||
| 46 | #define OT_CLIENT_REQUEST_INTERVAL_RANDOM \ | 54 | #define OT_CLIENT_REQUEST_INTERVAL_RANDOM \ |
| 47 | (OT_CLIENT_REQUEST_INTERVAL - OT_CLIENT_REQUEST_VARIATION / 2 + (int)(nrand48(ws->rand48_state) % OT_CLIENT_REQUEST_VARIATION)) | 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)) | ||
| 48 | 58 | ||
| 49 | /* If WANT_MODEST_FULLSCRAPES is on, ip addresses may not | 59 | /* If WANT_MODEST_FULLSCRAPES is on, ip addresses may not |
| 50 | fullscrape more frequently than this amount in seconds */ | 60 | fullscrape more frequently than this amount in seconds */ |
| @@ -61,10 +71,10 @@ typedef struct { | |||
| 61 | #define OT_PEER_TIMEOUT 45 | 71 | #define OT_PEER_TIMEOUT 45 |
| 62 | 72 | ||
| 63 | /* We maintain a list of 1024 pointers to sorted list of ot_torrent structs | 73 | /* We maintain a list of 1024 pointers to sorted list of ot_torrent structs |
| 64 | Sort key is, of course, its hash */ | 74 | Sort key is, of course, its hash. |
| 65 | /* Set from the Makefile, 8 to 16. More buckets means shorter vectors and a | 75 | Can be tweked from the Makefile, sensible values are between 8 in 16. More |
| 66 | cheaper insert, which only pays for a tracker carrying millions of | 76 | buckets means shorter vectors and a cheaper insert, which only pays for a |
| 67 | torrents; see DESIGN. */ | 77 | tracker carrying millions of torrents. Consult the DESIGN document. */ |
| 68 | #ifndef OT_BUCKET_COUNT_BITS | 78 | #ifndef OT_BUCKET_COUNT_BITS |
| 69 | #define OT_BUCKET_COUNT_BITS 10 | 79 | #define OT_BUCKET_COUNT_BITS 10 |
| 70 | #endif | 80 | #endif |
| @@ -76,12 +86,12 @@ typedef struct { | |||
| 76 | on startup */ | 86 | on startup */ |
| 77 | #define RANDOMTORRENTS (1024 * 1024 * 1) | 87 | #define RANDOMTORRENTS (1024 * 1024 * 1) |
| 78 | 88 | ||
| 79 | #if defined(CLOCK_REALTIME_COARSE) | 89 | #if defined(CLOCK_MONOTONIC_COARSE) |
| 80 | #define OT_CLOCK_CHEAP CLOCK_REALTIME_COARSE /* Linux */ | 90 | #define OT_CLOCK_CHEAP CLOCK_MONOTONIC_COARSE /* Linux and FreeBSD */ |
| 81 | #elif defined(CLOCK_REALTIME_FAST) | 91 | #elif defined(CLOCK_MONOTONIC_FAST) |
| 82 | #define OT_CLOCK_CHEAP CLOCK_REALTIME_FAST /* the BSDs */ | 92 | #define OT_CLOCK_CHEAP CLOCK_MONOTONIC_FAST /* the other BSDs */ |
| 83 | #else | 93 | #else |
| 84 | #define OT_CLOCK_CHEAP CLOCK_REALTIME /* macOS and whatever else */ | 94 | #define OT_CLOCK_CHEAP CLOCK_MONOTONIC /* macOS and whatever else */ |
| 85 | #endif | 95 | #endif |
| 86 | 96 | ||
| 87 | static inline ot_time time_now_seconds(void) { | 97 | static inline ot_time time_now_seconds(void) { |
| @@ -95,6 +105,35 @@ static inline ot_time time_now_minutes(void) { return time_now_seconds() / 60; } | |||
| 95 | /* For the deadlines the tracker sets on itself, which read better as the | 105 | /* For the deadlines the tracker sets on itself, which read better as the |
| 96 | span they allow than as an addition to the current second */ | 106 | span they allow than as an addition to the current second */ |
| 97 | static inline ot_time time_future_seconds(int span) { return time_now_seconds() + span; } | 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 | |||
| 98 | /* Cleared from the interrupt handler and read by every worker */ | 137 | /* Cleared from the interrupt handler and read by every worker */ |
| 99 | extern _Atomic int g_opentracker_running; | 138 | extern _Atomic int g_opentracker_running; |
| 100 | 139 | ||
| @@ -141,23 +180,40 @@ struct ot_peerlist; | |||
| 141 | typedef struct ot_peerlist ot_peerlist; | 180 | typedef struct ot_peerlist ot_peerlist; |
| 142 | typedef struct { | 181 | typedef struct { |
| 143 | ot_hash hash; | 182 | ot_hash hash; |
| 144 | ot_peerlist *peer_list6; | 183 | ot_peerlist *peer_list; /* NULL until the torrent has a peer in either family */ |
| 145 | ot_peerlist *peer_list4; | 184 | uint32_t base; /* epoch minutes, when the torrent was last cleaned */ |
| 185 | uint32_t down_count; /* completed events */ | ||
| 146 | } ot_torrent; | 186 | } ot_torrent; |
| 147 | 187 | ||
| 148 | #include "ot_vector.h" | 188 | #include "ot_vector.h" |
| 149 | 189 | ||
| 150 | struct ot_peerlist { | 190 | struct ot_peerlist { |
| 151 | ot_time base; | 191 | uint32_t seed_count; |
| 152 | size_t seed_count; | 192 | /* each family's peers: a normal vector, or a pointer to ot_vector[] buckets |
| 153 | size_t peer_count; | 193 | when data != NULL and space == 0 (size then holds the bucket count) */ |
| 154 | size_t down_count; | 194 | ot_vector peers6; |
| 155 | /* normal peers vector or | 195 | ot_vector peers4; |
| 156 | pointer to ot_vector[32] buckets if data != NULL and space == 0 | ||
| 157 | */ | ||
| 158 | ot_vector peers; | ||
| 159 | }; | 196 | }; |
| 160 | #define OT_PEERLIST_HASBUCKETS(peer_list) ((peer_list)->peers.size > (peer_list)->peers.space) | 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; } | ||
| 161 | 217 | ||
| 162 | /* The http replies the tracker counts when it has to refuse a request. */ | 218 | /* The http replies the tracker counts when it has to refuse a request. */ |
| 163 | enum { | 219 | enum { |
| @@ -175,7 +231,7 @@ enum { | |||
| 175 | }; | 231 | }; |
| 176 | 232 | ||
| 177 | /* One statistic. Used when workers report stats to their workstruct */ | 233 | /* One statistic. Used when workers report stats to their workstruct */ |
| 178 | typedef _Atomic unsigned long long ot_counter; | 234 | typedef _Atomic unsigned long ot_counter; |
| 179 | 235 | ||
| 180 | /* What each thread counts for itself. The two runs at the end are indexed by | 236 | /* What each thread counts for itself. The two runs at the end are indexed by |
| 181 | the http error code and by the age in minutes of a renewed peer */ | 237 | the http error code and by the age in minutes of a renewed peer */ |
| @@ -237,6 +293,9 @@ struct ot_workstruct { | |||
| 237 | uint16_t rand48_state[3]; | 293 | uint16_t rand48_state[3]; |
| 238 | 294 | ||
| 239 | int keep_alive; | 295 | int keep_alive; |
| 296 | |||
| 297 | /* Set when a request path gave the connection to a worker thread. */ | ||
| 298 | int handed_off; | ||
| 240 | }; | 299 | }; |
| 241 | 300 | ||
| 242 | /* Everything a thread that serves requests needs for itself. Set up and torn | 301 | /* Everything a thread that serves requests needs for itself. Set up and torn |
| @@ -269,7 +328,7 @@ size_t add_peer_to_torrent_and_return_peers(PROTO_FLAG proto, struct ot_workstru | |||
| 269 | size_t remove_peer_from_torrent(PROTO_FLAG proto, struct ot_workstruct *ws); | 328 | size_t remove_peer_from_torrent(PROTO_FLAG proto, struct ot_workstruct *ws); |
| 270 | void return_tcp_scrape_for_torrent(ot_hash const *hash_list, size_t amount, char **reply, const char *reply_end); | 329 | void return_tcp_scrape_for_torrent(ot_hash const *hash_list, size_t amount, char **reply, const char *reply_end); |
| 271 | size_t return_udp_scrape_for_torrent(ot_hash const hash, char *reply); | 330 | size_t return_udp_scrape_for_torrent(ot_hash const hash, char *reply); |
| 272 | void add_torrent_from_saved_state(ot_hash const hash, ot_time base, size_t down_count); | 331 | void add_torrent_from_saved_state(ot_hash const hash, ot_time age, size_t down_count); |
| 273 | #ifdef _DEBUG_RANDOMTORRENTS | 332 | #ifdef _DEBUG_RANDOMTORRENTS |
| 274 | void trackerlogic_add_random_torrents(size_t amount); | 333 | void trackerlogic_add_random_torrents(size_t amount); |
| 275 | #endif | 334 | #endif |
| @@ -279,5 +338,6 @@ void iterate_all_torrents(int (*for_each)(ot_torrent *torrent, uintptr_t data), | |||
| 279 | 338 | ||
| 280 | /* Helper, before it moves to its own object */ | 339 | /* Helper, before it moves to its own object */ |
| 281 | void free_peerlist(ot_peerlist *peer_list); | 340 | void free_peerlist(ot_peerlist *peer_list); |
| 341 | void free_peer_vector(ot_vector *peer_vector); | ||
| 282 | 342 | ||
| 283 | #endif | 343 | #endif |
