Yate
SocketAddr Class Reference

A socket address holder. More...

#include <yateclass.h>

Inheritance diagram for SocketAddr:
GenObject

Public Types

enum  Family {
  Unknown = AF_UNSPEC , IPv4 = AF_INET , AfMax = AF_MAX , AfUnsupported = AfMax ,
  IPv6 = AF_INET6 , IPv6 = AF_INET6 , Unix = AF_UNIX , Unix = AF_UNIX
}

Public Member Functions

 SocketAddr ()
 SocketAddr (const SocketAddr &value)
 SocketAddr (int family, const void *raw=0)
 SocketAddr (const struct sockaddr *addr, socklen_t len=0)
virtual ~SocketAddr ()
SocketAddroperator= (const SocketAddr &value)
bool operator== (const SocketAddr &other) const
bool operator!= (const SocketAddr &other) const
void clear ()
bool assign (int family)
void assign (const struct sockaddr *addr, socklen_t len=0)
bool assign (const DataBlock &addr)
bool local (const SocketAddr &remote)
bool valid () const
bool null () const
int family () const
const char * familyName () const
unsigned int scopeId () const
bool scopeId (unsigned int val)
const Stringhost () const
const Stringaddr (bool full=false) const
const Stringiface () const
bool iface (const char *name, bool uriUnescape=false)
virtual bool host (const String &name)
int port () const
bool port (int newport)
struct sockaddr * address () const
socklen_t length () const
bool isNullAddr () const
int copyAddr (DataBlock &addr) const
Public Member Functions inherited from GenObject
 GenObject ()
virtual ~GenObject ()
virtual bool alive () const
virtual void destruct ()
virtual const StringtoString () const
virtual const StringtraceId () const
virtual void * getObject (const String &name) const
NamedCountergetObjCounter () const
NamedCountersetObjCounter (NamedCounter *counter)

Static Public Member Functions

static bool supports (int family)
static int family (const String &addr)
static bool stringify (String &buf, struct sockaddr *addr)
static int unStringify (uint8_t *buf, const String &host, int family=Unknown)
static int copyAddr (uint8_t *buf, struct sockaddr *addr)
static unsigned int scopeId (struct sockaddr *addr)
static bool scopeId (struct sockaddr *addr, unsigned int val)
static StringappendAddr (String &buf, const String &addr, int family=Unknown, const String &iface=String::empty())
static StringappendTo (String &buf, const String &addr, int port, int family=Unknown, const String &iface=String::empty())
static String appendTo (const String &addr, int port, int family=Unknown, const String &iface=String::empty())
static bool isNullAddr (const String &addr, int family=Unknown)
static void splitIface (const String &buf, String &addr, String *iface=0)
static void split (const String &buf, String &addr, int &port, bool portPresent=false)
static const char * lookupFamily (int family)
static const Stringipv4NullAddr ()
static const Stringipv6NullAddr ()
static const TokenDictdictFamilyName ()
static const char * ifaceNameExtraEscape ()
static StringescapeIface (String &buf, const char *name)
Static Public Member Functions inherited from GenObject
static void * getObject (const String &name, const GenObject *obj)
static bool getObjCounting ()
static void setObjCounting (bool enable)
static NamedCountergetObjCounter (const String &name, bool create=true)
static ObjListgetObjCounters ()

Protected Member Functions

virtual void stringify ()
virtual void updateAddr (bool full=false) const

Protected Attributes

struct sockaddr * m_address
socklen_t m_length
String m_host
String m_iface
String m_addr
String m_addrFull

Detailed Description

A socket address holder.

Wrapper class to keep a socket address

Member Enumeration Documentation

◆ Family

enum Family

Known address families

Constructor & Destructor Documentation

◆ SocketAddr() [1/4]

SocketAddr ( )
inline

Default constructor of an empty address

Referenced by local(), operator!=(), operator=(), operator==(), SocketAddr(), and unStringify().

◆ SocketAddr() [2/4]

SocketAddr ( const SocketAddr & value)
inline

Copy constructor

