Skip to content

Commit

Permalink
IP: Add missing operator== for IPAddr and sockaddr.
Browse files Browse the repository at this point in the history
  • Loading branch information
SolidWallOfCode committed Dec 9, 2023
1 parent a9d6d47 commit 563597c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
12 changes: 12 additions & 0 deletions code/include/swoc/IPAddr.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1490,3 +1491,14 @@ template <> struct hash<swoc::IPAddr> {
};

} // 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));
}
18 changes: 15 additions & 3 deletions code/src/swoc_ip.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ IP4Addr const IP4Addr::MAX{INADDR_BROADCAST};
IP6Addr const IP6Addr::MIN{0, 0};
IP6Addr const IP6Addr::MAX{std::numeric_limits<uint64_t>::max(), std::numeric_limits<uint64_t>::max()};

IPEndpoint::IPEndpoint(string_view const &text) {
this->invalidate();
this->parse(text);
}

bool
IPEndpoint::assign(sockaddr *dst, sockaddr const *src) {
size_t n = 0;
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions unit_tests/test_ip.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand All @@ -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);
Expand Down

0 comments on commit 563597c

Please sign in to comment.