Skip to content

Commit f84e8cd

Browse files
committed
refactor: Move loglogdata out of network.c.
With another bit of refactoring, this can be an independent bit of code that can be independently benchmarked and tested.
1 parent 390f7db commit f84e8cd

File tree

6 files changed

+224
-170
lines changed

6 files changed

+224
-170
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ set(toxcore_SOURCES
309309
toxcore/mono_time.h
310310
toxcore/net_crypto.c
311311
toxcore/net_crypto.h
312+
toxcore/net_log.c
313+
toxcore/net_log.h
312314
toxcore/net_profile.c
313315
toxcore/net_profile.h
314316
toxcore/network.c

toxcore/BUILD.bazel

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,14 @@ cc_library(
439439

440440
cc_library(
441441
name = "network",
442-
srcs = ["network.c"],
443-
hdrs = ["network.h"],
442+
srcs = [
443+
"net_log.c",
444+
"network.c",
445+
],
446+
hdrs = [
447+
"net_log.h",
448+
"network.h",
449+
],
444450
visibility = [
445451
"//c-toxcore/auto_tests:__pkg__",
446452
"//c-toxcore/other:__pkg__",

toxcore/Makefile.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ libtoxcore_la_SOURCES = ../third_party/cmp/cmp.c \
100100
../toxcore/mono_time.h \
101101
../toxcore/net_crypto.c \
102102
../toxcore/net_crypto.h \
103+
../toxcore/net_log.c \
104+
../toxcore/net_log.h \
103105
../toxcore/net_profile.c \
104106
../toxcore/net_profile.h \
105107
../toxcore/network.c \

toxcore/net_log.c

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
/* SPDX-License-Identifier: GPL-3.0-or-later
2+
* Copyright © 2016-2025 The TokTok team.
3+
* Copyright © 2013 Tox project.
4+
*/
5+
6+
/**
7+
* Network traffic logging function.
8+
*/
9+
10+
#include "net_log.h"
11+
12+
#include "attributes.h"
13+
#include "logger.h"
14+
#include "network.h"
15+
#include "util.h"
16+
17+
static uint32_t data_0(uint16_t buflen, const uint8_t *_Nonnull buffer)
18+
{
19+
uint32_t data = 0;
20+
21+
if (buflen > 4) {
22+
net_unpack_u32(buffer + 1, &data);
23+
}
24+
25+
return data;
26+
}
27+
static uint32_t data_1(uint16_t buflen, const uint8_t *_Nonnull buffer)
28+
{
29+
uint32_t data = 0;
30+
31+
if (buflen > 8) {
32+
net_unpack_u32(buffer + 5, &data);
33+
}
34+
35+
return data;
36+
}
37+
38+
static const char *net_packet_type_name(Net_Packet_Type type)
39+
{
40+
switch (type) {
41+
case NET_PACKET_PING_REQUEST:
42+
return "PING_REQUEST";
43+
44+
case NET_PACKET_PING_RESPONSE:
45+
return "PING_RESPONSE";
46+
47+
case NET_PACKET_NODES_REQUEST:
48+
return "NODES_REQUEST";
49+
50+
case NET_PACKET_NODES_RESPONSE:
51+
return "NODES_RESPONSE";
52+
53+
case NET_PACKET_COOKIE_REQUEST:
54+
return "COOKIE_REQUEST";
55+
56+
case NET_PACKET_COOKIE_RESPONSE:
57+
return "COOKIE_RESPONSE";
58+
59+
case NET_PACKET_CRYPTO_HS:
60+
return "CRYPTO_HS";
61+
62+
case NET_PACKET_CRYPTO_DATA:
63+
return "CRYPTO_DATA";
64+
65+
case NET_PACKET_CRYPTO:
66+
return "CRYPTO";
67+
68+
case NET_PACKET_GC_HANDSHAKE:
69+
return "GC_HANDSHAKE";
70+
71+
case NET_PACKET_GC_LOSSLESS:
72+
return "GC_LOSSLESS";
73+
74+
case NET_PACKET_GC_LOSSY:
75+
return "GC_LOSSY";
76+
77+
case NET_PACKET_LAN_DISCOVERY:
78+
return "LAN_DISCOVERY";
79+
80+
case NET_PACKET_ONION_SEND_INITIAL:
81+
return "ONION_SEND_INITIAL";
82+
83+
case NET_PACKET_ONION_SEND_1:
84+
return "ONION_SEND_1";
85+
86+
case NET_PACKET_ONION_SEND_2:
87+
return "ONION_SEND_2";
88+
89+
case NET_PACKET_ANNOUNCE_REQUEST_OLD:
90+
return "ANNOUNCE_REQUEST_OLD";
91+
92+
case NET_PACKET_ANNOUNCE_RESPONSE_OLD:
93+
return "ANNOUNCE_RESPONSE_OLD";
94+
95+
case NET_PACKET_ONION_DATA_REQUEST:
96+
return "ONION_DATA_REQUEST";
97+
98+
case NET_PACKET_ONION_DATA_RESPONSE:
99+
return "ONION_DATA_RESPONSE";
100+
101+
case NET_PACKET_ANNOUNCE_REQUEST:
102+
return "ANNOUNCE_REQUEST";
103+
104+
case NET_PACKET_ANNOUNCE_RESPONSE:
105+
return "ANNOUNCE_RESPONSE";
106+
107+
case NET_PACKET_ONION_RECV_3:
108+
return "ONION_RECV_3";
109+
110+
case NET_PACKET_ONION_RECV_2:
111+
return "ONION_RECV_2";
112+
113+
case NET_PACKET_ONION_RECV_1:
114+
return "ONION_RECV_1";
115+
116+
case NET_PACKET_FORWARD_REQUEST:
117+
return "FORWARD_REQUEST";
118+
119+
case NET_PACKET_FORWARDING:
120+
return "FORWARDING";
121+
122+
case NET_PACKET_FORWARD_REPLY:
123+
return "FORWARD_REPLY";
124+
125+
case NET_PACKET_DATA_SEARCH_REQUEST:
126+
return "DATA_SEARCH_REQUEST";
127+
128+
case NET_PACKET_DATA_SEARCH_RESPONSE:
129+
return "DATA_SEARCH_RESPONSE";
130+
131+
case NET_PACKET_DATA_RETRIEVE_REQUEST:
132+
return "DATA_RETRIEVE_REQUEST";
133+
134+
case NET_PACKET_DATA_RETRIEVE_RESPONSE:
135+
return "DATA_RETRIEVE_RESPONSE";
136+
137+
case NET_PACKET_STORE_ANNOUNCE_REQUEST:
138+
return "STORE_ANNOUNCE_REQUEST";
139+
140+
case NET_PACKET_STORE_ANNOUNCE_RESPONSE:
141+
return "STORE_ANNOUNCE_RESPONSE";
142+
143+
case BOOTSTRAP_INFO_PACKET_ID:
144+
return "BOOTSTRAP_INFO";
145+
146+
case NET_PACKET_MAX:
147+
return "MAX";
148+
}
149+
150+
return "<unknown>";
151+
}
152+
153+
void net_log_data(const Logger *log, const char *message, const uint8_t *buffer,
154+
uint16_t buflen, const IP_Port *ip_port, long res)
155+
{
156+
if (res < 0) { /* Windows doesn't necessarily know `%zu` */
157+
Ip_Ntoa ip_str;
158+
const int error = net_error();
159+
Net_Strerror error_str;
160+
LOGGER_TRACE(log, "[%02x = %-21s] %s %3u%c %s:%u (%d: %s) | %08x%08x...%02x",
161+
buffer[0], net_packet_type_name((Net_Packet_Type)buffer[0]), message,
162+
min_u16(buflen, 999), 'E',
163+
net_ip_ntoa(&ip_port->ip, &ip_str), net_ntohs(ip_port->port), error,
164+
net_strerror(error, &error_str), data_0(buflen, buffer), data_1(buflen, buffer), buffer[buflen - 1]);
165+
} else if ((res > 0) && ((size_t)res <= buflen)) {
166+
Ip_Ntoa ip_str;
167+
LOGGER_TRACE(log, "[%02x = %-21s] %s %3u%c %s:%u (%d: %s) | %08x%08x...%02x",
168+
buffer[0], net_packet_type_name((Net_Packet_Type)buffer[0]), message,
169+
min_u16(res, 999), (size_t)res < buflen ? '<' : '=',
170+
net_ip_ntoa(&ip_port->ip, &ip_str), net_ntohs(ip_port->port), 0, "OK",
171+
data_0(buflen, buffer), data_1(buflen, buffer), buffer[buflen - 1]);
172+
} else { /* empty or overwrite */
173+
Ip_Ntoa ip_str;
174+
LOGGER_TRACE(log, "[%02x = %-21s] %s %ld%c%u %s:%u (%d: %s) | %08x%08x...%02x",
175+
buffer[0], net_packet_type_name((Net_Packet_Type)buffer[0]), message,
176+
res, res == 0 ? '!' : '>', buflen,
177+
net_ip_ntoa(&ip_port->ip, &ip_str), net_ntohs(ip_port->port), 0, "OK",
178+
data_0(buflen, buffer), data_1(buflen, buffer), buffer[buflen - 1]);
179+
}
180+
}

toxcore/net_log.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* SPDX-License-Identifier: GPL-3.0-or-later
2+
* Copyright © 2016-2025 The TokTok team.
3+
* Copyright © 2013 Tox project.
4+
*/
5+
6+
#ifndef C_TOXCORE_TOXCORE_NET_LOG_H
7+
#define C_TOXCORE_TOXCORE_NET_LOG_H
8+
9+
#include <stdbool.h> // bool
10+
#include <stdint.h> // uint*_t
11+
12+
#include "attributes.h"
13+
#include "logger.h"
14+
#include "network.h"
15+
16+
#ifdef __cplusplus
17+
extern "C" {
18+
#endif
19+
20+
void net_log_data(const Logger *_Nonnull log, const char *_Nonnull message, const uint8_t *_Nonnull buffer,
21+
uint16_t buflen, const IP_Port *_Nonnull ip_port, long res);
22+
23+
#ifdef __cplusplus
24+
} /* extern "C" */
25+
#endif
26+
27+
#endif /* C_TOXCORE_TOXCORE_NET_LOG_H */

0 commit comments

Comments
 (0)