summaryrefslogtreecommitdiff
path: root/toxcore/DHT.c
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2016-09-05 16:10:48 +0100
committeriphydf <iphydf@users.noreply.github.com>2016-09-06 11:54:37 +0100
commitad2656051697899e960694bb68ac104fcc5e92f1 (patch)
tree7e69fcd03db88b3839ee523f5d1b51ef9a38c372 /toxcore/DHT.c
parent4e6c86d1cb228308678f89ff6e4e09b3f46347aa (diff)
Improve static and const correctness.
- Any non-externally-visible declarations should be `static`. - Casting away the `const` qualifier from pointers-to-const is dangerous. All but one instance of this are now correct. The one instance where we can't keep `const` is one where toxav code actually writes to a chunk of memory marked as `const`. This code also assumes 4 byte alignment of data packets. I don't know whether that is a valid assumption, but it's likely unportable, and *not* obviously correct. - Replaced empty parameter lists with `(void)` to avoid passing parameters to it. Empty parameter lists are old style declarations for unknown number and type of arguments. - Commented out (as `#if DHT_HARDENING` block) the hardening code that was never executed. - Minor style fix: don't use `default` in enum-switches unless the number of enumerators in the default case is very large. In this case, it was 2, so we want to list them both explicitly to be warned about missing one if we add one in the future. - Removed the only two function declarations from nTox.h and put them into nTox.c. They are not used outside and nTox is not a library.
Diffstat (limited to 'toxcore/DHT.c')
-rw-r--r--toxcore/DHT.c16
1 files changed, 12 insertions, 4 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index ac94a36e..2e475d01 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -2136,6 +2136,7 @@ static void do_NAT(DHT *dht)
2136#define CHECK_TYPE_TEST_REQ 4 2136#define CHECK_TYPE_TEST_REQ 4
2137#define CHECK_TYPE_TEST_RES 5 2137#define CHECK_TYPE_TEST_RES 5
2138 2138
2139#if DHT_HARDENING
2139static int send_hardening_req(DHT *dht, Node_format *sendto, uint8_t type, uint8_t *contents, uint16_t length) 2140static int send_hardening_req(DHT *dht, Node_format *sendto, uint8_t type, uint8_t *contents, uint16_t length)
2140{ 2141{
2141 if (length > HARDREQ_DATA_SIZE - 1) { 2142 if (length > HARDREQ_DATA_SIZE - 1) {
@@ -2164,6 +2165,7 @@ static int send_hardening_getnode_req(DHT *dht, Node_format *dest, Node_format *
2164 memcpy(data + sizeof(Node_format), search_id, crypto_box_PUBLICKEYBYTES); 2165 memcpy(data + sizeof(Node_format), search_id, crypto_box_PUBLICKEYBYTES);
2165 return send_hardening_req(dht, dest, CHECK_TYPE_GETNODE_REQ, data, sizeof(Node_format) + crypto_box_PUBLICKEYBYTES); 2166 return send_hardening_req(dht, dest, CHECK_TYPE_GETNODE_REQ, data, sizeof(Node_format) + crypto_box_PUBLICKEYBYTES);
2166} 2167}
2168#endif
2167 2169
2168/* Send a get node hardening response */ 2170/* Send a get node hardening response */
2169static int send_hardening_getnode_res(const DHT *dht, const Node_format *sendto, const uint8_t *queried_client_id, 2171static int send_hardening_getnode_res(const DHT *dht, const Node_format *sendto, const uint8_t *queried_client_id,
@@ -2316,10 +2318,11 @@ static int handle_hardening(void *object, IP_Port source, const uint8_t *source_
2316 return 1; 2318 return 1;
2317} 2319}
2318 2320
2321#if DHT_HARDENING
2319/* Return a random node from all the nodes we are connected to. 2322/* Return a random node from all the nodes we are connected to.
2320 * TODO: improve this function. 2323 * TODO: improve this function.
2321 */ 2324 */
2322Node_format random_node(DHT *dht, sa_family_t sa_family) 2325static Node_format random_node(DHT *dht, sa_family_t sa_family)
2323{ 2326{
2324 uint8_t id[crypto_box_PUBLICKEYBYTES]; 2327 uint8_t id[crypto_box_PUBLICKEYBYTES];
2325 uint32_t i; 2328 uint32_t i;
@@ -2339,12 +2342,13 @@ Node_format random_node(DHT *dht, sa_family_t sa_family)
2339 2342
2340 return nodes_list[rand() % num_nodes]; 2343 return nodes_list[rand() % num_nodes];
2341} 2344}
2345#endif
2342 2346
2343/* Put up to max_num nodes in nodes from the closelist. 2347/* Put up to max_num nodes in nodes from the closelist.
2344 * 2348 *
2345 * return the number of nodes. 2349 * return the number of nodes.
2346 */ 2350 */
2347uint16_t list_nodes(Client_data *list, unsigned int length, Node_format *nodes, uint16_t max_num) 2351static uint16_t list_nodes(Client_data *list, unsigned int length, Node_format *nodes, uint16_t max_num)
2348{ 2352{
2349 if (max_num == 0) { 2353 if (max_num == 0) {
2350 return 0; 2354 return 0;
@@ -2417,7 +2421,8 @@ uint16_t closelist_nodes(DHT *dht, Node_format *nodes, uint16_t max_num)
2417 return list_nodes(dht->close_clientlist, LCLIENT_LIST, nodes, max_num); 2421 return list_nodes(dht->close_clientlist, LCLIENT_LIST, nodes, max_num);
2418} 2422}
2419 2423
2420void do_hardening(DHT *dht) 2424#if DHT_HARDENING
2425static void do_hardening(DHT *dht)
2421{ 2426{
2422 uint32_t i; 2427 uint32_t i;
2423 2428
@@ -2469,6 +2474,7 @@ void do_hardening(DHT *dht)
2469 //TODO: add the 2 other testers. 2474 //TODO: add the 2 other testers.
2470 } 2475 }
2471} 2476}
2477#endif
2472 2478
2473/*----------------------------------------------------------------------------------*/ 2479/*----------------------------------------------------------------------------------*/
2474 2480
@@ -2589,7 +2595,9 @@ void do_DHT(DHT *dht)
2589 do_DHT_friends(dht); 2595 do_DHT_friends(dht);
2590 do_NAT(dht); 2596 do_NAT(dht);
2591 do_to_ping(dht->ping); 2597 do_to_ping(dht->ping);
2592 //do_hardening(dht); 2598#if DHT_HARDENING
2599 do_hardening(dht);
2600#endif
2593#ifdef ENABLE_ASSOC_DHT 2601#ifdef ENABLE_ASSOC_DHT
2594 2602
2595 if (dht->assoc) 2603 if (dht->assoc)