diff options
| author | Dirk Engling <erdgeist@erdgeist.org> | 2026-10-24 02:04:33 +0200 |
|---|---|---|
| committer | Dirk Engling <erdgeist@erdgeist.org> | 2026-10-24 02:04:33 +0200 |
| commit | 168285b4569f862443419a2bffe221e1a8ab8150 (patch) | |
| tree | 27ef578cd4af2e6860bcbc9c0dcb7af16d80b4ec | |
| parent | 0eb6054309f23b3cec7cb799485b859fcf61f794 (diff) | |
Replace all manual hex conversion with the new helper
| -rw-r--r-- | opentracker.c | 11 | ||||
| -rw-r--r-- | ot_accesslist.c | 26 | ||||
| -rw-r--r-- | ot_fullscrape.c | 19 | ||||
| -rw-r--r-- | ot_stats.c | 23 |
4 files changed, 17 insertions, 62 deletions
diff --git a/opentracker.c b/opentracker.c index 4d44950..f218589 100644 --- a/opentracker.c +++ b/opentracker.c | |||
| @@ -28,6 +28,7 @@ | |||
| 28 | 28 | ||
| 29 | /* Opentracker */ | 29 | /* Opentracker */ |
| 30 | #include "ot_accesslist.h" | 30 | #include "ot_accesslist.h" |
| 31 | #include "ot_hex.h" | ||
| 31 | #include "ot_http.h" | 32 | #include "ot_http.h" |
| 32 | #include "ot_livesync.h" | 33 | #include "ot_livesync.h" |
| 33 | #include "ot_mutex.h" | 34 | #include "ot_mutex.h" |
| @@ -646,16 +647,10 @@ void load_state(const char *const state_filename) { | |||
| 646 | /* We do ignore anything that is not of the form "^[:xdigit:]:\d+:\d+" */ | 647 | /* We do ignore anything that is not of the form "^[:xdigit:]:\d+:\d+" */ |
| 647 | while (fgets(inbuf, sizeof(inbuf), state_filehandle)) { | 648 | while (fgets(inbuf, sizeof(inbuf), state_filehandle)) { |
| 648 | size_t i; | 649 | size_t i; |
| 649 | for (i = 0; i < sizeof(ot_hash); ++i) { | ||
| 650 | int eger = 16 * scan_fromhex(inbuf[2 * i]) + scan_fromhex(inbuf[1 + 2 * i]); | ||
| 651 | if (eger < 0) | ||
| 652 | continue; | ||
| 653 | infohash[i] = eger; | ||
| 654 | } | ||
| 655 | 650 | ||
| 656 | if (i != sizeof(ot_hash)) | 651 | if (!hex_tohash(infohash, inbuf)) |
| 657 | continue; | 652 | continue; |
| 658 | i *= 2; | 653 | i = 2 * sizeof(ot_hash); |
| 659 | 654 | ||
| 660 | if (inbuf[i++] != ':' || !(consumed = scan_ulonglong(inbuf + i, &base))) | 655 | if (inbuf[i++] != ':' || !(consumed = scan_ulonglong(inbuf + i, &base))) |
| 661 | continue; | 656 | continue; |
diff --git a/ot_accesslist.c b/ot_accesslist.c index 2827494..ee6adb2 100644 --- a/ot_accesslist.c +++ b/ot_accesslist.c | |||
| @@ -20,10 +20,10 @@ | |||
| 20 | #include "fmt.h" | 20 | #include "fmt.h" |
| 21 | #include "ip6.h" | 21 | #include "ip6.h" |
| 22 | #include "mmap.h" | 22 | #include "mmap.h" |
| 23 | #include "scan.h" | ||
| 24 | 23 | ||
| 25 | /* Opentracker */ | 24 | /* Opentracker */ |
| 26 | #include "ot_accesslist.h" | 25 | #include "ot_accesslist.h" |
| 26 | #include "ot_hex.h" | ||
| 27 | #include "ot_vector.h" | 27 | #include "ot_vector.h" |
| 28 | #include "trackerlogic.h" | 28 | #include "trackerlogic.h" |
| 29 | 29 | ||
| @@ -128,20 +128,11 @@ static void accesslist_readfile(void) { | |||
| 128 | 128 | ||
| 129 | /* We do ignore anything that is not of the form "^[:xdigit:]{40}[^:xdigit:].*" */ | 129 | /* We do ignore anything that is not of the form "^[:xdigit:]{40}[^:xdigit:].*" */ |
| 130 | while (read_offs <= map_end) { | 130 | while (read_offs <= map_end) { |
| 131 | size_t i; | 131 | if (hex_tohash(*info_hash, read_offs)) { |
| 132 | for (i = 0; i < sizeof(ot_hash); ++i) { | ||
| 133 | int eger1 = scan_fromhex((unsigned char)read_offs[2 * i]); | ||
| 134 | int eger2 = scan_fromhex((unsigned char)read_offs[1 + 2 * i]); | ||
| 135 | if (eger1 < 0 || eger2 < 0) | ||
| 136 | break; | ||
| 137 | (*info_hash)[i] = (uint8_t)(eger1 * 16 + eger2); | ||
| 138 | } | ||
| 139 | |||
| 140 | if (i == sizeof(ot_hash)) { | ||
| 141 | read_offs += 40; | 132 | read_offs += 40; |
| 142 | 133 | ||
| 143 | /* Append accesslist to accesslist vector */ | 134 | /* Append accesslist to accesslist vector */ |
| 144 | if (read_offs == map_end || scan_fromhex((unsigned char)*read_offs) < 0) | 135 | if (read_offs == map_end || !hex_isdigit((unsigned char)*read_offs)) |
| 145 | ++info_hash; | 136 | ++info_hash; |
| 146 | } | 137 | } |
| 147 | 138 | ||
| @@ -261,7 +252,6 @@ static void *accesslist_adddel_worker(char *fifoname, ot_accesslist *_Atomic | |||
| 261 | 252 | ||
| 262 | while ((linelen = getline(&line, &linecap, fifo)) > 0) { | 253 | while ((linelen = getline(&line, &linecap, fifo)) > 0) { |
| 263 | ot_hash info_hash; | 254 | ot_hash info_hash; |
| 264 | size_t i; | ||
| 265 | 255 | ||
| 266 | printf("Got line %*s", (int)linelen, line); | 256 | printf("Got line %*s", (int)linelen, line); |
| 267 | /* We do ignore anything that is not of the form "^[:xdigit:]{40}[^:xdigit:].*" | 257 | /* We do ignore anything that is not of the form "^[:xdigit:]{40}[^:xdigit:].*" |
| @@ -269,15 +259,7 @@ static void *accesslist_adddel_worker(char *fifoname, ot_accesslist *_Atomic | |||
| 269 | if (linelen < 41) | 259 | if (linelen < 41) |
| 270 | continue; | 260 | continue; |
| 271 | 261 | ||
| 272 | for (i = 0; i < sizeof(ot_hash); ++i) { | 262 | if (!hex_tohash(info_hash, line)) |
| 273 | int eger1 = scan_fromhex((unsigned char)line[2 * i]); | ||
| 274 | int eger2 = scan_fromhex((unsigned char)line[1 + 2 * i]); | ||
| 275 | if (eger1 < 0 || eger2 < 0) | ||
| 276 | break; | ||
| 277 | ((uint8_t *)info_hash)[i] = (uint8_t)(eger1 * 16 + eger2); | ||
| 278 | } | ||
| 279 | printf("parsed info_hash %20s\n", info_hash); | ||
| 280 | if (i != sizeof(ot_hash)) | ||
| 281 | continue; | 263 | continue; |
| 282 | 264 | ||
| 283 | /* From now on we modify g_accesslist_add and g_accesslist_delete, so prevent the | 265 | /* From now on we modify g_accesslist_add and g_accesslist_delete, so prevent the |
diff --git a/ot_fullscrape.c b/ot_fullscrape.c index 3e36e8e..08970a6 100644 --- a/ot_fullscrape.c +++ b/ot_fullscrape.c | |||
| @@ -23,6 +23,7 @@ | |||
| 23 | #include "textcode.h" | 23 | #include "textcode.h" |
| 24 | 24 | ||
| 25 | /* Opentracker */ | 25 | /* Opentracker */ |
| 26 | #include "ot_hex.h" | ||
| 26 | #include "ot_fullscrape.h" | 27 | #include "ot_fullscrape.h" |
| 27 | #include "ot_iovec.h" | 28 | #include "ot_iovec.h" |
| 28 | #include "ot_mutex.h" | 29 | #include "ot_mutex.h" |
| @@ -48,18 +49,6 @@ static void fullscrape_make_zstd(int taskid, ot_tasktype mode); | |||
| 48 | 49 | ||
| 49 | /* Converter function from memory to human readable hex strings | 50 | /* Converter function from memory to human readable hex strings |
| 50 | XXX - Duplicated from ot_stats. Needs fix. */ | 51 | XXX - Duplicated from ot_stats. Needs fix. */ |
| 51 | static char *to_hex(char *d, uint8_t *s) { | ||
| 52 | char *m = "0123456789ABCDEF"; | ||
| 53 | char *t = d; | ||
| 54 | char *e = d + 40; | ||
| 55 | while (d < e) { | ||
| 56 | *d++ = m[*s >> 4]; | ||
| 57 | *d++ = m[*s++ & 15]; | ||
| 58 | } | ||
| 59 | *d = 0; | ||
| 60 | return t; | ||
| 61 | } | ||
| 62 | |||
| 63 | /* This is the entry point into this worker thread | 52 | /* This is the entry point into this worker thread |
| 64 | It grabs tasks from mutex_tasklist and delivers results back | 53 | It grabs tasks from mutex_tasklist and delivers results back |
| 65 | */ | 54 | */ |
| @@ -117,12 +106,12 @@ static char * fullscrape_write_one( ot_tasktype mode, char *r, ot_torrent *torre | |||
| 117 | 106 | ||
| 118 | break; | 107 | break; |
| 119 | case TASK_FULLSCRAPE_TPB_ASCII: | 108 | case TASK_FULLSCRAPE_TPB_ASCII: |
| 120 | to_hex(r, *hash); | 109 | hex_fromhash(r, *hash); |
| 121 | r += 2 * sizeof(ot_hash); | 110 | r += 2 * sizeof(ot_hash); |
| 122 | r += sprintf(r, ":%zd:%zd\n", seed_count, peer_count - seed_count); | 111 | r += sprintf(r, ":%zd:%zd\n", seed_count, peer_count - seed_count); |
| 123 | break; | 112 | break; |
| 124 | case TASK_FULLSCRAPE_TPB_ASCII_PLUS: | 113 | case TASK_FULLSCRAPE_TPB_ASCII_PLUS: |
| 125 | to_hex(r, *hash); | 114 | hex_fromhash(r, *hash); |
| 126 | r += 2 * sizeof(ot_hash); | 115 | r += 2 * sizeof(ot_hash); |
| 127 | r += sprintf(r, ":%zd:%zd:%zd\n", seed_count, peer_count - seed_count, down_count); | 116 | r += sprintf(r, ":%zd:%zd:%zd\n", seed_count, peer_count - seed_count, down_count); |
| 128 | break; | 117 | break; |
| @@ -138,7 +127,7 @@ static char * fullscrape_write_one( ot_tasktype mode, char *r, ot_torrent *torre | |||
| 138 | r += sprintf(r, ":%zd:%zd\n", seed_count, peer_count - seed_count); | 127 | r += sprintf(r, ":%zd:%zd\n", seed_count, peer_count - seed_count); |
| 139 | break; | 128 | break; |
| 140 | case TASK_FULLSCRAPE_TRACKERSTATE: | 129 | case TASK_FULLSCRAPE_TRACKERSTATE: |
| 141 | to_hex(r, *hash); | 130 | hex_fromhash(r, *hash); |
| 142 | r += 2 * sizeof(ot_hash); | 131 | r += 2 * sizeof(ot_hash); |
| 143 | r += sprintf(r, ":%zd:%zd\n", torrent->peer_list6->base, down_count); | 132 | r += sprintf(r, ":%zd:%zd\n", torrent->peer_list6->base, down_count); |
| 144 | break; | 133 | break; |
| @@ -23,6 +23,7 @@ | |||
| 23 | 23 | ||
| 24 | /* Opentracker */ | 24 | /* Opentracker */ |
| 25 | #include "ot_accesslist.h" | 25 | #include "ot_accesslist.h" |
| 26 | #include "ot_hex.h" | ||
| 26 | #include "ot_iovec.h" | 27 | #include "ot_iovec.h" |
| 27 | #include "ot_mutex.h" | 28 | #include "ot_mutex.h" |
| 28 | #include "ot_stats.h" | 29 | #include "ot_stats.h" |
| @@ -295,18 +296,6 @@ static int torrent_statter(ot_torrent *torrent, uintptr_t data) { | |||
| 295 | } | 296 | } |
| 296 | 297 | ||
| 297 | /* Converter function from memory to human readable hex strings */ | 298 | /* Converter function from memory to human readable hex strings */ |
| 298 | static char *to_hex(char *d, uint8_t *s) { | ||
| 299 | char *m = "0123456789ABCDEF"; | ||
| 300 | char *t = d; | ||
| 301 | char *e = d + 40; | ||
| 302 | while (d < e) { | ||
| 303 | *d++ = m[*s >> 4]; | ||
| 304 | *d++ = m[*s++ & 15]; | ||
| 305 | } | ||
| 306 | *d = 0; | ||
| 307 | return t; | ||
| 308 | } | ||
| 309 | |||
| 310 | typedef struct { | 299 | typedef struct { |
| 311 | size_t val; | 300 | size_t val; |
| 312 | ot_hash hash; | 301 | ot_hash hash; |
| @@ -361,15 +350,15 @@ size_t stats_top_txt(char *reply, size_t amount) { | |||
| 361 | r += sprintf(r, "Top %zd torrents by peers:\n", amount); | 350 | r += sprintf(r, "Top %zd torrents by peers:\n", amount); |
| 362 | for (idx = 0; idx < amount; ++idx) | 351 | for (idx = 0; idx < amount; ++idx) |
| 363 | if (top100c[idx].val) | 352 | if (top100c[idx].val) |
| 364 | r += sprintf(r, "\t%zd\t%s\n", top100c[idx].val, to_hex(hex_out, top100c[idx].hash)); | 353 | r += sprintf(r, "\t%zd\t%s\n", top100c[idx].val, hex_fromhash(hex_out, top100c[idx].hash)); |
| 365 | r += sprintf(r, "Top %zd torrents by seeds:\n", amount); | 354 | r += sprintf(r, "Top %zd torrents by seeds:\n", amount); |
| 366 | for (idx = 0; idx < amount; ++idx) | 355 | for (idx = 0; idx < amount; ++idx) |
| 367 | if (top100s[idx].val) | 356 | if (top100s[idx].val) |
| 368 | r += sprintf(r, "\t%zd\t%s\n", top100s[idx].val, to_hex(hex_out, top100s[idx].hash)); | 357 | r += sprintf(r, "\t%zd\t%s\n", top100s[idx].val, hex_fromhash(hex_out, top100s[idx].hash)); |
| 369 | r += sprintf(r, "Top %zd torrents by leechers:\n", amount); | 358 | r += sprintf(r, "Top %zd torrents by leechers:\n", amount); |
| 370 | for (idx = 0; idx < amount; ++idx) | 359 | for (idx = 0; idx < amount; ++idx) |
| 371 | if (top100l[idx].val) | 360 | if (top100l[idx].val) |
| 372 | r += sprintf(r, "\t%zd\t%s\n", top100l[idx].val, to_hex(hex_out, top100l[idx].hash)); | 361 | r += sprintf(r, "\t%zd\t%s\n", top100l[idx].val, hex_fromhash(hex_out, top100l[idx].hash)); |
| 373 | 362 | ||
| 374 | return r - reply; | 363 | return r - reply; |
| 375 | } | 364 | } |
| @@ -667,9 +656,9 @@ void stats_issue_event(ot_status_event event, PROTO_FLAG proto, uintptr_t event_ | |||
| 667 | localtime_r(&ttt, &time_now); | 656 | localtime_r(&ttt, &time_now); |
| 668 | strftime(timestring, sizeof(timestring), "%FT%T%z", &time_now); | 657 | strftime(timestring, sizeof(timestring), "%FT%T%z", &time_now); |
| 669 | 658 | ||
| 670 | to_hex(hash_hex, *ws->hash); | 659 | hex_fromhash(hash_hex, *ws->hash); |
| 671 | if (ws->peer_id) | 660 | if (ws->peer_id) |
| 672 | to_hex(peerid_hex, (uint8_t *)ws->peer_id); | 661 | hex_fromhash(peerid_hex, (uint8_t *)ws->peer_id); |
| 673 | else { | 662 | else { |
| 674 | *peerid_hex = 0; | 663 | *peerid_hex = 0; |
| 675 | } | 664 | } |
