Commit 78681359 authored by Dominik Charousset's avatar Dominik Charousset

Add convenience functions for IP classes

parent 18ba5447
...@@ -46,6 +46,12 @@ public: ...@@ -46,6 +46,12 @@ public:
explicit ipv4_address(array_type bytes); explicit ipv4_address(array_type bytes);
static ipv4_address from_bits(uint32_t bits) {
ipv4_address result;
result.bits(bits);
return result;
}
// -- properties ------------------------------------------------------------- // -- properties -------------------------------------------------------------
/// Returns whether this is a loopback address. /// Returns whether this is a loopback address.
......
...@@ -62,4 +62,9 @@ private: ...@@ -62,4 +62,9 @@ private:
uint8_t prefix_length_; uint8_t prefix_length_;
}; };
// -- related free functions ---------------------------------------------------
/// @relates ipv4_subnet
std::string to_string(ipv4_subnet x);
} // namespace caf } // namespace caf
...@@ -81,6 +81,13 @@ public: ...@@ -81,6 +81,13 @@ public:
int compare(const ipv6_subnet& other) const noexcept; int compare(const ipv6_subnet& other) const noexcept;
// -- inspection -------------------------------------------------------------
template <class Inspector>
friend typename Inspector::result_type inspect(Inspector& f, ipv6_subnet& x) {
return f(x.address_, x.prefix_length_);
}
private: private:
// -- member variables ------------------------------------------------------- // -- member variables -------------------------------------------------------
...@@ -88,5 +95,10 @@ private: ...@@ -88,5 +95,10 @@ private:
uint8_t prefix_length_; uint8_t prefix_length_;
}; };
// -- related free functions ---------------------------------------------------
/// @relates ipv6_subnet
std::string to_string(ipv6_subnet x);
} // namespace caf } // namespace caf
...@@ -55,4 +55,11 @@ int ipv4_subnet::compare(const ipv4_subnet& other) const noexcept { ...@@ -55,4 +55,11 @@ int ipv4_subnet::compare(const ipv4_subnet& other) const noexcept {
: static_cast<int>(prefix_length_) - other.prefix_length_; : static_cast<int>(prefix_length_) - other.prefix_length_;
} }
std::string to_string(ipv4_subnet x) {
auto result = to_string(x.network_address());
result += '/';
result += std::to_string(x.prefix_length());
return result;
}
} // namespace caf } // namespace caf
...@@ -84,5 +84,12 @@ int ipv6_subnet::compare(const ipv6_subnet& other) const noexcept { ...@@ -84,5 +84,12 @@ int ipv6_subnet::compare(const ipv6_subnet& other) const noexcept {
: static_cast<int>(prefix_length_) - other.prefix_length_; : static_cast<int>(prefix_length_) - other.prefix_length_;
} }
std::string to_string(ipv6_subnet x) {
auto result = to_string(x.network_address());
result += '/';
result += std::to_string(x.prefix_length());
return result;
}
} // namespace caf } // namespace caf
...@@ -69,6 +69,8 @@ CAF_TEST(from string) { ...@@ -69,6 +69,8 @@ CAF_TEST(from string) {
CAF_CHECK_EQUAL(from_string("::1:2"), addr({}, {0x01, 0x02})); CAF_CHECK_EQUAL(from_string("::1:2"), addr({}, {0x01, 0x02}));
CAF_CHECK_EQUAL(from_string("::1:2"), addr({}, {0x01, 0x02})); CAF_CHECK_EQUAL(from_string("::1:2"), addr({}, {0x01, 0x02}));
CAF_CHECK_EQUAL(from_string("1::1"), addr({0x01}, {0x01})); CAF_CHECK_EQUAL(from_string("1::1"), addr({0x01}, {0x01}));
CAF_CHECK_EQUAL(from_string("2a00:bdc0:e003::"),
addr({0x2a00, 0xbdc0, 0xe003}, {}));
CAF_CHECK_EQUAL(from_string("1::"), addr({0x01}, {})); CAF_CHECK_EQUAL(from_string("1::"), addr({0x01}, {}));
CAF_CHECK_EQUAL(from_string("0.1.0.1"), addr({}, {0xFFFF, 0x01, 0x01})); CAF_CHECK_EQUAL(from_string("0.1.0.1"), addr({}, {0xFFFF, 0x01, 0x01}));
CAF_CHECK_EQUAL(from_string("::ffff:127.0.0.1"), CAF_CHECK_EQUAL(from_string("::ffff:127.0.0.1"),
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment