FastEngine 0.9.4
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_ipAddress.hpp
1/*
2 * Copyright 2025 Guillaume Guillet
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _FGE_C_IPADDRESS_HPP_INCLUDED_
18#define _FGE_C_IPADDRESS_HPP_INCLUDED_
19
20#include "FastEngine/fge_extern.hpp"
21#include "FastEngine/extra/extra_function.hpp"
22#include <array>
23#include <cstdint>
24#include <optional>
25#include <string>
26#include <variant>
27#include <vector>
28
29#ifndef _WIN32
30 #undef None
31#endif
32
33#define FGE_ANYPORT 0
34
35namespace fge::net
36{
37class IpAddress;
38class Packet;
39} // namespace fge::net
40
41template<>
42struct std::hash<fge::net::IpAddress>;
43
44namespace fge::net
45{
46
47using Port = uint16_t;
48
56class FGE_API IpAddress
57{
58public:
59 using Ipv4Data = uint32_t;
60 using Ipv6Data = std::array<uint16_t, 8>;
61 using Data = std::variant<Ipv4Data, Ipv6Data>;
62
63 enum class Types : uint8_t
64 {
65 None,
66 Ipv4,
67 Ipv6
68 };
69
70 enum class CheckHostname
71 {
72 No,
73 Yes
74 };
75
79 IpAddress() noexcept;
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;
114 IpAddress(Ipv6Data const& data) noexcept;
120 IpAddress(Ipv4Data address) noexcept;
121 ~IpAddress() = default;
122
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);
182 bool setNetworkByteOrdered(Ipv4Data address);
189 bool setNetworkByteOrdered(Ipv6Data const& data);
196 bool setNetworkByteOrdered(uint8_t const bytes[16]);
197
198 [[nodiscard]] bool operator==(IpAddress const& r) const;
199
205 [[nodiscard]] std::optional<std::string> toString() const;
206
212 [[nodiscard]] std::optional<Data> getNetworkByteOrder() const;
218 [[nodiscard]] std::optional<Data> getHostByteOrder() const;
219
220 [[nodiscard]] Types getType() const;
221
237 [[nodiscard]] std::optional<IpAddress> mapToIpv6() const;
243 [[nodiscard]] std::optional<IpAddress> mapToIpv4() const;
244 [[nodiscard]] bool isIpv4MappedIpv6() const;
245
251 [[nodiscard]] static std::optional<std::string> getHostName();
258 [[nodiscard]] static std::vector<IpAddress> getLocalAddresses(Types type = Types::None);
259
260 static IpAddress const None;
261
262 static IpAddress const Ipv4Any;
263 static IpAddress const Ipv6Any;
264 static IpAddress Any(Types addressType);
265
266 static IpAddress const Ipv4Loopback;
267 static IpAddress const Ipv6Loopback;
268 static IpAddress Loopback(Types addressType);
269
271
272private:
273 std::variant<std::monostate, Ipv4Data, Ipv6Data> g_address;
274
275 friend struct std::hash<IpAddress>;
276};
277
278FGE_API Packet const& operator>>(Packet const& pck, IpAddress& data);
279FGE_API Packet& operator<<(Packet& pck, IpAddress const& data);
280
281} // namespace fge::net
282
283template<>
284struct std::hash<fge::net::IpAddress>
285{
286 inline std::size_t operator()(fge::net::IpAddress const& r) const noexcept
287 {
288 if (std::holds_alternative<std::monostate>(r.g_address))
289 {
290 return std::hash<std::monostate>{}(std::monostate{});
291 }
292 if (std::holds_alternative<fge::net::IpAddress::Ipv4Data>(r.g_address))
293 {
294 return std::hash<fge::net::IpAddress::Ipv4Data>{}(std::get<fge::net::IpAddress::Ipv4Data>(r.g_address));
295 }
296 auto const& array = std::get<fge::net::IpAddress::Ipv6Data>(r.g_address);
297 return fge::Hash(array.data(), array.size() * 2);
298 }
299};
300
301#endif // _FGE_C_IPADDRESS_HPP_INCLUDED_
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