00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef __libutilxx__sella__net__IPAddressPort_H__
00010 #define __libutilxx__sella__net__IPAddressPort_H__
00011
00012 #include "../../common.h"
00013 #include "IPAddress.h"
00014 #include "TCPPort.h"
00015 #include "UDPPort.h"
00016
00017 #include <netinet/in.h>
00018
00019 #include <list>
00020 #include <vector>
00021 #include <string>
00022 #include <memory>
00023 #include <forward_list>
00024 #include <unordered_set>
00025
00026 namespace sella {
00027 namespace net {
00028 class IPAddressPort;
00029 }
00030 }
00031
00032 class sella::net::IPAddressPort {
00033 public:
00034 typedef std::shared_ptr<IPAddressPort> shared;
00035 typedef IPAddressPort *ptr;
00036
00037 typedef struct {
00038 size_t operator()(const IPAddressPort &a) const {
00039 const std::unordered_set<uint32_t> s;
00040 const std::unordered_set<uint32_t>::hasher &hfn = s.hash_function();
00041
00042 return hfn(a.getHash());
00043 }
00044 } hash;
00045
00046 typedef struct {
00047 size_t operator()(const ptr a) const {
00048 const std::unordered_set<uint32_t> s;
00049 const std::unordered_set<uint32_t>::hasher &hfn = s.hash_function();
00050
00051 return hfn(a->getHash());
00052 }
00053 } ptr_hash;
00054
00055 typedef struct {
00056 size_t operator()(const shared &a) const {
00057 const std::unordered_set<uint32_t> s;
00058 const std::unordered_set<uint32_t>::hasher &hfn = s.hash_function();
00059
00060 return hfn(a->getHash());
00061 }
00062 } shared_hash;
00063
00064 typedef struct {
00065 bool operator()(const ptr a, const IPAddressPort *b) const {
00066 if (a == NULL || b == NULL) {
00067 return false;
00068 }
00069 return (*a == *b);
00070 }
00071 } ptr_eq;
00072
00073 typedef struct {
00074 bool operator()(const shared &a, const shared &b) const {
00075 #ifdef KEYEQUAL_METHOD
00076 const std::unordered_set<std::string> s;
00077 const std::unordered_set<std::string>::key_equal &eqfn = s.key_eq();
00078
00079 return eqfn(a->str(), b->str());
00080 #else
00081 return (*a == *b);
00082 #endif
00083 }
00084 } shared_eq;
00085
00086 typedef std::list<IPAddressPort> list;
00087 typedef std::list<ptr> ptr_list;
00088 typedef std::list<shared> shared_list;
00089 typedef std::forward_list<IPAddressPort> flist;
00090 typedef std::forward_list<ptr> ptr_flist;
00091 typedef std::forward_list<shared> shared_flist;
00092 typedef std::vector<IPAddressPort> vector;
00093 typedef std::vector<ptr> ptr_vector;
00094 typedef std::vector<shared> shared_vector;
00095 typedef std::unordered_set<IPAddressPort, hash> uset;
00096 typedef std::unordered_set<ptr, ptr_hash, ptr_eq> ptr_uset;
00097 typedef std::unordered_set<shared, shared_hash, shared_eq> shared_uset;
00098 typedef std::unordered_multiset<IPAddressPort, hash> umset;
00099 typedef std::unordered_multiset<ptr, ptr_hash, ptr_eq> ptr_umset;
00100 typedef std::unordered_multiset<shared, shared_hash, shared_eq> shared_umset;
00101
00102 public:
00103 IPAddressPort(const IPAddress &address, const Port &port) throw ();
00104 IPAddressPort(const IPAddress &address, const TCPPort &port) throw ();
00105 IPAddressPort(const IPAddress &address, const UDPPort &port) throw ();
00106 IPAddressPort(const IPAddress &address, uint16_t port) throw (PortException);
00107 IPAddressPort(const std::string &address = std::string("0.0.0.0"), uint16_t port = 0) throw (AddressException, PortException);
00108 IPAddressPort(const struct sockaddr_storage *ss) throw (AddressException, PortException);
00109 IPAddressPort(const struct sockaddr *sa) throw (AddressException, PortException);
00110 IPAddressPort(const IPAddressPort &other) throw ();
00111 IPAddressPort(const IPAddressPort &&other) throw ();
00112 virtual ~IPAddressPort() throw ();
00113
00114 public:
00115 IPAddressPort& operator = (const IPAddressPort &other) throw ();
00116 IPAddressPort& operator = (const IPAddressPort &&other) throw ();
00117
00118 bool operator == (const IPAddressPort &other) const throw ();
00119 bool operator != (const IPAddressPort &other) const throw ();
00120 bool operator >= (const IPAddressPort &other) const throw ();
00121 bool operator <= (const IPAddressPort &other) const throw ();
00122 bool operator < (const IPAddressPort &other) const throw ();
00123 bool operator > (const IPAddressPort &other) const throw ();
00124
00125 public:
00126 int getFamily(void) const throw ();
00127 const IPAddress& getIPAddress(void) const throw ();
00128 const Port& getPort(void) const throw ();
00129 const struct sockaddr_storage& getSockaddrStorage(void) const throw ();
00130 const struct sockaddr& getSockaddr(void) const throw ();
00131 const struct sockaddr_in& getSockaddrIn(void) const throw ();
00132 const struct sockaddr_in6& getSockaddrIn6(void) const throw ();
00133
00134 bool hasIPAddress(void) const throw ();
00135 bool hasPort(void) const throw ();
00136
00137 const std::string& str(void) const throw (AddressException);
00138 const char* c_str(void) const throw (AddressException);
00139 uint32_t getU32(void) const throw ();
00140 uint32_t getHash(void) const throw ();
00141
00142 bool empty(void) const throw ();
00143
00144 static const IPAddressPort::shared shared_ptr(const IPAddress &address, const Port &port) throw ();
00145 static const IPAddressPort::shared shared_ptr(const IPAddress &address, const TCPPort &port) throw ();
00146 static const IPAddressPort::shared shared_ptr(const IPAddress &address, const UDPPort &port) throw ();
00147 static const IPAddressPort::shared shared_ptr(const IPAddress &address, uint16_t port) throw (PortException);
00148 static const IPAddressPort::shared shared_ptr(const std::string &address = std::string("0.0.0.0"), uint16_t port = 0) throw (AddressException, PortException);
00149 static const IPAddressPort::shared shared_ptr(const struct sockaddr_storage *ss) throw (AddressException, PortException);
00150 static const IPAddressPort::shared shared_ptr(const struct sockaddr *sa) throw (AddressException, PortException);
00151
00152 public:
00153 static bool shared_bi_cmp(shared &a, shared &b) { return (*a < *b); }
00154 static bool shared_bi_eq(shared &a, shared &b) { return (*a == *b); }
00155
00156 private:
00157 IPAddress address;
00158 Port port;
00159
00160 union __sockaddr_union {
00161 struct sockaddr_storage ss;
00162 struct sockaddr sa;
00163 struct sockaddr_in sa_in;
00164 struct sockaddr_in6 sa_in6;
00165 } u;
00166
00167 mutable std::string scache;
00168 mutable uint32_t hcache;
00169
00170 void init(const IPAddress &address, const Port &port) throw ();
00171 };
00172
00173 #endif
00174
00175
00176
00177