00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef __libutilxx__sella__net__Port_H__
00010 #define __libutilxx__sella__net__Port_H__
00011
00012 #include "../../common.h"
00013 #include "PortException.h"
00014
00015 #include <sys/socket.h>
00016 #include <stdint.h>
00017
00018 #include <list>
00019 #include <vector>
00020 #include <string>
00021 #include <memory>
00022 #include <forward_list>
00023 #include <unordered_set>
00024
00025 namespace sella {
00026 namespace net {
00027 class Port;
00028 }
00029 }
00030
00031 class sella::net::Port {
00032 public:
00033 typedef std::shared_ptr<Port> shared;
00034 typedef Port *ptr;
00035
00036 typedef struct {
00037 size_t operator()(const Port &a) const {
00038 const std::unordered_set<uint32_t> s;
00039 const std::unordered_set<uint32_t>::hasher &hfn = s.hash_function();
00040
00041 return hfn(a.getHash());
00042 }
00043 } hash;
00044
00045 typedef struct {
00046 size_t operator()(const ptr a) const {
00047 const std::unordered_set<uint32_t> s;
00048 const std::unordered_set<uint32_t>::hasher &hfn = s.hash_function();
00049
00050 return hfn(a->getHash());
00051 }
00052 } ptr_hash;
00053
00054 typedef struct {
00055 size_t operator()(const shared &a) const {
00056 const std::unordered_set<uint32_t> s;
00057 const std::unordered_set<uint32_t>::hasher &hfn = s.hash_function();
00058
00059 return hfn(a->getHash());
00060 }
00061 } shared_hash;
00062
00063 typedef struct {
00064 bool operator()(const ptr a, const ptr b) const {
00065 if (a == NULL || b == NULL) {
00066 return false;
00067 }
00068 return (*a == *b);
00069 }
00070 } ptr_eq;
00071
00072 typedef struct {
00073 bool operator()(const shared &a, const shared &b) const {
00074 return (*a == *b);
00075 }
00076 } shared_eq;
00077
00078 typedef std::list<Port> list;
00079 typedef std::list<ptr> ptr_list;
00080 typedef std::list<shared> shared_list;
00081 typedef std::forward_list<Port> flist;
00082 typedef std::forward_list<ptr> ptr_flist;
00083 typedef std::forward_list<shared> shared_flist;
00084 typedef std::vector<Port> vector;
00085 typedef std::vector<ptr> ptr_vector;
00086 typedef std::vector<shared> shared_vector;
00087 typedef std::unordered_set<Port, hash> uset;
00088 typedef std::unordered_set<ptr, hash> ptr_uset;
00089 typedef std::unordered_set<shared, hash> shared_uset;
00090 typedef std::unordered_multiset<Port, hash> umset;
00091 typedef std::unordered_multiset<ptr, hash> ptr_umset;
00092 typedef std::unordered_multiset<shared, hash> shared_umset;
00093
00094 public:
00095 static const int UDPPreferred = 0x01;
00096 static const int UDPRequired = 0x03;
00097
00098 static const int TCPPreferred = 0x04;
00099 static const int TCPRequired = 0x12;
00100
00101 static const int UDPProtocol = SOCK_DGRAM;
00102 static const int TCPProtocol = SOCK_STREAM;
00103
00104 public:
00105 Port(const std::string &service, int protocol = TCPPreferred) throw (PortException);
00106 Port(uint16_t port = 0, int protocol = TCPPreferred) throw (PortException);
00107 Port(const struct sockaddr_storage *ss, int protocol = TCPPreferred) throw (PortException);
00108 Port(const struct sockaddr *sa, int protocol = TCPPreferred) throw (PortException);
00109
00110 Port(const Port &other) throw ();
00111 Port(const Port &&other) throw ();
00112 virtual ~Port() throw ();
00113
00114 public:
00115 Port & operator = (const Port &other) throw ();
00116 Port & operator = (const Port &&other) throw ();
00117
00118 bool operator == (const Port &other) const throw ();
00119 bool operator != (const Port &other) const throw ();
00120 bool operator >= (const Port &other) const throw ();
00121 bool operator <= (const Port &other) const throw ();
00122 bool operator < (const Port &other) const throw ();
00123 bool operator > (const Port &other) const throw ();
00124
00125 public:
00126 uint16_t get(void) const throw ();
00127 uint16_t getPort(void) const throw ();
00128 int getProtocol(void) const throw ();
00129 const std::string& str(void) const throw ();
00130 const char* c_str(void) const throw ();
00131 uint32_t getU32(void) const throw ();
00132 uint32_t getHash(void) const throw ();
00133
00134 bool empty(void) const throw ();
00135 void clear(void) throw ();
00136
00137 static const Port::shared shared_ptr(const std::string &service, int protocol = TCPPreferred) throw (PortException);
00138 static const Port::shared shared_ptr(uint16_t port = 0, int protocol = TCPPreferred) throw (PortException);
00139 static const Port::shared shared_ptr(const struct sockaddr_storage *ss, int protocol = TCPPreferred) throw (PortException);
00140 static const Port::shared shared_ptr(const struct sockaddr *sa, int protocol = TCPPreferred) throw (PortException);
00141
00142 public:
00143 static bool shared_bi_cmp(shared &a, shared &b) { return (*a < *b); }
00144 static bool shared_bi_eq(shared &a, shared &b) { return (*a == *b); }
00145
00146 protected:
00147 bool parseUDPPreferred(uint16_t &port, int &protocol, const std::string &service) throw ();
00148 bool parseTCPPreferred(uint16_t &port, int &protocol, const std::string &service) throw ();
00149 bool parseUDPRequired(uint16_t &port, int &protocol, const std::string &service) throw ();
00150 bool parseTCPRequired(uint16_t &port, int &protocol, const std::string &service) throw ();
00151
00152 private:
00153 uint16_t port;
00154 int protocol;
00155
00156 mutable std::string scache;
00157 mutable uint32_t hcache;
00158 };
00159
00160 #endif
00161
00162
00163
00164