9 #ifndef __libutilxx__sella__net__Port_H__
10 #define __libutilxx__sella__net__Port_H__
12 #include "../../common.h"
13 #include "PortException.h"
15 #include <sys/socket.h>
22 #include <forward_list>
23 #include <unordered_set>
33 typedef std::shared_ptr<Port> shared;
37 size_t operator()(
const Port &a)
const {
43 size_t operator()(
const ptr a)
const {
49 size_t operator()(
const shared &a)
const {
55 bool operator()(
const ptr a,
const ptr b)
const {
56 if (a == NULL || b == NULL) {
64 bool operator()(
const shared &a,
const shared &b)
const {
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;
86 static const int UDPPreferred = 0x01;
87 static const int UDPRequired = 0x03;
89 static const int TCPPreferred = 0x04;
90 static const int TCPRequired = 0x12;
92 static const int UDPProtocol = SOCK_DGRAM;
93 static const int TCPProtocol = SOCK_STREAM;
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);
103 virtual ~
Port() throw ();
106 Port & operator = (const
Port &other) throw ();
107 Port & operator = (const
Port &&other) throw ();
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 ();
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 ();
125 bool empty(
void) const throw ();
126 void clear(
void) throw ();
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);
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); }
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 ();
147 mutable std::string scache;
148 mutable uint32_t hcache;