Line | Count | Source (jump to first uncovered line) |
1 | | /* SPDX-License-Identifier: GPL-3.0-or-later |
2 | | * Copyright © 2016-2026 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 const char *_Nonnull net_packet_type_name(Net_Packet_Type type) |
18 | 1.14M | { |
19 | 1.14M | switch (type) { |
20 | 29.6k | case NET_PACKET_PING_REQUEST: |
21 | 29.6k | return "PING_REQUEST"; |
22 | | |
23 | 23.4k | case NET_PACKET_PING_RESPONSE: |
24 | 23.4k | return "PING_RESPONSE"; |
25 | | |
26 | 128k | case NET_PACKET_NODES_REQUEST: |
27 | 128k | return "NODES_REQUEST"; |
28 | | |
29 | 128k | case NET_PACKET_NODES_RESPONSE: |
30 | 128k | return "NODES_RESPONSE"; |
31 | | |
32 | 1.06k | case NET_PACKET_COOKIE_REQUEST: |
33 | 1.06k | return "COOKIE_REQUEST"; |
34 | | |
35 | 705 | case NET_PACKET_COOKIE_RESPONSE: |
36 | 705 | return "COOKIE_RESPONSE"; |
37 | | |
38 | 903 | case NET_PACKET_CRYPTO_HS: |
39 | 903 | return "CRYPTO_HS"; |
40 | | |
41 | 216k | case NET_PACKET_CRYPTO_DATA: |
42 | 216k | return "CRYPTO_DATA"; |
43 | | |
44 | 4.47k | case NET_PACKET_CRYPTO: |
45 | 4.47k | return "CRYPTO"; |
46 | | |
47 | 436 | case NET_PACKET_GC_HANDSHAKE: |
48 | 436 | return "GC_HANDSHAKE"; |
49 | | |
50 | 2.11k | case NET_PACKET_GC_LOSSLESS: |
51 | 2.11k | return "GC_LOSSLESS"; |
52 | | |
53 | 2.18k | case NET_PACKET_GC_LOSSY: |
54 | 2.18k | return "GC_LOSSY"; |
55 | | |
56 | 885 | case NET_PACKET_LAN_DISCOVERY: |
57 | 885 | return "LAN_DISCOVERY"; |
58 | | |
59 | 61.6k | case NET_PACKET_ONION_SEND_INITIAL: |
60 | 61.6k | return "ONION_SEND_INITIAL"; |
61 | | |
62 | 73.9k | case NET_PACKET_ONION_SEND_1: |
63 | 73.9k | return "ONION_SEND_1"; |
64 | | |
65 | 73.5k | case NET_PACKET_ONION_SEND_2: |
66 | 73.5k | return "ONION_SEND_2"; |
67 | | |
68 | 66.7k | case NET_PACKET_ANNOUNCE_REQUEST_OLD: |
69 | 66.7k | return "ANNOUNCE_REQUEST_OLD"; |
70 | | |
71 | 53.6k | case NET_PACKET_ANNOUNCE_RESPONSE_OLD: |
72 | 53.6k | return "ANNOUNCE_RESPONSE_OLD"; |
73 | | |
74 | 4.37k | case NET_PACKET_ONION_DATA_REQUEST: |
75 | 4.37k | return "ONION_DATA_REQUEST"; |
76 | | |
77 | 3.68k | case NET_PACKET_ONION_DATA_RESPONSE: |
78 | 3.68k | return "ONION_DATA_RESPONSE"; |
79 | | |
80 | 2.36k | case NET_PACKET_ANNOUNCE_REQUEST: |
81 | 2.36k | return "ANNOUNCE_REQUEST"; |
82 | | |
83 | 2.19k | case NET_PACKET_ANNOUNCE_RESPONSE: |
84 | 2.19k | return "ANNOUNCE_RESPONSE"; |
85 | | |
86 | 72.8k | case NET_PACKET_ONION_RECV_3: |
87 | 72.8k | return "ONION_RECV_3"; |
88 | | |
89 | 72.7k | case NET_PACKET_ONION_RECV_2: |
90 | 72.7k | return "ONION_RECV_2"; |
91 | | |
92 | 72.5k | case NET_PACKET_ONION_RECV_1: |
93 | 72.5k | return "ONION_RECV_1"; |
94 | | |
95 | 88 | case NET_PACKET_FORWARD_REQUEST: |
96 | 88 | return "FORWARD_REQUEST"; |
97 | | |
98 | 205 | case NET_PACKET_FORWARDING: |
99 | 205 | return "FORWARDING"; |
100 | | |
101 | 201 | case NET_PACKET_FORWARD_REPLY: |
102 | 201 | return "FORWARD_REPLY"; |
103 | | |
104 | 14.7k | case NET_PACKET_DATA_SEARCH_REQUEST: |
105 | 14.7k | return "DATA_SEARCH_REQUEST"; |
106 | | |
107 | 11.0k | case NET_PACKET_DATA_SEARCH_RESPONSE: |
108 | 11.0k | return "DATA_SEARCH_RESPONSE"; |
109 | | |
110 | 66 | case NET_PACKET_DATA_RETRIEVE_REQUEST: |
111 | 66 | return "DATA_RETRIEVE_REQUEST"; |
112 | | |
113 | 62 | case NET_PACKET_DATA_RETRIEVE_RESPONSE: |
114 | 62 | return "DATA_RETRIEVE_RESPONSE"; |
115 | | |
116 | 68 | case NET_PACKET_STORE_ANNOUNCE_REQUEST: |
117 | 68 | return "STORE_ANNOUNCE_REQUEST"; |
118 | | |
119 | 72 | case NET_PACKET_STORE_ANNOUNCE_RESPONSE: |
120 | 72 | return "STORE_ANNOUNCE_RESPONSE"; |
121 | | |
122 | 54 | case BOOTSTRAP_INFO_PACKET_ID: |
123 | 54 | return "BOOTSTRAP_INFO"; |
124 | | |
125 | 62 | case NET_PACKET_MAX: |
126 | 62 | return "MAX"; |
127 | 1.14M | } |
128 | | |
129 | 15.0k | return "<unknown>"; |
130 | 1.14M | } |
131 | | |
132 | | void net_log_data(const Logger *log, const char *message, const uint8_t *buffer, |
133 | | uint16_t buflen, const IP_Port *ip_port, long res) |
134 | 1.14M | { |
135 | 1.14M | if (res < 0) { /* Windows doesn't necessarily know `%zu` */ |
136 | 765 | char data_str[TOX_BYTES_TO_STRING_BUF_SIZE] = {'\0'}; |
137 | | |
138 | 765 | if (buflen > 1) { |
139 | 765 | bytes_to_string(buffer + 1, buflen - 1, data_str, sizeof(data_str)); |
140 | 765 | } |
141 | | |
142 | 765 | Ip_Ntoa ip_str; |
143 | 765 | const int error = net_error(); |
144 | 765 | Net_Strerror error_str; |
145 | 765 | LOGGER_TRACE(log, "[%02x = %-21s] %s %3u%c %s:%u (%d: %s) | %s", |
146 | 765 | buffer[0], net_packet_type_name((Net_Packet_Type)buffer[0]), message, |
147 | 765 | min_u16(buflen, 999), 'E', |
148 | 765 | net_ip_ntoa(&ip_port->ip, &ip_str), net_ntohs(ip_port->port), error, |
149 | 765 | net_strerror(error, &error_str), data_str); |
150 | 1.14M | } else if ((res > 0) && ((size_t)res <= buflen)) { |
151 | 1.13M | char data_str[TOX_BYTES_TO_STRING_BUF_SIZE] = {'\0'}; |
152 | | |
153 | 1.13M | if (res > 1) { |
154 | 1.13M | bytes_to_string(buffer + 1, (size_t)res - 1, data_str, sizeof(data_str)); |
155 | 1.13M | } |
156 | | |
157 | 1.13M | Ip_Ntoa ip_str; |
158 | 1.13M | LOGGER_TRACE(log, "[%02x = %-21s] %s %3u%c %s:%u (%d: %s) | %s", |
159 | 1.13M | buffer[0], net_packet_type_name((Net_Packet_Type)buffer[0]), message, |
160 | 1.13M | min_u16(res, 999), (size_t)res < buflen ? '<' : '=', |
161 | 1.13M | net_ip_ntoa(&ip_port->ip, &ip_str), net_ntohs(ip_port->port), 0, "OK", |
162 | 1.13M | data_str); |
163 | 1.13M | } else { /* empty or overwrite */ |
164 | 417 | char data_str[TOX_BYTES_TO_STRING_BUF_SIZE] = {'\0'}; |
165 | | |
166 | 417 | if (buflen > 1) { |
167 | 3 | bytes_to_string(buffer + 1, buflen - 1, data_str, sizeof(data_str)); |
168 | 3 | } |
169 | | |
170 | 417 | Ip_Ntoa ip_str; |
171 | 417 | LOGGER_TRACE(log, "[%02x = %-21s] %s %ld%c%u %s:%u (%d: %s) | %s", |
172 | 417 | buffer[0], net_packet_type_name((Net_Packet_Type)buffer[0]), message, |
173 | 417 | res, res == 0 ? '!' : '>', buflen, |
174 | 417 | net_ip_ntoa(&ip_port->ip, &ip_str), net_ntohs(ip_port->port), 0, "OK", |
175 | 417 | data_str); |
176 | 417 | } |
177 | 1.14M | } |