join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
ipaddress.hpp
Go to the documentation of this file.
1
25#ifndef __JOIN_IPADDRESS_HPP__
26#define __JOIN_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
298 IpAddress toIpv6 () const;
299
304 IpAddress toIpv4 () const;
305
310 std::string toString () const;
311
316 std::string toArpa () const;
317
321 void clear ();
322
328 static IpAddress ipv4Address (const std::string& interface);
329
334 IpAddress operator~ () const;
335
342 uint8_t& operator[] (size_t position);
343
350 const uint8_t& operator[] (size_t position) const;
351
354
357
360
362 static const IpAddress ipv6Routers;
363
365 static constexpr socklen_t ipv6Length = 16;
366
369
372
374 static constexpr socklen_t ipv4Length = 4;
375
376 private:
378 std::unique_ptr <IpAddressImpl> _ip;
379 };
380
387 bool operator== (const IpAddress& a, const IpAddress& b);
388
395 bool operator!= (const IpAddress& a, const IpAddress& b);
396
403 bool operator< (const IpAddress& a, const IpAddress& b);
404
411 bool operator<= (const IpAddress& a, const IpAddress& b);
412
419 bool operator> (const IpAddress& a, const IpAddress& b);
420
427 bool operator>= (const IpAddress& a, const IpAddress& b);
428
435 IpAddress operator& (const IpAddress& a, const IpAddress& b);
436
443 IpAddress operator| (const IpAddress& a, const IpAddress& b);
444
451 IpAddress operator^ (const IpAddress& a, const IpAddress& b);
452
459 std::ostream& operator<< (std::ostream& out, const IpAddress& address);
460}
461
462#endif
IPv6, IPv4 address class.
Definition ipaddress.hpp:51
bool isIpv4Mapped() const
check if IP address is IPv4 mapped.
Definition ipaddress.cpp:1430
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:1439
static const IpAddress ipv6Routers
routers multicast IPv6 address.
Definition ipaddress.hpp:362
static const IpAddress ipv6AllNodes
all nodes multicast IPv6 address.
Definition ipaddress.hpp:356
static const IpAddress ipv6SolicitedNodes
solicited nodes multicast IPv6 address.
Definition ipaddress.hpp:359
IpAddress operator~() const
perform NOT operation on IP address.
Definition ipaddress.cpp:1554
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:1421
std::string toString() const
convert internal address structure to string.
Definition ipaddress.cpp:1500
static constexpr socklen_t ipv6Length
IPv6 length.
Definition ipaddress.hpp:365
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:1574
void clear()
clear IP address (wilcard address).
Definition ipaddress.cpp:1518
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:368
~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:371
std::string toArpa() const
convert IP address to the in-addr.arpa or ip6.arpa domain name.
Definition ipaddress.cpp:1509
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:1465
static IpAddress ipv4Address(const std::string &interface)
get the specified interface IPv4 address.
Definition ipaddress.cpp:1527
bool isMulticast() const
check if IP address is multicast.
Definition ipaddress.cpp:1358
static constexpr socklen_t ipv4Length
IPv4 length.
Definition ipaddress.hpp:374
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:353
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:1484
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:1720
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:1666
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
std::vector< IpAddress > IpAddressList
List of IP address.
Definition ipaddress.hpp:45
IpAddress operator|(const IpAddress &a, const IpAddress &b)
perform OR operation on IP address.
Definition ipaddress.cpp:1693
bool operator==(const BasicUnixEndpoint< Protocol > &a, const BasicUnixEndpoint< Protocol > &b) noexcept
compare if endpoints are equal.
Definition endpoint.hpp:183
IpAddress address
Definition tcpacceptor_test.cpp:35