Update.
[glibc.git] / nscd / nscd_gethst_r.c
blob25553babf4325e3d56f6f108a4b5f56e6bf78bf6
1 /* Copyright (C) 1998-2002, 2003, 2004 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 <assert.h>
21 #include <errno.h>
22 #include <netdb.h>
23 #include <resolv.h>
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <arpa/nameser.h>
30 #include <not-cancel.h>
32 #include "nscd-client.h"
33 #include "nscd_proto.h"
35 int __nss_not_use_nscd_hosts;
37 static int nscd_gethst_r (const char *key, size_t keylen, request_type type,
38 struct hostent *resultbuf, char *buffer,
39 size_t buflen, struct hostent **result,
40 int *h_errnop) internal_function;
43 int
44 __nscd_gethostbyname_r (const char *name, struct hostent *resultbuf,
45 char *buffer, size_t buflen, struct hostent **result,
46 int *h_errnop)
48 request_type reqtype;
50 reqtype = (_res.options & RES_USE_INET6) ? GETHOSTBYNAMEv6 : GETHOSTBYNAME;
52 return nscd_gethst_r (name, strlen (name) + 1, reqtype, resultbuf,
53 buffer, buflen, result, h_errnop);
57 int
58 __nscd_gethostbyname2_r (const char *name, int af, struct hostent *resultbuf,
59 char *buffer, size_t buflen, struct hostent **result,
60 int *h_errnop)
62 request_type reqtype;
64 reqtype = af == AF_INET6 ? GETHOSTBYNAMEv6 : GETHOSTBYNAME;
66 return nscd_gethst_r (name, strlen (name) + 1, reqtype, resultbuf,
67 buffer, buflen, result, h_errnop);
71 int
72 __nscd_gethostbyaddr_r (const void *addr, socklen_t len, int type,
73 struct hostent *resultbuf, char *buffer, size_t buflen,
74 struct hostent **result, int *h_errnop)
76 request_type reqtype;
78 if (!((len == INADDRSZ && type == AF_INET)
79 || (len == IN6ADDRSZ && type == AF_INET6)))
80 /* LEN and TYPE do not match. */
81 return -1;
83 reqtype = type == AF_INET6 ? GETHOSTBYADDRv6 : GETHOSTBYADDR;
85 return nscd_gethst_r (addr, len, reqtype, resultbuf, buffer, buflen, result,
86 h_errnop);
90 libc_locked_map_ptr (map_handle);
91 /* Note that we only free the structure if necessary. The memory
92 mapping is not removed since it is not visible to the malloc
93 handling. */
94 libc_freeres_fn (gr_map_free)
97 if (map_handle.mapped != NO_MAPPING)
98 free (map_handle.mapped);
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 int gc_cycle;
109 int nretries = 0;
111 /* If the mapping is available, try to search there instead of
112 communicating with the nscd. */
113 struct mapped_database *mapped;
114 mapped = __nscd_get_map_ref (GETFDHST, "hosts", &map_handle, &gc_cycle);
116 retry:;
117 const hst_response_header *hst_resp = NULL;
118 const char *h_name = NULL;
119 const uint32_t *aliases_len = NULL;
120 const char *addr_list = NULL;
121 size_t addr_list_len = 0;
122 int retval = -1;
123 const char *recend = (const char *) ~UINTMAX_C (0);
124 int sock = -1;
125 if (mapped != NO_MAPPING)
127 const struct datahead *found = __nscd_cache_search (type, key, keylen,
128 mapped);
129 if (found != NULL)
131 hst_resp = &found->data[0].hstdata;
132 h_name = (char *) (hst_resp + 1);
133 aliases_len = (uint32_t *) (h_name + hst_resp->h_name_len);
134 addr_list = ((char *) aliases_len
135 + hst_resp->h_aliases_cnt * sizeof (uint32_t));
136 addr_list_len = hst_resp->h_addr_list_cnt * INADDRSZ;
138 #ifndef _STRING_ARCH_unaligned
139 /* The aliases_len array in the mapped database might very
140 well be unaligned. We will access it word-wise so on
141 platforms which do not tolerate unaligned accesses we
142 need to make an aligned copy. */
143 if (((uintptr_t) aliases_len & (__alignof__ (*aliases_len) - 1))
144 != 0)
146 uint32_t *tmp = alloca (hst_resp->h_aliases_cnt
147 * sizeof (uint32_t));
148 aliases_len = memcpy (tmp, aliases_len,
149 hst_resp->h_aliases_cnt
150 * sizeof (uint32_t));
152 #endif
153 if (type != GETHOSTBYADDR && type != GETHOSTBYNAME)
155 if (hst_resp->h_length == INADDRSZ)
156 addr_list += addr_list_len;
157 addr_list_len = hst_resp->h_addr_list_cnt * IN6ADDRSZ;
159 recend = (const char *) found->data + found->recsize;
160 if (__builtin_expect ((const char *) addr_list + addr_list_len
161 > recend, 0))
162 goto out_close;
166 hst_response_header hst_resp_mem;
167 if (hst_resp == NULL)
169 sock = __nscd_open_socket (key, keylen, type, &hst_resp_mem,
170 sizeof (hst_resp_mem));
171 if (sock == -1)
173 __nss_not_use_nscd_hosts = 1;
174 goto out;;
177 hst_resp = &hst_resp_mem;
180 /* No value found so far. */
181 *result = NULL;
183 if (__builtin_expect (hst_resp->found == -1, 0))
185 /* The daemon does not cache this database. */
186 __nss_not_use_nscd_hosts = 1;
187 goto out_close;
190 if (hst_resp->found == 1)
192 struct iovec vec[4];
193 char *cp = buffer;
194 uintptr_t align1;
195 uintptr_t align2;
196 size_t total_len;
197 ssize_t cnt;
198 char *ignore;
199 int n;
201 /* A first check whether the buffer is sufficiently large is possible. */
202 /* Now allocate the buffer the array for the group members. We must
203 align the pointer and the base of the h_addr_list pointers. */
204 align1 = ((__alignof__ (char *) - (cp - ((char *) 0)))
205 & (__alignof__ (char *) - 1));
206 align2 = ((__alignof__ (char *) - ((cp + align1 + hst_resp->h_name_len)
207 - ((char *) 0)))
208 & (__alignof__ (char *) - 1));
209 if (buflen < (align1 + hst_resp->h_name_len + align2
210 + ((hst_resp->h_aliases_cnt + hst_resp->h_addr_list_cnt
211 + 2)
212 * sizeof (char *))
213 + hst_resp->h_addr_list_cnt * (type == AF_INET
214 ? INADDRSZ : IN6ADDRSZ)))
216 no_room:
217 __set_errno (ERANGE);
218 retval = ERANGE;
219 goto out_close;
221 cp += align1;
223 /* Prepare the result as far as we can. */
224 resultbuf->h_aliases = (char **) cp;
225 cp += (hst_resp->h_aliases_cnt + 1) * sizeof (char *);
226 resultbuf->h_addr_list = (char **) cp;
227 cp += (hst_resp->h_addr_list_cnt + 1) * sizeof (char *);
229 resultbuf->h_name = cp;
230 cp += hst_resp->h_name_len + align2;
232 if (type == GETHOSTBYADDR || type == GETHOSTBYNAME)
234 resultbuf->h_addrtype = AF_INET;
235 resultbuf->h_length = INADDRSZ;
237 else
239 resultbuf->h_addrtype = AF_INET6;
240 resultbuf->h_length = IN6ADDRSZ;
242 for (cnt = 0; cnt < hst_resp->h_addr_list_cnt; ++cnt)
244 resultbuf->h_addr_list[cnt] = cp;
245 cp += resultbuf->h_length;
247 resultbuf->h_addr_list[cnt] = NULL;
249 if (h_name == NULL)
251 vec[0].iov_base = resultbuf->h_name;
252 vec[0].iov_len = hst_resp->h_name_len;
253 total_len = hst_resp->h_name_len;
254 n = 1;
256 if (hst_resp->h_aliases_cnt > 0)
258 aliases_len = alloca (hst_resp->h_aliases_cnt
259 * sizeof (uint32_t));
260 vec[n].iov_base = (void *) aliases_len;
261 vec[n].iov_len = hst_resp->h_aliases_cnt * sizeof (uint32_t);
263 total_len += hst_resp->h_aliases_cnt * sizeof (uint32_t);
264 ++n;
267 if (type == GETHOSTBYADDR || type == GETHOSTBYNAME)
269 vec[n].iov_base = resultbuf->h_addr_list[0];
270 vec[n].iov_len = hst_resp->h_addr_list_cnt * INADDRSZ;
272 total_len += hst_resp->h_addr_list_cnt * INADDRSZ;
274 ++n;
276 else
278 if (hst_resp->h_length == INADDRSZ)
280 ignore = alloca (hst_resp->h_addr_list_cnt * INADDRSZ);
281 vec[n].iov_base = ignore;
282 vec[n].iov_len = hst_resp->h_addr_list_cnt * INADDRSZ;
284 total_len += hst_resp->h_addr_list_cnt * INADDRSZ;
286 ++n;
289 vec[n].iov_base = resultbuf->h_addr_list[0];
290 vec[n].iov_len = hst_resp->h_addr_list_cnt * IN6ADDRSZ;
292 total_len += hst_resp->h_addr_list_cnt * IN6ADDRSZ;
294 ++n;
297 if ((size_t) TEMP_FAILURE_RETRY (__readv (sock, vec, n))
298 != total_len)
299 goto out_close;
301 else
303 memcpy (resultbuf->h_name, h_name, hst_resp->h_name_len);
304 memcpy (resultbuf->h_addr_list[0], addr_list, addr_list_len);
307 /* Now we also can read the aliases. */
308 total_len = 0;
309 for (cnt = 0; cnt < hst_resp->h_aliases_cnt; ++cnt)
311 resultbuf->h_aliases[cnt] = cp;
312 cp += aliases_len[cnt];
313 total_len += aliases_len[cnt];
315 resultbuf->h_aliases[cnt] = NULL;
317 if (__builtin_expect ((const char *) addr_list + addr_list_len
318 + total_len > recend, 0))
319 goto out_close;
320 /* See whether this would exceed the buffer capacity. */
321 if (__builtin_expect (cp > buffer + buflen, 0))
322 goto no_room;
324 /* And finally read the aliases. */
325 if (addr_list == NULL)
327 if ((size_t) TEMP_FAILURE_RETRY (__read (sock,
328 resultbuf->h_aliases[0],
329 total_len)) == total_len)
331 retval = 0;
332 *result = resultbuf;
335 else
337 memcpy (resultbuf->h_aliases[0],
338 (const char *) addr_list + addr_list_len, total_len);
340 retval = 0;
341 *result = resultbuf;
344 else
346 /* Store the error number. */
347 *h_errnop = hst_resp->error;
349 /* The `errno' to some value != ERANGE. */
350 __set_errno (ENOENT);
351 /* Even though we have not found anything, the result is zero. */
352 retval = 0;
355 out_close:
356 if (sock != -1)
357 close_not_cancel_no_status (sock);
358 out:
359 if (__nscd_drop_map_ref (mapped, &gc_cycle) != 0 && retval != -1)
361 /* When we come here this means there has been a GC cycle while we
362 were looking for the data. This means the data might have been
363 inconsistent. Retry if possible. */
364 if ((gc_cycle & 1) != 0 || ++nretries == 5)
366 /* nscd is just running gc now. Disable using the mapping. */
367 __nscd_unmap (mapped);
368 mapped = NO_MAPPING;
371 goto retry;
374 return retval;