LibSWOC++ 1.5.14
Solid Wall of C++
Loading...
Searching...
No Matches
swoc::IPEndpoint Union Reference

#include <IPEndpoint.h>

Collaboration diagram for swoc::IPEndpoint:
Collaboration graph

Public Types

using self_type = IPEndpoint
 Self reference type.
 
using string_view = std::string_view
 

Public Member Functions

 IPEndpoint ()
 Default construct invalid instance.
 
 IPEndpoint (self_type const &that)
 Copy constructor.
 
 IPEndpoint (string_view const &text)
 Construct from the text representation of an address.
 
 IPEndpoint (IPAddr const &addr)
 Construct from an address.
 
 IPEndpoint (IPSrv const &srv)
 Construct from address and port.
 
 IPEndpoint (sockaddr const *addr)
 Construct from generic socket address.
 
 IPEndpoint (sockaddr_in const *sin)
 Construct from sockaddr_in.
 
 IPEndpoint (sockaddr_in6 const *sin6)
 Construct from sockaddr_in6.
 
self_typeoperator= (self_type const &that)
 Copy assignment.
 
bool parse (string_view const &str)
 
self_typeinvalidate ()
 Invalidate this endpoint.
 
self_typeassign (sockaddr const *addr)
 
self_typeassign (sockaddr_in const *sin)
 
self_typeassign (sockaddr_in6 const *sin6)
 
self_typeassign (IPAddr const &addr)
 Assign from IP address.
 
self_typeassign (IPAddr const &addr, in_port_t port)
 
self_typeassign (IP4Addr const &addr)
 Assign from IPv4 address.
 
self_typeassign (IP6Addr const &addr)
 Assign from IPv4 address.
 
self_typeassign (IP4Srv const &srv)
 Assign from IPv4 service.
 
self_typeassign (IP6Srv const &srv)
 Assign from IPv6 service.
 
self_typeassign (IPSrv const &srv)
 Assign from IP service.
 
const self_typecopy_to (sockaddr *addr) const
 Copy to sa.
 
bool is_valid () const
 Test for valid IP address.
 
bool is_ip4 () const
 Test for IPv4.
 
bool is_ip6 () const
 Test for IPv6.
 
socklen_t size () const
 
sa_family_t family () const
 
sockaddr_in * ip4 ()
 
sockaddr_in const * ip4 () const
 
sockaddr_in6 * ip6 ()
 
sockaddr_in6 const * ip6 () const
 
self_typeset_to_any (int family)
 
bool is_any () const
 
self_typeset_to_loopback (int family)
 
bool is_loopback () const
 
bool is_link_local () const
 
bool is_private () const
 
bool is_multicast () const
 
in_port_t network_order_port () const
 
in_port_t host_order_port () const
 
 operator sockaddr * ()
 Automatic conversion to sockaddr.
 
 operator sockaddr const * () const
 Automatic conversion to sockaddr.
 
size_t sa_size () const
 Size of the sockaddr variant based on the family.
 
swoc::MemSpan< void const > raw_addr () const
 

Static Public Member Functions

static bool tokenize (string_view src, string_view *host=nullptr, string_view *port=nullptr, string_view *rest=nullptr)
 
static void invalidate (sockaddr *addr)
 Invalidate a sockaddr.
 
static bool assign (sockaddr *dst, sockaddr const *src)
 
static bool is_link_local (sockaddr const *sa)
 
static bool is_private (sockaddr const *sa)
 
static bool is_multicast (sockaddr const *sa)
 
static bool is_valid (sockaddr const *sa)
 
static in_port_t & port (sockaddr *sa)
 
static in_port_t network_order_port (sockaddr const *sa)
 
static in_port_t host_order_port (sockaddr const *sa)
 
static size_t sa_size (sockaddr const *sa)
 Size of the sockaddr based on the family.
 
static string_view family_name (sa_family_t family)
 The string name of the address family.
 

Public Attributes

struct sockaddr sa
 Generic address.
 
struct sockaddr_in sa4
 IPv4.
 
struct sockaddr_in6 sa6
 IPv6.
 

Detailed Description

A union to hold sockaddr compliant IP address structures.

This class contains a number of static methods to perform operations on external sockaddr instances. These are all duplicates of methods that operate on the internal sockaddr and are provided primarily for backwards compatibility during the shift to using this class.

We use the term "endpoint" because these contain more than just the raw address, all of the data for an IP endpoint is present.

Definition at line 36 of file IPEndpoint.h.

Member Typedef Documentation

◆ self_type

