00001
00033 #ifndef LL_LLHOST_H
00034 #define LL_LLHOST_H
00035
00036 #include <iostream>
00037 #include <string>
00038
00039 #include "net.h"
00040
00041 #include "llstring.h"
00042
00043 const U32 INVALID_PORT = 0;
00044 const U32 INVALID_HOST_IP_ADDRESS = 0x0;
00045
00046 class LLHost {
00047 protected:
00048 U32 mPort;
00049 U32 mIP;
00050 public:
00051
00052 static LLHost invalid;
00053
00054
00055 LLHost()
00056 : mPort(INVALID_PORT),
00057 mIP(INVALID_HOST_IP_ADDRESS)
00058 { }
00059
00060 LLHost( U32 ipv4_addr, U32 port )
00061 : mPort( port )
00062 {
00063 mIP = ipv4_addr;
00064 }
00065
00066 LLHost( const char *ipv4_addr, U32 port )
00067 : mPort( port )
00068 {
00069 mIP = ip_string_to_u32(ipv4_addr);
00070 }
00071
00072 explicit LLHost(const U64 ip_port)
00073 {
00074 U32 ip = (U32)(ip_port >> 32);
00075 U32 port = (U32)(ip_port & (U64)0xFFFFFFFF);
00076 mIP = ip;
00077 mPort = port;
00078 }
00079
00080 explicit LLHost(const std::string& ip_and_port);
00081
00082 ~LLHost()
00083 { }
00084
00085
00086 void set( U32 ip, U32 port ) { mIP = ip; mPort = port; }
00087 void set( const char* ipstr, U32 port ) { mIP = ip_string_to_u32(ipstr); mPort = port; }
00088 void setAddress( const char* ipstr ) { mIP = ip_string_to_u32(ipstr); }
00089 void setAddress( U32 ip ) { mIP = ip; }
00090 void setPort( U32 port ) { mPort = port; }
00091 BOOL setHostByName(const char *hname);
00092
00093 LLHost& operator=(const LLHost &rhs);
00094 void invalidate() { mIP = INVALID_HOST_IP_ADDRESS; mPort = INVALID_PORT;};
00095
00096
00097 U32 getAddress() const { return mIP; }
00098 U32 getPort() const { return mPort; }
00099 BOOL isOk() const { return (mIP != INVALID_HOST_IP_ADDRESS) && (mPort != INVALID_PORT); }
00100 size_t hash() const { return (mIP << 16) | (mPort & 0xffff); }
00101 void getString(char* buffer, S32 length) const;
00102 void getIPString(char* buffer, S32 length) const;
00103 std::string getIPString() const;
00104 void getHostName(char *buf, S32 len) const;
00105 LLString getHostName() const;
00106 std::string getIPandPort() const;
00107
00108 friend std::ostream& operator<< (std::ostream& os, const LLHost &hh);
00109
00110
00111
00112
00113
00114 friend bool operator==( const LLHost &lhs, const LLHost &rhs );
00115 friend bool operator!=( const LLHost &lhs, const LLHost &rhs );
00116 friend bool operator<(const LLHost &lhs, const LLHost &rhs);
00117 };
00118
00119
00120
00121 class LLHostHash
00122 {
00123 public:
00124 size_t operator() (const LLHost &hh) const { return hh.hash(); }
00125 };
00126
00127
00128 inline bool operator==( const LLHost &lhs, const LLHost &rhs )
00129 {
00130 return (lhs.mIP == rhs.mIP) && (lhs.mPort == rhs.mPort);
00131 }
00132
00133 inline bool operator!=( const LLHost &lhs, const LLHost &rhs )
00134 {
00135 return (lhs.mIP != rhs.mIP) || (lhs.mPort != rhs.mPort);
00136 }
00137
00138 inline bool operator<(const LLHost &lhs, const LLHost &rhs)
00139 {
00140 if (lhs.mIP < rhs.mIP)
00141 {
00142 return true;
00143 }
00144 if (lhs.mIP > rhs.mIP)
00145 {
00146 return false;
00147 }
00148
00149 if (lhs.mPort < rhs.mPort)
00150 {
00151 return true;
00152 }
00153 else
00154 {
00155 return false;
00156 }
00157 }
00158
00159
00160 #endif // LL_LLHOST_H