$search
00001 00002 #ifndef CASTOR_NET_NETADDRESS_H 00003 #define CASTOR_NET_NETADDRESS_H 1 00004 00005 #include <set> 00006 #include <string> 00007 #include <vector> 00008 #include <asio.hpp> 00009 #include <boost/algorithm/string.hpp> 00010 00011 #include "Exception.h" 00012 #include "Types.h" 00013 00014 /* 00015 * Spica addressing scheme 00016 * 00017 * Generic: <spica-specific> 00018 * <protocol>://<address>:<explicit port> (0x00) 00019 * 00020 * Spica-specific: <spica-specific> 00021 * spica+<protocol>://<address> (0x01) 00022 * spica+<protocol>://<address>[:<spica port>] (0x02) 00023 * spica+<protocol>://<address>[/cidr] (0x03) 00024 * 00025 * 00026 * Layout: 00027 * 00028 * <uchar:spica-specific><uchar:protocol><ushort:port><uchar:address-length><uchar*:address> 00029 * 00030 * 0x00 <uchar:protocol> <ushort:port> <uchar:address-length> <uchar*:address> 00031 * 0x01 <uchar:protocol> <uchar:address-length> <uchar*:address> 00032 * 0x02 <uchar:protocol> <ushort:port> <uchar:address-length> <uchar*:address> 00033 * 0x03 <uchar:protocol> <uchar:cidr> <uchar:address-length> <uchar*:address> 00034 * 00035 */ 00036 00037 namespace castor { namespace net { 00038 00039 class NetAddress; 00040 00041 typedef boost::shared_ptr<NetAddress> NetAddressPtr; 00042 00043 enum SpicaSpecific { 00044 00045 ss_none = 0x00, 00046 ss_plain = 0x01, 00047 ss_host = 0x02, 00048 ss_network = 0x03, 00049 ss_undecided = 0x04, 00050 }; 00051 00052 class NetAddress { 00053 00054 private: 00055 00056 SpicaSpecific spicaSpecific; 00057 unsigned char protocol; 00058 unsigned short port; 00059 unsigned char cidr; 00060 asio::ip::address address; 00061 bool empty; 00062 00063 protected: 00064 00065 NetAddress(SpicaSpecific spicaSpecific, unsigned char protocol, 00066 const asio::ip::address &address, unsigned short port, 00067 unsigned char cidr); 00068 00069 public: 00070 00071 NetAddress(); 00072 NetAddress(const std::string spec); 00073 00074 static NetAddressPtr create(const std::string spec) throw(Exception); 00075 static NetAddressPtr create(SpicaSpecific ss, unsigned char protocol, const asio::ip::address &address, unsigned short port); 00076 static NetAddressPtr create(unsigned char protocol, const asio::ip::address &address, unsigned short port); 00077 static NetAddressPtr createPlain(unsigned char protocol, const asio::ip::address &address); 00078 static NetAddressPtr createHost(unsigned char protocol, const asio::ip::address &address, unsigned short port); 00079 static NetAddressPtr createNetwork(unsigned char protocol, const asio::ip::address &address, unsigned char cidr); 00080 static NetAddressPtr create(SpicaSpecific ss, const asio::ip::tcp::endpoint &endpoint); 00081 static NetAddressPtr create(SpicaSpecific ss, const asio::ip::udp::endpoint &endpoint); 00082 static NetAddressPtr create(const BytesPtr bytes) throw(); 00083 static NetAddressPtr create(const char *data, size_t size) throw(); 00084 00085 NetAddress(const NetAddress &other); 00086 NetAddress(const BytesPtr bytes); 00087 NetAddress(const asio::ip::address &address); 00088 NetAddress(SpicaSpecific ss, const asio::ip::tcp::endpoint &endpoint); 00089 NetAddress(SpicaSpecific ss, const asio::ip::udp::endpoint &endpoint); 00090 00091 void setSpicaSpecific(SpicaSpecific value); 00092 SpicaSpecific getSpicaSpecific() const; 00093 bool isSpicaSpecific() const; 00094 bool isPlain() const; 00095 bool isHost() const; 00096 bool isNetwork() const; 00097 00098 bool isAny() const; 00099 00100 bool isEmpty() const; 00101 00102 bool isLoopback() const; 00103 bool isMulticast() const; 00104 00105 void setProtocol(unsigned char value); 00106 unsigned char getProtocol() const; 00107 00108 void setPort(unsigned short value); 00109 00110 unsigned short getPort() const; 00111 unsigned char getCidr() const; 00112 00113 void setAddress(const asio::ip::address &address); 00114 const asio::ip::address &getAddress() const; 00115 00116 BytesPtr getBytes() const; 00117 00118 std::string str() const; 00119 00120 static std::string protocol_to_string(int protocol); 00121 00122 static unsigned char string_to_protocol(const std::string protocol); 00123 00124 bool equals(const NetAddress &other) const; 00125 00126 NetAddress &operator=(const NetAddress &other); 00127 00128 friend bool operator==(const NetAddress &one, const NetAddress &other); 00129 friend bool operator!=(const NetAddress &one, const NetAddress &other); 00130 friend bool operator<(const NetAddress &one, const NetAddress &other); 00131 00132 bool similarLess(const NetAddress &y, bool checkProtocol = false) const; 00133 bool sameScope(const NetAddress &y) const; 00134 00135 protected: 00136 00137 void parse(const char *data, size_t size) throw(); 00138 }; 00139 00140 struct NetAddressPtrLess { //: public std::binary_function<const NetAddressPtr &, const NetAddressPtr &, bool> { 00141 bool operator()(const NetAddressPtr &x, const NetAddressPtr &y) { 00142 return (*x < *y); 00143 } 00144 }; 00145 00146 typedef std::set<NetAddressPtr, NetAddressPtrLess> NetAddressPtrSet; 00147 typedef std::multiset<NetAddressPtr, NetAddressPtrLess> NetAddressPtrMultiSet; 00148 typedef std::set<NetAddress> NetAddressSet; 00149 00150 typedef std::vector<NetAddressPtr> NetAddressPtrList; 00151 00152 } } 00153 00154 std::ostream &operator<<(std::ostream &os, const castor::net::NetAddress &x); 00155 00156 #endif /* CASTOR_NET_NETADDRESS_H */