Parameters
valueAddress to copy

References address(), assign(), GenObject::GenObject(), length(), and SocketAddr().

◆ SocketAddr() [3/4]

SocketAddr ( int family,
const void * raw = 0 )
explicit

Constructor of a null address

Parameters
familyFamily of the address to create
rawRaw address data

References family().

◆ SocketAddr() [4/4]

SocketAddr ( const struct sockaddr * addr,
socklen_t len = 0 )

Constructor that stores a copy of an address

Parameters
addrPointer to the address to store
lenLength of the stored address, zero to use default

References addr().

◆ ~SocketAddr()

virtual ~SocketAddr ( )
virtual

Destructor that frees and zeroes out everything

Member Function Documentation

◆ addr()

const String & addr ( bool full = false) const
inline

Get the host and port of this address

Parameters
fullRetrieve the full address, interface included
Returns
Address String (host:port or hostiface:port)

References updateAddr().

Referenced by appendAddr(), appendTo(), appendTo(), assign(), assign(), copyAddr(), copyAddr(), family(), isNullAddr(), scopeId(), scopeId(), SocketAddr(), split(), splitIface(), and stringify().

◆ address()

struct sockaddr * address ( ) const
inline

Get the contained socket address

Returns
A pointer to the socket address

Referenced by Socket::bind(), Socket::connect(), Socket::connectAsync(), operator=(), scopeId(), scopeId(), Socket::sendTo(), SocketAddr(), and unStringify().

◆ appendAddr()

String & appendAddr ( String & buf,
const String & addr,
int family = Unknown,
const String & iface = String::empty() )
static

Append an address to a buffer

Parameters
bufDestination buffer
addrAddress to append
familyAddress family, set it to Unknown to detect
ifaceOptional interface name
Returns
Buffer address

References addr(), String::empty(), family(), and iface().

Referenced by appendTo().

◆ appendTo() [1/2]

String appendTo ( const String & addr,
int port,
int family = Unknown,
const String & iface = String::empty() )
inlinestatic

Append an address to a buffer in the form addr:port

Parameters
addrAddress to append
portPort to append
familyAddress family, set it to Unknown to detect
ifaceOptional interface name
Returns
A String with concatenated address and port

References addr(), appendTo(), String::empty(), family(), iface(), and port().

◆ appendTo() [2/2]

String & appendTo ( String & buf,
const String & addr,
int port,
int family = Unknown,
const String & iface = String::empty() )
inlinestatic

Append an address to a buffer in the form addr:port

Parameters
bufDestination buffer
addrAddress to append
portPort to append
familyAddress family, set it to Unknown to detect
ifaceOptional interface name
Returns
Buffer address

References addr(), appendAddr(), String::empty(), family(), iface(), and port().

Referenced by appendTo().

◆ assign() [1/3]

bool assign ( const DataBlock & addr)

Assigns a new address

Parameters
addrPacked binary address to store
Returns
True if the address family is supported

References addr().

◆ assign() [2/3]

void assign ( const struct sockaddr * addr,
socklen_t len = 0 )

Assigns a new address

Parameters
addrPointer to the address to store
lenLength of the stored address, zero to use default

References addr().

◆ assign() [3/3]

bool assign ( int family)

Assigns an empty address of a specific type

Parameters
familyFamily of the address to create
Returns
True if the address family is supported

References family().

Referenced by operator=(), and SocketAddr().

◆ clear()

void clear ( )

Clears up the address, frees the memory

◆ copyAddr() [1/2]

int copyAddr ( DataBlock & addr) const

Copy the host address to a buffer

Parameters
addrBuffer to put the packed address into
Returns
Address family, Unknown on failure

References addr().

Referenced by unStringify().

◆ copyAddr() [2/2]

int copyAddr ( uint8_t * buf,
struct sockaddr * addr )
static

Copy a host address to a buffer

Parameters
bufDestination buffer. It must be large enough to keep the address (4 bytes for IPv4, 16 bytes for IPv6)
addrThe host address
Returns
Address family, Unknown on failure

