From bc52d9816fc43b900d5302ed9851854f1943275c Mon Sep 17 00:00:00 2001 From: iphydf Date: Sun, 8 Jul 2018 14:55:26 +0000 Subject: [PATCH] Use named types for onion callbacks. This is now a style rule: you can only use typedef'd function types. Previous rules now applied in `onion_*.c`: * `struct`s must have a name (typedef of unnamed struct is not allowed). * `++i` for increment-stmt, not `i++`, e.g. in loops. * Only a single declarator per struct member declaration. * Type_Names vs. variable_names. --- toxcore/onion.c | 2 +- toxcore/onion.h | 5 ++-- toxcore/onion_announce.c | 8 ++--- toxcore/onion_client.c | 65 +++++++++++++++++++++------------------- toxcore/onion_client.h | 17 ++++++----- 5 files changed, 52 insertions(+), 45 deletions(-) diff --git a/toxcore/onion.c b/toxcore/onion.c index 5bbc7f751c8..cfe12a0502e 100644 --- a/toxcore/onion.c +++ b/toxcore/onion.c @@ -655,7 +655,7 @@ static int handle_recv_1(void *object, IP_Port source, const uint8_t *packet, ui return 0; } -void set_callback_handle_recv_1(Onion *onion, int (*function)(void *, IP_Port, const uint8_t *, uint16_t), void *object) +void set_callback_handle_recv_1(Onion *onion, onion_recv_1_cb *function, void *object) { onion->recv_1_function = function; onion->callback_object = object; diff --git a/toxcore/onion.h b/toxcore/onion.h index e81b3a52aef..8762d1e16b1 100644 --- a/toxcore/onion.h +++ b/toxcore/onion.h @@ -150,12 +150,13 @@ int send_onion_response(Networking_Core *net, IP_Port dest, const uint8_t *data, */ int onion_send_1(const Onion *onion, const uint8_t *plain, uint16_t len, IP_Port source, const uint8_t *nonce); +typedef int onion_recv_1_cb(void *, IP_Port, const uint8_t *, uint16_t); + /* Set the callback to be called when the dest ip_port doesn't have TOX_AF_INET6 or TOX_AF_INET as the family. * * Format: function(void *object, IP_Port dest, uint8_t *data, uint16_t length) */ -void set_callback_handle_recv_1(Onion *onion, int (*function)(void *, IP_Port, const uint8_t *, uint16_t), - void *object); +void set_callback_handle_recv_1(Onion *onion, onion_recv_1_cb *function, void *object); Onion *new_onion(DHT *dht); diff --git a/toxcore/onion_announce.c b/toxcore/onion_announce.c index 6c9e8659aba..8f122fe66bf 100644 --- a/toxcore/onion_announce.c +++ b/toxcore/onion_announce.c @@ -40,7 +40,7 @@ #define DATA_REQUEST_MIN_SIZE ONION_DATA_REQUEST_MIN_SIZE #define DATA_REQUEST_MIN_SIZE_RECV (DATA_REQUEST_MIN_SIZE + ONION_RETURN_3) -typedef struct { +typedef struct Onion_Announce_Entry { uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; IP_Port ret_ip_port; uint8_t ret[ONION_RETURN_3]; @@ -264,7 +264,7 @@ static int in_entries(const Onion_Announce *onion_a, const uint8_t *public_key) return -1; } -typedef struct { +typedef struct Cmp_data { const uint8_t *base_public_key; Onion_Announce_Entry entry; } Cmp_data; @@ -312,14 +312,14 @@ static void sort_onion_announce_list(Onion_Announce_Entry *list, unsigned int le // comparison function can use it as the base of comparison. VLA(Cmp_data, cmp_list, length); - for (uint32_t i = 0; i < length; i++) { + for (uint32_t i = 0; i < length; ++i) { cmp_list[i].base_public_key = comp_public_key; cmp_list[i].entry = list[i]; } qsort(cmp_list, length, sizeof(Cmp_data), cmp_entry); - for (uint32_t i = 0; i < length; i++) { + for (uint32_t i = 0; i < length; ++i) { list[i] = cmp_list[i].entry; } } diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index 35e725b7c02..483d640913f 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c @@ -39,7 +39,7 @@ #define ANNOUNCE_ARRAY_SIZE 256 #define ANNOUNCE_TIMEOUT 10 -typedef struct { +typedef struct Onion_Node { uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; IP_Port ip_port; uint8_t ping_id[ONION_PING_ID_SIZE]; @@ -57,7 +57,7 @@ typedef struct { uint32_t path_used; } Onion_Node; -typedef struct { +typedef struct Onion_Client_Paths { Onion_Path paths[NUMBER_ONION_PATHS]; uint64_t last_path_success[NUMBER_ONION_PATHS]; uint64_t last_path_used[NUMBER_ONION_PATHS]; @@ -66,12 +66,12 @@ typedef struct { unsigned int last_path_used_times[NUMBER_ONION_PATHS]; } Onion_Client_Paths; -typedef struct { +typedef struct Last_Pinged { uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; uint64_t timestamp; } Last_Pinged; -typedef struct { +typedef struct Onion_Friend { uint8_t status; /* 0 if friend is not valid, 1 if friend is valid.*/ uint8_t is_online; /* Set by the onion_set_friend_status function. */ @@ -95,17 +95,22 @@ typedef struct { Last_Pinged last_pinged[MAX_STORED_PINGED_NODES]; uint8_t last_pinged_index; - int (*tcp_relay_node_callback)(void *object, uint32_t number, IP_Port ip_port, const uint8_t *public_key); + recv_tcp_relay_cb *tcp_relay_node_callback; void *tcp_relay_node_callback_object; uint32_t tcp_relay_node_callback_number; - void (*dht_pk_callback)(void *data, int32_t number, const uint8_t *dht_public_key, void *userdata); + onion_dht_pk_cb *dht_pk_callback; void *dht_pk_callback_object; uint32_t dht_pk_callback_number; uint32_t run_count; } Onion_Friend; +typedef struct Onion_Data_Handler { + oniondata_handler_cb *function; + void *object; +} Onion_Data_Handler; + struct Onion_Client { DHT *dht; Net_Crypto *c; @@ -120,7 +125,8 @@ struct Onion_Client { Onion_Client_Paths onion_paths_friends; uint8_t secret_symmetric_key[CRYPTO_SYMMETRIC_KEY_SIZE]; - uint64_t last_run, first_run; + uint64_t last_run; + uint64_t first_run; uint8_t temp_public_key[CRYPTO_PUBLIC_KEY_SIZE]; uint8_t temp_secret_key[CRYPTO_SECRET_KEY_SIZE]; @@ -135,15 +141,12 @@ struct Onion_Client { Ping_Array *announce_ping_array; uint8_t last_pinged_index; - struct { - oniondata_handler_callback function; - void *object; - } Onion_Data_Handlers[256]; + Onion_Data_Handler onion_data_handlers[256]; uint64_t last_packet_recv; unsigned int onion_connected; - bool UDP_connected; + bool udp_connected; }; DHT *onion_get_dht(const Onion_Client *onion_c) @@ -265,7 +268,7 @@ static uint16_t random_nodes_path_onion(const Onion_Client *onion_c, Node_format unsigned int num_nodes = (onion_c->path_nodes_index < MAX_PATH_NODES) ? onion_c->path_nodes_index : MAX_PATH_NODES; - //if (dht_non_lan_connected(onion_c->dht)) { + // if (dht_non_lan_connected(onion_c->dht)) { if (dht_isconnected(onion_c->dht)) { if (num_nodes == 0) { return 0; @@ -606,7 +609,7 @@ static int client_send_announce_request(Onion_Client *onion_c, uint32_t num, IP_ return send_onion_packet_tcp_udp(onion_c, &path, dest, request, len); } -typedef struct { +typedef struct Onion_Client_Cmp_data { const uint8_t *base_public_key; Onion_Node entry; } Onion_Client_Cmp_data; @@ -654,14 +657,14 @@ static void sort_onion_node_list(Onion_Node *list, unsigned int length, const ui // comparison function can use it as the base of comparison. VLA(Onion_Client_Cmp_data, cmp_list, length); - for (uint32_t i = 0; i < length; i++) { + for (uint32_t i = 0; i < length; ++i) { cmp_list[i].base_public_key = comp_public_key; cmp_list[i].entry = list[i]; } qsort(cmp_list, length, sizeof(Onion_Client_Cmp_data), onion_client_cmp_entry); - for (uint32_t i = 0; i < length; i++) { + for (uint32_t i = 0; i < length; ++i) { list[i] = cmp_list[i].entry; } } @@ -926,11 +929,11 @@ static int handle_data_response(void *object, IP_Port source, const uint8_t *pac return 1; } - if (!onion_c->Onion_Data_Handlers[plain[0]].function) { + if (!onion_c->onion_data_handlers[plain[0]].function) { return 1; } - return onion_c->Onion_Data_Handlers[plain[0]].function(onion_c->Onion_Data_Handlers[plain[0]].object, temp_plain, plain, + return onion_c->onion_data_handlers[plain[0]].function(onion_c->onion_data_handlers[plain[0]].object, temp_plain, plain, SIZEOF_VLA(plain), userdata); } @@ -1025,7 +1028,7 @@ static int handle_tcp_onion(void *object, const uint8_t *data, uint16_t length, } /* Send data of length length to friendnum. - * This data will be received by the friend using the Onion_Data_Handlers callbacks. + * This data will be received by the friend using the onion_data_handlers callbacks. * * Even if this function succeeds, the friend might not receive any data. * @@ -1359,8 +1362,8 @@ int onion_delfriend(Onion_Client *onion_c, int friend_num) * return -1 on failure. * return 0 on success. */ -int recv_tcp_relay_handler(Onion_Client *onion_c, int friend_num, int (*callback)(void *object, - uint32_t number, IP_Port ip_port, const uint8_t *public_key), void *object, uint32_t number) +int recv_tcp_relay_handler(Onion_Client *onion_c, int friend_num, + recv_tcp_relay_cb *callback, void *object, uint32_t number) { if ((uint32_t)friend_num >= onion_c->num_friends) { return -1; @@ -1380,8 +1383,8 @@ int recv_tcp_relay_handler(Onion_Client *onion_c, int friend_num, int (*callback * return -1 on failure. * return 0 on success. */ -int onion_dht_pk_callback(Onion_Client *onion_c, int friend_num, void (*function)(void *data, int32_t number, - const uint8_t *dht_public_key, void *userdata), void *object, uint32_t number) +int onion_dht_pk_callback(Onion_Client *onion_c, int friend_num, + onion_dht_pk_cb *function, void *object, uint32_t number) { if ((uint32_t)friend_num >= onion_c->num_friends) { return -1; @@ -1512,7 +1515,7 @@ static void populate_path_nodes_tcp(Onion_Client *onion_c) { Node_format nodes_list[MAX_SENT_NODES]; - unsigned int num_nodes = copy_connected_tcp_relays(onion_c->c, nodes_list, MAX_SENT_NODES);; + unsigned int num_nodes = copy_connected_tcp_relays(onion_c->c, nodes_list, MAX_SENT_NODES); unsigned int i; for (i = 0; i < num_nodes; ++i) { @@ -1646,10 +1649,10 @@ static void do_friend(Onion_Client *onion_c, uint16_t friendnum) /* Function to call when onion data packet with contents beginning with byte is received. */ -void oniondata_registerhandler(Onion_Client *onion_c, uint8_t byte, oniondata_handler_callback cb, void *object) +void oniondata_registerhandler(Onion_Client *onion_c, uint8_t byte, oniondata_handler_cb *cb, void *object) { - onion_c->Onion_Data_Handlers[byte].function = cb; - onion_c->Onion_Data_Handlers[byte].object = object; + onion_c->onion_data_handlers[byte].function = cb; + onion_c->onion_data_handlers[byte].object = object; } #define ANNOUNCE_INTERVAL_NOT_ANNOUNCED 3 @@ -1796,7 +1799,7 @@ static int onion_isconnected(const Onion_Client *onion_c) unsigned int onion_connection_status(const Onion_Client *onion_c) { if (onion_c->onion_connected >= ONION_CONNECTION_SECONDS) { - if (onion_c->UDP_connected) { + if (onion_c->udp_connected) { return 2; } @@ -1829,13 +1832,13 @@ void do_onion_client(Onion_Client *onion_c) } } - bool UDP_connected = dht_non_lan_connected(onion_c->dht); + bool udp_connected = dht_non_lan_connected(onion_c->dht); if (is_timeout(onion_c->first_run, ONION_CONNECTION_SECONDS * 2)) { - set_tcp_onion_status(nc_get_tcp_c(onion_c->c), !UDP_connected); + set_tcp_onion_status(nc_get_tcp_c(onion_c->c), !udp_connected); } - onion_c->UDP_connected = UDP_connected + onion_c->udp_connected = udp_connected || get_random_tcp_onion_conn_number(nc_get_tcp_c(onion_c->c)) == -1; /* Check if connected to any TCP relays. */ if (onion_connection_status(onion_c)) { diff --git a/toxcore/onion_client.h b/toxcore/onion_client.h index 327d492326c..560c2851717 100644 --- a/toxcore/onion_client.h +++ b/toxcore/onion_client.h @@ -124,6 +124,8 @@ int onion_set_friend_online(Onion_Client *onion_c, int friend_num, uint8_t is_on */ int onion_getfriendip(const Onion_Client *onion_c, int friend_num, IP_Port *ip_port); +typedef int recv_tcp_relay_cb(void *object, uint32_t number, IP_Port ip_port, const uint8_t *public_key); + /* Set the function for this friend that will be callbacked with object and number * when that friends gives us one of the TCP relays he is connected to. * @@ -132,9 +134,10 @@ int onion_getfriendip(const Onion_Client *onion_c, int friend_num, IP_Port *ip_p * return -1 on failure. * return 0 on success. */ -int recv_tcp_relay_handler(Onion_Client *onion_c, int friend_num, int (*tcp_relay_node_callback)(void *object, - uint32_t number, IP_Port ip_port, const uint8_t *public_key), void *object, uint32_t number); +int recv_tcp_relay_handler(Onion_Client *onion_c, int friend_num, + recv_tcp_relay_cb *tcp_relay_node_callback, void *object, uint32_t number); +typedef void onion_dht_pk_cb(void *data, int32_t number, const uint8_t *dht_public_key, void *userdata); /* Set the function for this friend that will be callbacked with object and number * when that friend gives us his DHT temporary public key. @@ -144,8 +147,8 @@ 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_dht_pk_callback(Onion_Client *onion_c, int friend_num, void (*function)(void *data, int32_t number, - const uint8_t *dht_public_key, void *userdata), void *object, uint32_t number); +int onion_dht_pk_callback(Onion_Client *onion_c, int friend_num, onion_dht_pk_cb *function, void *object, + uint32_t number); /* Set a friends DHT public key. * timestamp is the time (current_time_monotonic()) at which the key was last confirmed belonging to @@ -177,11 +180,11 @@ unsigned int onion_getfriend_DHT_pubkey(const Onion_Client *onion_c, int friend_ */ int send_onion_data(Onion_Client *onion_c, int friend_num, const uint8_t *data, uint16_t length); -typedef int (*oniondata_handler_callback)(void *object, const uint8_t *source_pubkey, const uint8_t *data, - uint16_t len, void *userdata); +typedef int oniondata_handler_cb(void *object, const uint8_t *source_pubkey, const uint8_t *data, + uint16_t len, void *userdata); /* Function to call when onion data packet with contents beginning with byte is received. */ -void oniondata_registerhandler(Onion_Client *onion_c, uint8_t byte, oniondata_handler_callback cb, void *object); +void oniondata_registerhandler(Onion_Client *onion_c, uint8_t byte, oniondata_handler_cb *cb, void *object); void do_onion_client(Onion_Client *onion_c);