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

enum Family

Known address families

Constructor & Destructor Documentation

SocketAddr ( )
inline

Default constructor of an empty address

SocketAddr ( const SocketAddr value)
inline

Copy constructor

Parameters
valueAddress to copy

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

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

Constructor of a null address

Parameters
familyFamily of the address to create
rawRaw address data
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
virtual ~SocketAddr ( )
virtual

Destructor that frees and zeroes out everything

Member Function Documentation

const String& addr ( ) const
inline

Get the host and port of this address

Returns
Address String (host:port)

References SocketAddr::updateAddr().

struct sockaddr* address ( ) const
inline
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

Referenced by SocketAddr::appendTo().

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

References SocketAddr::appendAddr(), SocketAddr::family(), and SocketAddr::port().

Referenced by SocketAddr::appendTo().

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

References SocketAddr::appendTo(), and SocketAddr::family().

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

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

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
bool assign ( const DataBlock addr)

Assigns a new address

Parameters
addrPacked binary address to store
Returns
True if the address family is supported
void clear ( )

Clears up the address, frees the memory

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

Referenced by SocketAddr::unStringify().

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
static const TokenDict* dictFamilyName ( )
static

Retrieve the family name dictionary

Returns
Pointer to family name dictionary
int family ( ) const
inline

Get the family of the stored address

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

Referenced by SocketAddr::appendTo(), SocketAddr::familyName(), SocketAddr::isNullAddr(), and SocketAddr::unStringify().

static int family ( const String addr)
static

Retrieve the family of an address

Parameters
addrThe address to check
Returns
Address family
const char* familyName ( )
inline

Retrieve address family name

Returns
Address family name

References SocketAddr::family(), and SocketAddr::lookupFamily().

const String& host ( ) const
inline

Get the host of this address

Returns
Host name as String

Referenced by SocketAddr::unStringify().

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
static const String& ipv4NullAddr ( )
static

Retrieve IPv4 null address

Returns
IPv4 null address (0.0.0.0)
static const String& ipv6NullAddr ( )
static

Retrieve IPv6 null address

Returns
IPv6 null address (::)
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 SocketAddr::family().

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

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
static const char* lookupFamily ( int  family)
inlinestatic

Retrieve address family name

Parameters
familyAddress family to retrieve
Returns
Address family name

References TelEngine::lookup().

Referenced by SocketAddr::familyName().

bool null ( ) const
inline

Check if a null address is held

Returns
True if a null address is held
bool operator!= ( const SocketAddr other) const
inline

Inequality comparation operator

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

References SocketAddr::operator==().

SocketAddr& operator= ( const SocketAddr value)
inline

Assignment operator

Parameters
valueAddress to copy

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

bool operator== ( const SocketAddr other) const

Equality comparation operator

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

Referenced by SocketAddr::operator!=().

int port ( ) const

Get the port of the stored address (if supported)

Returns
Port number of the socket address or zero

Referenced by SocketAddr::appendTo().

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
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 SocketAddr::address().

Referenced by SocketAddr::scopeId().

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 SocketAddr::address(), and SocketAddr::scopeId().

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
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
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)
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
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
virtual void stringify ( )
protectedvirtual

Convert the host address to a String stored in m_host

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
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 SocketAddr::address(), SocketAddr::copyAddr(), SocketAddr::family(), and SocketAddr::host().

virtual void updateAddr ( ) const
protectedvirtual

Store host:port in m_addr

Referenced by SocketAddr::addr().

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: