libutil++  1.9.3
 All Classes Functions Variables
IPPrefix.h
1 /*
2 ** libutil++
3 ** $Id: IPPrefix.h 1696 2016-07-29 17:32:38Z sella $
4 ** Copyright (c) 2011-2016 Digital Genesis, LLC. All Rights Reserved.
5 ** Released under the LGPL Version 2.1 License.
6 ** http://www.digitalgenesis.com
7 */
8 
9 #ifndef __libutilxx__sella__net__IPPrefix_H__
10 #define __libutilxx__sella__net__IPPrefix_H__
11 
12 #include "../../common.h"
13 #include "IPAddress.h"
14 #include "AddressException.h"
15 
16 #include <netinet/in.h>
17 
18 #include <list>
19 #include <vector>
20 #include <string>
21 #include <memory>
22 #include <forward_list>
23 #include <unordered_set>
24 
25 namespace sella {
26  namespace net {
27  class IPPrefix;
28  }
29 }
30 
32  public:
33  typedef std::shared_ptr<IPPrefix> shared;
34  typedef IPPrefix *ptr;
35 
36  typedef struct {
37  size_t operator()(const IPPrefix &a) const {
38  return a.getHash();
39  }
40  } hash;
41 
42  typedef struct {
43  size_t operator()(const ptr a) const {
44  return a->getHash();
45  }
46  } ptr_hash;
47 
48  typedef struct {
49  size_t operator()(const shared &a) const {
50  return a->getHash();
51  }
52  } shared_hash;
53 
54  typedef struct {
55  bool operator()(const IPPrefix &a, const IPPrefix &b) const {
56  return (a == b);
57  }
58  } eq;
59 
60  typedef struct {
61  bool operator()(const ptr a, const ptr b) const {
62  if (a == NULL || b == NULL) {
63  return false;
64  }
65  return (*a == *b);
66  }
67  } ptr_eq;
68 
69  typedef struct {
70  bool operator()(const shared &a, const shared &b) const {
71 #ifdef KEYEQUAL_METHOD
72  const std::unordered_set<std::string> s;
73  const std::unordered_set<std::string>::key_equal &eqfn = s.key_eq();
74 
75  return eqfn(a->str(), b->str());
76 #else
77  return (*a == *b);
78 #endif
79  }
80  } shared_eq;
81 
82  typedef struct {
83  bool operator()(const IPPrefix &a, const IPPrefix &b) const {
84  return (a < b);
85  }
86  } cmp;
87 
88  typedef std::list<IPPrefix> list;
89  typedef std::list<ptr> ptr_list;
90  typedef std::list<shared> shared_list;
91  typedef std::forward_list<IPPrefix> flist;
92  typedef std::forward_list<ptr> ptr_flist;
93  typedef std::forward_list<shared> shared_flist;
94  typedef std::vector<IPPrefix> vector;
95  typedef std::vector<ptr> ptr_vector;
96  typedef std::vector<shared> shared_vector;
97  typedef std::unordered_set<IPPrefix, hash> uset;
98  typedef std::unordered_set<ptr, ptr_hash, ptr_eq> ptr_uset;
99  typedef std::unordered_set<shared, shared_hash, shared_eq> shared_uset;
100  typedef std::unordered_multiset<IPPrefix, hash> umset;
101  typedef std::unordered_multiset<ptr, ptr_hash, ptr_eq> ptr_umset;
102  typedef std::unordered_multiset<shared, shared_hash, shared_eq> shared_umset;
103 
104  public:
105  IPPrefix(const IPAddress &address, uint8_t cidr = 255) throw (AddressException);
106  IPPrefix(const std::string &address, uint8_t cidr) throw (AddressException);
107  IPPrefix(const std::string &prefix = std::string("0.0.0.0/32")) throw (AddressException);
108  IPPrefix(const IPPrefix &other) throw ();
109  IPPrefix(const IPPrefix &&other) throw ();
110  virtual ~IPPrefix() throw ();
111 
112  public:
113  IPPrefix& operator=(const IPPrefix &other) throw ();
114  IPPrefix& operator=(const IPPrefix &&other) throw ();
115 
116  bool operator==(const IPPrefix &other) const throw ();
117  bool operator!=(const IPPrefix &other) const throw ();
118  bool operator>=(const IPPrefix &other) const throw ();
119  bool operator<=(const IPPrefix &other) const throw ();
120  bool operator<(const IPPrefix &other) const throw ();
121  bool operator>(const IPPrefix &other) const throw ();
122 
123  /* Based off of PostgreSQL Network Address Operators: http://www.postgresql.org/docs/8.2/static/functions-net.html */
124  bool operator<<(const IPPrefix &other) const throw (); /* contained within */
125  bool operator<<=(const IPPrefix &other) const throw (); /* containd within or equals */
126  bool operator>>(const IPPrefix &other) const throw (); /* contains */
127  bool operator>>=(const IPPrefix &other) const throw (); /* contains or equals */
128 
129  bool operator<<(const IPAddress &other) const throw (); /* contained within */
130  bool operator<<=(const IPAddress &other) const throw (); /* containd within or equals */
131  bool operator>>(const IPAddress &other) const throw (); /* contains */
132  bool operator>>=(const IPAddress &other) const throw (); /* contains or equals */
133 
134  bool contains(const IPPrefix &other, bool orEquals = false) const throw ();
135  bool contains(const IPAddress &other, bool orEquals = false) const throw ();
136 
137  public:
138  int getFamily(void) const throw ();
139  const IPAddress& getIPAddress(void) const throw ();
140  IPAddress& getIPAddress(void) throw ();
141  uint8_t getCIDR(void) const throw ();
142  IPAddress getNetmask(void) const throw ();
143  IPAddress getInverseMask(void) const throw ();
144  IPAddress getNetwork(void) const throw ();
145  IPAddress getBroadcast(void) const throw ();
146  IPAddress::vector getIPAddresses(bool usable = true) const throw (); /* Large prefix ranges will produce very large vectors. */
147  const std::string& str(void) const throw (AddressException);
148  const char* c_str(void) const throw (AddressException);
149  uint32_t getU32(void) const throw ();
150  uint32_t getHash(void) const throw ();
151 
152  bool empty(void) const throw ();
153  void clear(void) throw ();
154 
155  static const IPPrefix::shared shared_ptr(const IPAddress &address, uint8_t cidr = 255) throw (AddressException);
156  static const IPPrefix::shared shared_ptr(const std::string &address, uint8_t cidr) throw (AddressException);
157  static const IPPrefix::shared shared_ptr(const std::string &prefix = std::string("0.0.0.0/32")) throw (AddressException);
158 
159  public:
160  static bool bi_cmp(const IPPrefix &a, const IPPrefix &b) { return (a < b); }
161  static bool shared_bi_cmp(shared &a, shared &b) { return (*a < *b); }
162  static bool shared_bi_eq(shared &a, shared &b) { return (*a == *b); }
163 
164  private:
165  IPAddress address;
166  uint8_t cidr;
167  uint32_t mask[4];
168 
169  mutable std::string scache;
170  mutable uint32_t hcache;
171 
172  void generateMask(void) throw ();
173 };
174 
175 #endif
176 
177 /*
178 ** vim: noet ts=3 sw=3
179 */