FastEngine 0.9.4
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
fge::net::IpAddress Class Reference

A class to represent an IP address. More...

#include <C_ipAddress.hpp>

Public Types

enum class  Types : uint8_t { None , Ipv4 , Ipv6 }
enum class  CheckHostname { No , Yes }
using Ipv4Data = uint32_t
using Ipv6Data = std::array<uint16_t, 8>
using Data = std::variant<Ipv4Data, Ipv6Data>

Public Member Functions

 IpAddress () noexcept
 Build a default invalid IP address.
 IpAddress (std::string const &address, CheckHostname check=CheckHostname::Yes)
 Build an address from a string.
 IpAddress (char const *address, CheckHostname check=CheckHostname::Yes)
 IpAddress (uint8_t byte3, uint8_t byte2, uint8_t byte1, uint8_t byte0) noexcept
 Build an ipv4 address from 4 bytes.
 IpAddress (std::initializer_list< uint16_t > words) noexcept
 Build an ipv4 address from 8 words in host byte order.
 IpAddress (Ipv6Data const &data) noexcept
 Build an ipv4 address from Ipv6Data in host byte order.
 IpAddress (Ipv4Data address) noexcept
 Build an ipv4 address from a host byte order integer.
bool set (std::string const &address, CheckHostname check=CheckHostname::Yes)
 Build an address from a string.
bool set (char const *address, CheckHostname check=CheckHostname::Yes)
bool set (uint8_t byte3, uint8_t byte2, uint8_t byte1, uint8_t byte0)
 Build an ipv4 address from 4 bytes.
bool set (std::initializer_list< uint16_t > words)
 Build an ipv6 address from 8 words in host byte order.
bool set (Ipv6Data const &data)
 Build an ipv6 address from Ipv6Data in host byte order.
bool set (uint8_t const bytes[16])
 Build an ipv6 address from a host byte order array of bytes.
bool set (Ipv4Data address)
 Build an ipv4 address from a host byte order integer.
bool setNetworkByteOrdered (Ipv4Data address)
 Build an ipv4 address from a network byte order integer.
bool setNetworkByteOrdered (Ipv6Data const &data)
 Build an ipv6 address from a network byte order data.
bool setNetworkByteOrdered (uint8_t const bytes[16])
 Build an ipv6 address from a network byte order array of bytes.
bool operator== (IpAddress const &r) const
std::optional< std::string > toString () const
 Get the ip address in a string format.
std::optional< Data > getNetworkByteOrder () const
 Get the ip address in a network byte order.
std::optional< Data > getHostByteOrder () const
 Get the ip address in host byte order.
Types getType () const
std::optional< IpAddressmapToIpv6 () const
 Map an ipv4 address to an ipv6 address.
std::optional< IpAddressmapToIpv4 () const
 Map an ipv6 (ipv4-mapped) address back to an ipv4 address.
bool isIpv4MappedIpv6 () const

Static Public Member Functions

static std::optional< std::string > getHostName ()
 Get the standard hostname for the local computer.
static std::vector< IpAddressgetLocalAddresses (Types type=Types::None)
 Get a list of local IpAddress of the local computer.
static IpAddress Any (Types addressType)
static IpAddress Loopback (Types addressType)

Static Public Attributes

static IpAddress const None
 Represent an invalid address.
static IpAddress const Ipv4Any
 Represent an unspecified ipv4 address "0.0.0.0".
static IpAddress const Ipv6Any
 Represent an unspecified ipv6 address "::".
static IpAddress const Ipv4Loopback
 Represent the local host ipv4 address "127.0.0.1".
static IpAddress const Ipv6Loopback
 Represent the local host ipv6 address "::1".
static IpAddress const Ipv4Broadcast
 Represent the broadcast ipv4 address "255.255.255.255".

Friends

struct std::hash< IpAddress >

Detailed Description

A class to represent an IP address.

This class will automatically handle the byte order of the IP address.

