From 99d594014014a37fdee9e83575a8895400c9cd60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Sch=C3=BCtz?= Date: Tue, 10 Jun 2014 20:54:48 +0200 Subject: [PATCH] Const correctness in various interdependent files --- testing/Messenger_test.c | 2 +- toxcore/DHT.c | 108 +++++++++++++++++++------------------- toxcore/DHT.h | 42 +++++++-------- toxcore/Messenger.c | 22 ++++---- toxcore/Messenger.h | 14 ++--- toxcore/friend_requests.c | 10 ++-- toxcore/friend_requests.h | 8 +-- toxcore/group_chats.c | 2 +- toxcore/group_chats.h | 2 +- toxcore/net_crypto.c | 2 +- toxcore/net_crypto.h | 2 +- toxcore/onion_client.c | 12 ++--- toxcore/onion_client.h | 8 +-- toxcore/ping.c | 10 ++-- toxcore/ping.h | 4 +- toxcore/ping_array.c | 2 +- toxcore/ping_array.h | 2 +- toxcore/tox.c | 4 +- toxcore/tox.h | 4 +- toxcore/util.c | 2 +- toxcore/util.h | 4 +- 21 files changed, 133 insertions(+), 133 deletions(-) diff --git a/testing/Messenger_test.c b/testing/Messenger_test.c index a6efb433..7c1d64e7 100644 --- a/testing/Messenger_test.c +++ b/testing/Messenger_test.c @@ -66,7 +66,7 @@ void print_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t len * networking_requesthandler and so cannot take a Messenger * */ static Messenger *m; -void print_request(Messenger *m, uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata) +void print_request(Messenger *m, const uint8_t *public_key, const uint8_t *data, uint16_t length, void *userdata) { printf("Friend request received from: \n"); printf("ClientID: "); diff --git a/toxcore/DHT.c b/toxcore/DHT.c index fed12861..92d6e159 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -83,7 +83,7 @@ Client_data *DHT_get_close_list(DHT *dht) * return 1 if client_id1 is closer. * return 2 if client_id2 is closer. */ -int id_closest(uint8_t *id, uint8_t *id1, uint8_t *id2) +int id_closest(const uint8_t *id, const uint8_t *id1, const uint8_t *id2) { size_t i; uint8_t distance1, distance2; @@ -106,7 +106,7 @@ int id_closest(uint8_t *id, uint8_t *id1, uint8_t *id2) /* Turns the result of id_closest into something quick_sort can use. * Assumes p1->c1 == p2->c1. */ -static int client_id_cmp(ClientPair p1, ClientPair p2) +static int client_id_cmp(const ClientPair p1, const ClientPair p2) { int c = id_closest(p1.c1.client_id, p1.c2.client_id, p2.c2.client_id); @@ -122,7 +122,7 @@ static int client_id_cmp(ClientPair p1, ClientPair p2) * If shared key is already in shared_keys, copy it to shared_key. * else generate it into shared_key and copy it to shared_keys */ -void get_shared_key(Shared_Keys *shared_keys, uint8_t *shared_key, uint8_t *secret_key, uint8_t *client_id) +void get_shared_key(Shared_Keys *shared_keys, uint8_t *shared_key, const uint8_t *secret_key, const uint8_t *client_id) { uint32_t i, num = ~0, curr = 0; @@ -168,7 +168,7 @@ void get_shared_key(Shared_Keys *shared_keys, uint8_t *shared_key, uint8_t *secr /* Copy shared_key to encrypt/decrypt DHT packet from client_id into shared_key * for packets that we receive. */ -void DHT_get_shared_key_recv(DHT *dht, uint8_t *shared_key, uint8_t *client_id) +void DHT_get_shared_key_recv(DHT *dht, uint8_t *shared_key, const uint8_t *client_id) { return get_shared_key(&dht->shared_keys_recv, shared_key, dht->self_secret_key, client_id); } @@ -176,7 +176,7 @@ void DHT_get_shared_key_recv(DHT *dht, uint8_t *shared_key, uint8_t *client_id) /* Copy shared_key to encrypt/decrypt DHT packet from client_id into shared_key * for packets that we send. */ -void DHT_get_shared_key_sent(DHT *dht, uint8_t *shared_key, uint8_t *client_id) +void DHT_get_shared_key_sent(DHT *dht, uint8_t *shared_key, const uint8_t *client_id) { return get_shared_key(&dht->shared_keys_sent, shared_key, dht->self_secret_key, client_id); } @@ -202,7 +202,7 @@ void to_host_family(IP *ip) * return length of packed nodes on success. * return -1 on failure. */ -int pack_nodes(uint8_t *data, uint16_t length, Node_format *nodes, uint16_t number) +int pack_nodes(uint8_t *data, uint16_t length, const Node_format *nodes, uint16_t number) { uint32_t i, packed_length = 0; @@ -263,7 +263,7 @@ int pack_nodes(uint8_t *data, uint16_t length, Node_format *nodes, uint16_t numb * return number of unpacked nodes on success. * return -1 on failure. */ -int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint16_t *processed_data_len, uint8_t *data, +int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint16_t *processed_data_len, const uint8_t *data, uint16_t length, uint8_t tcp_enabled) { uint32_t num = 0, len_processed = 0; @@ -338,7 +338,7 @@ int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint16_t *processed * * return True(1) or False(0) */ -static int client_or_ip_port_in_list(Client_data *list, uint32_t length, uint8_t *client_id, IP_Port ip_port) +static int client_or_ip_port_in_list(Client_data *list, uint32_t length, const uint8_t *client_id, IP_Port ip_port) { uint32_t i; uint64_t temp_time = unix_time(); @@ -417,7 +417,7 @@ static int client_or_ip_port_in_list(Client_data *list, uint32_t length, uint8_t * return 1 if true. * return 0 if false. */ -static int client_in_nodelist(Node_format *list, uint32_t length, uint8_t *client_id) +static int client_in_nodelist(const Node_format *list, uint32_t length, const uint8_t *client_id) { uint32_t i; @@ -432,7 +432,7 @@ static int client_in_nodelist(Node_format *list, uint32_t length, uint8_t *clien /* return friend number from the client_id. * return -1 if a failure occurs. */ -static int friend_number(DHT *dht, uint8_t *client_id) +static int friend_number(const DHT *dht, const uint8_t *client_id) { uint32_t i; @@ -452,15 +452,15 @@ static int friend_number(DHT *dht, uint8_t *client_id) * return 4 if it can test other nodes correctly * return HARDENING_ALL_OK if all ok. */ -static uint8_t hardening_correct(Hardening *h) +static uint8_t hardening_correct(const Hardening *h) { return h->routes_requests_ok + (h->send_nodes_ok << 1) + (h->testing_requests << 2); } /* * helper for get_close_nodes(). argument list is a monster :D */ -static void get_close_nodes_inner(uint8_t *client_id, Node_format *nodes_list, - sa_family_t sa_family, Client_data *client_list, uint32_t client_list_length, +static void get_close_nodes_inner(const uint8_t *client_id, Node_format *nodes_list, + sa_family_t sa_family, const Client_data *client_list, uint32_t client_list_length, uint32_t *num_nodes_ptr, uint8_t is_LAN, uint8_t want_good) { if ((sa_family != AF_INET) && (sa_family != AF_INET6) && (sa_family != 0)) @@ -471,13 +471,13 @@ static void get_close_nodes_inner(uint8_t *client_id, Node_format *nodes_list, uint32_t i; for (i = 0; i < client_list_length; i++) { - Client_data *client = &client_list[i]; + const Client_data *client = &client_list[i]; /* node already in list? */ if (client_in_nodelist(nodes_list, MAX_SENT_NODES, client->client_id)) continue; - IPPTsPng *ipptp = NULL; + const IPPTsPng *ipptp = NULL; if (sa_family == AF_INET) { ipptp = &client->assoc4; @@ -544,7 +544,7 @@ static void get_close_nodes_inner(uint8_t *client_id, Node_format *nodes_list, * * want_good : do we want only good nodes as checked with the hardening returned or not? */ -static int get_somewhat_close_nodes(DHT *dht, uint8_t *client_id, Node_format *nodes_list, sa_family_t sa_family, +static int get_somewhat_close_nodes(const DHT *dht, const uint8_t *client_id, Node_format *nodes_list, sa_family_t sa_family, uint8_t is_LAN, uint8_t want_good) { uint32_t num_nodes = 0, i; @@ -565,7 +565,7 @@ static int get_somewhat_close_nodes(DHT *dht, uint8_t *client_id, Node_format *n return num_nodes; } -int get_close_nodes(DHT *dht, uint8_t *client_id, Node_format *nodes_list, sa_family_t sa_family, uint8_t is_LAN, +int get_close_nodes(const DHT *dht, const uint8_t *client_id, Node_format *nodes_list, sa_family_t sa_family, uint8_t is_LAN, uint8_t want_good) { memset(nodes_list, 0, MAX_SENT_NODES * sizeof(Node_format)); @@ -632,7 +632,7 @@ int get_close_nodes(DHT *dht, uint8_t *client_id, Node_format *nodes_list, sa_fa */ static int replace_bad( Client_data *list, uint32_t length, - uint8_t *client_id, + const uint8_t *client_id, IP_Port ip_port ) { if ((ip_port.ip.family != AF_INET) && (ip_port.ip.family != AF_INET6)) @@ -680,7 +680,7 @@ static int replace_bad( Client_data *list, /* Sort the list. It will be sorted from furthest to closest. * Turns list into data that quick sort can use and reverts it back. */ -static void sort_list(Client_data *list, uint32_t length, uint8_t *comp_client_id) +static void sort_list(Client_data *list, uint32_t length, const uint8_t *comp_client_id) { Client_data cd; ClientPair pairs[length]; @@ -706,9 +706,9 @@ static void sort_list(Client_data *list, uint32_t length, uint8_t *comp_client_i */ static int replace_possible_bad( Client_data *list, uint32_t length, - uint8_t *client_id, + const uint8_t *client_id, IP_Port ip_port, - uint8_t *comp_client_id ) + const uint8_t *comp_client_id ) { if ((ip_port.ip.family != AF_INET) && (ip_port.ip.family != AF_INET6)) return 1; @@ -762,9 +762,9 @@ static int replace_possible_bad( Client_data *list, * returns 0 when the item was stored, 1 otherwise */ static int replace_good( Client_data *list, uint32_t length, - uint8_t *client_id, + const uint8_t *client_id, IP_Port ip_port, - uint8_t *comp_client_id ) + const uint8_t *comp_client_id ) { if ((ip_port.ip.family != AF_INET) && (ip_port.ip.family != AF_INET6)) return 1; @@ -825,7 +825,7 @@ static int replace_good( Client_data *list, * * returns 1+ if the item is used in any list, 0 else */ -int addto_lists(DHT *dht, IP_Port ip_port, uint8_t *client_id) +int addto_lists(DHT *dht, IP_Port ip_port, const uint8_t *client_id) { uint32_t i, used = 0; @@ -892,7 +892,7 @@ int addto_lists(DHT *dht, IP_Port ip_port, uint8_t *client_id) /* If client_id is a friend or us, update ret_ip_port * nodeclient_id is the id of the node that sent us this info. */ -static int returnedip_ports(DHT *dht, IP_Port ip_port, uint8_t *client_id, uint8_t *nodeclient_id) +static int returnedip_ports(DHT *dht, IP_Port ip_port, const uint8_t *client_id, const uint8_t *nodeclient_id) { uint32_t i, j; uint64_t temp_time = unix_time(); @@ -961,7 +961,7 @@ end: /* Send a getnodes request. sendback_node is the node that it will send back the response to (set to NULL to disable this) */ -static int getnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *client_id, Node_format *sendback_node) +static int getnodes(DHT *dht, IP_Port ip_port, const uint8_t *public_key, const uint8_t *client_id, const Node_format *sendback_node) { /* Check if packet is going to be sent to ourself. */ if (id_equal(public_key, dht->self_public_key)) @@ -1017,8 +1017,8 @@ static int getnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cli } /* Send a send nodes response: message for IPv6 nodes */ -static int sendnodes_ipv6(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *client_id, uint8_t *sendback_data, - uint16_t length, uint8_t *shared_encryption_key) +static int sendnodes_ipv6(const DHT *dht, IP_Port ip_port, const uint8_t *public_key, const uint8_t *client_id, 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)) @@ -1105,7 +1105,7 @@ static int handle_getnodes(void *object, IP_Port source, uint8_t *packet, uint32 } /* return 0 if no return 1 if yes */ -static uint8_t sent_getnode_to_node(DHT *dht, uint8_t *client_id, IP_Port node_ip_port, uint64_t ping_id, +static uint8_t sent_getnode_to_node(DHT *dht, const uint8_t *client_id, IP_Port node_ip_port, uint64_t ping_id, Node_format *sendback_node) { uint8_t data[sizeof(Node_format) * 2]; @@ -1128,10 +1128,10 @@ static uint8_t sent_getnode_to_node(DHT *dht, uint8_t *client_id, IP_Port node_i } /* Function is needed in following functions. */ -static int send_hardening_getnode_res(DHT *dht, Node_format *sendto, uint8_t *queried_client_id, uint8_t *nodes_data, +static int send_hardening_getnode_res(const DHT *dht, const Node_format *sendto, const uint8_t *queried_client_id, const uint8_t *nodes_data, uint16_t nodes_data_length); -static int handle_sendnodes_core(void *object, IP_Port source, uint8_t *packet, uint32_t length, +static int handle_sendnodes_core(void *object, IP_Port source, const uint8_t *packet, uint32_t length, Node_format *plain_nodes, uint16_t size_plain_nodes, uint32_t *num_nodes_out) { DHT *dht = object; @@ -1244,7 +1244,7 @@ static void get_bunchnodes(DHT *dht, Client_data *list, uint16_t length, uint16_ } } */ -int DHT_addfriend(DHT *dht, uint8_t *client_id) +int DHT_addfriend(DHT *dht, const uint8_t *client_id) { if (friend_number(dht, client_id) != -1) /* Is friend already in DHT? */ return 1; @@ -1298,7 +1298,7 @@ int DHT_addfriend(DHT *dht, uint8_t *client_id) return 0; } -int DHT_delfriend(DHT *dht, uint8_t *client_id) +int DHT_delfriend(DHT *dht, const uint8_t *client_id) { uint32_t i; DHT_Friend *temp; @@ -1334,7 +1334,7 @@ int DHT_delfriend(DHT *dht, uint8_t *client_id) } /* TODO: Optimize this. */ -int DHT_getfriendip(DHT *dht, uint8_t *client_id, IP_Port *ip_port) +int DHT_getfriendip(const DHT *dht, const uint8_t *client_id, IP_Port *ip_port) { uint32_t i, j; @@ -1367,7 +1367,7 @@ int DHT_getfriendip(DHT *dht, uint8_t *client_id, IP_Port *ip_port) } /* returns number of nodes not in kill-timeout */ -static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, uint8_t *client_id, +static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, const uint8_t *client_id, Client_data *list, uint32_t list_count, uint32_t *bootstrap_times) { uint32_t i; @@ -1454,12 +1454,12 @@ static void do_Close(DHT *dht) } } -void DHT_getnodes(DHT *dht, IP_Port *from_ipp, uint8_t *from_id, uint8_t *which_id) +void DHT_getnodes(DHT *dht, const IP_Port *from_ipp, const uint8_t *from_id, const uint8_t *which_id) { getnodes(dht, *from_ipp, from_id, which_id, NULL); } -void DHT_bootstrap(DHT *dht, IP_Port ip_port, uint8_t *public_key) +void DHT_bootstrap(DHT *dht, IP_Port ip_port, const uint8_t *public_key) { /*#ifdef ENABLE_ASSOC_DHT if (dht->assoc) { @@ -1474,7 +1474,7 @@ void DHT_bootstrap(DHT *dht, IP_Port ip_port, uint8_t *public_key) getnodes(dht, ip_port, public_key, dht->self_public_key, NULL); } int DHT_bootstrap_from_address(DHT *dht, const char *address, uint8_t ipv6enabled, - uint16_t port, uint8_t *public_key) + uint16_t port, const uint8_t *public_key) { IP_Port ip_port_v64; IP *ip_extra = NULL; @@ -1506,13 +1506,13 @@ int DHT_bootstrap_from_address(DHT *dht, const char *address, uint8_t ipv6enable * * return -1 if failure. */ -int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length) +int route_packet(const DHT *dht, const uint8_t *client_id, const uint8_t *packet, uint32_t length) { uint32_t i; for (i = 0; i < LCLIENT_LIST; ++i) { if (id_equal(client_id, dht->close_clientlist[i].client_id)) { - Client_data *client = &dht->close_clientlist[i]; + const Client_data *client = &dht->close_clientlist[i]; if (ip_isset(&client->assoc6.ip_port.ip)) return sendpacket(dht->net, client->assoc6.ip_port, packet, length); @@ -1533,7 +1533,7 @@ int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length) * return 0 if we are connected to friend or if no ips were found. * return -1 if no such friend. */ -static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num) +static int friend_iplist(const DHT *dht, IP_Port *ip_portlist, uint16_t friend_num) { if (friend_num >= dht->num_friends) return -1; @@ -1601,7 +1601,7 @@ static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num) * return ip for friend. * return number of nodes the packet was sent to. (Only works if more than (MAX_FRIEND_CLIENTS / 4). */ -int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t length) +int route_tofriend(const DHT *dht, const uint8_t *friend_id, const uint8_t *packet, uint32_t length) { int num = friend_number(dht, friend_id); @@ -1656,7 +1656,7 @@ int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t lengt * * return number of nodes the packet was sent to. */ -static int routeone_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t length) +static int routeone_tofriend(DHT *dht, const uint8_t *friend_id, const uint8_t *packet, uint32_t length) { int num = friend_number(dht, friend_id); @@ -1709,7 +1709,7 @@ static int routeone_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint * return 0 if we are connected to friend or if no ips were found. * return -1 if no such friend. */ -int friend_ips(DHT *dht, IP_Port *ip_portlist, uint8_t *friend_id) +int friend_ips(const DHT *dht, IP_Port *ip_portlist, const uint8_t *friend_id) { uint32_t i; @@ -1725,7 +1725,7 @@ int friend_ips(DHT *dht, IP_Port *ip_portlist, uint8_t *friend_id) /*----------------------------------------------------------------------------------*/ /*---------------------BEGINNING OF NAT PUNCHING FUNCTIONS--------------------------*/ -static int send_NATping(DHT *dht, uint8_t *public_key, uint64_t ping_id, uint8_t type) +static int send_NATping(DHT *dht, const uint8_t *public_key, uint64_t ping_id, uint8_t type) { uint8_t data[sizeof(uint64_t) + 1]; uint8_t packet[MAX_CRYPTO_REQUEST_SIZE]; @@ -1753,7 +1753,7 @@ static int send_NATping(DHT *dht, uint8_t *public_key, uint64_t ping_id, uint8_t } /* Handle a received ping request for. */ -static int handle_NATping(void *object, IP_Port source, uint8_t *source_pubkey, uint8_t *packet, uint32_t length) +static int handle_NATping(void *object, IP_Port source, const uint8_t *source_pubkey, const uint8_t *packet, uint32_t length) { if (length != sizeof(uint64_t) + 1) return 1; @@ -1962,7 +1962,7 @@ static int send_hardening_getnode_req(DHT *dht, Node_format *dest, Node_format * } /* Send a get node hardening response */ -static int send_hardening_getnode_res(DHT *dht, Node_format *sendto, uint8_t *queried_client_id, uint8_t *nodes_data, +static int send_hardening_getnode_res(const DHT *dht, const Node_format *sendto, const uint8_t *queried_client_id, const uint8_t *nodes_data, uint16_t nodes_data_length) { if (!ip_isset(&sendto->ip_port.ip)) @@ -1983,7 +1983,7 @@ static int send_hardening_getnode_res(DHT *dht, Node_format *sendto, uint8_t *qu } /* TODO: improve */ -static IPPTsPng *get_closelist_IPPTsPng(DHT *dht, uint8_t *client_id, sa_family_t sa_family) +static IPPTsPng *get_closelist_IPPTsPng(DHT *dht, const uint8_t *client_id, sa_family_t sa_family) { uint32_t i; @@ -2032,7 +2032,7 @@ static uint32_t have_nodes_closelist(DHT *dht, Node_format *nodes, uint16_t num) #define HARDEN_TIMEOUT 1200 /* Handle a received hardening packet */ -static int handle_hardening(void *object, IP_Port source, uint8_t *source_pubkey, uint8_t *packet, uint32_t length) +static int handle_hardening(void *object, IP_Port source, const uint8_t *source_pubkey, const uint8_t *packet, uint32_t length) { DHT *dht = object; @@ -2422,7 +2422,7 @@ void kill_DHT(DHT *dht) #define DHT_STATE_TYPE_CLIENTS_ASSOC46 4 /* Get the size of the DHT (for saving). */ -uint32_t DHT_size(DHT *dht) +uint32_t DHT_size(const DHT *dht) { uint32_t num = 0, i; @@ -2481,7 +2481,7 @@ void DHT_save(DHT *dht, uint8_t *data) } } -static int dht_load_state_callback(void *outer, uint8_t *data, uint32_t length, uint16_t type) +static int dht_load_state_callback(void *outer, const uint8_t *data, uint32_t length, uint16_t type) { DHT *dht = outer; uint32_t num, i, j; @@ -2547,7 +2547,7 @@ static int dht_load_state_callback(void *outer, uint8_t *data, uint32_t length, * return -1 if failure. * return 0 if success. */ -int DHT_load(DHT *dht, uint8_t *data, uint32_t length) +int DHT_load(DHT *dht, const uint8_t *data, uint32_t length) { uint32_t cookie_len = sizeof(uint32_t); @@ -2564,13 +2564,13 @@ int DHT_load(DHT *dht, uint8_t *data, uint32_t length) /* return 0 if we are not connected to the DHT. * return 1 if we are. */ -int DHT_isconnected(DHT *dht) +int DHT_isconnected(const DHT *dht) { uint32_t i; unix_time_update(); for (i = 0; i < LCLIENT_LIST; ++i) { - Client_data *client = &dht->close_clientlist[i]; + const Client_data *client = &dht->close_clientlist[i]; if (!is_timeout(client->assoc4.timestamp, BAD_NODE_TIMEOUT) || !is_timeout(client->assoc6.timestamp, BAD_NODE_TIMEOUT)) diff --git a/toxcore/DHT.h b/toxcore/DHT.h index 57813132..97a7109f 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h @@ -147,7 +147,7 @@ Node_format; * return length of packed nodes on success. * return -1 on failure. */ -int pack_nodes(uint8_t *data, uint16_t length, Node_format *nodes, uint16_t number); +int pack_nodes(uint8_t *data, uint16_t length, const Node_format *nodes, uint16_t number); /* Unpack data of length into nodes of size max_num_nodes. * Put the length of the data processed in processed_data_len. @@ -156,7 +156,7 @@ int pack_nodes(uint8_t *data, uint16_t length, Node_format *nodes, uint16_t numb * return number of unpacked nodes on success. * return -1 on failure. */ -int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint16_t *processed_data_len, uint8_t *data, +int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint16_t *processed_data_len, const uint8_t *data, uint16_t length, uint8_t tcp_enabled); @@ -176,7 +176,7 @@ typedef struct { /*----------------------------------------------------------------------------------*/ -typedef int (*cryptopacket_handler_callback)(void *object, IP_Port ip_port, uint8_t *source_pubkey, uint8_t *data, +typedef int (*cryptopacket_handler_callback)(void *object, IP_Port ip_port, const uint8_t *source_pubkey, const uint8_t *data, uint32_t len); typedef struct { @@ -221,19 +221,19 @@ typedef struct { * If shared key is already in shared_keys, copy it to shared_key. * else generate it into shared_key and copy it to shared_keys */ -void get_shared_key(Shared_Keys *shared_keys, uint8_t *shared_key, uint8_t *secret_key, uint8_t *client_id); +void get_shared_key(Shared_Keys *shared_keys, uint8_t *shared_key, const uint8_t *secret_key, const uint8_t *client_id); /* Copy shared_key to encrypt/decrypt DHT packet from client_id into shared_key * for packets that we receive. */ -void DHT_get_shared_key_recv(DHT *dht, uint8_t *shared_key, uint8_t *client_id); +void DHT_get_shared_key_recv(DHT *dht, uint8_t *shared_key, const uint8_t *client_id); /* Copy shared_key to encrypt/decrypt DHT packet from client_id into shared_key * for packets that we send. */ -void DHT_get_shared_key_sent(DHT *dht, uint8_t *shared_key, uint8_t *client_id); +void DHT_get_shared_key_sent(DHT *dht, uint8_t *shared_key, const uint8_t *client_id); -void DHT_getnodes(DHT *dht, IP_Port *from_ipp, uint8_t *from_id, uint8_t *which_id); +void DHT_getnodes(DHT *dht, const IP_Port *from_ipp, const uint8_t *from_id, const uint8_t *which_id); /* Add a new friend to the friends list. * client_id must be CLIENT_ID_SIZE bytes long. @@ -241,7 +241,7 @@ void DHT_getnodes(DHT *dht, IP_Port *from_ipp, uint8_t *from_id, uint8_t *which_ * return 0 if success. * return 1 if failure (friends list is full). */ -int DHT_addfriend(DHT *dht, uint8_t *client_id); +int DHT_addfriend(DHT *dht, const uint8_t *client_id); /* Delete a friend from the friends list. * client_id must be CLIENT_ID_SIZE bytes long. @@ -249,7 +249,7 @@ int DHT_addfriend(DHT *dht, uint8_t *client_id); * return 0 if success. * return 1 if failure (client_id not in friends list). */ -int DHT_delfriend(DHT *dht, uint8_t *client_id); +int DHT_delfriend(DHT *dht, const uint8_t *client_id); /* Get ip of friend. * client_id must be CLIENT_ID_SIZE bytes long. @@ -270,7 +270,7 @@ int DHT_delfriend(DHT *dht, uint8_t *client_id); * return 0, -- if client_id refers to a friend and we failed to find the friend (yet) * return 1, ip if client_id refers to a friend and we found him */ -int DHT_getfriendip(DHT *dht, uint8_t *client_id, IP_Port *ip_port); +int DHT_getfriendip(const DHT *dht, const uint8_t *client_id, IP_Port *ip_port); /* Compares client_id1 and client_id2 with client_id. * @@ -278,7 +278,7 @@ int DHT_getfriendip(DHT *dht, uint8_t *client_id, IP_Port *ip_port); * return 1 if client_id1 is closer. * return 2 if client_id2 is closer. */ -int id_closest(uint8_t *id, uint8_t *id1, uint8_t *id2); +int id_closest(const uint8_t *id, const uint8_t *id1, const uint8_t *id2); /* Get the (maximum MAX_SENT_NODES) closest nodes to client_id we know * and put them in nodes_list (must be MAX_SENT_NODES big). @@ -290,7 +290,7 @@ int id_closest(uint8_t *id, uint8_t *id1, uint8_t *id2); * return the number of nodes returned. * */ -int get_close_nodes(DHT *dht, uint8_t *client_id, Node_format *nodes_list, sa_family_t sa_family, uint8_t is_LAN, +int get_close_nodes(const DHT *dht, const uint8_t *client_id, Node_format *nodes_list, sa_family_t sa_family, uint8_t is_LAN, uint8_t want_good); @@ -317,7 +317,7 @@ void do_DHT(DHT *dht); /* Sends a "get nodes" request to the given node with ip, port and public_key * to setup connections */ -void DHT_bootstrap(DHT *dht, IP_Port ip_port, uint8_t *public_key); +void DHT_bootstrap(DHT *dht, IP_Port ip_port, const uint8_t *public_key); /* Resolves address into an IP address. If successful, sends a "get nodes" * request to the given node with ip, port and public_key to setup connections * @@ -330,7 +330,7 @@ void DHT_bootstrap(DHT *dht, IP_Port ip_port, uint8_t *public_key); * returns 0 otherwise */ int DHT_bootstrap_from_address(DHT *dht, const char *address, uint8_t ipv6enabled, - uint16_t port, uint8_t *public_key); + uint16_t port, const uint8_t *public_key); /* ROUTING FUNCTIONS */ @@ -339,13 +339,13 @@ int DHT_bootstrap_from_address(DHT *dht, const char *address, uint8_t ipv6enable * * return -1 if failure. */ -int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length); +int route_packet(const DHT *dht, const uint8_t *client_id, const uint8_t *packet, uint32_t length); /* Send the following packet to everyone who tells us they are connected to friend_id. * * return number of nodes it sent the packet to. */ -int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t length); +int route_tofriend(const DHT *dht, const uint8_t *friend_id, const uint8_t *packet, uint32_t length); /* Function to handle crypto packets. */ @@ -359,12 +359,12 @@ void cryptopacket_registerhandler(DHT *dht, uint8_t byte, cryptopacket_handler_c * returns number of ips returned. * returns -1 if no such friend. */ -int friend_ips(DHT *dht, IP_Port *ip_portlist, uint8_t *friend_id); +int friend_ips(const DHT *dht, IP_Port *ip_portlist, const uint8_t *friend_id); /* SAVE/LOAD functions */ /* Get the size of the DHT (for saving). */ -uint32_t DHT_size(DHT *dht); +uint32_t DHT_size(const DHT *dht); /* Save the DHT in data where data is an array of size DHT_size(). */ void DHT_save(DHT *dht, uint8_t *data); @@ -374,7 +374,7 @@ void DHT_save(DHT *dht, uint8_t *data); * return -1 if failure. * return 0 if success. */ -int DHT_load(DHT *dht, uint8_t *data, uint32_t length); +int DHT_load(DHT *dht, const uint8_t *data, uint32_t length); /* Initialize DHT. */ DHT *new_DHT(Networking_Core *net); @@ -384,9 +384,9 @@ void kill_DHT(DHT *dht); /* return 0 if we are not connected to the DHT. * return 1 if we are. */ -int DHT_isconnected(DHT *dht); +int DHT_isconnected(const DHT *dht); -int addto_lists(DHT *dht, IP_Port ip_port, uint8_t *client_id); +int addto_lists(DHT *dht, IP_Port ip_port, const uint8_t *client_id); #endif diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 037b0e66..931468ec 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -86,7 +86,7 @@ int realloc_friendlist(Messenger *m, uint32_t num) /* return the friend id associated to that public key. * return -1 if no such friend. */ -int32_t getfriend_id(Messenger *m, uint8_t *client_id) +int32_t getfriend_id(const Messenger *m, const uint8_t *client_id) { uint32_t i; @@ -256,7 +256,7 @@ int32_t m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t leng return FAERR_UNKNOWN; } -int32_t m_addfriend_norequest(Messenger *m, uint8_t *client_id) +int32_t m_addfriend_norequest(Messenger *m, const uint8_t *client_id) { if (getfriend_id(m, client_id) != -1) return -1; @@ -356,7 +356,7 @@ int m_friend_exists(Messenger *m, int32_t friendnumber) * return the message id if packet was successfully put into the send queue. * return 0 if it was not. */ -uint32_t m_sendmessage(Messenger *m, int32_t friendnumber, uint8_t *message, uint32_t length) +uint32_t m_sendmessage(Messenger *m, int32_t friendnumber, const uint8_t *message, uint32_t length) { if (friend_not_valid(m, friendnumber)) return 0; @@ -373,7 +373,7 @@ uint32_t m_sendmessage(Messenger *m, int32_t friendnumber, uint8_t *message, uin return 0; } -uint32_t m_sendmessage_withid(Messenger *m, int32_t friendnumber, uint32_t theid, uint8_t *message, uint32_t length) +uint32_t m_sendmessage_withid(Messenger *m, int32_t friendnumber, uint32_t theid, const uint8_t *message, uint32_t length) { if (length >= (MAX_CRYPTO_DATA_SIZE - sizeof(theid))) return 0; @@ -456,7 +456,7 @@ int setfriendname(Messenger *m, int32_t friendnumber, uint8_t *name, uint16_t le * return 0 if success. * return -1 if failure. */ -int setname(Messenger *m, uint8_t *name, uint16_t length) +int setname(Messenger *m, const uint8_t *name, uint16_t length) { if (length > MAX_NAME_LENGTH || length == 0) return -1; @@ -519,7 +519,7 @@ int m_get_self_name_size(Messenger *m) return m->name_length; } -int m_set_statusmessage(Messenger *m, uint8_t *status, uint16_t length) +int m_set_statusmessage(Messenger *m, const uint8_t *status, uint16_t length) { if (length > MAX_STATUSMESSAGE_LENGTH) return -1; @@ -722,10 +722,10 @@ void m_set_sends_receipts(Messenger *m, int32_t friendnumber, int yesno) /* static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); */ /* Set the function that will be executed when a friend request is received. */ -void m_callback_friendrequest(Messenger *m, void (*function)(Messenger *m, uint8_t *, uint8_t *, uint16_t, void *), +void m_callback_friendrequest(Messenger *m, void (*function)(Messenger *m, const uint8_t *, const uint8_t *, uint16_t, void *), void *userdata) { - void (*handle_friendrequest)(void *, uint8_t *, uint8_t *, uint16_t, void *) = (void *)function; + void (*handle_friendrequest)(void *, const uint8_t *, const uint8_t *, uint16_t, void *) = (void *)function; callback_friendrequest(&(m->fr), handle_friendrequest, m, userdata); } @@ -1701,7 +1701,7 @@ int send_custom_lossy_packet(Messenger *m, int32_t friendnumber, uint8_t *data, /* Function to filter out some friend requests*/ -static int friend_already_added(uint8_t *client_id, void *data) +static int friend_already_added(const uint8_t *client_id, void *data) { Messenger *m = data; @@ -2503,7 +2503,7 @@ static uint32_t friends_list_save(Messenger *m, uint8_t *data) return num * sizeof(struct SAVED_FRIEND); } -static int friends_list_load(Messenger *m, uint8_t *data, uint32_t length) +static int friends_list_load(Messenger *m, const uint8_t *data, uint32_t length) { if (length % sizeof(struct SAVED_FRIEND) != 0) { return -1; @@ -2626,7 +2626,7 @@ void messenger_save(Messenger *m, uint8_t *data) data += len; } -static int messenger_load_state_callback(void *outer, uint8_t *data, uint32_t length, uint16_t type) +static int messenger_load_state_callback(void *outer, const uint8_t *data, uint32_t length, uint16_t type) { Messenger *m = outer; diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index 51d5c73b..ad51a754 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -274,12 +274,12 @@ int32_t m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t leng * return the friend number if success. * return -1 if failure. */ -int32_t m_addfriend_norequest(Messenger *m, uint8_t *client_id); +int32_t m_addfriend_norequest(Messenger *m, const uint8_t *client_id); /* return the friend number associated to that client id. * return -1 if no such friend. */ -int32_t getfriend_id(Messenger *m, uint8_t *client_id); +int32_t getfriend_id(const Messenger *m, const uint8_t *client_id); /* Copies the public key associated to that friend id into client_id buffer. * Make sure that client_id is of size CLIENT_ID_SIZE. @@ -321,8 +321,8 @@ int m_friend_exists(Messenger *m, int32_t friendnumber); * m_sendmessage_withid will send a message with the id of your choosing, * however we can generate an id for you by calling plain m_sendmessage. */ -uint32_t m_sendmessage(Messenger *m, int32_t friendnumber, uint8_t *message, uint32_t length); -uint32_t m_sendmessage_withid(Messenger *m, int32_t friendnumber, uint32_t theid, uint8_t *message, uint32_t length); +uint32_t m_sendmessage(Messenger *m, int32_t friendnumber, const uint8_t *message, uint32_t length); +uint32_t m_sendmessage_withid(Messenger *m, int32_t friendnumber, uint32_t theid, const uint8_t *message, uint32_t length); /* Send an action to an online friend. * @@ -355,7 +355,7 @@ int setfriendname(Messenger *m, int32_t friendnumber, uint8_t *name, uint16_t le * return 0 if success. * return -1 if failure. */ -int setname(Messenger *m, uint8_t *name, uint16_t length); +int setname(Messenger *m, const uint8_t *name, uint16_t length); /* * Get your nickname. @@ -387,7 +387,7 @@ int m_get_self_name_size(Messenger *m); * returns 0 on success. * returns -1 on failure. */ -int m_set_statusmessage(Messenger *m, uint8_t *status, uint16_t length); +int m_set_statusmessage(Messenger *m, const uint8_t *status, uint16_t length); int m_set_userstatus(Messenger *m, uint8_t status); /* return the length of friendnumber's status message, including null on success. @@ -442,7 +442,7 @@ void m_set_sends_receipts(Messenger *m, int32_t friendnumber, int yesno); /* Set the function that will be executed when a friend request is received. * Function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ -void m_callback_friendrequest(Messenger *m, void (*function)(Messenger *m, uint8_t *, uint8_t *, uint16_t, void *), +void m_callback_friendrequest(Messenger *m, void (*function)(Messenger *m, const uint8_t *, const uint8_t *, uint16_t, void *), void *userdata); /* Set the function that will be executed when a message from a friend is received. diff --git a/toxcore/friend_requests.c b/toxcore/friend_requests.c index eb2a791c..4bf95a44 100644 --- a/toxcore/friend_requests.c +++ b/toxcore/friend_requests.c @@ -72,7 +72,7 @@ uint32_t get_nospam(Friend_Requests *fr) /* Set the function that will be executed when a friend request is received. */ -void callback_friendrequest(Friend_Requests *fr, void (*function)(void *, uint8_t *, uint8_t *, uint16_t, void *), +void callback_friendrequest(Friend_Requests *fr, void (*function)(void *, const uint8_t *, const uint8_t *, uint16_t, void *), void *object, void *userdata) { fr->handle_friendrequest = function; @@ -81,14 +81,14 @@ void callback_friendrequest(Friend_Requests *fr, void (*function)(void *, uint8_ fr->handle_friendrequest_userdata = userdata; } /* Set the function used to check if a friend request should be displayed to the user or not. */ -void set_filter_function(Friend_Requests *fr, int (*function)(uint8_t *, void *), void *userdata) +void set_filter_function(Friend_Requests *fr, int (*function)(const uint8_t *, void *), void *userdata) { fr->filter_function = function; fr->filter_function_userdata = userdata; } /* Add to list of received friend requests. */ -static void addto_receivedlist(Friend_Requests *fr, uint8_t *client_id) +static void addto_receivedlist(Friend_Requests *fr, const uint8_t *client_id) { if (fr->received_requests_index >= MAX_RECEIVED_STORED) fr->received_requests_index = 0; @@ -102,7 +102,7 @@ static void addto_receivedlist(Friend_Requests *fr, uint8_t *client_id) * return 0 if it did not. * return 1 if it did. */ -static int request_received(Friend_Requests *fr, uint8_t *client_id) +static int request_received(Friend_Requests *fr, const uint8_t *client_id) { uint32_t i; @@ -133,7 +133,7 @@ int remove_request_received(Friend_Requests *fr, uint8_t *client_id) } -static int friendreq_handlepacket(void *object, uint8_t *source_pubkey, uint8_t *packet, uint32_t length) +static int friendreq_handlepacket(void *object, const uint8_t *source_pubkey, const uint8_t *packet, uint32_t length) { Friend_Requests *fr = object; diff --git a/toxcore/friend_requests.h b/toxcore/friend_requests.h index 429ffbad..58ff91ce 100644 --- a/toxcore/friend_requests.h +++ b/toxcore/friend_requests.h @@ -30,12 +30,12 @@ typedef struct { uint32_t nospam; - void (*handle_friendrequest)(void *, uint8_t *, uint8_t *, uint16_t, void *); + void (*handle_friendrequest)(void *, const uint8_t *, const uint8_t *, uint16_t, void *); uint8_t handle_friendrequest_isset; void *handle_friendrequest_object; void *handle_friendrequest_userdata; - int (*filter_function)(uint8_t *, void *); + int (*filter_function)(const uint8_t *, void *); void *filter_function_userdata; /* NOTE: The following is just a temporary fix for the multiple friend requests received at the same time problem. * TODO: Make this better (This will most likely tie in with the way we will handle spam.) @@ -66,14 +66,14 @@ int remove_request_received(Friend_Requests *fr, uint8_t *client_id); /* Set the function that will be executed when a friend request for us is received. * Function format is function(uint8_t * public_key, uint8_t * data, uint16_t length, void * userdata) */ -void callback_friendrequest(Friend_Requests *fr, void (*function)(void *, uint8_t *, uint8_t *, uint16_t, void *), +void callback_friendrequest(Friend_Requests *fr, void (*function)(void *, const uint8_t *, const uint8_t *, uint16_t, void *), void *object, void *userdata); /* Set the function used to check if a friend request should be displayed to the user or not. * Function format is int function(uint8_t * public_key, void * userdata) * It must return 0 if the request is ok (anything else if it is bad.) */ -void set_filter_function(Friend_Requests *fr, int (*function)(uint8_t *, void *), void *userdata); +void set_filter_function(Friend_Requests *fr, int (*function)(const uint8_t *, void *), void *userdata); /* Sets up friendreq packet handlers. */ void friendreq_init(Friend_Requests *fr, Onion_Client *onion_c); diff --git a/toxcore/group_chats.c b/toxcore/group_chats.c index fbe76d16..85928436 100644 --- a/toxcore/group_chats.c +++ b/toxcore/group_chats.c @@ -675,7 +675,7 @@ static uint32_t group_send_nick(Group_Chat *chat, uint8_t *nick, uint16_t nick_l return send_data(chat, nick, nick_len, GROUP_CHAT_PEER_NICK); } -int set_nick(Group_Chat *chat, uint8_t *nick, uint16_t nick_len) +int set_nick(Group_Chat *chat, const uint8_t *nick, uint16_t nick_len) { if (nick_len > MAX_NICK_BYTES || nick_len == 0) return -1; diff --git a/toxcore/group_chats.h b/toxcore/group_chats.h index d9da54a5..49b83c81 100644 --- a/toxcore/group_chats.h +++ b/toxcore/group_chats.h @@ -144,7 +144,7 @@ uint32_t group_sendaction(Group_Chat *chat, uint8_t *action, uint32_t length); * * returns -1 on failure, 0 on success. */ -int set_nick(Group_Chat *chat, uint8_t *nick, uint16_t nick_len); +int set_nick(Group_Chat *chat, const uint8_t *nick, uint16_t nick_len); /* * Tell everyone about a new peer (a person we are inviting for example.) diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 47b23d54..bb38af26 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -2466,7 +2466,7 @@ void save_keys(Net_Crypto *c, uint8_t *keys) /* Load the public and private keys from the keys array. * Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES. */ -void load_keys(Net_Crypto *c, uint8_t *keys) +void load_keys(Net_Crypto *c, const uint8_t *keys) { memcpy(c->self_public_key, keys, crypto_box_PUBLICKEYBYTES); memcpy(c->self_secret_key, keys + crypto_box_PUBLICKEYBYTES, crypto_box_SECRETKEYBYTES); diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h index a3c7e7db..b1108ae1 100644 --- a/toxcore/net_crypto.h +++ b/toxcore/net_crypto.h @@ -347,7 +347,7 @@ void save_keys(Net_Crypto *c, uint8_t *keys); /* Load the public and private keys from the keys array. * Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES. */ -void load_keys(Net_Crypto *c, uint8_t *keys); +void load_keys(Net_Crypto *c, const uint8_t *keys); /* Create new instance of Net_Crypto. * Sets all the global connection variables to their default values. diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index dfdb1638..8e28eae5 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c @@ -467,7 +467,7 @@ static int handle_data_response(void *object, IP_Port source, uint8_t *packet, u #define FAKEID_DATA_ID 156 #define FAKEID_DATA_MIN_LENGTH (1 + sizeof(uint64_t) + crypto_box_PUBLICKEYBYTES) #define FAKEID_DATA_MAX_LENGTH (FAKEID_DATA_MIN_LENGTH + sizeof(Node_format)*MAX_SENT_NODES) -static int handle_fakeid_announce(void *object, uint8_t *source_pubkey, uint8_t *data, uint32_t length) +static int handle_fakeid_announce(void *object, const uint8_t *source_pubkey, const uint8_t *data, uint32_t length) { Onion_Client *onion_c = object; @@ -483,8 +483,8 @@ static int handle_fakeid_announce(void *object, uint8_t *source_pubkey, uint8_t return 1; uint64_t no_replay; - net_to_host(data + 1, sizeof(no_replay)); memcpy(&no_replay, data + 1, sizeof(uint64_t)); + net_to_host((uint8_t *) &no_replay, sizeof(no_replay)); if (no_replay <= onion_c->friends_list[friend_num].last_noreplay) return 1; @@ -623,7 +623,7 @@ static int send_dht_fakeid(Onion_Client *onion_c, int friend_num, uint8_t *data, return route_tofriend(onion_c->dht, onion_c->friends_list[friend_num].fake_client_id, packet, len); } -static int handle_dht_fakeid(void *object, IP_Port source, uint8_t *source_pubkey, uint8_t *packet, uint32_t length) +static int handle_dht_fakeid(void *object, IP_Port source, const uint8_t *source_pubkey, const uint8_t *packet, uint32_t length) { Onion_Client *onion_c = object; @@ -702,7 +702,7 @@ static int send_fakeid_announce(Onion_Client *onion_c, uint16_t friend_num, uint * return -1 on failure. * return friend number on success. */ -int onion_friend_num(Onion_Client *onion_c, uint8_t *client_id) +int onion_friend_num(const Onion_Client *onion_c, const uint8_t *client_id) { uint32_t i; @@ -744,7 +744,7 @@ static int realloc_onion_friends(Onion_Client *onion_c, uint32_t num) * return -1 on failure. * return the friend number on success or if the friend was already added. */ -int onion_addfriend(Onion_Client *onion_c, uint8_t *client_id) +int onion_addfriend(Onion_Client *onion_c, const uint8_t *client_id) { int num = onion_friend_num(onion_c, client_id); @@ -831,7 +831,7 @@ int recv_tcp_relay_handler(Onion_Client *onion_c, int friend_num, int (*tcp_rela * return -1 on failure. * return 0 on success. */ -int onion_set_friend_DHT_pubkey(Onion_Client *onion_c, int friend_num, uint8_t *dht_key, uint64_t timestamp) +int onion_set_friend_DHT_pubkey(Onion_Client *onion_c, int friend_num, const uint8_t *dht_key, uint64_t timestamp) { if ((uint32_t)friend_num >= onion_c->num_friends) return -1; diff --git a/toxcore/onion_client.h b/toxcore/onion_client.h index 9cf6cf3e..7995fa3a 100644 --- a/toxcore/onion_client.h +++ b/toxcore/onion_client.h @@ -102,7 +102,7 @@ typedef struct { uint32_t tcp_relay_node_callback_number; } Onion_Friend; -typedef int (*oniondata_handler_callback)(void *object, uint8_t *source_pubkey, uint8_t *data, uint32_t len); +typedef int (*oniondata_handler_callback)(void *object, const uint8_t *source_pubkey, const uint8_t *data, uint32_t len); typedef struct { DHT *dht; @@ -136,14 +136,14 @@ typedef struct { * return -1 on failure. * return the friend number on success or if the friend was already added. */ -int onion_friend_num(Onion_Client *onion_c, uint8_t *client_id); +int onion_friend_num(const Onion_Client *onion_c, const uint8_t *client_id); /* Add a friend who we want to connect to. * * return -1 on failure. * return the friend number on success. */ -int onion_addfriend(Onion_Client *onion_c, uint8_t *client_id); +int onion_addfriend(Onion_Client *onion_c, const uint8_t *client_id); /* Delete a friend. * @@ -190,7 +190,7 @@ int recv_tcp_relay_handler(Onion_Client *onion_c, int friend_num, int (*tcp_rela * return -1 on failure. * return 0 on success. */ -int onion_set_friend_DHT_pubkey(Onion_Client *onion_c, int friend_num, uint8_t *dht_key, uint64_t timestamp); +int onion_set_friend_DHT_pubkey(Onion_Client *onion_c, int friend_num, const uint8_t *dht_key, uint64_t timestamp); /* Copy friends DHT public key into dht_key. * diff --git a/toxcore/ping.c b/toxcore/ping.c index c01170ab..ea8fd04d 100644 --- a/toxcore/ping.c +++ b/toxcore/ping.c @@ -58,7 +58,7 @@ struct PING { #define DHT_PING_SIZE (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + PING_PLAIN_SIZE + crypto_box_MACBYTES) #define PING_DATA_SIZE (CLIENT_ID_SIZE + sizeof(IP_Port)) -int send_ping_request(PING *ping, IP_Port ipp, uint8_t *client_id) +int send_ping_request(PING *ping, IP_Port ipp, const uint8_t *client_id) { uint8_t pk[DHT_PING_SIZE]; int rc; @@ -100,7 +100,7 @@ int send_ping_request(PING *ping, IP_Port ipp, uint8_t *client_id) return sendpacket(ping->dht->net, ipp, pk, sizeof(pk)); } -static int send_ping_response(PING *ping, IP_Port ipp, uint8_t *client_id, uint64_t ping_id, +static int send_ping_response(PING *ping, IP_Port ipp, const uint8_t *client_id, uint64_t ping_id, uint8_t *shared_encryption_key) { uint8_t pk[DHT_PING_SIZE]; @@ -225,13 +225,13 @@ static int handle_ping_response(void *_dht, IP_Port source, uint8_t *packet, uin * return 1 if it is. * return 0 if it isn't. */ -static int in_list(Client_data *list, uint32_t length, uint8_t *client_id, IP_Port ip_port) +static int in_list(const Client_data *list, uint32_t length, const uint8_t *client_id, IP_Port ip_port) { uint32_t i; for (i = 0; i < length; ++i) { if (id_equal(list[i].client_id, client_id)) { - IPPTsPng *ipptp; + const IPPTsPng *ipptp; if (ip_port.ip.family == AF_INET) { ipptp = &list[i].assoc4; @@ -257,7 +257,7 @@ static int in_list(Client_data *list, uint32_t length, uint8_t *client_id, IP_Po * return 0 if node was added. * return -1 if node was not added. */ -int add_to_ping(PING *ping, uint8_t *client_id, IP_Port ip_port) +int add_to_ping(PING *ping, const uint8_t *client_id, IP_Port ip_port) { if (!ip_isset(&ip_port.ip)) return -1; diff --git a/toxcore/ping.h b/toxcore/ping.h index 168870d7..c19c912a 100644 --- a/toxcore/ping.h +++ b/toxcore/ping.h @@ -36,12 +36,12 @@ typedef struct PING PING; * return 0 if node was added. * return -1 if node was not added. */ -int add_to_ping(PING *ping, uint8_t *client_id, IP_Port ip_port); +int add_to_ping(PING *ping, const uint8_t *client_id, IP_Port ip_port); void do_to_ping(PING *ping); PING *new_ping(DHT *dht); void kill_ping(PING *ping); -int send_ping_request(PING *ping, IP_Port ipp, uint8_t *client_id); +int send_ping_request(PING *ping, IP_Port ipp, const uint8_t *client_id); #endif /* __PING_H__ */ diff --git a/toxcore/ping_array.c b/toxcore/ping_array.c index e6f684ef..5c92527e 100644 --- a/toxcore/ping_array.c +++ b/toxcore/ping_array.c @@ -59,7 +59,7 @@ static void ping_array_clear_timedout(Ping_Array *array) * return ping_id on success. * return 0 on failure. */ -uint64_t ping_array_add(Ping_Array *array, uint8_t *data, uint32_t length) +uint64_t ping_array_add(Ping_Array *array, const uint8_t *data, uint32_t length) { ping_array_clear_timedout(array); uint32_t index = array->last_added % array->total_size; diff --git a/toxcore/ping_array.h b/toxcore/ping_array.h index c5811b16..364ad833 100644 --- a/toxcore/ping_array.h +++ b/toxcore/ping_array.h @@ -48,7 +48,7 @@ typedef struct { * return ping_id on success. * return 0 on failure. */ -uint64_t ping_array_add(Ping_Array *array, uint8_t *data, uint32_t length); +uint64_t ping_array_add(Ping_Array *array, const uint8_t *data, uint32_t length); /* Check if ping_id is valid and not timed out. * diff --git a/toxcore/tox.c b/toxcore/tox.c index 12bee911..7cb7d692 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -138,7 +138,7 @@ int tox_friend_exists(Tox *tox, int32_t friendnumber) * m_sendmessage_withid will send a message with the id of your choosing, * however we can generate an id for you by calling plain m_sendmessage. */ -uint32_t tox_send_message(Tox *tox, int32_t friendnumber, uint8_t *message, uint32_t length) +uint32_t tox_send_message(Tox *tox, int32_t friendnumber, const uint8_t *message, uint32_t length) { Messenger *m = tox; return m_sendmessage(m, friendnumber, message, length); @@ -362,7 +362,7 @@ uint32_t tox_get_friendlist(Tox *tox, int32_t *out_list, uint32_t list_size) /* Set the function that will be executed when a friend request is received. * Function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ -void tox_callback_friend_request(Tox *tox, void (*function)(Tox *tox, uint8_t *, uint8_t *, uint16_t, void *), +void tox_callback_friend_request(Tox *tox, void (*function)(Tox *tox, const uint8_t *, const uint8_t *, uint16_t, void *), void *userdata) { Messenger *m = tox; diff --git a/toxcore/tox.h b/toxcore/tox.h index 5a63063f..c217e1a1 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -156,7 +156,7 @@ int tox_friend_exists(Tox *tox, int32_t friendnumber); * m_sendmessage_withid will send a message with the id of your choosing, * however we can generate an id for you by calling plain m_sendmessage. */ -uint32_t tox_send_message(Tox *tox, int32_t friendnumber, uint8_t *message, uint32_t length); +uint32_t tox_send_message(Tox *tox, int32_t friendnumber, const uint8_t *message, uint32_t length); uint32_t tox_send_message_withid(Tox *tox, int32_t friendnumber, uint32_t theid, uint8_t *message, uint32_t length); /* Send an action to an online friend. @@ -289,7 +289,7 @@ uint32_t tox_get_friendlist(Tox *tox, int32_t *out_list, uint32_t list_size); /* Set the function that will be executed when a friend request is received. * Function format is function(Tox *tox, uint8_t * public_key, uint8_t * data, uint16_t length, void *userdata) */ -void tox_callback_friend_request(Tox *tox, void (*function)(Tox *tox, uint8_t *, uint8_t *, uint16_t, void *), +void tox_callback_friend_request(Tox *tox, void (*function)(Tox *tox, const uint8_t *, const uint8_t *, uint16_t, void *), void *userdata); /* Set the function that will be executed when a message from a friend is received. diff --git a/toxcore/util.c b/toxcore/util.c index e3bfac4c..969ee704 100644 --- a/toxcore/util.c +++ b/toxcore/util.c @@ -86,7 +86,7 @@ void host_to_net(uint8_t *num, uint16_t numbytes) /* state load/save */ int load_state(load_state_callback_func load_state_callback, void *outer, - uint8_t *data, uint32_t length, uint16_t cookie_inner) + const uint8_t *data, uint32_t length, uint16_t cookie_inner) { if (!load_state_callback || !data) { #ifdef DEBUG diff --git a/toxcore/util.h b/toxcore/util.h index 74348c11..955ce8c4 100644 --- a/toxcore/util.h +++ b/toxcore/util.h @@ -43,9 +43,9 @@ void host_to_net(uint8_t *num, uint16_t numbytes); #define net_to_host(x, y) host_to_net(x, y) /* state load/save */ -typedef int (*load_state_callback_func)(void *outer, uint8_t *data, uint32_t len, uint16_t type); +typedef int (*load_state_callback_func)(void *outer, const uint8_t *data, uint32_t len, uint16_t type); int load_state(load_state_callback_func load_state_callback, void *outer, - uint8_t *data, uint32_t length, uint16_t cookie_inner); + const uint8_t *data, uint32_t length, uint16_t cookie_inner); /* Converts 4 bytes to uint32_t */ void bytes_to_U32(uint32_t *dest, const uint8_t *bytes);