Add the missing "; \".
[glibc.git] / nscd / nscd_gethst_r.c
blob70631fa9610b378741fb53b5c1ff65cce67e1f85
1 /* Copyright (C) 1998-2005, 2006, 2007, 2008, 2009
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <errno.h>
22 #include <resolv.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <arpa/nameser.h>
26 #include <not-cancel.h>
28 #include "nscd-client.h"
29 #include "nscd_proto.h"
31 int __nss_not_use_nscd_hosts;
33 static int nscd_gethst_r (const char *key, size_t keylen, request_type type,
34 struct hostent *resultbuf, char *buffer,
35 size_t buflen, struct hostent **result,
36 int *h_errnop) internal_function;
39 int
40 __nscd_gethostbyname_r (const char *name, struct hostent *resultbuf,
41 char *buffer, size_t buflen, struct hostent **result,
42 int *h_errnop)
44 request_type reqtype;
46 reqtype = (_res.options & RES_USE_INET6) ? GETHOSTBYNAMEv6 : GETHOSTBYNAME;
48 return nscd_gethst_r (name, strlen (name) + 1, reqtype, resultbuf,
49 buffer, buflen, result, h_errnop);
53 int
54 __nscd_gethostbyname2_r (const char *name, int af, struct hostent *resultbuf,
55 char *buffer, size_t buflen, struct hostent **result,
56 int *h_errnop)
58 request_type reqtype;
60 reqtype = af == AF_INET6 ? GETHOSTBYNAMEv6 : GETHOSTBYNAME;
62 return nscd_gethst_r (name, strlen (name) + 1, reqtype, resultbuf,
63 buffer, buflen, result, h_errnop);
67 int
68 __nscd_gethostbyaddr_r (const void *addr, socklen_t len, int type,
69 struct hostent *resultbuf, char *buffer, size_t buflen,
70 struct hostent **result, int *h_errnop)
72 request_type reqtype;
74 if (!((len == INADDRSZ && type == AF_INET)
75 || (len == IN6ADDRSZ && type == AF_INET6)))
76 /* LEN and TYPE do not match. */
77 return -1;
79 reqtype = type == AF_INET6 ? GETHOSTBYADDRv6 : GETHOSTBYADDR;
81 return nscd_gethst_r (addr, len, reqtype, resultbuf, buffer, buflen, result,
82 h_errnop);
86 libc_locked_map_ptr (, __hst_map_handle) attribute_hidden;
87 /* Note that we only free the structure if necessary. The memory
88 mapping is not removed since it is not visible to the malloc
89 handling. */
90 libc_freeres_fn (hst_map_free)
92 if (__hst_map_handle.mapped != NO_MAPPING)
94 void *p = __hst_map_handle.mapped;
95 __hst_map_handle.mapped = NO_MAPPING;
96 free (p);
101 int __nss_have_localdomain attribute_hidden;
103 static int
104 internal_function
105 nscd_gethst_r (const char *key, size_t keylen, request_type type,
106 struct hostent *resultbuf, char *buffer, size_t buflen,
107 struct hostent **result, int *h_errnop)
109 if (__builtin_expect (__nss_have_localdomain >= 0, 0))
111 if (__nss_have_localdomain == 0)
112 __nss_have_localdomain = getenv ("LOCALDOMAIN") != NULL ? 1 : -1;
113 if (__nss_have_localdomain > 0)
115 __nss_not_use_nscd_hosts = 1;
116 return -1;
120 int gc_cycle;
121 int nretries = 0;
123 /* If the mapping is available, try to search there instead of
124 communicating with the nscd. */
125 struct mapped_database *mapped;
126 mapped = __nscd_get_map_ref (GETFDHST, "hosts", &__hst_map_handle,
127 &gc_cycle);
129 retry:;
130 const char *h_name = NULL;
131 const uint32_t *aliases_len = NULL;
132 const char *addr_list = NULL;
133 size_t addr_list_len = 0;
134 int retval = -1;
135 const char *recend = (const char *) ~UINTMAX_C (0);
136 int sock = -1;
137 hst_response_header hst_resp;
138 if (mapped != NO_MAPPING)
140 /* No const qualifier, as it can change during garbage collection. */
141 struct datahead *found = __nscd_cache_search (type, key, keylen, mapped,
142 sizeof hst_resp);
143 if (found != NULL)
145 h_name = (char *) (&found->data[0].hstdata + 1);
146 hst_resp = found->data[0].hstdata;
147 aliases_len = (uint32_t *) (h_name + hst_resp.h_name_len);
148 addr_list = ((char *) aliases_len
149 + hst_resp.h_aliases_cnt * sizeof (uint32_t));
150 addr_list_len = hst_resp.h_addr_list_cnt * INADDRSZ;
151 recend = (const char *) found->data + found->recsize;
152 /* Now check if we can trust hst_resp fields. If GC is
153 in progress, it can contain anything. */
154 if (mapped->head->gc_cycle != gc_cycle)
156 retval = -2;
157 goto out;
160 #ifndef _STRING_ARCH_unaligned
161 /* The aliases_len array in the mapped database might very
162 well be unaligned. We will access it word-wise so on
163 platforms which do not tolerate unaligned accesses we
164 need to make an aligned copy. */
165 if (((uintptr_t) aliases_len & (__alignof__ (*aliases_len) - 1))
166 != 0)
168 uint32_t *tmp = alloca (hst_resp.h_aliases_cnt
169 * sizeof (uint32_t));
170 aliases_len = memcpy (tmp, aliases_len,
171 hst_resp.h_aliases_cnt
172 * sizeof (uint32_t));
174 #endif
175 if (type != GETHOSTBYADDR && type != GETHOSTBYNAME)
177 if (hst_resp.h_length == INADDRSZ)
178 addr_list += addr_list_len;
179 addr_list_len = hst_resp.h_addr_list_cnt * IN6ADDRSZ;
181 if (__builtin_expect ((const char *) addr_list + addr_list_len
182 > recend, 0))
183 goto out;
187 if (h_name == NULL)
189 sock = __nscd_open_socket (key, keylen, type, &hst_resp,
190 sizeof (hst_resp));
191 if (sock == -1)
193 __nss_not_use_nscd_hosts = 1;
194 goto out;
198 /* No value found so far. */
199 *result = NULL;
201 if (__builtin_expect (hst_resp.found == -1, 0))
203 /* The daemon does not cache this database. */
204 __nss_not_use_nscd_hosts = 1;
205 goto out_close;
208 if (hst_resp.found == 1)
210 char *cp = buffer;
211 uintptr_t align1;
212 uintptr_t align2;
213 size_t total_len;
214 ssize_t cnt;
215 char *ignore;
216 int n;
218 /* A first check whether the buffer is sufficiently large is possible. */
219 /* Now allocate the buffer the array for the group members. We must
220 align the pointer and the base of the h_addr_list pointers. */
221 align1 = ((__alignof__ (char *) - (cp - ((char *) 0)))
222 & (__alignof__ (char *) - 1));
223 align2 = ((__alignof__ (char *) - ((cp + align1 + hst_resp.h_name_len)
224 - ((char *) 0)))
225 & (__alignof__ (char *) - 1));
226 if (buflen < (align1 + hst_resp.h_name_len + align2
227 + ((hst_resp.h_aliases_cnt + hst_resp.h_addr_list_cnt
228 + 2)
229 * sizeof (char *))
230 + hst_resp.h_addr_list_cnt * (type == AF_INET
231 ? INADDRSZ : IN6ADDRSZ)))
233 no_room:
234 *h_errnop = NETDB_INTERNAL;
235 __set_errno (ERANGE);
236 retval = ERANGE;
237 goto out_close;
239 cp += align1;
241 /* Prepare the result as far as we can. */
242 resultbuf->h_aliases = (char **) cp;
243 cp += (hst_resp.h_aliases_cnt + 1) * sizeof (char *);
244 resultbuf->h_addr_list = (char **) cp;
245 cp += (hst_resp.h_addr_list_cnt + 1) * sizeof (char *);
247 resultbuf->h_name = cp;
248 cp += hst_resp.h_name_len + align2;
250 if (type == GETHOSTBYADDR || type == GETHOSTBYNAME)
252 resultbuf->h_addrtype = AF_INET;
253 resultbuf->h_length = INADDRSZ;
255 else
257 resultbuf->h_addrtype = AF_INET6;
258 resultbuf->h_length = IN6ADDRSZ;
260 for (cnt = 0; cnt < hst_resp.h_addr_list_cnt; ++cnt)
262 resultbuf->h_addr_list[cnt] = cp;
263 cp += resultbuf->h_length;
265 resultbuf->h_addr_list[cnt] = NULL;
267 if (h_name == NULL)
269 struct iovec vec[4];
271 vec[0].iov_base = resultbuf->h_name;
272 vec[0].iov_len = hst_resp.h_name_len;
273 total_len = hst_resp.h_name_len;
274 n = 1;
276 if (hst_resp.h_aliases_cnt > 0)
278 aliases_len = alloca (hst_resp.h_aliases_cnt
279 * sizeof (uint32_t));
280 vec[n].iov_base = (void *) aliases_len;
281 vec[n].iov_len = hst_resp.h_aliases_cnt * sizeof (uint32_t);
283 total_len += hst_resp.h_aliases_cnt * sizeof (uint32_t);
284 ++n;
287 if (type == GETHOSTBYADDR || type == GETHOSTBYNAME)
289 vec[n].iov_base = resultbuf->h_addr_list[0];
290 vec[n].iov_len = hst_resp.h_addr_list_cnt * INADDRSZ;
292 total_len += hst_resp.h_addr_list_cnt * INADDRSZ;
294 ++n;
296 else
298 if (hst_resp.h_length == INADDRSZ)
300 ignore = alloca (hst_resp.h_addr_list_cnt * INADDRSZ);
301 vec[n].iov_base = ignore;
302 vec[n].iov_len = hst_resp.h_addr_list_cnt * INADDRSZ;
304 total_len += hst_resp.h_addr_list_cnt * INADDRSZ;
306 ++n;
309 vec[n].iov_base = resultbuf->h_addr_list[0];
310 vec[n].iov_len = hst_resp.h_addr_list_cnt * IN6ADDRSZ;
312 total_len += hst_resp.h_addr_list_cnt * IN6ADDRSZ;
314 ++n;
317 if ((size_t) __readvall (sock, vec, n) != total_len)
318 goto out_close;
320 else
322 memcpy (resultbuf->h_name, h_name, hst_resp.h_name_len);
323 memcpy (resultbuf->h_addr_list[0], addr_list, addr_list_len);
326 /* Now we also can read the aliases. */
327 total_len = 0;
328 for (cnt = 0; cnt < hst_resp.h_aliases_cnt; ++cnt)
330 resultbuf->h_aliases[cnt] = cp;
331 cp += aliases_len[cnt];
332 total_len += aliases_len[cnt];
334 resultbuf->h_aliases[cnt] = NULL;
336 if (__builtin_expect ((const char *) addr_list + addr_list_len
337 + total_len > recend, 0))
339 /* aliases_len array might contain garbage during nscd GC cycle,
340 retry rather than fail in that case. */
341 if (addr_list != NULL && mapped->head->gc_cycle != gc_cycle)
342 retval = -2;
343 goto out_close;
345 /* See whether this would exceed the buffer capacity. */
346 if (__builtin_expect (cp > buffer + buflen, 0))
348 /* aliases_len array might contain garbage during nscd GC cycle,
349 retry rather than fail in that case. */
350 if (addr_list != NULL && mapped->head->gc_cycle != gc_cycle)
352 retval = -2;
353 goto out_close;
355 goto no_room;
358 /* And finally read the aliases. */
359 if (addr_list == NULL)
361 if (total_len == 0
362 || ((size_t) __readall (sock, resultbuf->h_aliases[0], total_len)
363 == total_len))
365 retval = 0;
366 *result = resultbuf;
369 else
371 memcpy (resultbuf->h_aliases[0],
372 (const char *) addr_list + addr_list_len, total_len);
374 /* Try to detect corrupt databases. */
375 if (resultbuf->h_name[hst_resp.h_name_len - 1] != '\0'
376 || ({for (cnt = 0; cnt < hst_resp.h_aliases_cnt; ++cnt)
377 if (resultbuf->h_aliases[cnt][aliases_len[cnt] - 1]
378 != '\0')
379 break;
380 cnt < hst_resp.h_aliases_cnt; }))
382 /* We cannot use the database. */
383 if (mapped->head->gc_cycle != gc_cycle)
384 retval = -2;
385 goto out_close;
388 retval = 0;
389 *result = resultbuf;
392 else
394 /* Store the error number. */
395 *h_errnop = hst_resp.error;
397 /* Set errno to 0 to indicate no error, just no found record. */
398 __set_errno (0);
399 /* Even though we have not found anything, the result is zero. */
400 retval = 0;
403 out_close:
404 if (sock != -1)
405 close_not_cancel_no_status (sock);
406 out:
407 if (__nscd_drop_map_ref (mapped, &gc_cycle) != 0)
409 /* When we come here this means there has been a GC cycle while we
410 were looking for the data. This means the data might have been
411 inconsistent. Retry if possible. */
412 if ((gc_cycle & 1) != 0 || ++nretries == 5 || retval == -1)
414 /* nscd is just running gc now. Disable using the mapping. */
415 if (atomic_decrement_val (&mapped->counter) == 0)
416 __nscd_unmap (mapped);
417 mapped = NO_MAPPING;
420 if (retval != -1)
421 goto retry;
424 return retval;