17namespace swoc {
inline namespace SWOC_VERSION_NS {
38 using string_view = std::string_view;
41 struct sockaddr_in
sa4;
42 struct sockaddr_in6
sa6;
83 static bool tokenize(string_view src, string_view *host =
nullptr, string_view *
port =
nullptr, string_view *rest =
nullptr);
92 bool parse(string_view
const &str);
109 static bool assign(sockaddr *dst, sockaddr
const *src);
166 socklen_t
size()
const;
169 sa_family_t
family()
const;
175 sockaddr_in
const *
ip4()
const;
181 sockaddr_in6
const *
ip6()
const;
240 static in_port_t &
port(sockaddr *
sa);
257 operator sockaddr *();
260 operator sockaddr
const *()
const;
266 static size_t sa_size(sockaddr
const*
sa);
281 sa.sa_family = AF_UNSPEC;
300 sa.sa_family = AF_UNSPEC;
306 addr->sa_family = AF_UNSPEC;
311 return sa.sa_family == AF_INET ||
sa.sa_family == AF_INET6;
322 std::memcpy(&
sa4, sin,
sizeof(sockaddr_in));
328 std::memcpy(&
sa6, sin6,
sizeof(sockaddr_in6));
346 return AF_INET ==
sa.sa_family;
351 return AF_INET6 ==
sa.sa_family;
364inline sockaddr_in
const *
374inline sockaddr_in6
const *
381 return this->
is_valid() ? this->
port(
const_cast<sockaddr *
>(&
sa)) : 0;
391 return sa && (
sa->sa_family == AF_INET ||
sa->sa_family == AF_INET6);
396 switch (
sa->sa_family) {
398 return reinterpret_cast<sockaddr_in *
>(
sa)->sin_port;
400 return reinterpret_cast<sockaddr_in6 *
>(
sa)->sin6_port;
403 throw std::domain_error(
"sockaddr does not contain a valid IP address");
418 switch (
sa.sa_family) {
420 return {&
sa4.sin_addr,
sizeof(
sa4.sin_addr)};
422 return {&
sa6.sin6_addr,
sizeof(
sa6.sin6_addr)};
451inline IPEndpoint::operator sockaddr *() {
return &
sa; }
453inline IPEndpoint::operator sockaddr
const *()
const {
return &
sa; }
460 return AF_INET ==
sa->sa_family ?
sizeof(sockaddr_in) : AF_INET6 ==
sa->sa_family ?
sizeof(sockaddr_in6) :
sizeof(sockaddr);
An IPv4 address and host_order_port, modeled on an SRV type for DNS.
An IPv6 address and host_order_port, modeled on an SRV type for DNS.
An IP address and host_order_port, modeled on an SRV type for DNS.
bool is_multicast(sockaddr const *sa)
bool is_private(sockaddr const *sa)
bool is_link_local(sockaddr const *sa)
For template deduction guides.
self_type & invalidate()
Invalidate this endpoint.
self_type & operator=(self_type const &that)
Copy assignment.
swoc::MemSpan< void const > raw_addr() const
bool parse(string_view const &str)
struct sockaddr_in sa4
IPv4.
bool is_ip6() const
Test for IPv6.
static in_port_t & port(sockaddr *sa)
self_type & set_to_loopback(int family)
in_port_t network_order_port() const
IPEndpoint(sockaddr_in const *sin)
Construct from sockaddr_in.
self_type & set_to_any(int family)
IPEndpoint(sockaddr_in6 const *sin6)
Construct from sockaddr_in6.
static bool tokenize(string_view src, string_view *host=nullptr, string_view *port=nullptr, string_view *rest=nullptr)
static bool assign(sockaddr *dst, sockaddr const *src)
bool is_link_local() const
static void invalidate(sockaddr *addr)
Invalidate a sockaddr.
sa_family_t family() const
const self_type & copy_to(sockaddr *addr) const
Copy to sa.
IPEndpoint self_type
Self reference type.
bool is_ip4() const
Test for IPv4.
bool is_multicast() const
struct sockaddr sa
Generic address.
size_t sa_size() const
Size of the sockaddr variant based on the family.
static string_view family_name(sa_family_t family)
The string name of the address family.
struct sockaddr_in6 sa6
IPv6.
IPEndpoint()
Default construct invalid instance.
bool is_valid() const
Test for valid IP address.
in_port_t host_order_port() const