From 6d97e999f1e7b4476faf749abbb5e925d09208c6 Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Sat, 24 Oct 2026 00:15:34 +0200 Subject: Replace endianess helpers and remove unused system and libowfat included * Replace libowfat big endian packers with htonl/ntohl * Turn bucket derivation in ot_mutex.c into its own helper * remove unused includes --- opentracker.c | 2 -- ot_fullscrape.c | 1 - ot_http.c | 1 - ot_iovec.c | 2 -- ot_livesync.c | 15 ++++++++++----- ot_mutex.c | 15 +++++++++++---- ot_stats.c | 2 -- ot_sync.c | 1 - proxy.c | 15 +++++++++------ trackerlogic.c | 3 --- 10 files changed, 30 insertions(+), 27 deletions(-) diff --git a/opentracker.c b/opentracker.c index e24beb2..89bfd3e 100644 --- a/opentracker.c +++ b/opentracker.c @@ -5,7 +5,6 @@ $Id$ */ /* System */ -#include #include #include #include @@ -21,7 +20,6 @@ #endif /* Libowfat */ -#include "byte.h" #include "io.h" #include "iob.h" #include "ip6.h" diff --git a/ot_fullscrape.c b/ot_fullscrape.c index ec7f2df..3e36e8e 100644 --- a/ot_fullscrape.c +++ b/ot_fullscrape.c @@ -10,7 +10,6 @@ #include #include #include -#include #ifdef WANT_COMPRESSION_GZIP #include #endif diff --git a/ot_http.c b/ot_http.c index e4486fc..45dacdc 100644 --- a/ot_http.c +++ b/ot_http.c @@ -15,7 +15,6 @@ /* Libowfat */ #include "array.h" -#include "byte.h" #include "case.h" #include "iob.h" #include "ip6.h" diff --git a/ot_iovec.c b/ot_iovec.c index e2cdd4b..b8ea657 100644 --- a/ot_iovec.c +++ b/ot_iovec.c @@ -5,9 +5,7 @@ /* System */ #include -#include #include -#include /* Libowfat */ diff --git a/ot_livesync.c b/ot_livesync.c index efba4d7..b876ef8 100644 --- a/ot_livesync.c +++ b/ot_livesync.c @@ -4,15 +4,14 @@ $id$ */ /* System */ +#include #include #include #include #include -#include #include /* Libowfat */ -#include "byte.h" #include "ip6.h" #include "ndelay.h" #include "socket.h" @@ -58,6 +57,8 @@ static sync_buffer g_v4_buf; static pthread_t thread_id; void livesync_init(void) { + uint32_t packet_type6 = htonl(OT_SYNC_PEER6); + uint32_t packet_type4 = htonl(OT_SYNC_PEER4); if (g_socket_in == -1) exerr("No socket address for live sync specified."); @@ -66,8 +67,8 @@ void livesync_init(void) { memcpy(g_v6_buf.data, &g_tracker_id, sizeof(g_tracker_id)); memcpy(g_v4_buf.data, &g_tracker_id, sizeof(g_tracker_id)); - uint32_pack_big((char *)g_v6_buf.data + sizeof(g_tracker_id), OT_SYNC_PEER6); - uint32_pack_big((char *)g_v4_buf.data + sizeof(g_tracker_id), OT_SYNC_PEER4); + memcpy(g_v6_buf.data + sizeof(g_tracker_id), &packet_type6, sizeof(packet_type6)); + memcpy(g_v4_buf.data + sizeof(g_tracker_id), &packet_type4, sizeof(packet_type4)); g_v6_buf.fill = sizeof(g_tracker_id) + sizeof(uint32_t); g_v4_buf.fill = sizeof(g_tracker_id) + sizeof(uint32_t); @@ -216,6 +217,8 @@ static void *livesync_worker(void *args) { memcpy(in_ip, V4mappedprefix, sizeof(V4mappedprefix)); while (1) { + uint32_t packet_type; + ws.request_size = socket_recv4(g_socket_in, (char *)ws.inbuf, LIVESYNC_INCOMING_BUFFSIZE, 12 + (char *)in_ip, &in_port); /* Expect at least tracker id and packet type */ @@ -228,7 +231,9 @@ static void *livesync_worker(void *args) { continue; } - switch (uint32_read_big(sizeof(g_tracker_id) + (char *)ws.inbuf)) { + memcpy(&packet_type, ws.inbuf + sizeof(g_tracker_id), sizeof(packet_type)); + + switch (ntohl(packet_type)) { case OT_SYNC_PEER6: livesync_handle_peersync(&ws, OT_PEER_SIZE6); break; diff --git a/ot_mutex.c b/ot_mutex.c index 300da3a..9aa476d 100644 --- a/ot_mutex.c +++ b/ot_mutex.c @@ -4,16 +4,15 @@ $id$ */ /* System */ +#include #include #include #include #include -#include #include /* Libowfat */ #include "io.h" -#include "uint32.h" /* Opentracker */ #include "ot_iovec.h" @@ -37,7 +36,15 @@ ot_vector *mutex_bucket_lock(int bucket) { return all_torrents + bucket; } -ot_vector *mutex_bucket_lock_by_hash(ot_hash const hash) { return mutex_bucket_lock(uint32_read_big((const char *)hash) >> OT_BUCKET_COUNT_SHIFT); } +/* Torrents are spread over buckets by the first four bytes of their info_hash, + which may sit at any alignment, hence the memcpy */ +static int bucket_from_hash(ot_hash const hash) { + uint32_t bucket; + memcpy(&bucket, hash, sizeof(bucket)); + return ntohl(bucket) >> OT_BUCKET_COUNT_SHIFT; +} + +ot_vector *mutex_bucket_lock_by_hash(ot_hash const hash) { return mutex_bucket_lock(bucket_from_hash(hash)); } void mutex_bucket_unlock(int bucket, int delta_torrentcount) { pthread_mutex_unlock(bucket_mutex + bucket); @@ -45,7 +52,7 @@ void mutex_bucket_unlock(int bucket, int delta_torrentcount) { } void mutex_bucket_unlock_by_hash(ot_hash const hash, int delta_torrentcount) { - mutex_bucket_unlock(uint32_read_big((char *)hash) >> OT_BUCKET_COUNT_SHIFT, delta_torrentcount); + mutex_bucket_unlock(bucket_from_hash(hash), delta_torrentcount); } size_t mutex_get_torrent_count(void) { return g_torrent_count; } diff --git a/ot_stats.c b/ot_stats.c index 15eef75..afc972a 100644 --- a/ot_stats.c +++ b/ot_stats.c @@ -4,13 +4,11 @@ $id$ */ /* System */ -#include #include #include #include #include #include -#include #include #include #include diff --git a/ot_sync.c b/ot_sync.c index 2241bf3..d3d30fa 100644 --- a/ot_sync.c +++ b/ot_sync.c @@ -7,7 +7,6 @@ #include #include #include -#include #include #include diff --git a/proxy.c b/proxy.c index 5a7698b..cec985e 100644 --- a/proxy.c +++ b/proxy.c @@ -8,8 +8,6 @@ #include #include #include -#include -#include #include #include #include @@ -472,13 +470,15 @@ static void handle_write(int64 peersocket) { } static void server_mainloop() { - int64 sock; + int64 sock; + uint32_t packet_type; /* inlined livesync_init() */ memset(g_peerbuffer_start, 0, sizeof(g_peerbuffer_start)); g_peerbuffer_pos = g_peerbuffer_start; memcpy(g_peerbuffer_pos, &g_tracker_id, sizeof(g_tracker_id)); - uint32_pack_big((char *)g_peerbuffer_pos + sizeof(g_tracker_id), OT_SYNC_PEER); + packet_type = htonl(OT_SYNC_PEER); + memcpy(g_peerbuffer_pos + sizeof(g_tracker_id), &packet_type, sizeof(packet_type)); g_peerbuffer_pos += sizeof(g_tracker_id) + sizeof(uint32_t); g_next_packet_time = time(NULL) + LIVESYNC_MAXDELAY; @@ -870,6 +870,7 @@ static void *livesync_worker(void *args) { while (1) { ot_ip6 in_ip; uint16_t in_port; + uint32_t packet_type; size_t datalen = socket_recv4(g_socket_in, (char *)g_inbuffer, LIVESYNC_INCOMING_BUFFSIZE, 12 + (char *)in_ip, &in_port); /* Expect at least tracker id and packet type */ @@ -879,7 +880,9 @@ static void *livesync_worker(void *args) { /* drop packet coming from ourselves */ continue; } - switch (uint32_read_big((char *)g_inbuffer + sizeof(g_tracker_id))) { + memcpy(&packet_type, g_inbuffer + sizeof(g_tracker_id), sizeof(packet_type)); + + switch (ntohl(packet_type)) { case OT_SYNC_PEER4: livesync_handle_peersync(datalen, OT_PEER_SIZE4); break; @@ -887,7 +890,7 @@ static void *livesync_worker(void *args) { livesync_handle_peersync(datalen, OT_PEER_SIZE6); break; default: - // fprintf( stderr, "Received an unknown live sync packet type %u.\n", uint32_read_big( sizeof( g_tracker_id ) + (char*)g_inbuffer ) ); + // fprintf( stderr, "Received an unknown live sync packet type %u.\n", ntohl( packet_type ) ); break; } } diff --git a/trackerlogic.c b/trackerlogic.c index a76064e..59e9643 100644 --- a/trackerlogic.c +++ b/trackerlogic.c @@ -5,15 +5,12 @@ /* System */ #include -#include #include #include #include #include -#include /* Libowfat */ -#include "array.h" #include "io.h" #include "iob.h" #include "ip6.h" -- cgit v1.3