Self reference type.

Definition at line 37 of file IPEndpoint.h.

◆ string_view

using swoc::IPEndpoint::string_view = std::string_view

Definition at line 38 of file IPEndpoint.h.

Constructor & Destructor Documentation

◆ IPEndpoint() [1/6]

swoc::IPEndpoint::IPEndpoint ( )
inline

Default construct invalid instance.

Definition at line 280 of file IPEndpoint.h.

◆ IPEndpoint() [2/6]

swoc::IPEndpoint::IPEndpoint ( IPEndpoint::self_type const & that)
inline

Copy constructor.

Definition at line 296 of file IPEndpoint.h.

◆ IPEndpoint() [3/6]

swoc::IPEndpoint::IPEndpoint ( string_view const & text)
explicit

Construct from the text representation of an address.

Definition at line 44 of file swoc_ip.cc.

◆ IPEndpoint() [4/6]

swoc::IPEndpoint::IPEndpoint ( IPAddr const & addr)
inlineexplicit

Construct from an address.

Definition at line 284 of file IPEndpoint.h.

◆ IPEndpoint() [5/6]

swoc::IPEndpoint::IPEndpoint ( IPSrv const & srv)
inlineexplicit

Construct from address and port.

Definition at line 288 of file IPEndpoint.h.

◆ IPEndpoint() [6/6]

swoc::IPEndpoint::IPEndpoint ( sockaddr const * addr)
inline

Construct from generic socket address.

Definition at line 292 of file IPEndpoint.h.

Member Function Documentation

◆ assign() [1/11]

IPEndpoint & swoc::IPEndpoint::assign ( IP4Addr const & addr)

Assign from IPv4 address.

Definition at line 70 of file swoc_ip.cc.

◆ assign() [2/11]

IPEndpoint & swoc::IPEndpoint::assign ( IP4Srv const & srv)

Assign from IPv4 service.

Definition at line 117 of file swoc_ip.cc.

◆ assign() [3/11]

IPEndpoint & swoc::IPEndpoint::assign ( IP6Addr const & addr)

Assign from IPv4 address.

Definition at line 79 of file swoc_ip.cc.

◆ assign() [4/11]

IPEndpoint & swoc::IPEndpoint::assign ( IP6Srv const & srv)

Assign from IPv6 service.

Definition at line 127 of file swoc_ip.cc.

◆ assign() [5/11]

IPEndpoint & swoc::IPEndpoint::assign ( IPAddr const & addr)

Assign from IP address.

Definition at line 88 of file swoc_ip.cc.

◆ assign() [6/11]

IPEndpoint & swoc::IPEndpoint::assign ( IPAddr const & addr,
in_port_t port )

Definition at line 107 of file swoc_ip.cc.

◆ assign() [7/11]

IPEndpoint & swoc::IPEndpoint::assign ( IPSrv const & srv)

Assign from IP service.

Definition at line 137 of file swoc_ip.cc.

◆ assign() [8/11]

bool swoc::IPEndpoint::assign ( sockaddr * dst,
sockaddr const * src )
static

Copy (assign) the contents of src to dst.

The caller must ensure dst is large enough to hold the contents of src, the size of which can vary depending on the type of address in dst.

Parameters
dstDestination.
srcSource.
Returns
true if dst is a valid IP address, false otherwise.

Definition at line 50 of file swoc_ip.cc.

◆ assign() [9/11]

IPEndpoint & swoc::IPEndpoint::assign ( sockaddr const * addr)
inline

Assign from a socket address. The entire address (all parts) are copied if the ip is valid.

Definition at line 333 of file IPEndpoint.h.

◆ assign() [10/11]

IPEndpoint & swoc::IPEndpoint::assign ( sockaddr_in const * sin)
inline

Assign from IPv4 socket address.

Parameters
sinIPv4 socket address.
Returns
this

Definition at line 321 of file IPEndpoint.h.

◆ assign() [11/11]

IPEndpoint & swoc::IPEndpoint::assign ( sockaddr_in6 const * sin6)
inline

Assign from IPv6 socket address.

Parameters
sin6IPv6 socket address.
Returns
this

Definition at line 327 of file IPEndpoint.h.

◆ copy_to()

IPEndpoint const & swoc::IPEndpoint::copy_to ( sockaddr * addr) const
inline

Copy to sa.

Definition at line 339 of file IPEndpoint.h.

◆ family()

sa_family_t swoc::IPEndpoint::family ( ) const
inline
Returns
The IP address family.

Definition at line 355 of file IPEndpoint.h.