References addr().

◆ dictFamilyName()

const TokenDict * dictFamilyName ( )
static

Retrieve the family name dictionary

Returns
Pointer to family name dictionary

◆ escapeIface()

String & escapeIface ( String & buf,
const char * name )
inlinestatic

Escape an interface name

Parameters
bufDestination buffer
nameInterface name
Returns
Destination buffer reference

References ifaceNameExtraEscape(), and String::uriEscapeTo().

◆ family() [1/2]

int family ( ) const
inline

Get the family of the stored address

Returns
Address family of the stored address or zero (AF_UNSPEC)

Referenced by appendAddr(), appendTo(), appendTo(), assign(), familyName(), isNullAddr(), isNullAddr(), lookupFamily(), SocketAddr(), supports(), and unStringify().

◆ family() [2/2]

int family ( const String & addr)
static

Retrieve the family of an address

Parameters
addrThe address to check
Returns
Address family

References addr().

◆ familyName()

const char * familyName ( ) const
inline

Retrieve address family name

Returns
Address family name

References family(), and lookupFamily().

◆ host() [1/2]

const String & host ( ) const
inline

Get the host of this address

Returns
Host name as String

Referenced by unStringify().

◆ host() [2/2]

virtual bool host ( const String & name)
virtual

Set the hostname of this address. Guess address family if not initialized

Parameters
nameHost to set
Returns
True if new host set, false if name could not be parsed

◆ iface() [1/2]

const String & iface ( ) const
inline

Get the interface to use for this address

Returns
Interface to use for this address

Referenced by appendAddr(), appendTo(), appendTo(), Socket::bind(), and splitIface().

◆ iface() [2/2]

bool iface ( const char * name,
bool uriUnescape = false )
inline

Set the interface to use for this address

Parameters
nameInterface to use for this address
uriUnescapeSet it to true to URI unescape the value
Returns
True on success, false otherwise

◆ ifaceNameExtraEscape()

const char * ifaceNameExtraEscape ( )
inlinestatic

Retrieve the string for interface name extra URI escape in address

Returns
Interface name extra URI escape in address

Referenced by escapeIface().

◆ ipv4NullAddr()

const String & ipv4NullAddr ( )
static

Retrieve IPv4 null address

Returns
IPv4 null address (0.0.0.0)

◆ ipv6NullAddr()

const String & ipv6NullAddr ( )
static

Retrieve IPv6 null address

Returns
IPv6 null address (::)

◆ isNullAddr() [1/2]

bool isNullAddr ( ) const
inline

Check if this address is empty or null

Returns
True if the address is empty or '0.0.0.0' (IPv4) or '::' IPv6

References family(), and isNullAddr().

Referenced by isNullAddr().

◆ isNullAddr() [2/2]

bool isNullAddr ( const String & addr,
int family = Unknown )
static

Check if an address is empty or null

Parameters
addrAddress to check
familyAddress family, set it to Unknown to detect
Returns
True if the address is empty or '0.0.0.0' (IPv4) or '::' IPv6

References addr(), and family().

◆ length()

socklen_t length ( ) const
inline

Get the length of the address

Returns
Length of the stored address

Referenced by Socket::bind(), Socket::connect(), Socket::connectAsync(), operator=(), Socket::sendTo(), and SocketAddr().

◆ local()

bool local ( const SocketAddr & remote)

Attempt to guess a local address that will be used to reach a remote one

Parameters
remoteRemote address to reach
Returns
True if guessed an address, false if failed

References SocketAddr().

◆ lookupFamily()

const char * lookupFamily ( int family)
inlinestatic

Retrieve address family name

Parameters
familyAddress family to retrieve
Returns
Address family name

References family().

Referenced by familyName().

◆ null()

bool null ( ) const
inline

Check if a null address is held

Returns
True if a null address is held

◆ operator!=()

bool operator!= ( const SocketAddr & other) const
inline

Inequality comparation operator

Parameters
otherAddress to compare to
Returns
True if the addresses are different

