10#include <sys/socket.h>
15namespace swoc {
inline namespace SWOC_VERSION_NS {
57 constexpr operator IP4Addr const &()
const;
60 constexpr IP4Addr const &addr()
const;
63 in_port_t host_order_port()
const;
66 in_port_t network_order_port()
const;
71 static constexpr sa_family_t family();
78 bool operator>(self_type
const &that)
const;
93 self_type &assign(
IP4Addr const &addr);
100 self_type &assign(in_port_t port);
108 self_type &assign(
IP4Addr const &addr, in_port_t port);
115 self_type &assign(sockaddr_in
const *s);
149 explicit IP6Srv(sockaddr_in6
const *s);
167 constexpr operator IP6Addr const &()
const;
179 static constexpr sa_family_t
family();
181 bool operator==(self_type that)
const;
182 bool operator!=(self_type that)
const;
184 bool operator<(self_type that)
const;
185 bool operator<=(self_type
const &that)
const;
186 bool operator>(self_type
const &that)
const;
187 bool operator>=(self_type
const &that)
const;
201 self_type &
assign(in_port_t port);
216 self_type &
assign(sockaddr_in6
const *s);
226 using self_type =
IPSrv;
236 explicit IPSrv(sockaddr
const *sa);
238 explicit IPSrv(sockaddr_in
const *s);
240 explicit IPSrv(sockaddr_in6
const *s);
262 constexpr in_port_t host_order_port()
const;
264 in_port_t network_order_port()
const;
266 constexpr sa_family_t family()
const;
269 bool is_valid()
const;
276 IP4Srv const & ip4()
const;
278 IP6Srv const & ip6()
const;
285 self_type &assign(
IP4Addr const &addr);
292 self_type &assign(
IP6Addr const &addr);
302 self_type &assign(
IPAddr const &addr);
309 self_type &assign(in_port_t port);
317 self_type &assign(
IP4Addr const &addr, in_port_t port);
325 self_type &assign(
IP6Addr const &addr, in_port_t port);
335 self_type &assign(sockaddr
const *sa);
342 self_type &assign(sockaddr_in
const *s);
349 self_type &assign(sockaddr_in6
const *s);
360 self_type &assign(
IPAddr const &addr, in_port_t port);
369 self_type &
operator=(sockaddr
const *sa);
371 self_type &
operator=(sockaddr_in
const *s);
373 self_type &
operator=(sockaddr_in6
const *s);
386 explicit data(sockaddr_in
const *s) : _ip4(s) {}
390 explicit data(sockaddr_in6
const *s) : _ip6(s) {}
393 IPAddr addr(sa_family_t f)
const;
396 constexpr in_port_t port(sa_family_t f)
const;
426 _port = ntohs(s->sin_port);
429inline constexpr IP4Srv::operator
IP4Addr const &()
const {
432inline constexpr IP4Addr const &
444inline constexpr sa_family_t
450IP4Srv::operator==(IP4Srv::self_type that)
const {
454IP4Srv::operator!=(IP4Srv::self_type that)
const {
458IP4Srv::operator<(IP4Srv::self_type that)
const {
459 return _addr < that._addr || (
_addr == that._addr &&
_port < that._port);
462IP4Srv::operator<=(IP4Srv::self_type
const &that)
const {
463 return _addr < that._addr || (
_addr == that._addr &&
_port <= that._port);
466IP4Srv::operator>(IP4Srv::self_type
const &that)
const {
470IP4Srv::operator>=(IP4Srv::self_type
const &that)
const {
471 return that <= *
this;
480inline constexpr IP6Srv::operator
IP6Addr const &()
const {
483inline constexpr IP6Addr const &
495inline constexpr sa_family_t
501IP6Srv::operator==(IP6Srv::self_type that)
const {
505IP6Srv::operator!=(IP6Srv::self_type that)
const {
506 return _port != that._port ||
_addr != that._addr;
509IP6Srv::operator<(IP6Srv::self_type that)
const {
510 return _addr < that._addr || (
_addr == that._addr &&
_port < that._port);
513IP6Srv::operator<=(IP6Srv::self_type
const &that)
const {
514 return _addr < that._addr || (
_addr == that._addr &&
_port <= that._port);
517IP6Srv::operator>(IP6Srv::self_type
const &that)
const {
521IP6Srv::operator>=(IP6Srv::self_type
const &that)
const {
522 return that <= *
this;
547 _port = ntohs(s->sin6_port);
564inline constexpr sa_family_t
589inline constexpr in_port_t
596 return ntohs(_srv.port(
_family));
608 _srv._ip4.assign(
addr, port);
615 _srv._ip6.assign(
addr, port);
630 _srv._ip4.assign(port);
631 }
else if (this->
is_ip6()) {
632 _srv._ip6.assign(port);
641 }
else if (
addr.is_ip6()) {
652 }
else if (
addr.is_ip6()) {
701 return (f == AF_INET) ?
_ip4.addr() : (f == AF_INET6) ?
_ip6.addr() : IPAddr::INVALID;
704constexpr inline in_port_t
706 return (f == AF_INET) ?
_ip4.host_order_port() : (f == AF_INET6) ?
_ip6.host_order_port() : 0;
723 return !lhs.is_ip4() || lhs.ip4() != rhs;
728 return !rhs.is_ip4() || rhs.ip4() != lhs;
733 return lhs.is_ip4() && lhs.ip4() < rhs;
738 return rhs.is_ip4() && lhs < rhs.ip4();
743 return lhs.is_ip4() && lhs.ip4() <= rhs;
748 return rhs.is_ip4() && lhs <= rhs.ip4();
753 return lhs.is_ip4() && lhs.ip4() > rhs;
758 return rhs.is_ip4() && lhs > rhs.ip4();
763 return lhs.is_ip4() && lhs.ip4() >= rhs;
768 return rhs.is_ip4() && lhs >= rhs.ip4();
773 return lhs.is_ip6() && lhs.ip6() == rhs;
778 return rhs.is_ip6() && rhs.ip6() == lhs;
783 return !lhs.is_ip6() || lhs.ip6() != rhs;
788 return !rhs.is_ip6() || rhs.ip6() != lhs;
793 return lhs.is_ip6() && lhs.ip6() < rhs;
798 return rhs.is_ip6() && lhs < rhs.ip6();
803 return lhs.is_ip6() && lhs.ip6() <= rhs;
808 return rhs.is_ip6() && lhs <= rhs.ip6();
813 return lhs.is_ip6() && lhs.ip6() > rhs;
818 return rhs.is_ip6() && lhs > rhs.ip6();
823 return lhs.is_ip6() && lhs.ip6() >= rhs;
828 return rhs.is_ip6() && lhs >= rhs.ip6();
835 return lhs.is_ip4() && lhs.ip4() == rhs;
840 return rhs.is_ip4() && lhs == rhs.ip4();
845 return !lhs.is_ip4() || lhs.ip4() != rhs;
850 return !rhs.is_ip4() || lhs != rhs.ip4();
855 return lhs.is_ip6() && lhs.ip6() == rhs;
860 return rhs.is_ip6() && lhs == rhs.ip6();
865 return !lhs.is_ip6() || lhs.ip6() != rhs;
870 return !rhs.is_ip6() || lhs != rhs.ip6();
in_addr_t _addr
Address in host order.
An IPv4 address and host_order_port, modeled on an SRV type for DNS.
constexpr IP4Addr const & addr() const
in_port_t network_order_port() const
in_port_t _port
Port [host order].
constexpr IP4Srv()=default
Default constructor.
in_port_t host_order_port() const
self_type & assign(IP4Addr const &addr)
IP4Srv(sockaddr_in const *s)
static constexpr sa_family_t family()
An IPv6 address and host_order_port, modeled on an SRV type for DNS.
self_type & assign(IP6Addr const &addr)
bool load(swoc::TextView text)
IP6Srv()=default
Default constructor.
in_port_t host_order_port() const
static constexpr sa_family_t family()
in_port_t _port
Port [host order].
in_port_t network_order_port() const
constexpr IP6Addr const & addr() const
An IP address and host_order_port, modeled on an SRV type for DNS.
in_port_t network_order_port() const
IP4Srv const & ip4() const
IP6Srv const & ip6() const
sa_family_t _family
Protocol family.
constexpr in_port_t host_order_port() const
IPSrv(IP6Addr addr, in_port_t port=0)
Construct for IPv6 address and port.
IPSrv()=default
Default constructor.
constexpr sa_family_t family() const
self_type & operator=(self_type const &that)=default
Copy assignment.
self_type & assign(IP4Addr const &addr)
For template deduction guides.
bool operator>(IP4Addr const &lhs, IP4Addr const &rhs)
bool operator<=(IP4Addr const &lhs, IP4Addr const &rhs)
bool operator>=(IP4Addr const &lhs, IP4Addr const &rhs)
bool operator==(IPAddr const &lhs, sockaddr const *sa)
Equality.
bool operator!=(IP4Addr const &lhs, IP4Addr const &rhs)
bool operator<(IP4Addr const &lhs, IP4Addr const &rhs)
constexpr in_port_t port(sa_family_t f) const
IPAddr addr(sa_family_t f) const
data(IP6Srv const &srv)
Construct from IPv6 data.
data(IP4Srv const &srv)
Construct from IPv4 data.
std::monostate _nil
Nil / invalid state.
IP4Srv _ip4
IPv4 address (host)
IP6Srv _ip6
IPv6 address (host)