From 248bf17de8a25c741393b28e75c4e5380947792c Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Sat, 24 Oct 2026 05:27:59 +0200 Subject: Use new ranged unsigned int scanner everywhere and squash some bugs along the way --- opentracker.c | 44 ++++++++++++++++++++++++++------------------ ot_http.c | 6 ++++-- ot_sync.c | 12 ++++++------ proxy.c | 8 +++++--- 4 files changed, 41 insertions(+), 29 deletions(-) diff --git a/opentracker.c b/opentracker.c index f829171..bcc075c 100644 --- a/opentracker.c +++ b/opentracker.c @@ -7,9 +7,11 @@ /* System */ #include #include +#include #include #include #include +#include #include #include #include @@ -23,7 +25,6 @@ #include "io.h" #include "iob.h" #include "ip6.h" -#include "scan.h" #include "socket.h" /* Opentracker */ @@ -32,6 +33,7 @@ #include "ot_http.h" #include "ot_livesync.h" #include "ot_mutex.h" +#include "ot_scan.h" #include "ot_random.h" #include "ot_stats.h" #include "ot_udp.h" @@ -380,8 +382,9 @@ char *set_config_option(char **option, char *value) { return *option = strdup(value); } -static int scan_ip6_port(const char *src, ot_ip6 ip, uint16 *port) { +static int scan_ip6_port(const char *src, ot_ip6 ip, uint16_t *port) { const char *s = src; + uint64_t value; int off, bracket = 0; while (isspace(*s)) ++s; @@ -404,13 +407,15 @@ static int scan_ip6_port(const char *src, ot_ip6 ip, uint16 *port) { if (*(s++) != ':') return 0; } - if (!(off = scan_ushort(s, port))) + if (!(off = scan_udec(s, &value, 65535))) return 0; + *port = (uint16_t)value; return off + s - src; } static int scan_ip6_net(const char *src, ot_net *net) { const char *s = src; + uint64_t value; int off; while (isspace(*s)) ++s; @@ -421,8 +426,9 @@ static int scan_ip6_net(const char *src, ot_net *net) { net->bits = 128; else { s++; - if (!(off = scan_int(s, &net->bits))) + if (!(off = scan_udec(s, &value, 128))) return 0; + net->bits = (int)value; if (ip6_isv4mapped(net->address)) net->bits += 96; if (net->bits > 128) @@ -523,7 +529,8 @@ int parse_configfile(char *config_filename) { } while (fgets(inbuf, sizeof(inbuf), config_file_handle)) { - char *p = inbuf, *end; + char *p = inbuf, *end; + uint64_t value; uint16_t tmpport = 6969; /* Skip white spaces */ @@ -568,7 +575,8 @@ int parse_configfile(char *config_filename) { ++bound; break; case CONFIG_LISTEN_UDP_WORKERS: - scan_uint(p, &g_udp_workers); + if (scan_udec(p, &value, UINT_MAX)) + g_udp_workers = (unsigned int)value; break; #ifdef WANT_ACCESSLIST_WHITE case CONFIG_ACCESS_WHITELIST: @@ -634,9 +642,9 @@ int parse_configfile(char *config_filename) { void load_state(const char *const state_filename) { FILE *state_filehandle; char inbuf[512]; - ot_hash infohash; - unsigned long long base, downcount; - int consumed; + ot_hash infohash; + uint64_t base, downcount; + int consumed; state_filehandle = fopen(state_filename, "r"); @@ -653,10 +661,10 @@ void load_state(const char *const state_filename) { continue; i = 2 * sizeof(ot_hash); - if (inbuf[i++] != ':' || !(consumed = scan_ulonglong(inbuf + i, &base))) + if (inbuf[i++] != ':' || !(consumed = scan_udec(inbuf + i, &base, UINT64_MAX))) continue; i += consumed; - if (inbuf[i++] != ':' || !(consumed = scan_ulonglong(inbuf + i, &downcount))) + if (inbuf[i++] != ':' || !(consumed = scan_udec(inbuf + i, &downcount, UINT64_MAX))) continue; add_torrent_from_saved_state(infohash, base, downcount); } @@ -725,7 +733,7 @@ int main(int argc, char **argv) { ot_ip6 serverip; ot_net tmpnet; int bound = 0, scanon = 1; - uint16_t tmpport; + uint64_t value; char *statefile = 0; pthread_t thread_id; /* time cacher */ @@ -764,28 +772,28 @@ int main(int argc, char **argv) { break; #endif case 'p': - if (!scan_ushort(optarg, &tmpport)) { + if (!scan_udec(optarg, &value, 65535)) { usage(argv[0]); exit(1); } - ot_try_bind(serverip, tmpport, FLAG_TCP); + ot_try_bind(serverip, (uint16_t)value, FLAG_TCP); bound++; break; case 'P': - if (!scan_ushort(optarg, &tmpport)) { + if (!scan_udec(optarg, &value, 65535)) { usage(argv[0]); exit(1); } - ot_try_bind(serverip, tmpport, FLAG_UDP); + ot_try_bind(serverip, (uint16_t)value, FLAG_UDP); bound++; break; #ifdef WANT_SYNC_LIVE case 's': - if (!scan_ushort(optarg, &tmpport)) { + if (!scan_udec(optarg, &value, 65535)) { usage(argv[0]); exit(1); } - livesync_bind_mcast(serverip, tmpport); + livesync_bind_mcast(serverip, (uint16_t)value); break; #endif case 'd': diff --git a/ot_http.c b/ot_http.c index 69bbc8e..626547b 100644 --- a/ot_http.c +++ b/ot_http.c @@ -18,7 +18,6 @@ #include "array.h" #include "iob.h" #include "ip6.h" -#include "scan.h" /* Opentracker */ #include "ot_accesslist.h" @@ -26,6 +25,7 @@ #include "ot_http.h" #include "ot_iovec.h" #include "ot_mutex.h" +#include "ot_scan.h" #include "ot_stats.h" #include "scan_urlencoded_query.h" #include "trackerlogic.h" @@ -618,6 +618,7 @@ static ssize_t http_handle_announce(const int64 sock, struct ot_workstruct * // if( accesslist_is_blessed( cookie->ip, OT_PERMISSION_MAY_STAT ) ) { char *tmp_buf = ws->reply; ot_net net; + uint64_t value; signed short parsed, bits; len = scan_urlencoded_query(&read_ptr, tmp_buf, SCAN_SEARCHPATH_VALUE); @@ -634,9 +635,10 @@ static ssize_t http_handle_announce(const int64 sock, struct ot_workstruct * if (tmp_buf[parsed++] != '/') bits = 128; else { - parsed = scan_short(tmp_buf + parsed, &bits); + parsed = scan_udec(tmp_buf + parsed, &value, 128); if (!parsed) HTTPERROR_400_PARAM; + bits = (signed short)value; if (ip6_isv4mapped(net.address)) bits += 96; } diff --git a/ot_sync.c b/ot_sync.c index d3d30fa..fa6bb8b 100644 --- a/ot_sync.c +++ b/ot_sync.c @@ -12,11 +12,11 @@ /* Libowfat */ #include "io.h" -#include "scan.h" /* Opentracker */ #include "ot_iovec.h" #include "ot_mutex.h" +#include "ot_scan.h" #include "ot_stats.h" #include "ot_sync.h" #include "trackerlogic.h" @@ -30,9 +30,9 @@ [..]: ( 20:01234567890abcdefghij16:XXXXYYYY )+ */ int add_changeset_to_tracker(uint8_t *data, size_t len) { - ot_hash *hash; - uint8_t *end = data + len; - unsigned long peer_count; + ot_hash *hash; + uint8_t *end = data + len; + uint64_t peer_count; /* We do know, that the string is \n terminated, so it can't overflow */ @@ -50,8 +50,8 @@ int add_changeset_to_tracker(uint8_t *data, size_t len) { hash = (ot_hash *)data; data += sizeof(ot_hash); - /* Scan string length indicator */ - data += (len = scan_ulong((char *)data, &peer_count)); + /* Scan string length indicator, strictly: bencode writes no leading zeros */ + data += (len = scan_udec_bencode((char *)data, &peer_count, (uint64_t)(end - data))); /* If no long was scanned, it is not divisible by 8, it is not followed by a colon or claims to need to much memory, we fail */ diff --git a/proxy.c b/proxy.c index ec51630..19a084a 100644 --- a/proxy.c +++ b/proxy.c @@ -20,11 +20,11 @@ #include "io.h" #include "iob.h" #include "ip6.h" -#include "scan.h" #include "socket.h" /* Opentracker */ #include "ot_mutex.h" +#include "ot_scan.h" #include "ot_random.h" #include "ot_stats.h" #include "ot_vector.h" @@ -531,8 +531,9 @@ static int64_t ot_try_bind(ot_ip6 ip, uint16_t port) { return sock; } -static int scan_ip6_port(const char *src, ot_ip6 ip, uint16 *port) { +static int scan_ip6_port(const char *src, ot_ip6 ip, uint16_t *port) { const char *s = src; + uint64_t value; int off, bracket = 0; while (isspace(*s)) ++s; @@ -553,8 +554,9 @@ static int scan_ip6_port(const char *src, ot_ip6 ip, uint16 *port) { if (*(s++) != ':') return 0; } - if (!(off = scan_ushort(s, port))) + if (!(off = scan_udec(s, &value, 65535))) return 0; + *port = (uint16_t)value; return off + s - src; } -- cgit v1.3