00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef __libutilxx__sella__net__IPPrefix_H__
00010 #define __libutilxx__sella__net__IPPrefix_H__
00011
00012 #include "../../common.h"
00013 #include "IPAddress.h"
00014 #include "AddressException.h"
00015
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 IPPrefix;
00028 }
00029 }
00030
00031 class sella::net::IPPrefix {
00032 public:
00033 typedef std::shared_ptr<IPPrefix> shared;
00034 typedef IPPrefix *ptr;
00035
00036 typedef struct {
00037 size_t operator()(const IPPrefix &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 #ifdef KEYEQUAL_METHOD
00075 const std::unordered_set<std::string> s;
00076 const std::unordered_set<std::string>::key_equal &eqfn = s.key_eq();
00077
00078 return eqfn(a->str(), b->str());
00079 #else
00080 return (*a == *b);
00081 #endif
00082 }
00083 } shared_eq;
00084
00085 typedef std::list<IPPrefix> list;
00086 typedef std::list<ptr> ptr_list;
00087 typedef std::list<shared> shared_list;
00088 typedef std::forward_list<IPPrefix> flist;
00089 typedef std::forward_list<ptr> ptr_flist;
00090 typedef std::forward_list<shared> shared_flist;
00091 typedef std::vector<IPPrefix> vector;
00092 typedef std::vector<ptr> ptr_vector;
00093 typedef std::vector<shared> shared_vector;
00094 typedef std::unordered_set<IPPrefix, hash> uset;
00095 typedef std::unordered_set<ptr, ptr_hash, ptr_eq> ptr_uset;
00096 typedef std::unordered_set<shared, shared_hash, shared_eq> shared_uset;
00097 typedef std::unordered_multiset<IPPrefix, hash> umset;
00098 typedef std::unordered_multiset<ptr, ptr_hash, ptr_eq> ptr_umset;
00099 typedef std::unordered_multiset<shared, shared_hash, shared_eq> shared_umset;
00100
00101 public:
00102 IPPrefix(const IPAddress &address, uint8_t cidr) throw (AddressException);
00103 IPPrefix(const std::string &address, uint8_t cidr) throw (AddressException);
00104 IPPrefix(const std::string &prefix = std::string("0.0.0.0/32")) throw (AddressException);
00105 IPPrefix(const IPPrefix &other) throw ();
00106 IPPrefix(const IPPrefix &&other) throw ();
00107 virtual ~IPPrefix() throw ();
00108
00109 public:
00110 IPPrefix& operator=(const IPPrefix &other) throw ();
00111 IPPrefix& operator=(const IPPrefix &&other) throw ();
00112
00113 bool operator==(const IPPrefix &other) const throw ();
00114 bool operator!=(const IPPrefix &other) const throw ();
00115 bool operator>=(const IPPrefix &other) const throw ();
00116 bool operator<=(const IPPrefix &other) const throw ();
00117 bool operator<(const IPPrefix &other) const throw ();
00118 bool operator>(const IPPrefix &other) const throw ();
00119
00120
00121 bool operator<<(const IPPrefix &other) const throw ();
00122 bool operator<<=(const IPPrefix &other) const throw ();
00123 bool operator>>(const IPPrefix &other) const throw ();
00124 bool operator>>=(const IPPrefix &other) const throw ();
00125
00126 bool operator<<(const IPAddress &other) const throw ();
00127 bool operator<<=(const IPAddress &other) const throw ();
00128 bool operator>>(const IPAddress &other) const throw ();
00129 bool operator>>=(const IPAddress &other) const throw ();
00130
00131 bool contains(const IPPrefix &other, bool orEquals = false) const throw ();
00132 bool contains(const IPAddress &other, bool orEquals = false) const throw ();
00133
00134 public:
00135 int getFamily(void) const throw ();
00136 const IPAddress& getIPAddress(void) const throw ();
00137 uint8_t getCIDR(void) const throw ();
00138 IPAddress getNetmask(void) const throw ();
00139 IPAddress getInverseMask(void) const throw ();
00140 IPAddress getNetwork(void) const throw ();
00141
00142 IPAddress::vector getIPAddresses(bool usable = true) const throw ();
00143 const std::string& str(void) const throw (AddressException);
00144 const char* c_str(void) const throw (AddressException);
00145 uint32_t getU32(void) const throw ();
00146 uint32_t getHash(void) const throw ();
00147
00148 bool empty(void) const throw ();
00149 void clear(void) throw ();
00150
00151 static const IPPrefix::shared shared_ptr(const IPAddress &address, uint8_t cidr) throw (AddressException);
00152 static const IPPrefix::shared shared_ptr(const std::string &address, uint8_t cidr) throw (AddressException);
00153 static const IPPrefix::shared shared_ptr(const std::string &prefix = std::string("0.0.0.0/32")) throw (AddressException);
00154
00155 public:
00156 static bool shared_bi_cmp(shared &a, shared &b) { return (*a < *b); }
00157 static bool shared_bi_eq(shared &a, shared &b) { return (*a == *b); }
00158
00159 private:
00160 IPAddress address;
00161 uint8_t cidr;
00162 uint32_t mask[4];
00163
00164 mutable std::string scache;
00165 mutable uint32_t hcache;
00166
00167 void generateMask(void) throw ();
00168 };
00169
00170 #endif
00171
00172
00173
00174