Skip to content

Commit 1710a0d

Browse files
committed
refactor: Move pack/unpack IP_Port from DHT into network module.
It's misplaced in DHT, since the data structures are located in network.
1 parent a975943 commit 1710a0d

File tree

8 files changed

+164
-162
lines changed

8 files changed

+164
-162
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
af83f07bb96eb17a7ee69f174c9960d23758c9c9314144d73e0f57fdef5d55e4 /usr/local/bin/tox-bootstrapd
1+
e9e6e7baafe3cfe9b212ffb4f1ea8a8b48e2dc1ee4c07c6b1b0b04893b3b6464 /usr/local/bin/tox-bootstrapd

toxcore/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ cc_library(
292292
"//c-toxcore/toxav:__pkg__",
293293
],
294294
deps = [
295+
":bin_pack",
295296
":ccompat",
296297
":crypto_core",
297298
":logger",

toxcore/DHT.c

Lines changed: 0 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include "DHT.h"
1010

1111
#include <assert.h>
12-
#include <limits.h>
1312
#include <stdlib.h>
1413
#include <string.h>
1514

@@ -364,84 +363,6 @@ int packed_node_size(Family ip_family)
364363
}
365364

366365

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-
445366
int dht_create_packet(const Memory *mem, const Random *rng,
446367
const uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE],
447368
const uint8_t *shared_key, const uint8_t type,
@@ -478,66 +399,6 @@ int dht_create_packet(const Memory *mem, const Random *rng,
478399
return 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + encrypted_length;
479400
}
480401

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-
541402
/** @brief Pack a single node from a node array.
542403
*
543404
* @retval true on success.

toxcore/DHT.h

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -204,26 +204,6 @@ non_null() const Client_data *dht_friend_client(const DHT_Friend *dht_friend, si
204204
*/
205205
int packed_node_size(Family ip_family);
206206

207-
/** @brief Pack an IP_Port structure into data of max size length.
208-
*
209-
* Packed_length is the offset of data currently packed.
210-
*
211-
* @return size of packed IP_Port data on success.
212-
* @retval -1 on failure.
213-
*/
214-
non_null()
215-
int pack_ip_port(const Logger *logger, uint8_t *data, uint16_t length, const IP_Port *ip_port);
216-
217-
/** @brief Unpack IP_Port structure from data of max size length into ip_port.
218-
*
219-
* len_processed is the offset of data currently unpacked.
220-
*
221-
* @return size of unpacked ip_port on success.
222-
* @retval -1 on failure.
223-
*/
224-
non_null()
225-
int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length, bool tcp_enabled);
226-
227207
/** @brief Encrypt plain and write resulting DHT packet into packet with max size length.
228208
*
229209
* @return size of packet on success.

toxcore/TCP_client.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include <stdio.h>
1313
#include <string.h>
1414

15-
#include "DHT.h"
1615
#include "TCP_common.h"
1716
#include "ccompat.h"
1817
#include "crypto_core.h"

toxcore/TCP_server.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include <unistd.h>
1919
#endif /* TCP_SERVER_USE_EPOLL */
2020

21-
#include "DHT.h"
2221
#include "TCP_common.h"
2322
#include "ccompat.h"
2423
#include "crypto_core.h"

toxcore/network.c

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
#include <stdlib.h>
8282
#include <string.h>
8383

84+
#include "bin_pack.h"
8485
#include "ccompat.h"
8586
#include "logger.h"
8687
#include "mem.h"
@@ -1573,6 +1574,143 @@ void ipport_copy(IP_Port *target, const IP_Port *source)
15731574
*target = tmp;
15741575
}
15751576

