Nazara Engine  0.4
A fast, complete, cross-platform API designed for game development
IpAddress.hpp
1 // Copyright (C) 2017 Jérôme Leclercq
2 // This file is part of the "Nazara Engine - Network module"
3 // For conditions of distribution and use, see copyright notice in Config.hpp
4 
5 #pragma once
6 
7 #ifndef NAZARA_IPADDRESS_HPP
8 #define NAZARA_IPADDRESS_HPP
9 
10 #include <Nazara/Prerequesites.hpp>
11 #include <Nazara/Core/String.hpp>
12 #include <Nazara/Network/Config.hpp>
13 #include <Nazara/Network/Enums.hpp>
14 #include <array>
15 #include <iosfwd>
16 
17 namespace Nz
18 {
19  struct HostnameInfo;
20 
21  class NAZARA_NETWORK_API IpAddress
22  {
23  public:
24  using IPv4 = std::array<UInt8, 4>; //< four 8bits blocks
25  using IPv6 = std::array<UInt16, 8>; //< eight 16bits blocks
26 
27  inline IpAddress();
28  inline IpAddress(const IPv4& ip, UInt16 port = 0);
29  inline IpAddress(const IPv6& ip, UInt16 port = 0);
30  inline IpAddress(const UInt8& a, const UInt8& b, const UInt8& c, const UInt8& d, UInt16 port = 0);
31  inline IpAddress(const UInt16& a, const UInt16& b, const UInt16& c, const UInt16& d, const UInt16& e, const UInt16& f, const UInt16& g, const UInt16& h, UInt16 port = 0);
32  inline explicit IpAddress(const char* address);
33  inline explicit IpAddress(const String& address);
34  IpAddress(const IpAddress&) = default;
35  IpAddress(IpAddress&&) = default;
36  ~IpAddress() = default;
37 
38  bool BuildFromAddress(const char* address);
39 
40  inline UInt16 GetPort() const;
41  inline NetProtocol GetProtocol() const;
42 
43  bool IsLoopback() const;
44  inline bool IsValid() const;
45 
46  inline void SetPort(UInt16 port);
47 
48  inline IPv4 ToIPv4() const;
49  inline IPv6 ToIPv6() const;
50  String ToString() const;
51  inline UInt32 ToUInt32() const;
52 
53  inline explicit operator bool() const;
54 
55  IpAddress& operator=(const IpAddress&) = default;
56  IpAddress& operator=(IpAddress&&) = default;
57 
58  static String ResolveAddress(const IpAddress& address, String* service = nullptr, ResolveError* error = nullptr);
59  static std::vector<HostnameInfo> ResolveHostname(NetProtocol procol, const String& hostname, const String& protocol = "http", ResolveError* error = nullptr);
60 
61  inline friend std::ostream& operator<<(std::ostream& out, const IpAddress& address);
62 
63  inline friend bool operator==(const IpAddress& first, const IpAddress& second);
64  inline friend bool operator!=(const IpAddress& first, const IpAddress& second);
65  inline friend bool operator<(const IpAddress& first, const IpAddress& second);
66  inline friend bool operator<=(const IpAddress& first, const IpAddress& second);
67  inline friend bool operator>(const IpAddress& first, const IpAddress& second);
68  inline friend bool operator>=(const IpAddress& first, const IpAddress& second);
69 
70  static IpAddress AnyIpV4;
71  static IpAddress AnyIpV6;
72  static IpAddress BroadcastIpV4;
73  static IpAddress Invalid;
74  static IpAddress LoopbackIpV4;
75  static IpAddress LoopbackIpV6;
76 
77  private:
78  union
79  {
80  IPv4 m_ipv4;
81  IPv6 m_ipv6;
82  };
83 
84  NetProtocol m_protocol;
85  UInt16 m_port;
86  bool m_isValid;
87  };
88 
89  struct HostnameInfo
90  {
91  IpAddress address;
92  NetProtocol protocol;
93  SocketType socketType;
94  String canonicalName;
95  };
96 
97 }
98 
99 #include <Nazara/Network/IpAddress.inl>
100 
101 #endif // NAZARA_IPADDRESS_HPP
TODO: Inherit SoundEmitter from Node.
Definition: Algorithm.hpp:12
bool operator<(const Bitset< Block, Allocator > &lhs, const Nz::Bitset< Block, Allocator > &rhs)
Compares two bitsets.
Definition: Bitset.inl:1517
Network class that represents an IP address.
Definition: IpAddress.hpp:21
bool operator>(const Bitset< Block, Allocator > &lhs, const Nz::Bitset< Block, Allocator > &rhs)
Compares two bitsets.
Definition: Bitset.inl:1566
Core class that represents a string.
Definition: String.hpp:22
bool operator!=(const Bitset< Block, Allocator > &lhs, const Nz::Bitset< Block, Allocator > &rhs)
Compares two bitsets.
Definition: Bitset.inl:1503
bool operator==(const Bitset< Block, Allocator > &lhs, const Nz::Bitset< Block, Allocator > &rhs)
Compares two bitsets.
Definition: Bitset.inl:1469
bool operator>=(const Bitset< Block, Allocator > &lhs, const Nz::Bitset< Block, Allocator > &rhs)
Compares two bitsets.
Definition: Bitset.inl:1580
bool operator<=(const Bitset< Block, Allocator > &lhs, const Nz::Bitset< Block, Allocator > &rhs)
Compares two bitsets.
Definition: Bitset.inl:1552