FastEngine 0.9.3
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_ipAddress.hpp
1/*
2 * Copyright 2024 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
227 [[nodiscard]] static std::optional<std::string> getHostName();
234 [[nodiscard]] static std::vector<IpAddress> getLocalAddresses(Types type = Types::None);
235
236 static IpAddress const None;
237
238 static IpAddress const Ipv4Any;
239 static IpAddress const Ipv6Any;
240 static IpAddress Any(Types addressType);
241
242 static IpAddress const Ipv4Loopback;
243 static IpAddress const Ipv6Loopback;
244 static IpAddress Loopback(Types addressType);
245
246 static IpAddress const Ipv4Broadcast;
247
248private:
249 std::variant<std::monostate, Ipv4Data, Ipv6Data> g_address;
250
251 friend struct std::hash<IpAddress>;
252};
253
254FGE_API Packet const& operator>>(Packet const& pck, IpAddress& data);
255FGE_API Packet& operator<<(Packet& pck, IpAddress const& data);
256
257} // namespace fge::net
258
259template<>
260struct std::hash<fge::net::IpAddress>
261{
262 inline std::size_t operator()(fge::net::IpAddress const& r) const noexcept
263 {
264 if (std::holds_alternative<std::monostate>(r.g_address))
265 {
266 return std::hash<std::monostate>{}(std::monostate{});
267 }
268 if (std::holds_alternative<fge::net::IpAddress::Ipv4Data>(r.g_address))
269 {
270 return std::hash<fge::net::IpAddress::Ipv4Data>{}(std::get<fge::net::IpAddress::Ipv4Data>(r.g_address));
271 }
272 auto const& array = std::get<fge::net::IpAddress::Ipv6Data>(r.g_address);
273 return fge::Hash(array.data(), array.size() * 2);
274 }
275};
276
277#endif // _FGE_C_IPADDRESS_HPP_INCLUDED_
A class to represent an IP address.
Definition C_ipAddress.hpp:57
IpAddress() noexcept
Build a default invalid IP address.
Definition C_packet.hpp:70