From 8e0ab68d30d9ed49e6f57e87251d8fcdecf40c93 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Thu, 24 Oct 2013 13:34:04 -0400 Subject: Removed define that could become confusing. --- toxcore/DHT.c | 44 +++++++++++++++++++++++++++----------------- toxcore/net_crypto.c | 8 ++++---- toxcore/net_crypto.h | 2 -- toxcore/network.h | 1 + toxcore/ping.c | 14 +++++++------- 5 files changed, 39 insertions(+), 30 deletions(-) (limited to 'toxcore') diff --git a/toxcore/DHT.c b/toxcore/DHT.c index ee51f16c..bce726c8 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -131,6 +131,7 @@ static int client_in_list(Client_data *list, uint32_t length, uint8_t *client_id uint64_t temp_time = unix_time(); for (i = 0; i < length; i++) + /* Dead nodes are considered dead (not in the list)*/ if (!is_timeout(temp_time, list[i].assoc4.timestamp, KILL_NODE_TIMEOUT) || !is_timeout(temp_time, list[i].assoc6.timestamp, KILL_NODE_TIMEOUT)) @@ -289,6 +290,7 @@ static void get_close_nodes_inner(DHT *dht, uint8_t *client_id, Node_format *nod continue; IPPTsPng *ipptp = NULL; + if (sa_family == AF_INET) ipptp = &client->assoc4; else @@ -640,9 +642,9 @@ static int getnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cli if (ping_id == 0) return -1; - uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING]; + uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + CLIENT_ID_SIZE + crypto_box_MACBYTES]; uint8_t plain[sizeof(ping_id) + CLIENT_ID_SIZE]; - uint8_t encrypt[sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING]; + uint8_t encrypt[sizeof(ping_id) + CLIENT_ID_SIZE + crypto_box_MACBYTES]; uint8_t nonce[crypto_box_NONCEBYTES]; new_nonce(nonce); @@ -656,7 +658,7 @@ static int getnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cli sizeof(ping_id) + CLIENT_ID_SIZE, encrypt ); - if (len != sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING) + if (len != sizeof(ping_id) + CLIENT_ID_SIZE + crypto_box_MACBYTES) return -1; data[0] = NET_PACKET_GET_NODES; @@ -667,6 +669,8 @@ static int getnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cli return sendpacket(dht->c->lossless_udp->net, ip_port, data, sizeof(data)); } +#define NODES_ENCRYPTED_MESSAGE_LENGTH (sizeof(Node_format) + crypto_secretbox_MACBYTES) + /* Send a send nodes response. */ /* because of BINARY compatibility, the Node_format MUST BE Node4_format, * IPv6 nodes are sent in a different message */ @@ -678,7 +682,7 @@ static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cl size_t Node4_format_size = sizeof(Node4_format); uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) - + Node4_format_size * MAX_SENT_NODES + ENCRYPTION_PADDING]; + + Node4_format_size * MAX_SENT_NODES + crypto_box_MACBYTES]; Node_format nodes_list[MAX_SENT_NODES]; int num_nodes = get_close_nodes(dht, client_id, nodes_list, AF_INET, LAN_ip(ip_port.ip) == 0); @@ -687,7 +691,7 @@ static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cl return 0; uint8_t plain[sizeof(ping_id) + Node4_format_size * MAX_SENT_NODES]; - uint8_t encrypt[sizeof(ping_id) + Node4_format_size * MAX_SENT_NODES + ENCRYPTION_PADDING]; + uint8_t encrypt[sizeof(ping_id) + Node4_format_size * MAX_SENT_NODES + crypto_box_MACBYTES]; uint8_t nonce[crypto_box_NONCEBYTES]; new_nonce(nonce); @@ -728,7 +732,7 @@ static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cl if (len == -1) return -1; - if ((unsigned int)len != sizeof(ping_id) + num_nodes * Node4_format_size + ENCRYPTION_PADDING) + if ((unsigned int)len != sizeof(ping_id) + num_nodes * Node4_format_size + crypto_box_MACBYTES) return -1; data[0] = NET_PACKET_SEND_NODES; @@ -748,7 +752,7 @@ static int sendnodes_ipv6(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_ size_t Node_format_size = sizeof(Node_format); uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) - + Node_format_size * MAX_SENT_NODES + ENCRYPTION_PADDING]; + + Node_format_size * MAX_SENT_NODES + crypto_box_MACBYTES]; Node_format nodes_list[MAX_SENT_NODES]; int num_nodes = get_close_nodes(dht, client_id, nodes_list, AF_INET6, LAN_ip(ip_port.ip) == 0); @@ -757,7 +761,7 @@ static int sendnodes_ipv6(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_ return 0; uint8_t plain[sizeof(ping_id) + Node_format_size * MAX_SENT_NODES]; - uint8_t encrypt[sizeof(ping_id) + Node_format_size * MAX_SENT_NODES + ENCRYPTION_PADDING]; + uint8_t encrypt[sizeof(ping_id) + Node_format_size * MAX_SENT_NODES + crypto_box_MACBYTES]; uint8_t nonce[crypto_box_NONCEBYTES]; new_nonce(nonce); @@ -774,7 +778,7 @@ static int sendnodes_ipv6(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_ if (len == -1) return -1; - if ((unsigned int)len != sizeof(ping_id) + num_nodes * Node_format_size + ENCRYPTION_PADDING) + if ((unsigned int)len != sizeof(ping_id) + num_nodes * Node_format_size + crypto_box_MACBYTES) return -1; data[0] = NET_PACKET_SEND_NODES_IPV6; @@ -791,7 +795,7 @@ static int handle_getnodes(void *object, IP_Port source, uint8_t *packet, uint32 uint64_t ping_id; if (length != ( 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES - + sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING )) + + sizeof(ping_id) + CLIENT_ID_SIZE + crypto_box_MACBYTES )) return 1; /* Check if packet is from ourself. */ @@ -804,7 +808,7 @@ static int handle_getnodes(void *object, IP_Port source, uint8_t *packet, uint32 dht->c->self_secret_key, packet + 1 + CLIENT_ID_SIZE, packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, - sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING, + sizeof(ping_id) + CLIENT_ID_SIZE + crypto_box_MACBYTES, plain ); if (len != sizeof(ping_id) + CLIENT_ID_SIZE) @@ -826,7 +830,7 @@ static int handle_sendnodes(void *object, IP_Port source, uint8_t *packet, uint3 DHT *dht = object; uint64_t ping_id; uint32_t cid_size = 1 + CLIENT_ID_SIZE; - cid_size += crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING; + cid_size += crypto_box_NONCEBYTES + sizeof(ping_id) + crypto_box_MACBYTES; size_t Node4_format_size = sizeof(Node4_format); @@ -843,7 +847,7 @@ static int handle_sendnodes(void *object, IP_Port source, uint8_t *packet, uint3 dht->c->self_secret_key, packet + 1 + CLIENT_ID_SIZE, packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, - sizeof(ping_id) + num_nodes * Node4_format_size + ENCRYPTION_PADDING, plain ); + sizeof(ping_id) + num_nodes * Node4_format_size + crypto_box_MACBYTES, plain ); if ((unsigned int)len != sizeof(ping_id) + num_nodes * Node4_format_size) return 1; @@ -888,7 +892,7 @@ static int handle_sendnodes_ipv6(void *object, IP_Port source, uint8_t *packet, DHT *dht = object; uint64_t ping_id; uint32_t cid_size = 1 + CLIENT_ID_SIZE; - cid_size += crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING; + cid_size += crypto_box_NONCEBYTES + sizeof(ping_id) + crypto_box_MACBYTES; size_t Node_format_size = sizeof(Node_format); @@ -905,7 +909,7 @@ static int handle_sendnodes_ipv6(void *object, IP_Port source, uint8_t *packet, dht->c->self_secret_key, packet + 1 + CLIENT_ID_SIZE, packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, - sizeof(ping_id) + num_nodes * Node_format_size + ENCRYPTION_PADDING, plain ); + sizeof(ping_id) + num_nodes * Node_format_size + crypto_box_MACBYTES, plain ); if ((unsigned int)len != sizeof(ping_id) + num_nodes * Node_format_size) return 1; @@ -1193,14 +1197,16 @@ static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num) connected = 0; /* If ip is not zero and node is good. */ - if (ip_isset(&client->assoc4.ret_ip_port.ip) && !is_timeout(temp_time, client->assoc4.ret_timestamp, BAD_NODE_TIMEOUT)) { + if (ip_isset(&client->assoc4.ret_ip_port.ip) + && !is_timeout(temp_time, client->assoc4.ret_timestamp, BAD_NODE_TIMEOUT)) { ipv4s[num_ipv4s] = client->assoc4.ret_ip_port; ++num_ipv4s; connected = 1; } - if (ip_isset(&client->assoc6.ret_ip_port.ip) && !is_timeout(temp_time, client->assoc6.ret_timestamp, BAD_NODE_TIMEOUT)) { + if (ip_isset(&client->assoc6.ret_ip_port.ip) + && !is_timeout(temp_time, client->assoc6.ret_timestamp, BAD_NODE_TIMEOUT)) { ipv6s[num_ipv6s] = client->assoc6.ret_ip_port; ++num_ipv6s; @@ -1213,12 +1219,15 @@ static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num) #ifdef FRIEND_IPLIST_PAD memcpy(ip_portlist, ipv6s, num_ipv6s * sizeof(IP_Port)); + if (num_ipv6s == MAX_FRIEND_CLIENTS) return MAX_FRIEND_CLIENTS; int num_ipv4s_used = MAX_FRIEND_CLIENTS - num_ipv6s; + if (num_ipv4s_used > num_ipv4s) num_ipv4s_used = num_ipv4s; + memcpy(&ip_portlist[num_ipv6s], ipv4s, num_ipv4s_used * sizeof(IP_Port)); return num_ipv6s + num_ipv4s_used; @@ -1825,6 +1834,7 @@ static int dht_load_state_callback(void *outer, uint8_t *data, uint32_t length, break; #ifdef DEBUG + default: fprintf(stderr, "Load state (DHT): contains unrecognized part (len %u, type %u)\n", length, type); diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index a9aa77f9..3c16f0ac 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -247,7 +247,7 @@ int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uin int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *packet, uint8_t *recv_public_key, uint8_t *data, uint32_t length, uint8_t request_id) { - if (MAX_DATA_SIZE < length + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING) + if (MAX_DATA_SIZE < length + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + crypto_box_MACBYTES) return -1; uint8_t nonce[crypto_box_NONCEBYTES]; @@ -278,7 +278,7 @@ int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t * int handle_request(uint8_t *self_public_key, uint8_t *self_secret_key, uint8_t *public_key, uint8_t *data, uint8_t *request_id, uint8_t *packet, uint16_t length) { - if (length > crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING && + if (length > crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + crypto_box_MACBYTES && length <= MAX_DATA_SIZE) { if (memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) { memcpy(public_key, packet + 1 + crypto_box_PUBLICKEYBYTES, crypto_box_PUBLICKEYBYTES); @@ -313,8 +313,8 @@ static int cryptopacket_handle(void *object, IP_Port source, uint8_t *packet, ui DHT *dht = object; if (packet[0] == NET_PACKET_CRYPTO) { - if (length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING || - length > MAX_DATA_SIZE + ENCRYPTION_PADDING) + if (length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + crypto_box_MACBYTES || + length > MAX_DATA_SIZE + crypto_box_MACBYTES) return 1; if (memcmp(packet + 1, dht->c->self_public_key, crypto_box_PUBLICKEYBYTES) == 0) { // Check if request is for us. diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h index 0de66e98..3ec70c21 100644 --- a/toxcore/net_crypto.h +++ b/toxcore/net_crypto.h @@ -77,8 +77,6 @@ typedef struct { #include "DHT.h" -#define ENCRYPTION_PADDING (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES) - /* return zero if the buffer contains only zeros. */ uint8_t crypto_iszero(uint8_t *buffer, uint32_t blen); diff --git a/toxcore/network.h b/toxcore/network.h index 1253c07e..0b76a574 100644 --- a/toxcore/network.h +++ b/toxcore/network.h @@ -70,6 +70,7 @@ typedef int sock_t; #include #include #define crypto_box_MACBYTES (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES) +#define crypto_secretbox_MACBYTES (crypto_secretbox_ZEROBYTES - crypto_secretbox_BOXZEROBYTES) #endif #ifndef IPV6_ADD_MEMBERSHIP diff --git a/toxcore/ping.c b/toxcore/ping.c index 2d0a4545..5fd34972 100644 --- a/toxcore/ping.c +++ b/toxcore/ping.c @@ -3,7 +3,7 @@ * * This file is donated to the Tox Project. * Copyright 2013 plutooo - * + * * Copyright (C) 2013 Tox project All Rights Reserved. * * This file is part of Tox. @@ -20,7 +20,7 @@ * * You should have received a copy of the GNU General Public License * along with Tox. If not, see . - * + * */ #ifdef HAVE_CONFIG_H @@ -132,7 +132,7 @@ static bool is_pinging(PING *ping, IP_Port ipp, uint64_t ping_id) // O(n) TOD return false; } -#define DHT_PING_SIZE (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(uint64_t) + ENCRYPTION_PADDING) +#define DHT_PING_SIZE (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(uint64_t) + crypto_box_MACBYTES) int send_ping_request(PING *ping, IP_Port ipp, uint8_t *client_id) { @@ -157,7 +157,7 @@ int send_ping_request(PING *ping, IP_Port ipp, uint8_t *client_id) (uint8_t *) &ping_id, sizeof(ping_id), pk + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES); - if (rc != sizeof(ping_id) + ENCRYPTION_PADDING) + if (rc != sizeof(ping_id) + crypto_box_MACBYTES) return 1; return sendpacket(ping->c->lossless_udp->net, ipp, pk, sizeof(pk)); @@ -182,7 +182,7 @@ static int send_ping_response(PING *ping, IP_Port ipp, uint8_t *client_id, uint6 (uint8_t *) &ping_id, sizeof(ping_id), pk + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES); - if (rc != sizeof(ping_id) + ENCRYPTION_PADDING) + if (rc != sizeof(ping_id) + crypto_box_MACBYTES) return 1; return sendpacket(ping->c->lossless_udp->net, ipp, pk, sizeof(pk)); @@ -207,7 +207,7 @@ static int handle_ping_request(void *_dht, IP_Port source, uint8_t *packet, uint ping->c->self_secret_key, packet + 1 + CLIENT_ID_SIZE, packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, - sizeof(ping_id) + ENCRYPTION_PADDING, + sizeof(ping_id) + crypto_box_MACBYTES, (uint8_t *) &ping_id); if (rc != sizeof(ping_id)) @@ -239,7 +239,7 @@ static int handle_ping_response(void *_dht, IP_Port source, uint8_t *packet, uin ping->c->self_secret_key, packet + 1 + CLIENT_ID_SIZE, packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, - sizeof(ping_id) + ENCRYPTION_PADDING, + sizeof(ping_id) + crypto_box_MACBYTES, (uint8_t *) &ping_id); if (rc != sizeof(ping_id)) -- cgit v1.2.3