SFML - Simple and Fast Multimedia Library

SFML 3.1.0
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
30#include <SFML/Network/Export.hpp>
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{
50 class SFML_NETWORK_API IpAddress
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
392 SFML_NETWORK_API std::istream& operator>>(std::istream& stream, std::optional<IpAddress>& address);
393
403 SFML_NETWORK_API std::ostream& operator<<(std::ostream& stream, IpAddress address);
404
405} // namespace sf
406
407
Export.hpp
SFML_NETWORK_API
#define SFML_NETWORK_API
Definition Network/Export.hpp:42
Time.hpp
sf::IpAddress
Encapsulate an IPv4 network address.
Definition IpAddress.hpp:51
sf::IpAddress::getPublicAddress
static std::optional< IpAddress > getPublicAddress(Time timeout=Time::Zero, std::optional< Type > type=Type::IpV4, bool secure=false)
Get the computer's public address.
sf::IpAddress::isV6
bool isV6() const
Check if this IP address is an IPv6 address.
sf::IpAddress::Any
static const IpAddress Any
The same as AnyV4.
Definition IpAddress.hpp:288
sf::IpAddress::resolve
static std::optional< IpAddress > resolve(std::string_view address)
Construct the address from a null-terminated string view.
sf::IpAddress::IpAddress
IpAddress(std::array< std::uint8_t, 16 > bytes)
Construct an IPv6 address from 16 bytes.
sf::IpAddress::toInteger
std::uint32_t toInteger() const
Get an integer representation of the address.
sf::IpAddress::IpAddress
IpAddress(std::uint32_t address)
Construct an IPv4 address from a 32-bit integer.
sf::IpAddress::LocalHost
static const IpAddress LocalHost
The same as LocalHostV4.
Definition IpAddress.hpp:289
sf::IpAddress::isV4
bool isV4() const
Check if this IP address is an IPv4 address.
sf::IpAddress::toString
std::string toString() const
Get a string representation of the address.
sf::IpAddress::BroadcastV4
static const IpAddress BroadcastV4
The "broadcast" IPv4 address (for sending UDP messages to everyone on a local network).
Definition IpAddress.hpp:293
sf::IpAddress::LocalHostV4
static const IpAddress LocalHostV4
The "localhost" IPv4 address (for connecting a computer to itself locally).
Definition IpAddress.hpp:292
sf::IpAddress::Broadcast
static const IpAddress Broadcast
The same as BroadcastV4.
Definition IpAddress.hpp:290
sf::IpAddress::operator<
friend bool operator<(IpAddress left, IpAddress right)
Overload of operator< to compare two IP addresses.
sf::IpAddress::IpAddress
IpAddress(std::uint8_t byte0, std::uint8_t byte1, std::uint8_t byte2, std::uint8_t byte3)
Construct an IPv4 address from 4 bytes.
sf::IpAddress::getType
Type getType() const
Get the type of this IP address.
sf::IpAddress::fromString
static std::optional< IpAddress > fromString(std::string_view address)
Try to construct an address from its string representation.
sf::IpAddress::Type
Type
Type of IP address.
Definition IpAddress.hpp:58
sf::IpAddress::Type::IpV4
@ IpV4
IPv4 address.
Definition IpAddress.hpp:59
sf::IpAddress::AnyV6
static const IpAddress AnyV6
Value representing any IPv6 address (::).
Definition IpAddress.hpp:294
sf::IpAddress::AnyV4
static const IpAddress AnyV4
Value representing any IPv4 address (0.0.0.0).
Definition IpAddress.hpp:291
sf::IpAddress::getLocalAddress
static std::optional< IpAddress > getLocalAddress(Type type=Type::IpV4)
Get the computer's local address.
sf::IpAddress::LocalHostV6
static const IpAddress LocalHostV6
The "localhost" IPv6 address (for connecting a computer to itself locally).
Definition IpAddress.hpp:295
sf::IpAddress::toBytes
std::array< std::uint8_t, 16 > toBytes() const
Get an array of bytes representing the address.
sf::Time
Represents a time value.
Definition Time.hpp:42
sf::Time::Zero
static const Time Zero
Predefined "zero" time value.
Definition Time.hpp:110
sf
Definition AudioResource.hpp:36
sf::operator!=
bool operator!=(IpAddress left, IpAddress right)
Overload of operator!= to compare two IP addresses.
sf::operator<
bool operator<(IpAddress left, IpAddress right)
Overload of operator< to compare two IP addresses.
sf::operator==
bool operator==(IpAddress left, IpAddress right)
Overload of operator== to compare two IP addresses.
sf::operator>
bool operator>(IpAddress left, IpAddress right)
Overload of operator> to compare two IP addresses.
sf::operator>=
bool operator>=(IpAddress left, IpAddress right)
Overload of operator>= to compare two IP addresses.
sf::operator>>
std::istream & operator>>(std::istream &stream, std::optional< IpAddress > &address)
Overload of operator>> to extract an IP address from an input stream.
sf::operator<=
bool operator<=(IpAddress left, IpAddress right)
Overload of operator<= to compare two IP addresses.
sf::operator<<
std::ostream & operator<<(std::ostream &stream, IpAddress address)
Overload of operator<< to print an IP address to an output stream.