Mercurial > hg > octave-nkf > gnulib-hg
annotate lib/canon-host.c @ 5498:ac6438eb46c6
Add base64.
author | Simon Josefsson <simon@josefsson.org> |
---|---|
date | Tue, 30 Nov 2004 20:46:52 +0000 |
parents | 81e1a3092078 |
children | a48fb0e98c8c |
rev | line source |
---|---|
548 | 1 /* Host name canonicalization |
2 | |
5458 | 3 Copyright (C) 1995, 1999, 2000, 2002, 2003, 2004 Free Software |
4 Foundation, Inc. | |
548 | 5 |
6 Written by Miles Bader <miles@gnu.ai.mit.edu> | |
7 | |
8 This program is free software; you can redistribute it and/or | |
9 modify it under the terms of the GNU General Public License as | |
10 published by the Free Software Foundation; either version 2, or (at | |
11 your option) any later version. | |
12 | |
13 This program is distributed in the hope that it will be useful, but | |
14 WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 General Public License for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
651
242f0fe39aa7
update FSF address in copyright
Jim Meyering <jim@meyering.net>
parents:
554
diff
changeset
|
19 along with this program; if not, write to the Free Software Foundation, |
242f0fe39aa7
update FSF address in copyright
Jim Meyering <jim@meyering.net>
parents:
554
diff
changeset
|
20 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
548 | 21 |
22 #ifdef HAVE_CONFIG_H | |
653 | 23 # include <config.h> |
548 | 24 #endif |
25 | |
772
ff648b6bd671
Include sys/types.h for the definitions
Jim Meyering <jim@meyering.net>
parents:
653
diff
changeset
|
26 #include <sys/types.h> |
548 | 27 #ifdef HAVE_UNISTD_H |
653 | 28 # include <unistd.h> |
548 | 29 #endif |
4635 | 30 #include <stdlib.h> |
31 #include <string.h> | |
548 | 32 #ifdef HAVE_NETDB_H |
653 | 33 # include <netdb.h> |
548 | 34 #endif |
554
d6b4cc9397db
[HAVE_SYS_SOCKET_H]: Include <sys/socket.h> for definition of AF_INET
Jim Meyering <jim@meyering.net>
parents:
552
diff
changeset
|
35 #ifdef HAVE_SYS_SOCKET_H |
653 | 36 # include <sys/socket.h> |
554
d6b4cc9397db
[HAVE_SYS_SOCKET_H]: Include <sys/socket.h> for definition of AF_INET
Jim Meyering <jim@meyering.net>
parents:
552
diff
changeset
|
37 #endif |
548 | 38 |
39 #ifdef HAVE_NETINET_IN_H | |
653 | 40 # include <netinet/in.h> |
548 | 41 #endif |
42 #ifdef HAVE_ARPA_INET_H | |
653 | 43 # include <arpa/inet.h> |
548 | 44 #endif |
45 | |
5458 | 46 #include "strdup.h" |
47 | |
548 | 48 /* Returns the canonical hostname associated with HOST (allocated in a static |
5458 | 49 buffer), or NULL if it can't be determined. */ |
548 | 50 char * |
5458 | 51 canon_host (char const *host) |
548 | 52 { |
5458 | 53 char *h_addr_copy = NULL; |
548 | 54 |
5458 | 55 #if HAVE_GETADDRINFO |
56 { | |
57 struct addrinfo hint = { 0 }; | |
58 struct addrinfo *res = NULL; | |
59 hint.ai_flags = AI_CANONNAME; | |
60 if (getaddrinfo (host, NULL, &hint, &res) == 0) | |
61 { | |
62 h_addr_copy = strdup (res->ai_canonname); | |
63 freeaddrinfo (res); | |
64 } | |
65 } | |
66 #elif HAVE_GETHOSTBYNAME | |
67 { | |
68 struct hostent *he = gethostbyname (host); | |
548 | 69 |
5458 | 70 if (he) |
71 { | |
72 # ifdef HAVE_GETHOSTBYADDR | |
73 char *addr = NULL; | |
74 | |
75 /* Try and get an ascii version of the numeric host address. */ | |
76 switch (he->h_addrtype) | |
77 { | |
653 | 78 # ifdef HAVE_INET_NTOA |
5458 | 79 case AF_INET: |
80 addr = inet_ntoa (*(struct in_addr *) he->h_addr); | |
81 break; | |
653 | 82 # endif /* HAVE_INET_NTOA */ |
5458 | 83 } |
548 | 84 |
5458 | 85 if (addr && strcmp (he->h_name, addr) == 0) |
86 { | |
87 /* gethostbyname has returned a string representation of the IP | |
88 address, for example, "127.0.0.1". So now, look up the host | |
89 name via the address. Although it may seem reasonable to look | |
90 up the host name via the address, we must not pass `he->h_addr' | |
91 directly to gethostbyaddr because on some systems he->h_addr | |
92 is located in a static library buffer that is reused in the | |
93 gethostbyaddr call. Make a copy and use that instead. */ | |
94 h_addr_copy = (char *) malloc (he->h_length); | |
95 if (h_addr_copy == NULL) | |
96 he = NULL; | |
97 else | |
98 { | |
99 memcpy (h_addr_copy, he->h_addr, he->h_length); | |
100 he = gethostbyaddr (h_addr_copy, he->h_length, he->h_addrtype); | |
101 free (h_addr_copy); | |
102 } | |
103 } | |
653 | 104 # endif /* HAVE_GETHOSTBYADDR */ |
548 | 105 |
5458 | 106 if (he) |
107 h_addr_copy = strdup (he->h_name); | |
108 } | |
109 } | |
552
5e4f16c3fa39
Be careful to always return something.
Jim Meyering <jim@meyering.net>
parents:
549
diff
changeset
|
110 #endif /* HAVE_GETHOSTBYNAME */ |
5458 | 111 |
112 return h_addr_copy; | |
548 | 113 } |
1631
3072de090265
(main) [TEST_CANON_HOST]: Add a simple test driver.
Jim Meyering <jim@meyering.net>
parents:
1622
diff
changeset
|
114 |
3072de090265
(main) [TEST_CANON_HOST]: Add a simple test driver.
Jim Meyering <jim@meyering.net>
parents:
1622
diff
changeset
|
115 #ifdef TEST_CANON_HOST |
3072de090265
(main) [TEST_CANON_HOST]: Add a simple test driver.
Jim Meyering <jim@meyering.net>
parents:
1622
diff
changeset
|
116 int |
3072de090265
(main) [TEST_CANON_HOST]: Add a simple test driver.
Jim Meyering <jim@meyering.net>
parents:
1622
diff
changeset
|
117 main (int argc, char **argv) |
3072de090265
(main) [TEST_CANON_HOST]: Add a simple test driver.
Jim Meyering <jim@meyering.net>
parents:
1622
diff
changeset
|
118 { |
3072de090265
(main) [TEST_CANON_HOST]: Add a simple test driver.
Jim Meyering <jim@meyering.net>
parents:
1622
diff
changeset
|
119 int i; |
3072de090265
(main) [TEST_CANON_HOST]: Add a simple test driver.
Jim Meyering <jim@meyering.net>
parents:
1622
diff
changeset
|
120 for (i = 1; i < argc; i++) |
3072de090265
(main) [TEST_CANON_HOST]: Add a simple test driver.
Jim Meyering <jim@meyering.net>
parents:
1622
diff
changeset
|
121 { |
3072de090265
(main) [TEST_CANON_HOST]: Add a simple test driver.
Jim Meyering <jim@meyering.net>
parents:
1622
diff
changeset
|
122 char *s = canon_host (argv[i]); |
3072de090265
(main) [TEST_CANON_HOST]: Add a simple test driver.
Jim Meyering <jim@meyering.net>
parents:
1622
diff
changeset
|
123 printf ("%s: %s\n", argv[i], (s ? s : "<undef>")); |
3072de090265
(main) [TEST_CANON_HOST]: Add a simple test driver.
Jim Meyering <jim@meyering.net>
parents:
1622
diff
changeset
|
124 } |
3072de090265
(main) [TEST_CANON_HOST]: Add a simple test driver.
Jim Meyering <jim@meyering.net>
parents:
1622
diff
changeset
|
125 exit (0); |
3072de090265
(main) [TEST_CANON_HOST]: Add a simple test driver.
Jim Meyering <jim@meyering.net>
parents:
1622
diff
changeset
|
126 } |
3072de090265
(main) [TEST_CANON_HOST]: Add a simple test driver.
Jim Meyering <jim@meyering.net>
parents:
1622
diff
changeset
|
127 #endif /* TEST_CANON_HOST */ |