diff options
author | iphydf <iphydf@users.noreply.github.com> | 2018-07-08 14:55:26 +0000 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2018-07-08 18:23:45 +0000 |
commit | ebdc43285a4fdf1f9d76f8839a9ba572a35cbb80 (patch) | |
tree | 780093381841fcccbed8138d5a8fe1cc99923c03 /toxcore/onion_client.c | |
parent | 819fe534ea62b07340b20f18841d1ed93cf5caff (diff) |
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.
Diffstat (limited to 'toxcore/onion_client.c')
-rw-r--r-- | toxcore/onion_client.c | 65 |
1 files changed, 34 insertions, 31 deletions
diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index 35e725b7..483d6409 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c | |||
@@ -39,7 +39,7 @@ | |||
39 | #define ANNOUNCE_ARRAY_SIZE 256 | 39 | #define ANNOUNCE_ARRAY_SIZE 256 |
40 | #define ANNOUNCE_TIMEOUT 10 | 40 | #define ANNOUNCE_TIMEOUT 10 |
41 | 41 | ||
42 | typedef struct { | 42 | typedef struct Onion_Node { |
43 | uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; | 43 | uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; |
44 | IP_Port ip_port; | 44 | IP_Port ip_port; |
45 | uint8_t ping_id[ONION_PING_ID_SIZE]; | 45 | uint8_t ping_id[ONION_PING_ID_SIZE]; |
@@ -57,7 +57,7 @@ typedef struct { | |||
57 | uint32_t path_used; | 57 | uint32_t path_used; |
58 | } Onion_Node; | 58 | } Onion_Node; |
59 | 59 | ||
60 | typedef struct { | 60 | typedef struct Onion_Client_Paths { |
61 | Onion_Path paths[NUMBER_ONION_PATHS]; | 61 | Onion_Path paths[NUMBER_ONION_PATHS]; |
62 | uint64_t last_path_success[NUMBER_ONION_PATHS]; | 62 | uint64_t last_path_success[NUMBER_ONION_PATHS]; |
63 | uint64_t last_path_used[NUMBER_ONION_PATHS]; | 63 | uint64_t last_path_used[NUMBER_ONION_PATHS]; |
@@ -66,12 +66,12 @@ typedef struct { | |||
66 | unsigned int last_path_used_times[NUMBER_ONION_PATHS]; | 66 | unsigned int last_path_used_times[NUMBER_ONION_PATHS]; |
67 | } Onion_Client_Paths; | 67 | } Onion_Client_Paths; |
68 | 68 | ||
69 | typedef struct { | 69 | typedef struct Last_Pinged { |
70 | uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; | 70 | uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; |
71 | uint64_t timestamp; | 71 | uint64_t timestamp; |
72 | } Last_Pinged; | 72 | } Last_Pinged; |
73 | 73 | ||
74 | typedef struct { | 74 | typedef struct Onion_Friend { |
75 | uint8_t status; /* 0 if friend is not valid, 1 if friend is valid.*/ | 75 | uint8_t status; /* 0 if friend is not valid, 1 if friend is valid.*/ |
76 | uint8_t is_online; /* Set by the onion_set_friend_status function. */ | 76 | uint8_t is_online; /* Set by the onion_set_friend_status function. */ |
77 | 77 | ||
@@ -95,17 +95,22 @@ typedef struct { | |||
95 | Last_Pinged last_pinged[MAX_STORED_PINGED_NODES]; | 95 | Last_Pinged last_pinged[MAX_STORED_PINGED_NODES]; |
96 | uint8_t last_pinged_index; | 96 | uint8_t last_pinged_index; |
97 | 97 | ||
98 | int (*tcp_relay_node_callback)(void *object, uint32_t number, IP_Port ip_port, const uint8_t *public_key); | 98 | recv_tcp_relay_cb *tcp_relay_node_callback; |
99 | void *tcp_relay_node_callback_object; | 99 | void *tcp_relay_node_callback_object; |
100 | uint32_t tcp_relay_node_callback_number; | 100 | uint32_t tcp_relay_node_callback_number; |
101 | 101 | ||
102 | void (*dht_pk_callback)(void *data, int32_t number, const uint8_t *dht_public_key, void *userdata); | 102 | onion_dht_pk_cb *dht_pk_callback; |
103 | void *dht_pk_callback_object; | 103 | void *dht_pk_callback_object; |
104 | uint32_t dht_pk_callback_number; | 104 | uint32_t dht_pk_callback_number; |
105 | 105 | ||
106 | uint32_t run_count; | 106 | uint32_t run_count; |
107 | } Onion_Friend; | 107 | } Onion_Friend; |
108 | 108 | ||
109 | typedef struct Onion_Data_Handler { | ||
110 | oniondata_handler_cb *function; | ||
111 | void *object; | ||
112 | } Onion_Data_Handler; | ||
113 | |||
109 | struct Onion_Client { | 114 | struct Onion_Client { |
110 | DHT *dht; | 115 | DHT *dht; |
111 | Net_Crypto *c; | 116 | Net_Crypto *c; |
@@ -120,7 +125,8 @@ struct Onion_Client { | |||
120 | Onion_Client_Paths onion_paths_friends; | 125 | Onion_Client_Paths onion_paths_friends; |
121 | 126 | ||
122 | uint8_t secret_symmetric_key[CRYPTO_SYMMETRIC_KEY_SIZE]; | 127 | uint8_t secret_symmetric_key[CRYPTO_SYMMETRIC_KEY_SIZE]; |
123 | uint64_t last_run, first_run; | 128 | uint64_t last_run; |
129 | uint64_t first_run; | ||
124 | 130 | ||
125 | uint8_t temp_public_key[CRYPTO_PUBLIC_KEY_SIZE]; | 131 | uint8_t temp_public_key[CRYPTO_PUBLIC_KEY_SIZE]; |
126 | uint8_t temp_secret_key[CRYPTO_SECRET_KEY_SIZE]; | 132 | uint8_t temp_secret_key[CRYPTO_SECRET_KEY_SIZE]; |
@@ -135,15 +141,12 @@ struct Onion_Client { | |||
135 | 141 | ||
136 | Ping_Array *announce_ping_array; | 142 | Ping_Array *announce_ping_array; |
137 | uint8_t last_pinged_index; | 143 | uint8_t last_pinged_index; |
138 | struct { | 144 | Onion_Data_Handler onion_data_handlers[256]; |
139 | oniondata_handler_callback function; | ||
140 | void *object; | ||
141 | } Onion_Data_Handlers[256]; | ||
142 | 145 | ||
143 | uint64_t last_packet_recv; | 146 | uint64_t last_packet_recv; |
144 | 147 | ||
145 | unsigned int onion_connected; | 148 | unsigned int onion_connected; |
146 | bool UDP_connected; | 149 | bool udp_connected; |
147 | }; | 150 | }; |
148 | 151 | ||
149 | DHT *onion_get_dht(const Onion_Client *onion_c) | 152 | 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 | |||
265 | 268 | ||
266 | unsigned int num_nodes = (onion_c->path_nodes_index < MAX_PATH_NODES) ? onion_c->path_nodes_index : MAX_PATH_NODES; | 269 | unsigned int num_nodes = (onion_c->path_nodes_index < MAX_PATH_NODES) ? onion_c->path_nodes_index : MAX_PATH_NODES; |
267 | 270 | ||
268 | //if (dht_non_lan_connected(onion_c->dht)) { | 271 | // if (dht_non_lan_connected(onion_c->dht)) { |
269 | if (dht_isconnected(onion_c->dht)) { | 272 | if (dht_isconnected(onion_c->dht)) { |
270 | if (num_nodes == 0) { | 273 | if (num_nodes == 0) { |
271 | return 0; | 274 | return 0; |
@@ -606,7 +609,7 @@ static int client_send_announce_request(Onion_Client *onion_c, uint32_t num, IP_ | |||
606 | return send_onion_packet_tcp_udp(onion_c, &path, dest, request, len); | 609 | return send_onion_packet_tcp_udp(onion_c, &path, dest, request, len); |
607 | } | 610 | } |
608 | 611 | ||
609 | typedef struct { | 612 | typedef struct Onion_Client_Cmp_data { |
610 | const uint8_t *base_public_key; | 613 | const uint8_t *base_public_key; |
611 | Onion_Node entry; | 614 | Onion_Node entry; |
612 | } Onion_Client_Cmp_data; | 615 | } Onion_Client_Cmp_data; |
@@ -654,14 +657,14 @@ static void sort_onion_node_list(Onion_Node *list, unsigned int length, const ui | |||
654 | // comparison function can use it as the base of comparison. | 657 | // comparison function can use it as the base of comparison. |
655 | VLA(Onion_Client_Cmp_data, cmp_list, length); | 658 | VLA(Onion_Client_Cmp_data, cmp_list, length); |
656 | 659 | ||
657 | for (uint32_t i = 0; i < length; i++) { | 660 | for (uint32_t i = 0; i < length; ++i) { |
658 | cmp_list[i].base_public_key = comp_public_key; | 661 | cmp_list[i].base_public_key = comp_public_key; |
659 | cmp_list[i].entry = list[i]; | 662 | cmp_list[i].entry = list[i]; |
660 | } | 663 | } |
661 | 664 | ||
662 | qsort(cmp_list, length, sizeof(Onion_Client_Cmp_data), onion_client_cmp_entry); | 665 | qsort(cmp_list, length, sizeof(Onion_Client_Cmp_data), onion_client_cmp_entry); |
663 | 666 | ||
664 | for (uint32_t i = 0; i < length; i++) { | 667 | for (uint32_t i = 0; i < length; ++i) { |
665 | list[i] = cmp_list[i].entry; | 668 | list[i] = cmp_list[i].entry; |
666 | } | 669 | } |
667 | } | 670 | } |
@@ -926,11 +929,11 @@ static int handle_data_response(void *object, IP_Port source, const uint8_t *pac | |||
926 | return 1; | 929 | return 1; |
927 | } | 930 | } |
928 | 931 | ||
929 | if (!onion_c->Onion_Data_Handlers[plain[0]].function) { | 932 | if (!onion_c->onion_data_handlers[plain[0]].function) { |
930 | return 1; | 933 | return 1; |
931 | } | 934 | } |
932 | 935 | ||
933 | return onion_c->Onion_Data_Handlers[plain[0]].function(onion_c->Onion_Data_Handlers[plain[0]].object, temp_plain, plain, | 936 | return onion_c->onion_data_handlers[plain[0]].function(onion_c->onion_data_handlers[plain[0]].object, temp_plain, plain, |
934 | SIZEOF_VLA(plain), userdata); | 937 | SIZEOF_VLA(plain), userdata); |
935 | } | 938 | } |
936 | 939 | ||
@@ -1025,7 +1028,7 @@ static int handle_tcp_onion(void *object, const uint8_t *data, uint16_t length, | |||
1025 | } | 1028 | } |
1026 | 1029 | ||
1027 | /* Send data of length length to friendnum. | 1030 | /* Send data of length length to friendnum. |
1028 | * This data will be received by the friend using the Onion_Data_Handlers callbacks. | 1031 | * This data will be received by the friend using the onion_data_handlers callbacks. |
1029 | * | 1032 | * |
1030 | * Even if this function succeeds, the friend might not receive any data. | 1033 | * Even if this function succeeds, the friend might not receive any data. |
1031 | * | 1034 | * |
@@ -1359,8 +1362,8 @@ int onion_delfriend(Onion_Client *onion_c, int friend_num) | |||
1359 | * return -1 on failure. | 1362 | * return -1 on failure. |
1360 | * return 0 on success. | 1363 | * return 0 on success. |
1361 | */ | 1364 | */ |
1362 | int recv_tcp_relay_handler(Onion_Client *onion_c, int friend_num, int (*callback)(void *object, | 1365 | int recv_tcp_relay_handler(Onion_Client *onion_c, int friend_num, |
1363 | uint32_t number, IP_Port ip_port, const uint8_t *public_key), void *object, uint32_t number) | 1366 | recv_tcp_relay_cb *callback, void *object, uint32_t number) |
1364 | { | 1367 | { |
1365 | if ((uint32_t)friend_num >= onion_c->num_friends) { | 1368 | if ((uint32_t)friend_num >= onion_c->num_friends) { |
1366 | return -1; | 1369 | return -1; |
@@ -1380,8 +1383,8 @@ int recv_tcp_relay_handler(Onion_Client *onion_c, int friend_num, int (*callback | |||
1380 | * return -1 on failure. | 1383 | * return -1 on failure. |
1381 | * return 0 on success. | 1384 | * return 0 on success. |
1382 | */ | 1385 | */ |
1383 | int onion_dht_pk_callback(Onion_Client *onion_c, int friend_num, void (*function)(void *data, int32_t number, | 1386 | int onion_dht_pk_callback(Onion_Client *onion_c, int friend_num, |
1384 | const uint8_t *dht_public_key, void *userdata), void *object, uint32_t number) | 1387 | onion_dht_pk_cb *function, void *object, uint32_t number) |
1385 | { | 1388 | { |
1386 | if ((uint32_t)friend_num >= onion_c->num_friends) { | 1389 | if ((uint32_t)friend_num >= onion_c->num_friends) { |
1387 | return -1; | 1390 | return -1; |
@@ -1512,7 +1515,7 @@ static void populate_path_nodes_tcp(Onion_Client *onion_c) | |||
1512 | { | 1515 | { |
1513 | Node_format nodes_list[MAX_SENT_NODES]; | 1516 | Node_format nodes_list[MAX_SENT_NODES]; |
1514 | 1517 | ||
1515 | unsigned int num_nodes = copy_connected_tcp_relays(onion_c->c, nodes_list, MAX_SENT_NODES);; | 1518 | unsigned int num_nodes = copy_connected_tcp_relays(onion_c->c, nodes_list, MAX_SENT_NODES); |
1516 | unsigned int i; | 1519 | unsigned int i; |
1517 | 1520 | ||
1518 | for (i = 0; i < num_nodes; ++i) { | 1521 | for (i = 0; i < num_nodes; ++i) { |
@@ -1646,10 +1649,10 @@ static void do_friend(Onion_Client *onion_c, uint16_t friendnum) | |||
1646 | 1649 | ||
1647 | 1650 | ||
1648 | /* Function to call when onion data packet with contents beginning with byte is received. */ | 1651 | /* Function to call when onion data packet with contents beginning with byte is received. */ |
1649 | void oniondata_registerhandler(Onion_Client *onion_c, uint8_t byte, oniondata_handler_callback cb, void *object) | 1652 | void oniondata_registerhandler(Onion_Client *onion_c, uint8_t byte, oniondata_handler_cb *cb, void *object) |
1650 | { | 1653 | { |
1651 | onion_c->Onion_Data_Handlers[byte].function = cb; | 1654 | onion_c->onion_data_handlers[byte].function = cb; |
1652 | onion_c->Onion_Data_Handlers[byte].object = object; | 1655 | onion_c->onion_data_handlers[byte].object = object; |
1653 | } | 1656 | } |
1654 | 1657 | ||
1655 | #define ANNOUNCE_INTERVAL_NOT_ANNOUNCED 3 | 1658 | #define ANNOUNCE_INTERVAL_NOT_ANNOUNCED 3 |
@@ -1796,7 +1799,7 @@ static int onion_isconnected(const Onion_Client *onion_c) | |||
1796 | unsigned int onion_connection_status(const Onion_Client *onion_c) | 1799 | unsigned int onion_connection_status(const Onion_Client *onion_c) |
1797 | { | 1800 | { |
1798 | if (onion_c->onion_connected >= ONION_CONNECTION_SECONDS) { | 1801 | if (onion_c->onion_connected >= ONION_CONNECTION_SECONDS) { |
1799 | if (onion_c->UDP_connected) { | 1802 | if (onion_c->udp_connected) { |
1800 | return 2; | 1803 | return 2; |
1801 | } | 1804 | } |
1802 | 1805 | ||
@@ -1829,13 +1832,13 @@ void do_onion_client(Onion_Client *onion_c) | |||
1829 | } | 1832 | } |
1830 | } | 1833 | } |
1831 | 1834 | ||
1832 | bool UDP_connected = dht_non_lan_connected(onion_c->dht); | 1835 | bool udp_connected = dht_non_lan_connected(onion_c->dht); |
1833 | 1836 | ||
1834 | if (is_timeout(onion_c->first_run, ONION_CONNECTION_SECONDS * 2)) { | 1837 | if (is_timeout(onion_c->first_run, ONION_CONNECTION_SECONDS * 2)) { |
1835 | set_tcp_onion_status(nc_get_tcp_c(onion_c->c), !UDP_connected); | 1838 | set_tcp_onion_status(nc_get_tcp_c(onion_c->c), !udp_connected); |
1836 | } | 1839 | } |
1837 | 1840 | ||
1838 | onion_c->UDP_connected = UDP_connected | 1841 | onion_c->udp_connected = udp_connected |
1839 | || get_random_tcp_onion_conn_number(nc_get_tcp_c(onion_c->c)) == -1; /* Check if connected to any TCP relays. */ | 1842 | || get_random_tcp_onion_conn_number(nc_get_tcp_c(onion_c->c)) == -1; /* Check if connected to any TCP relays. */ |
1840 | 1843 | ||
1841 | if (onion_connection_status(onion_c)) { | 1844 | if (onion_connection_status(onion_c)) { |