summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDirk Engling <erdgeist@erdgeist.org>2026-10-24 01:50:25 +0200
committerDirk Engling <erdgeist@erdgeist.org>2026-10-24 01:50:25 +0200
commitecb1f1e92ab449023d1427f9db26c5bac09c4901 (patch)
tree5af48da0cac32e2927aa5e2946c9f08173fedb69
parente4b6decf72a91453bc8d31b4b15e7aed923c56d2 (diff)
Use new entropy fascility and add tests for it
-rw-r--r--Makefile17
-rw-r--r--Makefile.arc4random3
-rw-r--r--opentracker.c15
-rw-r--r--ot_udp.c22
-rw-r--r--proxy.c9
-rw-r--r--tests/runner.c1
-rw-r--r--tests/test.h1
-rw-r--r--trackerlogic.c7
-rw-r--r--trackerlogic.h7
9 files changed, 25 insertions, 57 deletions
diff --git a/Makefile b/Makefile
index 8f967f7..60a61cd 100644
--- a/Makefile
+++ b/Makefile
@@ -51,9 +51,10 @@ FEATURES+=-DWANT_FULLSCRAPE
51# full scrape transfer, if you rely on an older versions, enable this flag 51# full scrape transfer, if you rely on an older versions, enable this flag
52#FEATURES+=-DWANT_NO_AUTO_FREE 52#FEATURES+=-DWANT_NO_AUTO_FREE
53 53
54# Is enabled on BSD systems by default in trackerlogic.h 54# opentracker takes its randomness from arc4random, which is part of libc on
55# on Linux systems the include Makefile adds -lbsd 55# the BSDs, on macOS and on glibc 2.36 and newer. On an older glibc or on musl
56#include Makefile.arc4random 56# it comes from libbsd instead, so uncomment the next include to link it.
57#LDFLAGS+=-lbsd
57 58
58#FEATURES+=-D_DEBUG_HTTPERROR 59#FEATURES+=-D_DEBUG_HTTPERROR
59#FEATURES+=-D_DEBUG_RANDOMTORRENTS 60#FEATURES+=-D_DEBUG_RANDOMTORRENTS
@@ -67,9 +68,9 @@ CFLAGS+=-I$(LIBOWFAT_HEADERS) -DGIT_VERSION=$(GIT_VERSION) -Wall -pipe -pthread
67LDFLAGS+=-L$(LIBOWFAT_LIBRARY) -lowfat -pthread 68LDFLAGS+=-L$(LIBOWFAT_LIBRARY) -lowfat -pthread
68 69
69BINARY =opentracker 70BINARY =opentracker
70HEADERS=trackerlogic.h scan_urlencoded_query.h ot_mutex.h ot_stats.h ot_vector.h ot_clean.h ot_udp.h ot_iovec.h ot_fullscrape.h ot_accesslist.h ot_http.h ot_livesync.h ot_rijndael.h 71HEADERS=trackerlogic.h scan_urlencoded_query.h ot_mutex.h ot_random.h ot_stats.h ot_vector.h ot_clean.h ot_udp.h ot_iovec.h ot_fullscrape.h ot_accesslist.h ot_http.h ot_livesync.h ot_rijndael.h
71SOURCES=opentracker.c trackerlogic.c scan_urlencoded_query.c ot_mutex.c ot_stats.c ot_vector.c ot_clean.c ot_udp.c ot_iovec.c ot_fullscrape.c ot_accesslist.c ot_http.c ot_livesync.c ot_rijndael.c 72SOURCES=opentracker.c trackerlogic.c scan_urlencoded_query.c ot_mutex.c ot_random.c ot_stats.c ot_vector.c ot_clean.c ot_udp.c ot_iovec.c ot_fullscrape.c ot_accesslist.c ot_http.c ot_livesync.c ot_rijndael.c
72SOURCES_proxy=proxy.c ot_vector.c ot_mutex.c 73SOURCES_proxy=proxy.c ot_vector.c ot_mutex.c ot_random.c
73 74
74OBJECTS = $(SOURCES:%.c=%.o) 75OBJECTS = $(SOURCES:%.c=%.o)
75OBJECTS_debug = $(SOURCES:%.c=%.debug.o) 76OBJECTS_debug = $(SOURCES:%.c=%.debug.o)
@@ -104,8 +105,8 @@ proxy.debug: $(OBJECTS_proxy_debug) $(HEADERS)
104# has to. tests/stubs.c supplies the few symbols those modules reference but 105# has to. tests/stubs.c supplies the few symbols those modules reference but
105# the tests never reach. 106# the tests never reach.
106TESTBIN=tests/runner 107TESTBIN=tests/runner
107TEST_SOURCES=tests/runner.c tests/test.c tests/stubs.c tests/t_scan_urlencoded_query.c tests/t_iovec.c tests/t_vector.c 108TEST_SOURCES=tests/runner.c tests/test.c tests/stubs.c tests/t_scan_urlencoded_query.c tests/t_iovec.c tests/t_vector.c tests/t_random.c
108TEST_UNITS=scan_urlencoded_query.c ot_iovec.c ot_vector.c 109TEST_UNITS=scan_urlencoded_query.c ot_iovec.c ot_vector.c ot_random.c
109TEST_HEADERS=tests/test.h $(HEADERS) 110TEST_HEADERS=tests/test.h $(HEADERS)
110CFLAGS_test = $(CFLAGS) -I. -g -O1 $(FEATURES) 111CFLAGS_test = $(CFLAGS) -I. -g -O1 $(FEATURES)
111 112
diff --git a/Makefile.arc4random b/Makefile.arc4random
deleted file mode 100644
index 1488408..0000000
--- a/Makefile.arc4random
+++ /dev/null
@@ -1,3 +0,0 @@
1FEATURES+=-DWANT_ARC4RANDOM
2LDFLAGS+=-lbsd
3
diff --git a/opentracker.c b/opentracker.c
index 89bfd3e..4d44950 100644
--- a/opentracker.c
+++ b/opentracker.c
@@ -31,6 +31,7 @@
31#include "ot_http.h" 31#include "ot_http.h"
32#include "ot_livesync.h" 32#include "ot_livesync.h"
33#include "ot_mutex.h" 33#include "ot_mutex.h"
34#include "ot_random.h"
34#include "ot_stats.h" 35#include "ot_stats.h"
35#include "ot_udp.h" 36#include "ot_udp.h"
36#include "trackerlogic.h" 37#include "trackerlogic.h"
@@ -294,13 +295,7 @@ static void *server_mainloop(void *args) {
294 if (!ws.inbuf || !ws.outbuf) 295 if (!ws.inbuf || !ws.outbuf)
295 panic("Initializing worker failed"); 296 panic("Initializing worker failed");
296 297
297#ifdef WANT_ARC4RANDOM 298 random_bytes(ws.rand48_state, sizeof(ws.rand48_state));
298 arc4random_buf(&ws.rand48_state[0], 3 * sizeof(uint16_t));
299#else
300 ws.rand48_state[0] = (uint16_t)random();
301 ws.rand48_state[1] = (uint16_t)random();
302 ws.rand48_state[2] = (uint16_t)random();
303#endif
304 299
305 for (;;) { 300 for (;;) {
306 int64 sock; 301 int64 sock;
@@ -743,11 +738,7 @@ int main(int argc, char **argv) {
743 serverip[10] = serverip[11] = -1; 738 serverip[10] = serverip[11] = -1;
744#endif 739#endif
745 740
746#ifdef WANT_DEV_RANDOM 741 random_init();
747 srandomdev();
748#else
749 srandom(time(NULL));
750#endif
751 742
752 while (scanon) { 743 while (scanon) {
753 switch (getopt(argc, argv, 744 switch (getopt(argc, argv,
diff --git a/ot_udp.c b/ot_udp.c
index fae2c87..82aa8bd 100644
--- a/ot_udp.c
+++ b/ot_udp.c
@@ -16,6 +16,7 @@
16#include "socket.h" 16#include "socket.h"
17 17
18/* Opentracker */ 18/* Opentracker */
19#include "ot_random.h"
19#include "ot_rijndael.h" 20#include "ot_rijndael.h"
20#include "ot_stats.h" 21#include "ot_stats.h"
21#include "ot_udp.h" 22#include "ot_udp.h"
@@ -34,21 +35,10 @@ static int g_opentracker_started = 0;
34 35
35static void udp_generate_rijndael_round_key(void) { 36static void udp_generate_rijndael_round_key(void) {
36 uint32_t key[16]; 37 uint32_t key[16];
37#ifdef WANT_ARC4RANDOM 38 random_bytes(key, sizeof(key));
38 arc4random_buf(&key[0], sizeof(key));
39#else
40 key[0] = random();
41 key[1] = random();
42 key[2] = random();
43 key[3] = random();
44#endif
45 rijndaelKeySetupEnc128(g_rijndael_round_key, (uint8_t *)key); 39 rijndaelKeySetupEnc128(g_rijndael_round_key, (uint8_t *)key);
46 40
47#ifdef WANT_ARC4RANDOM 41 g_key_of_the_hour[0] = random_uint32();
48 g_key_of_the_hour[0] = arc4random();
49#else
50 g_key_of_the_hour[0] = random();
51#endif
52 g_hour_of_the_key = g_now_minutes; 42 g_hour_of_the_key = g_now_minutes;
53} 43}
54 44
@@ -60,11 +50,7 @@ static void udp_make_connectionid(uint32_t connid[2], const ot_ip6 remoteip, int
60 50
61 if (g_now_minutes - g_hour_of_the_key >= 60) { 51 if (g_now_minutes - g_hour_of_the_key >= 60) {
62 uint32_t old_key_of_the_hour = g_key_of_the_hour[0]; 52 uint32_t old_key_of_the_hour = g_key_of_the_hour[0];
63#ifdef WANT_ARC4RANDOM 53 uint32_t new_key_of_the_hour = random_uint32();
64 uint32_t new_key_of_the_hour = arc4random();
65#else
66 uint32_t new_key_of_the_hour = random();
67#endif
68 /* If in the meantime another thread has performed 54 /* If in the meantime another thread has performed
69 key rotation, do not overwrite their results */ 55 key rotation, do not overwrite their results */
70 if (g_now_minutes - g_hour_of_the_key >= 60) { 56 if (g_now_minutes - g_hour_of_the_key >= 60) {
diff --git a/proxy.c b/proxy.c
index cec985e..d728e5b 100644
--- a/proxy.c
+++ b/proxy.c
@@ -25,6 +25,7 @@
25 25
26/* Opentracker */ 26/* Opentracker */
27#include "ot_mutex.h" 27#include "ot_mutex.h"
28#include "ot_random.h"
28#include "ot_stats.h" 29#include "ot_stats.h"
29#include "ot_vector.h" 30#include "ot_vector.h"
30#include "trackerlogic.h" 31#include "trackerlogic.h"
@@ -563,12 +564,8 @@ int main(int argc, char **argv) {
563 uint16_t tmpport; 564 uint16_t tmpport;
564 int scanon = 1, lbound = 0, sbound = 0; 565 int scanon = 1, lbound = 0, sbound = 0;
565 566
566 srandom(time(NULL)); 567 random_init();
567#ifdef WANT_ARC4RANDOM 568 g_tracker_id = random_uint32();
568 g_tracker_id = arc4random();
569#else
570 g_tracker_id = random();
571#endif
572 569
573 while (scanon) { 570 while (scanon) {
574 switch (getopt(argc, argv, ":l:c:L:h")) { 571 switch (getopt(argc, argv, ":l:c:L:h")) {
diff --git a/tests/runner.c b/tests/runner.c
index d73c516..1b6deb1 100644
--- a/tests/runner.c
+++ b/tests/runner.c
@@ -17,6 +17,7 @@ struct module {
17static const struct module modules[] = {{"scan_urlencoded_query", test_scan_urlencoded_query}, 17static const struct module modules[] = {{"scan_urlencoded_query", test_scan_urlencoded_query},
18 {"ot_iovec", test_iovec}, 18 {"ot_iovec", test_iovec},
19 {"ot_vector", test_vector}, 19 {"ot_vector", test_vector},
20 {"ot_random", test_random},
20 {NULL, NULL}}; 21 {NULL, NULL}};
21 22
22/* Run every module, or only those whose name contains argv[1] */ 23/* Run every module, or only those whose name contains argv[1] */
diff --git a/tests/test.h b/tests/test.h
index 941e058..19acb2f 100644
--- a/tests/test.h
+++ b/tests/test.h
@@ -79,5 +79,6 @@ extern unsigned long test_failures;
79void test_scan_urlencoded_query(void); 79void test_scan_urlencoded_query(void);
80void test_iovec(void); 80void test_iovec(void);
81void test_vector(void); 81void test_vector(void);
82void test_random(void);
82 83
83#endif 84#endif
diff --git a/trackerlogic.c b/trackerlogic.c
index 59e9643..d8a2f9b 100644
--- a/trackerlogic.c
+++ b/trackerlogic.c
@@ -22,6 +22,7 @@
22#include "ot_http.h" 22#include "ot_http.h"
23#include "ot_livesync.h" 23#include "ot_livesync.h"
24#include "ot_mutex.h" 24#include "ot_mutex.h"
25#include "ot_random.h"
25#include "ot_stats.h" 26#include "ot_stats.h"
26#include "ot_vector.h" 27#include "ot_vector.h"
27#include "ot_udp.h" 28#include "ot_udp.h"
@@ -536,8 +537,8 @@ void trackerlogic_add_random_torrents(size_t amount) {
536 ws.hash = (ot_hash *)ws.inbuf; 537 ws.hash = (ot_hash *)ws.inbuf;
537 538
538 while (amount--) { 539 while (amount--) {
539 arc4random_buf(ws.hash, sizeof(ot_hash)); 540 random_bytes(ws.hash, sizeof(ot_hash));
540 arc4random_buf(&ws.peer, sizeof(ws.peer)); 541 random_bytes(&ws.peer, sizeof(ws.peer));
541 542
542 OT_PEERFLAG(ws.peer) &= PEER_FLAG_SEEDING | PEER_FLAG_COMPLETED | PEER_FLAG_STOPPED; 543 OT_PEERFLAG(ws.peer) &= PEER_FLAG_SEEDING | PEER_FLAG_COMPLETED | PEER_FLAG_STOPPED;
543 544
@@ -555,7 +556,7 @@ void exerr(char *message) {
555} 556}
556 557
557void trackerlogic_init(void) { 558void trackerlogic_init(void) {
558 g_tracker_id = random(); 559 g_tracker_id = random_uint32();
559 560
560 if (!g_stats_path) 561 if (!g_stats_path)
561 g_stats_path = "stats"; 562 g_stats_path = "stats";
diff --git a/trackerlogic.h b/trackerlogic.h
index 88d9a11..aa10a54 100644
--- a/trackerlogic.h
+++ b/trackerlogic.h
@@ -12,13 +12,6 @@
12#include <sys/types.h> 12#include <sys/types.h>
13#include <time.h> 13#include <time.h>
14 14
15#if defined(__linux__) && defined(WANT_ARC4RANDOM)
16#include <bsd/stdlib.h>
17#endif
18#ifdef __FreeBSD__
19#define WANT_ARC4RANDOM
20#endif
21
22typedef uint8_t ot_hash[20]; 15typedef uint8_t ot_hash[20];
23typedef time_t ot_time; 16typedef time_t ot_time;
24typedef char ot_ip6[16]; 17typedef char ot_ip6[16];