From fc4d0cbce4f5cf7bbfeebde8b312396a4699ead6 Mon Sep 17 00:00:00 2001 From: Melissa Kilby Date: Thu, 27 Jun 2024 23:39:46 +0000 Subject: [PATCH 1/2] update(sinsp/ifinfo): add new public addr_to_string methods Signed-off-by: Melissa Kilby --- userspace/libsinsp/ifinfo.cpp | 29 ++++++++++++++++++++ userspace/libsinsp/ifinfo.h | 5 +++- userspace/libsinsp/test/ifinfo.ut.cpp | 38 +++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/userspace/libsinsp/ifinfo.cpp b/userspace/libsinsp/ifinfo.cpp index 6752cab665..4587824425 100644 --- a/userspace/libsinsp/ifinfo.cpp +++ b/userspace/libsinsp/ifinfo.cpp @@ -69,6 +69,35 @@ std::string sinsp_ipv4_ifinfo::to_string() const return std::string(s); } +std::string sinsp_ipv4_ifinfo::addr_to_string(const uint32_t addr) const +{ + char str_addr[16]; + convert_to_string(str_addr, sizeof(str_addr), addr); + return std::string(str_addr); +} + +std::string sinsp_ipv4_ifinfo::addr_to_string() const +{ + char str_addr[16]; + convert_to_string(str_addr, sizeof(str_addr), m_addr); + return std::string(str_addr); +} + +std::string sinsp_ipv6_ifinfo::addr_to_string() const +{ + std::ostringstream oss; + const uint16_t* words = reinterpret_cast(m_net.m_b); + for (int i = 0; i < 8; ++i) + { + if (i != 0) + { + oss << ':'; + } + oss << std::hex << ntohs(words[i]); + } + return oss.str(); +} + uint32_t sinsp_network_interfaces::infer_ipv4_address(uint32_t destination_address) { std::vector::iterator it; diff --git a/userspace/libsinsp/ifinfo.h b/userspace/libsinsp/ifinfo.h index b0675d53fc..ed42dc399a 100644 --- a/userspace/libsinsp/ifinfo.h +++ b/userspace/libsinsp/ifinfo.h @@ -41,6 +41,8 @@ class SINSP_PUBLIC sinsp_ipv4_ifinfo sinsp_ipv4_ifinfo(uint32_t addr, uint32_t netmask, uint32_t bcast, const char* name); std::string to_string() const; + std::string addr_to_string(const uint32_t addr) const; + std::string addr_to_string() const; std::string address() const; uint32_t m_addr; @@ -60,8 +62,9 @@ class SINSP_PUBLIC sinsp_ipv6_ifinfo public: sinsp_ipv6_ifinfo() = default; - ipv6addr m_net; + std::string addr_to_string() const; + ipv6addr m_net; std::string m_name; }; diff --git a/userspace/libsinsp/test/ifinfo.ut.cpp b/userspace/libsinsp/test/ifinfo.ut.cpp index 9aa8695c32..470d25ad2e 100644 --- a/userspace/libsinsp/test/ifinfo.ut.cpp +++ b/userspace/libsinsp/test/ifinfo.ut.cpp @@ -125,3 +125,41 @@ TEST(sinsp_network_interfaces, infer_defaults_to_first_non_loopback) interfaces.get_ipv4_list()->push_back(make_ipv4_interface("192.168.22.150", "255.255.255.0", "192.168.22.255", "eth1")); EXPECT_ADDR_EQ("192.168.22.149",interfaces.infer_ipv4_address(parse_ipv4_addr("193.168.22.11"))); } + +TEST(sinsp_network_interfaces, ipv4_addr_to_string) +{ + std::vector> ipv4_test_cases = + { + {make_ipv4_localhost(), "127.0.0.1"}, + {make_ipv4_interface("192.168.22.149", "255.255.255.0", "192.168.22.255", "eth0"), "192.168.22.149"}, + {make_ipv4_interface("192.168.22.150", "255.255.255.0", "192.168.22.255", "eth1"), "192.168.22.150"} + }; + + for (const auto& ipv4_test_case : ipv4_test_cases) + { + std::string ip_str = ipv4_test_case.first.addr_to_string(ipv4_test_case.first.m_addr); + ASSERT_EQ(ip_str, ipv4_test_case.second); + ip_str = ipv4_test_case.first.addr_to_string(); + ASSERT_EQ(ip_str, ipv4_test_case.second); + } +} + +TEST(sinsp_network_interfaces, ipv6_addr_to_string) +{ + sinsp_ipv6_ifinfo ifinfo; + + std::vector> ipv6_test_cases = + { + {ipv6addr("2001:0db8:85a3:0000:0000:8a2e:0370:7334"), "2001:db8:85a3:0:0:8a2e:370:7334"}, + {ipv6addr("fe80:0:0:0:2aa:ff:fe9a:4ca3"), "fe80:0:0:0:2aa:ff:fe9a:4ca3"}, + {ipv6addr("0:0:0:0:0:0:0:0"), "0:0:0:0:0:0:0:0"}, + {ipv6addr("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"), "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"} + }; + + for (const auto& ipv6_test_case : ipv6_test_cases) + { + ifinfo.m_net = ipv6_test_case.first; + std::string addr_str = ifinfo.addr_to_string(); + ASSERT_EQ(addr_str, ipv6_test_case.second); + } +} From 8bf2f593795e04d57f42208ed5df54be45ca197c Mon Sep 17 00:00:00 2001 From: Melissa Kilby Date: Fri, 28 Jun 2024 18:51:19 +0000 Subject: [PATCH 2/2] update(sinsp/ifinfo): make overloaded addr_to_string static Signed-off-by: Melissa Kilby Co-authored-by: Federico Di Pierro --- userspace/libsinsp/ifinfo.cpp | 2 +- userspace/libsinsp/ifinfo.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/userspace/libsinsp/ifinfo.cpp b/userspace/libsinsp/ifinfo.cpp index 4587824425..fc58b1bbb0 100644 --- a/userspace/libsinsp/ifinfo.cpp +++ b/userspace/libsinsp/ifinfo.cpp @@ -69,7 +69,7 @@ std::string sinsp_ipv4_ifinfo::to_string() const return std::string(s); } -std::string sinsp_ipv4_ifinfo::addr_to_string(const uint32_t addr) const +std::string sinsp_ipv4_ifinfo::addr_to_string(const uint32_t addr) { char str_addr[16]; convert_to_string(str_addr, sizeof(str_addr), addr); diff --git a/userspace/libsinsp/ifinfo.h b/userspace/libsinsp/ifinfo.h index ed42dc399a..0968c3a281 100644 --- a/userspace/libsinsp/ifinfo.h +++ b/userspace/libsinsp/ifinfo.h @@ -41,7 +41,7 @@ class SINSP_PUBLIC sinsp_ipv4_ifinfo sinsp_ipv4_ifinfo(uint32_t addr, uint32_t netmask, uint32_t bcast, const char* name); std::string to_string() const; - std::string addr_to_string(const uint32_t addr) const; + static std::string addr_to_string(const uint32_t addr); std::string addr_to_string() const; std::string address() const;