From d61aefe2fc4d8549f459bdfc2749c9f163449ebc Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Sat, 24 Oct 2026 17:23:32 +0200 Subject: Replace libowfat's ip scanner and formatters with own own --- opentracker.c | 16 ++++++++-------- ot_accesslist.c | 6 +++--- ot_http.c | 10 +++++----- ot_livesync.c | 10 +++++----- ot_stats.c | 13 ++++++------- ot_udp.c | 4 ++-- proxy.c | 10 +++++----- tests/runner.c | 1 + tests/test.h | 1 + trackerlogic.c | 8 ++++---- 10 files changed, 40 insertions(+), 39 deletions(-) diff --git a/opentracker.c b/opentracker.c index 354e837..8a39bde 100644 --- a/opentracker.c +++ b/opentracker.c @@ -24,17 +24,17 @@ /* Libowfat */ #include "io.h" #include "iob.h" -#include "ip6.h" #include "socket.h" /* Opentracker */ #include "ot_accesslist.h" #include "ot_hex.h" #include "ot_http.h" +#include "ot_ip.h" #include "ot_livesync.h" #include "ot_mutex.h" -#include "ot_scan.h" #include "ot_random.h" +#include "ot_scan.h" #include "ot_stats.h" #include "ot_udp.h" #include "trackerlogic.h" @@ -346,7 +346,7 @@ static int64_t ot_try_bind(ot_ip6 ip, uint16_t port, PROTO_FLAG proto) { char *protos[] = {"TCP", "UDP", "UDP mcast"}; char _debug[512]; int off = snprintf(_debug, sizeof(_debug), "Binding socket type %s to address [", protos[proto]); - off += fmt_ip6c(_debug + off, ip); + off += ip_format(_debug + off, ip); snprintf(_debug + off, sizeof(_debug) - off, "]:%d...", port); fputs(_debug, stderr); } @@ -394,14 +394,14 @@ static int scan_ip6_port(const char *src, ot_ip6 ip, uint16_t *port) { ++s; if (*s == '[') ++s, ++bracket; /* for v6 style notation */ - if (!(off = scan_ip6(s, ip))) + if (!(off = ip_scan(s, ip))) return 0; s += off; if (bracket && *s == ']') ++s; if (*s == 0 || isspace(*s)) return s - src; - if (!ip6_isv4mapped(ip)) { + if (!ip_is_v4mapped(ip)) { if (*s != ':' && *s != '.') return 0; if (!bracket && *(s) == ':') @@ -423,7 +423,7 @@ static int scan_ip6_net(const char *src, ot_net *net) { int off; while (isspace(*s)) ++s; - if (!(off = scan_ip6(s, net->address))) + if (!(off = ip_scan(s, net->address))) return 0; s += off; if (*s != '/') @@ -433,7 +433,7 @@ static int scan_ip6_net(const char *src, ot_net *net) { if (!(off = scan_udec(s, &value, 128))) return 0; net->bits = (int)value; - if (ip6_isv4mapped(net->address)) + if (ip_is_v4mapped(net->address)) net->bits += 96; if (net->bits > 128) return 0; @@ -761,7 +761,7 @@ int main(int argc, char **argv) { scanon = 0; break; case 'i': - if (!scan_ip6(optarg, serverip)) { + if (!ip_scan(optarg, serverip)) { usage(argv[0]); exit(1); } diff --git a/ot_accesslist.c b/ot_accesslist.c index 9163ea9..2465ada 100644 --- a/ot_accesslist.c +++ b/ot_accesslist.c @@ -22,11 +22,11 @@ #endif /* Libowfat */ -#include "ip6.h" /* Opentracker */ #include "ot_accesslist.h" #include "ot_hex.h" +#include "ot_ip.h" #include "ot_vector.h" #include "trackerlogic.h" @@ -556,10 +556,10 @@ int accesslist_bless_net(ot_net *net, ot_permissions permissions) { { char _debug[512]; int off = snprintf(_debug, sizeof(_debug), "Blessing ip net "); - off += fmt_ip6c(_debug + off, net->address); + off += ip_format(_debug + off, net->address); if (net->bits < 128) { _debug[off++] = '/'; - if (ip6_isv4mapped(net->address)) + if (ip_is_v4mapped(net->address)) off += snprintf(_debug + off, sizeof(_debug) - off, "%d", net->bits - 96); else off += snprintf(_debug + off, sizeof(_debug) - off, "%d", net->bits); diff --git a/ot_http.c b/ot_http.c index 85bfcb6..dcf634f 100644 --- a/ot_http.c +++ b/ot_http.c @@ -16,13 +16,13 @@ /* Libowfat */ #include "iob.h" -#include "ip6.h" /* Opentracker */ #include "ot_accesslist.h" #include "ot_fullscrape.h" #include "ot_http.h" #include "ot_iovec.h" +#include "ot_ip.h" #include "ot_mutex.h" #include "ot_scan.h" #include "ot_stats.h" @@ -525,7 +525,7 @@ static ssize_t http_handle_announce(const int64 sock, struct ot_workstruct * if (accesslist_is_blessed(cookie->ip, OT_PERMISSION_MAY_PROXY)) { ot_ip6 proxied_ip; char *fwd = http_header(ws->request, ws->header_size, "x-forwarded-for"); - if (fwd && scan_ip6(fwd, proxied_ip)) { + if (fwd && ip_scan(fwd, proxied_ip)) { OT_SETIP(ws->peer, proxied_ip); } else OT_SETIP(ws->peer, cookie->ip); @@ -611,7 +611,7 @@ static ssize_t http_handle_announce(const int64 sock, struct ot_workstruct * char *tmp_buf1 = ws->reply, *tmp_buf2 = ws->reply + 16; len = scan_urlencoded_query(&read_ptr, tmp_buf2, SCAN_SEARCHPATH_VALUE); tmp_buf2[len] = 0; - if ((len <= 0) || !scan_ip6(tmp_buf2, tmp_buf1)) + if ((len <= 0) || !ip_scan(tmp_buf2, tmp_buf1)) HTTPERROR_400_PARAM; OT_SETIP(&ws->peer, tmp_buf1); } break; @@ -633,7 +633,7 @@ static ssize_t http_handle_announce(const int64 sock, struct ot_workstruct * loglist_reset(); return ws->reply_size = sprintf(ws->reply, "Successfully removed.\n"); } - parsed = scan_ip6(tmp_buf, net.address); + parsed = ip_scan(tmp_buf, net.address); if (!parsed) HTTPERROR_400_PARAM; if (tmp_buf[parsed++] != '/') @@ -643,7 +643,7 @@ static ssize_t http_handle_announce(const int64 sock, struct ot_workstruct * if (!parsed) HTTPERROR_400_PARAM; bits = (signed short)value; - if (ip6_isv4mapped(net.address)) + if (ip_is_v4mapped(net.address)) bits += 96; } net.bits = bits; diff --git a/ot_livesync.c b/ot_livesync.c index c4dc6e5..f6bb28b 100644 --- a/ot_livesync.c +++ b/ot_livesync.c @@ -13,11 +13,11 @@ #include /* Libowfat */ -#include "ip6.h" #include "socket.h" /* Opentracker */ #include "ot_accesslist.h" +#include "ot_ip.h" #include "ot_livesync.h" #include "ot_mutex.h" #include "ot_stats.h" @@ -92,7 +92,7 @@ void livesync_bind_mcast(ot_ip6 ip, uint16_t port) { char tmpip[4] = {0, 0, 0, 0}; char *v4ip; - if (!ip6_isv4mapped(ip)) + if (!ip_is_v4mapped(ip)) exerr("v6 mcast support not yet available."); v4ip = ip + 12; @@ -140,14 +140,14 @@ static void livesync_handle_peersync(struct ot_workstruct *ws, size_t peer_size) /* Prepare peer record for v4mapped expansion */ if (peer_size == OT_PEER_SIZE4) - memcpy(&ws->peer, V4mappedprefix, sizeof(V4mappedprefix)); + memcpy(&ws->peer, ip_v4mapped_prefix, sizeof(ip_v4mapped_prefix)); /* Now basic sanity checks have been done on the live sync packet We might add more testing and logging. */ while ((ssize_t)(off + sizeof(ot_hash) + peer_size) <= ws->request_size) { char *peer_ip = ws->request + off + sizeof(ot_hash); if (peer_size == OT_PEER_SIZE4) - memcpy(&ws->peer[0] + sizeof(V4mappedprefix), peer_ip, peer_size); + memcpy(&ws->peer[0] + sizeof(ip_v4mapped_prefix), peer_ip, peer_size); else memcpy(&ws->peer, peer_ip, peer_size); ws->hash = (ot_hash *)(ws->request + off); @@ -215,7 +215,7 @@ static void *livesync_worker(void *args) { ws.inbuf = ws.request = malloc(LIVESYNC_INCOMING_BUFFSIZE); ws.outbuf = ws.reply = 0; - memcpy(in_ip, V4mappedprefix, sizeof(V4mappedprefix)); + memcpy(in_ip, ip_v4mapped_prefix, sizeof(ip_v4mapped_prefix)); while (1) { uint32_t packet_type; diff --git a/ot_stats.c b/ot_stats.c index d43f29c..62c59c9 100644 --- a/ot_stats.c +++ b/ot_stats.c @@ -18,13 +18,12 @@ /* Libowfat */ #include "io.h" -#include "ip4.h" -#include "ip6.h" /* Opentracker */ #include "ot_accesslist.h" #include "ot_hex.h" #include "ot_iovec.h" +#include "ot_ip.h" #include "ot_mutex.h" #include "ot_stats.h" #include "trackerlogic.h" @@ -206,7 +205,7 @@ static size_t stats_return_busy_networks(char *reply, stats_network_node *tree, if (scores[i - 1]) { r += sprintf(r, "%08zd: ", scores[i - 1]); // #ifdef WANT_V6 - r += fmt_ip6c(r, networks[i - 1]); + r += ip_format(r, networks[i - 1]); #if 0 // XXX r += fmt_ip4( r, networks[i - 1]); @@ -470,7 +469,7 @@ static void stats_return_fulllog(size_t *iovec_entries, struct iovec **iovector, re = r + 32 * OT_STATS_TMPSIZE; } r += sprintf(r, "%08ld: ", loglist->time); - r += fmt_ip6c(r, loglist->ip); + r += ip_format(r, loglist->ip); *r++ = '\n'; memcpy(r, loglist->data, loglist->size); r += loglist->size; @@ -663,7 +662,7 @@ void stats_issue_event(ot_status_event event, PROTO_FLAG proto, uintptr_t event_ *peerid_hex = 0; } - ip_readable[fmt_ip6c(ip_readable, (char *)&ws->peer)] = 0; + ip_readable[ip_format(ip_readable, (char *)&ws->peer)] = 0; #if 0 /* XXX */ ip_readable[ fmt_ip4( ip_readable, (char*)&ws->peer ) ] = 0; @@ -687,7 +686,7 @@ void stats_issue_event(ot_status_event event, PROTO_FLAG proto, uintptr_t event_ ot_ip6 *ip = (ot_ip6 *)event_data; /* ugly hack to transfer ip to stats */ char _debug[512]; int off = snprintf(_debug, sizeof(_debug), "[%08d] scrp: ", (unsigned int)(g_now_seconds - ot_start_time) / 60); - off += fmt_ip6c(_debug + off, *ip); + off += ip_format(_debug + off, *ip); off += snprintf(_debug + off, sizeof(_debug) - off, " - FULL SCRAPE\n"); (void)write(2, _debug, off); ot_full_scrape_request_count++; @@ -696,7 +695,7 @@ void stats_issue_event(ot_status_event event, PROTO_FLAG proto, uintptr_t event_ ot_ip6 *ip = (ot_ip6 *)event_data; /* ugly hack to transfer ip to stats */ char _debug[512]; int off = snprintf(_debug, sizeof(_debug), "[%08d] scrp: ", (unsigned int)(g_now_seconds - ot_start_time) / 60); - off += fmt_ip6c(_debug + off, *ip); + off += ip_format(_debug + off, *ip); off += snprintf(_debug + off, sizeof(_debug) - off, " - FULL SCRAPE\n"); (void)write(2, _debug, off); ot_full_scrape_request_count++; diff --git a/ot_udp.c b/ot_udp.c index 82aa8bd..d17f3ff 100644 --- a/ot_udp.c +++ b/ot_udp.c @@ -12,10 +12,10 @@ /* Libowfat */ #include "io.h" -#include "ip6.h" #include "socket.h" /* Opentracker */ +#include "ot_ip.h" #include "ot_random.h" #include "ot_rijndael.h" #include "ot_stats.h" @@ -176,7 +176,7 @@ int handle_udp6(int64 serversocket, struct ot_workstruct *ws) { } else { /* Limit amount of peers to OT_MAX_PEERS_UDP */ uint32_t numwant = ntohl(inpacket[92 / 4]); - size_t max_peers = ip6_isv4mapped(remoteip) ? OT_MAX_PEERS_UDP4 : OT_MAX_PEERS_UDP6; + size_t max_peers = ip_is_v4mapped(remoteip) ? OT_MAX_PEERS_UDP4 : OT_MAX_PEERS_UDP6; if (numwant > max_peers) numwant = max_peers; diff --git a/proxy.c b/proxy.c index 19a084a..784e2a1 100644 --- a/proxy.c +++ b/proxy.c @@ -19,13 +19,13 @@ /* Libowfat */ #include "io.h" #include "iob.h" -#include "ip6.h" #include "socket.h" /* Opentracker */ +#include "ot_ip.h" #include "ot_mutex.h" -#include "ot_scan.h" #include "ot_random.h" +#include "ot_scan.h" #include "ot_stats.h" #include "ot_vector.h" #include "trackerlogic.h" @@ -92,7 +92,7 @@ void livesync_bind_mcast(ot_ip6 ip, uint16_t port) { char tmpip[4] = {0, 0, 0, 0}; char *v4ip; - if (!ip6_isv4mapped(ip)) + if (!ip_is_v4mapped(ip)) exerr("v6 mcast support not yet available."); v4ip = ip + 12; @@ -539,14 +539,14 @@ static int scan_ip6_port(const char *src, ot_ip6 ip, uint16_t *port) { ++s; if (*s == '[') ++s, ++bracket; /* for v6 style notation */ - if (!(off = scan_ip6(s, ip))) + if (!(off = ip_scan(s, ip))) return 0; s += off; if (*s == 0 || isspace(*s)) return s - src; if (*s == ']' && bracket) ++s; - if (!ip6_isv4mapped(ip)) { + if (!ip_is_v4mapped(ip)) { if ((bracket && *(s) != ':') || (*(s) != '.')) return 0; s++; diff --git a/tests/runner.c b/tests/runner.c index 25456bc..10b6625 100644 --- a/tests/runner.c +++ b/tests/runner.c @@ -21,6 +21,7 @@ static const struct module modules[] = {{"scan_urlencoded_query", test_scan_urle {"ot_hex", test_hex}, {"ot_scan", test_scan}, {"ot_buf", test_buf}, + {"ot_ip", test_ip}, {"ot_accesslist", test_accesslist}, {NULL, NULL}}; diff --git a/tests/test.h b/tests/test.h index 676ca33..7449b73 100644 --- a/tests/test.h +++ b/tests/test.h @@ -84,5 +84,6 @@ void test_hex(void); void test_accesslist(void); void test_scan(void); void test_buf(void); +void test_ip(void); #endif diff --git a/trackerlogic.c b/trackerlogic.c index d8a2f9b..f5e3dc0 100644 --- a/trackerlogic.c +++ b/trackerlogic.c @@ -13,19 +13,19 @@ /* Libowfat */ #include "io.h" #include "iob.h" -#include "ip6.h" /* Opentracker */ #include "ot_accesslist.h" #include "ot_clean.h" #include "ot_fullscrape.h" #include "ot_http.h" +#include "ot_ip.h" #include "ot_livesync.h" #include "ot_mutex.h" #include "ot_random.h" #include "ot_stats.h" -#include "ot_vector.h" #include "ot_udp.h" +#include "ot_vector.h" #include "trackerlogic.h" /* Forward declaration */ @@ -511,7 +511,7 @@ void iterate_all_torrents(int (*for_each)(ot_torrent *torrent, uintptr_t data), ot_peer *peer_from_peer6(ot_peer6 *peer, size_t *peer_size) { ot_ip6 *ip = (ot_ip6 *)peer; - if (!ip6_isv4mapped(ip)) { + if (!ip_is_v4mapped(ip)) { *peer_size = OT_PEER_SIZE6; return (ot_peer *)peer; } @@ -521,7 +521,7 @@ ot_peer *peer_from_peer6(ot_peer6 *peer, size_t *peer_size) { size_t peer_size_from_peer6(ot_peer6 *peer) { ot_ip6 *ip = (ot_ip6 *)peer; - if (!ip6_isv4mapped(ip)) + if (!ip_is_v4mapped(ip)) return OT_PEER_SIZE6; return OT_PEER_SIZE4; } -- cgit v1.3