From 6871825565d0ba24436dbd5b92cc09e02f93af6a Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Mon, 26 Oct 2026 16:59:39 +0100 Subject: First run of making variable names more readable --- ot_accesslist.c | 30 +++++++++++++++--------------- ot_accesslist.h | 6 +++--- ot_config.c | 2 +- ot_fullscrape.c | 2 +- ot_vector.c | 10 +++++----- ot_vector.h | 2 +- proxy.c | 2 +- scan_urlencoded_query.c | 2 +- scan_urlencoded_query.h | 2 +- tests/t_scan_urlencoded_query.c | 12 ++++++------ tests/t_vector.c | 16 ++++++++-------- trackerlogic.c | 6 +++--- 12 files changed, 46 insertions(+), 46 deletions(-) diff --git a/ot_accesslist.c b/ot_accesslist.c index d7e88ee..ef845d6 100644 --- a/ot_accesslist.c +++ b/ot_accesslist.c @@ -339,7 +339,7 @@ static void *accesslist_adddel_worker(const char *fifoname, ot_accesslist *_ } } else { int exactmatch = 0; - ot_hash *insert_point = binary_search(info_hash, (*adding_to)->list, (*adding_to)->size, sizeof(ot_hash), OT_HASH_COMPARE_SIZE, &exactmatch); + ot_hash *insert_point = vector_binary_search(info_hash, (*adding_to)->list, (*adding_to)->size, sizeof(ot_hash), OT_HASH_COMPARE_SIZE, &exactmatch); /* Only if the info hash is not in the adding_to list, create a new head with that entry */ if (!exactmatch) { @@ -416,7 +416,7 @@ void accesslist_cleanup(void) { } #endif -int address_in_net(const ot_ip6 address, const ot_net *net) { +int accesslist_address_in_net(const ot_ip6 address, const ot_net *net) { int bits = net->bits, checkbits = (0x7f00 >> (bits & 7)); int result = memcmp(address, &net->address, bits >> 3); if (!result && (bits & 7)) @@ -424,7 +424,7 @@ int address_in_net(const ot_ip6 address, const ot_net *net) { return result == 0; } -void *set_value_for_net(const ot_net *net, ot_vector *vector, const void *value, const size_t member_size) { +void *accesslist_set_value_for_net(const ot_net *net, ot_vector *vector, const void *value, const size_t member_size) { size_t i; int exactmatch; @@ -435,7 +435,7 @@ void *set_value_for_net(const ot_net *net, ot_vector *vector, const void *value, /* Check each net in vector for overlap */ uint8_t *member = ((uint8_t *)vector->data); for (i = 0; i < vector->size; ++i) { - if (address_in_net(*(ot_ip6 *)member, net) || address_in_net(net->address, (ot_net *)member)) + if (accesslist_address_in_net(*(ot_ip6 *)member, net) || accesslist_address_in_net(net->address, (ot_net *)member)) return 0; member += member_size; } @@ -451,16 +451,16 @@ void *set_value_for_net(const ot_net *net, ot_vector *vector, const void *value, /* Takes a vector filled with { ot_net net, uint8_t[x] value }; Returns value associated with the net, or NULL if not found */ -void *get_value_for_net(const ot_ip6 address, const ot_vector *vector, const size_t member_size) { +void *accesslist_get_value_for_net(const ot_ip6 address, const ot_vector *vector, const size_t member_size) { int exactmatch; /* This binary search will return a pointer to the first non-containing network... */ - ot_net *net = binary_search(address, vector->data, vector->size, member_size, sizeof(ot_ip6), &exactmatch); + ot_net *net = vector_binary_search(address, vector->data, vector->size, member_size, sizeof(ot_ip6), &exactmatch); if (!net) return NULL; /* ... so we'll need to move back one step unless we've exactly hit the first address in network */ if (!exactmatch && ((void *)net > vector->data)) --net; - if (!address_in_net(address, net)) + if (!accesslist_address_in_net(address, net)) return NULL; return (void *)net; } @@ -472,7 +472,7 @@ static pthread_mutex_t g_lognets_list_mutex = PTHREAD_MUTEX_INITIALIZER; void loglist_add_network(const ot_net *net) { pthread_mutex_lock(&g_lognets_list_mutex); - set_value_for_net(net, &g_lognets_list, NULL, sizeof(ot_net)); + accesslist_set_value_for_net(net, &g_lognets_list, NULL, sizeof(ot_net)); pthread_mutex_unlock(&g_lognets_list_mutex); } @@ -487,7 +487,7 @@ void loglist_reset(void) { int loglist_check_address(const ot_ip6 address) { int result; pthread_mutex_lock(&g_lognets_list_mutex); - result = (NULL != get_value_for_net(address, &g_lognets_list, sizeof(ot_net))); + result = (NULL != accesslist_get_value_for_net(address, &g_lognets_list, sizeof(ot_net))); pthread_mutex_unlock(&g_lognets_list_mutex); return result; } @@ -508,17 +508,17 @@ int proxylist_add_network(const ot_net *proxy, const ot_net *net) { pthread_mutex_lock(&g_proxies_list_mutex); /* If we have a direct hit, use and extend the vector there */ - map = binary_search(proxy, g_proxies_list.data, g_proxies_list.size, sizeof(ot_proxymap), sizeof(ot_net), &exactmatch); + map = vector_binary_search(proxy, g_proxies_list.data, g_proxies_list.size, sizeof(ot_proxymap), sizeof(ot_net), &exactmatch); if (!map || !exactmatch) { /* else see, if we've got overlapping networks and get a new empty vector if not */ ot_vector empty; memset(&empty, 0, sizeof(ot_vector)); - map = set_value_for_net(proxy, &g_proxies_list, &empty, sizeof(ot_proxymap)); + map = accesslist_set_value_for_net(proxy, &g_proxies_list, &empty, sizeof(ot_proxymap)); } - if (map && set_value_for_net(net, &map->networks, NULL, sizeof(ot_net))) + if (map && accesslist_set_value_for_net(net, &map->networks, NULL, sizeof(ot_net))) result = 1; pthread_mutex_unlock(&g_proxies_list_mutex); @@ -531,8 +531,8 @@ int proxylist_check_proxy(const ot_ip6 proxy, const ot_ip6 address) { pthread_mutex_lock(&g_proxies_list_mutex); - if ((map = get_value_for_net(proxy, &g_proxies_list, sizeof(ot_proxymap)))) - if (!address || get_value_for_net(address, &map->networks, sizeof(ot_net))) + if ((map = accesslist_get_value_for_net(proxy, &g_proxies_list, sizeof(ot_proxymap)))) + if (!address || accesslist_get_value_for_net(address, &map->networks, sizeof(ot_net))) result = 1; pthread_mutex_unlock(&g_proxies_list_mutex); @@ -587,7 +587,7 @@ int accesslist_bless_net(ot_net *net, ot_permissions permissions) { int accesslist_is_blessed(ot_ip6 ip, ot_permissions permissions) { size_t i; for (i = 0; i < g_admin_nets_count; ++i) - if (address_in_net(ip, g_admin_nets + i) && (g_admin_nets_permissions[i] & permissions)) + if (accesslist_address_in_net(ip, g_admin_nets + i) && (g_admin_nets_permissions[i] & permissions)) return 1; return 0; } diff --git a/ot_accesslist.h b/ot_accesslist.h index 9dfdd95..0a8bf4c 100644 --- a/ot_accesslist.h +++ b/ot_accesslist.h @@ -40,7 +40,7 @@ extern const char *g_accesslist_pipe_delete; #endif /* Test if an address is subset of an ot_net, return value is considered a bool */ -int address_in_net(const ot_ip6 address, const ot_net *net); +int accesslist_address_in_net(const ot_ip6 address, const ot_net *net); /* Store a value into a vector of struct { ot_net net, uint8_t[x] value } member; returns NULL @@ -51,13 +51,13 @@ int address_in_net(const ot_ip6 address, const ot_net *net); returns pointer to new member in vector for success member_size can be sizeof(ot_net) to reduce the lookup to a boolean mapping */ -void *set_value_for_net(const ot_net *net, ot_vector *vector, const void *value, const size_t member_size); +void *accesslist_set_value_for_net(const ot_net *net, ot_vector *vector, const void *value, const size_t member_size); /* Takes a vector filled with struct { ot_net net, uint8_t[x] value } member; Returns pointer to _member_ associated with the net, or NULL if not found member_size can be sizeof(ot_net) to reduce the lookup to a boolean mapping */ -void *get_value_for_net(const ot_ip6 address, const ot_vector *vector, const size_t member_size); +void *accesslist_get_value_for_net(const ot_ip6 address, const ot_vector *vector, const size_t member_size); #ifdef WANT_IP_FROM_PROXY int proxylist_add_network(const ot_net *proxy, const ot_net *net); diff --git a/ot_config.c b/ot_config.c index e9666f9..a5fc06e 100644 --- a/ot_config.c +++ b/ot_config.c @@ -264,7 +264,7 @@ static config_kind parse_config_key(char **p) { return CONFIG_UNKNOWN; } -int parse_configfile(char *config_filename) { +static int parse_configfile(char *config_filename) { FILE *config_file_handle; char inbuf[512]; ot_ip6 tmpip; diff --git a/ot_fullscrape.c b/ot_fullscrape.c index eade79d..788e574 100644 --- a/ot_fullscrape.c +++ b/ot_fullscrape.c @@ -122,7 +122,7 @@ static char * fullscrape_write_one( ot_tasktype mode, char *r, ot_torrent *torre r += 8; break; case TASK_FULLSCRAPE_TPB_URLENCODED: - r += fmt_urlencoded(r, *hash, sizeof(ot_hash)); + r += urlencoded_format(r, *hash, sizeof(ot_hash)); r += sprintf(r, ":%zd:%zd\n", seed_count, peer_count - seed_count); break; case TASK_FULLSCRAPE_TRACKERSTATE: diff --git a/ot_vector.c b/ot_vector.c index 7f18246..924d468 100644 --- a/ot_vector.c +++ b/ot_vector.c @@ -19,7 +19,7 @@ static int vector_compare_peer4(const void *peer1, const void *peer2) { return m no exact match is found. In that case it sets exactmatch 0 and gives calling functions the chance to insert data */ -void *binary_search(const void *const key, const void *base, const size_t member_count, const size_t member_size, size_t compare_size, int *exactmatch) { +void *vector_binary_search(const void *const key, const void *base, const size_t member_count, const size_t member_size, size_t compare_size, int *exactmatch) { size_t interval = member_count; while (interval) { @@ -50,13 +50,13 @@ static uint8_t vector_hash_peer(ot_peer const *peer, size_t compare_size, size_t /* This is the generic insert operation for our vector type. It tries to locate the object at "key" with size "member_size" by comparing its first "compare_size" bytes with - those of objects in vector. Our special "binary_search" function does that and either returns the match or a + those of objects in vector. Our special "vector_binary_search" function does that and either returns the match or a pointer to where the object is to be inserted. vector_find_or_insert makes space for the object and copies it, if it wasn't found in vector. Caller needs to check the passed "exactmatch" variable to see, whether an insert took place. If resizing the vector failed, NULL is returned, else the pointer to the object in vector. */ void *vector_find_or_insert(ot_vector *vector, void *key, size_t member_size, size_t compare_size, int *exactmatch) { - uint8_t *match = binary_search(key, vector->data, vector->size, member_size, compare_size, exactmatch); + uint8_t *match = vector_binary_search(key, vector->data, vector->size, member_size, compare_size, exactmatch); if (*exactmatch) return match; @@ -89,7 +89,7 @@ ot_peer *vector_find_or_insert_peer(ot_vector *vector, ot_peer const *peer, size /* If space is zero but size is set, we're dealing with a list of vector->size buckets */ if (vector->space < vector->size) vector = ((ot_vector *)vector->data) + vector_hash_peer(peer, compare_size, vector->size); - match = binary_search(peer, vector->data, vector->size, peer_size, compare_size, exactmatch); + match = vector_binary_search(peer, vector->data, vector->size, peer_size, compare_size, exactmatch); if (*exactmatch) return match; @@ -137,7 +137,7 @@ int vector_remove_peer(ot_vector *vector, ot_peer const *peer, size_t peer_size) vector = ((ot_vector *)vector->data) + vector_hash_peer(peer, compare_size, vector->size); end = ((ot_peer *)vector->data) + peer_size * vector->size; - match = (ot_peer *)binary_search(peer, vector->data, vector->size, peer_size, compare_size, &exactmatch); + match = (ot_peer *)vector_binary_search(peer, vector->data, vector->size, peer_size, compare_size, &exactmatch); if (!exactmatch) return 0; diff --git a/ot_vector.h b/ot_vector.h index fbc75f4..d553956 100644 --- a/ot_vector.h +++ b/ot_vector.h @@ -26,7 +26,7 @@ typedef struct { /* trackerlogic.h needs ot_vector, break circular dependency here */ #include "trackerlogic.h" -void *binary_search(const void *const key, const void *base, const size_t member_count, const size_t member_size, size_t compare_size, int *exactmatch); +void *vector_binary_search(const void *const key, const void *base, const size_t member_count, const size_t member_size, size_t compare_size, int *exactmatch); void *vector_find_or_insert(ot_vector *vector, void *key, size_t member_size, size_t compare_size, int *exactmatch); ot_peer *vector_find_or_insert_peer(ot_vector *vector, ot_peer const *peer, size_t peer_size, int *exactmatch); diff --git a/proxy.c b/proxy.c index 9486e75..0cac20c 100644 --- a/proxy.c +++ b/proxy.c @@ -149,7 +149,7 @@ size_t add_peer_to_torrent_proxy(ot_hash hash, ot_peer *peer, size_t peer_size) size_t remove_peer_from_torrent_proxy(ot_hash hash, ot_peer *peer, size_t peer_size) { int exactmatch; ot_vector *torrents_list = mutex_bucket_lock_by_hash(hash); - ot_torrent *torrent = binary_search(hash, torrents_list->data, torrents_list->size, sizeof(ot_torrent), OT_HASH_COMPARE_SIZE, &exactmatch); + ot_torrent *torrent = vector_binary_search(hash, torrents_list->data, torrents_list->size, sizeof(ot_torrent), OT_HASH_COMPARE_SIZE, &exactmatch); if (exactmatch) { ot_peerlist *peer_list = peer_list = peer_size == OT_PEER_SIZE6 ? torrent->peer_list6 : torrent->peer_list4; diff --git a/scan_urlencoded_query.c b/scan_urlencoded_query.c index 4825a4a..cfc8708 100644 --- a/scan_urlencoded_query.c +++ b/scan_urlencoded_query.c @@ -160,7 +160,7 @@ ssize_t scan_fixed_int(char *data, size_t len, int *tmp) { return len; } -size_t fmt_urlencoded(char *dest, const uint8_t *src, size_t len) { +size_t urlencoded_format(char *dest, const uint8_t *src, size_t len) { static const char safe[] = "$/.=~-_"; static const char hexdigit[] = "0123456789abcdef"; size_t written = 0, i; diff --git a/scan_urlencoded_query.h b/scan_urlencoded_query.h index f71752d..24e09fd 100644 --- a/scan_urlencoded_query.h +++ b/scan_urlencoded_query.h @@ -56,6 +56,6 @@ ssize_t scan_fixed_int(char *data, size_t len, int *number); /* The other direction: percent encode len bytes from src into dest and return how many characters were written. dest needs room for three times len. */ -size_t fmt_urlencoded(char *dest, const uint8_t *src, size_t len); +size_t urlencoded_format(char *dest, const uint8_t *src, size_t len); #endif diff --git a/tests/t_scan_urlencoded_query.c b/tests/t_scan_urlencoded_query.c index f660ea3..cb6c982 100644 --- a/tests/t_scan_urlencoded_query.c +++ b/tests/t_scan_urlencoded_query.c @@ -265,7 +265,7 @@ static void case_fmt_passes_safe_bytes(void) { size_t n; test_case("alphanumerics and the safe punctuation pass through"); - n = fmt_urlencoded(dest, src, 11); + n = urlencoded_format(dest, src, 11); dest[n] = 0; CHECK_INT(n, 11); CHECK_STR(dest, "aZ09$-./=_~"); @@ -277,7 +277,7 @@ static void case_fmt_escapes_the_rest(void) { size_t n; test_case("everything else becomes a percent escape"); - n = fmt_urlencoded(dest, src, sizeof(src)); + n = urlencoded_format(dest, src, sizeof(src)); dest[n] = 0; CHECK_INT(n, 18); CHECK_STR(dest, "%20%25%26%2b%3a%3f"); @@ -289,7 +289,7 @@ static void case_fmt_hex_is_lower_case(void) { size_t n; test_case("escapes use lower case hex digits"); - n = fmt_urlencoded(dest, src, sizeof(src)); + n = urlencoded_format(dest, src, sizeof(src)); dest[n] = 0; CHECK_INT(n, 9); CHECK_STR(dest, "%00%ab%ff"); @@ -300,7 +300,7 @@ static void case_fmt_zero_length(void) { test_case("a zero length input writes nothing"); memset(dest, 0x5a, sizeof(dest)); - CHECK_INT(fmt_urlencoded(dest, (const uint8_t *)"abc", 0), 0); + CHECK_INT(urlencoded_format(dest, (const uint8_t *)"abc", 0), 0); CHECK_MEM(dest, "\x5a\x5a\x5a\x5a", 4); } @@ -312,7 +312,7 @@ static void case_fmt_worst_case_fits(void) { test_case("a hash of entirely unsafe bytes needs three times its length"); for (i = 0; i < sizeof(src); ++i) src[i] = 0xff; - n = fmt_urlencoded(dest, src, sizeof(src)); + n = urlencoded_format(dest, src, sizeof(src)); dest[n] = 0; CHECK_INT(n, 60); for (i = 0; i < 20; ++i) @@ -338,7 +338,7 @@ static void case_fmt_decoder_disagrees_on_dollar(void) { ssize_t got; test_case("the encoder passes '$' but the decoder rejects it"); - n = fmt_urlencoded(dest, src, sizeof(src)); + n = urlencoded_format(dest, src, sizeof(src)); dest[n] = 0; CHECK_STR(dest, "a$b"); diff --git a/tests/t_vector.c b/tests/t_vector.c index a8186b2..541a6da 100644 --- a/tests/t_vector.c +++ b/tests/t_vector.c @@ -13,7 +13,7 @@ #include "trackerlogic.h" #include "test.h" -/* binary_search returns the matching member when there is one, and otherwise +/* vector_binary_search returns the matching member when there is one, and otherwise the position at which the key would have to be inserted to keep the vector sorted. exactmatch distinguishes the two. */ @@ -25,7 +25,7 @@ static void case_search_finds_member(void) { void *found; test_case("an existing key is found"); - found = binary_search(key, sorted, 5, 4, 4, &exactmatch); + found = vector_binary_search(key, sorted, 5, 4, 4, &exactmatch); CHECK(exactmatch != 0); CHECK_PTR(found, sorted[2]); } @@ -36,9 +36,9 @@ static void case_search_finds_first_and_last(void) { int exactmatch; test_case("the first and last members are found"); - CHECK_PTR(binary_search(first, sorted, 5, 4, 4, &exactmatch), sorted[0]); + CHECK_PTR(vector_binary_search(first, sorted, 5, 4, 4, &exactmatch), sorted[0]); CHECK(exactmatch != 0); - CHECK_PTR(binary_search(last, sorted, 5, 4, 4, &exactmatch), sorted[4]); + CHECK_PTR(vector_binary_search(last, sorted, 5, 4, 4, &exactmatch), sorted[4]); CHECK(exactmatch != 0); } @@ -49,11 +49,11 @@ static void case_search_insert_position(void) { void *slot; test_case("a missing key yields its insert position"); - slot = binary_search(middle, sorted, 5, 4, 4, &exactmatch); + slot = vector_binary_search(middle, sorted, 5, 4, 4, &exactmatch); CHECK_INT(exactmatch, 0); CHECK_PTR(slot, sorted[2]); - slot = binary_search(before, sorted, 5, 4, 4, &exactmatch); + slot = vector_binary_search(before, sorted, 5, 4, 4, &exactmatch); CHECK_INT(exactmatch, 0); CHECK_PTR(slot, sorted[0]); } @@ -64,7 +64,7 @@ static void case_search_past_the_end(void) { void *slot; test_case("a key above every member yields the position past the end"); - slot = binary_search(beyond, sorted, 5, 4, 4, &exactmatch); + slot = vector_binary_search(beyond, sorted, 5, 4, 4, &exactmatch); CHECK_INT(exactmatch, 0); CHECK_PTR(slot, sorted[5]); } @@ -74,7 +74,7 @@ static void case_search_empty(void) { void *base = (void *)sorted; test_case("searching an empty vector reports no match at the base"); - CHECK_PTR(binary_search(sorted[0], base, 0, 4, 4, &exactmatch), base); + CHECK_PTR(vector_binary_search(sorted[0], base, 0, 4, 4, &exactmatch), base); CHECK_INT(exactmatch, 0); } diff --git a/trackerlogic.c b/trackerlogic.c index 74fea6d..9705bd6 100644 --- a/trackerlogic.c +++ b/trackerlogic.c @@ -381,7 +381,7 @@ size_t return_peers_for_torrent(struct ot_workstruct *ws, ot_torrent *torrent, s size_t return_udp_scrape_for_torrent(ot_hash const hash, char *reply) { int exactmatch, delta_torrentcount = 0; ot_vector *torrents_list = mutex_bucket_lock_by_hash(hash); - ot_torrent *torrent = binary_search(hash, torrents_list->data, torrents_list->size, sizeof(ot_torrent), OT_HASH_COMPARE_SIZE, &exactmatch); + ot_torrent *torrent = vector_binary_search(hash, torrents_list->data, torrents_list->size, sizeof(ot_torrent), OT_HASH_COMPARE_SIZE, &exactmatch); if (!exactmatch) { memset(reply, 0, 12); @@ -414,7 +414,7 @@ size_t return_tcp_scrape_for_torrent(ot_hash const *hash_list, size_t amount, ch int delta_torrentcount = 0; ot_hash const *hash = hash_list + i; ot_vector *torrents_list = mutex_bucket_lock_by_hash(*hash); - ot_torrent *torrent = binary_search(hash, torrents_list->data, torrents_list->size, sizeof(ot_torrent), OT_HASH_COMPARE_SIZE, &exactmatch); + ot_torrent *torrent = vector_binary_search(hash, torrents_list->data, torrents_list->size, sizeof(ot_torrent), OT_HASH_COMPARE_SIZE, &exactmatch); if (exactmatch) { if (clean_single_torrent(torrent)) { @@ -443,7 +443,7 @@ static ot_peerlist dummy_list; size_t remove_peer_from_torrent(PROTO_FLAG proto, struct ot_workstruct *ws) { int exactmatch; ot_vector *torrents_list = mutex_bucket_lock_by_hash(*ws->hash); - ot_torrent *torrent = binary_search(ws->hash, torrents_list->data, torrents_list->size, sizeof(ot_torrent), OT_HASH_COMPARE_SIZE, &exactmatch); + ot_torrent *torrent = vector_binary_search(ws->hash, torrents_list->data, torrents_list->size, sizeof(ot_torrent), OT_HASH_COMPARE_SIZE, &exactmatch); ot_peerlist *peer_list = &dummy_list; size_t peer_size; /* initialized in next line */ ot_peer const *peer_src = peer_from_peer6(&ws->peer, &peer_size); -- cgit v1.3