sockpp
Modern C++ socket library wrapper
Public Member Functions | Static Public Member Functions | Static Public Attributes | List of all members
sockpp::inet_address Class Reference

Class that represents an internet (IPv4) address. More...

#include <inet_address.h>

Inheritance diagram for sockpp::inet_address:
sockpp::sock_address

Public Member Functions

 inet_address ()
 Constructs an empty address. More...
 
 inet_address (in_port_t port)
 Constructs an address for any iface using the specified port. More...
 
 inet_address (uint32_t addr, in_port_t port)
 Constructs an address for the specified host using the specified port. More...
 
 inet_address (const std::string &saddr, in_port_t port)
 Constructs an address using the name of the host and the specified port. More...
 
 inet_address (const sockaddr &addr)
 Constructs the address by copying the specified structure. More...
 
 inet_address (const sock_address &addr)
 Constructs the address by copying the specified structure. More...
 
 inet_address (const sockaddr_in &addr)
 Constructs the address by copying the specified structure. More...
 
 inet_address (const inet_address &addr)
 Constructs the address by copying the specified address. More...
 
bool is_set () const
 Checks if the address is set to some value. More...
 
void create (in_addr_t addr, in_port_t port)
 Creates the socket address using the specified host address and port number. More...
 
void create (const std::string &saddr, in_port_t port)
 Creates the socket address using the specified host name and port number. More...
 
in_addr_t address () const
 Gets the 32-bit internet address. More...
 
uint8_t operator[] (int i) const
 Gets a byte of the 32-bit Internet Address. More...
 
in_port_t port () const
 Gets the port number. More...
 
socklen_t size () const override
 Gets the size of this structure. More...
 
const sockaddr * sockaddr_ptr () const override
 Gets a pointer to this object cast to a sockaddr. More...
 
sockaddr * sockaddr_ptr () override
 Gets a pointer to this object cast to a sockaddr. More...
 
const sockaddr_in * sockaddr_in_ptr () const
 Gets a const pointer to this object cast to a sockaddr_in. More...
 
sockaddr_in * sockaddr_in_ptr ()
 Gets a pointer to this object cast to a sockaddr_in. More...
 
std::string to_string () const
 Gets a printable string for the address. More...
 
- Public Member Functions inherited from sockpp::sock_address
virtual ~sock_address ()
 Virtual destructor.
 
virtual sa_family_t family () const
 Gets the network family of the address. More...
 

Static Public Member Functions

static in_addr_t resolve_name (const std::string &saddr)
 Attempts to resolve the host name into a 32-bit internet address. More...
 

Static Public Attributes

static constexpr sa_family_t ADDRESS_FAMILY = AF_INET
 The address family for this type of address.
 

Detailed Description

Class that represents an internet (IPv4) address.

This inherits from the IP-specific form of a socket address, sockaddr_in.

Constructor & Destructor Documentation

◆ inet_address() [1/8]

sockpp::inet_address::inet_address ( )
inline

Constructs an empty address.

The address is initialized to all zeroes.

◆ inet_address() [2/8]

sockpp::inet_address::inet_address ( in_port_t  port)
inlineexplicit

Constructs an address for any iface using the specified port.

This is a convenient way for a server to specify an address that will bind to all interfaces.

Parameters
portThe port number in native/host byte order.

◆ inet_address() [3/8]

sockpp::inet_address::inet_address ( uint32_t  addr,
in_port_t  port 
)
inline

Constructs an address for the specified host using the specified port.

Parameters
addrThe 32-bit host address in native/host byte order.
portThe port number in native/host byte order.

◆ inet_address() [4/8]

sockpp::inet_address::inet_address ( const std::string &  saddr,
in_port_t  port 
)
inline

Constructs an address using the name of the host and the specified port.

This attempts to resolve the host name to an address.

Parameters
saddrThe name of the host.
portThe port number in native/host byte order.

◆ inet_address() [5/8]

