From 563597cc45420c8986357c7a17eb85d154f4ffee Mon Sep 17 00:00:00 2001 From: "Alan M. Carroll" Date: Fri, 8 Dec 2023 18:21:15 -0600 Subject: [PATCH] IP: Add missing operator== for IPAddr and sockaddr. --- code/include/swoc/IPAddr.h | 12 ++++++++++++ code/src/swoc_ip.cc | 18 +++++++++++++++--- unit_tests/test_ip.cc | 8 ++++++++ 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/code/include/swoc/IPAddr.h b/code/include/swoc/IPAddr.h index 50d14af..ed90efe 100644 --- a/code/include/swoc/IPAddr.h +++ b/code/include/swoc/IPAddr.h @@ -836,6 +836,7 @@ IP4Addr::copy_to(sockaddr *sa) const { } /// Equality. + inline bool operator==(IP4Addr const &lhs, IP4Addr const &rhs) { return lhs._addr == rhs._addr; @@ -1490,3 +1491,14 @@ template <> struct hash { }; } // namespace std + +// These have to be global namespace, unfortunately. +inline bool +operator==(in6_addr const& lhs, in6_addr const& rhs) { + return 0 == memcmp(&lhs, &rhs, sizeof(in6_addr)); +} + +inline bool +operator!=(in6_addr const& lhs, in6_addr const& rhs) { + return 0 != memcmp(&lhs, &rhs, sizeof(in6_addr)); +} diff --git a/code/src/swoc_ip.cc b/code/src/swoc_ip.cc index 0ddc9bb..6403922 100644 --- a/code/src/swoc_ip.cc +++ b/code/src/swoc_ip.cc @@ -41,6 +41,11 @@ IP4Addr const IP4Addr::MAX{INADDR_BROADCAST}; IP6Addr const IP6Addr::MIN{0, 0}; IP6Addr const IP6Addr::MAX{std::numeric_limits::max(), std::numeric_limits::max()}; +IPEndpoint::IPEndpoint(string_view const &text) { + this->invalidate(); + this->parse(text); +} + bool IPEndpoint::assign(sockaddr *dst, sockaddr const *src) { size_t n = 0; @@ -642,9 +647,16 @@ IPAddr::is_multicast() const { return (AF_INET == _family && _addr._ip4.is_multicast()) || (AF_INET6 == _family && _addr._ip6.is_multicast()); } -IPEndpoint::IPEndpoint(string_view const &text) { - this->invalidate(); - this->parse(text); +bool operator == (IPAddr const& lhs, sockaddr const * sa) { + using sa4 = sockaddr_in const *; + using sa6 = sockaddr_in6 const *; + + if (lhs.family() != sa->sa_family) { + return false; + } + return lhs.family() == AF_UNSPEC || + ( lhs.is_ip4() && (lhs.ip4().network_order() == sa4(sa)->sin_addr.s_addr) ) || + ( lhs.is_ip6() && (lhs.ip6().network_order() == sa6(sa)->sin6_addr) ); } bool diff --git a/unit_tests/test_ip.cc b/unit_tests/test_ip.cc index cefc90f..fb0a6b6 100644 --- a/unit_tests/test_ip.cc +++ b/unit_tests/test_ip.cc @@ -189,8 +189,12 @@ TEST_CASE("Basic IP", "[libswoc][ip]") { REQUIRE(a6_3[14] == 0x1C); REQUIRE(a6_2[15] == 0x34); + REQUIRE(a6_1.host_order() != a6_2.host_order()); + a6_1.copy_to(&ep.sa); REQUIRE(a6_1 == IP6Addr(ep.ip6())); + REQUIRE(IPAddr(a6_1) == &ep.sa); + REQUIRE(IPAddr(a6_2) != &ep.sa); a6_2.copy_to(&ep.sa6); REQUIRE(a6_2 == IP6Addr(&ep.sa6)); REQUIRE(a6_1 != IP6Addr(ep.ip6())); @@ -200,8 +204,12 @@ TEST_CASE("Basic IP", "[libswoc][ip]") { a6_1.network_order(ep.sa6.sin6_addr); REQUIRE(a6_1 == IP6Addr(ep.ip6())); in6 = a6_2.network_order(); + REQUIRE(a6_2.host_order() != in6); + REQUIRE(a6_2.network_order() == in6); REQUIRE(a6_2 == IP6Addr(in6)); a6_2.host_order(in6); + REQUIRE(a6_2.network_order() != in6); + REQUIRE(a6_2.host_order() == in6); REQUIRE(in6.s6_addr[0] == 0x34); REQUIRE(in6.s6_addr[6] == 0xff); REQUIRE(in6.s6_addr[13] == 0x88);