From 163e9c3b03d2f70141c530ab14cbd0dc208c185c Mon Sep 17 00:00:00 2001 From: iphydf Date: Fri, 25 Feb 2022 14:31:51 +0000 Subject: [PATCH] cleanup: Remove more boolean conversions (and a bugfix). These were found by the new stronger type check in cimple. The one bugfix is in `crypto_sha512_cmp`, which used to think `crypto_verify_32` returns bool while actually it's -1/0/1. --- auto_tests/TCP_test.c | 22 +-- auto_tests/dht_test.c | 14 +- .../docker/tox-bootstrapd.sha256 | 2 +- testing/BUILD.bazel | 1 + toxav/msi.c | 4 +- toxav/toxav.c | 14 +- toxcore/DHT.c | 70 +++---- toxcore/LAN_discovery.c | 2 +- toxcore/Messenger.c | 173 ++++++++---------- toxcore/Messenger.h | 32 ++-- toxcore/TCP_client.c | 22 +-- toxcore/TCP_common.c | 13 +- toxcore/TCP_common.h | 6 - toxcore/TCP_connection.c | 10 +- toxcore/TCP_server.c | 22 +-- toxcore/crypto_core.c | 16 +- toxcore/crypto_core.h | 10 +- toxcore/friend_connection.c | 16 +- toxcore/friend_requests.c | 6 +- toxcore/group.c | 78 ++++---- toxcore/net_crypto.c | 56 +++--- toxcore/network.c | 14 +- toxcore/onion_announce.c | 8 +- toxcore/onion_client.c | 148 +++++++-------- toxcore/ping.c | 20 +- toxcore/tox.c | 2 +- toxcore/util.c | 11 +- toxcore/util.h | 20 +- toxcore/util_test.cc | 6 +- 29 files changed, 420 insertions(+), 398 deletions(-) diff --git a/auto_tests/TCP_test.c b/auto_tests/TCP_test.c index 1fefcab924..0d5ddbcd62 100644 --- a/auto_tests/TCP_test.c +++ b/auto_tests/TCP_test.c @@ -166,7 +166,7 @@ static void test_basic(void) ck_assert_msg(packet_resp_plain[0] == TCP_PACKET_ROUTING_RESPONSE, "Server sent the wrong packet id: %u", packet_resp_plain[0]); ck_assert_msg(packet_resp_plain[1] == 0, "Server did not refuse the connection."); - ck_assert_msg(public_key_cmp(packet_resp_plain + 2, f_public_key) == 0, "Server sent the wrong public key."); + ck_assert_msg(pk_equal(packet_resp_plain + 2, f_public_key), "Server sent the wrong public key."); // Closing connections. kill_sock(sock); @@ -315,14 +315,14 @@ static void test_some(void) ck_assert_msg(len == 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE, "Wrong response packet length of %d.", len); ck_assert_msg(data[0] == TCP_PACKET_ROUTING_RESPONSE, "Wrong response packet id of %d.", data[0]); ck_assert_msg(data[1] == 16, "Server didn't refuse connection using wrong public key."); - ck_assert_msg(public_key_cmp(data + 2, con3->public_key) == 0, "Key in response packet wrong."); + ck_assert_msg(pk_equal(data + 2, con3->public_key), "Key in response packet wrong."); // Connection 3 len = read_packet_sec_TCP(logger, con3, data, 2 + 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE); ck_assert_msg(len == 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE, "Wrong response packet length of %d.", len); ck_assert_msg(data[0] == TCP_PACKET_ROUTING_RESPONSE, "Wrong response packet id of %d.", data[0]); ck_assert_msg(data[1] == 16, "Server didn't refuse connection using wrong public key."); - ck_assert_msg(public_key_cmp(data + 2, con1->public_key) == 0, "Key in response packet wrong."); + ck_assert_msg(pk_equal(data + 2, con1->public_key), "Key in response packet wrong."); uint8_t test_packet[512] = {16, 17, 16, 86, 99, 127, 255, 189, 78}; // What is this packet???? @@ -458,7 +458,7 @@ static int oob_data_callback(void *object, const uint8_t *public_key, const uint return 1; } - if (public_key_cmp(public_key, oob_pubkey) != 0) { + if (!pk_equal(public_key, oob_pubkey)) { return 1; } @@ -563,7 +563,7 @@ static void test_client(void) // All callback methods save data should have run during the above network prodding. ck_assert_msg(oob_data_callback_good == 1, "OOB callback not called"); ck_assert_msg(response_callback_good == 1, "Response callback not called."); - ck_assert_msg(public_key_cmp(response_callback_public_key, f2_public_key) == 0, "Wrong public key."); + ck_assert_msg(pk_equal(response_callback_public_key, f2_public_key), "Wrong public key."); ck_assert_msg(status_callback_good == 1, "Status callback not called."); ck_assert_msg(status_callback_status == 2, "Wrong status callback status."); ck_assert_msg(status_callback_connection_id == response_callback_connection_id, @@ -680,17 +680,17 @@ static void test_tcp_connection(void) uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE]; crypto_new_keypair(self_public_key, self_secret_key); TCP_Server *tcp_s = new_TCP_server(logger, USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr); - ck_assert_msg(public_key_cmp(tcp_server_public_key(tcp_s), self_public_key) == 0, "Wrong public key"); + ck_assert_msg(pk_equal(tcp_server_public_key(tcp_s), self_public_key), "Wrong public key"); TCP_Proxy_Info proxy_info; proxy_info.proxy_type = TCP_PROXY_NONE; crypto_new_keypair(self_public_key, self_secret_key); TCP_Connections *tc_1 = new_tcp_connections(logger, mono_time, self_secret_key, &proxy_info); - ck_assert_msg(public_key_cmp(tcp_connections_public_key(tc_1), self_public_key) == 0, "Wrong public key"); + ck_assert_msg(pk_equal(tcp_connections_public_key(tc_1), self_public_key), "Wrong public key"); crypto_new_keypair(self_public_key, self_secret_key); TCP_Connections *tc_2 = new_tcp_connections(logger, mono_time, self_secret_key, &proxy_info); - ck_assert_msg(public_key_cmp(tcp_connections_public_key(tc_2), self_public_key) == 0, "Wrong public key"); + ck_assert_msg(pk_equal(tcp_connections_public_key(tc_2), self_public_key), "Wrong public key"); IP_Port ip_port_tcp_s; @@ -788,17 +788,17 @@ static void test_tcp_connection2(void) uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE]; crypto_new_keypair(self_public_key, self_secret_key); TCP_Server *tcp_s = new_TCP_server(logger, USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr); - ck_assert_msg(public_key_cmp(tcp_server_public_key(tcp_s), self_public_key) == 0, "Wrong public key"); + ck_assert_msg(pk_equal(tcp_server_public_key(tcp_s), self_public_key), "Wrong public key"); TCP_Proxy_Info proxy_info; proxy_info.proxy_type = TCP_PROXY_NONE; crypto_new_keypair(self_public_key, self_secret_key); TCP_Connections *tc_1 = new_tcp_connections(logger, mono_time, self_secret_key, &proxy_info); - ck_assert_msg(public_key_cmp(tcp_connections_public_key(tc_1), self_public_key) == 0, "Wrong public key"); + ck_assert_msg(pk_equal(tcp_connections_public_key(tc_1), self_public_key), "Wrong public key"); crypto_new_keypair(self_public_key, self_secret_key); TCP_Connections *tc_2 = new_tcp_connections(logger, mono_time, self_secret_key, &proxy_info); - ck_assert_msg(public_key_cmp(tcp_connections_public_key(tc_2), self_public_key) == 0, "Wrong public key"); + ck_assert_msg(pk_equal(tcp_connections_public_key(tc_2), self_public_key), "Wrong public key"); IP_Port ip_port_tcp_s; diff --git a/auto_tests/dht_test.c b/auto_tests/dht_test.c index 1ba181ac45..b07800f18c 100644 --- a/auto_tests/dht_test.c +++ b/auto_tests/dht_test.c @@ -72,7 +72,7 @@ static int client_in_list(Client_data *list, uint32_t length, const uint8_t *pub uint32_t i; for (i = 0; i < length; ++i) { - if (id_equal(public_key, list[i].public_key)) { + if (pk_equal(public_key, list[i].public_key)) { return i; } } @@ -106,7 +106,7 @@ static void test_addto_lists_update(DHT *dht, // check ip_port update for existing id test = random_u32() % length; test_ipp.port = random_u32() % TOX_PORT_DEFAULT; - id_copy(test_id, list[test].public_key); + pk_copy(test_id, list[test].public_key); used = addto_lists(dht, &test_ipp, test_id); ck_assert_msg(used >= 1, "Wrong number of added clients"); @@ -120,7 +120,7 @@ static void test_addto_lists_update(DHT *dht, test2 = random_u32() % (length / 2) + length / 2; ipport_copy(&test_ipp, ipv6 ? &list[test1].assoc6.ip_port : &list[test1].assoc4.ip_port); - id_copy(test_id, list[test2].public_key); + pk_copy(test_id, list[test2].public_key); if (ipv6) { list[test2].assoc6.ip_port.port = -1; @@ -139,7 +139,7 @@ static void test_addto_lists_update(DHT *dht, test2 = random_u32() % (length / 2) + length / 2; ipport_copy(&test_ipp, ipv6 ? &list[test2].assoc6.ip_port : &list[test2].assoc4.ip_port); - id_copy(test_id, list[test1].public_key); + pk_copy(test_id, list[test1].public_key); if (ipv6) { list[test1].assoc6.ip_port.port = -1; @@ -173,9 +173,9 @@ static void test_addto_lists_bad(DHT *dht, test3 = random_u32() % (length / 3) + 2 * length / 3; ck_assert_msg(!(test1 == test2 || test1 == test3 || test2 == test3), "Wrong test indices are chosen"); - id_copy((uint8_t *)&test_id1, list[test1].public_key); - id_copy((uint8_t *)&test_id2, list[test2].public_key); - id_copy((uint8_t *)&test_id3, list[test3].public_key); + pk_copy((uint8_t *)&test_id1, list[test1].public_key); + pk_copy((uint8_t *)&test_id2, list[test2].public_key); + pk_copy((uint8_t *)&test_id3, list[test3].public_key); // mark nodes as "bad" if (ipv6) { diff --git a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 index 2f8d32c70c..5590c6afd5 100644 --- a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 +++ b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 @@ -1 +1 @@ -aea24cfae8db82511a298e2dbedb6634145732190ebcad361690cb6ca6560d7c /usr/local/bin/tox-bootstrapd +a80ea98f55b82eb5d60baa5d7fc32913ff964b04b931a17b74233148a27786f5 /usr/local/bin/tox-bootstrapd diff --git a/testing/BUILD.bazel b/testing/BUILD.bazel index 22e404b221..2b73dcbcc1 100644 --- a/testing/BUILD.bazel +++ b/testing/BUILD.bazel @@ -13,6 +13,7 @@ sh_test( size = "small", srcs = ["//hs-tokstyle/tools:check-cimple"], args = ["$(locations %s)" % f for f in CIMPLE_FILES] + [ + "-Wno-boolean-return", "-Wno-callback-names", "-Wno-enum-names", "-Wno-memcpy-structs", diff --git a/toxav/msi.c b/toxav/msi.c index 55ccfea58e..83387d9391 100644 --- a/toxav/msi.c +++ b/toxav/msi.c @@ -391,7 +391,7 @@ static int msg_parse_in(const Logger *log, MSIMessage *dest, const uint8_t *data const uint8_t *it = data; int size_constraint = length; - while (*it) {/* until end byte is hit */ + while (*it != 0) {/* until end byte is hit */ switch (*it) { case ID_REQUEST: { if (!check_size(log, it, &size_constraint, 1) || @@ -435,7 +435,7 @@ static int msg_parse_in(const Logger *log, MSIMessage *dest, const uint8_t *data } } - if (dest->request.exists == false) { + if (!dest->request.exists) { LOGGER_ERROR(log, "Invalid request field!"); return -1; } diff --git a/toxav/toxav.c b/toxav/toxav.c index fa2d384654..1f84d993d7 100644 --- a/toxav/toxav.c +++ b/toxav/toxav.c @@ -421,7 +421,7 @@ bool toxav_answer(ToxAV *av, uint32_t friend_number, uint32_t audio_bit_rate, ui Toxav_Err_Answer rc = TOXAV_ERR_ANSWER_OK; ToxAVCall *call; - if (m_friend_exists(av->m, friend_number) == 0) { + if (!m_friend_exists(av->m, friend_number)) { rc = TOXAV_ERR_ANSWER_FRIEND_NOT_FOUND; goto RETURN; } @@ -612,7 +612,7 @@ static Toxav_Err_Call_Control call_control_handle(ToxAVCall *call, Toxav_Call_Co } static Toxav_Err_Call_Control call_control(ToxAV *av, uint32_t friend_number, Toxav_Call_Control control) { - if (m_friend_exists(av->m, friend_number) == 0) { + if (!m_friend_exists(av->m, friend_number)) { return TOXAV_ERR_CALL_CONTROL_FRIEND_NOT_FOUND; } @@ -644,7 +644,7 @@ bool toxav_audio_set_bit_rate(ToxAV *av, uint32_t friend_number, uint32_t bit_ra Toxav_Err_Bit_Rate_Set rc = TOXAV_ERR_BIT_RATE_SET_OK; ToxAVCall *call; - if (m_friend_exists(av->m, friend_number) == 0) { + if (!m_friend_exists(av->m, friend_number)) { rc = TOXAV_ERR_BIT_RATE_SET_FRIEND_NOT_FOUND; goto RETURN; } @@ -716,7 +716,7 @@ bool toxav_video_set_bit_rate(ToxAV *av, uint32_t friend_number, uint32_t bit_ra Toxav_Err_Bit_Rate_Set rc = TOXAV_ERR_BIT_RATE_SET_OK; ToxAVCall *call; - if (m_friend_exists(av->m, friend_number) == 0) { + if (!m_friend_exists(av->m, friend_number)) { rc = TOXAV_ERR_BIT_RATE_SET_FRIEND_NOT_FOUND; goto RETURN; } @@ -802,7 +802,7 @@ bool toxav_audio_send_frame(ToxAV *av, uint32_t friend_number, const int16_t *pc Toxav_Err_Send_Frame rc = TOXAV_ERR_SEND_FRAME_OK; ToxAVCall *call; - if (m_friend_exists(av->m, friend_number) == 0) { + if (!m_friend_exists(av->m, friend_number)) { rc = TOXAV_ERR_SEND_FRAME_FRIEND_NOT_FOUND; goto RETURN; } @@ -929,7 +929,7 @@ bool toxav_video_send_frame(ToxAV *av, uint32_t friend_number, uint16_t width, u int vpx_encode_flags = 0; - if (m_friend_exists(av->m, friend_number) == 0) { + if (!m_friend_exists(av->m, friend_number)) { rc = TOXAV_ERR_SEND_FRAME_FRIEND_NOT_FOUND; goto RETURN; } @@ -1238,7 +1238,7 @@ static ToxAVCall *call_new(ToxAV *av, uint32_t friend_number, Toxav_Err_Call *er Toxav_Err_Call rc = TOXAV_ERR_CALL_OK; ToxAVCall *call = nullptr; - if (m_friend_exists(av->m, friend_number) == 0) { + if (!m_friend_exists(av->m, friend_number)) { rc = TOXAV_ERR_CALL_FRIEND_NOT_FOUND; goto RETURN; } diff --git a/toxcore/DHT.c b/toxcore/DHT.c index e26cdbadaa..31d94274e5 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -261,7 +261,7 @@ void get_shared_key(const Mono_Time *mono_time, Shared_Keys *shared_keys, uint8_ Shared_Key *const key = &shared_keys->keys[index]; if (key->stored) { - if (id_equal(public_key, key->public_key)) { + if (pk_equal(public_key, key->public_key)) { memcpy(shared_key, key->shared_key, CRYPTO_SHARED_KEY_SIZE); ++key->times_requested; key->time_last_requested = mono_time_get(mono_time); @@ -373,7 +373,7 @@ int handle_request(const uint8_t *self_public_key, const uint8_t *self_secret_ke return -1; } - if (!id_equal(packet + 1, self_public_key)) { + if (!pk_equal(packet + 1, self_public_key)) { return -1; } @@ -662,7 +662,7 @@ static uint32_t index_of_client_pk(const Client_data *array, uint32_t size, cons assert(size == 0 || array != nullptr); for (uint32_t i = 0; i < size; ++i) { - if (id_equal(array[i].public_key, pk)) { + if (pk_equal(array[i].public_key, pk)) { return i; } } @@ -676,7 +676,7 @@ static uint32_t index_of_friend_pk(const DHT_Friend *array, uint32_t size, const assert(size == 0 || array != nullptr); for (uint32_t i = 0; i < size; ++i) { - if (id_equal(array[i].public_key, pk)) { + if (pk_equal(array[i].public_key, pk)) { return i; } } @@ -690,7 +690,7 @@ static uint32_t index_of_node_pk(const Node_format *array, uint32_t size, const assert(size == 0 || array != nullptr); for (uint32_t i = 0; i < size; ++i) { - if (id_equal(array[i].public_key, pk)) { + if (pk_equal(array[i].public_key, pk)) { return i; } } @@ -762,8 +762,8 @@ static void update_client(const Logger *log, const Mono_Time *mono_time, int ind * return True(1) or False(0) */ non_null() -static int client_or_ip_port_in_list(const Logger *log, const Mono_Time *mono_time, Client_data *list, uint16_t length, - const uint8_t *public_key, const IP_Port *ip_port) +static bool client_or_ip_port_in_list(const Logger *log, const Mono_Time *mono_time, Client_data *list, uint16_t length, + const uint8_t *public_key, const IP_Port *ip_port) { const uint64_t temp_time = mono_time_get(mono_time); uint32_t index = index_of_client_pk(list, length, public_key); @@ -771,7 +771,7 @@ static int client_or_ip_port_in_list(const Logger *log, const Mono_Time *mono_ti /* if public_key is in list, find it and maybe overwrite ip_port */ if (index != UINT32_MAX) { update_client(log, mono_time, index, &list[index], ip_port); - return 1; + return true; } /* public_key not in list yet: see if we can find an identical ip_port, in @@ -782,7 +782,7 @@ static int client_or_ip_port_in_list(const Logger *log, const Mono_Time *mono_ti index = index_of_client_ip_port(list, length, ip_port); if (index == UINT32_MAX) { - return 0; + return false; } IPPTsPng *assoc; @@ -805,7 +805,7 @@ static int client_or_ip_port_in_list(const Logger *log, const Mono_Time *mono_ti /* kill the other address, if it was set */ const IPPTsPng empty_ipptspng = {{{{0}}}}; *assoc = empty_ipptspng; - return 1; + return true; } bool add_to_list(Node_format *nodes_list, uint32_t length, const uint8_t *pk, const IP_Port *ip_port, @@ -1059,7 +1059,7 @@ static bool replace_all(const DHT *dht, sort_client_list(list, dht->cur_time, length, comp_public_key); Client_data *const client = &list[0]; - id_copy(client->public_key, public_key); + pk_copy(client->public_key, public_key); update_client_with_reset(dht->mono_time, client, ip_port); return true; @@ -1069,11 +1069,11 @@ static bool replace_all(const DHT *dht, * * simulate is set to 1 if we want to check if a node can be added to the list without adding it. * - * return -1 on failure. - * return 0 on success. + * return false on failure. + * return true on success. */ non_null() -static int add_to_close(DHT *dht, const uint8_t *public_key, const IP_Port *ip_port, bool simulate) +static bool add_to_close(DHT *dht, const uint8_t *public_key, const IP_Port *ip_port, bool simulate) { unsigned int index = bit_by_bit_cmp(public_key, dht->self_public_key); @@ -1092,22 +1092,22 @@ static int add_to_close(DHT *dht, const uint8_t *public_key, const IP_Port *ip_p } if (simulate) { - return 0; + return true; } - id_copy(client->public_key, public_key); + pk_copy(client->public_key, public_key); update_client_with_reset(dht->mono_time, client, ip_port); - return 0; + return true; } - return -1; + return false; } /** Return 1 if node can be added to close list, 0 if it can't. */ bool node_addable_to_close_list(DHT *dht, const uint8_t *public_key, const IP_Port *ip_port) { - return add_to_close(dht, public_key, ip_port, 1) == 0; + return add_to_close(dht, public_key, ip_port, true); } non_null() @@ -1151,7 +1151,7 @@ static bool ping_node_from_getnodes_ok(DHT *dht, const uint8_t *public_key, cons { bool ret = false; - if (add_to_close(dht, public_key, ip_port, 1) == 0) { + if (add_to_close(dht, public_key, ip_port, true)) { ret = true; } @@ -1225,7 +1225,7 @@ uint32_t addto_lists(DHT *dht, const IP_Port *ip_port, const uint8_t *public_key public_key, &ipp_copy); /* add_to_close should be called only if !in_list (don't extract to variable) */ - if (in_close_list || add_to_close(dht, public_key, &ipp_copy, 0)) { + if (in_close_list || !add_to_close(dht, public_key, &ipp_copy, false)) { ++used; } @@ -1241,7 +1241,7 @@ uint32_t addto_lists(DHT *dht, const IP_Port *ip_port, const uint8_t *public_key dht->friends_list[i].public_key)) { DHT_Friend *dht_friend = &dht->friends_list[i]; - if (id_equal(public_key, dht_friend->public_key)) { + if (pk_equal(public_key, dht_friend->public_key)) { friend_foundip = dht_friend; } @@ -1300,13 +1300,13 @@ static void returnedip_ports(DHT *dht, const IP_Port *ip_port, const uint8_t *pu { IP_Port ipp_copy = ip_port_normalize(ip_port); - if (id_equal(public_key, dht->self_public_key)) { + if (pk_equal(public_key, dht->self_public_key)) { update_client_data(dht->mono_time, dht->close_clientlist, LCLIENT_LIST, &ipp_copy, nodepublic_key, true); return; } for (uint32_t i = 0; i < dht->num_friends; ++i) { - if (id_equal(public_key, dht->friends_list[i].public_key)) { + if (pk_equal(public_key, dht->friends_list[i].public_key)) { Client_data *const client_list = dht->friends_list[i].client_list; if (update_client_data(dht->mono_time, client_list, MAX_FRIEND_CLIENTS, &ipp_copy, nodepublic_key, false)) { @@ -1319,7 +1319,7 @@ static void returnedip_ports(DHT *dht, const IP_Port *ip_port, const uint8_t *pu bool dht_getnodes(DHT *dht, const IP_Port *ip_port, const uint8_t *public_key, const uint8_t *client_id) { /* Check if packet is going to be sent to ourself. */ - if (id_equal(public_key, dht->self_public_key)) { + if (pk_equal(public_key, dht->self_public_key)) { return false; } @@ -1370,7 +1370,7 @@ static int sendnodes_ipv6(const DHT *dht, const IP_Port *ip_port, const uint8_t const uint8_t *sendback_data, uint16_t length, const uint8_t *shared_encryption_key) { /* Check if packet is going to be sent to ourself. */ - if (id_equal(public_key, dht->self_public_key)) { + if (pk_equal(public_key, dht->self_public_key)) { return -1; } @@ -1424,7 +1424,7 @@ static int handle_getnodes(void *object, const IP_Port *source, const uint8_t *p DHT *const dht = (DHT *)object; /* Check if packet is from ourself. */ - if (id_equal(packet + 1, dht->self_public_key)) { + if (pk_equal(packet + 1, dht->self_public_key)) { return true; } @@ -1469,7 +1469,7 @@ static bool sent_getnode_to_node(DHT *dht, const uint8_t *public_key, const IP_P return false; } - return ipport_equal(&test.ip_port, node_ip_port) && id_equal(test.public_key, public_key); + return ipport_equal(&test.ip_port, node_ip_port) && pk_equal(test.public_key, public_key); } non_null() @@ -1693,7 +1693,7 @@ int dht_getfriendip(const DHT *dht, const uint8_t *public_key, IP_Port *ip_port) const Client_data *const client = &frnd->client_list[client_index]; const IPPTsPng *const assocs[] = { &client->assoc6, &client->assoc4, nullptr }; - for (const IPPTsPng * const *it = assocs; *it; ++it) { + for (const IPPTsPng * const *it = assocs; *it != nullptr; ++it) { const IPPTsPng *const assoc = *it; if (!assoc_timeout(dht->cur_time, assoc)) { @@ -1837,7 +1837,7 @@ static void do_Close(DHT *dht) IPPTsPng *const assocs[] = { &client->assoc6, &client->assoc4, nullptr }; - for (IPPTsPng * const *it = assocs; *it; ++it) { + for (IPPTsPng * const *it = assocs; *it != nullptr; ++it) { IPPTsPng *const assoc = *it; if (assoc->timestamp != 0) { @@ -1849,7 +1849,7 @@ static void do_Close(DHT *dht) bool dht_bootstrap(DHT *dht, const IP_Port *ip_port, const uint8_t *public_key) { - if (id_equal(public_key, dht->self_public_key)) { + if (pk_equal(public_key, dht->self_public_key)) { // Bootstrapping off ourselves is ok (onion paths are still set up). return true; } @@ -1894,11 +1894,11 @@ int dht_bootstrap_from_address(DHT *dht, const char *address, bool ipv6enabled, int route_packet(const DHT *dht, const uint8_t *public_key, const uint8_t *packet, uint16_t length) { for (uint32_t i = 0; i < LCLIENT_LIST; ++i) { - if (id_equal(public_key, dht->close_clientlist[i].public_key)) { + if (pk_equal(public_key, dht->close_clientlist[i].public_key)) { const Client_data *const client = &dht->close_clientlist[i]; const IPPTsPng *const assocs[] = { &client->assoc6, &client->assoc4, nullptr }; - for (const IPPTsPng * const *it = assocs; *it; ++it) { + for (const IPPTsPng * const *it = assocs; *it != nullptr; ++it) { const IPPTsPng *const assoc = *it; if (ip_isset(&assoc->ip_port.ip)) { @@ -1949,7 +1949,7 @@ static int friend_iplist(const DHT *dht, IP_Port *ip_portlist, uint16_t friend_n ++num_ipv6s; } - if (id_equal(client->public_key, dht_friend->public_key)) { + if (pk_equal(client->public_key, dht_friend->public_key)) { if (!assoc_timeout(dht->cur_time, &client->assoc6) || !assoc_timeout(dht->cur_time, &client->assoc4)) { return 0; /* direct connectivity */ @@ -2468,7 +2468,7 @@ static int cryptopacket_handle(void *object, const IP_Port *source, const uint8_ } // Check if request is for us. - if (id_equal(packet + 1, dht->self_public_key)) { + if (pk_equal(packet + 1, dht->self_public_key)) { uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; uint8_t data[MAX_CRYPTO_REQUEST_SIZE]; uint8_t number; diff --git a/toxcore/LAN_discovery.c b/toxcore/LAN_discovery.c index 82f0dd5f7c..eb88fdb1ab 100644 --- a/toxcore/LAN_discovery.c +++ b/toxcore/LAN_discovery.c @@ -359,7 +359,7 @@ bool lan_discovery_send(Networking_Core *net, const Broadcast_Info *broadcast, c uint8_t data[CRYPTO_PUBLIC_KEY_SIZE + 1]; data[0] = NET_PACKET_LAN_DISCOVERY; - id_copy(data + 1, dht_pk); + pk_copy(data + 1, dht_pk); send_broadcasts(net, broadcast, port, data, 1 + CRYPTO_PUBLIC_KEY_SIZE); diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 2244a8f0ee..0f11326b22 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -23,17 +23,6 @@ static_assert(MAX_CONCURRENT_FILE_PIPES <= UINT8_MAX + 1, "uint8_t cannot represent all file transfer numbers"); -/** - * Determines if the friendnumber passed is valid in the Messenger object. - * - * @param friendnumber The index in the friend list. - */ -non_null() -static bool friend_is_valid(const Messenger *m, int32_t friendnumber) -{ - return (unsigned int)friendnumber < m->numfriends && m->friendlist[friendnumber].status != 0; -} - /** Set the size of the friend list to numfriends. * * return -1 if realloc fails. @@ -64,7 +53,7 @@ int32_t getfriend_id(const Messenger *m, const uint8_t *real_pk) { for (uint32_t i = 0; i < m->numfriends; ++i) { if (m->friendlist[i].status > 0) { - if (id_equal(real_pk, m->friendlist[i].real_pk)) { + if (pk_equal(real_pk, m->friendlist[i].real_pk)) { return i; } } @@ -81,7 +70,7 @@ int32_t getfriend_id(const Messenger *m, const uint8_t *real_pk) */ int get_real_pk(const Messenger *m, int32_t friendnumber, uint8_t *real_pk) { - if (!friend_is_valid(m, friendnumber)) { + if (!m_friend_exists(m, friendnumber)) { return -1; } @@ -94,7 +83,7 @@ int get_real_pk(const Messenger *m, int32_t friendnumber, uint8_t *real_pk) */ int getfriendcon_id(const Messenger *m, int32_t friendnumber) { - if (!friend_is_valid(m, friendnumber)) { + if (!m_friend_exists(m, friendnumber)) { return -1; } @@ -124,7 +113,7 @@ static uint16_t address_checksum(const uint8_t *address, uint32_t len) */ void getaddress(const Messenger *m, uint8_t *address) { - id_copy(address, nc_get_self_public_key(m->net_crypto)); + pk_copy(address, nc_get_self_public_key(m->net_crypto)); uint32_t nospam = get_nospam(m->fr); memcpy(address + CRYPTO_PUBLIC_KEY_SIZE, &nospam, sizeof(nospam)); uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum)); @@ -134,7 +123,7 @@ void getaddress(const Messenger *m, uint8_t *address) non_null() static int send_online_packet(Messenger *m, int32_t friendnumber) { - if (!friend_is_valid(m, friendnumber)) { + if (!m_friend_exists(m, friendnumber)) { return 0; } @@ -186,10 +175,10 @@ static int32_t init_new_friend(Messenger *m, const uint8_t *real_pk, uint8_t sta m->friendlist[i].status = status; m->friendlist[i].friendcon_id = friendcon_id; m->friendlist[i].friendrequest_lastsent = 0; - id_copy(m->friendlist[i].real_pk, real_pk); + pk_copy(m->friendlist[i].real_pk, real_pk); m->friendlist[i].statusmessage_length = 0; m->friendlist[i].userstatus = USERSTATUS_NONE; - m->friendlist[i].is_typing = 0; + m->friendlist[i].is_typing = false; m->friendlist[i].message_id = 0; friend_connection_callbacks(m->fr_c, friendcon_id, MESSENGER_CALLBACK_INDEX, &m_handle_status, &m_handle_packet, &m_handle_lossy_packet, m, i); @@ -237,7 +226,7 @@ int32_t m_addfriend(Messenger *m, const uint8_t *address, const uint8_t *data, u } uint8_t real_pk[CRYPTO_PUBLIC_KEY_SIZE]; - id_copy(real_pk, address); + pk_copy(real_pk, address); if (!public_key_valid(real_pk)) { return FAERR_BADCHECKSUM; @@ -255,7 +244,7 @@ int32_t m_addfriend(Messenger *m, const uint8_t *address, const uint8_t *data, u return FAERR_NOMESSAGE; } - if (id_equal(real_pk, nc_get_self_public_key(m->net_crypto))) { + if (pk_equal(real_pk, nc_get_self_public_key(m->net_crypto))) { return FAERR_OWNKEY; } @@ -301,7 +290,7 @@ int32_t m_addfriend_norequest(Messenger *m, const uint8_t *real_pk) return FAERR_BADCHECKSUM; } - if (id_equal(real_pk, nc_get_self_public_key(m->net_crypto))) { + if (pk_equal(real_pk, nc_get_self_public_key(m->net_crypto))) { return FAERR_OWNKEY; } @@ -311,7 +300,7 @@ int32_t m_addfriend_norequest(Messenger *m, const uint8_t *real_pk) non_null() static int clear_receipts(Messenger *m, int32_t friendnumber) { - if (!friend_is_valid(m, friendnumber)) { + if (!m_friend_exists(m, friendnumber)) { return -1; } @@ -331,7 +320,7 @@ static int clear_receipts(Messenger *m, int32_t friendnumber) non_null() static int add_receipt(Messenger *m, int32_t friendnumber, uint32_t packet_num, uint32_t msg_id) { - if (!friend_is_valid(m, friendnumber)) { + if (!m_friend_exists(m, friendnumber)) { return -1; } @@ -361,7 +350,7 @@ static int add_receipt(Messenger *m, int32_t friendnumber, uint32_t packet_num, non_null() static int friend_received_packet(const Messenger *m, int32_t friendnumber, uint32_t number) { - if (!friend_is_valid(m, friendnumber)) { + if (!m_friend_exists(m, friendnumber)) { return -1; } @@ -372,7 +361,7 @@ static int friend_received_packet(const Messenger *m, int32_t friendnumber, uint non_null(1) nullable(3) static int do_receipts(Messenger *m, int32_t friendnumber, void *userdata) { - if (!friend_is_valid(m, friendnumber)) { + if (!m_friend_exists(m, friendnumber)) { return -1; } @@ -410,7 +399,7 @@ static int do_receipts(Messenger *m, int32_t friendnumber, void *userdata) */ int m_delfriend(Messenger *m, int32_t friendnumber) { - if (!friend_is_valid(m, friendnumber)) { + if (!m_friend_exists(m, friendnumber)) { return -1; } @@ -449,7 +438,7 @@ int m_delfriend(Messenger *m, int32_t friendnumber) int m_get_friend_connectionstatus(const Messenger *m, int32_t friendnumber) { - if (!friend_is_valid(m, friendnumber)) { + if (!m_friend_exists(m, friendnumber)) { return -1; } @@ -457,7 +446,7 @@ int m_get_friend_connectionstatus(const Messenger *m, int32_t friendnumber) return CONNECTION_NONE; } - bool direct_connected = 0; + bool direct_connected = false; uint32_t num_online_relays = 0; const int crypt_conn_id = friend_connection_crypt_connection_id(m->fr_c, m->friendlist[friendnumber].friendcon_id); @@ -480,13 +469,17 @@ int m_get_friend_connectionstatus(const Messenger *m, int32_t friendnumber) return m->friendlist[friendnumber].last_connection_udp_tcp; } -int m_friend_exists(const Messenger *m, int32_t friendnumber) +/** + * Checks if there exists a friend with given friendnumber. + * + * @param friendnumber The index in the friend list. + * + * @retval true if friend exists. + * @retval false if friend doesn't exist. + */ +bool m_friend_exists(const Messenger *m, int32_t friendnumber) { - if (!friend_is_valid(m, friendnumber)) { - return 0; - } - - return 1; + return (unsigned int)friendnumber < m->numfriends && m->friendlist[friendnumber].status != 0; } /** Send a message of type to an online friend. @@ -508,7 +501,7 @@ int m_send_message_generic(Messenger *m, int32_t friendnumber, uint8_t type, con return -5; } - if (!friend_is_valid(m, friendnumber)) { + if (!m_friend_exists(m, friendnumber)) { LOGGER_WARNING(m->log, "friend number %d is invalid", friendnumber); return -1; } @@ -551,7 +544,7 @@ non_null() static int write_cryptpacket_id(const Messenger *m, int32_t friendnumber, uint8_t packet_id, const uint8_t *data, uint32_t length, bool congestion_control) { - if (!friend_is_valid(m, friendnumber)) { + if (!m_friend_exists(m, friendnumber)) { return 0; } @@ -592,7 +585,7 @@ static int m_sendname(const Messenger *m, int32_t friendnumber, const uint8_t *n */ int setfriendname(Messenger *m, int32_t friendnumber, const uint8_t *name, uint16_t length) { - if (!friend_is_valid(m, friendnumber)) { + if (!m_friend_exists(m, friendnumber)) { return -1; } @@ -630,7 +623,7 @@ int setname(Messenger *m, const uint8_t *name, uint16_t length) m->name_length = length; for (uint32_t i = 0; i < m->numfriends; ++i) { - m->friendlist[i].name_sent = 0; + m->friendlist[i].name_sent = false; } return 0; @@ -663,7 +656,7 @@ uint16_t getself_name(const Messenger *m, uint8_t *name) */ int getname(const Messenger *m, int32_t friendnumber, uint8_t *name) { - if (!friend_is_valid(m, friendnumber)) { + if (!m_friend_exists(m, friendnumber)) { return -1; } @@ -673,7 +666,7 @@ int getname(const Messenger *m, int32_t friendnumber, uint8_t *name) int m_get_name_size(const Messenger *m, int32_t friendnumber) { - if (!friend_is_valid(m, friendnumber)) { + if (!m_friend_exists(m, friendnumber)) { return -1; } @@ -702,7 +695,7 @@ int m_set_statusmessage(Messenger *m, const uint8_t *status, uint16_t length) m->statusmessage_length = length; for (uint32_t i = 0; i < m->numfriends; ++i) { - m->friendlist[i].statusmessage_sent = 0; + m->friendlist[i].statusmessage_sent = false; } return 0; @@ -721,7 +714,7 @@ int m_set_userstatus(Messenger *m, uint8_t status) m->userstatus = (Userstatus)status; for (uint32_t i = 0; i < m->numfriends; ++i) { - m->friendlist[i].userstatus_sent = 0; + m->friendlist[i].userstatus_sent = false; } return 0; @@ -735,7 +728,7 @@ int m_set_userstatus(Messenger *m, uint8_t status) */ int m_get_statusmessage_size(const Messenger *m, int32_t friendnumber) { - if (!friend_is_valid(m, friendnumber)) { + if (!m_friend_exists(m, friendnumber)) { return -1; } @@ -751,7 +744,7 @@ int m_get_statusmessage_size(const Messenger *m, int32_t friendnumber) */ int m_copy_statusmessage(const Messenger *m, int32_t friendnumber, uint8_t *buf, uint32_t maxlen) { - if (!friend_is_valid(m, friendnumber)) { + if (!m_friend_exists(m, friendnumber)) { return -1; } @@ -780,7 +773,7 @@ int m_copy_self_statusmessage(const Messenger *m, uint8_t *buf) uint8_t m_get_userstatus(const Messenger *m, int32_t friendnumber) { - if (!friend_is_valid(m, friendnumber)) { + if (!m_friend_exists(m, friendnumber)) { return USERSTATUS_INVALID; } @@ -800,20 +793,16 @@ uint8_t m_get_self_userstatus(const Messenger *m) uint64_t m_get_last_online(const Messenger *m, int32_t friendnumber) { - if (!friend_is_valid(m, friendnumber)) { + if (!m_friend_exists(m, friendnumber)) { return UINT64_MAX; } return m->friendlist[friendnumber].last_seen_time; } -int m_set_usertyping(Messenger *m, int32_t friendnumber, uint8_t is_typing) +int m_set_usertyping(Messenger *m, int32_t friendnumber, bool is_typing) { - if (is_typing != 0 && is_typing != 1) { - return -1; - } - - if (!friend_is_valid(m, friendnumber)) { + if (!m_friend_exists(m, friendnumber)) { return -1; } @@ -822,18 +811,18 @@ int m_set_usertyping(Messenger *m, int32_t friendnumber, uint8_t is_typing) } m->friendlist[friendnumber].user_istyping = is_typing; - m->friendlist[friendnumber].user_istyping_sent = 0; + m->friendlist[friendnumber].user_istyping_sent = false; return 0; } int m_get_istyping(const Messenger *m, int32_t friendnumber) { - if (!friend_is_valid(m, friendnumber)) { + if (!m_friend_exists(m, friendnumber)) { return -1; } - return m->friendlist[friendnumber].is_typing; + return m->friendlist[friendnumber].is_typing ? 1 : 0; } non_null() @@ -849,16 +838,16 @@ static int send_userstatus(const Messenger *m, int32_t friendnumber, uint8_t sta } non_null() -static int send_user_istyping(const Messenger *m, int32_t friendnumber, uint8_t is_typing) +static int send_user_istyping(const Messenger *m, int32_t friendnumber, bool is_typing) { - uint8_t typing = is_typing; + const uint8_t typing = is_typing ? 1 : 0; return write_cryptpacket_id(m, friendnumber, PACKET_ID_TYPING, &typing, sizeof(typing), false); } non_null() static int set_friend_statusmessage(const Messenger *m, int32_t friendnumber, const uint8_t *status, uint16_t length) { - if (!friend_is_valid(m, friendnumber)) { + if (!m_friend_exists(m, friendnumber)) { return -1; } @@ -881,7 +870,7 @@ static void set_friend_userstatus(const Messenger *m, int32_t friendnumber, uint } non_null() -static void set_friend_typing(const Messenger *m, int32_t friendnumber, uint8_t is_typing) +static void set_friend_typing(const Messenger *m, int32_t friendnumber, bool is_typing) { m->friendlist[friendnumber].is_typing = is_typing; } @@ -978,10 +967,10 @@ static void check_friend_connectionstatus(Messenger *m, int32_t friendnumber, ui break_files(m, friendnumber); clear_receipts(m, friendnumber); } else { - m->friendlist[friendnumber].name_sent = 0; - m->friendlist[friendnumber].userstatus_sent = 0; - m->friendlist[friendnumber].statusmessage_sent = 0; - m->friendlist[friendnumber].user_istyping_sent = 0; + m->friendlist[friendnumber].name_sent = false; + m->friendlist[friendnumber].userstatus_sent = false; + m->friendlist[friendnumber].statusmessage_sent = false; + m->friendlist[friendnumber].user_istyping_sent = false; } m->friendlist[friendnumber].status = status; @@ -1015,12 +1004,12 @@ void m_callback_conference_invite(Messenger *m, m_conference_invite_cb *function /** Send a conference invite packet. * - * return 1 on success - * return 0 on failure + * return true on success + * return false on failure */ -int send_conference_invite_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data, uint16_t length) +bool send_conference_invite_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data, uint16_t length) { - return write_cryptpacket_id(m, friendnumber, PACKET_ID_INVITE_CONFERENCE, data, length, false); + return write_cryptpacket_id(m, friendnumber, PACKET_ID_INVITE_CONFERENCE, data, length, false) != 0; } /*** FILE SENDING */ @@ -1064,7 +1053,7 @@ void callback_file_reqchunk(Messenger *m, m_file_chunk_request_cb *function) */ int file_get_id(const Messenger *m, int32_t friendnumber, uint32_t filenumber, uint8_t *file_id) { - if (!friend_is_valid(m, friendnumber)) { + if (!m_friend_exists(m, friendnumber)) { return -1; } @@ -1115,7 +1104,7 @@ non_null() static int file_sendrequest(const Messenger *m, int32_t friendnumber, uint8_t filenumber, uint32_t file_type, uint64_t filesize, const uint8_t *file_id, const uint8_t *filename, uint16_t filename_length) { - if (!friend_is_valid(m, friendnumber)) { + if (!m_friend_exists(m, friendnumber)) { return 0; } @@ -1149,7 +1138,7 @@ static int file_sendrequest(const Messenger *m, int32_t friendnumber, uint8_t fi long int new_filesender(const Messenger *m, int32_t friendnumber, uint32_t file_type, uint64_t filesize, const uint8_t *file_id, const uint8_t *filename, uint16_t filename_length) { - if (!friend_is_valid(m, friendnumber)) { + if (!m_friend_exists(m, friendnumber)) { return -1; } @@ -1227,7 +1216,7 @@ static int send_file_control_packet(const Messenger *m, int32_t friendnumber, bo */ int file_control(const Messenger *m, int32_t friendnumber, uint32_t filenumber, unsigned int control) { - if (!friend_is_valid(m, friendnumber)) { + if (!m_friend_exists(m, friendnumber)) { return -1; } @@ -1330,7 +1319,7 @@ int file_control(const Messenger *m, int32_t friendnumber, uint32_t filenumber, */ int file_seek(const Messenger *m, int32_t friendnumber, uint32_t filenumber, uint64_t position) { - if (!friend_is_valid(m, friendnumber)) { + if (!m_friend_exists(m, friendnumber)) { return -1; } @@ -1389,7 +1378,7 @@ static int64_t send_file_data_packet(const Messenger *m, int32_t friendnumber, u { assert(length == 0 || data != nullptr); - if (!friend_is_valid(m, friendnumber)) { + if (!m_friend_exists(m, friendnumber)) { return -1; } @@ -1423,7 +1412,7 @@ int send_file_data(const Messenger *m, int32_t friendnumber, uint32_t filenumber { assert(length == 0 || data != nullptr); - if (!friend_is_valid(m, friendnumber)) { + if (!m_friend_exists(m, friendnumber)) { return -1; } @@ -1770,7 +1759,7 @@ static int m_handle_lossy_packet(void *object, int friend_num, const uint8_t *pa { Messenger *m = (Messenger *)object; - if (!friend_is_valid(m, friend_num)) { + if (!m_friend_exists(m, friend_num)) { return 1; } @@ -1800,7 +1789,7 @@ void custom_lossy_packet_registerhandler(Messenger *m, m_friend_lossy_packet_cb int m_callback_rtp_packet(Messenger *m, int32_t friendnumber, uint8_t byte, m_lossy_rtp_packet_cb *function, void *object) { - if (!friend_is_valid(m, friendnumber)) { + if (!m_friend_exists(m, friendnumber)) { return -1; } @@ -1832,7 +1821,7 @@ int m_callback_rtp_packet(Messenger *m, int32_t friendnumber, uint8_t byte, m_lo */ int m_send_custom_lossy_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data, uint32_t length) { - if (!friend_is_valid(m, friendnumber)) { + if (!m_friend_exists(m, friendnumber)) { return -1; } @@ -1863,7 +1852,7 @@ static int handle_custom_lossless_packet(void *object, int friend_num, const uin { Messenger *m = (Messenger *)object; - if (!friend_is_valid(m, friend_num)) { + if (!m_friend_exists(m, friend_num)) { return -1; } @@ -1885,7 +1874,7 @@ void custom_lossless_packet_registerhandler(Messenger *m, m_friend_lossless_pack int send_custom_lossless_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data, uint32_t length) { - if (!friend_is_valid(m, friendnumber)) { + if (!m_friend_exists(m, friendnumber)) { return -1; } @@ -2300,27 +2289,27 @@ static void do_friends(Messenger *m, void *userdata) } if (m->friendlist[i].status == FRIEND_ONLINE) { /* friend is online. */ - if (m->friendlist[i].name_sent == 0) { + if (!m->friendlist[i].name_sent) { if (m_sendname(m, i, m->name, m->name_length)) { - m->friendlist[i].name_sent = 1; + m->friendlist[i].name_sent = true; } } - if (m->friendlist[i].statusmessage_sent == 0) { + if (!m->friendlist[i].statusmessage_sent) { if (send_statusmessage(m, i, m->statusmessage, m->statusmessage_length)) { - m->friendlist[i].statusmessage_sent = 1; + m->friendlist[i].statusmessage_sent = true; } } - if (m->friendlist[i].userstatus_sent == 0) { + if (!m->friendlist[i].userstatus_sent) { if (send_userstatus(m, i, m->userstatus)) { - m->friendlist[i].userstatus_sent = 1; + m->friendlist[i].userstatus_sent = true; } } - if (m->friendlist[i].user_istyping_sent == 0) { + if (!m->friendlist[i].user_istyping_sent) { if (send_user_istyping(m, i, m->friendlist[i].user_istyping)) { - m->friendlist[i].user_istyping_sent = 1; + m->friendlist[i].user_istyping_sent = true; } } @@ -2364,7 +2353,7 @@ static char *id_to_string(const uint8_t *pk, char *id_str, size_t length) snprintf(&id_str[i * 2], length - i * 2, "%02X", pk[i]); } - id_str[CRYPTO_PUBLIC_KEY_SIZE * 2] = 0; + id_str[CRYPTO_PUBLIC_KEY_SIZE * 2] = '\0'; return id_str; } @@ -2434,7 +2423,7 @@ void do_messenger(Messenger *m, void *userdata) const Client_data *cptr = dht_get_close_client(m->dht, client); const IPPTsPng *const assocs[] = { &cptr->assoc4, &cptr->assoc6, nullptr }; - for (const IPPTsPng * const *it = assocs; *it; ++it) { + for (const IPPTsPng * const *it = assocs; *it != nullptr; ++it) { const IPPTsPng *const assoc = *it; if (ip_isset(&assoc->ip_port.ip)) { @@ -2469,7 +2458,7 @@ void do_messenger(Messenger *m, void *userdata) } for (uint32_t dhtfriend = 0; dhtfriend < dht_get_num_friends(m->dht); ++dhtfriend) { - if (id_equal(m->friendlist[friend_idx].real_pk, dht_get_friend_public_key(m->dht, dhtfriend))) { + if (pk_equal(m->friendlist[friend_idx].real_pk, dht_get_friend_public_key(m->dht, dhtfriend))) { assert(dhtfriend < INT32_MAX); m2dht[friend_idx] = (int32_t)dhtfriend; break; @@ -2758,7 +2747,7 @@ static State_Load_Status load_nospam_keys(Messenger *m, const uint8_t *data, uin set_nospam(m->fr, nospam); load_secret_key(m->net_crypto, data + sizeof(uint32_t) + CRYPTO_PUBLIC_KEY_SIZE); - if (public_key_cmp(data + sizeof(uint32_t), nc_get_self_public_key(m->net_crypto)) != 0) { + if (!pk_equal(data + sizeof(uint32_t), nc_get_self_public_key(m->net_crypto))) { return STATE_LOAD_STATUS_ERROR; } @@ -2893,7 +2882,7 @@ static State_Load_Status friends_list_load(Messenger *m, const uint8_t *data, ui } else if (temp.status != 0) { /* TODO(irungentoo): This is not a good way to do this. */ uint8_t address[FRIEND_ADDRESS_SIZE]; - id_copy(address, temp.real_pk); + pk_copy(address, temp.real_pk); memcpy(address + CRYPTO_PUBLIC_KEY_SIZE, &temp.friendrequest_nospam, sizeof(uint32_t)); uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum)); memcpy(address + CRYPTO_PUBLIC_KEY_SIZE + sizeof(uint32_t), &checksum, sizeof(checksum)); diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index eff3a6d497..ca5f5eb013 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -207,15 +207,15 @@ typedef struct Friend { uint8_t info[MAX_FRIEND_REQUEST_DATA_SIZE]; // the data that is sent during the friend requests we do. uint8_t name[MAX_NAME_LENGTH]; uint16_t name_length; - uint8_t name_sent; // 0 if we didn't send our name to this friend 1 if we have. + bool name_sent; // false if we didn't send our name to this friend, true if we have. uint8_t statusmessage[MAX_STATUSMESSAGE_LENGTH]; uint16_t statusmessage_length; - uint8_t statusmessage_sent; + bool statusmessage_sent; Userstatus userstatus; - uint8_t userstatus_sent; - uint8_t user_istyping; - uint8_t user_istyping_sent; - uint8_t is_typing; + bool userstatus_sent; + bool user_istyping; + bool user_istyping_sent; + bool is_typing; uint16_t info_size; // Length of the info. uint32_t message_id; // a semi-unique id used in read receipts. uint32_t friendrequest_nospam; // The nospam number used in the friend request. @@ -378,13 +378,16 @@ int m_delfriend(Messenger *m, int32_t friendnumber); non_null() int m_get_friend_connectionstatus(const Messenger *m, int32_t friendnumber); -/** Checks if there exists a friend with given friendnumber. +/** + * Checks if there exists a friend with given friendnumber. + * + * @param friendnumber The index in the friend list. * - * return 1 if friend exists. - * return 0 if friend doesn't exist. + * @retval true if friend exists. + * @retval false if friend doesn't exist. */ non_null() -int m_friend_exists(const Messenger *m, int32_t friendnumber); +bool m_friend_exists(const Messenger *m, int32_t friendnumber); /** Send a message of type to an online friend. * @@ -499,10 +502,11 @@ non_null() uint64_t m_get_last_online(const Messenger *m, int32_t friendnumber); * returns -1 on failure. */ non_null() -int m_set_usertyping(Messenger *m, int32_t friendnumber, uint8_t is_typing); +int m_set_usertyping(Messenger *m, int32_t friendnumber, bool is_typing); /** Get the typing status of a friend. * + * returns -1 if friend number is invalid. * returns 0 if friend is not typing. * returns 1 if friend is typing. */ @@ -575,11 +579,11 @@ void m_callback_conference_invite(Messenger *m, m_conference_invite_cb *function /** Send a conference invite packet. * - * return 1 on success - * return 0 on failure + * return true on success + * return false on failure */ non_null() -int send_conference_invite_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data, uint16_t length); +bool send_conference_invite_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data, uint16_t length); /*** FILE SENDING */ diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c index 1ff01d7f30..f2325ce02f 100644 --- a/toxcore/TCP_client.c +++ b/toxcore/TCP_client.c @@ -92,11 +92,11 @@ void tcp_con_set_custom_uint(TCP_Client_Connection *con, uint32_t value) con->custom_uint = value; } -/** return 1 on success - * return 0 on failure +/** return true on success + * return false on failure */ non_null() -static int connect_sock_to(const Logger *logger, Socket sock, const IP_Port *ip_port, const TCP_Proxy_Info *proxy_info) +static bool connect_sock_to(const Logger *logger, Socket sock, const IP_Port *ip_port, const TCP_Proxy_Info *proxy_info) { IP_Port ipp_copy = *ip_port; @@ -107,7 +107,7 @@ static int connect_sock_to(const Logger *logger, Socket sock, const IP_Port *ip_ /* nonblocking socket, connect will never return success */ net_connect(logger, sock, &ipp_copy); - return 1; + return true; } /** return 1 on success. @@ -329,7 +329,7 @@ int send_routing_request(const Logger *logger, TCP_Client_Connection *con, const uint8_t packet[1 + CRYPTO_PUBLIC_KEY_SIZE]; packet[0] = TCP_PACKET_ROUTING_REQUEST; memcpy(packet + 1, public_key, CRYPTO_PUBLIC_KEY_SIZE); - return write_packet_TCP_secure_connection(logger, &con->con, packet, sizeof(packet), 1); + return write_packet_TCP_secure_connection(logger, &con->con, packet, sizeof(packet), true); } void routing_response_handler(TCP_Client_Connection *con, tcp_routing_response_cb *response_callback, void *object) @@ -368,7 +368,7 @@ int send_data(const Logger *logger, TCP_Client_Connection *con, uint8_t con_id, VLA(uint8_t, packet, 1 + length); packet[0] = con_id + NUM_RESERVED_PORTS; memcpy(packet + 1, data, length); - return write_packet_TCP_secure_connection(logger, &con->con, packet, SIZEOF_VLA(packet), 0); + return write_packet_TCP_secure_connection(logger, &con->con, packet, SIZEOF_VLA(packet), false); } /** return 1 on success. @@ -386,7 +386,7 @@ int send_oob_packet(const Logger *logger, TCP_Client_Connection *con, const uint packet[0] = TCP_PACKET_OOB_SEND; memcpy(packet + 1, public_key, CRYPTO_PUBLIC_KEY_SIZE); memcpy(packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, data, length); - return write_packet_TCP_secure_connection(logger, &con->con, packet, SIZEOF_VLA(packet), 0); + return write_packet_TCP_secure_connection(logger, &con->con, packet, SIZEOF_VLA(packet), false); } @@ -433,7 +433,7 @@ static int client_send_disconnect_notification(const Logger *logger, TCP_Client_ uint8_t packet[1 + 1]; packet[0] = TCP_PACKET_DISCONNECT_NOTIFICATION; packet[1] = id; - return write_packet_TCP_secure_connection(logger, &con->con, packet, sizeof(packet), 1); + return write_packet_TCP_secure_connection(logger, &con->con, packet, sizeof(packet), true); } /** return 1 on success. @@ -449,7 +449,7 @@ static int tcp_send_ping_request(const Logger *logger, TCP_Client_Connection *co uint8_t packet[1 + sizeof(uint64_t)]; packet[0] = TCP_PACKET_PING; memcpy(packet + 1, &con->ping_request_id, sizeof(uint64_t)); - const int ret = write_packet_TCP_secure_connection(logger, &con->con, packet, sizeof(packet), 1); + const int ret = write_packet_TCP_secure_connection(logger, &con->con, packet, sizeof(packet), true); if (ret == 1) { con->ping_request_id = 0; @@ -471,7 +471,7 @@ static int tcp_send_ping_response(const Logger *logger, TCP_Client_Connection *c uint8_t packet[1 + sizeof(uint64_t)]; packet[0] = TCP_PACKET_PONG; memcpy(packet + 1, &con->ping_response_id, sizeof(uint64_t)); - const int ret = write_packet_TCP_secure_connection(logger, &con->con, packet, sizeof(packet), 1); + const int ret = write_packet_TCP_secure_connection(logger, &con->con, packet, sizeof(packet), true); if (ret == 1) { con->ping_response_id = 0; @@ -504,7 +504,7 @@ int send_onion_request(const Logger *logger, TCP_Client_Connection *con, const u VLA(uint8_t, packet, 1 + length); packet[0] = TCP_PACKET_ONION_REQUEST; memcpy(packet + 1, data, length); - return write_packet_TCP_secure_connection(logger, &con->con, packet, SIZEOF_VLA(packet), 0); + return write_packet_TCP_secure_connection(logger, &con->con, packet, SIZEOF_VLA(packet), false); } void onion_response_handler(TCP_Client_Connection *con, tcp_onion_response_cb *onion_callback, void *object) diff --git a/toxcore/TCP_common.c b/toxcore/TCP_common.c index 18a6c2dfeb..a0a634ce40 100644 --- a/toxcore/TCP_common.c +++ b/toxcore/TCP_common.c @@ -84,10 +84,11 @@ int send_pending_data(const Logger *logger, TCP_Connection *con) return -1; } -/** return 0 on failure (only if calloc fails) - * return 1 on success +/** return false on failure (only if calloc fails) + * return true on success */ -bool add_priority(TCP_Connection *con, const uint8_t *packet, uint16_t size, uint16_t sent) +non_null() +static bool add_priority(TCP_Connection *con, const uint8_t *packet, uint16_t size, uint16_t sent) { TCP_Priority_List *p = con->priority_queue_end; TCP_Priority_List *new_list = (TCP_Priority_List *)calloc(1, sizeof(TCP_Priority_List)); @@ -129,11 +130,11 @@ int write_packet_TCP_secure_connection(const Logger *logger, TCP_Connection *con return -1; } - bool sendpriority = 1; + bool sendpriority = true; if (send_pending_data(logger, con) == -1) { if (priority) { - sendpriority = 0; + sendpriority = false; } else { return 0; } @@ -162,7 +163,7 @@ int write_packet_TCP_secure_connection(const Logger *logger, TCP_Connection *con return 1; } - return add_priority(con, packet, SIZEOF_VLA(packet), len); + return add_priority(con, packet, SIZEOF_VLA(packet), len) ? 1 : 0; } len = net_send(logger, con->sock, packet, SIZEOF_VLA(packet), &con->ip_port); diff --git a/toxcore/TCP_common.h b/toxcore/TCP_common.h index 247e4bcef8..93d3563e04 100644 --- a/toxcore/TCP_common.h +++ b/toxcore/TCP_common.h @@ -70,12 +70,6 @@ int send_pending_data_nonpriority(const Logger *logger, TCP_Connection *con); non_null() int send_pending_data(const Logger *logger, TCP_Connection *con); -/** return 0 on failure (only if calloc fails) - * return 1 on success - */ -non_null() -bool add_priority(TCP_Connection *con, const uint8_t *packet, uint16_t size, uint16_t sent); - /** return 1 on success. * return 0 if could not send packet. * return -1 on failure (connection must be killed). diff --git a/toxcore/TCP_connection.c b/toxcore/TCP_connection.c index 0edec14333..ce268283a6 100644 --- a/toxcore/TCP_connection.c +++ b/toxcore/TCP_connection.c @@ -305,7 +305,7 @@ int send_packet_tcp_connection(const TCP_Connections *tcp_c, int connections_num // TODO(irungentoo): thread safety? int ret = -1; - bool limit_reached = 0; + bool limit_reached = false; for (unsigned int i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) { uint32_t tcp_con_num = con_to->connections[i].tcp_connection; @@ -323,7 +323,7 @@ int send_packet_tcp_connection(const TCP_Connections *tcp_c, int connections_num ret = send_data(tcp_c->logger, tcp_con->connection, connection_id, packet, length); if (ret == 0) { - limit_reached = 1; + limit_reached = true; } if (ret == 1) { @@ -498,7 +498,7 @@ static int find_tcp_connection_to(const TCP_Connections *tcp_c, const uint8_t *p const TCP_Connection_to *con_to = get_connection(tcp_c, i); if (con_to != nullptr) { - if (public_key_cmp(con_to->public_key, public_key) == 0) { + if (pk_equal(con_to->public_key, public_key)) { return i; } } @@ -519,11 +519,11 @@ static int find_tcp_connection_relay(const TCP_Connections *tcp_c, const uint8_t if (tcp_con != nullptr) { if (tcp_con->status == TCP_CONN_SLEEPING) { - if (public_key_cmp(tcp_con->relay_pk, relay_pk) == 0) { + if (pk_equal(tcp_con->relay_pk, relay_pk)) { return i; } } else { - if (public_key_cmp(tcp_con_public_key(tcp_con->connection), relay_pk) == 0) { + if (pk_equal(tcp_con_public_key(tcp_con->connection), relay_pk)) { return i; } } diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c index c8275b4c57..bb9d6d0666 100644 --- a/toxcore/TCP_server.c +++ b/toxcore/TCP_server.c @@ -387,7 +387,7 @@ static int send_routing_response(const Logger *logger, TCP_Secure_Connection *co data[1] = rpid; memcpy(data + 2, public_key, CRYPTO_PUBLIC_KEY_SIZE); - return write_packet_TCP_secure_connection(logger, &con->con, data, sizeof(data), 1); + return write_packet_TCP_secure_connection(logger, &con->con, data, sizeof(data), true); } /** return 1 on success. @@ -398,7 +398,7 @@ non_null() static int send_connect_notification(const Logger *logger, TCP_Secure_Connection *con, uint8_t id) { uint8_t data[2] = {TCP_PACKET_CONNECTION_NOTIFICATION, (uint8_t)(id + NUM_RESERVED_PORTS)}; - return write_packet_TCP_secure_connection(logger, &con->con, data, sizeof(data), 1); + return write_packet_TCP_secure_connection(logger, &con->con, data, sizeof(data), true); } /** return 1 on success. @@ -409,7 +409,7 @@ non_null() static int send_disconnect_notification(const Logger *logger, TCP_Secure_Connection *con, uint8_t id) { uint8_t data[2] = {TCP_PACKET_DISCONNECT_NOTIFICATION, (uint8_t)(id + NUM_RESERVED_PORTS)}; - return write_packet_TCP_secure_connection(logger, &con->con, data, sizeof(data), 1); + return write_packet_TCP_secure_connection(logger, &con->con, data, sizeof(data), true); } /** return 0 on success. @@ -422,7 +422,7 @@ static int handle_TCP_routing_req(TCP_Server *tcp_server, uint32_t con_id, const TCP_Secure_Connection *con = &tcp_server->accepted_connection_array[con_id]; /* If person tries to cennect to himself we deny the request*/ - if (public_key_cmp(con->public_key, public_key) == 0) { + if (pk_equal(con->public_key, public_key)) { if (send_routing_response(tcp_server->logger, con, 0, public_key) == -1) { return -1; } @@ -432,7 +432,7 @@ static int handle_TCP_routing_req(TCP_Server *tcp_server, uint32_t con_id, const for (uint32_t i = 0; i < NUM_CLIENT_CONNECTIONS; ++i) { if (con->connections[i].status != 0) { - if (public_key_cmp(public_key, con->connections[i].public_key) == 0) { + if (pk_equal(public_key, con->connections[i].public_key)) { if (send_routing_response(tcp_server->logger, con, i + NUM_RESERVED_PORTS, public_key) == -1) { return -1; } @@ -472,7 +472,7 @@ static int handle_TCP_routing_req(TCP_Server *tcp_server, uint32_t con_id, const for (uint32_t i = 0; i < NUM_CLIENT_CONNECTIONS; ++i) { if (other_conn->connections[i].status == 1 - && public_key_cmp(other_conn->connections[i].public_key, con->public_key) == 0) { + && pk_equal(other_conn->connections[i].public_key, con->public_key)) { other_id = i; break; } @@ -515,7 +515,7 @@ static int handle_TCP_oob_send(TCP_Server *tcp_server, uint32_t con_id, const ui memcpy(resp_packet + 1, con->public_key, CRYPTO_PUBLIC_KEY_SIZE); memcpy(resp_packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, data, length); write_packet_TCP_secure_connection(tcp_server->logger, &tcp_server->accepted_connection_array[other_index].con, - resp_packet, SIZEOF_VLA(resp_packet), 0); + resp_packet, SIZEOF_VLA(resp_packet), false); } return 0; @@ -577,7 +577,7 @@ static int handle_onion_recv_1(void *object, const IP_Port *dest, const uint8_t memcpy(packet + 1, data, length); packet[0] = TCP_PACKET_ONION_RESPONSE; - if (write_packet_TCP_secure_connection(tcp_server->logger, &con->con, packet, SIZEOF_VLA(packet), 0) != 1) { + if (write_packet_TCP_secure_connection(tcp_server->logger, &con->con, packet, SIZEOF_VLA(packet), false) != 1) { return 1; } @@ -634,7 +634,7 @@ static int handle_TCP_packet(TCP_Server *tcp_server, uint32_t con_id, const uint uint8_t response[1 + sizeof(uint64_t)]; response[0] = TCP_PACKET_PONG; memcpy(response + 1, data + 1, sizeof(uint64_t)); - write_packet_TCP_secure_connection(tcp_server->logger, &con->con, response, sizeof(response), 1); + write_packet_TCP_secure_connection(tcp_server->logger, &con->con, response, sizeof(response), true); return 0; } @@ -722,7 +722,7 @@ static int handle_TCP_packet(TCP_Server *tcp_server, uint32_t con_id, const uint memcpy(new_data, data, length); new_data[0] = other_c_id; const int ret = write_packet_TCP_secure_connection(tcp_server->logger, - &tcp_server->accepted_connection_array[index].con, new_data, length, 0); + &tcp_server->accepted_connection_array[index].con, new_data, length, false); if (ret == -1) { return -1; @@ -1081,7 +1081,7 @@ static void do_TCP_confirmed(TCP_Server *tcp_server, const Mono_Time *mono_time) } memcpy(ping + 1, &ping_id, sizeof(uint64_t)); - const int ret = write_packet_TCP_secure_connection(tcp_server->logger, &conn->con, ping, sizeof(ping), 1); + const int ret = write_packet_TCP_secure_connection(tcp_server->logger, &conn->con, ping, sizeof(ping), true); if (ret == 1) { conn->last_pinged = mono_time_get(mono_time); diff --git a/toxcore/crypto_core.c b/toxcore/crypto_core.c index 6102c32539..ebaa9fa0df 100644 --- a/toxcore/crypto_core.c +++ b/toxcore/crypto_core.c @@ -56,7 +56,7 @@ static_assert(CRYPTO_SHA256_SIZE == crypto_hash_sha256_BYTES, static_assert(CRYPTO_SHA512_SIZE == crypto_hash_sha512_BYTES, "CRYPTO_SHA512_SIZE should be equal to crypto_hash_sha512_BYTES"); static_assert(CRYPTO_PUBLIC_KEY_SIZE == 32, - "CRYPTO_PUBLIC_KEY_SIZE is required to be 32 bytes for public_key_cmp to work"); + "CRYPTO_PUBLIC_KEY_SIZE is required to be 32 bytes for public_key_eq to work"); #if !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) static uint8_t *crypto_malloc(size_t bytes) @@ -119,22 +119,24 @@ bool crypto_memunlock(void *data, size_t length) #endif } -int32_t public_key_cmp(const uint8_t *pk1, const uint8_t *pk2) +bool public_key_eq(const uint8_t *pk1, const uint8_t *pk2) { #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION // Hope that this is better for the fuzzer - return memcmp(pk1, pk2, CRYPTO_PUBLIC_KEY_SIZE) == 0 ? 0 : -1; + return memcmp(pk1, pk2, CRYPTO_PUBLIC_KEY_SIZE) == 0; #else - return crypto_verify_32(pk1, pk2); + return crypto_verify_32(pk1, pk2) == 0; #endif } -int32_t crypto_sha512_cmp(const uint8_t *cksum1, const uint8_t *cksum2) +bool crypto_sha512_eq(const uint8_t *cksum1, const uint8_t *cksum2) { #ifndef VANILLA_NACL - return crypto_verify_64(cksum1, cksum2); + return crypto_verify_64(cksum1, cksum2) == 0; #else - return crypto_verify_32(cksum1, cksum2) && crypto_verify_32(cksum1 + 8, cksum2 + 8); + const int lo = crypto_verify_32(cksum1, cksum2) == 0 ? 1 : 0; + const int hi = crypto_verify_32(cksum1 + 8, cksum2 + 8) == 0 ? 1 : 0; + return (lo & hi) == 1; #endif } diff --git a/toxcore/crypto_core.h b/toxcore/crypto_core.h index 3fa19c7d7b..20f2f18414 100644 --- a/toxcore/crypto_core.h +++ b/toxcore/crypto_core.h @@ -86,20 +86,20 @@ void crypto_sha512(uint8_t *hash, const uint8_t *data, size_t length); * @brief Compare 2 public keys of length @ref CRYPTO_PUBLIC_KEY_SIZE, not vulnerable to * timing attacks. * - * @retval 0 if both mem locations of length are equal - * @retval -1 if they are not + * @retval true if both mem locations of length are equal + * @retval false if they are not */ non_null() -int32_t public_key_cmp(const uint8_t pk1[CRYPTO_PUBLIC_KEY_SIZE], const uint8_t pk2[CRYPTO_PUBLIC_KEY_SIZE]); +bool public_key_eq(const uint8_t pk1[CRYPTO_PUBLIC_KEY_SIZE], const uint8_t pk2[CRYPTO_PUBLIC_KEY_SIZE]); /** * @brief Compare 2 SHA512 checksums of length CRYPTO_SHA512_SIZE, not vulnerable to * timing attacks. * - * @return 0 if both mem locations of length are equal, -1 if they are not. + * @return true if both mem locations of length are equal, false if they are not. */ non_null() -int32_t crypto_sha512_cmp(const uint8_t *cksum1, const uint8_t *cksum2); +bool crypto_sha512_eq(const uint8_t *cksum1, const uint8_t *cksum2); /** * @brief Return a random 8 bit integer. diff --git a/toxcore/friend_connection.c b/toxcore/friend_connection.c index 4b74321b75..71325a4b3b 100644 --- a/toxcore/friend_connection.c +++ b/toxcore/friend_connection.c @@ -202,7 +202,7 @@ int getfriend_conn_id_pk(const Friend_Connections *fr_c, const uint8_t *real_pk) const Friend_Conn *friend_con = get_conn(fr_c, i); if (friend_con != nullptr) { - if (public_key_cmp(friend_con->real_public_key, real_pk) == 0) { + if (pk_equal(friend_con->real_public_key, real_pk)) { return i; } } @@ -229,7 +229,7 @@ static int friend_add_tcp_relay(Friend_Connections *fr_c, int friendcon_id, cons } /* Local ip and same pk means that they are hosting a TCP relay. */ - if (ip_is_local(&ipp_copy.ip) && public_key_cmp(friend_con->dht_temp_pk, public_key) == 0) { + if (ip_is_local(&ipp_copy.ip) && pk_equal(friend_con->dht_temp_pk, public_key)) { if (!net_family_is_unspec(friend_con->dht_ip_port.ip.family)) { ipp_copy.ip = friend_con->dht_ip_port.ip; } else { @@ -241,7 +241,7 @@ static int friend_add_tcp_relay(Friend_Connections *fr_c, int friendcon_id, cons for (unsigned i = 0; i < FRIEND_MAX_STORED_TCP_RELAYS; ++i) { if (!net_family_is_unspec(friend_con->tcp_relays[i].ip_port.ip.family) - && public_key_cmp(friend_con->tcp_relays[i].public_key, public_key) == 0) { + && pk_equal(friend_con->tcp_relays[i].public_key, public_key)) { memset(&friend_con->tcp_relays[i], 0, sizeof(Node_format)); } } @@ -351,7 +351,7 @@ static void dht_ip_callback(void *object, int32_t number, const IP_Port *ip_port friend_new_connection(fr_c, number); } - set_direct_ip_port(fr_c->net_crypto, friend_con->crypt_connection_id, ip_port, 1); + set_direct_ip_port(fr_c->net_crypto, friend_con->crypt_connection_id, ip_port, true); friend_con->dht_ip_port = *ip_port; friend_con->dht_ip_port_lastrecv = mono_time_get(fr_c->mono_time); @@ -443,7 +443,7 @@ static void dht_pk_callback(void *object, int32_t number, const uint8_t *dht_pub return; } - if (public_key_cmp(friend_con->dht_temp_pk, dht_public_key) == 0) { + if (pk_equal(friend_con->dht_temp_pk, dht_public_key)) { return; } @@ -577,13 +577,13 @@ static int handle_new_connections(void *object, const New_Connection *n_c) friend_con->crypt_connection_id = id; if (!net_family_is_ipv4(n_c->source.ip.family) && !net_family_is_ipv6(n_c->source.ip.family)) { - set_direct_ip_port(fr_c->net_crypto, friend_con->crypt_connection_id, &friend_con->dht_ip_port, 0); + set_direct_ip_port(fr_c->net_crypto, friend_con->crypt_connection_id, &friend_con->dht_ip_port, false); } else { friend_con->dht_ip_port = n_c->source; friend_con->dht_ip_port_lastrecv = mono_time_get(fr_c->mono_time); } - if (public_key_cmp(friend_con->dht_temp_pk, n_c->dht_public_key) != 0) { + if (!pk_equal(friend_con->dht_temp_pk, n_c->dht_public_key)) { change_dht_pk(fr_c, friendcon_id, n_c->dht_public_key); } @@ -973,7 +973,7 @@ void do_friend_connections(Friend_Connections *fr_c, void *userdata) if (friend_con->dht_lock > 0) { if (friend_new_connection(fr_c, i) == 0) { - set_direct_ip_port(fr_c->net_crypto, friend_con->crypt_connection_id, &friend_con->dht_ip_port, 0); + set_direct_ip_port(fr_c->net_crypto, friend_con->crypt_connection_id, &friend_con->dht_ip_port, false); connect_to_saved_tcp_relays(fr_c, i, MAX_FRIEND_TCP_CONNECTIONS / 2); /* Only fill it half up. */ } } diff --git a/toxcore/friend_requests.c b/toxcore/friend_requests.c index 38efddfdcc..80ae31654a 100644 --- a/toxcore/friend_requests.c +++ b/toxcore/friend_requests.c @@ -73,7 +73,7 @@ static void addto_receivedlist(Friend_Requests *fr, const uint8_t *real_pk) fr->received.requests_index = 0; } - id_copy(fr->received.requests[fr->received.requests_index], real_pk); + pk_copy(fr->received.requests[fr->received.requests_index], real_pk); ++fr->received.requests_index; } @@ -86,7 +86,7 @@ non_null() static bool request_received(const Friend_Requests *fr, const uint8_t *real_pk) { for (uint32_t i = 0; i < MAX_RECEIVED_STORED; ++i) { - if (id_equal(fr->received.requests[i], real_pk)) { + if (pk_equal(fr->received.requests[i], real_pk)) { return true; } } @@ -102,7 +102,7 @@ static bool request_received(const Friend_Requests *fr, const uint8_t *real_pk) int remove_request_received(Friend_Requests *fr, const uint8_t *real_pk) { for (uint32_t i = 0; i < MAX_RECEIVED_STORED; ++i) { - if (id_equal(fr->received.requests[i], real_pk)) { + if (pk_equal(fr->received.requests[i], real_pk)) { crypto_memzero(fr->received.requests[i], CRYPTO_PUBLIC_KEY_SIZE); return 0; } diff --git a/toxcore/group.c b/toxcore/group.c index e6a0d71542..1c0ed9ed20 100644 --- a/toxcore/group.c +++ b/toxcore/group.c @@ -60,7 +60,19 @@ static_assert(GROUP_ID_LENGTH == CRYPTO_PUBLIC_KEY_SIZE, non_null() static bool group_id_eq(const uint8_t *a, const uint8_t *b) { - return public_key_cmp(a, b) == 0; + return pk_equal(a, b); +} + +non_null() +static bool g_title_eq(Group_c *g, const uint8_t *title, uint8_t title_len) +{ + return memeq(g->title, g->title_len, title, title_len); +} + +non_null() +static bool g_peer_nick_eq(Group_Peer *peer, const uint8_t *nick, uint8_t nick_len) +{ + return memeq(peer->nick, peer->nick_len, nick, nick_len); } /** return false if the groupnumber is not valid. @@ -190,7 +202,7 @@ non_null() static int peer_in_group(const Group_c *g, const uint8_t *real_pk) { for (uint32_t i = 0; i < g->numpeers; ++i) { - if (id_equal(g->group[i].real_pk, real_pk)) { + if (pk_equal(g->group[i].real_pk, real_pk)) { return i; } } @@ -202,7 +214,7 @@ non_null() static int frozen_in_group(const Group_c *g, const uint8_t *real_pk) { for (uint32_t i = 0; i < g->numfrozen; ++i) { - if (id_equal(g->frozen[i].real_pk, real_pk)) { + if (pk_equal(g->frozen[i].real_pk, real_pk)) { return i; } } @@ -285,14 +297,14 @@ typedef enum Groupchat_Closest_Change { non_null() static bool add_to_closest(Group_c *g, const uint8_t *real_pk, const uint8_t *temp_pk) { - if (public_key_cmp(g->real_pk, real_pk) == 0) { + if (pk_equal(g->real_pk, real_pk)) { return false; } unsigned int index = DESIRED_CLOSEST; for (unsigned int i = 0; i < DESIRED_CLOSEST; ++i) { - if (g->closest_peers[i].active && public_key_cmp(real_pk, g->closest_peers[i].real_pk) == 0) { + if (g->closest_peers[i].active && pk_equal(real_pk, g->closest_peers[i].real_pk)) { return true; } } @@ -366,7 +378,7 @@ static bool pk_in_closest_peers(const Group_c *g, const uint8_t *real_pk) continue; } - if (public_key_cmp(g->closest_peers[i].real_pk, real_pk) == 0) { + if (pk_equal(g->closest_peers[i].real_pk, real_pk)) { return true; } } @@ -641,12 +653,12 @@ static int addpeer(Group_Chats *g_c, uint32_t groupnumber, const uint8_t *real_p get_peer_index(g, peer_number); if (peer_index != -1) { - if (!id_equal(g->group[peer_index].real_pk, real_pk)) { + if (!pk_equal(g->group[peer_index].real_pk, real_pk)) { return -1; } if (fresh || !g->group[peer_index].temp_pk_updated) { - id_copy(g->group[peer_index].temp_pk, temp_pk); + pk_copy(g->group[peer_index].temp_pk, temp_pk); g->group[peer_index].temp_pk_updated = true; } @@ -657,11 +669,11 @@ static int addpeer(Group_Chats *g_c, uint32_t groupnumber, const uint8_t *real_p const int frozen_index = get_frozen_index(g, peer_number); if (frozen_index != -1) { - if (!id_equal(g->frozen[frozen_index].real_pk, real_pk)) { + if (!pk_equal(g->frozen[frozen_index].real_pk, real_pk)) { return -1; } - id_copy(g->frozen[frozen_index].temp_pk, temp_pk); + pk_copy(g->frozen[frozen_index].temp_pk, temp_pk); return -1; } @@ -680,8 +692,8 @@ static int addpeer(Group_Chats *g_c, uint32_t groupnumber, const uint8_t *real_p const uint32_t new_index = g->numpeers; - id_copy(g->group[new_index].real_pk, real_pk); - id_copy(g->group[new_index].temp_pk, temp_pk); + pk_copy(g->group[new_index].real_pk, real_pk); + pk_copy(g->group[new_index].temp_pk, temp_pk); g->group[new_index].temp_pk_updated = true; g->group[new_index].peer_number = peer_number; g->group[new_index].last_active = mono_time_get(g_c->mono_time); @@ -717,7 +729,7 @@ static void remove_from_closest(Group_c *g, int peer_index) { for (uint32_t i = 0; i < DESIRED_CLOSEST; ++i) { if (g->closest_peers[i].active - && id_equal(g->closest_peers[i].real_pk, g->group[peer_index].real_pk)) { + && pk_equal(g->closest_peers[i].real_pk, g->group[peer_index].real_pk)) { g->closest_peers[i].active = false; g->changed = GROUPCHAT_CLOSEST_CHANGE_REMOVED; break; @@ -789,7 +801,7 @@ static bool delpeer(Group_Chats *g_c, uint32_t groupnumber, int peer_index, void static int cmp_u64(uint64_t a, uint64_t b) { - return (a > b) - (a < b); + return (a > b ? 1 : 0) - (a < b ? 1 : 0); } /** Order peers with friends first and with more recently active earlier */ @@ -899,13 +911,12 @@ static bool setnick(Group_Chats *g_c, uint32_t groupnumber, int peer_index, cons g->group[peer_index].nick_updated = true; - if (g->group[peer_index].nick_len == nick_len - && (nick_len == 0 || !memcmp(g->group[peer_index].nick, nick, nick_len))) { + if (g_peer_nick_eq(&g->group[peer_index], nick, nick_len)) { /* same name as already stored */ return true; } - if (nick_len != 0) { + if (nick_len > 0) { memcpy(g->group[peer_index].nick, nick, nick_len); } @@ -936,7 +947,7 @@ static bool settitle(Group_Chats *g_c, uint32_t groupnumber, int peer_index, con return false; } - if (g->title_len == title_len && !memcmp(g->title, title, title_len)) { + if (g_title_eq(g, title, title_len)) { /* same title as already set */ return true; } @@ -970,7 +981,7 @@ static void check_disconnected(Group_Chats *g_c, uint32_t groupnumber, void *use } for (uint32_t i = 0; i < g->numpeers; ++i) { - while (i < g->numpeers && !id_equal(g->group[i].real_pk, g->real_pk)) { + while (i < g->numpeers && !pk_equal(g->group[i].real_pk, g->real_pk)) { freeze_peer(g_c, groupnumber, i, userdata); } } @@ -1027,7 +1038,7 @@ static void rejoin_frozen_friend(Group_Chats *g_c, int friendcon_id) } for (uint32_t j = 0; j < g->numfrozen; ++j) { - if (id_equal(g->frozen[j].real_pk, real_pk)) { + if (pk_equal(g->frozen[j].real_pk, real_pk)) { try_send_rejoin(g_c, g, real_pk); break; } @@ -1108,7 +1119,7 @@ static int add_conn_to_groupchat(Group_Chats *g_c, int friendcon_id, Group_c *g, ind = empty; } - if (!(g->connections[ind].reasons & reason)) { + if ((g->connections[ind].reasons & reason) == 0) { g->connections[ind].reasons |= reason; if (reason == GROUPCHAT_CONNECTION_REASON_INTRODUCER) { @@ -1128,7 +1139,7 @@ static unsigned int send_peer_introduced(const Group_Chats *g_c, int friendcon_i */ static void remove_connection_reason(Group_Chats *g_c, Group_c *g, uint16_t i, uint8_t reason) { - if (!(g->connections[i].reasons & reason)) { + if ((g->connections[i].reasons & reason) == 0) { return; } @@ -1856,7 +1867,7 @@ int group_title_send(const Group_Chats *g_c, uint32_t groupnumber, const uint8_t } /* same as already set? */ - if (g->title_len == title_len && !memcmp(g->title, title, title_len)) { + if (g_title_eq(g, title, title_len)) { return 0; } @@ -2310,7 +2321,7 @@ static int handle_send_peers(Group_Chats *g_c, uint32_t groupnumber, const uint8 d += sizeof(uint16_t); if (g->status == GROUPCHAT_STATUS_VALID - && public_key_cmp(d, nc_get_self_public_key(g_c->m->net_crypto)) == 0) { + && pk_equal(d, nc_get_self_public_key(g_c->m->net_crypto))) { g->peer_number = peer_num; g->status = GROUPCHAT_STATUS_CONNECTED; @@ -2647,7 +2658,7 @@ static bool check_message_info(uint32_t message_number, uint8_t message_id, Grou ++peer->num_last_message_infos; } - memmove(i + 1, i, ((peer->last_message_infos + peer->num_last_message_infos - 1) - i) * sizeof(Message_Info)); + memmove(i + 1, i, (&peer->last_message_infos[peer->num_last_message_infos - 1] - i) * sizeof(Message_Info)); i->message_number = message_number; i->message_id = message_id; @@ -2713,7 +2724,7 @@ static void handle_message_packet_group(Group_Chats *g_c, uint32_t groupnumber, uint8_t real_pk[CRYPTO_PUBLIC_KEY_SIZE]; get_friendcon_public_keys(real_pk, nullptr, g_c->fr_c, g->connections[i].number); - if (id_equal(g->group[index].real_pk, real_pk)) { + if (pk_equal(g->group[index].real_pk, real_pk)) { /* Received message from peer relayed via another peer, so * the introduction was successful */ remove_connection_reason(g_c, g, i, GROUPCHAT_CONNECTION_REASON_INTRODUCER); @@ -2727,7 +2738,7 @@ static void handle_message_packet_group(Group_Chats *g_c, uint32_t groupnumber, uint8_t real_pk[CRYPTO_PUBLIC_KEY_SIZE]; get_friendcon_public_keys(real_pk, nullptr, g_c->fr_c, g->connections[connection_index].number); - const bool direct_from_sender = id_equal(g->group[index].real_pk, real_pk); + const bool direct_from_sender = pk_equal(g->group[index].real_pk, real_pk); switch (message_id) { case GROUP_MESSAGE_PING_ID: { @@ -3251,6 +3262,7 @@ static uint8_t *save_peer(const Group_Peer *peer, uint8_t *data) host_to_lendian_bytes64(data, peer->last_active); data += sizeof(uint64_t); + // TODO(iphydf): This looks broken: nick_len can be > 255. *data = peer->nick_len; ++data; @@ -3271,7 +3283,7 @@ static uint32_t saved_conf_size(const Group_c *g) for (uint32_t j = 0; j < g->numpeers + g->numfrozen; ++j) { const Group_Peer *peer = (j < g->numpeers) ? &g->group[j] : &g->frozen[j - g->numpeers]; - if (id_equal(peer->real_pk, g->real_pk)) { + if (pk_equal(peer->real_pk, g->real_pk)) { continue; } @@ -3318,7 +3330,7 @@ static uint8_t *save_conf(const Group_c *g, uint8_t *data) for (uint32_t j = 0; j < g->numpeers + g->numfrozen; ++j) { const Group_Peer *peer = (j < g->numpeers) ? &g->group[j] : &g->frozen[j - g->numpeers]; - if (id_equal(peer->real_pk, g->real_pk)) { + if (pk_equal(peer->real_pk, g->real_pk)) { continue; } @@ -3444,9 +3456,9 @@ static uint32_t load_group(Group_c *g, const Group_Chats *g_c, const uint8_t *da Group_Peer *peer = &g->frozen[j]; memset(peer, 0, sizeof(Group_Peer)); - id_copy(peer->real_pk, data); + pk_copy(peer->real_pk, data); data += CRYPTO_PUBLIC_KEY_SIZE; - id_copy(peer->temp_pk, data); + pk_copy(peer->temp_pk, data); data += CRYPTO_PUBLIC_KEY_SIZE; lendian_bytes_to_host16(&peer->peer_number, data); @@ -3481,7 +3493,7 @@ static uint32_t load_group(Group_c *g, const Group_Chats *g_c, const uint8_t *da g->status = GROUPCHAT_STATUS_CONNECTED; - id_copy(g->real_pk, nc_get_self_public_key(g_c->m->net_crypto)); + pk_copy(g->real_pk, nc_get_self_public_key(g_c->m->net_crypto)); assert((data - init_data) < UINT32_MAX); @@ -3515,7 +3527,7 @@ static State_Load_Status load_conferences_helper(Group_Chats *g_c, const uint8_t // HACK: suppress unused variable warning if (!ret) { // wipe_group_chat(...) must be able to wipe partially allocated groups - assert(ret == true); + assert(ret); } return STATE_LOAD_STATUS_ERROR; diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 1009aca6f4..cbbaa4046d 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -415,7 +415,7 @@ static int tcp_oob_handle_cookie_request(const Net_Crypto *c, unsigned int tcp_c return -1; } - if (public_key_cmp(dht_public_key, dht_public_key_temp) != 0) { + if (!pk_equal(dht_public_key, dht_public_key_temp)) { return -1; } @@ -514,25 +514,25 @@ static int create_crypto_handshake(const Net_Crypto *c, uint8_t *packet, const u * peer_real_pk must be at least CRYPTO_PUBLIC_KEY_SIZE * cookie must be at least COOKIE_LENGTH * - * return -1 on failure. - * return 0 on success. + * return false on failure. + * return true on success. */ non_null(1, 2, 3, 4, 5, 6, 7) nullable(9) -static int handle_crypto_handshake(const Net_Crypto *c, uint8_t *nonce, uint8_t *session_pk, uint8_t *peer_real_pk, - uint8_t *dht_public_key, uint8_t *cookie, const uint8_t *packet, uint16_t length, const uint8_t *expected_real_pk) +static bool handle_crypto_handshake(const Net_Crypto *c, uint8_t *nonce, uint8_t *session_pk, uint8_t *peer_real_pk, + uint8_t *dht_public_key, uint8_t *cookie, const uint8_t *packet, uint16_t length, const uint8_t *expected_real_pk) { if (length != HANDSHAKE_PACKET_LENGTH) { - return -1; + return false; } uint8_t cookie_plain[COOKIE_DATA_LENGTH]; if (open_cookie(c->mono_time, cookie_plain, packet + 1, c->secret_symmetric_key) != 0) { - return -1; + return false; } - if (expected_real_pk != nullptr && public_key_cmp(cookie_plain, expected_real_pk) != 0) { - return -1; + if (expected_real_pk != nullptr && !pk_equal(cookie_plain, expected_real_pk)) { + return false; } uint8_t cookie_hash[CRYPTO_SHA512_SIZE]; @@ -544,11 +544,11 @@ static int handle_crypto_handshake(const Net_Crypto *c, uint8_t *nonce, uint8_t HANDSHAKE_PACKET_LENGTH - (1 + COOKIE_LENGTH + CRYPTO_NONCE_SIZE), plain); if (len != sizeof(plain)) { - return -1; + return false; } - if (crypto_sha512_cmp(cookie_hash, plain + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE) != 0) { - return -1; + if (!crypto_sha512_eq(cookie_hash, plain + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE)) { + return false; } memcpy(nonce, plain, CRYPTO_NONCE_SIZE); @@ -556,7 +556,7 @@ static int handle_crypto_handshake(const Net_Crypto *c, uint8_t *nonce, uint8_t memcpy(cookie, plain + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_SHA512_SIZE, COOKIE_LENGTH); memcpy(peer_real_pk, cookie_plain, CRYPTO_PUBLIC_KEY_SIZE); memcpy(dht_public_key, cookie_plain + CRYPTO_PUBLIC_KEY_SIZE, CRYPTO_PUBLIC_KEY_SIZE); - return 0; + return true; } @@ -627,15 +627,15 @@ static IP_Port return_ip_port_connection(const Net_Crypto *c, int crypt_connecti } const uint64_t current_time = mono_time_get(c->mono_time); - bool v6 = 0; - bool v4 = 0; + bool v6 = false; + bool v4 = false; if ((UDP_DIRECT_TIMEOUT + conn->direct_lastrecv_timev4) > current_time) { - v4 = 1; + v4 = true; } if ((UDP_DIRECT_TIMEOUT + conn->direct_lastrecv_timev6) > current_time) { - v6 = 1; + v6 = true; } /* Prefer IP_Ports which haven't timed out to those which have. @@ -1728,12 +1728,12 @@ static int handle_packet_crypto_hs(Net_Crypto *c, int crypt_connection_id, const uint8_t dht_public_key[CRYPTO_PUBLIC_KEY_SIZE]; uint8_t cookie[COOKIE_LENGTH]; - if (handle_crypto_handshake(c, conn->recv_nonce, conn->peersessionpublic_key, peer_real_pk, dht_public_key, cookie, - packet, length, conn->public_key) != 0) { + if (!handle_crypto_handshake(c, conn->recv_nonce, conn->peersessionpublic_key, peer_real_pk, dht_public_key, cookie, + packet, length, conn->public_key)) { return -1; } - if (public_key_cmp(dht_public_key, conn->dht_public_key) == 0) { + if (pk_equal(dht_public_key, conn->dht_public_key)) { encrypt_precompute(conn->peersessionpublic_key, conn->sessionsecret_key, conn->shared_key); if (conn->status == CRYPTO_CONN_COOKIE_REQUESTING) { @@ -1941,7 +1941,7 @@ static int getcryptconnection_id(const Net_Crypto *c, const uint8_t *public_key) continue; } - if (public_key_cmp(public_key, c->crypto_connections[i].public_key) == 0) { + if (pk_equal(public_key, c->crypto_connections[i].public_key)) { return i; } } @@ -2021,8 +2021,8 @@ static int handle_new_connection_handshake(Net_Crypto *c, const IP_Port *source, n_c.source = *source; n_c.cookie_length = COOKIE_LENGTH; - if (handle_crypto_handshake(c, n_c.recv_nonce, n_c.peersessionpublic_key, n_c.public_key, n_c.dht_public_key, - n_c.cookie, data, length, nullptr) != 0) { + if (!handle_crypto_handshake(c, n_c.recv_nonce, n_c.peersessionpublic_key, n_c.public_key, n_c.dht_public_key, + n_c.cookie, data, length, nullptr)) { free(n_c.cookie); return -1; } @@ -2036,7 +2036,7 @@ static int handle_new_connection_handshake(Net_Crypto *c, const IP_Port *source, return -1; } - if (public_key_cmp(n_c.dht_public_key, conn->dht_public_key) != 0) { + if (!pk_equal(n_c.dht_public_key, conn->dht_public_key)) { connection_kill(c, crypt_connection_id, userdata); } else { if (conn->status != CRYPTO_CONN_COOKIE_REQUESTING && conn->status != CRYPTO_CONN_HANDSHAKE_SENT) { @@ -2673,7 +2673,7 @@ static void send_crypto_packets(Net_Crypto *c) conn->last_sendqueue_counter = (conn->last_sendqueue_counter + 1) % (CONGESTION_QUEUE_ARRAY_SIZE * CONGESTION_LAST_SENT_ARRAY_SIZE); - bool direct_connected = 0; + bool direct_connected = false; /* return value can be ignored since the `if` above ensures the connection is established */ crypto_connection_status(c, i, &direct_connected, nullptr); @@ -3020,14 +3020,14 @@ bool crypto_connection_status(const Net_Crypto *c, int crypt_connection_id, bool } if (direct_connected != nullptr) { - *direct_connected = 0; + *direct_connected = false; const uint64_t current_time = mono_time_get(c->mono_time); if ((UDP_DIRECT_TIMEOUT + conn->direct_lastrecv_timev4) > current_time) { - *direct_connected = 1; + *direct_connected = true; } else if ((UDP_DIRECT_TIMEOUT + conn->direct_lastrecv_timev6) > current_time) { - *direct_connected = 1; + *direct_connected = true; } } diff --git a/toxcore/network.c b/toxcore/network.c index 07f4408658..a07f57d1d3 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -1023,7 +1023,7 @@ Networking_Core *new_networking_ex(const Logger *log, const IP *ip, uint16_t por int res = bind(temp->sock.socket, (struct sockaddr *)&addr, addrsize); #endif - if (!res) { + if (res == 0) { temp->port = *portptr; char ip_str[IP_NTOA_LEN]; @@ -1361,7 +1361,7 @@ int addr_resolve(const char *address, IP *to, IP *extra) get_ip4(&to->ip.v4, &addr->sin_addr); result = TOX_ADDR_RESOLVE_INET; done = true; - } else if (!(result & TOX_ADDR_RESOLVE_INET)) { /* AF_UNSPEC requested, store away */ + } else if ((result & TOX_ADDR_RESOLVE_INET) == 0) { /* AF_UNSPEC requested, store away */ const struct sockaddr_in *addr = (const struct sockaddr_in *)(void *)walker->ai_addr; get_ip4(&ip4.ip.v4, &addr->sin_addr); result |= TOX_ADDR_RESOLVE_INET; @@ -1378,7 +1378,7 @@ int addr_resolve(const char *address, IP *to, IP *extra) result = TOX_ADDR_RESOLVE_INET6; done = true; } - } else if (!(result & TOX_ADDR_RESOLVE_INET6)) { /* AF_UNSPEC requested, store away */ + } else if ((result & TOX_ADDR_RESOLVE_INET6) == 0) { /* AF_UNSPEC requested, store away */ if (walker->ai_addrlen == sizeof(struct sockaddr_in6)) { const struct sockaddr_in6 *addr = (const struct sockaddr_in6 *)(void *)walker->ai_addr; get_ip6(&ip6.ip.v6, &addr->sin6_addr); @@ -1392,13 +1392,13 @@ int addr_resolve(const char *address, IP *to, IP *extra) } if (family == AF_UNSPEC) { - if (result & TOX_ADDR_RESOLVE_INET6) { + if ((result & TOX_ADDR_RESOLVE_INET6) != 0) { ip_copy(to, &ip6); - if ((result & TOX_ADDR_RESOLVE_INET) && (extra != nullptr)) { + if ((result & TOX_ADDR_RESOLVE_INET) != 0 && (extra != nullptr)) { ip_copy(extra, &ip4); } - } else if (result & TOX_ADDR_RESOLVE_INET) { + } else if ((result & TOX_ADDR_RESOLVE_INET) != 0) { ip_copy(to, &ip4); } else { result = 0; @@ -1412,7 +1412,7 @@ int addr_resolve(const char *address, IP *to, IP *extra) bool addr_resolve_or_parse_ip(const char *address, IP *to, IP *extra) { - if (!addr_resolve(address, to, extra)) { + if (addr_resolve(address, to, extra) == 0) { if (!addr_parse_ip(address, to)) { return false; } diff --git a/toxcore/onion_announce.c b/toxcore/onion_announce.c index faba9dc1de..ef7acac40d 100644 --- a/toxcore/onion_announce.c +++ b/toxcore/onion_announce.c @@ -48,7 +48,7 @@ struct Onion_Announce { non_null() static bool onion_ping_id_eq(const uint8_t *a, const uint8_t *b) { - return public_key_cmp(a, b) == 0; + return pk_equal(a, b); } uint8_t *onion_announce_entry_public_key(Onion_Announce *onion_a, uint32_t entry) @@ -253,7 +253,7 @@ static int in_entries(const Onion_Announce *onion_a, const uint8_t *public_key) { for (unsigned int i = 0; i < ONION_ANNOUNCE_MAX_ENTRIES; ++i) { if (!mono_time_is_timeout(onion_a->mono_time, onion_a->entries[i].time, ONION_ANNOUNCE_TIMEOUT) - && public_key_cmp(onion_a->entries[i].public_key, public_key) == 0) { + && pk_equal(onion_a->entries[i].public_key, public_key)) { return i; } } @@ -428,8 +428,8 @@ static int handle_announce_request(void *object, const IP_Port *source, const ui pl[0] = 0; memcpy(pl + 1, ping_id2, ONION_PING_ID_SIZE); } else { - if (public_key_cmp(onion_a->entries[index].public_key, packet_public_key) == 0) { - if (public_key_cmp(onion_a->entries[index].data_public_key, data_public_key) != 0) { + if (pk_equal(onion_a->entries[index].public_key, packet_public_key)) { + if (!pk_equal(onion_a->entries[index].data_public_key, data_public_key)) { pl[0] = 0; memcpy(pl + 1, ping_id2, ONION_PING_ID_SIZE); } else { diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index f72978a588..91c3f3ac7b 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c @@ -156,7 +156,7 @@ bool onion_add_bs_path_node(Onion_Client *onion_c, const IP_Port *ip_port, const } for (unsigned int i = 0; i < MAX_PATH_NODES; ++i) { - if (public_key_cmp(public_key, onion_c->path_nodes_bs[i].public_key) == 0) { + if (pk_equal(public_key, onion_c->path_nodes_bs[i].public_key)) { return false; } } @@ -188,7 +188,7 @@ static int onion_add_path_node(Onion_Client *onion_c, const IP_Port *ip_port, co } for (unsigned int i = 0; i < MAX_PATH_NODES; ++i) { - if (public_key_cmp(public_key, onion_c->path_nodes[i].public_key) == 0) { + if (pk_equal(public_key, onion_c->path_nodes[i].public_key)) { return -1; } } @@ -229,7 +229,7 @@ uint16_t onion_backup_nodes(const Onion_Client *onion_c, Node_format *nodes, uin bool already_saved = false; for (uint16_t k = 0; k < num_nodes; ++k) { - if (public_key_cmp(nodes[k].public_key, onion_c->path_nodes_bs[j].public_key) == 0) { + if (pk_equal(nodes[k].public_key, onion_c->path_nodes_bs[j].public_key)) { already_saved = true; break; } @@ -690,16 +690,16 @@ static int client_add_to_list(Onion_Client *onion_c, uint32_t num, const uint8_t return -1; } - Onion_Node *list_nodes = nullptr; + Onion_Node *node_list = nullptr; const uint8_t *reference_id = nullptr; unsigned int list_length; if (num == 0) { - list_nodes = onion_c->clients_announce_list; + node_list = onion_c->clients_announce_list; reference_id = nc_get_self_public_key(onion_c->c); list_length = MAX_ONION_CLIENTS_ANNOUNCE; - if (is_stored == 1 && public_key_cmp(pingid_or_key, onion_c->temp_public_key) != 0) { + if (is_stored == 1 && !pk_equal(pingid_or_key, onion_c->temp_public_key)) { is_stored = 0; } } else { @@ -711,25 +711,25 @@ static int client_add_to_list(Onion_Client *onion_c, uint32_t num, const uint8_t onion_c->friends_list[num - 1].last_seen = mono_time_get(onion_c->mono_time); } - list_nodes = onion_c->friends_list[num - 1].clients_list; + node_list = onion_c->friends_list[num - 1].clients_list; reference_id = onion_c->friends_list[num - 1].real_public_key; list_length = MAX_ONION_CLIENTS; } - sort_onion_node_list(list_nodes, list_length, onion_c->mono_time, reference_id); + sort_onion_node_list(node_list, list_length, onion_c->mono_time, reference_id); int index = -1; - int stored = 0; + bool stored = false; - if (onion_node_timed_out(&list_nodes[0], onion_c->mono_time) - || id_closest(reference_id, list_nodes[0].public_key, public_key) == 2) { + if (onion_node_timed_out(&node_list[0], onion_c->mono_time) + || id_closest(reference_id, node_list[0].public_key, public_key) == 2) { index = 0; } for (unsigned int i = 0; i < list_length; ++i) { - if (public_key_cmp(list_nodes[i].public_key, public_key) == 0) { + if (pk_equal(node_list[i].public_key, public_key)) { index = i; - stored = 1; + stored = true; break; } } @@ -738,39 +738,39 @@ static int client_add_to_list(Onion_Client *onion_c, uint32_t num, const uint8_t return 0; } - memcpy(list_nodes[index].public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE); - list_nodes[index].ip_port = *ip_port; + memcpy(node_list[index].public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE); + node_list[index].ip_port = *ip_port; // TODO(irungentoo): remove this and find a better source of nodes to use for paths. onion_add_path_node(onion_c, ip_port, public_key); if (is_stored == 1) { - memcpy(list_nodes[index].data_public_key, pingid_or_key, CRYPTO_PUBLIC_KEY_SIZE); + memcpy(node_list[index].data_public_key, pingid_or_key, CRYPTO_PUBLIC_KEY_SIZE); } else { - memcpy(list_nodes[index].ping_id, pingid_or_key, ONION_PING_ID_SIZE); + memcpy(node_list[index].ping_id, pingid_or_key, ONION_PING_ID_SIZE); } - list_nodes[index].is_stored = is_stored; - list_nodes[index].timestamp = mono_time_get(onion_c->mono_time); - list_nodes[index].unsuccessful_pings = 0; + node_list[index].is_stored = is_stored; + node_list[index].timestamp = mono_time_get(onion_c->mono_time); + node_list[index].unsuccessful_pings = 0; if (!stored) { - list_nodes[index].last_pinged = 0; - list_nodes[index].added_time = mono_time_get(onion_c->mono_time); + node_list[index].last_pinged = 0; + node_list[index].added_time = mono_time_get(onion_c->mono_time); } - list_nodes[index].path_used = path_used; + node_list[index].path_used = path_used; return 0; } non_null() -static int good_to_ping(const Mono_Time *mono_time, Last_Pinged *last_pinged, uint8_t *last_pinged_index, - const uint8_t *public_key) +static bool good_to_ping(const Mono_Time *mono_time, Last_Pinged *last_pinged, uint8_t *last_pinged_index, + const uint8_t *public_key) { for (unsigned int i = 0; i < MAX_STORED_PINGED_NODES; ++i) { if (!mono_time_is_timeout(mono_time, last_pinged[i].timestamp, MIN_NODE_PING_TIME)) { - if (public_key_cmp(last_pinged[i].public_key, public_key) == 0) { - return 0; + if (pk_equal(last_pinged[i].public_key, public_key)) { + return false; } } } @@ -778,7 +778,7 @@ static int good_to_ping(const Mono_Time *mono_time, Last_Pinged *last_pinged, ui memcpy(last_pinged[*last_pinged_index % MAX_STORED_PINGED_NODES].public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE); last_pinged[*last_pinged_index % MAX_STORED_PINGED_NODES].timestamp = mono_time_get(mono_time); ++*last_pinged_index; - return 1; + return true; } non_null() @@ -793,7 +793,7 @@ static int client_ping_nodes(Onion_Client *onion_c, uint32_t num, const Node_for return 0; } - const Onion_Node *list_nodes = nullptr; + const Onion_Node *node_list = nullptr; const uint8_t *reference_id = nullptr; unsigned int list_length; @@ -801,13 +801,13 @@ static int client_ping_nodes(Onion_Client *onion_c, uint32_t num, const Node_for uint8_t *last_pinged_index = nullptr; if (num == 0) { - list_nodes = onion_c->clients_announce_list; + node_list = onion_c->clients_announce_list; reference_id = nc_get_self_public_key(onion_c->c); list_length = MAX_ONION_CLIENTS_ANNOUNCE; last_pinged = onion_c->last_pinged; last_pinged_index = &onion_c->last_pinged_index; } else { - list_nodes = onion_c->friends_list[num - 1].clients_list; + node_list = onion_c->friends_list[num - 1].clients_list; reference_id = onion_c->friends_list[num - 1].real_public_key; list_length = MAX_ONION_CLIENTS; last_pinged = onion_c->friends_list[num - 1].last_pinged; @@ -823,15 +823,15 @@ static int client_ping_nodes(Onion_Client *onion_c, uint32_t num, const Node_for } } - if (onion_node_timed_out(&list_nodes[0], onion_c->mono_time) - || id_closest(reference_id, list_nodes[0].public_key, nodes[i].public_key) == 2 - || onion_node_timed_out(&list_nodes[1], onion_c->mono_time) - || id_closest(reference_id, list_nodes[1].public_key, nodes[i].public_key) == 2) { + if (onion_node_timed_out(&node_list[0], onion_c->mono_time) + || id_closest(reference_id, node_list[0].public_key, nodes[i].public_key) == 2 + || onion_node_timed_out(&node_list[1], onion_c->mono_time) + || id_closest(reference_id, node_list[1].public_key, nodes[i].public_key) == 2) { uint32_t j; /* check if node is already in list. */ for (j = 0; j < list_length; ++j) { - if (public_key_cmp(list_nodes[j].public_key, nodes[i].public_key) == 0) { + if (pk_equal(node_list[j].public_key, nodes[i].public_key)) { break; } } @@ -1070,16 +1070,16 @@ int send_onion_data(Onion_Client *onion_c, int friend_num, const uint8_t *data, unsigned int good_nodes[MAX_ONION_CLIENTS]; unsigned int num_good = 0; unsigned int num_nodes = 0; - const Onion_Node *list_nodes = onion_c->friends_list[friend_num].clients_list; + const Onion_Node *node_list = onion_c->friends_list[friend_num].clients_list; for (unsigned int i = 0; i < MAX_ONION_CLIENTS; ++i) { - if (onion_node_timed_out(&list_nodes[i], onion_c->mono_time)) { + if (onion_node_timed_out(&node_list[i], onion_c->mono_time)) { continue; } ++num_nodes; - if (list_nodes[i].is_stored != 0) { + if (node_list[i].is_stored != 0) { good_nodes[num_good] = i; ++num_good; } @@ -1113,13 +1113,13 @@ int send_onion_data(Onion_Client *onion_c, int friend_num, const uint8_t *data, uint8_t o_packet[ONION_MAX_PACKET_SIZE]; len = create_data_request(o_packet, sizeof(o_packet), onion_c->friends_list[friend_num].real_public_key, - list_nodes[good_nodes[i]].data_public_key, nonce, packet, SIZEOF_VLA(packet)); + node_list[good_nodes[i]].data_public_key, nonce, packet, SIZEOF_VLA(packet)); if (len == -1) { continue; } - if (send_onion_packet_tcp_udp(onion_c, &path, &list_nodes[good_nodes[i]].ip_port, o_packet, len) == 0) { + if (send_onion_packet_tcp_udp(onion_c, &path, &node_list[good_nodes[i]].ip_port, o_packet, len) == 0) { ++good; } } @@ -1196,7 +1196,7 @@ static int handle_dht_dhtpk(void *object, const IP_Port *source, const uint8_t * return 1; } - if (public_key_cmp(source_pubkey, plain + 1 + sizeof(uint64_t)) != 0) { + if (!pk_equal(source_pubkey, plain + 1 + sizeof(uint64_t))) { return 1; } @@ -1271,7 +1271,7 @@ int onion_friend_num(const Onion_Client *onion_c, const uint8_t *public_key) continue; } - if (public_key_cmp(public_key, onion_c->friends_list[i].real_public_key) == 0) { + if (pk_equal(public_key, onion_c->friends_list[i].real_public_key)) { return i; } } @@ -1435,7 +1435,7 @@ int onion_set_friend_DHT_pubkey(Onion_Client *onion_c, int friend_num, const uin } if (onion_c->friends_list[friend_num].know_dht_public_key) { - if (public_key_cmp(dht_key, onion_c->friends_list[friend_num].dht_public_key) == 0) { + if (pk_equal(dht_key, onion_c->friends_list[friend_num].dht_public_key)) { return -1; } } @@ -1583,43 +1583,43 @@ static void do_friend(Onion_Client *onion_c, uint16_t friendnum) if (!onion_c->friends_list[friendnum].is_online) { unsigned int count = 0; - Onion_Node *list_nodes = onion_c->friends_list[friendnum].clients_list; + Onion_Node *node_list = onion_c->friends_list[friendnum].clients_list; // ensure we get a response from some node roughly once per // (interval / MAX_ONION_CLIENTS) bool ping_random = true; for (unsigned i = 0; i < MAX_ONION_CLIENTS; ++i) { - if (!(mono_time_is_timeout(onion_c->mono_time, list_nodes[i].timestamp, interval / MAX_ONION_CLIENTS) - && mono_time_is_timeout(onion_c->mono_time, list_nodes[i].last_pinged, ONION_NODE_PING_INTERVAL))) { + if (!(mono_time_is_timeout(onion_c->mono_time, node_list[i].timestamp, interval / MAX_ONION_CLIENTS) + && mono_time_is_timeout(onion_c->mono_time, node_list[i].last_pinged, ONION_NODE_PING_INTERVAL))) { ping_random = false; break; } } for (unsigned i = 0; i < MAX_ONION_CLIENTS; ++i) { - if (onion_node_timed_out(&list_nodes[i], onion_c->mono_time)) { + if (onion_node_timed_out(&node_list[i], onion_c->mono_time)) { continue; } ++count; - if (list_nodes[i].last_pinged == 0) { - list_nodes[i].last_pinged = mono_time_get(onion_c->mono_time); + if (node_list[i].last_pinged == 0) { + node_list[i].last_pinged = mono_time_get(onion_c->mono_time); continue; } - if (list_nodes[i].unsuccessful_pings >= ONION_NODE_MAX_PINGS) { + if (node_list[i].unsuccessful_pings >= ONION_NODE_MAX_PINGS) { continue; } - if (mono_time_is_timeout(onion_c->mono_time, list_nodes[i].last_pinged, interval) + if (mono_time_is_timeout(onion_c->mono_time, node_list[i].last_pinged, interval) || (ping_random && random_range_u32(MAX_ONION_CLIENTS - i) == 0)) { - if (client_send_announce_request(onion_c, friendnum + 1, &list_nodes[i].ip_port, - list_nodes[i].public_key, nullptr, -1) == 0) { - list_nodes[i].last_pinged = mono_time_get(onion_c->mono_time); - ++list_nodes[i].unsuccessful_pings; + if (client_send_announce_request(onion_c, friendnum + 1, &node_list[i].ip_port, + node_list[i].public_key, nullptr, -1) == 0) { + node_list[i].last_pinged = mono_time_get(onion_c->mono_time); + ++node_list[i].unsuccessful_pings; ping_random = false; } } @@ -1683,40 +1683,40 @@ non_null() static void do_announce(Onion_Client *onion_c) { unsigned int count = 0; - Onion_Node *list_nodes = onion_c->clients_announce_list; + Onion_Node *node_list = onion_c->clients_announce_list; for (unsigned int i = 0; i < MAX_ONION_CLIENTS_ANNOUNCE; ++i) { - if (onion_node_timed_out(&list_nodes[i], onion_c->mono_time)) { + if (onion_node_timed_out(&node_list[i], onion_c->mono_time)) { continue; } ++count; /* Don't announce ourselves the first time this is run to new peers */ - if (list_nodes[i].last_pinged == 0) { - list_nodes[i].last_pinged = 1; + if (node_list[i].last_pinged == 0) { + node_list[i].last_pinged = 1; continue; } - if (list_nodes[i].unsuccessful_pings >= ONION_NODE_MAX_PINGS) { + if (node_list[i].unsuccessful_pings >= ONION_NODE_MAX_PINGS) { continue; } unsigned int interval = ANNOUNCE_INTERVAL_NOT_ANNOUNCED; - if (list_nodes[i].is_stored != 0 && path_exists(onion_c->mono_time, &onion_c->onion_paths_self, list_nodes[i].path_used)) { + if (node_list[i].is_stored != 0 && path_exists(onion_c->mono_time, &onion_c->onion_paths_self, node_list[i].path_used)) { interval = ANNOUNCE_INTERVAL_ANNOUNCED; - const uint32_t pathnum = list_nodes[i].path_used % NUMBER_ONION_PATHS; + const uint32_t pathnum = node_list[i].path_used % NUMBER_ONION_PATHS; /* A node/path is considered "stable", and can be pinged less * aggressively, if it has survived for at least TIME_TO_STABLE * and the latest packets sent to it are not timing out. */ - if (mono_time_is_timeout(onion_c->mono_time, list_nodes[i].added_time, TIME_TO_STABLE) - && !(list_nodes[i].unsuccessful_pings > 0 - && mono_time_is_timeout(onion_c->mono_time, list_nodes[i].last_pinged, ONION_NODE_TIMEOUT)) + if (mono_time_is_timeout(onion_c->mono_time, node_list[i].added_time, TIME_TO_STABLE) + && !(node_list[i].unsuccessful_pings > 0 + && mono_time_is_timeout(onion_c->mono_time, node_list[i].last_pinged, ONION_NODE_TIMEOUT)) && mono_time_is_timeout(onion_c->mono_time, onion_c->onion_paths_self.path_creation_time[pathnum], TIME_TO_STABLE) && !(onion_c->onion_paths_self.last_path_used_times[pathnum] > 0 && mono_time_is_timeout(onion_c->mono_time, onion_c->onion_paths_self.last_path_used[pathnum], ONION_PATH_TIMEOUT))) { @@ -1724,21 +1724,21 @@ static void do_announce(Onion_Client *onion_c) } } - if (mono_time_is_timeout(onion_c->mono_time, list_nodes[i].last_pinged, interval) + if (mono_time_is_timeout(onion_c->mono_time, node_list[i].last_pinged, interval) || (mono_time_is_timeout(onion_c->mono_time, onion_c->last_announce, ONION_NODE_PING_INTERVAL) && random_range_u32(MAX_ONION_CLIENTS_ANNOUNCE - i) == 0)) { - uint32_t path_to_use = list_nodes[i].path_used; + uint32_t path_to_use = node_list[i].path_used; - if (list_nodes[i].unsuccessful_pings == ONION_NODE_MAX_PINGS - 1 - && mono_time_is_timeout(onion_c->mono_time, list_nodes[i].added_time, TIME_TO_STABLE)) { + if (node_list[i].unsuccessful_pings == ONION_NODE_MAX_PINGS - 1 + && mono_time_is_timeout(onion_c->mono_time, node_list[i].added_time, TIME_TO_STABLE)) { /* Last chance for a long-lived node - try a random path */ path_to_use = -1; } - if (client_send_announce_request(onion_c, 0, &list_nodes[i].ip_port, list_nodes[i].public_key, - list_nodes[i].ping_id, path_to_use) == 0) { - list_nodes[i].last_pinged = mono_time_get(onion_c->mono_time); - ++list_nodes[i].unsuccessful_pings; + if (client_send_announce_request(onion_c, 0, &node_list[i].ip_port, node_list[i].public_key, + node_list[i].ping_id, path_to_use) == 0) { + node_list[i].last_pinged = mono_time_get(onion_c->mono_time); + ++node_list[i].unsuccessful_pings; onion_c->last_announce = mono_time_get(onion_c->mono_time); } } diff --git a/toxcore/ping.c b/toxcore/ping.c index 870588f675..03a28d129e 100644 --- a/toxcore/ping.c +++ b/toxcore/ping.c @@ -47,7 +47,7 @@ void ping_send_request(Ping *ping, const IP_Port *ipp, const uint8_t *public_key int rc; uint64_t ping_id; - if (id_equal(public_key, dht_get_self_public_key(ping->dht))) { + if (pk_equal(public_key, dht_get_self_public_key(ping->dht))) { return; } @@ -57,7 +57,7 @@ void ping_send_request(Ping *ping, const IP_Port *ipp, const uint8_t *public_key dht_get_shared_key_sent(ping->dht, shared_key, public_key); // Generate random ping_id. uint8_t data[PING_DATA_SIZE]; - id_copy(data, public_key); + pk_copy(data, public_key); memcpy(data + CRYPTO_PUBLIC_KEY_SIZE, ipp, sizeof(IP_Port)); ping_id = ping_array_add(ping->ping_array, ping->mono_time, data, sizeof(data)); @@ -71,7 +71,7 @@ void ping_send_request(Ping *ping, const IP_Port *ipp, const uint8_t *public_key memcpy(ping_plain + 1, &ping_id, sizeof(ping_id)); pk[0] = NET_PACKET_PING_REQUEST; - id_copy(pk + 1, dht_get_self_public_key(ping->dht)); // Our pubkey + pk_copy(pk + 1, dht_get_self_public_key(ping->dht)); // Our pubkey random_nonce(pk + 1 + CRYPTO_PUBLIC_KEY_SIZE); // Generate new nonce @@ -96,7 +96,7 @@ static int ping_send_response(const Ping *ping, const IP_Port *ipp, const uint8_ { uint8_t pk[DHT_PING_SIZE]; - if (id_equal(public_key, dht_get_self_public_key(ping->dht))) { + if (pk_equal(public_key, dht_get_self_public_key(ping->dht))) { return 1; } @@ -105,7 +105,7 @@ static int ping_send_response(const Ping *ping, const IP_Port *ipp, const uint8_ memcpy(ping_plain + 1, &ping_id, sizeof(ping_id)); pk[0] = NET_PACKET_PING_RESPONSE; - id_copy(pk + 1, dht_get_self_public_key(ping->dht)); // Our pubkey + pk_copy(pk + 1, dht_get_self_public_key(ping->dht)); // Our pubkey random_nonce(pk + 1 + CRYPTO_PUBLIC_KEY_SIZE); // Generate new nonce // Encrypt ping_id using recipient privkey @@ -133,7 +133,7 @@ static int handle_ping_request(void *object, const IP_Port *source, const uint8_ Ping *ping = dht_get_ping(dht); - if (id_equal(packet + 1, dht_get_self_public_key(ping->dht))) { + if (pk_equal(packet + 1, dht_get_self_public_key(ping->dht))) { return 1; } @@ -182,7 +182,7 @@ static int handle_ping_response(void *object, const IP_Port *source, const uint8 Ping *ping = dht_get_ping(dht); - if (id_equal(packet + 1, dht_get_self_public_key(ping->dht))) { + if (pk_equal(packet + 1, dht_get_self_public_key(ping->dht))) { return 1; } @@ -217,7 +217,7 @@ static int handle_ping_response(void *object, const IP_Port *source, const uint8 return 1; } - if (!id_equal(packet + 1, data)) { + if (!pk_equal(packet + 1, data)) { return 1; } @@ -242,7 +242,7 @@ static int in_list(const Client_data *list, uint16_t length, const Mono_Time *mo const IP_Port *ip_port) { for (unsigned int i = 0; i < length; ++i) { - if (id_equal(list[i].public_key, public_key)) { + if (pk_equal(list[i].public_key, public_key)) { const IPPTsPng *ipptp; if (net_family_is_ipv4(ip_port->ip.family)) { @@ -299,7 +299,7 @@ int32_t ping_add(Ping *ping, const uint8_t *public_key, const IP_Port *ip_port) return 0; } - if (public_key_cmp(ping->to_ping[i].public_key, public_key) == 0) { + if (pk_equal(ping->to_ping[i].public_key, public_key)) { return -1; } } diff --git a/toxcore/tox.c b/toxcore/tox.c index 42f7a2647b..f6c69444a4 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -1428,7 +1428,7 @@ bool tox_friend_get_typing(const Tox *tox, uint32_t friend_number, Tox_Err_Frien } SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_QUERY_OK); - return !!ret; + return ret != 0; } void tox_callback_friend_typing(Tox *tox, tox_friend_typing_cb *callback) diff --git a/toxcore/util.c b/toxcore/util.c index 8174ef3fa2..43fa437eae 100644 --- a/toxcore/util.c +++ b/toxcore/util.c @@ -21,12 +21,12 @@ /** id functions */ -bool id_equal(const uint8_t *dest, const uint8_t *src) +bool pk_equal(const uint8_t *dest, const uint8_t *src) { - return public_key_cmp(dest, src) == 0; + return public_key_eq(dest, src); } -uint32_t id_copy(uint8_t *dest, const uint8_t *src) +uint32_t pk_copy(uint8_t *dest, const uint8_t *src) { memcpy(dest, src, CRYPTO_PUBLIC_KEY_SIZE); return CRYPTO_PUBLIC_KEY_SIZE; @@ -56,6 +56,11 @@ int create_recursive_mutex(pthread_mutex_t *mutex) return 0; } +bool memeq(const uint8_t *a, size_t a_size, const uint8_t *b, size_t b_size) +{ + return a_size == b_size && memcmp(a, b, a_size) == 0; +} + int16_t max_s16(int16_t a, int16_t b) { return a > b ? a : b; diff --git a/toxcore/util.h b/toxcore/util.h index 1232a75049..428f19ac26 100644 --- a/toxcore/util.h +++ b/toxcore/util.h @@ -20,13 +20,27 @@ extern "C" { #endif -/** id functions */ -non_null() bool id_equal(const uint8_t *dest, const uint8_t *src); -non_null() uint32_t id_copy(uint8_t *dest, const uint8_t *src); /* return value is CLIENT_ID_SIZE */ +/** Equality function for public keys. */ +non_null() bool pk_equal(const uint8_t *dest, const uint8_t *src); +/** + * @brief Copy a public key from `src` to `dest`. + * @retval CLIENT_ID_SIZE + */ +non_null() uint32_t pk_copy(uint8_t *dest, const uint8_t *src); /** Returns -1 if failed or 0 if success */ non_null() int create_recursive_mutex(pthread_mutex_t *mutex); +/** + * @brief Checks whether two buffers are the same length and contents. + * + * Calls `memcmp` after checking the sizes are equal. + * + * @retval true if sizes and contents are equal. + * @retval false otherwise. + */ +non_null() bool memeq(const uint8_t *a, size_t a_size, const uint8_t *b, size_t b_size); + // Safe min/max functions with specific types. This forces the conversion to the // desired type before the comparison expression, giving the choice of // conversion to the caller. Use these instead of inline comparisons or MIN/MAX diff --git a/toxcore/util_test.cc b/toxcore/util_test.cc index e8390a1789..c8b481fdd4 100644 --- a/toxcore/util_test.cc +++ b/toxcore/util_test.cc @@ -16,7 +16,7 @@ TEST(Util, TwoRandomIdsAreNotEqual) crypto_new_keypair(pk1, sk1); crypto_new_keypair(pk2, sk2); - EXPECT_FALSE(id_equal(pk1, pk2)); + EXPECT_FALSE(pk_equal(pk1, pk2)); } TEST(Util, IdCopyMakesKeysEqual) @@ -26,9 +26,9 @@ TEST(Util, IdCopyMakesKeysEqual) uint8_t pk2[CRYPTO_PUBLIC_KEY_SIZE] = {0}; crypto_new_keypair(pk1, sk1); - id_copy(pk2, pk1); + pk_copy(pk2, pk1); - EXPECT_TRUE(id_equal(pk1, pk2)); + EXPECT_TRUE(pk_equal(pk1, pk2)); } } // namespace