|
9 | 9 | #include "DHT.h" |
10 | 10 |
|
11 | 11 | #include <assert.h> |
12 | | -#include <limits.h> |
13 | 12 | #include <stdlib.h> |
14 | 13 | #include <string.h> |
15 | 14 |
|
@@ -364,84 +363,6 @@ int packed_node_size(Family ip_family) |
364 | 363 | } |
365 | 364 |
|
366 | 365 |
|
367 | | -/** @brief Packs an IP structure. |
368 | | - * |
369 | | - * It's the caller's responsibility to make sure `is_ipv4` tells the truth. This |
370 | | - * function is an implementation detail of @ref bin_pack_ip_port. |
371 | | - * |
372 | | - * @param is_ipv4 whether this IP is an IP4 or IP6. |
373 | | - * |
374 | | - * @retval true on success. |
375 | | - */ |
376 | | -non_null() |
377 | | -static bool bin_pack_ip(Bin_Pack *bp, const IP *ip, bool is_ipv4) |
378 | | -{ |
379 | | - if (is_ipv4) { |
380 | | - return bin_pack_bin_b(bp, ip->ip.v4.uint8, SIZE_IP4); |
381 | | - } else { |
382 | | - return bin_pack_bin_b(bp, ip->ip.v6.uint8, SIZE_IP6); |
383 | | - } |
384 | | -} |
385 | | - |
386 | | -/** @brief Packs an IP_Port structure. |
387 | | - * |
388 | | - * @retval true on success. |
389 | | - */ |
390 | | -non_null() |
391 | | -static bool bin_pack_ip_port(Bin_Pack *bp, const Logger *logger, const IP_Port *ip_port) |
392 | | -{ |
393 | | - bool is_ipv4; |
394 | | - uint8_t family; |
395 | | - |
396 | | - if (net_family_is_ipv4(ip_port->ip.family)) { |
397 | | - // TODO(irungentoo): use functions to convert endianness |
398 | | - is_ipv4 = true; |
399 | | - family = TOX_AF_INET; |
400 | | - } else if (net_family_is_tcp_ipv4(ip_port->ip.family)) { |
401 | | - is_ipv4 = true; |
402 | | - family = TOX_TCP_INET; |
403 | | - } else if (net_family_is_ipv6(ip_port->ip.family)) { |
404 | | - is_ipv4 = false; |
405 | | - family = TOX_AF_INET6; |
406 | | - } else if (net_family_is_tcp_ipv6(ip_port->ip.family)) { |
407 | | - is_ipv4 = false; |
408 | | - family = TOX_TCP_INET6; |
409 | | - } else { |
410 | | - Ip_Ntoa ip_str; |
411 | | - // TODO(iphydf): Find out why we're trying to pack invalid IPs, stop |
412 | | - // doing that, and turn this into an error. |
413 | | - LOGGER_TRACE(logger, "cannot pack invalid IP: %s", net_ip_ntoa(&ip_port->ip, &ip_str)); |
414 | | - return false; |
415 | | - } |
416 | | - |
417 | | - return bin_pack_u08_b(bp, family) |
418 | | - && bin_pack_ip(bp, &ip_port->ip, is_ipv4) |
419 | | - && bin_pack_u16_b(bp, net_ntohs(ip_port->port)); |
420 | | -} |
421 | | - |
422 | | -non_null() |
423 | | -static bool bin_pack_ip_port_handler(const void *obj, const Logger *logger, Bin_Pack *bp) |
424 | | -{ |
425 | | - const IP_Port *ip_port = (const IP_Port *)obj; |
426 | | - return bin_pack_ip_port(bp, logger, ip_port); |
427 | | -} |
428 | | - |
429 | | -int pack_ip_port(const Logger *logger, uint8_t *data, uint16_t length, const IP_Port *ip_port) |
430 | | -{ |
431 | | - const uint32_t size = bin_pack_obj_size(bin_pack_ip_port_handler, ip_port, logger); |
432 | | - |
433 | | - if (size > length) { |
434 | | - return -1; |
435 | | - } |
436 | | - |
437 | | - if (!bin_pack_obj(bin_pack_ip_port_handler, ip_port, logger, data, length)) { |
438 | | - return -1; |
439 | | - } |
440 | | - |
441 | | - assert(size < INT_MAX); |
442 | | - return (int)size; |
443 | | -} |
444 | | - |
445 | 366 | int dht_create_packet(const Memory *mem, const Random *rng, |
446 | 367 | const uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE], |
447 | 368 | const uint8_t *shared_key, const uint8_t type, |
@@ -478,66 +399,6 @@ int dht_create_packet(const Memory *mem, const Random *rng, |
478 | 399 | return 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + encrypted_length; |
479 | 400 | } |
480 | 401 |
|
481 | | -int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length, bool tcp_enabled) |
482 | | -{ |
483 | | - if (data == nullptr) { |
484 | | - return -1; |
485 | | - } |
486 | | - |
487 | | - bool is_ipv4; |
488 | | - Family host_family; |
489 | | - |
490 | | - if (data[0] == TOX_AF_INET) { |
491 | | - is_ipv4 = true; |
492 | | - host_family = net_family_ipv4(); |
493 | | - } else if (data[0] == TOX_TCP_INET) { |
494 | | - if (!tcp_enabled) { |
495 | | - return -1; |
496 | | - } |
497 | | - |
498 | | - is_ipv4 = true; |
499 | | - host_family = net_family_tcp_ipv4(); |
500 | | - } else if (data[0] == TOX_AF_INET6) { |
501 | | - is_ipv4 = false; |
502 | | - host_family = net_family_ipv6(); |
503 | | - } else if (data[0] == TOX_TCP_INET6) { |
504 | | - if (!tcp_enabled) { |
505 | | - return -1; |
506 | | - } |
507 | | - |
508 | | - is_ipv4 = false; |
509 | | - host_family = net_family_tcp_ipv6(); |
510 | | - } else { |
511 | | - return -1; |
512 | | - } |
513 | | - |
514 | | - ipport_reset(ip_port); |
515 | | - |
516 | | - if (is_ipv4) { |
517 | | - const uint32_t size = 1 + SIZE_IP4 + sizeof(uint16_t); |
518 | | - |
519 | | - if (size > length) { |
520 | | - return -1; |
521 | | - } |
522 | | - |
523 | | - ip_port->ip.family = host_family; |
524 | | - memcpy(&ip_port->ip.ip.v4, data + 1, SIZE_IP4); |
525 | | - memcpy(&ip_port->port, data + 1 + SIZE_IP4, sizeof(uint16_t)); |
526 | | - return size; |
527 | | - } else { |
528 | | - const uint32_t size = 1 + SIZE_IP6 + sizeof(uint16_t); |
529 | | - |
530 | | - if (size > length) { |
531 | | - return -1; |
532 | | - } |
533 | | - |
534 | | - ip_port->ip.family = host_family; |
535 | | - memcpy(&ip_port->ip.ip.v6, data + 1, SIZE_IP6); |
536 | | - memcpy(&ip_port->port, data + 1 + SIZE_IP6, sizeof(uint16_t)); |
537 | | - return size; |
538 | | - } |
539 | | -} |
540 | | - |
541 | 402 | /** @brief Pack a single node from a node array. |
542 | 403 | * |
543 | 404 | * @retval true on success. |
|
0 commit comments