Yate
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
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 () const
 
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)
 
static StringappendTo (String &buf, const String &addr, int port, int family=Unknown)
 
static String appendTo (const String &addr, int port, int family=Unknown)
 
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 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 () const
 

Protected Attributes

struct sockaddr * m_address
 
socklen_t m_length
 
String m_host
 
String m_addr
 

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

◆ SocketAddr() [2/4]

SocketAddr ( const SocketAddr value)
inline

Copy constructor

Parameters
valueAddress to copy

References address(), and length().

◆ 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

◆ 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

◆ ~SocketAddr()

virtual ~SocketAddr ( )
virtual

Destructor that frees and zeroes out everything

Member Function Documentation

◆ addr()

const String & addr ( ) const
inline

Get the host and port of this address

Returns
Address String (host:port)

◆ 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=(), Socket::sendTo(), SocketAddr(), and unStringify().

◆ appendAddr()

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

Append an address to a buffer

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

◆ appendTo() [1/2]

static String appendTo ( const String addr,
int  port,
int  family = Unknown 
)
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
Returns
A String with concatenated address and port

◆ appendTo() [2/2]

static String & appendTo ( String buf,
const String addr,
int  port,
int  family = Unknown 
)
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
Returns
Buffer address

◆ 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

◆ 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

◆ 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

◆ 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

◆ copyAddr() [2/2]

static 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

◆ dictFamilyName()

static const TokenDict * dictFamilyName ( )
static

Retrieve the family name dictionary

Returns
Pointer to family name dictionary

◆ 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)

◆ family() [2/2]

static int family ( const String addr)
static

Retrieve the family of an address

Parameters
addrThe address to check
Returns
Address family

◆ familyName()

const char * familyName ( ) const
inline

Retrieve address family name

Returns
Address family name

◆ 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

◆ ipv4NullAddr()

static const String & ipv4NullAddr ( )
static

Retrieve IPv4 null address

Returns
IPv4 null address (0.0.0.0)

◆ ipv6NullAddr()

static 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

◆ isNullAddr() [2/2]

static 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

◆ 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

◆ lookupFamily()

static const char * lookupFamily ( int  family)
inlinestatic

Retrieve address family name

Parameters
familyAddress family to retrieve
Returns
Address family name

◆ 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

◆ operator=()

SocketAddr & operator= ( const SocketAddr value)
inline

Assignment operator

Parameters
valueAddress to copy

References address(), and length().

◆ operator==()

bool operator== ( const SocketAddr other) const

Equality comparation operator

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

◆ port() [1/2]

int port ( ) const

Get the port of the stored address (if supported)

Returns
Port number of the socket address or zero

◆ 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

◆ scopeId() [2/4]

static 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

◆ scopeId() [3/4]

static 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

◆ 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

◆ split()

static 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 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)

◆ splitIface()

static 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

◆ stringify() [1/2]

virtual void stringify ( )
protectedvirtual

Convert the host address to a String stored in m_host

◆ stringify() [2/2]

static 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

◆ supports()

static 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

◆ unStringify()

static 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(), and host().

◆ updateAddr()

virtual void updateAddr ( ) const
protectedvirtual

Store host:port in m_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: