From 791fd013495bf77700bc1b3f7a73f836f11f1a31 Mon Sep 17 00:00:00 2001 From: jfreegman Date: Sat, 29 Jan 2022 11:14:56 -0500 Subject: [PATCH] Make some non-const pointers const --- toxav/groupav.c | 4 ++-- toxcore/LAN_discovery.c | 2 +- toxcore/Messenger.c | 2 +- toxcore/TCP_connection.c | 28 ++++++++++++++-------------- toxcore/TCP_server.c | 2 +- toxcore/friend_connection.c | 4 ++-- toxcore/group.c | 10 +++++----- toxcore/net_crypto.c | 14 +++++++------- toxcore/network.c | 16 ++++++++-------- toxcore/onion_announce.c | 4 ++-- toxcore/onion_client.c | 6 +++--- 11 files changed, 46 insertions(+), 46 deletions(-) diff --git a/toxav/groupav.c b/toxav/groupav.c index 81e74a4867..ded8a2dd9a 100644 --- a/toxav/groupav.c +++ b/toxav/groupav.c @@ -249,7 +249,7 @@ static Group_AV *new_group_av(const Logger *log, Tox *tox, Group_Chats *g_c, aud static void group_av_peer_new(void *object, uint32_t groupnumber, uint32_t friendgroupnumber) { - Group_AV *group_av = (Group_AV *)object; + const Group_AV *group_av = (const Group_AV *)object; Group_Peer_AV *peer_av = (Group_Peer_AV *)calloc(1, sizeof(Group_Peer_AV)); if (!peer_av) { @@ -570,7 +570,7 @@ int join_av_groupchat(const Logger *log, Tox *tox, Group_Chats *g_c, uint32_t fr * return 0 on success. * return -1 on failure. */ -static int send_audio_packet(Group_Chats *g_c, uint32_t groupnumber, uint8_t *packet, uint16_t length) +static int send_audio_packet(const Group_Chats *g_c, uint32_t groupnumber, const uint8_t *packet, uint16_t length) { if (length == 0 || length > MAX_CRYPTO_DATA_SIZE - 1 - sizeof(uint16_t)) { return -1; diff --git a/toxcore/LAN_discovery.c b/toxcore/LAN_discovery.c index 5c4b8544f3..f98f24ca60 100644 --- a/toxcore/LAN_discovery.c +++ b/toxcore/LAN_discovery.c @@ -173,7 +173,7 @@ static void fetch_broadcast_info(uint16_t port) continue; } - struct sockaddr_in *sock4 = (struct sockaddr_in *)(void *)&i_faces[i].ifr_broadaddr; + const struct sockaddr_in *sock4 = (const struct sockaddr_in *)(void *)&i_faces[i].ifr_broadaddr; if (count >= MAX_INTERFACES) { break; diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 1561e652fa..8bd208b90f 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -2257,7 +2257,7 @@ static int m_handle_packet(void *object, int i, const uint8_t *temp, uint16_t le memcpy(ft->id, data + 1 + sizeof(uint32_t) + sizeof(uint64_t), FILE_ID_LENGTH); VLA(uint8_t, filename_terminated, filename_length + 1); - uint8_t *filename = nullptr; + const uint8_t *filename = nullptr; if (filename_length) { /* Force NULL terminate file name. */ diff --git a/toxcore/TCP_connection.c b/toxcore/TCP_connection.c index e0b3b7ab44..90c833ab46 100644 --- a/toxcore/TCP_connection.c +++ b/toxcore/TCP_connection.c @@ -292,7 +292,7 @@ uint32_t tcp_connected_relays_count(const TCP_Connections *tcp_c) int send_packet_tcp_connection(const TCP_Connections *tcp_c, int connections_number, const uint8_t *packet, uint16_t length) { - TCP_Connection_to *con_to = get_connection(tcp_c, connections_number); + const TCP_Connection_to *con_to = get_connection(tcp_c, connections_number); if (!con_to) { return -1; @@ -489,7 +489,7 @@ void set_onion_packet_tcp_connection_callback(TCP_Connections *tcp_c, tcp_onion_ static int find_tcp_connection_to(const TCP_Connections *tcp_c, const uint8_t *public_key) { for (unsigned int i = 0; i < tcp_c->connections_length; ++i) { - TCP_Connection_to *con_to = get_connection(tcp_c, i); + const TCP_Connection_to *con_to = get_connection(tcp_c, i); if (con_to) { if (public_key_cmp(con_to->public_key, public_key) == 0) { @@ -509,7 +509,7 @@ static int find_tcp_connection_to(const TCP_Connections *tcp_c, const uint8_t *p static int find_tcp_connection_relay(const TCP_Connections *tcp_c, const uint8_t *relay_pk) { for (uint32_t i = 0; i < tcp_c->tcp_connections_length; ++i) { - TCP_con *tcp_con = get_tcp_connection(tcp_c, i); + const TCP_con *tcp_con = get_tcp_connection(tcp_c, i); if (tcp_con) { if (tcp_con->status == TCP_CONN_SLEEPING) { @@ -562,7 +562,7 @@ int new_tcp_connection_to(TCP_Connections *tcp_c, const uint8_t *public_key, int */ int kill_tcp_connection_to(TCP_Connections *tcp_c, int connections_number) { - TCP_Connection_to *con_to = get_connection(tcp_c, connections_number); + const TCP_Connection_to *con_to = get_connection(tcp_c, connections_number); if (!con_to) { return -1; @@ -959,8 +959,8 @@ static int tcp_response_callback(void *object, uint8_t connection_id, const uint static int tcp_status_callback(void *object, uint32_t number, uint8_t connection_id, uint8_t status) { - TCP_Client_Connection *tcp_client_con = (TCP_Client_Connection *)object; - TCP_Connections *tcp_c = (TCP_Connections *)tcp_con_custom_object(tcp_client_con); + const TCP_Client_Connection *tcp_client_con = (const TCP_Client_Connection *)object; + const TCP_Connections *tcp_c = (const TCP_Connections *)tcp_con_custom_object(tcp_client_con); unsigned int tcp_connections_number = tcp_con_custom_uint(tcp_client_con); TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); @@ -1002,17 +1002,17 @@ static int tcp_conn_data_callback(void *object, uint32_t number, uint8_t connect return -1; } - TCP_Client_Connection *tcp_client_con = (TCP_Client_Connection *)object; + const TCP_Client_Connection *tcp_client_con = (TCP_Client_Connection *)object; TCP_Connections *tcp_c = (TCP_Connections *)tcp_con_custom_object(tcp_client_con); unsigned int tcp_connections_number = tcp_con_custom_uint(tcp_client_con); - TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); + const TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); if (!tcp_con) { return -1; } - TCP_Connection_to *con_to = get_connection(tcp_c, number); + const TCP_Connection_to *con_to = get_connection(tcp_c, number); if (!con_to) { return -1; @@ -1032,11 +1032,11 @@ static int tcp_conn_oob_callback(void *object, const uint8_t *public_key, const return -1; } - TCP_Client_Connection *tcp_client_con = (TCP_Client_Connection *)object; + const TCP_Client_Connection *tcp_client_con = (const TCP_Client_Connection *)object; TCP_Connections *tcp_c = (TCP_Connections *)tcp_con_custom_object(tcp_client_con); unsigned int tcp_connections_number = tcp_con_custom_uint(tcp_client_con); - TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); + const TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); if (!tcp_con) { return -1; @@ -1253,7 +1253,7 @@ int add_tcp_relay_connection(TCP_Connections *tcp_c, int connections_number, IP_ tcp_connections_number = add_tcp_relay_instance(tcp_c, ip_port, relay_pk); - TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); + const TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); if (!tcp_con) { return -1; @@ -1292,7 +1292,7 @@ uint32_t tcp_copy_connected_relays(const TCP_Connections *tcp_c, Node_format *tc uint32_t copied = 0; for (uint32_t i = 0; (i < tcp_c->tcp_connections_length) && (copied < max_num); ++i) { - TCP_con *tcp_con = get_tcp_connection(tcp_c, (i + r) % tcp_c->tcp_connections_length); + const TCP_con *tcp_con = get_tcp_connection(tcp_c, (i + r) % tcp_c->tcp_connections_length); if (!tcp_con) { continue; @@ -1473,7 +1473,7 @@ static void kill_nonused_tcp(TCP_Connections *tcp_c) uint32_t kill_count = 0; for (uint32_t i = 0; i < tcp_c->tcp_connections_length && kill_count < max_kill_count; ++i) { - TCP_con *tcp_con = get_tcp_connection(tcp_c, i); + const TCP_con *tcp_con = get_tcp_connection(tcp_c, i); if (tcp_con == nullptr) { continue; diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c index 8cdfbeed8a..8155d922cc 100644 --- a/toxcore/TCP_server.c +++ b/toxcore/TCP_server.c @@ -481,7 +481,7 @@ static int handle_TCP_oob_send(TCP_Server *tcp_server, uint32_t con_id, const ui return -1; } - TCP_Secure_Connection *con = &tcp_server->accepted_connection_array[con_id]; + const TCP_Secure_Connection *con = &tcp_server->accepted_connection_array[con_id]; int other_index = get_TCP_connection_index(tcp_server, public_key); diff --git a/toxcore/friend_connection.c b/toxcore/friend_connection.c index abadf26e08..86f94b6d32 100644 --- a/toxcore/friend_connection.c +++ b/toxcore/friend_connection.c @@ -193,7 +193,7 @@ Friend_Conn *get_conn(const Friend_Connections *fr_c, int friendcon_id) int getfriend_conn_id_pk(const Friend_Connections *fr_c, const uint8_t *real_pk) { for (uint32_t i = 0; i < fr_c->num_cons; ++i) { - Friend_Conn *friend_con = get_conn(fr_c, i); + const Friend_Conn *friend_con = get_conn(fr_c, i); if (friend_con) { if (public_key_cmp(friend_con->real_public_key, real_pk) == 0) { @@ -502,7 +502,7 @@ static int handle_lossy_packet(void *object, int number, const uint8_t *data, ui return -1; } - Friend_Connections *const fr_c = (Friend_Connections *)object; + const Friend_Connections *const fr_c = (const Friend_Connections *)object; const Friend_Conn *friend_con = get_conn(fr_c, number); if (!friend_con) { diff --git a/toxcore/group.c b/toxcore/group.c index e20094187b..8d3a77f94b 100644 --- a/toxcore/group.c +++ b/toxcore/group.c @@ -914,7 +914,7 @@ static bool settitle(Group_Chats *g_c, uint32_t groupnumber, int peer_index, con /** Check if the group has no online connection, and freeze all peers if so */ static void check_disconnected(Group_Chats *g_c, uint32_t groupnumber, void *userdata) { - Group_c *g = get_group_c(g_c, groupnumber); + const Group_c *g = get_group_c(g_c, groupnumber); if (!g) { return; @@ -1432,7 +1432,7 @@ static unsigned int send_lossy_group_peer(const Friend_Connections *fr_c, int fr */ int invite_friend(const Group_Chats *g_c, uint32_t friendnumber, uint32_t groupnumber) { - Group_c *g = get_group_c(g_c, groupnumber); + const Group_c *g = get_group_c(g_c, groupnumber); if (!g) { return -1; @@ -1922,7 +1922,7 @@ static void handle_friend_invite_packet(Messenger *m, uint32_t friendnumber, con return; } else { - Group_c *g = get_group_c(g_c, groupnumber); + const Group_c *g = get_group_c(g_c, groupnumber); if (g && g->status == GROUPCHAT_STATUS_CONNECTED) { send_invite_response(g_c, groupnumber, friendnumber, invite_data, invite_length); @@ -2067,7 +2067,7 @@ static int send_packet_online(const Friend_Connections *fr_c, int friendcon_id, static bool ping_groupchat(const Group_Chats *g_c, uint32_t groupnumber); -static int handle_packet_online(Group_Chats *g_c, int friendcon_id, const uint8_t *data, uint16_t length) +static int handle_packet_online(const Group_Chats *g_c, int friendcon_id, const uint8_t *data, uint16_t length) { if (length != ONLINE_PACKET_DATA_SIZE) { return -1; @@ -2886,7 +2886,7 @@ static bool type_uses_lossy(uint8_t type) static int handle_lossy(void *object, int friendcon_id, const uint8_t *data, uint16_t length, void *userdata) { - Group_Chats *g_c = (Group_Chats *)object; + const Group_Chats *g_c = (const Group_Chats *)object; if (data[0] != PACKET_ID_LOSSY_CONFERENCE) { return -1; diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 5ea58e31d0..e3cf7fd30c 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -2164,7 +2164,7 @@ static int tcp_data_callback(void *object, int crypt_connection_id, const uint8_ return -1; } - Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); + const Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); if (conn == nullptr) { return -1; @@ -2224,7 +2224,7 @@ static int tcp_oob_callback(void *object, const uint8_t *public_key, unsigned in */ int add_tcp_relay_peer(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port, const uint8_t *public_key) { - Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); + const Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); if (conn == nullptr) { return -1; @@ -2306,7 +2306,7 @@ static void do_tcp(Net_Crypto *c, void *userdata) pthread_mutex_unlock(&c->tcp_mutex); for (uint32_t i = 0; i < c->crypto_connections_length; ++i) { - Crypto_Connection *conn = get_crypto_connection(c, i); + const Crypto_Connection *conn = get_crypto_connection(c, i); if (conn == nullptr) { continue; @@ -2756,7 +2756,7 @@ bool max_speed_reached(Net_Crypto *c, int crypt_connection_id) */ uint32_t crypto_num_free_sendqueue_slots(const Net_Crypto *c, int crypt_connection_id) { - Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); + const Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); if (conn == nullptr) { return 0; @@ -2836,7 +2836,7 @@ int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, const uint8_t */ int cryptpacket_received(const Net_Crypto *c, int crypt_connection_id, uint32_t packet_number) { - Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); + const Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); if (conn == nullptr) { return -1; @@ -2926,7 +2926,7 @@ int crypto_kill(Net_Crypto *c, int crypt_connection_id) bool crypto_connection_status(const Net_Crypto *c, int crypt_connection_id, bool *direct_connected, unsigned int *online_tcp_relays) { - Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); + const Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); if (conn == nullptr) { return false; @@ -3031,7 +3031,7 @@ Net_Crypto *new_net_crypto(const Logger *log, Mono_Time *mono_time, DHT *dht, co static void kill_timedout(Net_Crypto *c, void *userdata) { for (uint32_t i = 0; i < c->crypto_connections_length; ++i) { - Crypto_Connection *conn = get_crypto_connection(c, i); + const Crypto_Connection *conn = get_crypto_connection(c, i); if (conn == nullptr) { continue; diff --git a/toxcore/network.c b/toxcore/network.c index fc88d02710..8178efd570 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -662,7 +662,7 @@ static int receivepacket(const Logger *log, Socket sock, IP_Port *ip_port, uint8 *length = (uint32_t)fail_or_len; if (addr.ss_family == AF_INET) { - struct sockaddr_in *addr_in = (struct sockaddr_in *)&addr; + const struct sockaddr_in *addr_in = (const struct sockaddr_in *)&addr; const Family *const family = make_tox_family(addr_in->sin_family); assert(family != nullptr); @@ -675,7 +675,7 @@ static int receivepacket(const Logger *log, Socket sock, IP_Port *ip_port, uint8 get_ip4(&ip_port->ip.ip.v4, &addr_in->sin_addr); ip_port->port = addr_in->sin_port; } else if (addr.ss_family == AF_INET6) { - struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)&addr; + const struct sockaddr_in6 *addr_in6 = (const struct sockaddr_in6 *)&addr; const Family *const family = make_tox_family(addr_in6->sin6_family); assert(family != nullptr); @@ -1342,12 +1342,12 @@ int addr_resolve(const char *address, IP *to, IP *extra) switch (walker->ai_family) { case AF_INET: { if (walker->ai_family == family) { /* AF_INET requested, done */ - struct sockaddr_in *addr = (struct sockaddr_in *)(void *)walker->ai_addr; + const struct sockaddr_in *addr = (const struct sockaddr_in *)(void *)walker->ai_addr; 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 */ - struct sockaddr_in *addr = (struct sockaddr_in *)(void *)walker->ai_addr; + 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; } @@ -1358,14 +1358,14 @@ int addr_resolve(const char *address, IP *to, IP *extra) case AF_INET6: { if (walker->ai_family == family) { /* AF_INET6 requested, done */ if (walker->ai_addrlen == sizeof(struct sockaddr_in6)) { - struct sockaddr_in6 *addr = (struct sockaddr_in6 *)(void *)walker->ai_addr; + const struct sockaddr_in6 *addr = (const struct sockaddr_in6 *)(void *)walker->ai_addr; get_ip6(&to->ip.v6, &addr->sin6_addr); result = TOX_ADDR_RESOLVE_INET6; done = true; } } else if (!(result & TOX_ADDR_RESOLVE_INET6)) { /* AF_UNSPEC requested, store away */ if (walker->ai_addrlen == sizeof(struct sockaddr_in6)) { - struct sockaddr_in6 *addr = (struct sockaddr_in6 *)(void *)walker->ai_addr; + const struct sockaddr_in6 *addr = (const struct sockaddr_in6 *)(void *)walker->ai_addr; get_ip6(&ip6.ip.v6, &addr->sin6_addr); result |= TOX_ADDR_RESOLVE_INET6; } @@ -1495,10 +1495,10 @@ int32_t net_getipport(const char *node, IP_Port **res, int tox_type) } if (cur->ai_family == AF_INET) { - struct sockaddr_in *addr = (struct sockaddr_in *)(void *)cur->ai_addr; + const struct sockaddr_in *addr = (const struct sockaddr_in *)(const void *)cur->ai_addr; memcpy(&ip_port->ip.ip.v4, &addr->sin_addr, sizeof(IP4)); } else if (cur->ai_family == AF_INET6) { - struct sockaddr_in6 *addr = (struct sockaddr_in6 *)(void *)cur->ai_addr; + const struct sockaddr_in6 *addr = (const struct sockaddr_in6 *)(const void *)cur->ai_addr; memcpy(&ip_port->ip.ip.v6, &addr->sin6_addr, sizeof(IP6)); } else { continue; diff --git a/toxcore/onion_announce.c b/toxcore/onion_announce.c index a4ac495ff8..139ff9b501 100644 --- a/toxcore/onion_announce.c +++ b/toxcore/onion_announce.c @@ -390,7 +390,7 @@ static int handle_announce_request(void *object, IP_Port source, const uint8_t * int index; - uint8_t *data_public_key = plain + ONION_PING_ID_SIZE + CRYPTO_PUBLIC_KEY_SIZE; + const uint8_t *data_public_key = plain + ONION_PING_ID_SIZE + CRYPTO_PUBLIC_KEY_SIZE; if (onion_ping_id_eq(ping_id1, plain) || onion_ping_id_eq(ping_id2, plain)) { @@ -461,7 +461,7 @@ static int handle_announce_request(void *object, IP_Port source, const uint8_t * static int handle_data_request(void *object, IP_Port source, const uint8_t *packet, uint16_t length, void *userdata) { - Onion_Announce *onion_a = (Onion_Announce *)object; + const Onion_Announce *onion_a = (const Onion_Announce *)object; if (length <= DATA_REQUEST_MIN_SIZE_RECV) { return 1; diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index da07e01287..4503063b2e 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c @@ -765,7 +765,7 @@ static int client_ping_nodes(Onion_Client *onion_c, uint32_t num, const Node_for return 0; } - Onion_Node *list_nodes = nullptr; + const Onion_Node *list_nodes = nullptr; const uint8_t *reference_id = nullptr; unsigned int list_length; @@ -1037,7 +1037,7 @@ 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; - Onion_Node *list_nodes = onion_c->friends_list[friend_num].clients_list; + const Onion_Node *list_nodes = 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)) { @@ -1709,7 +1709,7 @@ static void do_announce(Onion_Client *onion_c) if (count != MAX_ONION_CLIENTS_ANNOUNCE) { uint16_t num_nodes; - Node_format *path_nodes; + const Node_format *path_nodes; if (random_u08() % 2 == 0 || onion_c->path_nodes_index == 0) { num_nodes = min_u16(onion_c->path_nodes_index_bs, MAX_PATH_NODES);