join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
ipaddress.hpp
Go to the documentation of this file.
1
25#ifndef JOIN_CORE_IPADDRESS_HPP
26#define JOIN_CORE_IPADDRESS_HPP
27
28// C++.
29#include <ostream>
30#include <string>
31#include <memory>
32#include <vector>
33
34// C.
35#include <unistd.h>
36#include <sys/socket.h>
37#include <netinet/in.h>
38
39namespace join
40{
41 class IpAddress;
42 class IpAddressImpl;
43
45 using IpAddressList = std::vector<IpAddress>;
46
51 {
52 public:
56 IpAddress ();
57
62 IpAddress (int family);
63
69
75
81 IpAddress (const struct sockaddr& address);
82
89 IpAddress (const void* address, socklen_t length);
90
98 IpAddress (const void* address, socklen_t length, uint32_t scope);
99
106 IpAddress (const std::string& address, int family);
107
113 IpAddress (const std::string& address);
114
121 IpAddress (const char* address, int family);
122
128 IpAddress (const char* address);
129
135 IpAddress (int prefix, int family);
136
143
150
157 IpAddress& operator= (const struct sockaddr& address);
158
162 ~IpAddress ();
163
168 int family () const;
169
174 const void* addr () const;
175
180 socklen_t length () const;
181
186 uint32_t scope () const;
187
192 int prefix () const;
193
198 bool isWildcard () const;
199
204 bool isLoopBack () const;
205
210 bool isLinkLocal () const;
211
216 bool isSiteLocal () const;
217
222 bool isUniqueLocal () const;
223
228 bool isUnicast () const;
229
235 bool isBroadcast (int prefix = 0) const;
236
241 bool isMulticast () const;
242
247 bool isGlobal () const;
248
254 static bool isIpAddress (const std::string& address);
255
260 bool isIpv6Address () const;
261
267 static bool isIpv6Address (const std::string& address);
268
273 bool isIpv4Compat () const;
274
279 bool isIpv4Mapped () const;
280
285 bool isIpv4Address () const;
286
292 static bool isIpv4Address (const std::string& address);
293
299 IpAddress toIpv6 () const;
300
305 IpAddress toIpv4 () const;
306
311 std::string toString () const;
312
317 std::string toArpa () const;
318
322 void clear ();
323
329 static IpAddress ipv4Address (const std::string& interface);
330
335 IpAddress operator~() const;
336
343 uint8_t& operator[] (size_t position);
344
351 const uint8_t& operator[] (size_t position) const;
352
355
358
361
363 static const IpAddress ipv6Routers;
364
366 static constexpr socklen_t ipv6Length = 16;
367
370
373
375 static constexpr socklen_t ipv4Length = 4;
376
377 private:
379 std::unique_ptr<IpAddressImpl> _ip;
380 };
381
388 bool operator== (const IpAddress& a, const IpAddress& b);
389
396 bool operator!= (const IpAddress& a, const IpAddress& b);
397
404 bool operator< (const IpAddress& a, const IpAddress& b);
405
412 bool operator<= (const IpAddress& a, const IpAddress& b);
413
420 bool operator> (const IpAddress& a, const IpAddress& b);
421
428 bool operator>= (const IpAddress& a, const IpAddress& b);
429
436 IpAddress operator& (const IpAddress& a, const IpAddress& b);
437
444 IpAddress operator| (const IpAddress& a, const IpAddress& b);
445
452 IpAddress operator^ (const IpAddress& a, const IpAddress& b);
453
460 std::ostream& operator<< (std::ostream& out, const IpAddress& address);
461}
462
463namespace std
464{
468 template <>
469 struct hash<join::IpAddress>
470 {
471 size_t operator() (const join::IpAddress& ipAddress) const noexcept
472 {
473 size_t h = 0;
474 const uint8_t* bytes = reinterpret_cast<const uint8_t*> (ipAddress.addr ());
475 for (socklen_t i = 0; i < ipAddress.length (); ++i)
476 {
477 h ^= std::hash<uint8_t>{}(bytes[i] + 0x9e3779b9 + (h << 6) + (h >> 2));
478 }
479 return h;
480 }
481 };
482}
483
484#endif
IPv6, IPv4 address class.
Definition ipaddress.hpp:51
bool isIpv4Mapped() const
check if IP address is IPv4 mapped.
Definition ipaddress.cpp:1429
bool isLinkLocal() const
check if IP address is link local.
Definition ipaddress.cpp:1313
IpAddress & operator=(const IpAddress &address)
assign the IpAddress instance by copy.
Definition ipaddress.cpp:1189
bool isIpv4Address() const
check if IP address is an IPv4 address.
Definition ipaddress.cpp:1438
static const IpAddress ipv6Routers
routers multicast IPv6 address.
Definition ipaddress.hpp:363
static const IpAddress ipv6AllNodes
all nodes multicast IPv6 address.
Definition ipaddress.hpp:357
static const IpAddress ipv6SolicitedNodes
solicited nodes multicast IPv6 address.
Definition ipaddress.hpp:360
IpAddress operator~() const
perform NOT operation on IP address.
Definition ipaddress.cpp:1553
bool isSiteLocal() const
check if IP address is site local (deprecated).
Definition ipaddress.cpp:1322
bool isIpv4Compat() const
check if IP address is IPv4 compatible (deprecated).
Definition ipaddress.cpp:1420
std::string toString() const
convert internal address structure to string.
Definition ipaddress.cpp:1499
static constexpr socklen_t ipv6Length
IPv6 length.
Definition ipaddress.hpp:366
bool isLoopBack() const
check if IP address is a loopback address.
Definition ipaddress.cpp:1304
int prefix() const
get prefix length from netmask address.
Definition ipaddress.cpp:1286
uint32_t scope() const
get the scope identifier of the address.
Definition ipaddress.cpp:1277
uint8_t & operator[](size_t position)
returns a reference to the element at the specified location.
Definition ipaddress.cpp:1573
void clear()
clear IP address (wilcard address).
Definition ipaddress.cpp:1517
bool isBroadcast(int prefix=0) const
check if IP address is a broadcast address.
Definition ipaddress.cpp:1349
int family() const
get address family.
Definition ipaddress.cpp:1250
static const IpAddress ipv4Wildcard
wildcard IPv4 address.
Definition ipaddress.hpp:369
~IpAddress()
destroy the IpAddress instance.
Definition ipaddress.cpp:1242
socklen_t length() const
get the size in byte of the internal address structure.
Definition ipaddress.cpp:1268
static const IpAddress ipv4Broadcast
broadcast IPv4 address.
Definition ipaddress.hpp:372
std::string toArpa() const
convert IP address to the in-addr.arpa or ip6.arpa domain name.
Definition ipaddress.cpp:1508
bool isGlobal() const
check if IP address is global.
Definition ipaddress.cpp:1367
bool isUnicast() const
check if IP address is unicast.
Definition ipaddress.cpp:1331
IpAddress toIpv6() const
convert IP address to an IPv6 address.
Definition ipaddress.cpp:1464
static IpAddress ipv4Address(const std::string &interface)
get the specified interface IPv4 address.
Definition ipaddress.cpp:1526
bool isMulticast() const
check if IP address is multicast.
Definition ipaddress.cpp:1358
static constexpr socklen_t ipv4Length
IPv4 length.
Definition ipaddress.hpp:375
bool isWildcard() const
check if IP address is a wildcard address.
Definition ipaddress.cpp:1295
static bool isIpAddress(const std::string &address)
check if the specified string is an IP address.
Definition ipaddress.cpp:1376
const void * addr() const
get the internal address structure.
Definition ipaddress.cpp:1259
static const IpAddress ipv6Wildcard
wildcard IPv6 address.
Definition ipaddress.hpp:354
IpAddress()
create the IpAddress instance (default: IPv6 wildcard address).
Definition ipaddress.cpp:947
IpAddress toIpv4() const
convert IP address to an IPv4 address.
Definition ipaddress.cpp:1483
bool isUniqueLocal() const
check if IP address is unique local.
Definition ipaddress.cpp:1340
bool isIpv6Address() const
check if IP address is an IPv6 address.
Definition ipaddress.cpp:1394
Definition acceptor.hpp:32
bool operator<(const BasicUnixEndpoint< Protocol > &a, const BasicUnixEndpoint< Protocol > &b) noexcept
compare if endpoint is lower.
Definition endpoint.hpp:207
IpAddress operator^(const IpAddress &a, const IpAddress &b)
perform XOR operation on IP address.
Definition ipaddress.cpp:1719
bool operator>(const BasicUnixEndpoint< Protocol > &a, const BasicUnixEndpoint< Protocol > &b) noexcept
compare if endpoint is greater.
Definition endpoint.hpp:219
IpAddress operator&(const IpAddress &a, const IpAddress &b)
perform AND operation on IP address.
Definition ipaddress.cpp:1665
bool operator!=(const BasicUnixEndpoint< Protocol > &a, const BasicUnixEndpoint< Protocol > &b) noexcept
compare if endpoints are not equal.
Definition endpoint.hpp:195
bool operator>=(const BasicUnixEndpoint< Protocol > &a, const BasicUnixEndpoint< Protocol > &b) noexcept
compare if endpoint is greater or equal.
Definition endpoint.hpp:243
std::ostream & operator<<(std::ostream &os, const BasicUnixEndpoint< Protocol > &endpoint)
push endpoint representation into a stream.
Definition endpoint.hpp:255
bool operator<=(const BasicUnixEndpoint< Protocol > &a, const BasicUnixEndpoint< Protocol > &b) noexcept
compare if endpoint is lower or equal.
Definition endpoint.hpp:231
IpAddress operator|(const IpAddress &a, const IpAddress &b)
perform OR operation on IP address.
Definition ipaddress.cpp:1692
bool operator==(const BasicUnixEndpoint< Protocol > &a, const BasicUnixEndpoint< Protocol > &b) noexcept
compare if endpoints are equal.
Definition endpoint.hpp:183
std::vector< IpAddress > IpAddressList
List of IP address.
Definition ipaddress.hpp:45
Definition error.hpp:137
IpAddress address
Definition tcpacceptor_test.cpp:35