diff options
author | iphydf <iphydf@users.noreply.github.com> | 2018-07-09 15:17:00 +0000 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2018-07-12 09:32:46 +0000 |
commit | cbda01021c561bd061cb03a1c1bab58199ac2307 (patch) | |
tree | 537e8397e84d3ccd4e16753101ded1bf676392b2 /toxcore/DHT.h | |
parent | 37f8f566d53a3e2603467aa0bbe1e29581ddd561 (diff) |
Fix style in DHT.c.
* Removed `ARRAY_SIZE` and use NULL markers for end of array, instead.
The alternative is + size, but for these arrays, NULL markers made
sense, since they are arrays of non-null pointers.
* Made `INDEX_OF_PK` a self-contained macro, not dependent upon the
naming inside its call site. This is a minor change but makes the code
more local and reviews easier.
* No nested structs.
* Use only named function types ending in `_cb` for callbacks.
* Replaced two macros with functions.
* `++i` instead of `i++`.
* struct member names start with lowercase letters.
* It takes a bit of work to support `/**/` comments in preprocessor
macros, so I've decided not to support these. If a macro is complex
enough to need comments inside it, it's too complex. `//` comments are
allowed at the end of macro definitions.
* Callback typedefs must name their parameters.
Diffstat (limited to 'toxcore/DHT.h')
-rw-r--r-- | toxcore/DHT.h | 46 |
1 files changed, 21 insertions, 25 deletions
diff --git a/toxcore/DHT.h b/toxcore/DHT.h index 15ff44e5..0852827c 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h | |||
@@ -64,10 +64,10 @@ | |||
64 | 64 | ||
65 | #define MAX_CRYPTO_REQUEST_SIZE 1024 | 65 | #define MAX_CRYPTO_REQUEST_SIZE 1024 |
66 | 66 | ||
67 | #define CRYPTO_PACKET_FRIEND_REQ 32 /* Friend request crypto packet ID. */ | 67 | #define CRYPTO_PACKET_FRIEND_REQ 32 // Friend request crypto packet ID. |
68 | #define CRYPTO_PACKET_HARDENING 48 /* Hardening crypto packet ID. */ | 68 | #define CRYPTO_PACKET_HARDENING 48 // Hardening crypto packet ID. |
69 | #define CRYPTO_PACKET_DHTPK 156 | 69 | #define CRYPTO_PACKET_DHTPK 156 |
70 | #define CRYPTO_PACKET_NAT_PING 254 /* NAT ping crypto packet ID. */ | 70 | #define CRYPTO_PACKET_NAT_PING 254 // NAT ping crypto packet ID. |
71 | 71 | ||
72 | /* Create a request to peer. | 72 | /* Create a request to peer. |
73 | * send_public_key and send_secret_key are the pub/secret keys of the sender. | 73 | * send_public_key and send_secret_key are the pub/secret keys of the sender. |
@@ -89,12 +89,12 @@ int create_request(const uint8_t *send_public_key, const uint8_t *send_secret_ke | |||
89 | int handle_request(const uint8_t *self_public_key, const uint8_t *self_secret_key, uint8_t *public_key, uint8_t *data, | 89 | int handle_request(const uint8_t *self_public_key, const uint8_t *self_secret_key, uint8_t *public_key, uint8_t *data, |
90 | uint8_t *request_id, const uint8_t *packet, uint16_t length); | 90 | uint8_t *request_id, const uint8_t *packet, uint16_t length); |
91 | 91 | ||
92 | typedef struct { | 92 | typedef struct IPPTs { |
93 | IP_Port ip_port; | 93 | IP_Port ip_port; |
94 | uint64_t timestamp; | 94 | uint64_t timestamp; |
95 | } IPPTs; | 95 | } IPPTs; |
96 | 96 | ||
97 | typedef struct { | 97 | typedef struct Hardening { |
98 | /* Node routes request correctly (true (1) or false/didn't check (0)) */ | 98 | /* Node routes request correctly (true (1) or false/didn't check (0)) */ |
99 | uint8_t routes_requests_ok; | 99 | uint8_t routes_requests_ok; |
100 | /* Time which we last checked this.*/ | 100 | /* Time which we last checked this.*/ |
@@ -112,7 +112,7 @@ typedef struct { | |||
112 | uint8_t testing_pingedid[CRYPTO_PUBLIC_KEY_SIZE]; | 112 | uint8_t testing_pingedid[CRYPTO_PUBLIC_KEY_SIZE]; |
113 | } Hardening; | 113 | } Hardening; |
114 | 114 | ||
115 | typedef struct { | 115 | typedef struct IPPTsPng { |
116 | IP_Port ip_port; | 116 | IP_Port ip_port; |
117 | uint64_t timestamp; | 117 | uint64_t timestamp; |
118 | uint64_t last_pinged; | 118 | uint64_t last_pinged; |
@@ -123,7 +123,7 @@ typedef struct { | |||
123 | uint64_t ret_timestamp; | 123 | uint64_t ret_timestamp; |
124 | } IPPTsPng; | 124 | } IPPTsPng; |
125 | 125 | ||
126 | typedef struct { | 126 | typedef struct Client_data { |
127 | uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; | 127 | uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; |
128 | IPPTsPng assoc4; | 128 | IPPTsPng assoc4; |
129 | IPPTsPng assoc6; | 129 | IPPTsPng assoc6; |
@@ -131,7 +131,7 @@ typedef struct { | |||
131 | 131 | ||
132 | /*----------------------------------------------------------------------------------*/ | 132 | /*----------------------------------------------------------------------------------*/ |
133 | 133 | ||
134 | typedef struct { | 134 | typedef struct NAT { |
135 | /* 1 if currently hole punching, otherwise 0 */ | 135 | /* 1 if currently hole punching, otherwise 0 */ |
136 | uint8_t hole_punching; | 136 | uint8_t hole_punching; |
137 | uint32_t punching_index; | 137 | uint32_t punching_index; |
@@ -139,18 +139,17 @@ typedef struct { | |||
139 | uint32_t punching_index2; | 139 | uint32_t punching_index2; |
140 | 140 | ||
141 | uint64_t punching_timestamp; | 141 | uint64_t punching_timestamp; |
142 | uint64_t recvNATping_timestamp; | 142 | uint64_t recv_nat_ping_timestamp; |
143 | uint64_t NATping_id; | 143 | uint64_t nat_ping_id; |
144 | uint64_t NATping_timestamp; | 144 | uint64_t nat_ping_timestamp; |
145 | } NAT; | 145 | } NAT; |
146 | 146 | ||
147 | #define DHT_FRIEND_MAX_LOCKS 32 | 147 | #define DHT_FRIEND_MAX_LOCKS 32 |
148 | 148 | ||
149 | typedef struct { | 149 | typedef struct Node_format { |
150 | uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; | 150 | uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; |
151 | IP_Port ip_port; | 151 | IP_Port ip_port; |
152 | } | 152 | } Node_format; |
153 | Node_format; | ||
154 | 153 | ||
155 | typedef struct DHT_Friend DHT_Friend; | 154 | typedef struct DHT_Friend DHT_Friend; |
156 | 155 | ||
@@ -199,7 +198,7 @@ int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint16_t *processed | |||
199 | #define MAX_KEYS_PER_SLOT 4 | 198 | #define MAX_KEYS_PER_SLOT 4 |
200 | #define KEYS_TIMEOUT 600 | 199 | #define KEYS_TIMEOUT 600 |
201 | 200 | ||
202 | typedef struct { | 201 | typedef struct Shared_Key { |
203 | uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; | 202 | uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; |
204 | uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE]; | 203 | uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE]; |
205 | uint32_t times_requested; | 204 | uint32_t times_requested; |
@@ -207,19 +206,14 @@ typedef struct { | |||
207 | uint64_t time_last_requested; | 206 | uint64_t time_last_requested; |
208 | } Shared_Key; | 207 | } Shared_Key; |
209 | 208 | ||
210 | typedef struct { | 209 | typedef struct Shared_Keys { |
211 | Shared_Key keys[256 * MAX_KEYS_PER_SLOT]; | 210 | Shared_Key keys[256 * MAX_KEYS_PER_SLOT]; |
212 | } Shared_Keys; | 211 | } Shared_Keys; |
213 | 212 | ||
214 | /*----------------------------------------------------------------------------------*/ | 213 | /*----------------------------------------------------------------------------------*/ |
215 | 214 | ||
216 | typedef int (*cryptopacket_handler_callback)(void *object, IP_Port ip_port, const uint8_t *source_pubkey, | 215 | typedef int cryptopacket_handler_cb(void *object, IP_Port ip_port, const uint8_t *source_pubkey, |
217 | const uint8_t *data, uint16_t len, void *userdata); | 216 | const uint8_t *data, uint16_t len, void *userdata); |
218 | |||
219 | typedef struct { | ||
220 | cryptopacket_handler_callback function; | ||
221 | void *object; | ||
222 | } Cryptopacket_Handles; | ||
223 | 217 | ||
224 | #define DHT_DEFINED | 218 | #define DHT_DEFINED |
225 | typedef struct DHT DHT; | 219 | typedef struct DHT DHT; |
@@ -261,6 +255,8 @@ void dht_get_shared_key_sent(DHT *dht, uint8_t *shared_key, const uint8_t *publi | |||
261 | 255 | ||
262 | void dht_getnodes(DHT *dht, const IP_Port *from_ipp, const uint8_t *from_id, const uint8_t *which_id); | 256 | void dht_getnodes(DHT *dht, const IP_Port *from_ipp, const uint8_t *from_id, const uint8_t *which_id); |
263 | 257 | ||
258 | typedef void dht_ip_cb(void *object, int32_t number, IP_Port ip_port); | ||
259 | |||
264 | /* Add a new friend to the friends list. | 260 | /* Add a new friend to the friends list. |
265 | * public_key must be CRYPTO_PUBLIC_KEY_SIZE bytes long. | 261 | * public_key must be CRYPTO_PUBLIC_KEY_SIZE bytes long. |
266 | * | 262 | * |
@@ -273,7 +269,7 @@ void dht_getnodes(DHT *dht, const IP_Port *from_ipp, const uint8_t *from_id, con | |||
273 | * return 0 if success. | 269 | * return 0 if success. |
274 | * return -1 if failure (friends list is full). | 270 | * return -1 if failure (friends list is full). |
275 | */ | 271 | */ |
276 | int dht_addfriend(DHT *dht, const uint8_t *public_key, void (*ip_callback)(void *data, int32_t number, IP_Port), | 272 | int dht_addfriend(DHT *dht, const uint8_t *public_key, dht_ip_cb *ip_callback, |
277 | void *data, int32_t number, uint16_t *lock_count); | 273 | void *data, int32_t number, uint16_t *lock_count); |
278 | 274 | ||
279 | /* Delete a friend from the friends list. | 275 | /* Delete a friend from the friends list. |
@@ -387,7 +383,7 @@ int route_tofriend(const DHT *dht, const uint8_t *friend_id, const uint8_t *pack | |||
387 | 383 | ||
388 | /* Function to handle crypto packets. | 384 | /* Function to handle crypto packets. |
389 | */ | 385 | */ |
390 | void cryptopacket_registerhandler(DHT *dht, uint8_t byte, cryptopacket_handler_callback cb, void *object); | 386 | void cryptopacket_registerhandler(DHT *dht, uint8_t byte, cryptopacket_handler_cb *cb, void *object); |
391 | 387 | ||
392 | /* SAVE/LOAD functions */ | 388 | /* SAVE/LOAD functions */ |
393 | 389 | ||