1 /* Host name canonicalization
3 Copyright (C) 1995 Free Software Foundation, Inc.
5 Written by Miles Bader <miles@gnu.ai.mit.edu>
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2, or (at
10 your option) any later version.
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
34 #ifdef HAVE_SYS_SOCKET_H
35 #include <sys/socket.h>
38 #ifdef HAVE_NETINET_IN_H
39 #include <netinet/in.h>
41 #ifdef HAVE_ARPA_INET_H
42 #include <arpa/inet.h>
45 /* Returns the canonical hostname associated with HOST (allocated in a static
46 buffer), or 0 if it can't be determined. */
51 #ifdef HAVE_GETHOSTBYNAME
52 struct hostent
*he
= gethostbyname (host
);
56 #ifdef HAVE_GETHOSTBYADDR
59 /* Try and get an ascii version of the numeric host address. */
60 switch (he
->h_addrtype
)
64 addr
= inet_ntoa (*(struct in_addr
*) he
->h_addr
);
66 #endif /* HAVE_INET_NTOA */
69 if (addr
&& strcmp (he
->h_name
, addr
) == 0)
70 /* gethostbyname() cheated! Lookup the host name via the address
71 this time to get the actual host name. */
72 he
= gethostbyaddr (he
->h_addr
, he
->h_length
, he
->h_addrtype
);
73 #endif /* HAVE_GETHOSTBYADDR */
76 return (char *) (he
->h_name
);
78 #endif /* HAVE_GETHOSTBYNAME */