LIBWIRE
Next-generation C++17 networking library.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Public Member Functions | Public Attributes | Static Public Attributes | List of all members
libwire::address Struct Reference

IPv4/IPv6 address structure. More...

#include <address.hpp>

Public Member Functions

 address ()=default
 Uninitialized address value, is_invalid() = true. More...
 
 address (const memory_view &) noexcept
 Construct IP address from raw bytes in network byte order (big endian). More...
 
 address (uint8_t, uint8_t, uint8_t, uint8_t) noexcept
 Construct IPv4 address from 4 bytes in network byte order (big endian). More...
 
 address (uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t) noexcept
 Construct IPv6 address from 16 bytes in network byte order (big endian). More...
 
 address (const std::string &text_ip, ip assume_ipver=ip(0)) noexcept(!LIBWIRE_EXCEPTIONS_ENABLED_BOOL)
 Parse IP address from string. More...
 
bool is_invalid () const noexcept
 
bool operator!= (const address &) const noexcept
 
bool operator== (const address &) const noexcept
 
std::string to_string () const noexcept
 Convert address object to string representation. More...
 

Public Attributes

std::array< uint8_t, 16 > parts {}
 
ip version = ip(0)
 

Static Public Attributes

static const address invalid
 

Detailed Description

IPv4/IPv6 address structure.

As you expect it can store both IPv6 and IPv4 addresses to make library interface IP-version agnostic as much as possible.

Address stored in as set of unsigned 8-bit numbers in parts array. If IP version is IPv4 then only first 4 elements is used to store 4 corresponding parts of address. For 127.0.0.1 parts array will contain {127, 0, 0, 1, 0, ...}. Remaining bytes will be filled with zeros. If IPv6 address used then all parts will be used as you expect.

If you have some hardcoded address then it's better to specify it using initializer list e.g. {127, 0, 0, 1}. This will avoid runtime overhead of string parsing.

Examples:
dns_query.cpp.

Definition at line 63 of file address.hpp.

Constructor & Destructor Documentation

libwire::address::address ( )
default

Uninitialized address value, is_invalid() = true.

Note
Any interaction with invalid address object other than is_invalid and comparsions have undefined behavior, but invalid object can be used as a "null" value, since it will not be equal to any valid value.
libwire::address::address ( const memory_view )
noexcept

Construct IP address from raw bytes in network byte order (big endian).

Size of view must be either 4 or 16 bytes.

libwire::address::address ( uint8_t  ,
uint8_t  ,
uint8_t  ,
uint8_t   
)
noexcept

Construct IPv4 address from 4 bytes in network byte order (big endian).

Thus 127.0.0.1 => {127, 0, 0, 1}

libwire::address::address ( uint8_t  ,
uint8_t  ,
uint8_t  ,
uint8_t  ,
uint8_t  ,
uint8_t  ,
uint8_t  ,
uint8_t  ,
uint8_t  ,
uint8_t  ,
uint8_t  ,
uint8_t  ,
uint8_t  ,
uint8_t  ,
uint8_t  ,
uint8_t   
)
noexcept

Construct IPv6 address from 16 bytes in network byte order (big endian).

Thus ::1 => {0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,1}

Todo:
Sadly, there is no good way to short-cut it, if you can come up with a solution without templates hell - PR's are welcome!
libwire::address::address ( const std::string &  text_ip,
ip  assume_ipver = ip(0) 
)
noexcept

Parse IP address from string.

supports both IPv4 and IPv6, use version member variable to determine address version.

If exceptions are enabled - will throw std::invalid_argument, otherwise constructed address will have is_invalid() = true.

assume_ipver can be used to accept only one IP version (only IPv4 addresses or only IPv6).

Warning
Multiple consecutive zeros are NOT allowed "000.0.11.11" will throw.

Member Function Documentation

bool libwire::address::is_invalid ( ) const
noexcept
bool libwire::address::operator!= ( const address ) const
noexcept
bool libwire::address::operator== ( const address ) const
noexcept
std::string libwire::address::to_string ( ) const
noexcept

Convert address object to string representation.

For IPv6 it gives correctly compacted version.

Examples:
dns_query.cpp.

Member Data Documentation

const address libwire::address::invalid
static

Definition at line 125 of file address.hpp.

std::array<uint8_t, 16> libwire::address::parts {}

Definition at line 123 of file address.hpp.

ip libwire::address::version = ip(0)

Definition at line 122 of file address.hpp.