join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
ip_address.hpp
Go to the documentation of this file.
1
25#ifndef JOIN_CORE_IP_ADDRESS_HPP
26#define JOIN_CORE_IP_ADDRESS_HPP
27
28// C++.
29#include <ostream>
30#include <compare>
31#include <string>
32#include <memory>
33#include <vector>
34
35// C.
36#include <unistd.h>
37#include <sys/socket.h>
38#include <netinet/in.h>
39
40namespace join
41{
42 class IpAddress;
43 class IpAddressImpl;
44
46 using IpAddressList = std::vector<IpAddress>;
47
52 {
53 public:
57 IpAddress ();
58
63 IpAddress (int family);
64
70
76
82 IpAddress (const struct sockaddr& address);
83
90 IpAddress (const void* address, socklen_t length);
91
99 IpAddress (const void* address, socklen_t length, uint32_t scope);
100
107 IpAddress (const std::string& address, int family);
108
114 IpAddress (const std::string& address);
115
122 IpAddress (const char* address, int family);
123
129 IpAddress (const char* address);
130
136 IpAddress (int prefix, int family);
137
144
151
158 IpAddress& operator= (const struct sockaddr& address);
159
163 ~IpAddress ();
164
169 int family () const;
170
175 const void* addr () const;
176
181 socklen_t length () const;
182
187 uint32_t scope () const;
188
193 int prefix () const;
194
199 bool isWildcard () const;
200
205 bool isLoopBack () const;
206
211 bool isLinkLocal () const;
212
217 bool isSiteLocal () const;
218
223 bool isUniqueLocal () const;
224
229 bool isUnicast () const;
230
236 bool isBroadcast (int prefix = 0) const;
237
242 bool isMulticast () const;
243
248 bool isGlobal () const;
249
255 static bool isIpAddress (const std::string& address);
256
261 bool isIpv6Address () const;
262
268 static bool isIpv6Address (const std::string& address);
269
274 bool isIpv4Compat () const;
275
280 bool isIpv4Mapped () const;
281
286 bool isIpv4Address () const;
287
293 static bool isIpv4Address (const std::string& address);
294
300 IpAddress toIpv6 () const;
301
306 IpAddress toIpv4 () const;
307
312 std::string toString () const;
313
318 std::string toArpa () const;
319
323 void clear ();
324
330 static IpAddress ipv4Address (const std::string& interface);
331
336 IpAddress operator~() const;
337
344 uint8_t& operator[] (size_t position);
345
352 const uint8_t& operator[] (size_t position) const;
353
356
359
362
364 static const IpAddress ipv6Routers;
365
367 static constexpr socklen_t ipv6Length = 16;
368
371
374
376 static constexpr socklen_t ipv4Length = 4;
377
378 private:
380 std::unique_ptr<IpAddressImpl> _ip;
381 };
382
389 bool operator== (const IpAddress& a, const IpAddress& b);
390
397 std::strong_ordering operator<=> (const IpAddress& a, const IpAddress& b);
398
405 IpAddress operator& (const IpAddress& a, const IpAddress& b);
406
413 IpAddress operator| (const IpAddress& a, const IpAddress& b);
414
421 IpAddress operator^ (const IpAddress& a, const IpAddress& b);
422
429 std::ostream& operator<< (std::ostream& out, const IpAddress& address);
430}
431
432namespace std
433{
437 template <>
438 struct hash<join::IpAddress>
439 {
440 size_t operator() (const join::IpAddress& ipAddress) const noexcept
441 {
442 size_t h = 0;
443 const uint8_t* bytes = reinterpret_cast<const uint8_t*> (ipAddress.addr ());
444 for (socklen_t i = 0; i < ipAddress.length (); ++i)
445 {
446 h ^= std::hash<uint8_t>{}(bytes[i] + 0x9e3779b9 + (h << 6) + (h >> 2));
447 }
448 return h;
449 }
450 };
451}
452
453#endif
IPv6, IPv4 address class.
Definition ip_address.hpp:52
bool isIpv4Mapped() const
check if IP address is IPv4 mapped.
Definition ip_address.cpp:1429
bool isLinkLocal() const
check if IP address is link local.
Definition ip_address.cpp:1313
IpAddress & operator=(const IpAddress &address)
assign the IpAddress instance by copy.
Definition ip_address.cpp:1189
bool isIpv4Address() const
check if IP address is an IPv4 address.
Definition ip_address.cpp:1438
static const IpAddress ipv6Routers
routers multicast IPv6 address.
Definition ip_address.hpp:364
static const IpAddress ipv6AllNodes
all nodes multicast IPv6 address.
Definition ip_address.hpp:358
static const IpAddress ipv6SolicitedNodes
solicited nodes multicast IPv6 address.
Definition ip_address.hpp:361
IpAddress operator~() const
perform NOT operation on IP address.
Definition ip_address.cpp:1553
bool isSiteLocal() const
check if IP address is site local (deprecated).
Definition ip_address.cpp:1322
bool isIpv4Compat() const
check if IP address is IPv4 compatible (deprecated).
Definition ip_address.cpp:1420
std::string toString() const
convert internal address structure to string.
Definition ip_address.cpp:1499
static constexpr socklen_t ipv6Length
IPv6 length.
Definition ip_address.hpp:367
bool isLoopBack() const
check if IP address is a loopback address.
Definition ip_address.cpp:1304
int prefix() const
get prefix length from netmask address.
Definition ip_address.cpp:1286
uint32_t scope() const
get the scope identifier of the address.
Definition ip_address.cpp:1277
uint8_t & operator[](size_t position)
returns a reference to the element at the specified location.
Definition ip_address.cpp:1573
void clear()
clear IP address (wilcard address).
Definition ip_address.cpp:1517
bool isBroadcast(int prefix=0) const
check if IP address is a broadcast address.
Definition ip_address.cpp:1349
int family() const
get address family.
Definition ip_address.cpp:1250
static const IpAddress ipv4Wildcard
wildcard IPv4 address.
Definition ip_address.hpp:370
~IpAddress()
destroy the IpAddress instance.
Definition ip_address.cpp:1242
socklen_t length() const
get the size in byte of the internal address structure.
Definition ip_address.cpp:1268
static const IpAddress ipv4Broadcast
broadcast IPv4 address.
Definition ip_address.hpp:373
std::string toArpa() const
convert IP address to the in-addr.arpa or ip6.arpa domain name.
Definition ip_address.cpp:1508
bool isGlobal() const
check if IP address is global.
Definition ip_address.cpp:1367
bool isUnicast() const
check if IP address is unicast.
Definition ip_address.cpp:1331
IpAddress toIpv6() const
convert IP address to an IPv6 address.
Definition ip_address.cpp:1464
static IpAddress ipv4Address(const std::string &interface)
get the specified interface IPv4 address.
Definition ip_address.cpp:1526
bool isMulticast() const
check if IP address is multicast.
Definition ip_address.cpp:1358
static constexpr socklen_t ipv4Length
IPv4 length.
Definition ip_address.hpp:376
bool isWildcard() const
check if IP address is a wildcard address.
Definition ip_address.cpp:1295
static bool isIpAddress(const std::string &address)
check if the specified string is an IP address.
Definition ip_address.cpp:1376
const void * addr() const
get the internal address structure.
Definition ip_address.cpp:1259
static const IpAddress ipv6Wildcard
wildcard IPv6 address.
Definition ip_address.hpp:355
IpAddress()
create the IpAddress instance (default: IPv6 wildcard address).
Definition ip_address.cpp:947
IpAddress toIpv4() const
convert IP address to an IPv4 address.
Definition ip_address.cpp:1483
bool isUniqueLocal() const
check if IP address is unique local.
Definition ip_address.cpp:1340
bool isIpv6Address() const
check if IP address is an IPv6 address.
Definition ip_address.cpp:1394
Definition acceptor.hpp:32
bool operator==(const BasicLinkLayerEndpoint< Protocol > &a, const BasicLinkLayerEndpoint< Protocol > &b) noexcept
compare if endpoints are equal.
Definition endpoint.hpp:196
IpAddress operator^(const IpAddress &a, const IpAddress &b)
perform XOR operation on IP address.
Definition ip_address.cpp:1683
IpAddress operator&(const IpAddress &a, const IpAddress &b)
perform AND operation on IP address.
Definition ip_address.cpp:1629
std::ostream & operator<<(std::ostream &os, const BasicLinkLayerEndpoint< Protocol > &endpoint)
push endpoint representation into a stream.
Definition endpoint.hpp:221
IpAddress operator|(const IpAddress &a, const IpAddress &b)
perform OR operation on IP address.
Definition ip_address.cpp:1656
std::vector< IpAddress > IpAddressList
List of IP address.
Definition ip_address.hpp:46
std::strong_ordering operator<=>(const BasicLinkLayerEndpoint< Protocol > &a, const BasicLinkLayerEndpoint< Protocol > &b) noexcept
three-way compare endpoints.
Definition endpoint.hpp:208
Definition error.hpp:144
IpAddress address
Definition tcp_acceptor_test.cpp:35