| 
 | 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 | +}  | 
0 commit comments