00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef __libutilxx__sella__net__IPAddress_H__
00010 #define __libutilxx__sella__net__IPAddress_H__
00011
00012 #include "../../common.h"
00013 #include "AddressException.h"
00014
00015 #include <arpa/inet.h>
00016 #include <netinet/in.h>
00017
00018 #include <list>
00019 #include <vector>
00020 #include <string>
00021 #include <memory>
00022 #include <forward_list>
00023 #include <unordered_set>
00024
00025 namespace sella {
00026 namespace net {
00027 class IPAddress;
00028 }
00029 }
00030
00031 class sella::net::IPAddress {
00032 public:
00033 typedef std::shared_ptr<IPAddress> shared;
00034 typedef IPAddress *ptr;
00035
00036 typedef struct {
00037 size_t operator()(const IPAddress &a) const {
00038 const std::unordered_set<uint32_t> s;
00039 const std::unordered_set<uint32_t>::hasher &hfn = s.hash_function();
00040
00041 return hfn(a.getHash());
00042 }
00043 } hash;
00044
00045 typedef struct {
00046 size_t operator()(const ptr a) const {
00047 const std::unordered_set<uint32_t> s;
00048 const std::unordered_set<uint32_t>::hasher &hfn = s.hash_function();
00049
00050 return hfn(a->getHash());
00051 }
00052 } ptr_hash;
00053
00054 typedef struct {
00055 size_t operator()(const shared &a) const {
00056 const std::unordered_set<uint32_t> s;
00057 const std::unordered_set<uint32_t>::hasher &hfn = s.hash_function();
00058
00059 return hfn(a->getHash());
00060 }
00061 } shared_hash;
00062
00063 typedef struct {
00064 bool operator()(const ptr a, const ptr b) const {
00065 if (a == NULL || b == NULL) {
00066 return false;
00067 }
00068 return (*a == *b);
00069 }
00070 } ptr_eq;
00071
00072 typedef struct {
00073 bool operator()(const shared &a, const shared &b) const {
00074 return (*a == *b);
00075 }
00076 } shared_eq;
00077
00078 typedef std::list<IPAddress> list;
00079 typedef std::list<ptr> ptr_list;
00080 typedef std::list<shared> shared_list;
00081 typedef std::forward_list<IPAddress> flist;
00082 typedef std::forward_list<ptr> ptr_flist;
00083 typedef std::forward_list<shared> shared_flist;
00084 typedef std::vector<IPAddress> vector;
00085 typedef std::vector<ptr> ptr_vector;
00086 typedef std::vector<shared> shared_vector;
00087 typedef std::unordered_set<IPAddress, hash> uset;
00088 typedef std::unordered_set<ptr, ptr_hash, ptr_eq> ptr_uset;
00089 typedef std::unordered_set<shared, shared_hash, shared_eq> shared_uset;
00090 typedef std::unordered_multiset<IPAddress, hash> umset;
00091 typedef std::unordered_multiset<ptr, ptr_hash, ptr_eq> ptr_umset;
00092 typedef std::unordered_multiset<shared, shared_hash, shared_eq> shared_umset;
00093
00094 public:
00095 static const int IPv4Preferred = 0x01;
00096 static const int IPv4Required = 0x03;
00097
00098 static const int IPv6Preferred = 0x04;
00099 static const int IPv6Required = 0x12;
00100
00101 static const int IPv4Family = AF_INET;
00102 static const int IPv6Family = AF_INET6;
00103
00104 public:
00105 IPAddress(const std::string &text, int family = IPv4Preferred, uint8_t cidr = 255) throw (AddressException);
00106 IPAddress(int family = IPv4Preferred) throw (AddressException);
00107 IPAddress(const struct sockaddr_storage *ss, uint8_t cidr = 255) throw (AddressException);
00108 IPAddress(const struct sockaddr *sa, uint8_t cidr = 255) throw (AddressException);
00109 IPAddress(const struct in_addr *in, uint8_t cidr = 255) throw (AddressException);
00110 IPAddress(const struct in6_addr *in6, uint8_t cidr = 255) throw (AddressException);
00111 IPAddress(uint32_t address, uint8_t cidr = 255) throw (AddressException);
00112 IPAddress(unsigned char address[16], uint8_t cidr = 255) throw (AddressException);
00113
00114 IPAddress(const IPAddress &other) throw ();
00115 IPAddress(const IPAddress &&other) throw ();
00116 virtual ~IPAddress() throw ();
00117
00118 public:
00119 IPAddress& operator=(const IPAddress &other) throw ();
00120 IPAddress& operator=(const IPAddress &&other) throw ();
00121
00122 bool operator==(const IPAddress &other) const throw ();
00123 bool operator!=(const IPAddress &other) const throw ();
00124 bool operator>=(const IPAddress &other) const throw ();
00125 bool operator<=(const IPAddress &other) const throw ();
00126 bool operator<(const IPAddress &other) const throw ();
00127 bool operator>(const IPAddress &other) const throw ();
00128
00129 IPAddress operator&(const IPAddress &other) const throw (AddressException);
00130 IPAddress operator|(const IPAddress &other) const throw (AddressException);
00131 IPAddress operator^(const IPAddress &other) const throw (AddressException);
00132 IPAddress operator~() const throw ();
00133
00134 IPAddress& operator+=(uint32_t increment) throw ();
00135 IPAddress operator+(uint32_t increment) const throw ();
00136 IPAddress operator++(int) throw ();
00137 IPAddress& operator++() throw ();
00138
00139 IPAddress& operator-=(uint32_t increment) throw ();
00140 IPAddress operator-(uint32_t increment) const throw ();
00141 IPAddress operator--(int) throw ();
00142 IPAddress& operator--() throw ();
00143
00144 public:
00145 void applyCIDR(uint8_t cidr) throw (AddressException);
00146
00147 int getFamily(void) const throw ();
00148 size_t getAddressStructureSize(void) const throw ();
00149
00150 const struct in_addr& getIPv4AddressStructure(void) const throw (AddressException);
00151 const struct in6_addr& getIPv6AddressStructure(void) const throw (AddressException);
00152 const std::string& str(void) const throw (AddressException);
00153 const char* c_str(void) const throw (AddressException);
00154 uint32_t getU32(void) const throw ();
00155 uint32_t getHash(void) const throw ();
00156
00157 bool empty(void) const throw ();
00158 void clear(void) throw ();
00159
00160 static const IPAddress::shared shared_ptr(const std::string &text, int family = IPv4Preferred, uint8_t cidr = 255) throw (AddressException);
00161 static const IPAddress::shared shared_ptr(int family = IPv4Preferred, uint8_t cidr = 255) throw (AddressException);
00162 static const IPAddress::shared shared_ptr(const struct sockaddr_storage *ss, uint8_t cidr = 255) throw (AddressException);
00163 static const IPAddress::shared shared_ptr(const struct sockaddr *sa, uint8_t cidr = 255) throw (AddressException);
00164 static const IPAddress::shared shared_ptr(const struct in_addr *in, uint8_t cidr = 255) throw (AddressException);
00165 static const IPAddress::shared shared_ptr(const struct in6_addr *in6, uint8_t cidr = 255) throw (AddressException);
00166 static const IPAddress::shared shared_ptr(uint32_t address, uint8_t cidr = 255) throw (AddressException);
00167 static const IPAddress::shared shared_ptr(unsigned char address[16], uint8_t cidr = 255) throw (AddressException);
00168
00169 public:
00170 static bool shared_bi_cmp(shared &a, shared &b) { return (*a < *b); }
00171 static bool shared_bi_eq(shared &a, shared &b) { return (*a == *b); }
00172
00173 protected:
00174 bool parse(int &family, struct in_addr &v4, struct in6_addr &v6, const std::string &addr) throw ();
00175 bool parse(int &family, struct in6_addr &v6, struct in_addr &v4, const std::string &addr) throw ();
00176 bool parse(int &family, struct in_addr &v4, const std::string &addr) throw ();
00177 bool parse(int &family, struct in6_addr &v6, const std::string &addr) throw ();
00178
00179 private:
00180 int family;
00181
00182 union {
00183 struct in_addr v4;
00184 struct in6_addr v6;
00185 } addr;
00186
00187 mutable std::string scache;
00188 mutable uint32_t hcache;
00189 };
00190
00191 #endif
00192
00193
00194
00195