Loading...
Searching...
No Matches
IpAddress.hpp
Go to the documentation of this file.
1
2//
3// SFML - Simple and Fast Multimedia Library
4// Copyright (C) 2007-2026 Laurent Gomila (laurent@sfml-dev.org)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25#pragma once
26
28// Headers
31
32#include <SFML/System/Time.hpp>
33
34#include <array>
35#include <iosfwd>
36#include <optional>
37#include <string>
38#include <string_view>
39#include <variant>
40
41#include <cstdint>
42
43
44namespace sf
45{
51{
52public:
57 enum class Type
58 {
59 IpV4,
60 IpV6
61 };
62
79 [[deprecated("Use sf::Dns::resolve() instead")]] [[nodiscard]] static std::optional<IpAddress> resolve(
80 std::string_view address);
81
95 IpAddress(std::uint8_t byte0, std::uint8_t byte1, std::uint8_t byte2, std::uint8_t byte3);
96
110 explicit IpAddress(std::uint32_t address);
111
118 IpAddress(std::array<std::uint8_t, 16> bytes);
119
148 [[nodiscard]] static std::optional<IpAddress> fromString(std::string_view address);
149
162 [[nodiscard]] std::string toString() const;
163
181 [[nodiscard]] std::uint32_t toInteger() const;
182
200 [[nodiscard]] std::array<std::uint8_t, 16> toBytes() const;
201
208 [[nodiscard]] Type getType() const;
209
218 [[nodiscard]] bool isV4() const;
219
228 [[nodiscard]] bool isV6() const;
229
246 [[nodiscard]] static std::optional<IpAddress> getLocalAddress(Type type = Type::IpV4);
247
280 [[nodiscard]] static std::optional<IpAddress> getPublicAddress(Time timeout = Time::Zero,
281 std::optional<Type> type = Type::IpV4,
282 bool secure = false);
283
285 // Static member data
287 // NOLINTBEGIN(readability-identifier-naming)
288 static const IpAddress Any;
289 static const IpAddress LocalHost;
290 static const IpAddress Broadcast;
291 static const IpAddress AnyV4;
292 static const IpAddress LocalHostV4;
293 static const IpAddress BroadcastV4;
294 static const IpAddress AnyV6;
295 static const IpAddress LocalHostV6;
296 // NOLINTEND(readability-identifier-naming)
297
298private:
299#if defined(SFML_SYSTEM_WINDOWS)
300 friend SFML_NETWORK_API bool operator<(IpAddress left, IpAddress right);
301#else
302 friend bool operator<(IpAddress left, IpAddress right);
303#endif
304
306 // Member data
308 using V4Data = std::uint32_t;
309 using V6Data = std::array<std::uint8_t, 16>;
310 std::variant<V4Data, V6Data> m_address;
311};
312
322[[nodiscard]] SFML_NETWORK_API bool operator==(IpAddress left, IpAddress right);
323
333[[nodiscard]] SFML_NETWORK_API bool operator!=(IpAddress left, IpAddress right);
334
344[[nodiscard]] SFML_NETWORK_API bool operator<(IpAddress left, IpAddress right);
345
355[[nodiscard]] SFML_NETWORK_API bool operator>(IpAddress left, IpAddress right);
356
366[[nodiscard]] SFML_NETWORK_API bool operator<=(IpAddress left, IpAddress right);
367
377[[nodiscard]] SFML_NETWORK_API bool operator>=(IpAddress left, IpAddress right);
378
392SFML_NETWORK_API std::istream& operator>>(std::istream& stream, std::optional<IpAddress>& address);
393
403SFML_NETWORK_API std::ostream& operator<<(std::ostream& stream, IpAddress address);
404
405} // namespace sf
406
407
#define SFML_NETWORK_API
Encapsulate an IPv4 network address.
Definition IpAddress.hpp:51
static std::optional< IpAddress > getPublicAddress(Time timeout=Time::Zero, std::optional< Type > type=Type::IpV4, bool secure=false)
Get the computer's public address.
bool isV6() const
Check if this IP address is an IPv6 address.
static const IpAddress Any
The same as AnyV4.
static std::optional< IpAddress > resolve(std::string_view address)
Construct the address from a null-terminated string view.
IpAddress(std::array< std::uint8_t, 16 > bytes)
Construct an IPv6 address from 16 bytes.
std::uint32_t toInteger() const
Get an integer representation of the address.
IpAddress(std::uint32_t address)
Construct an IPv4 address from a 32-bit integer.
static const IpAddress LocalHost
The same as LocalHostV4.
bool isV4() const
Check if this IP address is an IPv4 address.
std::string toString() const
Get a string representation of the address.
static const IpAddress BroadcastV4
The "broadcast" IPv4 address (for sending UDP messages to everyone on a local network).
static const IpAddress LocalHostV4
The "localhost" IPv4 address (for connecting a computer to itself locally).
static const IpAddress Broadcast
The same as BroadcastV4.
friend bool operator<(IpAddress left, IpAddress right)
Overload of operator< to compare two IP addresses.
IpAddress(std::uint8_t byte0, std::uint8_t byte1, std::uint8_t byte2, std::uint8_t byte3)
Construct an IPv4 address from 4 bytes.
Type getType() const
Get the type of this IP address.
static std::optional< IpAddress > fromString(std::string_view address)
Try to construct an address from its string representation.
Type
Type of IP address.
Definition IpAddress.hpp:58
@ IpV4
IPv4 address.
Definition IpAddress.hpp:59
static const IpAddress AnyV6
Value representing any IPv6 address (::).
static const IpAddress AnyV4
Value representing any IPv4 address (0.0.0.0).
static std::optional< IpAddress > getLocalAddress(Type type=Type::IpV4)
Get the computer's local address.
static const IpAddress LocalHostV6
The "localhost" IPv6 address (for connecting a computer to itself locally).
std::array< std::uint8_t, 16 > toBytes() const
Get an array of bytes representing the address.
Represents a time value.
Definition Time.hpp:42
static const Time Zero
Predefined "zero" time value.
Definition Time.hpp:110
bool operator!=(IpAddress left, IpAddress right)
Overload of operator!= to compare two IP addresses.
bool operator<(IpAddress left, IpAddress right)
Overload of operator< to compare two IP addresses.
bool operator==(IpAddress left, IpAddress right)
Overload of operator== to compare two IP addresses.
bool operator>(IpAddress left, IpAddress right)
Overload of operator> to compare two IP addresses.
bool operator>=(IpAddress left, IpAddress right)
Overload of operator>= to compare two IP addresses.
std::istream & operator>>(std::istream &stream, std::optional< IpAddress > &address)
Overload of operator>> to extract an IP address from an input stream.
bool operator<=(IpAddress left, IpAddress right)
Overload of operator<= to compare two IP addresses.
std::ostream & operator<<(std::ostream &stream, IpAddress address)
Overload of operator<< to print an IP address to an output stream.