If NO_COMPAT_NEEDED is defined don't define any compatibility functions.
[glibc/history.git] / nscd / nscd_gethst_r.c
blobaea8288594586451908944f1d4c1572eba3aea37
1 /* Copyright (C) 1998-2005, 2006, 2007, 2008 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
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
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <resolv.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <arpa/nameser.h>
25 #include <not-cancel.h>
27 #include "nscd-client.h"
28 #include "nscd_proto.h"
30 int __nss_not_use_nscd_hosts;
32 static int nscd_gethst_r (const char *key, size_t keylen, request_type type,
33 struct hostent *resultbuf, char *buffer,
34 size_t buflen, struct hostent **result,
35 int *h_errnop) internal_function;
38 int
39 __nscd_gethostbyname_r (const char *name, struct hostent *resultbuf,
40 char *buffer, size_t buflen, struct hostent **result,
41 int *h_errnop)
43 request_type reqtype;
45 reqtype = (_res.options & RES_USE_INET6) ? GETHOSTBYNAMEv6 : GETHOSTBYNAME;
47 return nscd_gethst_r (name, strlen (name) + 1, reqtype, resultbuf,
48 buffer, buflen, result, h_errnop);
52 int
53 __nscd_gethostbyname2_r (const char *name, int af, struct hostent *resultbuf,
54 char *buffer, size_t buflen, struct hostent **result,
55 int *h_errnop)
57 request_type reqtype;
59 reqtype = af == AF_INET6 ? GETHOSTBYNAMEv6 : GETHOSTBYNAME;
61 return nscd_gethst_r (name, strlen (name) + 1, reqtype, resultbuf,
62 buffer, buflen, result, h_errnop);
66 int
67 __nscd_gethostbyaddr_r (const void *addr, socklen_t len, int type,
68 struct hostent *resultbuf, char *buffer, size_t buflen,
69 struct hostent **result, int *h_errnop)
71 request_type reqtype;
73 if (!((len == INADDRSZ && type == AF_INET)
74 || (len == IN6ADDRSZ && type == AF_INET6)))
75 /* LEN and TYPE do not match. */
76 return -1;
78 reqtype = type == AF_INET6 ? GETHOSTBYADDRv6 : GETHOSTBYADDR;
80 return nscd_gethst_r (addr, len, reqtype, resultbuf, buffer, buflen, result,
81 h_errnop);
85 libc_locked_map_ptr (, __hst_map_handle) attribute_hidden;
86 /* Note that we only free the structure if necessary. The memory
87 mapping is not removed since it is not visible to the malloc
88 handling. */
89 libc_freeres_fn (hst_map_free)
91 if (__hst_map_handle.mapped != NO_MAPPING)
93 void *p = __hst_map_handle.mapped;
94 __hst_map_handle.mapped = NO_MAPPING;
95 free (p);
100 int __nss_have_localdomain attribute_hidden;
102 static int
103 internal_function
104 nscd_gethst_r (const char *key, size_t keylen, request_type type,
105 struct hostent *resultbuf, char *buffer, size_t buflen,
106 struct hostent **result, int *h_errnop)
108 if (__builtin_expect (__nss_have_localdomain >= 0, 0))
110 if (__nss_have_localdomain == 0)
111 __nss_have_localdomain = getenv ("LOCALDOMAIN") != NULL ? 1 : -1;
112 if (__nss_have_localdomain > 0)
114 __nss_not_use_nscd_hosts = 1;
115 return -1;
119 int gc_cycle;
120 int nretries = 0;
122 /* If the mapping is available, try to search there instead of
123 communicating with the nscd. */
124 struct mapped_database *mapped;
125 mapped = __nscd_get_map_ref (GETFDHST, "hosts", &__hst_map_handle,
126 &gc_cycle);
128 retry:;
129 const char *h_name = NULL;
130 const uint32_t *aliases_len = NULL;
131 const char *addr_list = NULL;
132 size_t addr_list_len = 0;
133 int retval = -1;
134 const char *recend = (const char *) ~UINTMAX_C (0);
135 int sock = -1;
136 hst_response_header hst_resp;
137 if (mapped != NO_MAPPING)
139 /* No const qualifier, as it can change during garbage collection. */
140 struct datahead *found = __nscd_cache_search (type, key, keylen, mapped);
141 if (found != NULL)
143 h_name = (char *) (&found->data[0].hstdata + 1);
144 hst_resp = found->data[0].hstdata;
145 aliases_len = (uint32_t *) (h_name + hst_resp.h_name_len);
146 addr_list = ((char *) aliases_len
147 + hst_resp.h_aliases_cnt * sizeof (uint32_t));
148 addr_list_len = hst_resp.h_addr_list_cnt * INADDRSZ;
149 recend = (const char *) found->data + found->recsize;
150 /* Now check if we can trust hst_resp fields. If GC is
151 in progress, it can contain anything. */
152 if (mapped->head->gc_cycle != gc_cycle)
154 retval = -2;
155 goto out;
158 #ifndef _STRING_ARCH_unaligned
159 /* The aliases_len array in the mapped database might very
160 well be unaligned. We will access it word-wise so on
161 platforms which do not tolerate unaligned accesses we
162 need to make an aligned copy. */
163 if (((uintptr_t) aliases_len & (__alignof__ (*aliases_len) - 1))
164 != 0)
166 uint32_t *tmp = alloca (hst_resp.h_aliases_cnt
167 * sizeof (uint32_t));
168 aliases_len = memcpy (tmp, aliases_len,
169 hst_resp.h_aliases_cnt
170 * sizeof (uint32_t));
172 #endif
173 if (type != GETHOSTBYADDR && type != GETHOSTBYNAME)
175 if (hst_resp.h_length == INADDRSZ)
176 addr_list += addr_list_len;
177 addr_list_len = hst_resp.h_addr_list_cnt * IN6ADDRSZ;
179 if (__builtin_expect ((const char *) addr_list + addr_list_len
180 > recend, 0))
181 goto out;
185 if (h_name == NULL)
187 sock = __nscd_open_socket (key, keylen, type, &hst_resp,
188 sizeof (hst_resp));
189 if (sock == -1)
191 __nss_not_use_nscd_hosts = 1;
192 goto out;
196 /* No value found so far. */
197 *result = NULL;
199 if (__builtin_expect (hst_resp.found == -1, 0))
201 /* The daemon does not cache this database. */
202 __nss_not_use_nscd_hosts = 1;
203 goto out_close;
206 if (hst_resp.found == 1)
208 char *cp = buffer;
209 uintptr_t align1;
210 uintptr_t align2;
211 size_t total_len;
212 ssize_t cnt;
213 char *ignore;
214 int n;
216 /* A first check whether the buffer is sufficiently large is possible. */
217 /* Now allocate the buffer the array for the group members. We must
218 align the pointer and the base of the h_addr_list pointers. */
219 align1 = ((__alignof__ (char *) - (cp - ((char *) 0)))
220 & (__alignof__ (char *) - 1));
221 align2 = ((__alignof__ (char *) - ((cp + align1 + hst_resp.h_name_len)
222 - ((char *) 0)))
223 & (__alignof__ (char *) - 1));
224 if (buflen < (align1 + hst_resp.h_name_len + align2
225 + ((hst_resp.h_aliases_cnt + hst_resp.h_addr_list_cnt
226 + 2)
227 * sizeof (char *))
228 + hst_resp.h_addr_list_cnt * (type == AF_INET
229 ? INADDRSZ : IN6ADDRSZ)))
231 no_room:
232 *h_errnop = NETDB_INTERNAL;
233 __set_errno (ERANGE);
234 retval = ERANGE;
235 goto out_close;
237 cp += align1;
239 /* Prepare the result as far as we can. */
240 resultbuf->h_aliases = (char **) cp;
241 cp += (hst_resp.h_aliases_cnt + 1) * sizeof (char *);
242 resultbuf->h_addr_list = (char **) cp;
243 cp += (hst_resp.h_addr_list_cnt + 1) * sizeof (char *);
245 resultbuf->h_name = cp;
246 cp += hst_resp.h_name_len + align2;
248 if (type == GETHOSTBYADDR || type == GETHOSTBYNAME)
250 resultbuf->h_addrtype = AF_INET;
251 resultbuf->h_length = INADDRSZ;
253 else
255 resultbuf->h_addrtype = AF_INET6;
256 resultbuf->h_length = IN6ADDRSZ;
258 for (cnt = 0; cnt < hst_resp.h_addr_list_cnt; ++cnt)
260 resultbuf->h_addr_list[cnt] = cp;
261 cp += resultbuf->h_length;
263 resultbuf->h_addr_list[cnt] = NULL;
265 if (h_name == NULL)
267 struct iovec vec[4];
269 vec[0].iov_base = resultbuf->h_name;
270 vec[0].iov_len = hst_resp.h_name_len;
271 total_len = hst_resp.h_name_len;
272 n = 1;
274 if (hst_resp.h_aliases_cnt > 0)
276 aliases_len = alloca (hst_resp.h_aliases_cnt
277 * sizeof (uint32_t));
278 vec[n].iov_base = (void *) aliases_len;
279 vec[n].iov_len = hst_resp.h_aliases_cnt * sizeof (uint32_t);
281 total_len += hst_resp.h_aliases_cnt * sizeof (uint32_t);
282 ++n;
285 if (type == GETHOSTBYADDR || type == GETHOSTBYNAME)
287 vec[n].iov_base = resultbuf->h_addr_list[0];
288 vec[n].iov_len = hst_resp.h_addr_list_cnt * INADDRSZ;
290 total_len += hst_resp.h_addr_list_cnt * INADDRSZ;
292 ++n;
294 else
296 if (hst_resp.h_length == INADDRSZ)
298 ignore = alloca (hst_resp.h_addr_list_cnt * INADDRSZ);
299 vec[n].iov_base = ignore;
300 vec[n].iov_len = hst_resp.h_addr_list_cnt * INADDRSZ;
302 total_len += hst_resp.h_addr_list_cnt * INADDRSZ;
304 ++n;
307 vec[n].iov_base = resultbuf->h_addr_list[0];
308 vec[n].iov_len = hst_resp.h_addr_list_cnt * IN6ADDRSZ;
310 total_len += hst_resp.h_addr_list_cnt * IN6ADDRSZ;
312 ++n;
315 if ((size_t) __readvall (sock, vec, n) != total_len)
316 goto out_close;
318 else
320 memcpy (resultbuf->h_name, h_name, hst_resp.h_name_len);
321 memcpy (resultbuf->h_addr_list[0], addr_list, addr_list_len);
324 /* Now we also can read the aliases. */
325 total_len = 0;
326 for (cnt = 0; cnt < hst_resp.h_aliases_cnt; ++cnt)
328 resultbuf->h_aliases[cnt] = cp;
329 cp += aliases_len[cnt];
330 total_len += aliases_len[cnt];
332 resultbuf->h_aliases[cnt] = NULL;
334 if (__builtin_expect ((const char *) addr_list + addr_list_len
335 + total_len > recend, 0))
337 /* aliases_len array might contain garbage during nscd GC cycle,
338 retry rather than fail in that case. */
339 if (addr_list != NULL && mapped->head->gc_cycle != gc_cycle)
340 retval = -2;
341 goto out_close;
343 /* See whether this would exceed the buffer capacity. */
344 if (__builtin_expect (cp > buffer + buflen, 0))
346 /* aliases_len array might contain garbage during nscd GC cycle,
347 retry rather than fail in that case. */
348 if (addr_list != NULL && mapped->head->gc_cycle != gc_cycle)
350 retval = -2;
351 goto out_close;
353 goto no_room;
356 /* And finally read the aliases. */
357 if (addr_list == NULL)
359 if (total_len == 0
360 || ((size_t) __readall (sock, resultbuf->h_aliases[0], total_len)
361 == total_len))
363 retval = 0;
364 *result = resultbuf;
367 else
369 memcpy (resultbuf->h_aliases[0],
370 (const char *) addr_list + addr_list_len, total_len);
372 /* Try to detect corrupt databases. */
373 if (resultbuf->h_name[hst_resp.h_name_len - 1] != '\0'
374 || ({for (cnt = 0; cnt < hst_resp.h_aliases_cnt; ++cnt)
375 if (resultbuf->h_aliases[cnt][aliases_len[cnt] - 1]
376 != '\0')
377 break;
378 cnt < hst_resp.h_aliases_cnt; }))
380 /* We cannot use the database. */
381 if (mapped->head->gc_cycle != gc_cycle)
382 retval = -2;
383 goto out_close;
386 retval = 0;
387 *result = resultbuf;
390 else
392 /* Store the error number. */
393 *h_errnop = hst_resp.error;
395 /* Set errno to 0 to indicate no error, just no found record. */
396 __set_errno (0);
397 /* Even though we have not found anything, the result is zero. */
398 retval = 0;
401 out_close:
402 if (sock != -1)
403 close_not_cancel_no_status (sock);
404 out:
405 if (__nscd_drop_map_ref (mapped, &gc_cycle) != 0)
407 /* When we come here this means there has been a GC cycle while we
408 were looking for the data. This means the data might have been
409 inconsistent. Retry if possible. */
410 if ((gc_cycle & 1) != 0 || ++nretries == 5 || retval == -1)
412 /* nscd is just running gc now. Disable using the mapping. */
413 if (atomic_decrement_val (&mapped->counter) == 0)
414 __nscd_unmap (mapped);
415 mapped = NO_MAPPING;
418 if (retval != -1)
419 goto retry;
422 return retval;