Constructor & Destructor Documentation

◆ IpAddress() [1/6]

fge::net::IpAddress::IpAddress ( )
noexcept

Build a default invalid IP address.

◆ IpAddress() [2/6]

fge::net::IpAddress::IpAddress ( std::string const & address,
CheckHostname check = CheckHostname::Yes )

Build an address from a string.

The string can be in the ipv4 form XXX.XXX.XXX.XXX, ipv6, or a hostname.

Parameters
addressA string representing the address
checkControl if the method should check if the address is a hostname or not

◆ IpAddress() [3/6]

fge::net::IpAddress::IpAddress ( uint8_t byte3,
uint8_t byte2,
uint8_t byte1,
uint8_t byte0 )
noexcept

Build an ipv4 address from 4 bytes.

Parameters
byte3The most significant byte
byte2The second most significant byte
byte1The second least significant byte
byte0The least significant byte

◆ IpAddress() [4/6]

fge::net::IpAddress::IpAddress ( std::initializer_list< uint16_t > words)
noexcept

Build an ipv4 address from 8 words in host byte order.

Warning
the first word is the most significant word.
Parameters
wordsThe 8 words representing the ipv6 address in host byte order

◆ IpAddress() [5/6]

fge::net::IpAddress::IpAddress ( Ipv6Data const & data)
noexcept

Build an ipv4 address from Ipv6Data in host byte order.

Warning
the first word is the least significant word.
Parameters
dataThe Ipv6Data representing the address in host byte order

◆ IpAddress() [6/6]

fge::net::IpAddress::IpAddress ( Ipv4Data address)
noexcept

Build an ipv4 address from a host byte order integer.

Parameters
addressThe host byte order integer

Member Function Documentation

◆ getHostByteOrder()

std::optional< Data > fge::net::IpAddress::getHostByteOrder ( ) const
nodiscard

Get the ip address in host byte order.

Returns
The host byte order integer

◆ getHostName()

std::optional< std::string > fge::net::IpAddress::getHostName ( )
staticnodiscard

Get the standard hostname for the local computer.

Returns
The hostname

◆ getLocalAddresses()

std::vector< IpAddress > fge::net::IpAddress::getLocalAddresses ( Types type = Types::None)
staticnodiscard

Get a list of local IpAddress of the local computer.

Parameters
typeThe type of address to get, if None, all addresses will be returned
Returns
A vector containing the local IP addresses or an empty vector if an error occurred

◆ getNetworkByteOrder()

std::optional< Data > fge::net::IpAddress::getNetworkByteOrder ( ) const
nodiscard

Get the ip address in a network byte order.

Returns
The network byte order integer

◆ mapToIpv4()

std::optional< IpAddress > fge::net::IpAddress::mapToIpv4 ( ) const
nodiscard

Map an ipv6 (ipv4-mapped) address back to an ipv4 address.

Returns
The mapped ipv4 address or an empty optional if the address is not an ipv6 address

◆ mapToIpv6()

std::optional< IpAddress > fge::net::IpAddress::mapToIpv6 ( ) const
nodiscard

Map an ipv4 address to an ipv6 address.

When using a socket that can communicate with both ipv4 and ipv6, you have to map the ipv4 address to an ipv6 address.

FROM MSDN:

Dual-stack sockets always require IPv6 addresses. The ability to interact with an IPv4 address requires the use of the IPv4-mapped IPv6 address format. Any IPv4 addresses must be represented in the IPv4-mapped IPv6 address format which enables an IPv6 only application to communicate with an IPv4 node. The IPv4-mapped IPv6 address format allows the IPv4 address of an IPv4 node to be represented as an IPv6 address. The IPv4 address is encoded into the low-order 32 bits of the IPv6 address, and the high-order 96 bits hold the fixed prefix 0:0:0:0:0:FFFF. The IPv4-mapped IPv6 address format is specified in RFC 4291.

