summaryrefslogtreecommitdiff
path: root/protocols/Tox/libtox/src/toxcore/network.c
diff options
context:
space:
mode:
authorGeorge Hazan <george.hazan@gmail.com>2023-08-23 19:38:07 +0300
committerGeorge Hazan <george.hazan@gmail.com>2023-08-23 19:38:07 +0300
commitabacfceb23fc41d0b632a7985c2291c02225b432 (patch)
tree186f0c918c8bf671e0bec368f9ff27fd2352d47c /protocols/Tox/libtox/src/toxcore/network.c
parenta8249e73fa598dde2fbde216c7e8f416ebdbf88d (diff)
libtox synced with their master, it could help to fix #3649
Diffstat (limited to 'protocols/Tox/libtox/src/toxcore/network.c')
-rw-r--r--protocols/Tox/libtox/src/toxcore/network.c170
1 files changed, 150 insertions, 20 deletions
diff --git a/protocols/Tox/libtox/src/toxcore/network.c b/protocols/Tox/libtox/src/toxcore/network.c
index 35893b73dc..8544496c1c 100644
--- a/protocols/Tox/libtox/src/toxcore/network.c
+++ b/protocols/Tox/libtox/src/toxcore/network.c
@@ -655,6 +655,121 @@ static uint32_t data_1(uint16_t buflen, const uint8_t *buffer)
return data;
}
+static const char *net_packet_type_name(Net_Packet_Type type)
+{
+ switch (type) {
+ case NET_PACKET_PING_REQUEST:
+ return "PING_REQUEST";
+
+ case NET_PACKET_PING_RESPONSE:
+ return "PING_RESPONSE";
+
+ case NET_PACKET_GET_NODES:
+ return "GET_NODES";
+
+ case NET_PACKET_SEND_NODES_IPV6:
+ return "SEND_NODES_IPV6";
+
+ case NET_PACKET_COOKIE_REQUEST:
+ return "COOKIE_REQUEST";
+
+ case NET_PACKET_COOKIE_RESPONSE:
+ return "COOKIE_RESPONSE";
+
+ case NET_PACKET_CRYPTO_HS:
+ return "CRYPTO_HS";
+
+ case NET_PACKET_CRYPTO_DATA:
+ return "CRYPTO_DATA";
+
+ case NET_PACKET_CRYPTO:
+ return "CRYPTO";
+
+ case NET_PACKET_GC_HANDSHAKE:
+ return "GC_HANDSHAKE";
+
+ case NET_PACKET_GC_LOSSLESS:
+ return "GC_LOSSLESS";
+
+ case NET_PACKET_GC_LOSSY:
+ return "GC_LOSSY";
+
+ case NET_PACKET_LAN_DISCOVERY:
+ return "LAN_DISCOVERY";
+
+ case NET_PACKET_ONION_SEND_INITIAL:
+ return "ONION_SEND_INITIAL";
+
+ case NET_PACKET_ONION_SEND_1:
+ return "ONION_SEND_1";
+
+ case NET_PACKET_ONION_SEND_2:
+ return "ONION_SEND_2";
+
+ case NET_PACKET_ANNOUNCE_REQUEST_OLD:
+ return "ANNOUNCE_REQUEST_OLD";
+
+ case NET_PACKET_ANNOUNCE_RESPONSE_OLD:
+ return "ANNOUNCE_RESPONSE_OLD";
+
+ case NET_PACKET_ONION_DATA_REQUEST:
+ return "ONION_DATA_REQUEST";
+
+ case NET_PACKET_ONION_DATA_RESPONSE:
+ return "ONION_DATA_RESPONSE";
+
+ case NET_PACKET_ANNOUNCE_REQUEST:
+ return "ANNOUNCE_REQUEST";
+
+ case NET_PACKET_ANNOUNCE_RESPONSE:
+ return "ANNOUNCE_RESPONSE";
+
+ case NET_PACKET_ONION_RECV_3:
+ return "ONION_RECV_3";
+
+ case NET_PACKET_ONION_RECV_2:
+ return "ONION_RECV_2";
+
+ case NET_PACKET_ONION_RECV_1:
+ return "ONION_RECV_1";
+
+ case NET_PACKET_FORWARD_REQUEST:
+ return "FORWARD_REQUEST";
+
+ case NET_PACKET_FORWARDING:
+ return "FORWARDING";
+
+ case NET_PACKET_FORWARD_REPLY:
+ return "FORWARD_REPLY";
+
+ case NET_PACKET_DATA_SEARCH_REQUEST:
+ return "DATA_SEARCH_REQUEST";
+
+ case NET_PACKET_DATA_SEARCH_RESPONSE:
+ return "DATA_SEARCH_RESPONSE";
+
+ case NET_PACKET_DATA_RETRIEVE_REQUEST:
+ return "DATA_RETRIEVE_REQUEST";
+
+ case NET_PACKET_DATA_RETRIEVE_RESPONSE:
+ return "DATA_RETRIEVE_RESPONSE";
+
+ case NET_PACKET_STORE_ANNOUNCE_REQUEST:
+ return "STORE_ANNOUNCE_REQUEST";
+
+ case NET_PACKET_STORE_ANNOUNCE_RESPONSE:
+ return "STORE_ANNOUNCE_RESPONSE";
+
+ case BOOTSTRAP_INFO_PACKET_ID:
+ return "BOOTSTRAP_INFO";
+
+ case NET_PACKET_MAX:
+ return "MAX";
+ }
+
+ return "<unknown>";
+}
+
non_null()
static void loglogdata(const Logger *log, const char *message, const uint8_t *buffer,
uint16_t buflen, const IP_Port *ip_port, long res)
@@ -663,21 +778,24 @@ static void loglogdata(const Logger *log, const char *message, const uint8_t *bu
Ip_Ntoa ip_str;
const int error = net_error();
char *strerror = net_new_strerror(error);
- LOGGER_TRACE(log, "[%2u] %s %3u%c %s:%u (%u: %s) | %08x%08x...%02x",
- buffer[0], message, min_u16(buflen, 999), 'E',
+ LOGGER_TRACE(log, "[%02x = %-20s] %s %3u%c %s:%u (%u: %s) | %08x%08x...%02x",
+ buffer[0], net_packet_type_name((Net_Packet_Type)buffer[0]), message,
+ min_u16(buflen, 999), 'E',
net_ip_ntoa(&ip_port->ip, &ip_str), net_ntohs(ip_port->port), error,
strerror, data_0(buflen, buffer), data_1(buflen, buffer), buffer[buflen - 1]);
net_kill_strerror(strerror);
} else if ((res > 0) && ((size_t)res <= buflen)) {
Ip_Ntoa ip_str;
- LOGGER_TRACE(log, "[%2u] %s %3u%c %s:%u (%u: %s) | %08x%08x...%02x",
- buffer[0], message, min_u16(res, 999), (size_t)res < buflen ? '<' : '=',
+ LOGGER_TRACE(log, "[%02x = %-20s] %s %3u%c %s:%u (%u: %s) | %08x%08x...%02x",
+ buffer[0], net_packet_type_name((Net_Packet_Type)buffer[0]), message,
+ min_u16(res, 999), (size_t)res < buflen ? '<' : '=',
net_ip_ntoa(&ip_port->ip, &ip_str), net_ntohs(ip_port->port), 0, "OK",
data_0(buflen, buffer), data_1(buflen, buffer), buffer[buflen - 1]);
} else { /* empty or overwrite */
Ip_Ntoa ip_str;
- LOGGER_TRACE(log, "[%2u] %s %lu%c%u %s:%u (%u: %s) | %08x%08x...%02x",
- buffer[0], message, res, res == 0 ? '!' : '>', buflen,
+ LOGGER_TRACE(log, "[%02x = %-20s] %s %lu%c%u %s:%u (%u: %s) | %08x%08x...%02x",
+ buffer[0], net_packet_type_name((Net_Packet_Type)buffer[0]), message,
+ res, res == 0 ? '!' : '>', buflen,
net_ip_ntoa(&ip_port->ip, &ip_str), net_ntohs(ip_port->port), 0, "OK",
data_0(buflen, buffer), data_1(buflen, buffer), buffer[buflen - 1]);
}
@@ -1616,7 +1734,7 @@ bool net_connect(const Logger *log, Socket sock, const IP_Port *ip_port)
} else {
Ip_Ntoa ip_str;
LOGGER_ERROR(log, "cannot connect to %s:%d which is neither IPv4 nor IPv6",
- net_ip_ntoa(&ip_port->ip, &ip_str), ip_port->port);
+ net_ip_ntoa(&ip_port->ip, &ip_str), net_ntohs(ip_port->port));
return false;
}
@@ -1638,7 +1756,7 @@ bool net_connect(const Logger *log, Socket sock, const IP_Port *ip_port)
if (!should_ignore_connect_error(error)) {
char *net_strerror = net_new_strerror(error);
LOGGER_ERROR(log, "failed to connect to %s:%d: %d (%s)",
- net_ip_ntoa(&ip_port->ip, &ip_str), ip_port->port, error, net_strerror);
+ net_ip_ntoa(&ip_port->ip, &ip_str), net_ntohs(ip_port->port), error, net_strerror);
net_kill_strerror(net_strerror);
return false;
}
@@ -1649,18 +1767,6 @@ bool net_connect(const Logger *log, Socket sock, const IP_Port *ip_port)
int32_t net_getipport(const char *node, IP_Port **res, int tox_type)
{
-#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
- if ((true)) {
- *res = (IP_Port *)calloc(1, sizeof(IP_Port));
- assert(*res != nullptr);
- IP_Port *ip_port = *res;
- ip_port->ip.ip.v4.uint32 = 0x7F000003; // 127.0.0.3
- ip_port->ip.family = *make_tox_family(AF_INET);
-
- return 1;
- }
-#endif
-
// Try parsing as IP address first.
IP_Port parsed = {{{0}}};
@@ -1676,6 +1782,18 @@ int32_t net_getipport(const char *node, IP_Port **res, int tox_type)
return 1;
}
+#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
+ if ((true)) {
+ *res = (IP_Port *)calloc(1, sizeof(IP_Port));
+ assert(*res != nullptr);
+ IP_Port *ip_port = *res;
+ ip_port->ip.ip.v4.uint32 = net_htonl(0x7F000003); // 127.0.0.3
+ ip_port->ip.family = *make_tox_family(AF_INET);
+
+ return 1;
+ }
+#endif
+
// It's not an IP address, so now we try doing a DNS lookup.
struct addrinfo *infos;
const int ret = getaddrinfo(node, nullptr, nullptr, &infos);
@@ -1814,6 +1932,12 @@ uint16_t net_ntohs(uint16_t hostshort)
return ntohs(hostshort);
}
+size_t net_pack_bool(uint8_t *bytes, bool v)
+{
+ bytes[0] = v ? 1 : 0;
+ return 1;
+}
+
size_t net_pack_u16(uint8_t *bytes, uint16_t v)
{
bytes[0] = (v >> 8) & 0xff;
@@ -1837,6 +1961,12 @@ size_t net_pack_u64(uint8_t *bytes, uint64_t v)
return p - bytes;
}
+size_t net_unpack_bool(const uint8_t *bytes, bool *v)
+{
+ *v = bytes[0] != 0;
+ return 1;
+}
+
size_t net_unpack_u16(const uint8_t *bytes, uint16_t *v)
{
const uint8_t hi = bytes[0];