From 5d2033ac8932d223a00d7d6e29cedd9b2e23bc7f Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Fri, 3 Apr 2026 02:56:06 +0200 Subject: Clean up top100 stats code --- ot_http.c | 8 ++++---- ot_stats.c | 56 ++++++++++++++++++++++++++------------------------------ 2 files changed, 30 insertions(+), 34 deletions(-) diff --git a/ot_http.c b/ot_http.c index a789f0d..e4486fc 100644 --- a/ot_http.c +++ b/ot_http.c @@ -581,11 +581,11 @@ static ssize_t http_handle_announce(const int64 sock, struct ot_workstruct * len = scan_urlencoded_query(&read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE); if ((len <= 0) || scan_fixed_int(write_ptr, len, &tmp)) HTTPERROR_400_PARAM; + if (tmp < 0) + tmp = 50; + if (tmp > 200) + tmp = 200; numwant = tmp; - if (numwant < 0) - numwant = 50; - if (numwant > 200) - numwant = 200; break; case 5: /* matched "compact" */ len = scan_urlencoded_query(&read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE); diff --git a/ot_stats.c b/ot_stats.c index 3d64bce..a9e745c 100644 --- a/ot_stats.c +++ b/ot_stats.c @@ -190,7 +190,7 @@ static size_t stats_get_highscore_networks(stats_network_node *node, int depth, return score; } -static size_t stats_return_busy_networks(char *reply, stats_network_node *tree, int amount, int limit) { +static size_t stats_return_busy_networks(char *reply, stats_network_node *tree, size_t amount, int limit) { ot_ip6 networks[amount]; ot_ip6 node_value; size_t scores[amount]; @@ -204,14 +204,14 @@ static size_t stats_return_busy_networks(char *reply, stats_network_node *tree, stats_get_highscore_networks(tree, 0, node_value, scores, networks, amount, limit); r += sprintf(r, "Networks, limit /%d:\n", limit + STATS_NETWORK_NODE_BITWIDTH); - for (i = amount - 1; i >= 0; --i) { - if (scores[i]) { - r += sprintf(r, "%08zd: ", scores[i]); + for (i = amount; i > 0; --i) { + if (scores[i - 1]) { + r += sprintf(r, "%08zd: ", scores[i - 1]); // #ifdef WANT_V6 - r += fmt_ip6c(r, networks[i]); + r += fmt_ip6c(r, networks[i - 1]); #if 0 // XXX - r += fmt_ip4( r, networks[i]); + r += fmt_ip4( r, networks[i - 1]); #endif *r++ = '\n'; } @@ -315,6 +315,21 @@ typedef struct { ot_hash hash; } ot_record; +static inline void stats_insert_into_highscore(size_t val, ot_record* list, size_t amount, ot_hash* hash) { + size_t i; + for (i = 0; (i < amount) && (list[i].val > val); ++i) + {} + /* if we score better than a record in the list, we need to make room in the high + score array by moving lower scoring entries up */ + if (i < amount) { + memmove(list + i + 1, list + i, (amount - i - 1) * sizeof(ot_record)); + + memcpy(&list[i].hash, hash, sizeof(ot_hash)); + list[i].val = val; + } +} + + /* Fetches stats from tracker */ size_t stats_top_txt(char *reply, size_t amount) { size_t j, idx, bucket; @@ -328,6 +343,7 @@ size_t stats_top_txt(char *reply, size_t amount) { byte_zero(top100c, sizeof(top100c)); byte_zero(top100l, sizeof(top100l)); + /* Iterate over complete torrent list */ for (bucket = 0; bucket < OT_BUCKET_COUNT; ++bucket) { ot_vector *torrents_list = mutex_bucket_lock(bucket); for (j = 0; j < torrents_list->size; ++j) { @@ -335,30 +351,10 @@ size_t stats_top_txt(char *reply, size_t amount) { size_t peer_count = torrent->peer_list6->peer_count + torrent->peer_list4->peer_count; size_t seed_count = torrent->peer_list6->seed_count + torrent->peer_list4->seed_count; size_t leech_count = peer_count - seed_count; - idx = amount - 1; - while ((idx >= 0) && (peer_count > top100c[idx].val)) - --idx; - if (idx++ != amount - 1) { - memmove(top100c + idx + 1, top100c + idx, (amount - 1 - idx) * sizeof(ot_record)); - memcpy(&top100c[idx].hash, &torrent->hash, sizeof(ot_hash)); - top100c[idx].val = peer_count; - } - idx = amount - 1; - while ((idx >= 0) && (seed_count > top100s[idx].val)) - --idx; - if (idx++ != amount - 1) { - memmove(top100s + idx + 1, top100s + idx, (amount - 1 - idx) * sizeof(ot_record)); - memcpy(&top100s[idx].hash, &torrent->hash, sizeof(ot_hash)); - top100s[idx].val = seed_count; - } - idx = amount - 1; - while ((idx >= 0) && (leech_count > top100l[idx].val)) - --idx; - if (idx++ != amount - 1) { - memmove(top100l + idx + 1, top100l + idx, (amount - 1 - idx) * sizeof(ot_record)); - memcpy(&top100l[idx].hash, &torrent->hash, sizeof(ot_hash)); - top100l[idx].val = leech_count; - } + + stats_insert_into_highscore(peer_count, top100c, amount, &torrent->hash); + stats_insert_into_highscore(seed_count, top100s, amount, &torrent->hash); + stats_insert_into_highscore(leech_count, top100l, amount, &torrent->hash); } mutex_bucket_unlock(bucket, 0); if (!g_opentracker_running) -- cgit v1.2.3