summaryrefslogtreecommitdiff
path: root/toxcore/DHT.c
diff options
context:
space:
mode:
authorDiadlo <polsha3@gmail.com>2017-03-06 09:23:35 +0300
committerDiadlo <polsha3@gmail.com>2017-06-05 01:51:12 +0300
commit71260e38e8d12547b0e55916daf6cadd72f52e19 (patch)
tree42960614e6b6d27995fe4ff717c4dc4348673c5d /toxcore/DHT.c
parent69c71ddd239fe80c2b3b981166d1101f1ac4a418 (diff)
Extract variables
Diffstat (limited to 'toxcore/DHT.c')
-rw-r--r--toxcore/DHT.c67
1 files changed, 39 insertions, 28 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index 6dde6b8c..d8035ab1 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -91,7 +91,9 @@ static unsigned int bit_by_bit_cmp(const uint8_t *pk1, const uint8_t *pk2)
91 } 91 }
92 92
93 for (j = 0; j < 8; ++j) { 93 for (j = 0; j < 8; ++j) {
94 if ((pk1[i] & (1 << (7 - j))) != (pk2[i] & (1 << (7 - j)))) { 94 uint8_t mask = 1 << (7 - j);
95
96 if ((pk1[i] & mask) != (pk2[i] & mask)) {
95 break; 97 break;
96 } 98 }
97 } 99 }
@@ -169,6 +171,8 @@ void DHT_get_shared_key_sent(DHT *dht, uint8_t *shared_key, const uint8_t *publi
169 get_shared_key(&dht->shared_keys_sent, shared_key, dht->self_secret_key, public_key); 171 get_shared_key(&dht->shared_keys_sent, shared_key, dht->self_secret_key, public_key);
170} 172}
171 173
174#define CRYPTO_SIZE 1 + CRYPTO_PUBLIC_KEY_SIZE * 2 + CRYPTO_NONCE_SIZE
175
172/* Create a request to peer. 176/* Create a request to peer.
173 * send_public_key and send_secret_key are the pub/secret keys of the sender. 177 * send_public_key and send_secret_key are the pub/secret keys of the sender.
174 * recv_public_key is public key of receiver. 178 * recv_public_key is public key of receiver.
@@ -186,8 +190,7 @@ int create_request(const uint8_t *send_public_key, const uint8_t *send_secret_ke
186 return -1; 190 return -1;
187 } 191 }
188 192
189 if (MAX_CRYPTO_REQUEST_SIZE < length + 1 + CRYPTO_PUBLIC_KEY_SIZE * 2 + CRYPTO_NONCE_SIZE + 1 + 193 if (MAX_CRYPTO_REQUEST_SIZE < length + CRYPTO_SIZE + 1 + CRYPTO_MAC_SIZE) {
190 CRYPTO_MAC_SIZE) {
191 return -1; 194 return -1;
192 } 195 }
193 196
@@ -197,7 +200,7 @@ int create_request(const uint8_t *send_public_key, const uint8_t *send_secret_ke
197 memcpy(temp + 1, data, length); 200 memcpy(temp + 1, data, length);
198 temp[0] = request_id; 201 temp[0] = request_id;
199 int len = encrypt_data(recv_public_key, send_secret_key, nonce, temp, length + 1, 202 int len = encrypt_data(recv_public_key, send_secret_key, nonce, temp, length + 1,
200 1 + CRYPTO_PUBLIC_KEY_SIZE * 2 + CRYPTO_NONCE_SIZE + packet); 203 CRYPTO_SIZE + packet);
201 204
202 if (len == -1) { 205 if (len == -1) {
203 crypto_memzero(temp, MAX_CRYPTO_REQUEST_SIZE); 206 crypto_memzero(temp, MAX_CRYPTO_REQUEST_SIZE);
@@ -209,7 +212,7 @@ int create_request(const uint8_t *send_public_key, const uint8_t *send_secret_ke
209 memcpy(packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, send_public_key, CRYPTO_PUBLIC_KEY_SIZE); 212 memcpy(packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, send_public_key, CRYPTO_PUBLIC_KEY_SIZE);
210 213
211 crypto_memzero(temp, MAX_CRYPTO_REQUEST_SIZE); 214 crypto_memzero(temp, MAX_CRYPTO_REQUEST_SIZE);
212 return len + 1 + CRYPTO_PUBLIC_KEY_SIZE * 2 + CRYPTO_NONCE_SIZE; 215 return len + CRYPTO_SIZE;
213} 216}
214 217
215/* Puts the senders public key in the request in public_key, the data from the request 218/* Puts the senders public key in the request in public_key, the data from the request
@@ -225,8 +228,7 @@ int handle_request(const uint8_t *self_public_key, const uint8_t *self_secret_ke
225 return -1; 228 return -1;
226 } 229 }
227 230
228 if (length <= CRYPTO_PUBLIC_KEY_SIZE * 2 + CRYPTO_NONCE_SIZE + 1 + CRYPTO_MAC_SIZE || 231 if (length <= CRYPTO_SIZE + CRYPTO_MAC_SIZE || length > MAX_CRYPTO_REQUEST_SIZE) {
229 length > MAX_CRYPTO_REQUEST_SIZE) {
230 return -1; 232 return -1;
231 } 233 }
232 234
@@ -238,8 +240,7 @@ int handle_request(const uint8_t *self_public_key, const uint8_t *self_secret_ke
238 const uint8_t *nonce = packet + 1 + CRYPTO_PUBLIC_KEY_SIZE * 2; 240 const uint8_t *nonce = packet + 1 + CRYPTO_PUBLIC_KEY_SIZE * 2;
239 uint8_t temp[MAX_CRYPTO_REQUEST_SIZE]; 241 uint8_t temp[MAX_CRYPTO_REQUEST_SIZE];
240 int len1 = decrypt_data(public_key, self_secret_key, nonce, 242 int len1 = decrypt_data(public_key, self_secret_key, nonce,
241 packet + 1 + CRYPTO_PUBLIC_KEY_SIZE * 2 + CRYPTO_NONCE_SIZE, 243 packet + CRYPTO_SIZE, length - CRYPTO_SIZE, temp);
242 length - (CRYPTO_PUBLIC_KEY_SIZE * 2 + CRYPTO_NONCE_SIZE + 1), temp);
243 244
244 if (len1 == -1 || len1 == 0) { 245 if (len1 == -1 || len1 == 0) {
245 crypto_memzero(temp, MAX_CRYPTO_REQUEST_SIZE); 246 crypto_memzero(temp, MAX_CRYPTO_REQUEST_SIZE);
@@ -1053,12 +1054,15 @@ static unsigned int ping_node_from_getnodes_ok(DHT *dht, const uint8_t *public_k
1053 ret = 1; 1054 ret = 1;
1054 } 1055 }
1055 1056
1056 if (ret && index_of_node_pk(dht->to_bootstrap, dht->num_to_bootstrap, public_key) == UINT32_MAX 1057 unsigned int *num = &dht->num_to_bootstrap;
1057 && !is_pk_in_close_list(dht, public_key, ip_port)) { 1058 uint32_t index = index_of_node_pk(dht->to_bootstrap, *num, public_key);
1058 if (dht->num_to_bootstrap < MAX_CLOSE_TO_BOOTSTRAP_NODES) { 1059 bool in_close_list = is_pk_in_close_list(dht, public_key, ip_port);
1059 memcpy(dht->to_bootstrap[dht->num_to_bootstrap].public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE); 1060
1060 dht->to_bootstrap[dht->num_to_bootstrap].ip_port = ip_port; 1061 if (ret && index == UINT32_MAX && !in_close_list) {
1061 ++dht->num_to_bootstrap; 1062 if (*num < MAX_CLOSE_TO_BOOTSTRAP_NODES) {
1063 memcpy(dht->to_bootstrap[*num].public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE);
1064 dht->to_bootstrap[*num].ip_port = ip_port;
1065 ++*num;
1062 } else { 1066 } else {
1063 // TODO(irungentoo): ipv6 vs v4 1067 // TODO(irungentoo): ipv6 vs v4
1064 add_to_list(dht->to_bootstrap, MAX_CLOSE_TO_BOOTSTRAP_NODES, public_key, ip_port, dht->self_public_key); 1068 add_to_list(dht->to_bootstrap, MAX_CLOSE_TO_BOOTSTRAP_NODES, public_key, ip_port, dht->self_public_key);
@@ -1078,12 +1082,16 @@ static unsigned int ping_node_from_getnodes_ok(DHT *dht, const uint8_t *public_k
1078 store_ok = 1; 1082 store_ok = 1;
1079 } 1083 }
1080 1084
1081 if (store_ok && index_of_node_pk(dht->to_bootstrap, dht->num_to_bootstrap, public_key) == UINT32_MAX 1085 unsigned int *friend_num = &dht_friend->num_to_bootstrap;
1082 && !is_pk_in_client_list(dht_friend->client_list, MAX_FRIEND_CLIENTS, public_key, ip_port)) { 1086 const uint32_t index = index_of_node_pk(dht_friend->to_bootstrap, *friend_num, public_key);
1083 if (dht_friend->num_to_bootstrap < MAX_SENT_NODES) { 1087 const bool pk_in_list = is_pk_in_client_list(dht_friend->client_list, MAX_FRIEND_CLIENTS, public_key, ip_port);
1084 memcpy(dht_friend->to_bootstrap[dht_friend->num_to_bootstrap].public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE); 1088
1085 dht_friend->to_bootstrap[dht_friend->num_to_bootstrap].ip_port = ip_port; 1089 if (store_ok && index == UINT32_MAX && !pk_in_list) {
1086 ++dht_friend->num_to_bootstrap; 1090 if (*friend_num < MAX_SENT_NODES) {
1091 Node_format *format = &dht_friend->to_bootstrap[*friend_num];
1092 memcpy(format->public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE);
1093 format->ip_port = ip_port;
1094 ++*friend_num;
1087 } else { 1095 } else {
1088 add_to_list(dht_friend->to_bootstrap, MAX_SENT_NODES, public_key, ip_port, dht_friend->public_key); 1096 add_to_list(dht_friend->to_bootstrap, MAX_SENT_NODES, public_key, ip_port, dht_friend->public_key);
1089 } 1097 }
@@ -1297,8 +1305,8 @@ static int sendnodes_ipv6(const DHT *dht, IP_Port ip_port, const uint8_t *public
1297 plain[0] = num_nodes; 1305 plain[0] = num_nodes;
1298 memcpy(plain + 1 + nodes_length, sendback_data, length); 1306 memcpy(plain + 1 + nodes_length, sendback_data, length);
1299 1307
1300 VLA(uint8_t, data, 1 + nodes_length + length + 1 + CRYPTO_PUBLIC_KEY_SIZE 1308 const int crypto_size = 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + CRYPTO_MAC_SIZE;
1301 + CRYPTO_NONCE_SIZE + CRYPTO_MAC_SIZE); 1309 VLA(uint8_t, data, 1 + nodes_length + length + crypto_size);
1302 1310
1303 int len = DHT_create_packet(dht->self_public_key, shared_encryption_key, NET_PACKET_SEND_NODES_IPV6, 1311 int len = DHT_create_packet(dht->self_public_key, shared_encryption_key, NET_PACKET_SEND_NODES_IPV6,
1304 plain, 1 + nodes_length + length, data); 1312 plain, 1 + nodes_length + length, data);
@@ -1310,10 +1318,13 @@ static int sendnodes_ipv6(const DHT *dht, IP_Port ip_port, const uint8_t *public
1310 return sendpacket(dht->net, ip_port, data, len); 1318 return sendpacket(dht->net, ip_port, data, len);
1311} 1319}
1312 1320
1321#define CRYPTO_NODE_SIZE CRYPTO_PUBLIC_KEY_SIZE + sizeof(uint64_t)
1322
1313static int handle_getnodes(void *object, IP_Port source, const uint8_t *packet, uint16_t length, void *userdata) 1323static int handle_getnodes(void *object, IP_Port source, const uint8_t *packet, uint16_t length, void *userdata)
1314{ 1324{
1315 if (length != (1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE + sizeof( 1325 const int crypto_size = 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + CRYPTO_MAC_SIZE;
1316 uint64_t) + CRYPTO_MAC_SIZE)) { 1326
1327 if (length != (crypto_size + CRYPTO_NODE_SIZE)) {
1317 return 1; 1328 return 1;
1318 } 1329 }
1319 1330
@@ -1324,17 +1335,17 @@ static int handle_getnodes(void *object, IP_Port source, const uint8_t *packet,
1324 return 1; 1335 return 1;
1325 } 1336 }
1326 1337
1327 uint8_t plain[CRYPTO_PUBLIC_KEY_SIZE + sizeof(uint64_t)]; 1338 uint8_t plain[CRYPTO_NODE_SIZE];
1328 uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE]; 1339 uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE];
1329 1340
1330 DHT_get_shared_key_recv(dht, shared_key, packet + 1); 1341 DHT_get_shared_key_recv(dht, shared_key, packet + 1);
1331 int len = decrypt_data_symmetric(shared_key, 1342 int len = decrypt_data_symmetric(shared_key,
1332 packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, 1343 packet + 1 + CRYPTO_PUBLIC_KEY_SIZE,
1333 packet + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE, 1344 packet + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE,
1334 CRYPTO_PUBLIC_KEY_SIZE + sizeof(uint64_t) + CRYPTO_MAC_SIZE, 1345 CRYPTO_NODE_SIZE + CRYPTO_MAC_SIZE,
1335 plain); 1346 plain);
1336 1347
1337 if (len != CRYPTO_PUBLIC_KEY_SIZE + sizeof(uint64_t)) { 1348 if (len != CRYPTO_NODE_SIZE) {
1338 return 1; 1349 return 1;
1339 } 1350 }
1340 1351