Class IPv4Address#
Defined in File IPv4Address.h
Class Documentation#
-
class IPv4Address#
TRAP::Network::IPv4Address is a utility class for manipulating network addresses. It provides a set of implicit constructors and conversion functions to easily build or transform an IPv4 address from/to various representations.
Public Functions
-
constexpr IPv4Address() noexcept = default#
This constructor creates an empty (invalid) address.
-
explicit IPv4Address(std::string_view address)#
Construct the address from a string.
Here address can be either a decimal address (ex: “192.168.1.180”) or a network name (ex: “localhost”).
- Parameters:
address – IPv4 address or network name.
-
constexpr IPv4Address(u8 byte0, u8 byte1, u8 byte2, u8 byte3)#
Construct the address from 4 bytes.
Calling IPv4Address(a, b, c, d) is equivalent to calling IPv4Address(“a.b.c.d”), but safer as it doesn’t have to parse a string to get the address components.
- Parameters:
byte0 – First byte of the address.
byte1 – Second byte of the address.
byte2 – Third byte of the address.
byte3 – Fourth byte of the address.
-
explicit constexpr IPv4Address(u32 address)#
Construct the address from a 32 bit integer.
This constructor uses the internal representation of the address directly. It should be used for optimization purposes, and only if you get that representation from IPv4Address::ToInteger().
- Parameters:
address – 4 Bytes of the address packed into a 32 bit integer.
-
constexpr IPv4Address(const IPv4Address&) = default#
Copy constructor.
-
constexpr IPv4Address &operator=(const IPv4Address&) = default#
Copy assignment operator.
-
constexpr IPv4Address(IPv4Address&&) noexcept = default#
Move constructor.
-
constexpr IPv4Address &operator=(IPv4Address&&) noexcept = default#
Move assignment operator.
-
constexpr ~IPv4Address() = default#
Destructor.
-
std::string ToString() const#
Get a string representation of the address.
The returned string is the decimal representation of the IPv4 address (like “192.168.1.180”), even if it was constructed from a host name.
- Returns:
String representation of the address.
-
constexpr u32 ToInteger() const#
Get an integer representation of the address.
The returned number is in the internal representation of the address, and should be used for optimization purposes only (like sending the address through a socket). The integer produced by this function can then be converted back to a TRAP::Network::IPv4Address with the proper constructor.
- Returns:
32 bit unsigned integer representation of the address.
Public Static Functions
-
static IPv4Address GetLocalAddress()#
Get the computer’s local address.
The local address is the address of the computer from the LAN point of view, i.e. something like 192.168.1.180. It is meaningful only for communications over the local network. Unlike GetPublicAddress, this function is fast and may be used safely anywhere.
- Returns:
Local IPv4 address of the computer.
-
static IPv4Address GetPublicAddress(Utils::TimeStep timeout = Utils::TimeStep(0.0f))#
Get the computer’s public address.
The public address is the address of the computer from the internet point of view, i.e. something like 89.54.1.169. It is necessary for communications over the world wide web. The only way to get a public address is to ask it to a distant website; as a consequence, this function depends on both your network connection and the server, and may be very slow. You should use it as few as possible. Because this function depends on the network connection and on a distant server, you may use a time limit if you don’t want your program to be possibly stuck waiting in case there is a problem; this limit is deactivated by default.
- Parameters:
timeout – Maximum time to wait.
- Returns:
Public IP address of the computer.
Public Static Attributes
-
static const IPv4Address None#
-
static const IPv4Address Any#
-
static const IPv4Address LocalHost#
-
static const IPv4Address Broadcast#
Friends
-
inline friend constexpr auto operator<=>(const IPv4Address &lhs, const IPv4Address &rhs)#
-
inline friend constexpr auto operator==(const IPv4Address &lhs, const IPv4Address &rhs)#
-
constexpr IPv4Address() noexcept = default#