Returns
The mapped ipv6 address or an empty optional if the address is not an ipv4 address

◆ set() [1/6]

bool fge::net::IpAddress::set ( Ipv4Data address)

Build an ipv4 address from a host byte order integer.

Parameters
addressThe host byte order integer
Returns
true if the address is valid, false otherwise

◆ set() [2/6]

bool fge::net::IpAddress::set ( Ipv6Data const & data)

Build an ipv6 address from Ipv6Data in host byte order.

Warning
Contrary to the initializer list, the first word is the least significant word.
Parameters
dataThe Ipv6Data representing the ipv6 address in host byte order
Returns
true if the address is valid, false otherwise

◆ set() [3/6]

bool fge::net::IpAddress::set ( std::initializer_list< uint16_t > words)

Build an ipv6 address from 8 words in host byte order.

Manualy build an ipv6 address with a initializer list.

Warning
the first word is the most significant word.
Parameters
wordsThe 8 words representing the ipv6 address in host byte order

◆ set() [4/6]

bool fge::net::IpAddress::set ( std::string const & address,
CheckHostname check = CheckHostname::Yes )

Build an address from a string.

The string can be in the ipv4 form XXX.XXX.XXX.XXX, ipv6, or a hostname.

Parameters
addressA string representing the address
checkControl if the method should check if the address is a hostname or not
Returns
true if the address is valid, false otherwise

◆ set() [5/6]

bool fge::net::IpAddress::set ( uint8_t byte3,
uint8_t byte2,
uint8_t byte1,
uint8_t byte0 )

Build an ipv4 address from 4 bytes.

Parameters
byte3The most significant byte
byte2The second most significant byte
byte1The second least significant byte
byte0The least significant byte
Returns
true if the address is valid, false otherwise

◆ set() [6/6]

bool fge::net::IpAddress::set ( uint8_t const bytes[16])

Build an ipv6 address from a host byte order array of bytes.

Parameters
bytesThe host byte order array
Returns
true if the address is valid, false otherwise

◆ setNetworkByteOrdered() [1/3]

bool fge::net::IpAddress::setNetworkByteOrdered ( Ipv4Data address)

Build an ipv4 address from a network byte order integer.

Parameters
addressThe network byte order integer
Returns
true if the address is valid, false otherwise

◆ setNetworkByteOrdered() [2/3]

bool fge::net::IpAddress::setNetworkByteOrdered ( Ipv6Data const & data)

Build an ipv6 address from a network byte order data.

Parameters
dataThe network byte order data
Returns
true if the address is valid, false otherwise

◆ setNetworkByteOrdered() [3/3]

bool fge::net::IpAddress::setNetworkByteOrdered ( uint8_t const bytes[16])

Build an ipv6 address from a network byte order array of bytes.

Parameters
bytesThe network byte order array
Returns
true if the address is valid, false otherwise

◆ toString()

std::optional< std::string > fge::net::IpAddress::toString ( ) const
nodiscard

Get the ip address in a string format.

Returns
A string representing the ip address

Member Data Documentation

◆ Ipv4Any

IpAddress const fge::net::IpAddress::Ipv4Any
static

Represent an unspecified ipv4 address "0.0.0.0".

◆ Ipv4Broadcast

IpAddress const fge::net::IpAddress::Ipv4Broadcast
static

Represent the broadcast ipv4 address "255.255.255.255".

◆ Ipv4Loopback

IpAddress const fge::net::IpAddress::Ipv4Loopback
static

Represent the local host ipv4 address "127.0.0.1".

◆ Ipv6Any

IpAddress const fge::net::IpAddress::Ipv6Any
static

Represent an unspecified ipv6 address "::".

◆ Ipv6Loopback

IpAddress const fge::net::IpAddress::Ipv6Loopback
static

Represent the local host ipv6 address "::1".

◆ None

IpAddress const fge::net::IpAddress::None
static

Represent an invalid address.


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