diff options
author | irungentoo <irungentoo@gmail.com> | 2014-05-13 14:53:59 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2014-05-13 14:53:59 -0400 |
commit | a05ab89dbfa0ff3fe2f926dbc583e2c2964e9fa2 (patch) | |
tree | 2cea00d0273a482d95bba2c018d0523edbab23c4 /toxcore | |
parent | a5ff3f8aff43a0f98592eb8b72b0db8671fe4354 (diff) |
pack/unpack nodes can now pack and unpack TCP nodes.
Diffstat (limited to 'toxcore')
-rw-r--r-- | toxcore/DHT.c | 63 | ||||
-rw-r--r-- | toxcore/DHT.h | 3 | ||||
-rw-r--r-- | toxcore/onion_client.c | 4 |
3 files changed, 58 insertions, 12 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 9a5550dd..5d6992f1 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c | |||
@@ -205,24 +205,43 @@ int pack_nodes(uint8_t *data, uint16_t length, Node_format *nodes, uint16_t numb | |||
205 | uint32_t i, packed_length = 0; | 205 | uint32_t i, packed_length = 0; |
206 | 206 | ||
207 | for (i = 0; i < number; ++i) { | 207 | for (i = 0; i < number; ++i) { |
208 | int ipv6 = -1; | ||
209 | uint8_t net_family; | ||
210 | |||
208 | if (nodes[i].ip_port.ip.family == AF_INET) { | 211 | if (nodes[i].ip_port.ip.family == AF_INET) { |
212 | ipv6 = 0; | ||
213 | net_family = TOX_AF_INET; | ||
214 | } else if (nodes[i].ip_port.ip.family == TCP_INET) { | ||
215 | ipv6 = 0; | ||
216 | net_family = TOX_TCP_INET; | ||
217 | } else if (nodes[i].ip_port.ip.family == AF_INET6) { | ||
218 | ipv6 = 1; | ||
219 | net_family = TOX_AF_INET6; | ||
220 | } else if (nodes[i].ip_port.ip.family == TCP_INET6) { | ||
221 | ipv6 = 1; | ||
222 | net_family = TOX_TCP_INET6; | ||
223 | } else { | ||
224 | return -1; | ||
225 | } | ||
226 | |||
227 | if (ipv6 == 0) { | ||
209 | uint32_t size = 1 + sizeof(IP4) + sizeof(uint16_t) + CLIENT_ID_SIZE; | 228 | uint32_t size = 1 + sizeof(IP4) + sizeof(uint16_t) + CLIENT_ID_SIZE; |
210 | 229 | ||
211 | if (packed_length + size > length) | 230 | if (packed_length + size > length) |
212 | return -1; | 231 | return -1; |
213 | 232 | ||
214 | data[packed_length] = TOX_AF_INET; | 233 | data[packed_length] = net_family; |
215 | memcpy(data + packed_length + 1, &nodes[i].ip_port.ip.ip4, sizeof(IP4)); | 234 | memcpy(data + packed_length + 1, &nodes[i].ip_port.ip.ip4, sizeof(IP4)); |
216 | memcpy(data + packed_length + 1 + sizeof(IP4), &nodes[i].ip_port.port, sizeof(uint16_t)); | 235 | memcpy(data + packed_length + 1 + sizeof(IP4), &nodes[i].ip_port.port, sizeof(uint16_t)); |
217 | memcpy(data + packed_length + 1 + sizeof(IP4) + sizeof(uint16_t), nodes[i].client_id, CLIENT_ID_SIZE); | 236 | memcpy(data + packed_length + 1 + sizeof(IP4) + sizeof(uint16_t), nodes[i].client_id, CLIENT_ID_SIZE); |
218 | packed_length += size; | 237 | packed_length += size; |
219 | } else if (nodes[i].ip_port.ip.family == AF_INET6) { | 238 | } else if (ipv6 == 1) { |
220 | uint32_t size = 1 + sizeof(IP6) + sizeof(uint16_t) + CLIENT_ID_SIZE; | 239 | uint32_t size = 1 + sizeof(IP6) + sizeof(uint16_t) + CLIENT_ID_SIZE; |
221 | 240 | ||
222 | if (packed_length + size > length) | 241 | if (packed_length + size > length) |
223 | return -1; | 242 | return -1; |
224 | 243 | ||
225 | data[packed_length] = TOX_AF_INET6; | 244 | data[packed_length] = net_family; |
226 | memcpy(data + packed_length + 1, &nodes[i].ip_port.ip.ip6, sizeof(IP6)); | 245 | memcpy(data + packed_length + 1, &nodes[i].ip_port.ip.ip6, sizeof(IP6)); |
227 | memcpy(data + packed_length + 1 + sizeof(IP6), &nodes[i].ip_port.port, sizeof(uint16_t)); | 246 | memcpy(data + packed_length + 1 + sizeof(IP6), &nodes[i].ip_port.port, sizeof(uint16_t)); |
228 | memcpy(data + packed_length + 1 + sizeof(IP6) + sizeof(uint16_t), nodes[i].client_id, CLIENT_ID_SIZE); | 247 | memcpy(data + packed_length + 1 + sizeof(IP6) + sizeof(uint16_t), nodes[i].client_id, CLIENT_ID_SIZE); |
@@ -237,35 +256,61 @@ int pack_nodes(uint8_t *data, uint16_t length, Node_format *nodes, uint16_t numb | |||
237 | 256 | ||
238 | /* Unpack data of length into nodes of size max_num_nodes. | 257 | /* Unpack data of length into nodes of size max_num_nodes. |
239 | * Put the length of the data processed in processed_data_len. | 258 | * Put the length of the data processed in processed_data_len. |
259 | * tcp_enabled sets if TCP nodes are expected (true) or not (false). | ||
240 | * | 260 | * |
241 | * return number of unpacked nodes on success. | 261 | * return number of unpacked nodes on success. |
242 | * return -1 on failure. | 262 | * return -1 on failure. |
243 | */ | 263 | */ |
244 | int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint16_t *processed_data_len, uint8_t *data, | 264 | int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint16_t *processed_data_len, uint8_t *data, |
245 | uint16_t length) | 265 | uint16_t length, uint8_t tcp_enabled) |
246 | { | 266 | { |
247 | uint32_t num = 0, len_processed = 0; | 267 | uint32_t num = 0, len_processed = 0; |
248 | 268 | ||
249 | while (num < max_num_nodes && len_processed < length) { | 269 | while (num < max_num_nodes && len_processed < length) { |
270 | int ipv6 = -1; | ||
271 | uint8_t host_family; | ||
272 | |||
250 | if (data[len_processed] == TOX_AF_INET) { | 273 | if (data[len_processed] == TOX_AF_INET) { |
274 | ipv6 = 0; | ||
275 | host_family = AF_INET; | ||
276 | } else if (data[len_processed] == TOX_TCP_INET) { | ||
277 | if (!tcp_enabled) | ||
278 | return -1; | ||
279 | |||
280 | ipv6 = 0; | ||
281 | host_family = TCP_INET; | ||
282 | } else if (data[len_processed] == TOX_AF_INET6) { | ||
283 | ipv6 = 1; | ||
284 | host_family = AF_INET6; | ||
285 | } else if (data[len_processed] == TOX_TCP_INET6) { | ||
286 | if (!tcp_enabled) | ||
287 | return -1; | ||
288 | |||
289 | ipv6 = 1; | ||
290 | host_family = TCP_INET6; | ||
291 | } else { | ||
292 | return -1; | ||
293 | } | ||
294 | |||
295 | if (ipv6 == 0) { | ||
251 | uint32_t size = 1 + sizeof(IP4) + sizeof(uint16_t) + CLIENT_ID_SIZE; | 296 | uint32_t size = 1 + sizeof(IP4) + sizeof(uint16_t) + CLIENT_ID_SIZE; |
252 | 297 | ||
253 | if (len_processed + size > length) | 298 | if (len_processed + size > length) |
254 | return -1; | 299 | return -1; |
255 | 300 | ||
256 | nodes[num].ip_port.ip.family = AF_INET; | 301 | nodes[num].ip_port.ip.family = host_family; |
257 | memcpy(&nodes[num].ip_port.ip.ip4, data + len_processed + 1, sizeof(IP4)); | 302 | memcpy(&nodes[num].ip_port.ip.ip4, data + len_processed + 1, sizeof(IP4)); |
258 | memcpy(&nodes[num].ip_port.port, data + len_processed + 1 + sizeof(IP4), sizeof(uint16_t)); | 303 | memcpy(&nodes[num].ip_port.port, data + len_processed + 1 + sizeof(IP4), sizeof(uint16_t)); |
259 | memcpy(nodes[num].client_id, data + len_processed + 1 + sizeof(IP4) + sizeof(uint16_t), CLIENT_ID_SIZE); | 304 | memcpy(nodes[num].client_id, data + len_processed + 1 + sizeof(IP4) + sizeof(uint16_t), CLIENT_ID_SIZE); |
260 | len_processed += size; | 305 | len_processed += size; |
261 | ++num; | 306 | ++num; |
262 | } else if (data[len_processed] == TOX_AF_INET6) { | 307 | } else if (ipv6 == 1) { |
263 | uint32_t size = 1 + sizeof(IP6) + sizeof(uint16_t) + CLIENT_ID_SIZE; | 308 | uint32_t size = 1 + sizeof(IP6) + sizeof(uint16_t) + CLIENT_ID_SIZE; |
264 | 309 | ||
265 | if (len_processed + size > length) | 310 | if (len_processed + size > length) |
266 | return -1; | 311 | return -1; |
267 | 312 | ||
268 | nodes[num].ip_port.ip.family = AF_INET6; | 313 | nodes[num].ip_port.ip.family = host_family; |
269 | memcpy(&nodes[num].ip_port.ip.ip6, data + len_processed + 1, sizeof(IP6)); | 314 | memcpy(&nodes[num].ip_port.ip.ip6, data + len_processed + 1, sizeof(IP6)); |
270 | memcpy(&nodes[num].ip_port.port, data + len_processed + 1 + sizeof(IP6), sizeof(uint16_t)); | 315 | memcpy(&nodes[num].ip_port.port, data + len_processed + 1 + sizeof(IP6), sizeof(uint16_t)); |
271 | memcpy(nodes[num].client_id, data + len_processed + 1 + sizeof(IP6) + sizeof(uint16_t), CLIENT_ID_SIZE); | 316 | memcpy(nodes[num].client_id, data + len_processed + 1 + sizeof(IP6) + sizeof(uint16_t), CLIENT_ID_SIZE); |
@@ -1156,7 +1201,7 @@ static int handle_sendnodes_core(void *object, IP_Port source, uint8_t *packet, | |||
1156 | return 1; | 1201 | return 1; |
1157 | 1202 | ||
1158 | uint16_t length_nodes = 0; | 1203 | uint16_t length_nodes = 0; |
1159 | int num_nodes = unpack_nodes(plain_nodes, plain[0], &length_nodes, plain + 1, data_size); | 1204 | int num_nodes = unpack_nodes(plain_nodes, plain[0], &length_nodes, plain + 1, data_size, 0); |
1160 | 1205 | ||
1161 | if (length_nodes != data_size) | 1206 | if (length_nodes != data_size) |
1162 | return 1; | 1207 | return 1; |
@@ -2048,7 +2093,7 @@ static int handle_hardening(void *object, IP_Port source, uint8_t *source_pubkey | |||
2048 | 2093 | ||
2049 | uint16_t length_nodes = length - 1 - CLIENT_ID_SIZE; | 2094 | uint16_t length_nodes = length - 1 - CLIENT_ID_SIZE; |
2050 | Node_format nodes[MAX_SENT_NODES]; | 2095 | Node_format nodes[MAX_SENT_NODES]; |
2051 | int num_nodes = unpack_nodes(nodes, MAX_SENT_NODES, 0, packet + 1 + CLIENT_ID_SIZE, length_nodes); | 2096 | int num_nodes = unpack_nodes(nodes, MAX_SENT_NODES, 0, packet + 1 + CLIENT_ID_SIZE, length_nodes, 0); |
2052 | 2097 | ||
2053 | /* TODO: MAX_SENT_NODES nodes should be returned at all times | 2098 | /* TODO: MAX_SENT_NODES nodes should be returned at all times |
2054 | (right now we have a small network size so it could cause problems for testing and etc..) */ | 2099 | (right now we have a small network size so it could cause problems for testing and etc..) */ |
diff --git a/toxcore/DHT.h b/toxcore/DHT.h index d6514ee9..41fe99c9 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h | |||
@@ -147,12 +147,13 @@ int pack_nodes(uint8_t *data, uint16_t length, Node_format *nodes, uint16_t numb | |||
147 | 147 | ||
148 | /* Unpack data of length into nodes of size max_num_nodes. | 148 | /* Unpack data of length into nodes of size max_num_nodes. |
149 | * Put the length of the data processed in processed_data_len. | 149 | * Put the length of the data processed in processed_data_len. |
150 | * tcp_enabled sets if TCP nodes are expected (true) or not (false). | ||
150 | * | 151 | * |
151 | * return number of unpacked nodes on success. | 152 | * return number of unpacked nodes on success. |
152 | * return -1 on failure. | 153 | * return -1 on failure. |
153 | */ | 154 | */ |
154 | int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint16_t *processed_data_len, uint8_t *data, | 155 | int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint16_t *processed_data_len, uint8_t *data, |
155 | uint16_t length); | 156 | uint16_t length, uint8_t tcp_enabled); |
156 | 157 | ||
157 | 158 | ||
158 | /*----------------------------------------------------------------------------------*/ | 159 | /*----------------------------------------------------------------------------------*/ |
diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index 18221320..9c2bd93d 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c | |||
@@ -393,7 +393,7 @@ static int handle_announce_response(void *object, IP_Port source, uint8_t *packe | |||
393 | 393 | ||
394 | if (len_nodes != 0) { | 394 | if (len_nodes != 0) { |
395 | Node_format nodes[MAX_SENT_NODES]; | 395 | Node_format nodes[MAX_SENT_NODES]; |
396 | int num_nodes = unpack_nodes(nodes, MAX_SENT_NODES, 0, plain + 1 + ONION_PING_ID_SIZE, len_nodes); | 396 | int num_nodes = unpack_nodes(nodes, MAX_SENT_NODES, 0, plain + 1 + ONION_PING_ID_SIZE, len_nodes, 0); |
397 | 397 | ||
398 | if (num_nodes <= 0) | 398 | if (num_nodes <= 0) |
399 | return 1; | 399 | return 1; |
@@ -485,7 +485,7 @@ static int handle_fakeid_announce(void *object, uint8_t *source_pubkey, uint8_t | |||
485 | if (len_nodes != 0) { | 485 | if (len_nodes != 0) { |
486 | Node_format nodes[MAX_SENT_NODES]; | 486 | Node_format nodes[MAX_SENT_NODES]; |
487 | int num_nodes = unpack_nodes(nodes, MAX_SENT_NODES, 0, data + 1 + sizeof(uint64_t) + crypto_box_PUBLICKEYBYTES, | 487 | int num_nodes = unpack_nodes(nodes, MAX_SENT_NODES, 0, data + 1 + sizeof(uint64_t) + crypto_box_PUBLICKEYBYTES, |
488 | len_nodes); | 488 | len_nodes, 0); |
489 | 489 | ||
490 | if (num_nodes <= 0) | 490 | if (num_nodes <= 0) |
491 | return 1; | 491 | return 1; |