Skip to content

Commit

Permalink
update(sinsp/ifinfo): add new public addr_to_string methods
Browse files Browse the repository at this point in the history
Signed-off-by: Melissa Kilby <[email protected]>
  • Loading branch information
incertum committed Jun 27, 2024
1 parent ae5fbf6 commit 39635c9
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
29 changes: 29 additions & 0 deletions userspace/libsinsp/ifinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const uint16_t*>(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<sinsp_ipv4_ifinfo>::iterator it;
Expand Down
5 changes: 4 additions & 1 deletion userspace/libsinsp/ifinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
};

Expand Down
38 changes: 38 additions & 0 deletions userspace/libsinsp/test/ifinfo.ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::pair<sinsp_ipv4_ifinfo, std::string>> 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<std::pair<ipv6addr, std::string>> 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);
}
}

0 comments on commit 39635c9

Please sign in to comment.