diff options
author | iphydf <iphydf@users.noreply.github.com> | 2018-01-13 16:50:32 +0000 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2018-01-13 23:55:08 +0000 |
commit | e775b5533b7137b18edcd6e414fd461d07018425 (patch) | |
tree | cab52d97c0e02d5d2f17b8db1da3ab3e800fb3ce /toxcore | |
parent | 1e1efec34ad820fc65dcf98581a0d8f6fa1873a8 (diff) |
Make Ping_Array a module-private type.
Diffstat (limited to 'toxcore')
-rw-r--r-- | toxcore/DHT.c | 16 | ||||
-rw-r--r-- | toxcore/DHT.h | 4 | ||||
-rw-r--r-- | toxcore/onion_client.c | 10 | ||||
-rw-r--r-- | toxcore/onion_client.h | 2 | ||||
-rw-r--r-- | toxcore/ping.c | 12 | ||||
-rw-r--r-- | toxcore/ping_array.api.h | 72 | ||||
-rw-r--r-- | toxcore/ping_array.c | 105 | ||||
-rw-r--r-- | toxcore/ping_array.h | 55 |
8 files changed, 183 insertions, 93 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 1086ebc1..141ae533 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c | |||
@@ -1179,9 +1179,9 @@ static int getnodes(DHT *dht, IP_Port ip_port, const uint8_t *public_key, const | |||
1179 | 1179 | ||
1180 | if (sendback_node != NULL) { | 1180 | if (sendback_node != NULL) { |
1181 | memcpy(plain_message + sizeof(receiver), sendback_node, sizeof(Node_format)); | 1181 | memcpy(plain_message + sizeof(receiver), sendback_node, sizeof(Node_format)); |
1182 | ping_id = ping_array_add(&dht->dht_harden_ping_array, plain_message, sizeof(plain_message)); | 1182 | ping_id = ping_array_add(dht->dht_harden_ping_array, plain_message, sizeof(plain_message)); |
1183 | } else { | 1183 | } else { |
1184 | ping_id = ping_array_add(&dht->dht_ping_array, plain_message, sizeof(receiver)); | 1184 | ping_id = ping_array_add(dht->dht_ping_array, plain_message, sizeof(receiver)); |
1185 | } | 1185 | } |
1186 | 1186 | ||
1187 | if (ping_id == 0) { | 1187 | if (ping_id == 0) { |
@@ -1296,9 +1296,9 @@ static uint8_t sent_getnode_to_node(DHT *dht, const uint8_t *public_key, IP_Port | |||
1296 | { | 1296 | { |
1297 | uint8_t data[sizeof(Node_format) * 2]; | 1297 | uint8_t data[sizeof(Node_format) * 2]; |
1298 | 1298 | ||
1299 | if (ping_array_check(data, sizeof(data), &dht->dht_ping_array, ping_id) == sizeof(Node_format)) { | 1299 | if (ping_array_check(dht->dht_ping_array, data, sizeof(data), ping_id) == sizeof(Node_format)) { |
1300 | memset(sendback_node, 0, sizeof(Node_format)); | 1300 | memset(sendback_node, 0, sizeof(Node_format)); |
1301 | } else if (ping_array_check(data, sizeof(data), &dht->dht_harden_ping_array, ping_id) == sizeof(data)) { | 1301 | } else if (ping_array_check(dht->dht_harden_ping_array, data, sizeof(data), ping_id) == sizeof(data)) { |
1302 | memcpy(sendback_node, data + sizeof(Node_format), sizeof(Node_format)); | 1302 | memcpy(sendback_node, data + sizeof(Node_format), sizeof(Node_format)); |
1303 | } else { | 1303 | } else { |
1304 | return 0; | 1304 | return 0; |
@@ -2578,8 +2578,8 @@ DHT *new_DHT(Logger *log, Networking_Core *net, bool holepunching_enabled) | |||
2578 | new_symmetric_key(dht->secret_symmetric_key); | 2578 | new_symmetric_key(dht->secret_symmetric_key); |
2579 | crypto_new_keypair(dht->self_public_key, dht->self_secret_key); | 2579 | crypto_new_keypair(dht->self_public_key, dht->self_secret_key); |
2580 | 2580 | ||
2581 | ping_array_init(&dht->dht_ping_array, DHT_PING_ARRAY_SIZE, PING_TIMEOUT); | 2581 | dht->dht_ping_array = ping_array_new(DHT_PING_ARRAY_SIZE, PING_TIMEOUT); |
2582 | ping_array_init(&dht->dht_harden_ping_array, DHT_PING_ARRAY_SIZE, PING_TIMEOUT); | 2582 | dht->dht_harden_ping_array = ping_array_new(DHT_PING_ARRAY_SIZE, PING_TIMEOUT); |
2583 | 2583 | ||
2584 | for (uint32_t i = 0; i < DHT_FAKE_FRIEND_NUMBER; ++i) { | 2584 | for (uint32_t i = 0; i < DHT_FAKE_FRIEND_NUMBER; ++i) { |
2585 | uint8_t random_key_bytes[CRYPTO_PUBLIC_KEY_SIZE]; | 2585 | uint8_t random_key_bytes[CRYPTO_PUBLIC_KEY_SIZE]; |
@@ -2622,8 +2622,8 @@ void kill_DHT(DHT *dht) | |||
2622 | networking_registerhandler(dht->net, NET_PACKET_SEND_NODES_IPV6, NULL, NULL); | 2622 | networking_registerhandler(dht->net, NET_PACKET_SEND_NODES_IPV6, NULL, NULL); |
2623 | cryptopacket_registerhandler(dht, CRYPTO_PACKET_NAT_PING, NULL, NULL); | 2623 | cryptopacket_registerhandler(dht, CRYPTO_PACKET_NAT_PING, NULL, NULL); |
2624 | cryptopacket_registerhandler(dht, CRYPTO_PACKET_HARDENING, NULL, NULL); | 2624 | cryptopacket_registerhandler(dht, CRYPTO_PACKET_HARDENING, NULL, NULL); |
2625 | ping_array_free_all(&dht->dht_ping_array); | 2625 | ping_array_kill(dht->dht_ping_array); |
2626 | ping_array_free_all(&dht->dht_harden_ping_array); | 2626 | ping_array_kill(dht->dht_harden_ping_array); |
2627 | kill_ping(dht->ping); | 2627 | kill_ping(dht->ping); |
2628 | free(dht->friends_list); | 2628 | free(dht->friends_list); |
2629 | free(dht->loaded_nodes_list); | 2629 | free(dht->loaded_nodes_list); |
diff --git a/toxcore/DHT.h b/toxcore/DHT.h index 510b3c5c..dae6ea31 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h | |||
@@ -252,8 +252,8 @@ typedef struct { | |||
252 | Shared_Keys shared_keys_sent; | 252 | Shared_Keys shared_keys_sent; |
253 | 253 | ||
254 | struct PING *ping; | 254 | struct PING *ping; |
255 | Ping_Array dht_ping_array; | 255 | Ping_Array *dht_ping_array; |
256 | Ping_Array dht_harden_ping_array; | 256 | Ping_Array *dht_harden_ping_array; |
257 | uint64_t last_run; | 257 | uint64_t last_run; |
258 | 258 | ||
259 | Cryptopacket_Handles cryptopackethandlers[256]; | 259 | Cryptopacket_Handles cryptopackethandlers[256]; |
diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index 0d1191a6..1490d78d 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c | |||
@@ -397,7 +397,7 @@ static int new_sendback(Onion_Client *onion_c, uint32_t num, const uint8_t *publ | |||
397 | memcpy(data + sizeof(uint32_t), public_key, CRYPTO_PUBLIC_KEY_SIZE); | 397 | memcpy(data + sizeof(uint32_t), public_key, CRYPTO_PUBLIC_KEY_SIZE); |
398 | memcpy(data + sizeof(uint32_t) + CRYPTO_PUBLIC_KEY_SIZE, &ip_port, sizeof(IP_Port)); | 398 | memcpy(data + sizeof(uint32_t) + CRYPTO_PUBLIC_KEY_SIZE, &ip_port, sizeof(IP_Port)); |
399 | memcpy(data + sizeof(uint32_t) + CRYPTO_PUBLIC_KEY_SIZE + sizeof(IP_Port), &path_num, sizeof(uint32_t)); | 399 | memcpy(data + sizeof(uint32_t) + CRYPTO_PUBLIC_KEY_SIZE + sizeof(IP_Port), &path_num, sizeof(uint32_t)); |
400 | *sendback = ping_array_add(&onion_c->announce_ping_array, data, sizeof(data)); | 400 | *sendback = ping_array_add(onion_c->announce_ping_array, data, sizeof(data)); |
401 | 401 | ||
402 | if (*sendback == 0) { | 402 | if (*sendback == 0) { |
403 | return -1; | 403 | return -1; |
@@ -423,7 +423,7 @@ static uint32_t check_sendback(Onion_Client *onion_c, const uint8_t *sendback, u | |||
423 | memcpy(&sback, sendback, sizeof(uint64_t)); | 423 | memcpy(&sback, sendback, sizeof(uint64_t)); |
424 | uint8_t data[sizeof(uint32_t) + CRYPTO_PUBLIC_KEY_SIZE + sizeof(IP_Port) + sizeof(uint32_t)]; | 424 | uint8_t data[sizeof(uint32_t) + CRYPTO_PUBLIC_KEY_SIZE + sizeof(IP_Port) + sizeof(uint32_t)]; |
425 | 425 | ||
426 | if (ping_array_check(data, sizeof(data), &onion_c->announce_ping_array, sback) != sizeof(data)) { | 426 | if (ping_array_check(onion_c->announce_ping_array, data, sizeof(data), sback) != sizeof(data)) { |
427 | return ~0; | 427 | return ~0; |
428 | } | 428 | } |
429 | 429 | ||
@@ -1733,7 +1733,9 @@ Onion_Client *new_onion_client(Net_Crypto *c) | |||
1733 | return NULL; | 1733 | return NULL; |
1734 | } | 1734 | } |
1735 | 1735 | ||
1736 | if (ping_array_init(&onion_c->announce_ping_array, ANNOUNCE_ARRAY_SIZE, ANNOUNCE_TIMEOUT) != 0) { | 1736 | onion_c->announce_ping_array = ping_array_new(ANNOUNCE_ARRAY_SIZE, ANNOUNCE_TIMEOUT); |
1737 | |||
1738 | if (onion_c->announce_ping_array == NULL) { | ||
1737 | free(onion_c); | 1739 | free(onion_c); |
1738 | return NULL; | 1740 | return NULL; |
1739 | } | 1741 | } |
@@ -1758,7 +1760,7 @@ void kill_onion_client(Onion_Client *onion_c) | |||
1758 | return; | 1760 | return; |
1759 | } | 1761 | } |
1760 | 1762 | ||
1761 | ping_array_free_all(&onion_c->announce_ping_array); | 1763 | ping_array_kill(onion_c->announce_ping_array); |
1762 | realloc_onion_friends(onion_c, 0); | 1764 | realloc_onion_friends(onion_c, 0); |
1763 | networking_registerhandler(onion_c->net, NET_PACKET_ANNOUNCE_RESPONSE, NULL, NULL); | 1765 | networking_registerhandler(onion_c->net, NET_PACKET_ANNOUNCE_RESPONSE, NULL, NULL); |
1764 | networking_registerhandler(onion_c->net, NET_PACKET_ONION_DATA_RESPONSE, NULL, NULL); | 1766 | networking_registerhandler(onion_c->net, NET_PACKET_ONION_DATA_RESPONSE, NULL, NULL); |
diff --git a/toxcore/onion_client.h b/toxcore/onion_client.h index 216dbec0..9acb9bac 100644 --- a/toxcore/onion_client.h +++ b/toxcore/onion_client.h | |||
@@ -162,7 +162,7 @@ typedef struct { | |||
162 | Node_format path_nodes_bs[MAX_PATH_NODES]; | 162 | Node_format path_nodes_bs[MAX_PATH_NODES]; |
163 | uint16_t path_nodes_index_bs; | 163 | uint16_t path_nodes_index_bs; |
164 | 164 | ||
165 | Ping_Array announce_ping_array; | 165 | Ping_Array *announce_ping_array; |
166 | uint8_t last_pinged_index; | 166 | uint8_t last_pinged_index; |
167 | struct { | 167 | struct { |
168 | oniondata_handler_callback function; | 168 | oniondata_handler_callback function; |
diff --git a/toxcore/ping.c b/toxcore/ping.c index 72b3fe62..b99c2a8c 100644 --- a/toxcore/ping.c +++ b/toxcore/ping.c | |||
@@ -48,7 +48,7 @@ | |||
48 | struct PING { | 48 | struct PING { |
49 | DHT *dht; | 49 | DHT *dht; |
50 | 50 | ||
51 | Ping_Array ping_array; | 51 | Ping_Array *ping_array; |
52 | Node_format to_ping[MAX_TO_PING]; | 52 | Node_format to_ping[MAX_TO_PING]; |
53 | uint64_t last_to_ping; | 53 | uint64_t last_to_ping; |
54 | }; | 54 | }; |
@@ -76,7 +76,7 @@ int send_ping_request(PING *ping, IP_Port ipp, const uint8_t *public_key) | |||
76 | uint8_t data[PING_DATA_SIZE]; | 76 | uint8_t data[PING_DATA_SIZE]; |
77 | id_copy(data, public_key); | 77 | id_copy(data, public_key); |
78 | memcpy(data + CRYPTO_PUBLIC_KEY_SIZE, &ipp, sizeof(IP_Port)); | 78 | memcpy(data + CRYPTO_PUBLIC_KEY_SIZE, &ipp, sizeof(IP_Port)); |
79 | ping_id = ping_array_add(&ping->ping_array, data, sizeof(data)); | 79 | ping_id = ping_array_add(ping->ping_array, data, sizeof(data)); |
80 | 80 | ||
81 | if (ping_id == 0) { | 81 | if (ping_id == 0) { |
82 | return 1; | 82 | return 1; |
@@ -217,7 +217,7 @@ static int handle_ping_response(void *object, IP_Port source, const uint8_t *pac | |||
217 | memcpy(&ping_id, ping_plain + 1, sizeof(ping_id)); | 217 | memcpy(&ping_id, ping_plain + 1, sizeof(ping_id)); |
218 | uint8_t data[PING_DATA_SIZE]; | 218 | uint8_t data[PING_DATA_SIZE]; |
219 | 219 | ||
220 | if (ping_array_check(data, sizeof(data), &ping->ping_array, ping_id) != sizeof(data)) { | 220 | if (ping_array_check(ping->ping_array, data, sizeof(data), ping_id) != sizeof(data)) { |
221 | return 1; | 221 | return 1; |
222 | } | 222 | } |
223 | 223 | ||
@@ -359,7 +359,9 @@ PING *new_ping(DHT *dht) | |||
359 | return NULL; | 359 | return NULL; |
360 | } | 360 | } |
361 | 361 | ||
362 | if (ping_array_init(&ping->ping_array, PING_NUM_MAX, PING_TIMEOUT) != 0) { | 362 | ping->ping_array = ping_array_new(PING_NUM_MAX, PING_TIMEOUT); |
363 | |||
364 | if (ping->ping_array == NULL) { | ||
363 | free(ping); | 365 | free(ping); |
364 | return NULL; | 366 | return NULL; |
365 | } | 367 | } |
@@ -375,7 +377,7 @@ void kill_ping(PING *ping) | |||
375 | { | 377 | { |
376 | networking_registerhandler(ping->dht->net, NET_PACKET_PING_REQUEST, NULL, NULL); | 378 | networking_registerhandler(ping->dht->net, NET_PACKET_PING_REQUEST, NULL, NULL); |
377 | networking_registerhandler(ping->dht->net, NET_PACKET_PING_RESPONSE, NULL, NULL); | 379 | networking_registerhandler(ping->dht->net, NET_PACKET_PING_RESPONSE, NULL, NULL); |
378 | ping_array_free_all(&ping->ping_array); | 380 | ping_array_kill(ping->ping_array); |
379 | 381 | ||
380 | free(ping); | 382 | free(ping); |
381 | } | 383 | } |
diff --git a/toxcore/ping_array.api.h b/toxcore/ping_array.api.h new file mode 100644 index 00000000..55b80570 --- /dev/null +++ b/toxcore/ping_array.api.h | |||
@@ -0,0 +1,72 @@ | |||
1 | %{ | ||
2 | /* | ||
3 | * Implementation of an efficient array to store that we pinged something. | ||
4 | */ | ||
5 | |||
6 | /* | ||
7 | * Copyright © 2016-2017 The TokTok team. | ||
8 | * Copyright © 2013 Tox project. | ||
9 | * | ||
10 | * This file is part of Tox, the free peer to peer instant messenger. | ||
11 | * | ||
12 | * Tox is free software: you can redistribute it and/or modify | ||
13 | * it under the terms of the GNU General Public License as published by | ||
14 | * the Free Software Foundation, either version 3 of the License, or | ||
15 | * (at your option) any later version. | ||
16 | * | ||
17 | * Tox is distributed in the hope that it will be useful, | ||
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
20 | * GNU General Public License for more details. | ||
21 | * | ||
22 | * You should have received a copy of the GNU General Public License | ||
23 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
24 | */ | ||
25 | #ifndef PING_ARRAY_H | ||
26 | #define PING_ARRAY_H | ||
27 | |||
28 | #include "network.h" | ||
29 | %} | ||
30 | |||
31 | class ping_Array { | ||
32 | |||
33 | struct this; | ||
34 | |||
35 | /** | ||
36 | * Initialize a Ping_Array. | ||
37 | * size represents the total size of the array and should be a power of 2. | ||
38 | * timeout represents the maximum timeout in seconds for the entry. | ||
39 | * | ||
40 | * return 0 on success. | ||
41 | * return -1 on failure. | ||
42 | */ | ||
43 | static this new(uint32_t size, uint32_t timeout); | ||
44 | |||
45 | /** | ||
46 | * Free all the allocated memory in a Ping_Array. | ||
47 | */ | ||
48 | void kill(); | ||
49 | |||
50 | /** | ||
51 | * Add a data with length to the Ping_Array list and return a ping_id. | ||
52 | * | ||
53 | * return ping_id on success. | ||
54 | * return 0 on failure. | ||
55 | */ | ||
56 | uint64_t add(const uint8_t *data, uint32_t length); | ||
57 | |||
58 | /** | ||
59 | * Check if ping_id is valid and not timed out. | ||
60 | * | ||
61 | * On success, copies the data into data of length, | ||
62 | * | ||
63 | * return length of data copied on success. | ||
64 | * return -1 on failure. | ||
65 | */ | ||
66 | int32_t check(uint8_t[length] data, uint64_t ping_id); | ||
67 | |||
68 | } | ||
69 | |||
70 | %{ | ||
71 | #endif | ||
72 | %} | ||
diff --git a/toxcore/ping_array.c b/toxcore/ping_array.c index ea3e5101..eafedc77 100644 --- a/toxcore/ping_array.c +++ b/toxcore/ping_array.c | |||
@@ -30,6 +30,55 @@ | |||
30 | #include "crypto_core.h" | 30 | #include "crypto_core.h" |
31 | #include "util.h" | 31 | #include "util.h" |
32 | 32 | ||
33 | |||
34 | typedef struct { | ||
35 | void *data; | ||
36 | uint32_t length; | ||
37 | uint64_t time; | ||
38 | uint64_t ping_id; | ||
39 | } Ping_Array_Entry; | ||
40 | |||
41 | struct Ping_Array { | ||
42 | Ping_Array_Entry *entries; | ||
43 | |||
44 | uint32_t last_deleted; /* number representing the next entry to be deleted. */ | ||
45 | uint32_t last_added; /* number representing the last entry to be added. */ | ||
46 | uint32_t total_size; /* The length of entries */ | ||
47 | uint32_t timeout; /* The timeout after which entries are cleared. */ | ||
48 | }; | ||
49 | |||
50 | /* Initialize a Ping_Array. | ||
51 | * size represents the total size of the array and should be a power of 2. | ||
52 | * timeout represents the maximum timeout in seconds for the entry. | ||
53 | * | ||
54 | * return 0 on success. | ||
55 | * return -1 on failure. | ||
56 | */ | ||
57 | Ping_Array *ping_array_new(uint32_t size, uint32_t timeout) | ||
58 | { | ||
59 | if (size == 0 || timeout == 0) { | ||
60 | return NULL; | ||
61 | } | ||
62 | |||
63 | Ping_Array *empty_array = (Ping_Array *)calloc(1, sizeof(Ping_Array)); | ||
64 | |||
65 | if (empty_array == NULL) { | ||
66 | return NULL; | ||
67 | } | ||
68 | |||
69 | empty_array->entries = (Ping_Array_Entry *)calloc(size, sizeof(Ping_Array_Entry)); | ||
70 | |||
71 | if (empty_array->entries == NULL) { | ||
72 | free(empty_array); | ||
73 | return NULL; | ||
74 | } | ||
75 | |||
76 | empty_array->last_deleted = empty_array->last_added = 0; | ||
77 | empty_array->total_size = size; | ||
78 | empty_array->timeout = timeout; | ||
79 | return empty_array; | ||
80 | } | ||
81 | |||
33 | static void clear_entry(Ping_Array *array, uint32_t index) | 82 | static void clear_entry(Ping_Array *array, uint32_t index) |
34 | { | 83 | { |
35 | free(array->entries[index].data); | 84 | free(array->entries[index].data); |
@@ -39,6 +88,20 @@ static void clear_entry(Ping_Array *array, uint32_t index) | |||
39 | array->entries[index].ping_id = 0; | 88 | array->entries[index].ping_id = 0; |
40 | } | 89 | } |
41 | 90 | ||
91 | /* Free all the allocated memory in a Ping_Array. | ||
92 | */ | ||
93 | void ping_array_kill(Ping_Array *array) | ||
94 | { | ||
95 | while (array->last_deleted != array->last_added) { | ||
96 | uint32_t index = array->last_deleted % array->total_size; | ||
97 | clear_entry(array, index); | ||
98 | ++array->last_deleted; | ||
99 | } | ||
100 | |||
101 | free(array->entries); | ||
102 | free(array); | ||
103 | } | ||
104 | |||
42 | /* Clear timed out entries. | 105 | /* Clear timed out entries. |
43 | */ | 106 | */ |
44 | static void ping_array_clear_timedout(Ping_Array *array) | 107 | static void ping_array_clear_timedout(Ping_Array *array) |
@@ -101,7 +164,7 @@ uint64_t ping_array_add(Ping_Array *array, const uint8_t *data, uint32_t length) | |||
101 | * return length of data copied on success. | 164 | * return length of data copied on success. |
102 | * return -1 on failure. | 165 | * return -1 on failure. |
103 | */ | 166 | */ |
104 | int ping_array_check(uint8_t *data, uint32_t length, Ping_Array *array, uint64_t ping_id) | 167 | int32_t ping_array_check(Ping_Array *array, uint8_t *data, size_t length, uint64_t ping_id) |
105 | { | 168 | { |
106 | if (ping_id == 0) { | 169 | if (ping_id == 0) { |
107 | return -1; | 170 | return -1; |
@@ -130,43 +193,3 @@ int ping_array_check(uint8_t *data, uint32_t length, Ping_Array *array, uint64_t | |||
130 | clear_entry(array, index); | 193 | clear_entry(array, index); |
131 | return len; | 194 | return len; |
132 | } | 195 | } |
133 | |||
134 | /* Initialize a Ping_Array. | ||
135 | * size represents the total size of the array and should be a power of 2. | ||
136 | * timeout represents the maximum timeout in seconds for the entry. | ||
137 | * | ||
138 | * return 0 on success. | ||
139 | * return -1 on failure. | ||
140 | */ | ||
141 | int ping_array_init(Ping_Array *empty_array, uint32_t size, uint32_t timeout) | ||
142 | { | ||
143 | if (size == 0 || timeout == 0 || empty_array == NULL) { | ||
144 | return -1; | ||
145 | } | ||
146 | |||
147 | empty_array->entries = (Ping_Array_Entry *)calloc(size, sizeof(Ping_Array_Entry)); | ||
148 | |||
149 | if (empty_array->entries == NULL) { | ||
150 | return -1; | ||
151 | } | ||
152 | |||
153 | empty_array->last_deleted = empty_array->last_added = 0; | ||
154 | empty_array->total_size = size; | ||
155 | empty_array->timeout = timeout; | ||
156 | return 0; | ||
157 | } | ||
158 | |||
159 | /* Free all the allocated memory in a Ping_Array. | ||
160 | */ | ||
161 | void ping_array_free_all(Ping_Array *array) | ||
162 | { | ||
163 | while (array->last_deleted != array->last_added) { | ||
164 | uint32_t index = array->last_deleted % array->total_size; | ||
165 | clear_entry(array, index); | ||
166 | ++array->last_deleted; | ||
167 | } | ||
168 | |||
169 | free(array->entries); | ||
170 | array->entries = NULL; | ||
171 | } | ||
172 | |||
diff --git a/toxcore/ping_array.h b/toxcore/ping_array.h index bdf3c6db..b927bbd4 100644 --- a/toxcore/ping_array.h +++ b/toxcore/ping_array.h | |||
@@ -26,51 +26,42 @@ | |||
26 | 26 | ||
27 | #include "network.h" | 27 | #include "network.h" |
28 | 28 | ||
29 | typedef struct { | 29 | #ifndef PING_ARRAY_DEFINED |
30 | void *data; | 30 | #define PING_ARRAY_DEFINED |
31 | uint32_t length; | 31 | typedef struct Ping_Array Ping_Array; |
32 | uint64_t time; | 32 | #endif /* PING_ARRAY_DEFINED */ |
33 | uint64_t ping_id; | ||
34 | } Ping_Array_Entry; | ||
35 | 33 | ||
34 | /** | ||
35 | * Initialize a Ping_Array. | ||
36 | * size represents the total size of the array and should be a power of 2. | ||
37 | * timeout represents the maximum timeout in seconds for the entry. | ||
38 | * | ||
39 | * return 0 on success. | ||
40 | * return -1 on failure. | ||
41 | */ | ||
42 | struct Ping_Array *ping_array_new(uint32_t size, uint32_t timeout); | ||
36 | 43 | ||
37 | typedef struct { | 44 | /** |
38 | Ping_Array_Entry *entries; | 45 | * Free all the allocated memory in a Ping_Array. |
39 | 46 | */ | |
40 | uint32_t last_deleted; /* number representing the next entry to be deleted. */ | 47 | void ping_array_kill(struct Ping_Array *_array); |
41 | uint32_t last_added; /* number representing the last entry to be added. */ | ||
42 | uint32_t total_size; /* The length of entries */ | ||
43 | uint32_t timeout; /* The timeout after which entries are cleared. */ | ||
44 | } Ping_Array; | ||
45 | |||
46 | 48 | ||
47 | /* Add a data with length to the Ping_Array list and return a ping_id. | 49 | /** |
50 | * Add a data with length to the Ping_Array list and return a ping_id. | ||
48 | * | 51 | * |
49 | * return ping_id on success. | 52 | * return ping_id on success. |
50 | * return 0 on failure. | 53 | * return 0 on failure. |
51 | */ | 54 | */ |
52 | uint64_t ping_array_add(Ping_Array *array, const uint8_t *data, uint32_t length); | 55 | uint64_t ping_array_add(struct Ping_Array *_array, const uint8_t *data, uint32_t length); |
53 | 56 | ||
54 | /* Check if ping_id is valid and not timed out. | 57 | /** |
58 | * Check if ping_id is valid and not timed out. | ||
55 | * | 59 | * |
56 | * On success, copies the data into data of length, | 60 | * On success, copies the data into data of length, |
57 | * | 61 | * |
58 | * return length of data copied on success. | 62 | * return length of data copied on success. |
59 | * return -1 on failure. | 63 | * return -1 on failure. |
60 | */ | 64 | */ |
61 | int ping_array_check(uint8_t *data, uint32_t length, Ping_Array *array, uint64_t ping_id); | 65 | int32_t ping_array_check(struct Ping_Array *_array, uint8_t *data, size_t length, uint64_t ping_id); |
62 | |||
63 | /* Initialize a Ping_Array. | ||
64 | * size represents the total size of the array and should be a power of 2. | ||
65 | * timeout represents the maximum timeout in seconds for the entry. | ||
66 | * | ||
67 | * return 0 on success. | ||
68 | * return -1 on failure. | ||
69 | */ | ||
70 | int ping_array_init(Ping_Array *empty_array, uint32_t size, uint32_t timeout); | ||
71 | |||
72 | /* Free all the allocated memory in a Ping_Array. | ||
73 | */ | ||
74 | void ping_array_free_all(Ping_Array *array); | ||
75 | 66 | ||
76 | #endif | 67 | #endif |