00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "nhostaddress.h"
00023
00024 NHostAddress::NHostAddress(void)
00025 : NObject(),
00026 m_addr("0.0.0.0"),
00027 m_host()
00028 {
00029 WORD version;
00030 WSADATA wsaData;
00031
00032 memset(&m_host, 0, sizeof(m_host));
00033
00034 version = MAKEWORD(2 , 2);
00035
00036 if (WSAStartup(version, &wsaData) != 0) {
00037 throw NNetworkException("Unable to initialize winsock", m_addr, NException::NETWORK);
00038 }
00039
00040 if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
00041 WSACleanup();
00042 throw NNetworkException("Incorrect Winsock version", m_addr, NException::NETWORK);
00043 }
00044
00045 }
00046
00047
00048
00049 NHostAddress::NHostAddress(const NString &addr)
00050 : NObject(),
00051 m_addr(addr),
00052 m_host()
00053 {
00054
00055 }
00056
00057
00058 NHostAddress::NHostAddress(const NHostAddress &addr)
00059 : NObject(),
00060 m_addr("0.0.0.0"),
00061 m_host()
00062 {
00063 m_host = addr.m_host;
00064 m_addr = addr.m_addr;
00065 }
00066
00067 NHostAddress::NHostAddress(const sockaddr *addr)
00068 : NObject(),
00069 m_addr("0.0.0.0"),
00070 m_host()
00071 {
00072 setAddress(addr);
00073 }
00074
00075 void NHostAddress::clear(void) {
00076 m_addr = "0.0.0.0";
00077 }
00078
00079
00080 void NHostAddress::setAddress(nuint32 ip) {
00081 m_addr = NString::number(static_cast<int>(ip));
00082 }
00083
00084
00085 void NHostAddress::setAddress(const NString &addr) {
00086 m_addr = addr;
00087 }
00088
00089 void NHostAddress::setAddress(const sockaddr *sock) {
00090 assert(sock);
00091
00092 m_addr = sock->sa_data;
00093 }
00094
00095
00096 nuint32 NHostAddress::resolve(void) const {
00097 struct hostent *result = NULL;
00098 size_t bufflen = 1024;
00099 int ret = 0;
00100 nuint32 ret_addr = 0;
00101
00102
00103 if (isalpha(m_addr[0])) {
00104 result = gethostbyname(m_addr.toChar());
00105 }
00106 else {
00107 ret_addr = inet_addr(m_addr.toChar());
00108 result = gethostbyaddr((char *)&ret_addr, 4, AF_INET);
00109 }
00110
00111 if (WSAGetLastError() != 0) {
00112 throwErrorByCode(WSAGetLastError());
00113 }
00114
00115 memcpy(&m_host, result, sizeof(struct hostent));
00116 ret_addr |= (unsigned char)(m_host.h_addr[0]);
00117 ret_addr <<= 8;
00118 ret_addr |= (unsigned char)(m_host.h_addr[1]);
00119 ret_addr <<= 8;
00120 ret_addr |= (unsigned char) m_host.h_addr[2];
00121 ret_addr <<= 8;
00122 ret_addr |= (unsigned char) m_host.h_addr[3];
00123
00124 return ret_addr;
00125 }
00126
00127 void NHostAddress::throwErrorByCode(int herr) const {
00128 NString msg = "Unable to resolve address ";
00129
00130 msg += m_addr;
00131 msg += ": ";
00132 switch (herr) {
00133 case WSAHOST_NOT_FOUND: {
00134 throw NNetworkException("Host not found", m_addr,
00135 NException::NETWORK, NNetworkException::DNS_HOST_UNKNOWN);
00136 }
00137 case WSATRY_AGAIN: {
00138 throw NNetworkException("Try again", m_addr,
00139 NException::NETWORK, NNetworkException::DNS_TRY_AGAIN);
00140 }
00141 case WSANO_RECOVERY: {
00142 throw NNetworkException("Non-recovarable name server error", m_addr,
00143 NException::NETWORK, NNetworkException::DNS_NO_RECOVERY);
00144
00145 }
00146 case WSANO_DATA: {
00147 throw NNetworkException("Address is valid, but there's no assigned ip address",
00148 m_addr, NException::NETWORK, NNetworkException::DNS_NO_ADDRESS); }
00149 default: {
00150 throw NNetworkException("Unknown error", m_addr,
00151 NException::NETWORK, NNetworkException::UNHANDLED_EXCEPTION);
00152 }
00153 }
00154 }
00155
00156
00157 NString NHostAddress::toString(void) const {
00158 NString ret;
00159 char addr[16];
00160
00161 if (!m_host.h_addr_list) {
00162 resolve();
00163 }
00164
00165 memset(addr, 0, sizeof(addr));
00166 #if defined(_MSC_VER_)
00167 _snprintf_s(addr, sizeof(addr), "%s", inet_ntoa(*(struct in_addr *) m_host.h_addr));
00168 #else
00169 snprintf(addr, sizeof(addr), "%s", inet_ntoa(*(struct in_addr *) m_host.h_addr));
00170 #endif // defined(_MSC_VER_)
00171 ret = addr;
00172 return ret;
00173 }
00174
00175 NString NHostAddress::toString(nuint32 ip) {
00176 NString ret;
00177 struct in_addr tmp;
00178 char addr[32];
00179
00180 memset(addr, 0, sizeof(addr));
00181 if (!inet_ntop(AF_INET, &ip, addr, sizeof(addr))) {
00182 throw NNetworkException(nerror(errno), NString::number((int) ip),
00183 NException::NETWORK);
00184 }
00185
00186 ret = addr;
00187
00188 return ret;
00189 }
00190
00191
00192
00193 NString NHostAddress::address(void) const {
00194 return m_addr;
00195 }
00196
00197
00198 NList<NString> NHostAddress::aliases(void) {
00199 NList<NString> ret;
00200 NString tmp;
00201
00202 for (int i = 0; m_host.h_addr_list[i]; i++) {
00203 tmp = m_host.h_aliases[i];
00204 ret.append(tmp);
00205 }
00206
00207 return ret;
00208 }
00209
00210
00211 NList<nuint32> NHostAddress::addresses(void) {
00212 NList<nuint32> ret;
00213
00214 for (int i = 0; m_host.h_addr_list[i]; i++) {
00215 ret.append(atoi(m_host.h_addr_list[i]));
00216 }
00217
00218 return ret;
00219
00220 }
00221
00222
00223 bool NHostAddress::isNull(void) const {
00224 if (m_addr == "0.0.0.0") {
00225 return true;
00226 }
00227
00228 return false;
00229 }
00230
00231
00232 bool NHostAddress::isLocalhost(void) const {
00233 if (m_addr == "127.0.0.1") {
00234 return true;
00235 }
00236
00237 return false;
00238 }
00239
00240
00241 NHostAddress &NHostAddress::operator=(const NHostAddress &rhs) {
00242 m_addr = rhs.m_addr;
00243
00244 return *this;
00245 }
00246
00247
00248 NHostAddress &NHostAddress::operator=(const NString &rhs) {
00249 m_addr = rhs;
00250
00251 return *this;
00252 }