1 /* Hosts file parser in nss_files module.
2 Copyright (C) 1996-2001, 2003, 2004, 2006 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
23 #include <arpa/nameser.h>
28 /* Get implementation for some internal functions. */
29 #include "../resolv/mapv4v6addr.h"
30 #include "../resolv/res_hconf.h"
33 #define ENTNAME hostent
34 #define DATABASE "hosts"
37 #define EXTRA_ARGS , af, flags
38 #define EXTRA_ARGS_DECL , int af, int flags
40 #define ENTDATA hostent_data
43 unsigned char host_addr
[16]; /* IPv4 or IPv6 address. */
44 char *h_addr_ptrs
[2]; /* Points to that and null terminator. */
47 #define TRAILING_LIST_MEMBER h_aliases
48 #define TRAILING_LIST_SEPARATOR_P isspace
49 #include "files-parse.c"
55 STRING_FIELD (addr
, isspace
, 1);
58 if (inet_pton (af
, addr
, entdata
->host_addr
) <= 0)
60 if (af
== AF_INET6
&& (flags
& AI_V4MAPPED
) != 0
61 && inet_pton (AF_INET
, addr
, entdata
->host_addr
) > 0)
62 map_v4v6_address ((char *) entdata
->host_addr
,
63 (char *) entdata
->host_addr
);
64 else if (af
== AF_INET
65 && inet_pton (AF_INET6
, addr
, entdata
->host_addr
) > 0)
67 if (IN6_IS_ADDR_V4MAPPED (entdata
->host_addr
))
68 memcpy (entdata
->host_addr
, entdata
->host_addr
+ 12, INADDRSZ
);
69 else if (IN6_IS_ADDR_LOOPBACK (entdata
->host_addr
))
71 in_addr_t localhost
= htonl (INADDR_LOOPBACK
);
72 memcpy (entdata
->host_addr
, &localhost
, sizeof (localhost
));
75 /* Illegal address: ignore line. */
79 /* Illegal address: ignore line. */
83 /* We always return entries of the requested form. */
84 result
->h_addrtype
= af
;
85 result
->h_length
= af
== AF_INET
? INADDRSZ
: IN6ADDRSZ
;
87 /* Store a pointer to the address in the expected form. */
88 entdata
->h_addr_ptrs
[0] = entdata
->host_addr
;
89 entdata
->h_addr_ptrs
[1] = NULL
;
90 result
->h_addr_list
= entdata
->h_addr_ptrs
;
92 STRING_FIELD (result
->h_name
, isspace
, 1);
97 #define HOST_DB_LOOKUP(name, keysize, keypattern, break_if_match, proto...) \
99 _nss_files_get##name##_r (proto, \
100 struct STRUCTURE *result, char *buffer, \
101 size_t buflen, int *errnop H_ERRNO_PROTO) \
103 enum nss_status status; \
105 __libc_lock_lock (lock); \
107 /* Reset file pointer to beginning or open file. */ \
108 status = internal_setent (keep_stream); \
110 if (status == NSS_STATUS_SUCCESS) \
112 /* Tell getent function that we have repositioned the file pointer. */ \
115 while ((status = internal_getent (result, buffer, buflen, errnop \
116 H_ERRNO_ARG EXTRA_ARGS_VALUE)) \
117 == NSS_STATUS_SUCCESS) \
120 if (status == NSS_STATUS_SUCCESS \
121 && _res_hconf.flags & HCONF_FLAG_MULTI) \
123 /* We have to get all host entries from the file. */ \
124 const size_t tmp_buflen = MIN (buflen, 4096); \
125 char tmp_buffer[tmp_buflen]; \
126 struct hostent tmp_result_buf; \
131 while (result->h_aliases[naliases] != NULL) \
134 bufferend = (char *) &result->h_aliases[naliases + 1]; \
136 while ((status = internal_getent (&tmp_result_buf, tmp_buffer, \
137 tmp_buflen, errnop H_ERRNO_ARG \
139 == NSS_STATUS_SUCCESS) \
142 struct hostent *old_result = result; \
143 result = &tmp_result_buf; \
144 /* The following piece is a bit clumsy but we want to use the \
145 `break_if_match' value. The optimizer should do its \
150 result = old_result; \
152 while ((matches = 0)); \
156 /* We could be very clever and try to recycle a few bytes \
157 in the buffer instead of generating new arrays. But \
158 we are not doing this here since it's more work than \
159 it's worth. Simply let the user provide a bit bigger \
161 char **new_h_addr_list; \
162 char **new_h_aliases; \
163 int newaliases = 0; \
164 size_t newstrlen = 0; \
167 /* Count the new aliases and the length of the strings. */ \
168 while (tmp_result_buf.h_aliases[newaliases] != NULL) \
170 char *cp = tmp_result_buf.h_aliases[newaliases]; \
172 newstrlen += strlen (cp) + 1; \
174 /* If the real name is different add it also to the \
175 aliases. This means that there is a duplication \
176 in the alias list but this is really the users \
178 if (strcmp (old_result->h_name, \
179 tmp_result_buf.h_name) != 0) \
182 newstrlen += strlen (tmp_result_buf.h_name) + 1; \
185 /* Make sure bufferend is aligned. */ \
186 assert ((bufferend - (char *) 0) % sizeof (char *) == 0); \
188 /* Now we can check whether the buffer is large enough. \
189 16 is the maximal size of the IP address. */ \
190 if (bufferend + 16 + (naddrs + 2) * sizeof (char *) \
191 + roundup (newstrlen, sizeof (char *)) \
192 + (naliases + newaliases + 1) * sizeof (char *) \
193 >= buffer + buflen) \
196 *herrnop = NETDB_INTERNAL; \
197 status = NSS_STATUS_TRYAGAIN; \
202 (char **) (bufferend \
203 + roundup (newstrlen, sizeof (char *)) \
206 (char **) ((char *) new_h_addr_list \
207 + (naddrs + 2) * sizeof (char *)); \
209 /* Copy the old data in the new arrays. */ \
210 for (cnt = 0; cnt < naddrs; ++cnt) \
211 new_h_addr_list[cnt] = old_result->h_addr_list[cnt]; \
213 for (cnt = 0; cnt < naliases; ++cnt) \
214 new_h_aliases[cnt] = old_result->h_aliases[cnt]; \
216 /* Store the new strings. */ \
218 while (tmp_result_buf.h_aliases[cnt] != NULL) \
220 new_h_aliases[naliases++] = bufferend; \
221 bufferend = (__stpcpy (bufferend, \
222 tmp_result_buf.h_aliases[cnt]) \
227 if (cnt < newaliases) \
229 new_h_aliases[naliases++] = bufferend; \
230 bufferend = __stpcpy (bufferend, \
231 tmp_result_buf.h_name) + 1; \
234 /* Final NULL pointer. */ \
235 new_h_aliases[naliases] = NULL; \
237 /* Round up the buffer end address. */ \
238 bufferend += (sizeof (char *) \
239 - ((bufferend - (char *) 0) \
240 % sizeof (char *))) % sizeof (char *); \
242 /* Now the new address. */ \
243 new_h_addr_list[naddrs++] = \
244 memcpy (bufferend, tmp_result_buf.h_addr, \
245 tmp_result_buf.h_length); \
247 /* Also here a final NULL pointer. */ \
248 new_h_addr_list[naddrs] = NULL; \
250 /* Store the new array pointers. */ \
251 old_result->h_aliases = new_h_aliases; \
252 old_result->h_addr_list = new_h_addr_list; \
254 /* Compute the new buffer end. */ \
255 bufferend = (char *) &new_h_aliases[naliases + 1]; \
256 assert (bufferend <= buffer + buflen); \
258 result = old_result; \
262 if (status != NSS_STATUS_TRYAGAIN) \
263 status = NSS_STATUS_SUCCESS; \
268 internal_endent (); \
271 __libc_lock_unlock (lock); \
277 #define EXTRA_ARGS_VALUE \
278 , ((_res.options & RES_USE_INET6) ? AF_INET6 : AF_INET), \
279 ((_res.options & RES_USE_INET6) ? AI_V4MAPPED : 0)
280 #include "files-XXX.c"
281 HOST_DB_LOOKUP (hostbyname
, ,,
283 LOOKUP_NAME_CASE (h_name
, h_aliases
)
287 #undef EXTRA_ARGS_VALUE
288 /* XXX Is using _res to determine whether we want to convert IPv4 addresses
289 to IPv6 addresses really the right thing to do? */
290 #define EXTRA_ARGS_VALUE \
291 , af, ((_res.options & RES_USE_INET6) ? AI_V4MAPPED : 0)
292 HOST_DB_LOOKUP (hostbyname2
, ,,
294 LOOKUP_NAME_CASE (h_name
, h_aliases
)
295 }, const char *name
, int af
)
297 #undef EXTRA_ARGS_VALUE
298 /* We only need to consider IPv4 mapped addresses if the input to the
299 gethostbyaddr() function is an IPv6 address. */
300 #define EXTRA_ARGS_VALUE \
301 , af, (len == IN6ADDRSZ ? AI_V4MAPPED : 0)
302 DB_LOOKUP (hostbyaddr
, ,,
304 if (result
->h_length
== (int) len
305 && ! memcmp (addr
, result
->h_addr_list
[0], len
))
307 }, const void *addr
, socklen_t len
, int af
)