summaryrefslogtreecommitdiff
path: root/trackerlogic.h
diff options
context:
space:
mode:
Diffstat (limited to 'trackerlogic.h')
-rw-r--r--trackerlogic.h79
1 files changed, 73 insertions, 6 deletions
diff --git a/trackerlogic.h b/trackerlogic.h
index aa10a54..ad6c911 100644
--- a/trackerlogic.h
+++ b/trackerlogic.h
@@ -6,6 +6,7 @@
6#ifndef OT_TRACKERLOGIC_H__ 6#ifndef OT_TRACKERLOGIC_H__
7#define OT_TRACKERLOGIC_H__ 7#define OT_TRACKERLOGIC_H__
8 8
9#include <stdatomic.h>
9#include <stdint.h> 10#include <stdint.h>
10#include <stdlib.h> 11#include <stdlib.h>
11#include <sys/time.h> 12#include <sys/time.h>
@@ -67,10 +68,28 @@ typedef struct {
67 on startup */ 68 on startup */
68#define RANDOMTORRENTS (1024 * 1024 * 1) 69#define RANDOMTORRENTS (1024 * 1024 * 1)
69 70
70/* From opentracker.c */ 71#if defined(CLOCK_REALTIME_COARSE)
71extern time_t g_now_seconds; 72#define OT_CLOCK_CHEAP CLOCK_REALTIME_COARSE /* Linux */
72extern volatile int g_opentracker_running; 73#elif defined(CLOCK_REALTIME_FAST)
73#define g_now_minutes (g_now_seconds / 60) 74#define OT_CLOCK_CHEAP CLOCK_REALTIME_FAST /* the BSDs */
75#else
76#define OT_CLOCK_CHEAP CLOCK_REALTIME /* macOS and whatever else */
77#endif
78
79static inline ot_time time_now_seconds(void) {
80 struct timespec now;
81 clock_gettime(OT_CLOCK_CHEAP, &now);
82 return (ot_time)now.tv_sec;
83}
84
85static inline ot_time time_now_minutes(void) { return time_now_seconds() / 60; }
86
87/* For the deadlines the tracker sets on itself, which read better as the
88 span they allow than as an addition to the current second */
89static inline ot_time time_future_seconds(int span) { return time_now_seconds() + span; }
90/* Cleared from the interrupt handler and read by every worker */
91extern _Atomic int g_opentracker_running;
92
74 93
75extern uint32_t g_tracker_id; 94extern uint32_t g_tracker_id;
76typedef enum { FLAG_TCP, FLAG_UDP, FLAG_MCA, FLAG_SELFPIPE } PROTO_FLAG; 95typedef enum { FLAG_TCP, FLAG_UDP, FLAG_MCA, FLAG_SELFPIPE } PROTO_FLAG;
@@ -129,8 +148,56 @@ struct ot_peerlist {
129}; 148};
130#define OT_PEERLIST_HASBUCKETS(peer_list) ((peer_list)->peers.size > (peer_list)->peers.space) 149#define OT_PEERLIST_HASBUCKETS(peer_list) ((peer_list)->peers.size > (peer_list)->peers.space)
131 150
151/* The http replies the tracker counts when it has to refuse a request. */
152enum {
153 CODE_HTTPERROR_302,
154 CODE_HTTPERROR_400,
155 CODE_HTTPERROR_400_PARAM,
156 CODE_HTTPERROR_400_COMPACT,
157 CODE_HTTPERROR_402_NOTMODEST,
158 CODE_HTTPERROR_402_PAYMENT_REQUIRED,
159 CODE_HTTPERROR_403_IP,
160 CODE_HTTPERROR_404,
161 CODE_HTTPERROR_500,
162
163 CODE_HTTPERROR_COUNT
164};
165
166/* One statistic. Used when workers report stats to their workstruct */
167typedef _Atomic unsigned long long ot_counter;
168
169/* What each thread counts for itself. The two runs at the end are indexed by
170 the http error code and by the age in minutes of a renewed peer */
171typedef enum {
172 OT_COUNTER_TCP_CONNECTIONS,
173 OT_COUNTER_UDP_CONNECTIONS,
174 OT_COUNTER_TCP_ANNOUNCES,
175 OT_COUNTER_UDP_ANNOUNCES,
176 OT_COUNTER_TCP_SCRAPES,
177 OT_COUNTER_UDP_SCRAPES,
178 OT_COUNTER_TCP_CONNECTS,
179 OT_COUNTER_UDP_CONNECTS,
180 OT_COUNTER_UDP_CONNID_MISSMATCH,
181 OT_COUNTER_COMPLETED,
182 OT_COUNTER_FULLSCRAPES,
183 OT_COUNTER_FULLSCRAPE_REQUESTS,
184 OT_COUNTER_FULLSCRAPE_BYTES,
185 OT_COUNTER_SYNCS,
186 OT_COUNTER_STALLS,
187
188 OT_COUNTER_FAILED, /* CODE_HTTPERROR_COUNT of them */
189 OT_COUNTER_RENEWED = OT_COUNTER_FAILED + CODE_HTTPERROR_COUNT, /* OT_PEER_TIMEOUT of them */
190
191 OT_COUNTER_COUNT = OT_COUNTER_RENEWED + OT_PEER_TIMEOUT
192} ot_counter_index;
193
132struct ot_workstruct { 194struct ot_workstruct {
133 /* Thread specific, static */ 195 /* Thread specific, static */
196
197 /* This thread's own statistics, and the link ot_stats keeps them on */
198 ot_counter counters[OT_COUNTER_COUNT];
199 struct ot_workstruct *counters_next;
200
134 char *inbuf; 201 char *inbuf;
135#define G_INBUF_SIZE 8192 202#define G_INBUF_SIZE 8192
136 char *outbuf; 203 char *outbuf;
@@ -181,13 +248,13 @@ struct ot_workstruct {
181 248
182void trackerlogic_init(void); 249void trackerlogic_init(void);
183void trackerlogic_deinit(void); 250void trackerlogic_deinit(void);
184void exerr(char *message); 251void exerr(const char *message);
185 252
186/* add_peer_to_torrent does only release the torrent bucket if from_sync is set, 253/* add_peer_to_torrent does only release the torrent bucket if from_sync is set,
187 otherwise it is released in return_peers_for_torrent */ 254 otherwise it is released in return_peers_for_torrent */
188size_t add_peer_to_torrent_and_return_peers(PROTO_FLAG proto, struct ot_workstruct *ws, size_t amount); 255size_t add_peer_to_torrent_and_return_peers(PROTO_FLAG proto, struct ot_workstruct *ws, size_t amount);
189size_t remove_peer_from_torrent(PROTO_FLAG proto, struct ot_workstruct *ws); 256size_t remove_peer_from_torrent(PROTO_FLAG proto, struct ot_workstruct *ws);
190size_t return_tcp_scrape_for_torrent(ot_hash const *hash_list, size_t amount, char *reply); 257void return_tcp_scrape_for_torrent(ot_hash const *hash_list, size_t amount, char **reply, const char *reply_end);
191size_t return_udp_scrape_for_torrent(ot_hash const hash, char *reply); 258size_t return_udp_scrape_for_torrent(ot_hash const hash, char *reply);
192void add_torrent_from_saved_state(ot_hash const hash, ot_time base, size_t down_count); 259void add_torrent_from_saved_state(ot_hash const hash, ot_time base, size_t down_count);
193#ifdef _DEBUG_RANDOMTORRENTS 260#ifdef _DEBUG_RANDOMTORRENTS