libutil++  1.9.3
 All Classes Functions Variables
Port.h
1 /*
2 ** libutil++
3 ** $Id: Port.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__Port_H__
10 #define __libutilxx__sella__net__Port_H__
11 
12 #include "../../common.h"
13 #include "PortException.h"
14 
15 #include <sys/socket.h>
16 #include <stdint.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 Port;
28  }
29 }
30 
32  public:
33  typedef std::shared_ptr<Port> shared;
34  typedef Port *ptr;
35 
36  typedef struct {
37  size_t operator()(const Port &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 ptr a, const ptr b) const {
56  if (a == NULL || b == NULL) {
57  return false;
58  }
59  return (*a == *b);
60  }
61  } ptr_eq;
62 
63  typedef struct {
64  bool operator()(const shared &a, const shared &b) const {
65  return (*a == *b);
66  }
67  } shared_eq;
68 
69  typedef std::list<Port> list;
70  typedef std::list<ptr> ptr_list;
71  typedef std::list<shared> shared_list;
72  typedef std::forward_list<Port> flist;
73  typedef std::forward_list<ptr> ptr_flist;
74  typedef std::forward_list<shared> shared_flist;
75  typedef std::vector<Port> vector;
76  typedef std::vector<ptr> ptr_vector;
77  typedef std::vector<shared> shared_vector;
78  typedef std::unordered_set<Port, hash> uset;
79  typedef std::unordered_set<ptr, hash> ptr_uset;
80  typedef std::unordered_set<shared, hash> shared_uset;
81  typedef std::unordered_multiset<Port, hash> umset;
82  typedef std::unordered_multiset<ptr, hash> ptr_umset;
83  typedef std::unordered_multiset<shared, hash> shared_umset;
84 
85  public:
86  static const int UDPPreferred = 0x01;
87  static const int UDPRequired = 0x03;
88 
89  static const int TCPPreferred = 0x04;
90  static const int TCPRequired = 0x12;
91 
92  static const int UDPProtocol = SOCK_DGRAM;
93  static const int TCPProtocol = SOCK_STREAM;
94 
95  public:
96  Port(const std::string &service, int protocol = TCPPreferred) throw (PortException);
97  Port(uint16_t port = 0, int protocol = TCPPreferred) throw (PortException);
98  Port(const struct sockaddr_storage *ss, int protocol = TCPPreferred) throw (PortException);
99  Port(const struct sockaddr *sa, int protocol = TCPPreferred) throw (PortException);
100 
101  Port(const Port &other) throw ();
102  Port(const Port &&other) throw ();
103  virtual ~Port() throw ();
104 
105  public:
106  Port & operator = (const Port &other) throw ();
107  Port & operator = (const Port &&other) throw ();
108 
109  bool operator == (const Port &other) const throw ();
110  bool operator != (const Port &other) const throw ();
111  bool operator >= (const Port &other) const throw ();
112  bool operator <= (const Port &other) const throw ();
113  bool operator < (const Port &other) const throw ();
114  bool operator > (const Port &other) const throw ();
115 
116  public:
117  uint16_t get(void) const throw ();
118  uint16_t getPort(void) const throw ();
119  int getProtocol(void) const throw ();
120  const std::string& str(void) const throw ();
121  const char* c_str(void) const throw ();
122  uint32_t getU32(void) const throw ();
123  uint32_t getHash(void) const throw ();
124 
125  bool empty(void) const throw ();
126  void clear(void) throw ();
127 
128  static const Port::shared shared_ptr(const std::string &service, int protocol = TCPPreferred) throw (PortException);
129  static const Port::shared shared_ptr(uint16_t port = 0, int protocol = TCPPreferred) throw (PortException);
130  static const Port::shared shared_ptr(const struct sockaddr_storage *ss, int protocol = TCPPreferred) throw (PortException);
131  static const Port::shared shared_ptr(const struct sockaddr *sa, int protocol = TCPPreferred) throw (PortException);
132 
133  public:
134  static bool shared_bi_cmp(shared &a, shared &b) { return (*a < *b); }
135  static bool shared_bi_eq(shared &a, shared &b) { return (*a == *b); }
136 
137  protected:
138  bool parseUDPPreferred(uint16_t &port, int &protocol, const std::string &service) throw ();
139  bool parseTCPPreferred(uint16_t &port, int &protocol, const std::string &service) throw ();
140  bool parseUDPRequired(uint16_t &port, int &protocol, const std::string &service) throw ();
141  bool parseTCPRequired(uint16_t &port, int &protocol, const std::string &service) throw ();
142 
143  private:
144  uint16_t port;
145  int protocol;
146 
147  mutable std::string scache;
148  mutable uint32_t hcache;
149 };
150 
151 #endif
152 
153 /*
154 ** vim: noet ts=3 sw=3
155 */