References operator==(), and SocketAddr().

◆ operator=()

SocketAddr & operator= ( const SocketAddr & value)
inline

Assignment operator

Parameters
valueAddress to copy

References address(), assign(), length(), and SocketAddr().

◆ operator==()

bool operator== ( const SocketAddr & other) const

Equality comparation operator

Parameters
otherAddress to compare to
Returns
True if the addresses are equal

References SocketAddr().

Referenced by operator!=().

◆ port() [1/2]

int port ( ) const

Get the port of the stored address (if supported)

Returns
Port number of the socket address or zero

Referenced by appendTo(), appendTo(), and split().

◆ port() [2/2]

bool port ( int newport)

Set the port of the stored address (if supported)

Parameters
newportPort number to set in the socket address
Returns
True if new port set, false if not supported

◆ scopeId() [1/4]

unsigned int scopeId ( ) const
inline

Retrieve the sin6_scope_id value of an IPv6 address

Returns
The requested value (it may be 0), 0 if not available

References address(), and scopeId().

Referenced by scopeId(), and scopeId().

◆ scopeId() [2/4]

unsigned int scopeId ( struct sockaddr * addr)
inlinestatic

Retrieve the scope id value of an IPv6 address

Parameters
addrThe address
Returns
The requested value (it may be 0), 0 if not available

References addr().

◆ scopeId() [3/4]

bool scopeId ( struct sockaddr * addr,
unsigned int val )
inlinestatic

Set the scope id value of an IPv6 address

Parameters
addrAddress to set
valValue to set
Returns
True on success, false if not available

References addr().

◆ scopeId() [4/4]

bool scopeId ( unsigned int val)
inline

Set the sin6_scope_id value of an IPv6 address

Parameters
valValue to set
Returns
True on success, false if not available

References address(), and scopeId().

◆ split()

void split ( const String & buf,
String & addr,
int & port,
bool portPresent = false )
static

Split an address into ip/port. Handled formats: addr, addr:port, [addr], [addr]:port It is safe to call this method with the same destination and source string

Parameters
bufSource buffer
addrDestination buffer for address
portDestination port
portPresentSet it to true if the port is always present after the last ':'. This will handle IPv6 addresses without square brackets and port present (e.g. fe80::23:5060 will split into addr=fe80::23 and port=5060)

References addr(), and port().

◆ splitIface()

void splitIface ( const String & buf,
String & addr,
String * iface = 0 )
static

Split an interface from address An interface may be present in addr after a percent char (e.g. fe80::23eth0) It is safe call this method with the same destination and source string

Parameters
bufSource buffer
addrDestination buffer for address
ifaceOptional pointer to be filled with interface name

References addr(), and iface().

◆ stringify() [1/2]

virtual void stringify ( )
protectedvirtual

Convert the host address to a String stored in m_host

◆ stringify() [2/2]

bool stringify ( String & buf,
struct sockaddr * addr )
static

Convert the host address to a String

Parameters
bufDestination buffer
addrSocket address
Returns
True on success, false if address family is not supported

References addr().

◆ supports()

bool supports ( int family)
static

Check if an address family is supported by the library

Parameters
familyFamily of the address to check
Returns
True if the address family is supported

References family().

◆ unStringify()

int unStringify ( uint8_t * buf,
const String & host,
int family = Unknown )
inlinestatic

Put a host address to a buffer

Parameters
bufDestination buffer. It must be large enough to keep the address (4 bytes for IPv4, 16 bytes for IPv6)
hostThe host address
familyAddress family, set it to Unknown to detect
Returns
Address family, Unknown on failure

References address(), copyAddr(), family(), host(), and SocketAddr().

◆ updateAddr()

virtual void updateAddr ( bool full = false) const
protectedvirtual

Store host:port in m_addr Store hostiface:port in m_addrFull

Parameters
fullBuild the full addr

Referenced by addr().

◆ valid()

bool valid ( ) const
inline

Check if a non-null address is held

Returns
True if a valid address is held, false if null

The documentation for this class was generated from the following file: