17#ifndef _FGE_C_IPADDRESS_HPP_INCLUDED_
18#define _FGE_C_IPADDRESS_HPP_INCLUDED_
20#include "FastEngine/fge_extern.hpp"
21#include "FastEngine/extra/extra_function.hpp"
42struct std::hash<fge::net::IpAddress>;
59 using Ipv4Data = uint32_t;
60 using Ipv6Data = std::array<uint16_t, 8>;
61 using Data = std::variant<Ipv4Data, Ipv6Data>;
63 enum class Types : uint8_t
70 enum class CheckHostname
88 IpAddress(std::
string const& address, CheckHostname check = CheckHostname::Yes);
89 IpAddress(
char const* address, CheckHostname check = CheckHostname::Yes);
98 IpAddress(uint8_t byte3, uint8_t byte2, uint8_t byte1, uint8_t byte0) noexcept;
106 IpAddress(std::initializer_list<uint16_t> words) noexcept;
132 bool set(std::
string const& address, CheckHostname check = CheckHostname::Yes);
133 bool set(
char const* address, CheckHostname check = CheckHostname::Yes);
143 bool set(uint8_t byte3, uint8_t byte2, uint8_t byte1, uint8_t byte0);
152 bool set(std::initializer_list<uint16_t> words);
161 bool set(Ipv6Data const& data);
168 bool set(uint8_t const bytes[16]);
175 bool set(Ipv4Data address);
198 [[nodiscard]]
bool operator==(
IpAddress const& r) const;
205 [[nodiscard]] std::optional<std::
string>
toString() const;
220 [[nodiscard]] Types getType() const;
244 [[nodiscard]]
bool isIpv4MappedIpv6() const;
268 static
IpAddress Loopback(Types addressType);
273 std::variant<std::monostate, Ipv4Data, Ipv6Data> g_address;
284struct std::hash<fge::net::IpAddress>
288 if (std::holds_alternative<std::monostate>(r.g_address))
290 return std::hash<std::monostate>{}(std::monostate{});
292 if (std::holds_alternative<fge::net::IpAddress::Ipv4Data>(r.g_address))
294 return std::hash<fge::net::IpAddress::Ipv4Data>{}(std::get<fge::net::IpAddress::Ipv4Data>(r.g_address));
296 auto const& array = std::get<fge::net::IpAddress::Ipv6Data>(r.g_address);
297 return fge::Hash(array.data(), array.size() * 2);
A class to represent an IP address.
Definition C_ipAddress.hpp:57
static IpAddress const Ipv4Broadcast
Represent the broadcast ipv4 address "255.255.255.255".
Definition C_ipAddress.hpp:270
std::optional< IpAddress > mapToIpv6() const
Map an ipv4 address to an ipv6 address.
static std::optional< std::string > getHostName()
Get the standard hostname for the local computer.
static IpAddress const Ipv4Any
Represent an unspecified ipv4 address "0.0.0.0".
Definition C_ipAddress.hpp:262
static IpAddress const Ipv6Loopback
Represent the local host ipv6 address "::1".
Definition C_ipAddress.hpp:267
std::optional< Data > getHostByteOrder() const
Get the ip address in host byte order.
std::optional< IpAddress > mapToIpv4() const
Map an ipv6 (ipv4-mapped) address back to an ipv4 address.
std::optional< std::string > toString() const
Get the ip address in a string format.
bool set(std::string const &address, CheckHostname check=CheckHostname::Yes)
Build an address from a string.
IpAddress() noexcept
Build a default invalid IP address.
static std::vector< IpAddress > getLocalAddresses(Types type=Types::None)
Get a list of local IpAddress of the local computer.
std::optional< Data > getNetworkByteOrder() const
Get the ip address in a network byte order.
static IpAddress const Ipv6Any
Represent an unspecified ipv6 address "::".
Definition C_ipAddress.hpp:263
static IpAddress const None
Represent an invalid address.
Definition C_ipAddress.hpp:260
static IpAddress const Ipv4Loopback
Represent the local host ipv4 address "127.0.0.1".
Definition C_ipAddress.hpp:266
bool setNetworkByteOrdered(Ipv4Data address)
Build an ipv4 address from a network byte order integer.
Definition C_packet.hpp:52