This commit is contained in:
jeka committed 2026-02-28 15:15:34 +03:00
1 parent b27ea97055
commit 9d0223d3fa
19 files changed
+87 -1496

No files matched your search

+13 -32
View File
@@ -13,10 +13,9 @@
#include <time.h>
void log_dump(const char* prefix, const uint8_t* data, size_t len) {
// Build packet data as hex string for single-line output
char hex_buf[513]; // 256 bytes * 2 chars + 1 for null terminator
char hex_buf[513] = {'\0'};
size_t hex_len = 0;
size_t show_len = (len > 128) ? 128 : len; // Show max 128 bytes
size_t show_len = (len > 128) ? 128 : len;
for (size_t i = 0; i < show_len && hex_len < 512 - 3; i++) {
hex_len += snprintf(hex_buf + hex_len, sizeof(hex_buf) - hex_len, "%02x", data[i]);
@@ -40,39 +39,21 @@ void log_dump(const char* prefix, const uint8_t* data, size_t len) {
}
size_t addr_to_string(const struct sockaddr_storage *addr, char *buf, size_t buflen) {
if (!buf || buflen == 0)
return 0;
ip_str_t ip_to_str(const void *addr, int family) {
ip_str_t result;
result.str[0] = '\0';
buf[0] = '\0'; // всегда валидная строка
char ip[INET6_ADDRSTRLEN];
uint16_t port;
int n;
if (addr->ss_family == AF_INET) {
const struct sockaddr_in *a = (const struct sockaddr_in *)addr;
inet_ntop(AF_INET, &a->sin_addr, ip, sizeof(ip));
port = ntohs(a->sin_port);
n = snprintf(buf, buflen, "%s:%u", ip, port);
} else if (addr->ss_family == AF_INET6) {
const struct sockaddr_in6 *a = (const struct sockaddr_in6 *)addr;
inet_ntop(AF_INET6, &a->sin6_addr, ip, sizeof(ip));
port = ntohs(a->sin6_port);
n = snprintf(buf, buflen, "[%s]:%u", ip, port);
} else {
return 0;
if (!addr) {
return result;
}
// snprintf гарантирует '\0', но на всякий случай:
buf[buflen - 1] = '\0';
if (family == AF_INET) {
inet_ntop(AF_INET, addr, result.str, 16);
} else if (family == AF_INET6) {
inet_ntop(AF_INET6, addr, result.str, 48);
}
// Возвращаем фактическую длину (как snprintf)
return (n < 0) ? 0 : (size_t)n;
return result;
}