sockpp::inet_address::inet_address ( const sockaddr &  addr)
inline

Constructs the address by copying the specified structure.

Parameters
addrThe other address

◆ inet_address() [6/8]

sockpp::inet_address::inet_address ( const sock_address addr)
inline

Constructs the address by copying the specified structure.

Parameters
addrThe other address

◆ inet_address() [7/8]

sockpp::inet_address::inet_address ( const sockaddr_in &  addr)
inline

Constructs the address by copying the specified structure.

Parameters
addrThe other address

◆ inet_address() [8/8]

sockpp::inet_address::inet_address ( const inet_address addr)
inline

Constructs the address by copying the specified address.

Parameters
addrThe other address

Member Function Documentation

◆ address()

in_addr_t sockpp::inet_address::address ( ) const
inline

Gets the 32-bit internet address.

Returns
The internet address in the local host's byte order.

◆ create() [1/2]

void sockpp::inet_address::create ( in_addr_t  addr,
in_port_t  port 
)

Creates the socket address using the specified host address and port number.

Parameters
addrThe host address.
portThe host port number.

◆ create() [2/2]

void sockpp::inet_address::create ( const std::string &  saddr,
in_port_t  port 
)

Creates the socket address using the specified host name and port number.

Parameters
saddrThe string host name.
portThe port number in native/host byte order.
Exceptions
sys_error,getaddrinfo_error

◆ is_set()

bool sockpp::inet_address::is_set ( ) const

Checks if the address is set to some value.

This doesn't attempt to determine if the address is valid, simply that it's not all zero.

Returns
bool

◆ operator[]()

uint8_t sockpp::inet_address::operator[] ( int  i) const
inline

Gets a byte of the 32-bit Internet Address.

Parameters
iThe byte to read (0-3)
Returns
The specified byte in the 32-bit Internet Address

◆ port()

in_port_t sockpp::inet_address::port ( ) const
inline

Gets the port number.

Returns
The port number in native/host byte order.

◆ resolve_name()

static in_addr_t sockpp::inet_address::resolve_name ( const std::string &  saddr)
static

Attempts to resolve the host name into a 32-bit internet address.

Parameters
saddrThe string host name.
Returns
The internet address in network byte order.
Exceptions
sys_error,getaddrinfo_error

◆ size()

socklen_t sockpp::inet_address::size ( ) const
inlineoverridevirtual

Gets the size of this structure.

This is equivalent to sizeof(this) but more convenient in some places.

Returns
The size of this structure.

Implements sockpp::sock_address.

◆ sockaddr_in_ptr() [1/2]

const sockaddr_in* sockpp::inet_address::sockaddr_in_ptr ( ) const
inline

Gets a const pointer to this object cast to a sockaddr_in.

Returns
const sockaddr_in pointer to this object.

◆ sockaddr_in_ptr() [2/2]

sockaddr_in* sockpp::inet_address::sockaddr_in_ptr ( )
inline

Gets a pointer to this object cast to a sockaddr_in.

Returns
sockaddr_in pointer to this object.

◆ sockaddr_ptr() [1/2]

const sockaddr* sockpp::inet_address::sockaddr_ptr ( ) const
inlineoverridevirtual

Gets a pointer to this object cast to a sockaddr.

Returns
A pointer to this object cast to a sockaddr.

Implements sockpp::sock_address.

◆ sockaddr_ptr() [2/2]

sockaddr* sockpp::inet_address::sockaddr_ptr ( )
inlineoverridevirtual

Gets a pointer to this object cast to a sockaddr.

Returns
A pointer to this object cast to a sockaddr.

Implements sockpp::sock_address.

◆ to_string()

std::string sockpp::inet_address::to_string ( ) const

Gets a printable string for the address.

This gets the simple dot notation of the address as returned from inet_ntop(). It does not attempt a host lookup.

Returns
A string representation of the address in the form 'address:port'

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