libutil++  1.9.3
 All Classes Functions Variables
IPAddressPort.h
1 /*
2 ** libutil++
3 ** $Id: IPAddressPort.h 1653 2016-02-28 19:54:59Z 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__IPAddressPort_H__
10 #define __libutilxx__sella__net__IPAddressPort_H__
11 
12 #include "../../common.h"
13 #include "IPAddress.h"
14 #include "TCPPort.h"
15 #include "UDPPort.h"
16 
17 #include <netinet/in.h>
18 
19 #include <list>
20 #include <vector>
21 #include <string>
22 #include <memory>
23 #include <forward_list>
24 #include <unordered_set>
25 
26 namespace sella {
27  namespace net {
28  class IPAddressPort;
29  }
30 }
31 
33  public:
34  typedef std::shared_ptr<IPAddressPort> shared;
35  typedef IPAddressPort *ptr;
36 
37  typedef struct {
38  size_t operator()(const IPAddressPort &a) const {
39  return a.getHash();
40  }
41  } hash;
42 
43  typedef struct {
44  size_t operator()(const ptr a) const {
45  return a->getHash();
46  }
47  } ptr_hash;
48 
49  typedef struct {
50  size_t operator()(const shared &a) const {
51  return a->getHash();
52  }
53  } shared_hash;
54 
55  typedef struct {
56  bool operator()(const ptr a, const IPAddressPort *b) const {
57  if (a == NULL || b == NULL) {
58  return false;
59  }
60  return (*a == *b);
61  }
62  } ptr_eq;
63 
64  typedef struct {
65  bool operator()(const shared &a, const shared &b) const {
66 #ifdef KEYEQUAL_METHOD
67  const std::unordered_set<std::string> s;
68  const std::unordered_set<std::string>::key_equal &eqfn = s.key_eq();
69 
70  return eqfn(a->str(), b->str());
71 #else
72  return (*a == *b);
73 #endif
74  }
75  } shared_eq;
76 
77  typedef std::list<IPAddressPort> list;
78  typedef std::list<ptr> ptr_list;
79  typedef std::list<shared> shared_list;
80  typedef std::forward_list<IPAddressPort> flist;
81  typedef std::forward_list<ptr> ptr_flist;
82  typedef std::forward_list<shared> shared_flist;
83  typedef std::vector<IPAddressPort> vector;
84  typedef std::vector<ptr> ptr_vector;
85  typedef std::vector<shared> shared_vector;
86  typedef std::unordered_set<IPAddressPort, hash> uset;
87  typedef std::unordered_set<ptr, ptr_hash, ptr_eq> ptr_uset;
88  typedef std::unordered_set<shared, shared_hash, shared_eq> shared_uset;
89  typedef std::unordered_multiset<IPAddressPort, hash> umset;
90  typedef std::unordered_multiset<ptr, ptr_hash, ptr_eq> ptr_umset;
91  typedef std::unordered_multiset<shared, shared_hash, shared_eq> shared_umset;
92 
93  public:
94  IPAddressPort(const IPAddress &address, const Port &port) throw ();
95  IPAddressPort(const IPAddress &address, const TCPPort &port) throw ();
96  IPAddressPort(const IPAddress &address, const UDPPort &port) throw ();
97  IPAddressPort(const IPAddress &address, uint16_t port) throw (PortException);
98  IPAddressPort(const std::string &address = std::string("0.0.0.0"), uint16_t port = 0) throw (AddressException, PortException);
99  IPAddressPort(const struct sockaddr_storage *ss) throw (AddressException, PortException);
100  IPAddressPort(const struct sockaddr *sa) throw (AddressException, PortException);
101  IPAddressPort(const IPAddressPort &other) throw ();
102  IPAddressPort(const IPAddressPort &&other) throw ();
103  virtual ~IPAddressPort() throw ();
104 
105  public:
106  IPAddressPort& operator = (const IPAddressPort &other) throw ();
107  IPAddressPort& operator = (const IPAddressPort &&other) throw ();
108 
109  bool operator == (const IPAddressPort &other) const throw ();
110  bool operator != (const IPAddressPort &other) const throw ();
111  bool operator >= (const IPAddressPort &other) const throw ();
112  bool operator <= (const IPAddressPort &other) const throw ();
113  bool operator < (const IPAddressPort &other) const throw ();
114  bool operator > (const IPAddressPort &other) const throw ();
115 
116  public:
117  int getFamily(void) const throw ();
118  const IPAddress& getIPAddress(void) const throw ();
119  const Port& getPort(void) const throw ();
120  const struct sockaddr_storage& getSockaddrStorage(void) const throw ();
121  const struct sockaddr& getSockaddr(void) const throw ();
122  const struct sockaddr_in& getSockaddrIn(void) const throw ();
123  const struct sockaddr_in6& getSockaddrIn6(void) const throw ();
124 
125  bool hasIPAddress(void) const throw ();
126  bool hasPort(void) const throw ();
127 
128  const std::string& str(void) const throw (AddressException);
129  const char* c_str(void) const throw (AddressException);
130  uint32_t getU32(void) const throw ();
131  uint32_t getHash(void) const throw ();
132 
133  bool empty(void) const throw ();
134 
135  static const IPAddressPort::shared shared_ptr(const IPAddress &address, const Port &port) throw ();
136  static const IPAddressPort::shared shared_ptr(const IPAddress &address, const TCPPort &port) throw ();
137  static const IPAddressPort::shared shared_ptr(const IPAddress &address, const UDPPort &port) throw ();
138  static const IPAddressPort::shared shared_ptr(const IPAddress &address, uint16_t port) throw (PortException);
139  static const IPAddressPort::shared shared_ptr(const std::string &address = std::string("0.0.0.0"), uint16_t port = 0) throw (AddressException, PortException);
140  static const IPAddressPort::shared shared_ptr(const struct sockaddr_storage *ss) throw (AddressException, PortException);
141  static const IPAddressPort::shared shared_ptr(const struct sockaddr *sa) throw (AddressException, PortException);
142 
143  public:
144  static bool shared_bi_cmp(shared &a, shared &b) { return (*a < *b); }
145  static bool shared_bi_eq(shared &a, shared &b) { return (*a == *b); }
146 
147  private:
148  IPAddress address;
149  Port port;
150 
151  union __sockaddr_union {
152  struct sockaddr_storage ss;
153  struct sockaddr sa;
154  struct sockaddr_in sa_in;
155  struct sockaddr_in6 sa_in6;
156  } u;
157 
158  mutable std::string scache;
159  mutable uint32_t hcache;
160 
161  void init(const IPAddress &address, const Port &port) throw ();
162 };
163 
164 #endif
165 
166 /*
167 ** vim: noet ts=3 sw=3
168 */