9 #ifndef __libutilxx__sella__net__IPAddress_H__
10 #define __libutilxx__sella__net__IPAddress_H__
12 #include "../../common.h"
13 #include "AddressException.h"
15 #include <arpa/inet.h>
16 #include <netinet/in.h>
22 #include <forward_list>
23 #include <unordered_set>
35 typedef std::shared_ptr<IPAddress> shared;
39 size_t operator()(
const IPAddress &a)
const {
45 size_t operator()(
const ptr a)
const {
51 size_t operator()(
const shared &a)
const {
63 bool operator()(
const ptr a,
const ptr b)
const {
64 if (a == NULL || b == NULL) {
72 bool operator()(
const shared &a,
const shared &b)
const {
83 typedef std::list<IPAddress> list;
84 typedef std::list<ptr> ptr_list;
85 typedef std::list<shared> shared_list;
86 typedef std::forward_list<IPAddress> flist;
87 typedef std::forward_list<ptr> ptr_flist;
88 typedef std::forward_list<shared> shared_flist;
89 typedef std::vector<IPAddress> vector;
90 typedef std::vector<ptr> ptr_vector;
91 typedef std::vector<shared> shared_vector;
92 typedef std::unordered_set<IPAddress, hash> uset;
93 typedef std::unordered_set<ptr, ptr_hash, ptr_eq> ptr_uset;
94 typedef std::unordered_set<shared, shared_hash, shared_eq> shared_uset;
95 typedef std::unordered_multiset<IPAddress, hash> umset;
96 typedef std::unordered_multiset<ptr, ptr_hash, ptr_eq> ptr_umset;
97 typedef std::unordered_multiset<shared, shared_hash, shared_eq> shared_umset;
100 static const int IPv4Preferred = 0x01;
101 static const int IPv4Required = 0x03;
103 static const int IPv6Preferred = 0x04;
104 static const int IPv6Required = 0x12;
106 static const int IPv4Family = AF_INET;
107 static const int IPv6Family = AF_INET6;
110 IPAddress(
const std::string &text,
int family = IPv4Preferred, uint8_t cidr = 255) throw (AddressException);
111 IPAddress(
int family = IPv4Preferred) throw (AddressException);
112 IPAddress(const struct sockaddr_storage *ss, uint8_t cidr = 255) throw (AddressException);
113 IPAddress(const struct sockaddr *sa, uint8_t cidr = 255) throw (AddressException);
114 IPAddress(const struct in_addr *in, uint8_t cidr = 255) throw (AddressException);
115 IPAddress(const struct in6_addr *in6, uint8_t cidr = 255) throw (AddressException);
116 IPAddress(uint32_t address, uint8_t cidr = 255) throw (AddressException);
117 IPAddress(
unsigned char address[16], uint8_t cidr = 255) throw (AddressException);
127 bool operator==(const
IPAddress &other) const throw ();
128 bool operator!=(const
IPAddress &other) const throw ();
129 bool operator>=(const
IPAddress &other) const throw ();
130 bool operator<=(const
IPAddress &other) const throw ();
131 bool operator<(const
IPAddress &other) const throw ();
132 bool operator>(const
IPAddress &other) const throw ();
139 IPAddress& operator+=(uint32_t increment) throw ();
140 IPAddress operator+(uint32_t increment) const throw ();
144 IPAddress& operator-=(uint32_t increment) throw ();
145 IPAddress operator-(uint32_t increment) const throw ();
150 void applyCIDR(uint8_t cidr) throw (AddressException);
152 int getFamily(
void) const throw ();
153 size_t getAddressStructureSize(
void) const throw ();
155 const struct in_addr& getIPv4AddressStructure(
void) const throw (AddressException);
156 const struct in6_addr& getIPv6AddressStructure(
void) const throw (AddressException);
157 const std::
string& str(
void) const throw (AddressException);
158 const
char* c_str(
void) const throw (AddressException);
159 uint32_t getU32(
void) const throw ();
160 uint32_t getHash(
void) const throw ();
162 bool empty(
void) const throw ();
163 void clear(
void) throw ();
165 static const
IPAddress::shared shared_ptr(const std::
string &text,
int family = IPv4Preferred, uint8_t cidr = 255) throw (AddressException);
166 static const
IPAddress::shared shared_ptr(
int family = IPv4Preferred, uint8_t cidr = 255) throw (AddressException);
167 static const
IPAddress::shared shared_ptr(const struct sockaddr_storage *ss, uint8_t cidr = 255) throw (AddressException);
168 static const
IPAddress::shared shared_ptr(const struct sockaddr *sa, uint8_t cidr = 255) throw (AddressException);
169 static const
IPAddress::shared shared_ptr(const struct in_addr *in, uint8_t cidr = 255) throw (AddressException);
170 static const
IPAddress::shared shared_ptr(const struct in6_addr *in6, uint8_t cidr = 255) throw (AddressException);
171 static const
IPAddress::shared shared_ptr(uint32_t address, uint8_t cidr = 255) throw (AddressException);
172 static const
IPAddress::shared shared_ptr(
unsigned char address[16], uint8_t cidr = 255) throw (AddressException);
176 static bool shared_bi_cmp(shared &a, shared &b) {
return (*a < *b); }
177 static bool shared_bi_eq(shared &a, shared &b) {
return (*a == *b); }
180 bool parse(
int &family,
struct in_addr &v4,
struct in6_addr &v6,
const std::string &addr)
throw ();
181 bool parse(
int &family,
struct in6_addr &v6,
struct in_addr &v4,
const std::string &addr)
throw ();
182 bool parse(
int &family,
struct in_addr &v4,
const std::string &addr)
throw ();
183 bool parse(
int &family,
struct in6_addr &v6,
const std::string &addr)
throw ();
193 mutable std::string scache;
194 mutable uint32_t hcache;