10 #include <hilti/rt/exception.h> 11 #include <hilti/rt/extension-points.h> 12 #include <hilti/rt/types/address.h> 13 #include <hilti/rt/util.h> 32 switch ( _prefix.
family() ) {
33 case AddressFamily::IPv4:
34 if ( _length < 0 || _length > 32 )
35 throw InvalidArgument(
fmt(
"prefix length %s is invalid for IPv4 networks", _length));
37 case AddressFamily::IPv6:
38 if ( _length < 0 || _length > 128 )
39 throw InvalidArgument(
fmt(
"prefix length %s is invalid for IPv6 networks", _length));
41 case AddressFamily::Undef:
42 throw InvalidArgument(
43 fmt(
"Network can only be constructed from either IPv4 or IPv6 addresses, not %s", prefix));
68 const auto&
prefix()
const {
return _prefix; }
77 auto length()
const {
return (
family() == AddressFamily::IPv4 ? _length - 96 : _length); }
82 bool operator==(
const Network& other)
const {
return _prefix == other._prefix && _length == other._length; }
83 bool operator!=(
const Network& other)
const {
return ! (*
this == other); }
84 bool operator<(
const Network& other)
const {
85 return std::tie(_prefix, _length) < std::tie(other._prefix, other._length);
92 operator std::string()
const {
93 if ( _prefix.
family() == AddressFamily::Undef )
94 return "<bad network>";
101 if ( _prefix.
family() == AddressFamily::IPv4 )
104 _prefix = _prefix.
mask(_length);
111 namespace detail::adl {
115 inline std::ostream& operator<<(std::ostream& out,
const Network& x) {
std::string to_string(T &&x)
Definition: extension-points.h:26
Network(const std::string &prefix, int length)
Definition: network.h:58
bool contains(const Address &x) const
Definition: network.h:80
Network(const Address &prefix, int length)
Definition: network.h:31
auto length() const
Definition: network.h:77
Address mask(unsigned int width) const
Definition: address.cc:55
AddressFamily family() const
Definition: address.cc:53
auto family() const
Definition: network.h:71
std::string fmt(const char *fmt, const Args &... args)
Definition: fmt.h:13
const auto & prefix() const
Definition: network.h:68