libutil++  1.9.3
 All Classes Functions Variables
IPAddress.h
1 /*
2 ** libutil++
3 ** $Id: IPAddress.h 1901 2017-06-24 20:03:54Z 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__IPAddress_H__
10 #define __libutilxx__sella__net__IPAddress_H__
11 
12 #include "../../common.h"
13 #include "AddressException.h"
14 
15 #include <arpa/inet.h>
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 IPAddress;
28  }
29 }
30 
32  friend class IPPrefix;
33 
34  public:
35  typedef std::shared_ptr<IPAddress> shared;
36  typedef IPAddress *ptr;
37 
38  typedef struct {
39  size_t operator()(const IPAddress &a) const {
40  return a.getHash();
41  }
42  } hash;
43 
44  typedef struct {
45  size_t operator()(const ptr a) const {
46  return a->getHash();
47  }
48  } ptr_hash;
49 
50  typedef struct {
51  size_t operator()(const shared &a) const {
52  return a->getHash();
53  }
54  } shared_hash;
55 
56  typedef struct {
57  bool operator()(const IPAddress &a, const IPAddress &b) const {
58  return (a == b);
59  }
60  } eq;
61 
62  typedef struct {
63  bool operator()(const ptr a, const ptr b) const {
64  if (a == NULL || b == NULL) {
65  return false;
66  }
67  return (*a == *b);
68  }
69  } ptr_eq;
70 
71  typedef struct {
72  bool operator()(const shared &a, const shared &b) const {
73  return (*a == *b);
74  }
75  } shared_eq;
76 
77  typedef struct {
78  bool operator()(const IPAddress &a, const IPAddress &b) const {
79  return (a < b);
80  }
81  } cmp;
82 
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;
98 
99  public:
100  static const int IPv4Preferred = 0x01;
101  static const int IPv4Required = 0x03;
102 
103  static const int IPv6Preferred = 0x04;
104  static const int IPv6Required = 0x12;
105 
106  static const int IPv4Family = AF_INET;
107  static const int IPv6Family = AF_INET6;
108 
109  public:
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);
118 
119  IPAddress(const IPAddress &other) throw ();
120  IPAddress(const IPAddress &&other) throw ();
121  virtual ~IPAddress() throw ();
122 
123  public:
124  IPAddress& operator=(const IPAddress &other) throw ();
125  IPAddress& operator=(const IPAddress &&other) throw ();
126 
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 ();
133 
134  IPAddress operator&(const IPAddress &other) const throw (AddressException);
135  IPAddress operator|(const IPAddress &other) const throw (AddressException);
136  IPAddress operator^(const IPAddress &other) const throw (AddressException);
137  IPAddress operator~() const throw ();
138 
139  IPAddress& operator+=(uint32_t increment) throw ();
140  IPAddress operator+(uint32_t increment) const throw ();
141  IPAddress operator++(int) throw ();
142  IPAddress& operator++() throw ();
143 
144  IPAddress& operator-=(uint32_t increment) throw ();
145  IPAddress operator-(uint32_t increment) const throw ();
146  IPAddress operator--(int) throw ();
147  IPAddress& operator--() throw ();
148 
149  public:
150  void applyCIDR(uint8_t cidr) throw (AddressException);
151 
152  int getFamily(void) const throw ();
153  size_t getAddressStructureSize(void) const throw ();
154 
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 ();
161 
162  bool empty(void) const throw ();
163  void clear(void) throw ();
164 
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);
173 
174  public:
175  static bool bi_cmp(const IPAddress &a, const IPAddress &b) { return (a < b); }
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); }
178 
179  protected:
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 ();
184 
185  private:
186  int family;
187 
188  union {
189  struct in_addr v4;
190  struct in6_addr v6;
191  } addr;
192 
193  mutable std::string scache;
194  mutable uint32_t hcache;
195 };
196 
197 #endif
198 
199 /*
200 ** vim: noet ts=3 sw=3
201 */