diff options
author | iphydf <iphydf@users.noreply.github.com> | 2016-09-05 16:10:48 +0100 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2016-09-06 11:54:37 +0100 |
commit | ad2656051697899e960694bb68ac104fcc5e92f1 (patch) | |
tree | 7e69fcd03db88b3839ee523f5d1b51ef9a38c372 /toxcore | |
parent | 4e6c86d1cb228308678f89ff6e4e09b3f46347aa (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')
-rw-r--r-- | toxcore/DHT.c | 16 | ||||
-rw-r--r-- | toxcore/Messenger.c | 8 | ||||
-rw-r--r-- | toxcore/group.c | 6 | ||||
-rw-r--r-- | toxcore/net_crypto.c | 2 | ||||
-rw-r--r-- | toxcore/network.c | 14 | ||||
-rw-r--r-- | toxcore/util.c | 4 | ||||
-rw-r--r-- | toxcore/util.h | 4 |
7 files changed, 31 insertions, 23 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 | ||
2139 | static int send_hardening_req(DHT *dht, Node_format *sendto, uint8_t type, uint8_t *contents, uint16_t length) | 2140 | static 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 */ |
2169 | static int send_hardening_getnode_res(const DHT *dht, const Node_format *sendto, const uint8_t *queried_client_id, | 2171 | static 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 | */ |
2322 | Node_format random_node(DHT *dht, sa_family_t sa_family) | 2325 | static 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 | */ |
2347 | uint16_t list_nodes(Client_data *list, unsigned int length, Node_format *nodes, uint16_t max_num) | 2351 | static 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 | ||
2420 | void do_hardening(DHT *dht) | 2424 | #if DHT_HARDENING |
2425 | static 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) |
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 472ddd9e..7ec2ad01 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c | |||
@@ -56,7 +56,7 @@ static uint8_t friend_not_valid(const Messenger *m, int32_t friendnumber) | |||
56 | * | 56 | * |
57 | * return -1 if realloc fails. | 57 | * return -1 if realloc fails. |
58 | */ | 58 | */ |
59 | int realloc_friendlist(Messenger *m, uint32_t num) | 59 | static int realloc_friendlist(Messenger *m, uint32_t num) |
60 | { | 60 | { |
61 | if (num == 0) { | 61 | if (num == 0) { |
62 | free(m->friendlist); | 62 | free(m->friendlist); |
@@ -1191,8 +1191,8 @@ long int new_filesender(const Messenger *m, int32_t friendnumber, uint32_t file_ | |||
1191 | return i; | 1191 | return i; |
1192 | } | 1192 | } |
1193 | 1193 | ||
1194 | int send_file_control_packet(const Messenger *m, int32_t friendnumber, uint8_t send_receive, uint8_t filenumber, | 1194 | static int send_file_control_packet(const Messenger *m, int32_t friendnumber, uint8_t send_receive, uint8_t filenumber, |
1195 | uint8_t control_type, uint8_t *data, uint16_t data_length) | 1195 | uint8_t control_type, uint8_t *data, uint16_t data_length) |
1196 | { | 1196 | { |
1197 | if ((unsigned int)(1 + 3 + data_length) > MAX_CRYPTO_DATA_SIZE) { | 1197 | if ((unsigned int)(1 + 3 + data_length) > MAX_CRYPTO_DATA_SIZE) { |
1198 | return -1; | 1198 | return -1; |
@@ -2845,7 +2845,7 @@ static int messenger_load_state_callback(void *outer, const uint8_t *data, uint3 | |||
2845 | switch (type) { | 2845 | switch (type) { |
2846 | case MESSENGER_STATE_TYPE_NOSPAMKEYS: | 2846 | case MESSENGER_STATE_TYPE_NOSPAMKEYS: |
2847 | if (length == crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t)) { | 2847 | if (length == crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t)) { |
2848 | set_nospam(&(m->fr), *(uint32_t *)data); | 2848 | set_nospam(&(m->fr), *(const uint32_t *)data); |
2849 | load_secret_key(m->net_crypto, (&data[sizeof(uint32_t)]) + crypto_box_PUBLICKEYBYTES); | 2849 | load_secret_key(m->net_crypto, (&data[sizeof(uint32_t)]) + crypto_box_PUBLICKEYBYTES); |
2850 | 2850 | ||
2851 | if (public_key_cmp((&data[sizeof(uint32_t)]), m->net_crypto->self_public_key) != 0) { | 2851 | if (public_key_cmp((&data[sizeof(uint32_t)]), m->net_crypto->self_public_key) != 0) { |
diff --git a/toxcore/group.c b/toxcore/group.c index f1086657..dbfb31cd 100644 --- a/toxcore/group.c +++ b/toxcore/group.c | |||
@@ -1188,7 +1188,7 @@ static unsigned int send_message_group(const Group_Chats *g_c, int groupnumber, | |||
1188 | uint16_t len); | 1188 | uint16_t len); |
1189 | 1189 | ||
1190 | #define GROUP_MESSAGE_PING_ID 0 | 1190 | #define GROUP_MESSAGE_PING_ID 0 |
1191 | int group_ping_send(const Group_Chats *g_c, int groupnumber) | 1191 | static int group_ping_send(const Group_Chats *g_c, int groupnumber) |
1192 | { | 1192 | { |
1193 | if (send_message_group(g_c, groupnumber, GROUP_MESSAGE_PING_ID, 0, 0)) { | 1193 | if (send_message_group(g_c, groupnumber, GROUP_MESSAGE_PING_ID, 0, 0)) { |
1194 | return 0; | 1194 | return 0; |
@@ -1203,8 +1203,8 @@ int group_ping_send(const Group_Chats *g_c, int groupnumber) | |||
1203 | * return 0 on success | 1203 | * return 0 on success |
1204 | * return -1 on failure | 1204 | * return -1 on failure |
1205 | */ | 1205 | */ |
1206 | int group_new_peer_send(const Group_Chats *g_c, int groupnumber, uint16_t peer_num, const uint8_t *real_pk, | 1206 | static int group_new_peer_send(const Group_Chats *g_c, int groupnumber, uint16_t peer_num, const uint8_t *real_pk, |
1207 | uint8_t *temp_pk) | 1207 | uint8_t *temp_pk) |
1208 | { | 1208 | { |
1209 | uint8_t packet[GROUP_MESSAGE_NEW_PEER_LENGTH]; | 1209 | uint8_t packet[GROUP_MESSAGE_NEW_PEER_LENGTH]; |
1210 | 1210 | ||
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 0d8fabac..e09d7444 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c | |||
@@ -458,7 +458,7 @@ static int add_ip_port_connection(Net_Crypto *c, int crypt_connection_id, IP_Por | |||
458 | * return IP_Port with family 0 on failure. | 458 | * return IP_Port with family 0 on failure. |
459 | * return IP_Port on success. | 459 | * return IP_Port on success. |
460 | */ | 460 | */ |
461 | IP_Port return_ip_port_connection(Net_Crypto *c, int crypt_connection_id) | 461 | static IP_Port return_ip_port_connection(Net_Crypto *c, int crypt_connection_id) |
462 | { | 462 | { |
463 | IP_Port empty; | 463 | IP_Port empty; |
464 | empty.ip.family = 0; | 464 | empty.ip.family = 0; |
diff --git a/toxcore/network.c b/toxcore/network.c index 05867adc..2097078a 100644 --- a/toxcore/network.c +++ b/toxcore/network.c | |||
@@ -268,11 +268,11 @@ uint64_t current_time_monotonic(void) | |||
268 | 268 | ||
269 | static uint32_t data_0(uint16_t buflen, const uint8_t *buffer) | 269 | static uint32_t data_0(uint16_t buflen, const uint8_t *buffer) |
270 | { | 270 | { |
271 | return buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0; | 271 | return buflen > 4 ? ntohl(*(const uint32_t *)&buffer[1]) : 0; |
272 | } | 272 | } |
273 | static uint32_t data_1(uint16_t buflen, const uint8_t *buffer) | 273 | static uint32_t data_1(uint16_t buflen, const uint8_t *buffer) |
274 | { | 274 | { |
275 | return buflen > 7 ? ntohl(*(uint32_t *)&buffer[5]) : 0; | 275 | return buflen > 7 ? ntohl(*(const uint32_t *)&buffer[5]) : 0; |
276 | } | 276 | } |
277 | 277 | ||
278 | static void loglogdata(Logger *log, const char *message, const uint8_t *buffer, | 278 | static void loglogdata(Logger *log, const char *message, const uint8_t *buffer, |
@@ -359,7 +359,7 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint1 | |||
359 | return -1; | 359 | return -1; |
360 | } | 360 | } |
361 | 361 | ||
362 | int res = sendto(net->sock, (char *) data, length, 0, (struct sockaddr *)&addr, addrsize); | 362 | int res = sendto(net->sock, (const char *) data, length, 0, (struct sockaddr *)&addr, addrsize); |
363 | 363 | ||
364 | loglogdata(net->log, "O=>", data, length, ip_port, res); | 364 | loglogdata(net->log, "O=>", data, length, ip_port, res); |
365 | 365 | ||
@@ -874,13 +874,13 @@ const char *ip_ntoa(const IP *ip) | |||
874 | if (ip) { | 874 | if (ip) { |
875 | if (ip->family == AF_INET) { | 875 | if (ip->family == AF_INET) { |
876 | /* returns standard quad-dotted notation */ | 876 | /* returns standard quad-dotted notation */ |
877 | struct in_addr *addr = (struct in_addr *)&ip->ip4; | 877 | const struct in_addr *addr = (const struct in_addr *)&ip->ip4; |
878 | 878 | ||
879 | addresstext[0] = 0; | 879 | addresstext[0] = 0; |
880 | inet_ntop(ip->family, addr, addresstext, sizeof(addresstext)); | 880 | inet_ntop(ip->family, addr, addresstext, sizeof(addresstext)); |
881 | } else if (ip->family == AF_INET6) { | 881 | } else if (ip->family == AF_INET6) { |
882 | /* returns hex-groups enclosed into square brackets */ | 882 | /* returns hex-groups enclosed into square brackets */ |
883 | struct in6_addr *addr = (struct in6_addr *)&ip->ip6; | 883 | const struct in6_addr *addr = (const struct in6_addr *)&ip->ip6; |
884 | 884 | ||
885 | addresstext[0] = '['; | 885 | addresstext[0] = '['; |
886 | inet_ntop(ip->family, addr, &addresstext[1], sizeof(addresstext) - 3); | 886 | inet_ntop(ip->family, addr, &addresstext[1], sizeof(addresstext) - 3); |
@@ -921,12 +921,12 @@ int ip_parse_addr(const IP *ip, char *address, size_t length) | |||
921 | } | 921 | } |
922 | 922 | ||
923 | if (ip->family == AF_INET) { | 923 | if (ip->family == AF_INET) { |
924 | struct in_addr *addr = (struct in_addr *)&ip->ip4; | 924 | const struct in_addr *addr = (const struct in_addr *)&ip->ip4; |
925 | return inet_ntop(ip->family, addr, address, length) != NULL; | 925 | return inet_ntop(ip->family, addr, address, length) != NULL; |
926 | } | 926 | } |
927 | 927 | ||
928 | if (ip->family == AF_INET6) { | 928 | if (ip->family == AF_INET6) { |
929 | struct in6_addr *addr = (struct in6_addr *)&ip->ip6; | 929 | const struct in6_addr *addr = (const struct in6_addr *)&ip->ip6; |
930 | return inet_ntop(ip->family, addr, address, length) != NULL; | 930 | return inet_ntop(ip->family, addr, address, length) != NULL; |
931 | } | 931 | } |
932 | 932 | ||
diff --git a/toxcore/util.c b/toxcore/util.c index 9f1a7e28..92ad4510 100644 --- a/toxcore/util.c +++ b/toxcore/util.c | |||
@@ -38,7 +38,7 @@ | |||
38 | static uint64_t unix_time_value; | 38 | static uint64_t unix_time_value; |
39 | static uint64_t unix_base_time_value; | 39 | static uint64_t unix_base_time_value; |
40 | 40 | ||
41 | void unix_time_update() | 41 | void unix_time_update(void) |
42 | { | 42 | { |
43 | if (unix_base_time_value == 0) { | 43 | if (unix_base_time_value == 0) { |
44 | unix_base_time_value = ((uint64_t)time(NULL) - (current_time_monotonic() / 1000ULL)); | 44 | unix_base_time_value = ((uint64_t)time(NULL) - (current_time_monotonic() / 1000ULL)); |
@@ -47,7 +47,7 @@ void unix_time_update() | |||
47 | unix_time_value = (current_time_monotonic() / 1000ULL) + unix_base_time_value; | 47 | unix_time_value = (current_time_monotonic() / 1000ULL) + unix_base_time_value; |
48 | } | 48 | } |
49 | 49 | ||
50 | uint64_t unix_time() | 50 | uint64_t unix_time(void) |
51 | { | 51 | { |
52 | return unix_time_value; | 52 | return unix_time_value; |
53 | } | 53 | } |
diff --git a/toxcore/util.h b/toxcore/util.h index 17b1a27b..840f0a3e 100644 --- a/toxcore/util.h +++ b/toxcore/util.h | |||
@@ -32,8 +32,8 @@ | |||
32 | #define MIN(a,b) (((a)<(b))?(a):(b)) | 32 | #define MIN(a,b) (((a)<(b))?(a):(b)) |
33 | #define PAIR(TYPE1__, TYPE2__) struct { TYPE1__ first; TYPE2__ second; } | 33 | #define PAIR(TYPE1__, TYPE2__) struct { TYPE1__ first; TYPE2__ second; } |
34 | 34 | ||
35 | void unix_time_update(); | 35 | void unix_time_update(void); |
36 | uint64_t unix_time(); | 36 | uint64_t unix_time(void); |
37 | int is_timeout(uint64_t timestamp, uint64_t timeout); | 37 | int is_timeout(uint64_t timestamp, uint64_t timeout); |
38 | 38 | ||
39 | 39 | ||