summaryrefslogtreecommitdiff
path: root/toxcore/DHT.c
diff options
context:
space:
mode:
authorDiadlo <polsha3@gmail.com>2017-06-04 21:01:14 +0300
committerDiadlo <polsha3@gmail.com>2017-06-05 02:04:43 +0300
commit91e3eb0159d7303644f317b61b67e51b93c2f568 (patch)
tree38699792cd79a4875201941f4e2a1b1d1e31f7e6 /toxcore/DHT.c
parent6a69e071096a6dc5ac6914e983adc5dfc6fb9797 (diff)
Improve formatting and syntax
Diffstat (limited to 'toxcore/DHT.c')
-rw-r--r--toxcore/DHT.c127
1 files changed, 50 insertions, 77 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index c01b932e..e3b50569 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -83,7 +83,8 @@ int id_closest(const uint8_t *pk, const uint8_t *pk1, const uint8_t *pk2)
83 */ 83 */
84static unsigned int bit_by_bit_cmp(const uint8_t *pk1, const uint8_t *pk2) 84static unsigned int bit_by_bit_cmp(const uint8_t *pk1, const uint8_t *pk2)
85{ 85{
86 unsigned int i, j = 0; 86 unsigned int i;
87 unsigned int j = 0;
87 88
88 for (i = 0; i < CRYPTO_PUBLIC_KEY_SIZE; ++i) { 89 for (i = 0; i < CRYPTO_PUBLIC_KEY_SIZE; ++i) {
89 if (pk1[i] == pk2[i]) { 90 if (pk1[i] == pk2[i]) {
@@ -112,7 +113,8 @@ static unsigned int bit_by_bit_cmp(const uint8_t *pk1, const uint8_t *pk2)
112 */ 113 */
113void get_shared_key(Shared_Keys *shared_keys, uint8_t *shared_key, const uint8_t *secret_key, const uint8_t *public_key) 114void get_shared_key(Shared_Keys *shared_keys, uint8_t *shared_key, const uint8_t *secret_key, const uint8_t *public_key)
114{ 115{
115 uint32_t num = ~0, curr = 0; 116 uint32_t num = ~0;
117 uint32_t curr = 0;
116 118
117 for (uint32_t i = 0; i < MAX_KEYS_PER_SLOT; ++i) { 119 for (uint32_t i = 0; i < MAX_KEYS_PER_SLOT; ++i) {
118 int index = public_key[30] * MAX_KEYS_PER_SLOT + i; 120 int index = public_key[30] * MAX_KEYS_PER_SLOT + i;
@@ -135,11 +137,9 @@ void get_shared_key(Shared_Keys *shared_keys, uint8_t *shared_key, const uint8_t
135 curr = index; 137 curr = index;
136 } 138 }
137 } 139 }
138 } else { 140 } else if (num != 0) {
139 if (num != 0) { 141 num = 0;
140 num = 0; 142 curr = index;
141 curr = index;
142 }
143 } 143 }
144 } 144 }
145 145
@@ -286,23 +286,18 @@ int to_host_family(IP *ip)
286 */ 286 */
287int packed_node_size(uint8_t ip_family) 287int packed_node_size(uint8_t ip_family)
288{ 288{
289 if (ip_family == AF_INET) { 289 switch (ip_family) {
290 return PACKED_NODE_SIZE_IP4; 290 case AF_INET:
291 } 291 case TCP_INET:
292 292 return PACKED_NODE_SIZE_IP4;
293 if (ip_family == TCP_INET) {
294 return PACKED_NODE_SIZE_IP4;
295 }
296 293
297 if (ip_family == AF_INET6) { 294 case AF_INET6:
298 return PACKED_NODE_SIZE_IP6; 295 case TCP_INET6:
299 } 296 return PACKED_NODE_SIZE_IP6;
300 297
301 if (ip_family == TCP_INET6) { 298 default:
302 return PACKED_NODE_SIZE_IP6; 299 return -1;
303 } 300 }
304
305 return -1;
306} 301}
307 302
308 303
@@ -370,8 +365,7 @@ static int DHT_create_packet(const uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE],
370 365
371 random_nonce(nonce); 366 random_nonce(nonce);
372 367
373 int encrypted_length = encrypt_data_symmetric(shared_key, nonce, 368 int encrypted_length = encrypt_data_symmetric(shared_key, nonce, plain, plain_length, encrypted);
374 plain, plain_length, encrypted);
375 369
376 if (encrypted_length == -1) { 370 if (encrypted_length == -1) {
377 return -1; 371 return -1;
@@ -727,12 +721,10 @@ static void get_close_nodes_inner(const uint8_t *public_key, Node_format *nodes_
727 ipptp = &client->assoc4; 721 ipptp = &client->assoc4;
728 } else if (sa_family == AF_INET6) { 722 } else if (sa_family == AF_INET6) {
729 ipptp = &client->assoc6; 723 ipptp = &client->assoc6;
724 } else if (client->assoc4.timestamp >= client->assoc6.timestamp) {
725 ipptp = &client->assoc4;
730 } else { 726 } else {
731 if (client->assoc4.timestamp >= client->assoc6.timestamp) { 727 ipptp = &client->assoc6;
732 ipptp = &client->assoc4;
733 } else {
734 ipptp = &client->assoc6;
735 }
736 } 728 }
737 729
738 /* node not in a good condition? */ 730 /* node not in a good condition? */
@@ -751,10 +743,7 @@ static void get_close_nodes_inner(const uint8_t *public_key, Node_format *nodes_
751 } 743 }
752 744
753 if (num_nodes < MAX_SENT_NODES) { 745 if (num_nodes < MAX_SENT_NODES) {
754 memcpy(nodes_list[num_nodes].public_key, 746 memcpy(nodes_list[num_nodes].public_key, client->public_key, CRYPTO_PUBLIC_KEY_SIZE);
755 client->public_key,
756 CRYPTO_PUBLIC_KEY_SIZE);
757
758 nodes_list[num_nodes].ip_port = ipptp->ip_port; 747 nodes_list[num_nodes].ip_port = ipptp->ip_port;
759 num_nodes++; 748 num_nodes++;
760 } else { 749 } else {
@@ -843,14 +832,12 @@ static int cmp_dht_entry(const void *a, const void *b)
843 t1 = INCORRECT_HARDENING(entry1.assoc4) && INCORRECT_HARDENING(entry1.assoc6); 832 t1 = INCORRECT_HARDENING(entry1.assoc4) && INCORRECT_HARDENING(entry1.assoc6);
844 t2 = INCORRECT_HARDENING(entry2.assoc4) && INCORRECT_HARDENING(entry2.assoc6); 833 t2 = INCORRECT_HARDENING(entry2.assoc4) && INCORRECT_HARDENING(entry2.assoc6);
845 834
846 if (t1 != t2) { 835 if (t1 && !t2) {
847 if (t1) { 836 return -1;
848 return -1; 837 }
849 }
850 838
851 if (t2) { 839 if (!t1 && t2) {
852 return 1; 840 return 1;
853 }
854 } 841 }
855 842
856 int close = id_closest(cmp_public_key, entry1.public_key, entry2.public_key); 843 int close = id_closest(cmp_public_key, entry1.public_key, entry2.public_key);
@@ -873,12 +860,9 @@ static int cmp_dht_entry(const void *a, const void *b)
873 */ 860 */
874static unsigned int store_node_ok(const Client_data *client, const uint8_t *public_key, const uint8_t *comp_public_key) 861static unsigned int store_node_ok(const Client_data *client, const uint8_t *public_key, const uint8_t *comp_public_key)
875{ 862{
876 if ((is_timeout(client->assoc4.timestamp, BAD_NODE_TIMEOUT) && is_timeout(client->assoc6.timestamp, BAD_NODE_TIMEOUT)) 863 return is_timeout(client->assoc4.timestamp, BAD_NODE_TIMEOUT) &&
877 || (id_closest(comp_public_key, client->public_key, public_key) == 2)) { 864 is_timeout(client->assoc6.timestamp, BAD_NODE_TIMEOUT) ||
878 return 1; 865 id_closest(comp_public_key, client->public_key, public_key) == 2;
879 }
880
881 return 0;
882} 866}
883 867
884static void sort_client_list(Client_data *list, unsigned int length, const uint8_t *comp_public_key) 868static void sort_client_list(Client_data *list, unsigned int length, const uint8_t *comp_public_key)
@@ -1001,11 +985,7 @@ static int add_to_close(DHT *dht, const uint8_t *public_key, IP_Port ip_port, bo
1001 */ 985 */
1002bool node_addable_to_close_list(DHT *dht, const uint8_t *public_key, IP_Port ip_port) 986bool node_addable_to_close_list(DHT *dht, const uint8_t *public_key, IP_Port ip_port)
1003{ 987{
1004 if (add_to_close(dht, public_key, ip_port, 1) == 0) { 988 return add_to_close(dht, public_key, ip_port, 1) == 0;
1005 return 1;
1006 }
1007
1008 return 0;
1009} 989}
1010 990
1011static bool is_pk_in_client_list(Client_data *list, unsigned int client_list_length, const uint8_t *public_key, 991static bool is_pk_in_client_list(Client_data *list, unsigned int client_list_length, const uint8_t *public_key,
@@ -1117,31 +1097,23 @@ uint32_t addto_lists(DHT *dht, IP_Port ip_port, const uint8_t *public_key)
1117 /* NOTE: Current behavior if there are two clients with the same id is 1097 /* NOTE: Current behavior if there are two clients with the same id is
1118 * to replace the first ip by the second. 1098 * to replace the first ip by the second.
1119 */ 1099 */
1120 if (!client_or_ip_port_in_list(dht->log, dht->close_clientlist, LCLIENT_LIST, public_key, ip_port)) { 1100 const bool in_close_list = client_or_ip_port_in_list(dht->log, dht->close_clientlist,
1121 if (add_to_close(dht, public_key, ip_port, 0)) { 1101 LCLIENT_LIST, public_key, ip_port);
1122 used++; 1102
1123 } 1103 /* add_to_close should be called only if !in_list (don't extract to variable) */
1124 } else { 1104 if (in_close_list || add_to_close(dht, public_key, ip_port, 0)) {
1125 used++; 1105 used++;
1126 } 1106 }
1127 1107
1128 DHT_Friend *friend_foundip = 0; 1108 DHT_Friend *friend_foundip = 0;
1129 1109
1130 for (uint32_t i = 0; i < dht->num_friends; ++i) { 1110 for (uint32_t i = 0; i < dht->num_friends; ++i) {
1131 if (!client_or_ip_port_in_list(dht->log, dht->friends_list[i].client_list, 1111 const bool in_list = client_or_ip_port_in_list(dht->log, dht->friends_list[i].client_list,
1132 MAX_FRIEND_CLIENTS, public_key, ip_port)) { 1112 MAX_FRIEND_CLIENTS, public_key, ip_port);
1133 if (replace_all(dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS,
1134 public_key, ip_port, dht->friends_list[i].public_key)) {
1135
1136 DHT_Friend *dht_friend = &dht->friends_list[i];
1137 1113
1138 if (public_key_cmp(public_key, dht_friend->public_key) == 0) { 1114 /* replace_all should be called only if !in_list (don't extract to variable) */
1139 friend_foundip = dht_friend; 1115 if (in_list || replace_all(dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, public_key,
1140 } 1116 ip_port, dht->friends_list[i].public_key)) {
1141
1142 used++;
1143 }
1144 } else {
1145 DHT_Friend *dht_friend = &dht->friends_list[i]; 1117 DHT_Friend *dht_friend = &dht->friends_list[i];
1146 1118
1147 if (id_equal(public_key, dht_friend->public_key)) { 1119 if (id_equal(public_key, dht_friend->public_key)) {
@@ -1152,12 +1124,14 @@ uint32_t addto_lists(DHT *dht, IP_Port ip_port, const uint8_t *public_key)
1152 } 1124 }
1153 } 1125 }
1154 1126
1155 if (friend_foundip) { 1127 if (!friend_foundip) {
1156 for (uint32_t i = 0; i < friend_foundip->lock_count; ++i) { 1128 return used;
1157 if (friend_foundip->callbacks[i].ip_callback) { 1129 }
1158 friend_foundip->callbacks[i].ip_callback(friend_foundip->callbacks[i].data, friend_foundip->callbacks[i].number, 1130
1159 ip_port); 1131 for (uint32_t i = 0; i < friend_foundip->lock_count; ++i) {
1160 } 1132 if (friend_foundip->callbacks[i].ip_callback) {
1133 friend_foundip->callbacks[i].ip_callback(friend_foundip->callbacks[i].data,
1134 friend_foundip->callbacks[i].number, ip_port);
1161 } 1135 }
1162 } 1136 }
1163 1137
@@ -1913,8 +1887,7 @@ int route_tofriend(const DHT *dht, const uint8_t *friend_id, const uint8_t *pack
1913 } 1887 }
1914 1888
1915 /* If ip is not zero and node is good. */ 1889 /* If ip is not zero and node is good. */
1916 if (ip_isset(&assoc->ret_ip_port.ip) && 1890 if (ip_isset(&assoc->ret_ip_port.ip) && !is_timeout(assoc->ret_timestamp, BAD_NODE_TIMEOUT)) {
1917 !is_timeout(assoc->ret_timestamp, BAD_NODE_TIMEOUT)) {
1918 int retval = sendpacket(dht->net, assoc->ip_port, packet, length); 1891 int retval = sendpacket(dht->net, assoc->ip_port, packet, length);
1919 1892
1920 if ((unsigned int)retval == length) { 1893 if ((unsigned int)retval == length) {
@@ -2420,7 +2393,7 @@ static Node_format random_node(DHT *dht, Family sa_family)
2420 * 2393 *
2421 * return the number of nodes. 2394 * return the number of nodes.
2422 */ 2395 */
2423static uint16_t list_nodes(Client_data *list, unsigned int length, Node_format *nodes, uint16_t max_num) 2396static uint16_t list_nodes(Client_data *list, size_t length, Node_format *nodes, uint16_t max_num)
2424{ 2397{
2425 if (max_num == 0) { 2398 if (max_num == 0) {
2426 return 0; 2399 return 0;