linux/nhostaddress.cpp

00001 // LICENSE: (Please see the file COPYING for details)
00002 //
00003 // NUS - Nemesis Utilities System: A C++ application development framework 
00004 // Copyright (C) 2006, 2008 Otavio Rodolfo Piske
00005 //
00006 //  This file is part of NUS
00007 //
00008 //  This library is free software; you can redistribute it and/or
00009 //  License as published by the Free Software Foundation version 2.1
00010 //  of the License.
00011 //
00012 //  modify it under the terms of the GNU Lesser General Public
00013 //  This library is distributed in the hope that it will be useful,
00014 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 //  Lesser General Public License for more details.
00017 //
00018 //  You should have received a copy of the GNU Lesser General Public
00019 //  License along with this library; if not, write to the Free Software
00020 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00021 //
00022 #include "nhostaddress.h"
00023 
00024 NHostAddress::NHostAddress(void)
00025       : NObject(),
00026       m_addr("0.0.0.0"),
00027       m_host()
00028 {
00029       memset(&m_host, 0, sizeof(m_host));
00030 }
00031 
00032 
00033 
00034 NHostAddress::NHostAddress(const NString &addr)
00035       : NObject(),
00036       m_addr(addr),
00037       m_host()
00038 {
00039 
00040 }
00041 
00042 
00043 NHostAddress::NHostAddress(const NHostAddress &addr)
00044       : NObject(),
00045       m_addr("0.0.0.0"),
00046       m_host()
00047 {
00048       m_host = addr.m_host;
00049       m_addr = addr.m_addr;
00050 }
00051 
00052 NHostAddress::NHostAddress(const sockaddr *addr)
00053       : NObject(),
00054       m_addr("0.0.0.0"),
00055       m_host()
00056 {
00057       setAddress(addr);
00058 }
00059 
00060 
00061 void NHostAddress::clear(void) {
00062       m_addr = "0.0.0.0";
00063 }
00064 
00065 
00066 void NHostAddress::setAddress(nuint32 ip) {
00067       m_addr = NString::number(static_cast<int>(ip));
00068 }
00069 
00070 
00071 void NHostAddress::setAddress(const NString &addr) {
00072       m_addr = addr;
00073 }
00074 
00075 void NHostAddress::setAddress(const sockaddr *sock) {
00076       assert(sock);
00077       
00078       m_addr = sock->sa_data;
00079 }
00080 
00081 
00082 nuint32 NHostAddress::resolve(void) const {
00083       struct hostent *result = NULL;
00084       nuint32 ret_addr = 0;
00085       
00086       result = gethostbyname(m_addr.toChar());
00087       if (!result) {
00088             char *msg = static_cast<char *>(malloc(1024));
00089             if (!msg) {
00090                   throw NMemException(N_ENOMEM, NException::NETWORK);
00091             }
00092             
00093             throwErrorByCode(h_errno);
00094       }
00095       
00096       memcpy(&m_host, result, sizeof(m_host));
00097       
00098       ret_addr |= (unsigned char)(m_host.h_addr[3]);
00099       ret_addr <<= 8;
00100       ret_addr |= (unsigned char)(m_host.h_addr[2]);
00101       ret_addr <<= 8;
00102       ret_addr |= (unsigned char) m_host.h_addr[1];
00103       ret_addr <<= 8;
00104       ret_addr |= (unsigned char) m_host.h_addr[0];
00105       
00106       return ret_addr;
00107 }
00108 
00109 void NHostAddress::throwErrorByCode(int herr) const {
00110       switch (herr) {
00111             case HOST_NOT_FOUND: {
00112                    throw NNetworkException("Host not found", m_addr,
00113                         NException::NETWORK, NNetworkException::DNS_HOST_UNKNOWN);
00114             }
00115             case TRY_AGAIN: {
00116                   throw NNetworkException("Try again", m_addr, 
00117                         NException::NETWORK, NNetworkException::DNS_TRY_AGAIN);
00118             }
00119             case NO_RECOVERY: {
00120                   throw NNetworkException("Non-recovarable name server error", m_addr, 
00121                               NException::NETWORK, NNetworkException::DNS_NO_RECOVERY);
00122             }
00123             case NO_ADDRESS: {
00124                   throw NNetworkException("Address is valid, but there's no assigned ip address",
00125                                     m_addr, NException::NETWORK, NNetworkException::DNS_NO_ADDRESS);
00126             } 
00127             default: {
00128                   throw NNetworkException("Unknown error", m_addr, 
00129                                     NException::NETWORK, NNetworkException::UNHANDLED_EXCEPTION);
00130             }
00131       }
00132 }
00133 
00134 
00135 NString NHostAddress::toString(void) const {
00136       NString ret;
00137       char addr[16];
00138       
00139       if (!m_host.h_addr) {
00140             resolve();
00141       }
00142             
00143       memset(addr, 0, sizeof(addr));
00144       snprintf(addr, sizeof(addr), "%s", inet_ntoa(*(struct in_addr *) m_host.h_addr));
00145       ret = addr;
00146       return ret;
00147 }
00148 
00149 
00150 
00151 NString NHostAddress::toString(nuint32 ip) {
00152       NString ret;
00153       char addr[32];
00154             
00155       memset(addr, 0, sizeof(addr));
00156       if (!inet_ntop(AF_INET, &ip, addr, sizeof(addr))) { 
00157             throw NNetworkException(nerror(errno), NString::number((int) ip), 
00158                         NException::NETWORK);
00159       }
00160       
00161       ret = addr;
00162       
00163       return ret;
00164 }
00165 
00166 NString NHostAddress::address(void) const {
00167       return m_addr;
00168 }
00169 
00170 
00171 NList<NString> NHostAddress::aliases(void) {
00172       NList<NString> ret;
00173       NString tmp;
00174       
00175       while (*m_host.h_aliases) { 
00176             tmp = *m_host.h_aliases;
00177             ret.append(tmp);
00178             m_host.h_aliases++;
00179       }
00180       
00181       
00182       return ret;
00183 }
00184 
00185 
00186 NList<nuint32> NHostAddress::addresses(void) {
00187       NList<nuint32> ret;
00188       nuint32 tmp = 0;
00189       
00190       for (int i = 0; m_host.h_addr_list[i]; i++) {
00191             tmp |= (unsigned char) m_host.h_addr_list[i][3];
00192             tmp <<= 8;
00193             tmp |= (unsigned char) m_host.h_addr_list[i][2];
00194             tmp <<= 8;
00195             tmp |= (unsigned char) m_host.h_addr_list[i][1];
00196             tmp <<= 8;
00197             tmp |= (unsigned char) m_host.h_addr_list[i][0];
00198             ret.append(tmp);
00199             
00200       }
00201       
00202       return ret;
00203 
00204 }
00205 
00206 
00207 bool NHostAddress::isNull(void) const {
00208       if (m_addr == "0.0.0.0") {
00209             return true;
00210       }
00211       
00212       return false;
00213 }
00214 
00215 
00216 bool NHostAddress::isLocalhost(void) const {
00217       if (m_addr == "127.0.0.1") {
00218             return true;
00219       }
00220       
00221       return false;
00222 }
00223 
00224 
00225 NHostAddress &NHostAddress::operator=(const NHostAddress &rhs) {
00226       m_addr = rhs.m_addr;
00227       
00228       return *this;
00229 }
00230 
00231 
00232 NHostAddress &NHostAddress::operator=(const NString &rhs) {
00233       m_addr = rhs;
00234       
00235       return *this;
00236 }

Generated on Wed Mar 5 23:10:35 2008 for NemesisUtilitiesSystem by  doxygen 1.5.4