2004-09-18 Roland McGrath <roland@redhat.com>
[glibc/history.git] / nscd / nscd_gethst_r.c
blob42327dd317547266f1487c31e3c2b8d2d4282788
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 <sys/mman.h>
31 #include <not-cancel.h>
33 #include "nscd-client.h"
34 #include "nscd_proto.h"
36 int __nss_not_use_nscd_hosts;
38 static int nscd_gethst_r (const char *key, size_t keylen, request_type type,
39 struct hostent *resultbuf, char *buffer,
40 size_t buflen, struct hostent **result,
41 int *h_errnop) internal_function;
44 int
45 __nscd_gethostbyname_r (const char *name, struct hostent *resultbuf,
46 char *buffer, size_t buflen, struct hostent **result,
47 int *h_errnop)
49 request_type reqtype;
51 reqtype = (_res.options & RES_USE_INET6) ? GETHOSTBYNAMEv6 : GETHOSTBYNAME;
53 return nscd_gethst_r (name, strlen (name) + 1, reqtype, resultbuf,
54 buffer, buflen, result, h_errnop);
58 int
59 __nscd_gethostbyname2_r (const char *name, int af, struct hostent *resultbuf,
60 char *buffer, size_t buflen, struct hostent **result,
61 int *h_errnop)
63 request_type reqtype;
65 reqtype = af == AF_INET6 ? GETHOSTBYNAMEv6 : GETHOSTBYNAME;
67 return nscd_gethst_r (name, strlen (name) + 1, reqtype, resultbuf,
68 buffer, buflen, result, h_errnop);
72 int
73 __nscd_gethostbyaddr_r (const void *addr, socklen_t len, int type,
74 struct hostent *resultbuf, char *buffer, size_t buflen,
75 struct hostent **result, int *h_errnop)
77 request_type reqtype;
79 if (!((len == INADDRSZ && type == AF_INET)
80 || (len == IN6ADDRSZ && type == AF_INET6)))
81 /* LEN and TYPE do not match. */
82 return -1;
84 reqtype = type == AF_INET6 ? GETHOSTBYADDRv6 : GETHOSTBYADDR;
86 return nscd_gethst_r (addr, len, reqtype, resultbuf, buffer, buflen, result,
87 h_errnop);
91 libc_locked_map_ptr (map_handle);
92 /* Note that we only free the structure if necessary. The memory
93 mapping is not removed since it is not visible to the malloc
94 handling. */
95 libc_freeres_fn (gr_map_free)
98 if (map_handle.mapped != NO_MAPPING)
99 free (map_handle.mapped);
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 const hst_response_header *hst_resp = NULL;
110 const char *h_name = NULL;
111 const uint32_t *aliases_len = NULL;
112 const char *addr_list = NULL;
113 size_t addr_list_len = 0;
114 int retval = -1;
115 int gc_cycle;
116 const char *recend = (const char *) ~UINTMAX_C (0);
117 int sock = -1;
119 /* If the mapping is available, try to search there instead of
120 communicating with the nscd. */
121 struct mapped_database *mapped = __nscd_get_map_ref (GETFDHST, "hosts",
122 &map_handle, &gc_cycle);
123 retry:
124 if (mapped != MAP_FAILED)
126 const struct datahead *found = __nscd_cache_search (type, key, keylen,
127 mapped);
128 if (found != NULL)
130 hst_resp = &found->data[0].hstdata;
131 h_name = (char *) (hst_resp + 1);
132 aliases_len = (uint32_t *) (h_name + hst_resp->h_name_len);
133 addr_list = ((char *) aliases_len
134 + hst_resp->h_aliases_cnt * sizeof (uint32_t));
135 addr_list_len = hst_resp->h_addr_list_cnt * INADDRSZ;
137 #ifndef _STRING_ARCH_unaligned
138 /* The aliases_len array in the mapped database might very
139 well be unaligned. We will access it word-wise so on
140 platforms which do not tolerate unaligned accesses we
141 need to make an aligned copy. */
142 if (((uintptr_t) aliases_len & (__alignof__ (*aliases_len) - 1))
143 != 0)
145 uint32_t *tmp = alloca (hst_resp->h_aliases_cnt
146 * sizeof (uint32_t));
147 aliases_len = memcpy (tmp, aliases_len,
148 hst_resp->h_aliases_cnt
149 * sizeof (uint32_t));
151 #endif
152 if (type != GETHOSTBYADDR && type != GETHOSTBYNAME)
154 if (hst_resp->h_length == INADDRSZ)
155 addr_list += addr_list_len;
156 addr_list_len = hst_resp->h_addr_list_cnt * IN6ADDRSZ;
158 recend = (const char *) found->data + found->recsize;
159 if (__builtin_expect ((const char *) addr_list + addr_list_len
160 > recend, 0))
161 goto out_close;
165 hst_response_header hst_resp_mem;
166 if (hst_resp == NULL)
168 sock = __nscd_open_socket (key, keylen, type, &hst_resp_mem,
169 sizeof (hst_resp_mem));
170 if (sock == -1)
172 __nss_not_use_nscd_hosts = 1;
173 goto out;;
176 hst_resp = &hst_resp_mem;
179 /* No value found so far. */
180 *result = NULL;
182 if (__builtin_expect (hst_resp->found == -1, 0))
184 /* The daemon does not cache this database. */
185 __nss_not_use_nscd_hosts = 1;
186 goto out_close;
189 if (hst_resp->found == 1)
191 struct iovec vec[4];
192 char *cp = buffer;
193 uintptr_t align1;
194 uintptr_t align2;
195 size_t total_len;
196 ssize_t cnt;
197 char *ignore;
198 int n;
200 /* A first check whether the buffer is sufficiently large is possible. */
201 /* Now allocate the buffer the array for the group members. We must
202 align the pointer and the base of the h_addr_list pointers. */
203 align1 = ((__alignof__ (char *) - (cp - ((char *) 0)))
204 & (__alignof__ (char *) - 1));
205 align2 = ((__alignof__ (char *) - ((cp + align1 + hst_resp->h_name_len)
206 - ((char *) 0)))
207 & (__alignof__ (char *) - 1));
208 if (buflen < (align1 + hst_resp->h_name_len + align2
209 + ((hst_resp->h_aliases_cnt + hst_resp->h_addr_list_cnt
210 + 2)
211 * sizeof (char *))
212 + hst_resp->h_addr_list_cnt * (type == AF_INET
213 ? INADDRSZ : IN6ADDRSZ)))
215 no_room:
216 __set_errno (ERANGE);
217 retval = ERANGE;
218 goto out_close;
220 cp += align1;
222 /* Prepare the result as far as we can. */
223 resultbuf->h_aliases = (char **) cp;
224 cp += (hst_resp->h_aliases_cnt + 1) * sizeof (char *);
225 resultbuf->h_addr_list = (char **) cp;
226 cp += (hst_resp->h_addr_list_cnt + 1) * sizeof (char *);
228 resultbuf->h_name = cp;
229 cp += hst_resp->h_name_len + align2;
231 if (type == GETHOSTBYADDR || type == GETHOSTBYNAME)
233 resultbuf->h_addrtype = AF_INET;
234 resultbuf->h_length = INADDRSZ;
236 else
238 resultbuf->h_addrtype = AF_INET6;
239 resultbuf->h_length = IN6ADDRSZ;
241 for (cnt = 0; cnt < hst_resp->h_addr_list_cnt; ++cnt)
243 resultbuf->h_addr_list[cnt] = cp;
244 cp += resultbuf->h_length;
246 resultbuf->h_addr_list[cnt] = NULL;
248 if (h_name == NULL)
250 vec[0].iov_base = resultbuf->h_name;
251 vec[0].iov_len = hst_resp->h_name_len;
252 total_len = hst_resp->h_name_len;
253 n = 1;
255 if (hst_resp->h_aliases_cnt > 0)
257 aliases_len = alloca (hst_resp->h_aliases_cnt
258 * sizeof (uint32_t));
259 vec[n].iov_base = (void *) aliases_len;
260 vec[n].iov_len = hst_resp->h_aliases_cnt * sizeof (uint32_t);
262 total_len += hst_resp->h_aliases_cnt * sizeof (uint32_t);
263 ++n;
266 if (type == GETHOSTBYADDR || type == GETHOSTBYNAME)
268 vec[n].iov_base = resultbuf->h_addr_list[0];
269 vec[n].iov_len = hst_resp->h_addr_list_cnt * INADDRSZ;
271 total_len += hst_resp->h_addr_list_cnt * INADDRSZ;
273 ++n;
275 else
277 if (hst_resp->h_length == INADDRSZ)
279 ignore = alloca (hst_resp->h_addr_list_cnt * INADDRSZ);
280 vec[n].iov_base = ignore;
281 vec[n].iov_len = hst_resp->h_addr_list_cnt * INADDRSZ;
283 total_len += hst_resp->h_addr_list_cnt * INADDRSZ;
285 ++n;
288 vec[n].iov_base = resultbuf->h_addr_list[0];
289 vec[n].iov_len = hst_resp->h_addr_list_cnt * IN6ADDRSZ;
291 total_len += hst_resp->h_addr_list_cnt * IN6ADDRSZ;
293 ++n;
296 if ((size_t) TEMP_FAILURE_RETRY (__readv (sock, vec, n))
297 != total_len)
298 goto out_close;
300 else
302 memcpy (resultbuf->h_name, h_name, hst_resp->h_name_len);
303 memcpy (resultbuf->h_addr_list[0], addr_list, addr_list_len);
306 /* Now we also can read the aliases. */
307 total_len = 0;
308 for (cnt = 0; cnt < hst_resp->h_aliases_cnt; ++cnt)
310 resultbuf->h_aliases[cnt] = cp;
311 cp += aliases_len[cnt];
312 total_len += aliases_len[cnt];
314 resultbuf->h_aliases[cnt] = NULL;
316 if (__builtin_expect ((const char *) addr_list + addr_list_len
317 + total_len > recend, 0))
318 goto out_close;
319 /* See whether this would exceed the buffer capacity. */
320 if (__builtin_expect (cp > buffer + buflen, 0))
321 goto no_room;
323 /* And finally read the aliases. */
324 if (addr_list == NULL)
326 if ((size_t) TEMP_FAILURE_RETRY (__read (sock,
327 resultbuf->h_aliases[0],
328 total_len)) == total_len)
330 retval = 0;
331 *result = resultbuf;
334 else
336 memcpy (resultbuf->h_aliases[0],
337 (const char *) addr_list + addr_list_len, total_len);
339 retval = 0;
340 *result = resultbuf;
343 else
345 /* Store the error number. */
346 *h_errnop = hst_resp->error;
348 /* The `errno' to some value != ERANGE. */
349 __set_errno (ENOENT);
350 /* Even though we have not found anything, the result is zero. */
351 retval = 0;
354 out_close:
355 if (sock != -1)
356 close_not_cancel_no_status (sock);
357 out:
358 if (__nscd_drop_map_ref (mapped, gc_cycle) != 0)
359 /* When we come here this means there has been a GC cycle while we
360 were looking for the data. This means the data might have been
361 inconsistent. Retry. */
362 goto retry;
364 return retval;