6 #include <netinet/in.h> 11 #include <hilti/rt/extension-points.h> 12 #include <hilti/rt/result.h> 13 #include <hilti/rt/types/bytes.h> 14 #include <hilti/rt/types/stream.h> 15 #include <hilti/rt/util.h> 19 HILTI_RT_ENUM(AddressFamily, Undef, IPv4, IPv6);
34 explicit Address(
const std::string& addr) { _parse(addr); }
37 explicit Address(
struct in_addr addr4) { _init(addr4); }
42 explicit Address(
struct in6_addr addr6) { _init(addr6); }
57 explicit Address(uint64_t addr6a, uint64_t addr6b, AddressFamily
family = AddressFamily::IPv6)
58 : _a1(addr6a), _a2(addr6b), _family(
family) {}
72 AddressFamily
family()
const;
85 std::variant<struct in_addr, struct in6_addr>
asInAddr()
const;
87 bool operator==(
const Address& other)
const;
88 bool operator!=(
const Address& other)
const {
return ! (*
this == other); }
89 bool operator<(
const Address& other)
const {
90 return std::tie(_a1, _a2, _family) < std::tie(other._a1, other._a2, other._family);
99 operator std::string()
const;
104 void _init(
struct in_addr addr);
105 void _init(
struct in6_addr addr);
108 void _parse(
const std::string& addr);
113 AddressFamily _family = AddressFamily::Undef;
118 inline Bytes pack(
const Address& addr, ByteOrder
fmt) {
return addr.pack(fmt); }
142 inline Address parse(
const std::string& data) {
return Address(data); }
146 namespace detail::adl {
147 extern std::string
to_string(
const AddressFamily& x, adl::tag );
151 inline std::ostream& operator<<(std::ostream& out,
const Address& x) {
156 inline std::ostream& operator<<(std::ostream& out,
const AddressFamily&
family) {
return out <<
to_string(family); }
std::string to_string(T &&x)
Definition: extension-points.h:26
Address(uint32_t addr4)
Definition: address.h:49
Address(struct in_addr addr4)
Definition: address.h:37
std::variant< struct in_addr, struct in6_addr > asInAddr() const
Definition: address.cc:75
Address(uint64_t addr6a, uint64_t addr6b, AddressFamily family=AddressFamily::IPv6)
Definition: address.h:57
const std::string & str() const &
Definition: bytes.h:223
Address mask(unsigned int width) const
Definition: address.cc:55
Address(struct in6_addr addr6)
Definition: address.h:42
AddressFamily family() const
Definition: address.cc:53
Address(const std::string &addr)
Definition: address.h:34
std::string fmt(const char *fmt, const Args &... args)
Definition: fmt.h:13