Line | Count | Source (jump to first uncovered line) |
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 | 2.05M | { |
19 | 2.05M | uint32_t data = 0; |
20 | | |
21 | 2.05M | if (buflen > 4) { |
22 | 2.05M | net_unpack_u32(buffer + 1, &data); |
23 | 2.05M | } |
24 | | |
25 | 2.05M | return data; |
26 | 2.05M | } |
27 | | static uint32_t data_1(uint16_t buflen, const uint8_t *_Nonnull buffer) |
28 | 2.05M | { |
29 | 2.05M | uint32_t data = 0; |
30 | | |
31 | 2.05M | if (buflen > 8) { |
32 | 2.05M | net_unpack_u32(buffer + 5, &data); |
33 | 2.05M | } |
34 | | |
35 | 2.05M | return data; |
36 | 2.05M | } |
37 | | |
38 | | static const char *net_packet_type_name(Net_Packet_Type type) |
39 | 2.05M | { |
40 | 2.05M | switch (type) { |
41 | 13.7k | case NET_PACKET_PING_REQUEST: |
42 | 13.7k | return "PING_REQUEST"; |
43 | | |
44 | 9.24k | case NET_PACKET_PING_RESPONSE: |
45 | 9.24k | return "PING_RESPONSE"; |
46 | | |
47 | 172k | case NET_PACKET_NODES_REQUEST: |
48 | 172k | return "NODES_REQUEST"; |
49 | | |
50 | 148k | case NET_PACKET_NODES_RESPONSE: |
51 | 148k | return "NODES_RESPONSE"; |
52 | | |
53 | 4.44k | case NET_PACKET_COOKIE_REQUEST: |
54 | 4.44k | return "COOKIE_REQUEST"; |
55 | | |
56 | 3.66k | case NET_PACKET_COOKIE_RESPONSE: |
57 | 3.66k | return "COOKIE_RESPONSE"; |
58 | | |
59 | 3.29k | case NET_PACKET_CRYPTO_HS: |
60 | 3.29k | return "CRYPTO_HS"; |
61 | | |
62 | 571k | case NET_PACKET_CRYPTO_DATA: |
63 | 571k | return "CRYPTO_DATA"; |
64 | | |
65 | 12.3k | case NET_PACKET_CRYPTO: |
66 | 12.3k | return "CRYPTO"; |
67 | | |
68 | 1.34k | case NET_PACKET_GC_HANDSHAKE: |
69 | 1.34k | return "GC_HANDSHAKE"; |
70 | | |
71 | 27.9k | case NET_PACKET_GC_LOSSLESS: |
72 | 27.9k | return "GC_LOSSLESS"; |
73 | | |
74 | 27.0k | case NET_PACKET_GC_LOSSY: |
75 | 27.0k | return "GC_LOSSY"; |
76 | | |
77 | 713 | case NET_PACKET_LAN_DISCOVERY: |
78 | 713 | return "LAN_DISCOVERY"; |
79 | | |
80 | 141k | case NET_PACKET_ONION_SEND_INITIAL: |
81 | 141k | return "ONION_SEND_INITIAL"; |
82 | | |
83 | 136k | case NET_PACKET_ONION_SEND_1: |
84 | 136k | return "ONION_SEND_1"; |
85 | | |
86 | 132k | case NET_PACKET_ONION_SEND_2: |
87 | 132k | return "ONION_SEND_2"; |
88 | | |
89 | 117k | case NET_PACKET_ANNOUNCE_REQUEST_OLD: |
90 | 117k | return "ANNOUNCE_REQUEST_OLD"; |
91 | | |
92 | 113k | case NET_PACKET_ANNOUNCE_RESPONSE_OLD: |
93 | 113k | return "ANNOUNCE_RESPONSE_OLD"; |
94 | | |
95 | 7.49k | case NET_PACKET_ONION_DATA_REQUEST: |
96 | 7.49k | return "ONION_DATA_REQUEST"; |
97 | | |
98 | 6.95k | case NET_PACKET_ONION_DATA_RESPONSE: |
99 | 6.95k | return "ONION_DATA_RESPONSE"; |
100 | | |
101 | 3.00k | case NET_PACKET_ANNOUNCE_REQUEST: |
102 | 3.00k | return "ANNOUNCE_REQUEST"; |
103 | | |
104 | 2.98k | case NET_PACKET_ANNOUNCE_RESPONSE: |
105 | 2.98k | return "ANNOUNCE_RESPONSE"; |
106 | | |
107 | 125k | case NET_PACKET_ONION_RECV_3: |
108 | 125k | return "ONION_RECV_3"; |
109 | | |
110 | 125k | case NET_PACKET_ONION_RECV_2: |
111 | 125k | return "ONION_RECV_2"; |
112 | | |
113 | 124k | case NET_PACKET_ONION_RECV_1: |
114 | 124k | return "ONION_RECV_1"; |
115 | | |
116 | 53 | case NET_PACKET_FORWARD_REQUEST: |
117 | 53 | return "FORWARD_REQUEST"; |
118 | | |
119 | 166 | case NET_PACKET_FORWARDING: |
120 | 166 | return "FORWARDING"; |
121 | | |
122 | 129 | case NET_PACKET_FORWARD_REPLY: |
123 | 129 | return "FORWARD_REPLY"; |
124 | | |
125 | 10.3k | case NET_PACKET_DATA_SEARCH_REQUEST: |
126 | 10.3k | return "DATA_SEARCH_REQUEST"; |
127 | | |
128 | 7.58k | case NET_PACKET_DATA_SEARCH_RESPONSE: |
129 | 7.58k | return "DATA_SEARCH_RESPONSE"; |
130 | | |
131 | 22 | case NET_PACKET_DATA_RETRIEVE_REQUEST: |
132 | 22 | return "DATA_RETRIEVE_REQUEST"; |
133 | | |
134 | 22 | case NET_PACKET_DATA_RETRIEVE_RESPONSE: |
135 | 22 | return "DATA_RETRIEVE_RESPONSE"; |
136 | | |
137 | 17 | case NET_PACKET_STORE_ANNOUNCE_REQUEST: |
138 | 17 | return "STORE_ANNOUNCE_REQUEST"; |
139 | | |
140 | 16 | case NET_PACKET_STORE_ANNOUNCE_RESPONSE: |
141 | 16 | return "STORE_ANNOUNCE_RESPONSE"; |
142 | | |
143 | 21 | case BOOTSTRAP_INFO_PACKET_ID: |
144 | 21 | return "BOOTSTRAP_INFO"; |
145 | | |
146 | 18 | case NET_PACKET_MAX: |
147 | 18 | return "MAX"; |
148 | 2.05M | } |
149 | | |
150 | 4.27k | return "<unknown>"; |
151 | 2.05M | } |
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 | 2.05M | { |
156 | 2.05M | if (res < 0) { /* Windows doesn't necessarily know `%zu` */ |
157 | 1.11k | Ip_Ntoa ip_str; |
158 | 1.11k | const int error = net_error(); |
159 | 1.11k | Net_Strerror error_str; |
160 | 1.11k | LOGGER_TRACE(log, "[%02x = %-21s] %s %3u%c %s:%u (%d: %s) | %08x%08x...%02x", |
161 | 1.11k | buffer[0], net_packet_type_name((Net_Packet_Type)buffer[0]), message, |
162 | 1.11k | min_u16(buflen, 999), 'E', |
163 | 1.11k | net_ip_ntoa(&ip_port->ip, &ip_str), net_ntohs(ip_port->port), error, |
164 | 1.11k | net_strerror(error, &error_str), data_0(buflen, buffer), data_1(buflen, buffer), buffer[buflen - 1]); |
165 | 2.05M | } else if ((res > 0) && ((size_t)res <= buflen)) { |
166 | 2.05M | Ip_Ntoa ip_str; |
167 | 2.05M | LOGGER_TRACE(log, "[%02x = %-21s] %s %3u%c %s:%u (%d: %s) | %08x%08x...%02x", |
168 | 2.05M | buffer[0], net_packet_type_name((Net_Packet_Type)buffer[0]), message, |
169 | 2.05M | min_u16(res, 999), (size_t)res < buflen ? '<' : '=', |
170 | 2.05M | net_ip_ntoa(&ip_port->ip, &ip_str), net_ntohs(ip_port->port), 0, "OK", |
171 | 2.05M | data_0(buflen, buffer), data_1(buflen, buffer), buffer[buflen - 1]); |
172 | 2.05M | } else { /* empty or overwrite */ |
173 | 0 | Ip_Ntoa ip_str; |
174 | 0 | LOGGER_TRACE(log, "[%02x = %-21s] %s %ld%c%u %s:%u (%d: %s) | %08x%08x...%02x", |
175 | 0 | buffer[0], net_packet_type_name((Net_Packet_Type)buffer[0]), message, |
176 | 0 | res, res == 0 ? '!' : '>', buflen, |
177 | 0 | net_ip_ntoa(&ip_port->ip, &ip_str), net_ntohs(ip_port->port), 0, "OK", |
178 | 0 | data_0(buflen, buffer), data_1(buflen, buffer), buffer[buflen - 1]); |
179 | 0 | } |
180 | 2.05M | } |