◆ family_name()

std::string_view swoc::IPEndpoint::family_name ( sa_family_t family)
static

The string name of the address family.

Definition at line 226 of file swoc_ip.cc.

◆ host_order_port() [1/2]

in_port_t swoc::IPEndpoint::host_order_port ( ) const
inline

Port in host order.

Returns
The port or 0 if not a valid IP address.

Definition at line 385 of file IPEndpoint.h.

◆ host_order_port() [2/2]

in_port_t swoc::IPEndpoint::host_order_port ( sockaddr const * sa)
inlinestatic

Port in host order.

Parameters
saThe socket address.
Returns
The port or 0 if sa is not a valid IP address.

Definition at line 412 of file IPEndpoint.h.

◆ invalidate() [1/2]

IPEndpoint & swoc::IPEndpoint::invalidate ( )
inline

Invalidate this endpoint.

Definition at line 299 of file IPEndpoint.h.

◆ invalidate() [2/2]

void swoc::IPEndpoint::invalidate ( sockaddr * addr)
inlinestatic

Invalidate a sockaddr.

Definition at line 305 of file IPEndpoint.h.

◆ ip4() [1/2]

sockaddr_in * swoc::IPEndpoint::ip4 ( )
inline
Returns
A pointer to a sockaddr_in or nullptr if not IPv4.

Definition at line 360 of file IPEndpoint.h.

◆ ip4() [2/2]

sockaddr_in const * swoc::IPEndpoint::ip4 ( ) const
inline
Returns
A pointer to a sockaddr_in or nullptr if not IPv4.

Definition at line 365 of file IPEndpoint.h.

◆ ip6() [1/2]

sockaddr_in6 * swoc::IPEndpoint::ip6 ( )
inline
Returns
A pointer to a sockaddr_in6 or nullptr if not IPv6.

Definition at line 370 of file IPEndpoint.h.

◆ ip6() [2/2]

sockaddr_in6 const * swoc::IPEndpoint::ip6 ( ) const
inline
Returns
A pointer to a sockaddr_in6 or nullptr if not IPv6.

Definition at line 375 of file IPEndpoint.h.

◆ is_any()

bool swoc::IPEndpoint::is_any ( ) const
Returns
true if this is the ANY address, false if not.

Definition at line 256 of file swoc_ip.cc.

◆ is_ip4()

bool swoc::IPEndpoint::is_ip4 ( ) const
inline

Test for IPv4.

Definition at line 345 of file IPEndpoint.h.

◆ is_ip6()

bool swoc::IPEndpoint::is_ip6 ( ) const
inline

Test for IPv6.

Definition at line 350 of file IPEndpoint.h.

◆ is_link_local() [1/2]

bool swoc::IPEndpoint::is_link_local ( ) const
inline
Returns
true if the address is in the link local network.

Definition at line 427 of file IPEndpoint.h.

◆ is_link_local() [2/2]

bool swoc::IPEndpoint::is_link_local ( sockaddr const * sa)
inlinestatic
Returns
true if the address is in the link local network.

Definition at line 431 of file IPEndpoint.h.

◆ is_loopback()

bool swoc::IPEndpoint::is_loopback ( ) const
Returns
true if this is a loopback address, false if not.

Definition at line 285 of file swoc_ip.cc.

◆ is_multicast() [1/2]

bool swoc::IPEndpoint::is_multicast ( ) const
inline
Returns
true if the address is multicast.

Definition at line 443 of file IPEndpoint.h.

◆ is_multicast() [2/2]

bool swoc::IPEndpoint::is_multicast ( sockaddr const * sa)
inlinestatic
Returns
true if the address is multicast.

Definition at line 447 of file IPEndpoint.h.

◆ is_private() [1/2]

bool swoc::IPEndpoint::is_private ( ) const
inline
Returns
true if the address is private.

Definition at line 435 of file IPEndpoint.h.

◆ is_private() [2/2]

bool swoc::IPEndpoint::is_private ( sockaddr const * sa)
inlinestatic
Returns
true if the address is private.

Definition at line 439 of file IPEndpoint.h.

◆ is_valid() [1/2]

bool swoc::IPEndpoint::is_valid ( ) const
inline

Test for valid IP address.

Definition at line 310 of file IPEndpoint.h.

◆ is_valid() [2/2]

bool swoc::IPEndpoint::is_valid ( sockaddr const * sa)
inlinestatic

Test for valid IP address.

Parameters
saThe socket address.
Returns
true if sa contains a valid IP address, false if not. sa can be nullptr in which case false is returned.

