Remove K&R cruft.
[gnulib.git] / lib / canon-host.c
1 /* Host name canonicalization
2
3    Copyright (C) 1995, 1999, 2000, 2002, 2003 Free Software Foundation, Inc.
4
5    Written by Miles Bader <miles@gnu.ai.mit.edu>
6
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.
11
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.
16
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 Foundation,
19    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include <sys/types.h>
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
29 #include <stdlib.h>
30 #include <string.h>
31 #ifdef HAVE_NETDB_H
32 # include <netdb.h>
33 #endif
34 #ifdef HAVE_SYS_SOCKET_H
35 # include <sys/socket.h>
36 #endif
37
38 #ifdef HAVE_NETINET_IN_H
39 # include <netinet/in.h>
40 #endif
41 #ifdef HAVE_ARPA_INET_H
42 # include <arpa/inet.h>
43 #endif
44
45 /* Returns the canonical hostname associated with HOST (allocated in a static
46    buffer), or 0 if it can't be determined.  */
47 char *
48 canon_host (const char *host)
49 {
50 #ifdef HAVE_GETHOSTBYNAME
51   struct hostent *he = gethostbyname (host);
52
53   if (he)
54     {
55 # ifdef HAVE_GETHOSTBYADDR
56       char *addr = 0;
57
58       /* Try and get an ascii version of the numeric host address.  */
59       switch (he->h_addrtype)
60         {
61 #  ifdef HAVE_INET_NTOA
62         case AF_INET:
63           addr = inet_ntoa (*(struct in_addr *) he->h_addr);
64           break;
65 #  endif /* HAVE_INET_NTOA */
66         }
67
68       if (addr && strcmp (he->h_name, addr) == 0)
69         {
70           /* gethostbyname has returned a string representation of the IP
71              address, for example, "127.0.0.1".  So now, look up the host
72              name via the address.  Although it may seem reasonable to look
73              up the host name via the address, we must not pass `he->h_addr'
74              directly to gethostbyaddr because on some systems he->h_addr
75              is located in a static library buffer that is reused in the
76              gethostbyaddr call.  Make a copy and use that instead.  */
77           char *h_addr_copy = (char *) malloc (he->h_length);
78           if (h_addr_copy == NULL)
79             he = NULL;
80           else
81             {
82               memcpy (h_addr_copy, he->h_addr, he->h_length);
83               he = gethostbyaddr (h_addr_copy, he->h_length, he->h_addrtype);
84               free (h_addr_copy);
85             }
86         }
87 # endif /* HAVE_GETHOSTBYADDR */
88
89       if (he)
90         return (char *) (he->h_name);
91     }
92 #endif /* HAVE_GETHOSTBYNAME */
93   return 0;
94 }
95
96 #ifdef TEST_CANON_HOST
97 int
98 main (int argc, char **argv)
99 {
100   int i;
101   for (i = 1; i < argc; i++)
102     {
103       char *s = canon_host (argv[i]);
104       printf ("%s: %s\n", argv[i], (s ? s : "<undef>"));
105     }
106   exit (0);
107 }
108 #endif /* TEST_CANON_HOST */