Skip to content

Commit 5ae1f6f

Browse files
committed
permit comparing net::Inet with net::ip4::Addr and net::ip6::Addr
while this can be done explicitly if we know if our ip type is ip4 or ip6, this allows us to pass `auto ip` to the comparison
1 parent 84eede2 commit 5ae1f6f

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

api/net/addr.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,19 @@ union Addr {
122122
bool operator<=(const Addr& other) const noexcept
123123
{ return (*this < other or *this == other); }
124124

125+
126+
bool operator==(const net::ip4::Addr& other) const noexcept
127+
{ return ip4_.addr == other; }
128+
129+
bool operator!=(const net::ip4::Addr& other) const noexcept
130+
{ return ip4_.addr != other; }
131+
132+
bool operator==(const net::ip6::Addr& other) const noexcept
133+
{ return ip6_ == other; }
134+
135+
bool operator!=(const net::ip6::Addr& other) const noexcept
136+
{ return ip6_ != other; }
137+
125138
private:
126139
struct {
127140
uint64_t big;
@@ -135,4 +148,10 @@ union Addr {
135148

136149
static_assert(sizeof(Addr) == sizeof(ip6::Addr));
137150

151+
inline bool operator==(const ip4::Addr& lhs, const Addr& rhs) noexcept { return rhs == lhs; }
152+
inline bool operator!=(const ip4::Addr& lhs, const Addr& rhs) noexcept { return !(lhs == rhs); }
153+
154+
inline bool operator==(const ip6::Addr& lhs, const Addr& rhs) noexcept { return rhs == lhs; }
155+
inline bool operator!=(const ip6::Addr& lhs, const Addr& rhs) noexcept { return !(lhs == rhs); }
156+
138157
}

api/net/inet.hpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,4 +535,34 @@ namespace net {
535535
};
536536
}
537537

538+
namespace net::ip4 {
539+
inline bool operator==(const net::ip4::Addr& lhs, const net::Inet& rhs) noexcept {
540+
return lhs == rhs.ip_addr();
541+
}
542+
inline bool operator==(const net::Inet& lhs, const net::ip4::Addr& rhs) noexcept {
543+
return lhs.ip_addr() == rhs;
544+
}
545+
inline bool operator!=(const net::ip4::Addr& lhs, const net::Inet& rhs) noexcept {
546+
return !(lhs == rhs);
547+
}
548+
inline bool operator!=(const net::Inet& lhs, const net::ip4::Addr& rhs) noexcept {
549+
return !(lhs == rhs);
550+
}
551+
} // namespace net::ip4
552+
553+
namespace net::ip6 {
554+
inline bool operator==(const net::ip6::Addr& lhs, const net::Inet& rhs) noexcept {
555+
return lhs == rhs.ip6_addr();
556+
}
557+
inline bool operator==(const net::Inet& lhs, const net::ip6::Addr& rhs) noexcept {
558+
return lhs.ip6_addr() == rhs;
559+
}
560+
inline bool operator!=(const net::ip6::Addr& lhs, const net::Inet& rhs) noexcept {
561+
return !(lhs == rhs);
562+
}
563+
inline bool operator!=(const net::Inet& lhs, const net::ip6::Addr& rhs) noexcept {
564+
return !(lhs == rhs);
565+
}
566+
} // namespace net::ip6
567+
538568
#endif

test/unit/net/router_test.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ CASE("net::router: Actual routing verifying TTL")
191191
auto packet = static_unique_ptr_cast<net::PacketIP4>(std::move(pckt));
192192
EXPECT(packet->ip_protocol() == Protocol::ICMPv4);
193193
EXPECT(packet->ip_ttl() == PacketIP4::DEFAULT_TTL);
194+
EXPECT(ip == src.address());
195+
EXPECT(ip.is_loopback() == false);
194196

195197
auto icmp = icmp4::Packet(std::move(packet));
196198
ICMP_error err{icmp.type(), icmp.code()};
@@ -208,6 +210,8 @@ CASE("net::router: Actual routing verifying TTL")
208210
EXPECT(packet->destination() == dst);
209211
EXPECT(packet->ip_ttl() == (PacketIP4::DEFAULT_TTL-1));
210212

213+
EXPECT(ip == dst.address());
214+
EXPECT(ip.is_loopback() == false);
211215
tcp_packet_recv++;
212216
});
213217

0 commit comments

Comments
 (0)