Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

InetAddrNetDB.cxx

00001 /*
00002  * Copyright 1991-2002 Eric M. Hopper <hopper@omnifarious.org>
00003  * 
00004  *     This program is free software; you can redistribute it and/or modify it
00005  *     under the terms of the GNU Lesser General Public License as published
00006  *     by the Free Software Foundation; either version 2 of the License, or
00007  *     (at your option) any later version.
00008  * 
00009  *     This program is distributed in the hope that it will be useful, but
00010  *     WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  *     Lesser General Public License for more details.
00013  * 
00014  *     You should have received a copy of the GNU Lesser General Public
00015  *     License along with this program; if not, write to the Free Software
00016  *     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 /* $Header: /home/hopper/src/cvs/C++/EHnet++/InetAddrNetDB.cxx,v 1.7 2002/08/29 00:58:04 hopper Exp $ */
00020 
00021 // For log, see ChangeLog
00022 //
00023 // $Revision: 1.7 $
00024 //
00025 // Revision 1.2  1998/06/02 01:08:15  hopper
00026 // Changed a static_cast to a more correct reinterpret_cast.
00027 //
00028 // Revision 1.1  1996/02/12 00:32:35  hopper
00029 // Fixed to use the new C++ standard library string class instead of all the
00030 // 'NetString' silliness.
00031 //
00032 
00033 // $Revision: 1.7 $
00034 
00035 #include <EHnet++/InetAddress.h>
00036 #include <netdb.h>
00037 #include <sys/socket.h>
00038 #include <sys/param.h>
00039 #include <string>
00040 
00041 /* My UNIX (UnixWare) has no declaration for gethostname in any header
00042    file anywhere.  I should get GNU configure to check for this, and
00043    only use the declaration below conditionally, but that'll have to
00044    wait. */
00045 extern "C" int gethostname(char *name, int namelen);
00046 
00047 namespace strmod {
00048 namespace ehnet {
00049 
00050 bool InetAddress::NameToIaddr(const char *name_addr, U4Byte &num)
00051 {
00052    ::hostent *hostinfo;
00053    char *temp_name = const_cast<char *>(name_addr);
00054 
00055    hostinfo = ::gethostbyname(temp_name);
00056    if (hostinfo == 0 || hostinfo->h_addrtype != AF_INET) {
00057       return(false);
00058    } else {
00059       num = ((struct in_addr *)(hostinfo->h_addr_list[0]))->s_addr;
00060       return(true);
00061    }
00062 }
00063 
00064 ::std::string InetAddress::IaddrToName(const sockaddr_in &inaddr)
00065 {
00066    ::std::string hostname;
00067    ::hostent *hostinfo;
00068 
00069    /* If we're dealing with a non-INADDR_ANY address. */
00070    if (inaddr.sin_addr.s_addr != INADDR_ANY) {
00071       char *cast_addr;
00072 
00073       /* Do messy casting to type gethostbyaddr likes. */
00074       cast_addr = const_cast<char *>(
00075          reinterpret_cast<const char *>(&inaddr.sin_addr));
00076       /* Look up the name for the given address.*/
00077       hostinfo = ::gethostbyaddr(cast_addr, sizeof(inaddr.sin_addr), AF_INET);
00078 
00079       if (hostinfo != 0) {
00080          /* Eureka! We found a 'real' hostname for our internet number. */
00081 
00082          if (inaddr.sin_addr.s_addr != INADDR_ANY) {
00083             hostname = hostinfo->h_name;
00084          }
00085       } else {
00086          /* Else, no name information was found for this inaddr, so convert
00087             the inaddr to the <num>.<num>.<num>.<num> form.  Precedent for
00088             doing this has been set by many UNIIX utilities, most notably,
00089             traceroute. */
00090 
00091          unsigned long addr = ntohl(inaddr.sin_addr.s_addr);
00092 
00093          hostname = ToDec((addr >> 24) & 0xffUL);
00094          hostname += '.';
00095          hostname += ToDec((addr >> 16) & 0xffUL);
00096          hostname += '.';
00097          hostname += ToDec((addr >> 8) & 0xffUL);
00098          hostname += '.';
00099          hostname += ToDec(addr & 0xffUL);
00100       }
00101    } else {
00102       /* INADDR_ANY addresses are used by bind to set up ports on the local
00103          machine.  Not using INADDR_ANY relegates us to having several
00104          different ports for several different IPs.  All hosts have AT LEAST
00105          two IPs, their real internet number, and localhost loopback
00106          (127.0.0.1). Computers that double as routers may have many more,
00107          one for each subnet that they 'appear' on. */
00108       /* This means that the hostname for an INADDR_ANY address is the local
00109          machine's hostname. */
00110 
00111       char buf[MAXHOSTNAMELEN + 1];
00112 
00113       ::gethostname(buf, MAXHOSTNAMELEN + 1);
00114       hostinfo = ::gethostbyname(buf);
00115       hostname = "INADDR_ANY(";
00116       if (hostinfo != 0) {
00117          hostname += hostinfo->h_name;
00118       } else {
00119          hostname += buf;
00120       }
00121       hostname += ")";
00122    }
00123    return(hostname);
00124 }
00125 
00126 } // end namespace ehnet
00127 } // end namespace lcore

Generated on Wed Jan 29 00:32:44 2003 for libNet by doxygen1.3-rc1