1577+
/** @brief Packs an IP structure.
1578+
*
1579+
* It's the caller's responsibility to make sure `is_ipv4` tells the truth. This
1580+
* function is an implementation detail of @ref bin_pack_ip_port.
1581+
*
1582+
* @param is_ipv4 whether this IP is an IP4 or IP6.
1583+
*
1584+
* @retval true on success.
1585+
*/
1586+
non_null()
1587+
static bool bin_pack_ip(Bin_Pack *bp, const IP *ip, bool is_ipv4)
1588+
{
1589+
if (is_ipv4) {
1590+
return bin_pack_bin_b(bp, ip->ip.v4.uint8, SIZE_IP4);
1591+
} else {
1592+
return bin_pack_bin_b(bp, ip->ip.v6.uint8, SIZE_IP6);
1593+
}
1594+
}
1595+
1596+
/** @brief Packs an IP_Port structure.
1597+
*
1598+
* @retval true on success.
1599+
*/
1600+
bool bin_pack_ip_port(Bin_Pack *bp, const Logger *logger, const IP_Port *ip_port)
1601+
{
1602+
bool is_ipv4;
1603+
uint8_t family;
1604+
1605+
if (net_family_is_ipv4(ip_port->ip.family)) {
1606+
// TODO(irungentoo): use functions to convert endianness
1607+
is_ipv4 = true;
1608+
family = TOX_AF_INET;
1609+
} else if (net_family_is_tcp_ipv4(ip_port->ip.family)) {
1610+
is_ipv4 = true;
1611+
family = TOX_TCP_INET;
1612+
} else if (net_family_is_ipv6(ip_port->ip.family)) {
1613+
is_ipv4 = false;
1614+
family = TOX_AF_INET6;
1615+
} else if (net_family_is_tcp_ipv6(ip_port->ip.family)) {
1616+
is_ipv4 = false;
1617+
family = TOX_TCP_INET6;
1618+
} else {
1619+
Ip_Ntoa ip_str;
1620+
// TODO(iphydf): Find out why we're trying to pack invalid IPs, stop
1621+
// doing that, and turn this into an error.
1622+
LOGGER_TRACE(logger, "cannot pack invalid IP: %s", net_ip_ntoa(&ip_port->ip, &ip_str));
1623+
return false;
1624+
}
1625+
1626+
return bin_pack_u08_b(bp, family)
1627+
&& bin_pack_ip(bp, &ip_port->ip, is_ipv4)
1628+
&& bin_pack_u16_b(bp, net_ntohs(ip_port->port));
1629+
}
1630+
1631+
non_null()
1632+
static bool bin_pack_ip_port_handler(const void *obj, const Logger *logger, Bin_Pack *bp)
1633+
{
1634+
const IP_Port *ip_port = (const IP_Port *)obj;
1635+
return bin_pack_ip_port(bp, logger, ip_port);
1636+
}
1637+
1638+
int pack_ip_port(const Logger *logger, uint8_t *data, uint16_t length, const IP_Port *ip_port)
1639+
{
1640+
const uint32_t size = bin_pack_obj_size(bin_pack_ip_port_handler, ip_port, logger);
1641+
1642+
if (size > length) {
1643+
return -1;
1644+
}
1645+
1646+
if (!bin_pack_obj(bin_pack_ip_port_handler, ip_port, logger, data, length)) {
1647+
return -1;
1648+
}
1649+
1650+
assert(size < INT_MAX);
1651+
return (int)size;
1652+
}
1653+
1654+
int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length, bool tcp_enabled)
1655+
{
1656+
if (data == nullptr) {
1657+
return -1;
1658+
}
1659+
1660+
bool is_ipv4;
1661+
Family host_family;
1662+
1663+
if (data[0] == TOX_AF_INET) {
1664+
is_ipv4 = true;
1665+
host_family = net_family_ipv4();
1666+
} else if (data[0] == TOX_TCP_INET) {
1667+
if (!tcp_enabled) {
1668+
return -1;
1669+
}
1670+
1671+
is_ipv4 = true;
1672+
host_family = net_family_tcp_ipv4();
1673+
} else if (data[0] == TOX_AF_INET6) {
1674+
is_ipv4 = false;
1675+
host_family = net_family_ipv6();
1676+
} else if (data[0] == TOX_TCP_INET6) {
1677+
if (!tcp_enabled) {
1678+
return -1;
1679+
}
1680+
1681+
is_ipv4 = false;
1682+
host_family = net_family_tcp_ipv6();
1683+
} else {
1684+
return -1;
1685+
}
1686+
1687+
ipport_reset(ip_port);
1688+
1689+
if (is_ipv4) {
1690+
const uint32_t size = 1 + SIZE_IP4 + sizeof(uint16_t);
1691+
1692+
if (size > length) {
1693+
return -1;
1694+
}
1695+
1696+
ip_port->ip.family = host_family;
1697+
memcpy(&ip_port->ip.ip.v4, data + 1, SIZE_IP4);
1698+
memcpy(&ip_port->port, data + 1 + SIZE_IP4, sizeof(uint16_t));
1699+
return size;
1700+
} else {
1701+
const uint32_t size = 1 + SIZE_IP6 + sizeof(uint16_t);
1702+
1703+
if (size > length) {
1704+
return -1;
1705+
}
1706+
1707+
ip_port->ip.family = host_family;
1708+
memcpy(&ip_port->ip.ip.v6, data + 1, SIZE_IP6);
1709+
memcpy(&ip_port->port, data + 1 + SIZE_IP6, sizeof(uint16_t));
1710+
return size;
1711+
}
1712+
}
1713+
15761714
const char *net_ip_ntoa(const IP *ip, Ip_Ntoa *ip_str)
15771715
{
15781716
assert(ip_str != nullptr);

0 commit comments

Comments
 (0)