From 646a7b1ed44decdb6aff868633419dbf3f3d95d1 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Tue, 23 Dec 2025 16:07:51 +0100 Subject: Add remote address to stats --- lib/Json.h | 18 ++++++++++++++++++ lib/Socket.cpp | 32 +++++++++++++++++++++++++++----- 2 files changed, 45 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/Json.h b/lib/Json.h index a40b41f..8a1a63d 100644 --- a/lib/Json.h +++ b/lib/Json.h @@ -57,6 +57,24 @@ namespace json { bool, std::nullopt_t> v; + explicit value_t() { + v = std::nullopt; + } + + explicit value_t(const std::unordered_map& object) { + v = std::make_shared >( + object); + } + + explicit value_t(const std::vector& array) { + v = array; + } + + value_t(const std::string& str) { + v = str; + } + + void operator=(const std::unordered_map& object) { v = std::make_shared >( object); diff --git a/lib/Socket.cpp b/lib/Socket.cpp index 33c9c73..da9031f 100644 --- a/lib/Socket.cpp +++ b/lib/Socket.cpp @@ -71,13 +71,35 @@ void InetAddress::resolveUdpDestination(const std::string& destination, int port string InetAddress::to_string() const { char received_from_str[64] = {}; - sockaddr *addr = reinterpret_cast(&addr); - const char* ret = inet_ntop(AF_INET, addr, received_from_str, 63); - if (ret == nullptr) { - throw invalid_argument(string("Error converting InetAddress") + strerror(errno)); + if (addr.ss_family == AF_INET) { + const sockaddr_in *s = reinterpret_cast(&addr); + const char *ret = inet_ntop(AF_INET, &s->sin_addr, received_from_str, 63); + + if (ret == nullptr) { + throw invalid_argument(string("Error converting AF_INET InetAddress") + strerror(errno)); + } + + auto port = ntohs(s->sin_port); + + return string(ret) + ":" + std::to_string(port); + } + else if (addr.ss_family == AF_INET6) { + const sockaddr_in6 *s = reinterpret_cast(&addr); + const char *ret = inet_ntop(AF_INET6, &s->sin6_addr, received_from_str, 63); + + if (ret == nullptr) { + throw invalid_argument(string("Error converting AF_INET6 InetAddress") + strerror(errno)); + } + + auto port = ntohs(s->sin6_port); + + return string(ret) + ":" + std::to_string(port); + } + else { + fprintf(stderr, "Unknown AF %u\n", addr.ss_family); + return ""; } - return ret; } UDPPacket::UDPPacket() { } -- cgit v1.2.3