[COMMIT seastar master] inet_address: Add helper to check for address being lo/any

0 views
Skip to first unread message

Commit Bot

<bot@cloudius-systems.com>
unread,
Sep 14, 2022, 9:53:00 AMSep 14
to seastar-dev@googlegroups.com, Pavel Emelyanov
From: Pavel Emelyanov <xe...@scylladb.com>
Committer: Avi Kivity <a...@scylladb.com>
Branch: master

inet_address: Add helper to check for address being lo/any

IPv4 loopback address is 127.0.0.0/8
IPv4 any address is 0.0.0.0

IPv6 loopback address is ::1
IPv6 any address is ::

Sanity unit test included.

Signed-off-by: Pavel Emelyanov <xe...@scylladb.com>

---
diff --git a/include/seastar/net/inet_address.hh b/include/seastar/net/inet_address.hh
--- a/include/seastar/net/inet_address.hh
+++ b/include/seastar/net/inet_address.hh
@@ -112,6 +112,9 @@ public:
static future<std::vector<inet_address>> find_all(const sstring&, family);

static std::optional<inet_address> parse_numerical(const sstring&);
+
+ bool is_loopback() const noexcept;
+ bool is_addr_any() const noexcept;
};

std::ostream& operator<<(std::ostream&, const inet_address&);
diff --git a/src/net/inet_address.cc b/src/net/inet_address.cc
--- a/src/net/inet_address.cc
+++ b/src/net/inet_address.cc
@@ -181,6 +181,28 @@ const void * seastar::net::inet_address::data() const noexcept {
return &_in;
}

+bool seastar::net::inet_address::is_loopback() const noexcept {
+ switch (_in_family) {
+ case family::INET:
+ return (net::ntoh(_in.s_addr) & 0xff000000) == 0x7f000000;
+ case family::INET6:
+ return std::equal(std::begin(_in6.s6_addr), std::end(_in6.s6_addr), std::begin(::in6addr_loopback.s6_addr));
+ default:
+ return false;
+ }
+}
+
+bool seastar::net::inet_address::is_addr_any() const noexcept {
+ switch (_in_family) {
+ case family::INET:
+ return _in.s_addr == INADDR_ANY;
+ case family::INET6:
+ return std::equal(std::begin(_in6.s6_addr), std::end(_in6.s6_addr), std::begin(::in6addr_any.s6_addr));
+ default:
+ return false;
+ }
+}
+
seastar::net::ipv6_address::ipv6_address(const ::in6_addr& in) noexcept {
std::copy(std::begin(in.s6_addr), std::end(in.s6_addr), ip.begin());
}
diff --git a/tests/unit/network_interface_test.cc b/tests/unit/network_interface_test.cc
--- a/tests/unit/network_interface_test.cc
+++ b/tests/unit/network_interface_test.cc
@@ -90,3 +90,20 @@ SEASTAR_TEST_CASE(match_ipv6_scope) {

return make_ready_future();
}
+
+SEASTAR_TEST_CASE(is_standard_addresses_sanity) {
+ BOOST_REQUIRE_EQUAL(net::inet_address("127.0.0.1").is_loopback(), true);
+ BOOST_REQUIRE_EQUAL(net::inet_address("127.0.0.11").is_loopback(), true);
+ BOOST_REQUIRE_EQUAL(net::inet_address(::in_addr{INADDR_ANY}).is_addr_any(), true);
+ auto addr = net::inet_address("1.2.3.4");
+ BOOST_REQUIRE_EQUAL(addr.is_loopback(), false);
+ BOOST_REQUIRE_EQUAL(addr.is_addr_any(), false);
+
+ BOOST_REQUIRE_EQUAL(net::inet_address("::1").is_loopback(), true);
+ BOOST_REQUIRE_EQUAL(net::inet_address(::in6addr_any).is_addr_any(), true);
+ auto addr6 = net::inet_address("acf1:f5e5:5a99:337f:ebe2:c57e:0e27:69c6");
+ BOOST_REQUIRE_EQUAL(addr6.is_loopback(), false);
+ BOOST_REQUIRE_EQUAL(addr6.is_addr_any(), false);
+
+ return make_ready_future<>();
+}
Reply all
Reply to author
Forward
0 new messages