Yate: SocketAddr Class Reference

Yate

- Main Page
- Namespaces
- Classes
- Files

- Class List
- Class Hierarchy
- Class Members

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:

## 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 ()
SocketAddr & | operator= (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 String & | host () const
const String & | addr () 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 String & | toString () const
virtual const String & | traceId () const
virtual void * | getObject (const String &name) const
NamedCounter * | getObjCounter () const
NamedCounter * | setObjCounter (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 String & | appendAddr (String &buf, const String &addr, int family=Unknown)
static String & | appendTo (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 String & | ipv4NullAddr ()
static const String & | ipv6NullAddr ()
static const TokenDict * | dictFamilyName ()
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 NamedCounter * | getObjCounter (const String &name, bool create=true)
static ObjList & | getObjCounters ()

## 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

  value | Address to copy

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

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

Constructor of a null address

Parameters

  family | Family of the address to create
  raw | Raw address data

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

Constructor that stores a copy of an address

Parameters

  addr | Pointer to the address to store
  len | Length 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

Get the contained socket address

Returns
  A pointer to the socket address

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

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

Append an address to a buffer

Parameters

  buf | Destination buffer
  addr | Address to append
  family | Address 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

  buf | Destination buffer
  addr | Address to append
  port | Port to append
  family | Address 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

  addr | Address to append
  port | Port to append
  family | Address 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

  family | Family 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

  addr | Pointer to the address to store
  len | Length of the stored address, zero to use default

bool assign | ( | const DataBlock & | addr | ) |

Assigns a new address

Parameters

  addr | Packed 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

  addr | Buffer 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

  buf | Destination buffer. It must be large enough to keep the address (4 bytes for IPv4, 16 bytes for IPv6)
  addr | The 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

  addr | The address to check

Returns
  Address family

const char* familyName | ( |  | ) | const
| 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

  name | Host 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

  addr | Address to check
  family | Address 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

  remote | Remote address to reach

Returns
  True if guessed an address, false if failed

static const char* lookupFamily | ( | int | family | ) |
| inlinestatic

Retrieve address family name

Parameters

  family | Address 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

  other | Address to compare to

Returns
  True if the addresses are different

References SocketAddr::operator==().

SocketAddr& operator= | ( | const SocketAddr & | value | ) |
| inline

Assignment operator

Parameters

  value | Address to copy

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

bool operator== | ( | const SocketAddr & | other | ) | const

Equality comparation operator

Parameters

  other | Address 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

  newport | Port 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

  val | Value 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

  addr | The 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

  addr | Address to set
  val | Value 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

  buf | Source buffer
  addr | Destination buffer for address
  port | Destination port
  portPresent | Set 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

  buf | Source buffer
  addr | Destination buffer for address
  iface | Optional pointer to be filled with interface name

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

Convert the host address to a String

Parameters

  buf | Destination buffer
  addr | Socket 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

  family | Family 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

  buf | Destination buffer. It must be large enough to keep the address (4 bytes for IPv4, 16 bytes for IPv6)
  host | The host address
  family | Address 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:

- yateclass.h