Definition at line 390 of file IPEndpoint.h.

◆ network_order_port() [1/2]

in_port_t swoc::IPEndpoint::network_order_port ( ) const
inline

Port in network order.

Returns
The port or 0 if not a valid IP address.

Definition at line 380 of file IPEndpoint.h.

◆ network_order_port() [2/2]

in_port_t swoc::IPEndpoint::network_order_port ( sockaddr const * sa)
inlinestatic

Port in network order.

Parameters
saThe socket address.
Returns
The port or 0 if sa is not a valid IP address.

Definition at line 407 of file IPEndpoint.h.

◆ operator sockaddr *()

swoc::IPEndpoint::operator sockaddr * ( )
inline

Automatic conversion to sockaddr.

Definition at line 451 of file IPEndpoint.h.

◆ operator sockaddr const *()

swoc::IPEndpoint::operator sockaddr const * ( ) const
inline

Automatic conversion to sockaddr.

Definition at line 453 of file IPEndpoint.h.

◆ operator=()

IPEndpoint & swoc::IPEndpoint::operator= ( self_type const & that)
inline

Copy assignment.

Definition at line 315 of file IPEndpoint.h.

◆ parse()

bool swoc::IPEndpoint::parse ( std::string_view const & str)

Parse a string for an IP address.

The address resulting from the parse is copied to this object if the conversion is successful, otherwise this object is invalidated.

Returns
true on success, false otherwise.

Definition at line 205 of file swoc_ip.cc.

◆ port()

in_port_t & swoc::IPEndpoint::port ( sockaddr * sa)
inlinestatic

Direct access to port.

Returns
Reference to the port in the socket address.
Note
If sa is not a valid IP address an assertion is thrown.
The raw port is in network order. is_valid

Definition at line 395 of file IPEndpoint.h.

◆ raw_addr()

swoc::MemSpan< void const > swoc::IPEndpoint::raw_addr ( ) const
inline

The address as a byte sequence.

Returns
Span of the address memory.

This is raw access. If the contained data is not a valid address family an empty span is returned.

Definition at line 417 of file IPEndpoint.h.

◆ sa_size() [1/2]

size_t swoc::IPEndpoint::sa_size ( ) const
inline

Size of the sockaddr variant based on the family.

Definition at line 455 of file IPEndpoint.h.

◆ sa_size() [2/2]

size_t swoc::IPEndpoint::sa_size ( sockaddr const * sa)
inlinestatic

Size of the sockaddr based on the family.

Definition at line 459 of file IPEndpoint.h.

◆ set_to_any()

IPEndpoint & swoc::IPEndpoint::set_to_any ( int family)

Set to be the ANY address for family family. family must be AF_INET or AF_INET6.

Returns
This object.

Definition at line 241 of file swoc_ip.cc.

◆ set_to_loopback()

IPEndpoint & swoc::IPEndpoint::set_to_loopback ( int family)

Set to be loopback address for family family. family must be AF_INET or AF_INET6.

Returns
This object.

Definition at line 270 of file swoc_ip.cc.

◆ size()

socklen_t swoc::IPEndpoint::size ( ) const

Effectively size of the address.

Returns
The size of the structure appropriate for the address family of the stored address.

Definition at line 214 of file swoc_ip.cc.

◆ tokenize()

bool swoc::IPEndpoint::tokenize ( std::string_view str,
std::string_view * addr = nullptr,
std::string_view * port = nullptr,
std::string_view * rest = nullptr )
static

Break a string in to IP address relevant tokens.

Parameters
srcSource text. [in]
hostThe host / address. [out]
portThe host_order_port. [out]
restAny text past the end of the IP address. [out]
Returns
true if an IP address was found, false otherwise.

Any of the out parameters can be nullptr in which case they are not updated. This parses and discards the IPv6 brackets.

Note
This is intended for internal use to do address parsing, but it can be useful in other contexts.

Easier to work with for parsing.

Definition at line 147 of file swoc_ip.cc.

Member Data Documentation

◆ sa

struct sockaddr swoc::IPEndpoint::sa

Generic address.

Definition at line 40 of file IPEndpoint.h.

◆ sa4

struct sockaddr_in swoc::IPEndpoint::sa4

IPv4.

Definition at line 41 of file IPEndpoint.h.

◆ sa6

struct sockaddr_in6 swoc::IPEndpoint::sa6

IPv6.

Definition at line 42 of file IPEndpoint.h.


The documentation for this union was generated from the following files: