1 /* Copyright (C) 1998-2002, 2003, 2004, 2005 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
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
;
44 __nscd_gethostbyname_r (const char *name
, struct hostent
*resultbuf
,
45 char *buffer
, size_t buflen
, struct hostent
**result
,
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
);
58 __nscd_gethostbyname2_r (const char *name
, int af
, struct hostent
*resultbuf
,
59 char *buffer
, size_t buflen
, struct hostent
**result
,
64 reqtype
= af
== AF_INET6
? GETHOSTBYNAMEv6
: GETHOSTBYNAME
;
66 return nscd_gethst_r (name
, strlen (name
) + 1, reqtype
, resultbuf
,
67 buffer
, buflen
, result
, h_errnop
);
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
)
78 if (!((len
== INADDRSZ
&& type
== AF_INET
)
79 || (len
== IN6ADDRSZ
&& type
== AF_INET6
)))
80 /* LEN and TYPE do not match. */
83 reqtype
= type
== AF_INET6
? GETHOSTBYADDRv6
: GETHOSTBYADDR
;
85 return nscd_gethst_r (addr
, len
, reqtype
, resultbuf
, buffer
, buflen
, result
,
90 libc_locked_map_ptr (, __hst_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
94 libc_freeres_fn (hst_map_free
)
96 if (__hst_map_handle
.mapped
!= NO_MAPPING
)
98 void *p
= __hst_map_handle
.mapped
;
99 __hst_map_handle
.mapped
= NO_MAPPING
;
107 nscd_gethst_r (const char *key
, size_t keylen
, request_type type
,
108 struct hostent
*resultbuf
, char *buffer
, size_t buflen
,
109 struct hostent
**result
, int *h_errnop
)
114 /* If the mapping is available, try to search there instead of
115 communicating with the nscd. */
116 struct mapped_database
*mapped
;
117 mapped
= __nscd_get_map_ref (GETFDHST
, "hosts", &__hst_map_handle
,
121 const hst_response_header
*hst_resp
= NULL
;
122 const char *h_name
= NULL
;
123 const uint32_t *aliases_len
= NULL
;
124 const char *addr_list
= NULL
;
125 size_t addr_list_len
= 0;
127 const char *recend
= (const char *) ~UINTMAX_C (0);
129 if (mapped
!= NO_MAPPING
)
131 const struct datahead
*found
= __nscd_cache_search (type
, key
, keylen
,
135 hst_resp
= &found
->data
[0].hstdata
;
136 h_name
= (char *) (hst_resp
+ 1);
137 aliases_len
= (uint32_t *) (h_name
+ hst_resp
->h_name_len
);
138 addr_list
= ((char *) aliases_len
139 + hst_resp
->h_aliases_cnt
* sizeof (uint32_t));
140 addr_list_len
= hst_resp
->h_addr_list_cnt
* INADDRSZ
;
142 #ifndef _STRING_ARCH_unaligned
143 /* The aliases_len array in the mapped database might very
144 well be unaligned. We will access it word-wise so on
145 platforms which do not tolerate unaligned accesses we
146 need to make an aligned copy. */
147 if (((uintptr_t) aliases_len
& (__alignof__ (*aliases_len
) - 1))
150 uint32_t *tmp
= alloca (hst_resp
->h_aliases_cnt
151 * sizeof (uint32_t));
152 aliases_len
= memcpy (tmp
, aliases_len
,
153 hst_resp
->h_aliases_cnt
154 * sizeof (uint32_t));
157 if (type
!= GETHOSTBYADDR
&& type
!= GETHOSTBYNAME
)
159 if (hst_resp
->h_length
== INADDRSZ
)
160 addr_list
+= addr_list_len
;
161 addr_list_len
= hst_resp
->h_addr_list_cnt
* IN6ADDRSZ
;
163 recend
= (const char *) found
->data
+ found
->recsize
;
164 if (__builtin_expect ((const char *) addr_list
+ addr_list_len
170 hst_response_header hst_resp_mem
;
171 if (hst_resp
== NULL
)
173 sock
= __nscd_open_socket (key
, keylen
, type
, &hst_resp_mem
,
174 sizeof (hst_resp_mem
));
177 __nss_not_use_nscd_hosts
= 1;
181 hst_resp
= &hst_resp_mem
;
184 /* No value found so far. */
187 if (__builtin_expect (hst_resp
->found
== -1, 0))
189 /* The daemon does not cache this database. */
190 __nss_not_use_nscd_hosts
= 1;
194 if (hst_resp
->found
== 1)
205 /* A first check whether the buffer is sufficiently large is possible. */
206 /* Now allocate the buffer the array for the group members. We must
207 align the pointer and the base of the h_addr_list pointers. */
208 align1
= ((__alignof__ (char *) - (cp
- ((char *) 0)))
209 & (__alignof__ (char *) - 1));
210 align2
= ((__alignof__ (char *) - ((cp
+ align1
+ hst_resp
->h_name_len
)
212 & (__alignof__ (char *) - 1));
213 if (buflen
< (align1
+ hst_resp
->h_name_len
+ align2
214 + ((hst_resp
->h_aliases_cnt
+ hst_resp
->h_addr_list_cnt
217 + hst_resp
->h_addr_list_cnt
* (type
== AF_INET
218 ? INADDRSZ
: IN6ADDRSZ
)))
221 *h_errnop
= NETDB_INTERNAL
;
222 __set_errno (ERANGE
);
228 /* Prepare the result as far as we can. */
229 resultbuf
->h_aliases
= (char **) cp
;
230 cp
+= (hst_resp
->h_aliases_cnt
+ 1) * sizeof (char *);
231 resultbuf
->h_addr_list
= (char **) cp
;
232 cp
+= (hst_resp
->h_addr_list_cnt
+ 1) * sizeof (char *);
234 resultbuf
->h_name
= cp
;
235 cp
+= hst_resp
->h_name_len
+ align2
;
237 if (type
== GETHOSTBYADDR
|| type
== GETHOSTBYNAME
)
239 resultbuf
->h_addrtype
= AF_INET
;
240 resultbuf
->h_length
= INADDRSZ
;
244 resultbuf
->h_addrtype
= AF_INET6
;
245 resultbuf
->h_length
= IN6ADDRSZ
;
247 for (cnt
= 0; cnt
< hst_resp
->h_addr_list_cnt
; ++cnt
)
249 resultbuf
->h_addr_list
[cnt
] = cp
;
250 cp
+= resultbuf
->h_length
;
252 resultbuf
->h_addr_list
[cnt
] = NULL
;
256 vec
[0].iov_base
= resultbuf
->h_name
;
257 vec
[0].iov_len
= hst_resp
->h_name_len
;
258 total_len
= hst_resp
->h_name_len
;
261 if (hst_resp
->h_aliases_cnt
> 0)
263 aliases_len
= alloca (hst_resp
->h_aliases_cnt
264 * sizeof (uint32_t));
265 vec
[n
].iov_base
= (void *) aliases_len
;
266 vec
[n
].iov_len
= hst_resp
->h_aliases_cnt
* sizeof (uint32_t);
268 total_len
+= hst_resp
->h_aliases_cnt
* sizeof (uint32_t);
272 if (type
== GETHOSTBYADDR
|| type
== GETHOSTBYNAME
)
274 vec
[n
].iov_base
= resultbuf
->h_addr_list
[0];
275 vec
[n
].iov_len
= hst_resp
->h_addr_list_cnt
* INADDRSZ
;
277 total_len
+= hst_resp
->h_addr_list_cnt
* INADDRSZ
;
283 if (hst_resp
->h_length
== INADDRSZ
)
285 ignore
= alloca (hst_resp
->h_addr_list_cnt
* INADDRSZ
);
286 vec
[n
].iov_base
= ignore
;
287 vec
[n
].iov_len
= hst_resp
->h_addr_list_cnt
* INADDRSZ
;
289 total_len
+= hst_resp
->h_addr_list_cnt
* INADDRSZ
;
294 vec
[n
].iov_base
= resultbuf
->h_addr_list
[0];
295 vec
[n
].iov_len
= hst_resp
->h_addr_list_cnt
* IN6ADDRSZ
;
297 total_len
+= hst_resp
->h_addr_list_cnt
* IN6ADDRSZ
;
302 if ((size_t) __readvall (sock
, vec
, n
) != total_len
)
307 memcpy (resultbuf
->h_name
, h_name
, hst_resp
->h_name_len
);
308 memcpy (resultbuf
->h_addr_list
[0], addr_list
, addr_list_len
);
311 /* Now we also can read the aliases. */
313 for (cnt
= 0; cnt
< hst_resp
->h_aliases_cnt
; ++cnt
)
315 resultbuf
->h_aliases
[cnt
] = cp
;
316 cp
+= aliases_len
[cnt
];
317 total_len
+= aliases_len
[cnt
];
319 resultbuf
->h_aliases
[cnt
] = NULL
;
321 if (__builtin_expect ((const char *) addr_list
+ addr_list_len
322 + total_len
> recend
, 0))
324 /* See whether this would exceed the buffer capacity. */
325 if (__builtin_expect (cp
> buffer
+ buflen
, 0))
328 /* And finally read the aliases. */
329 if (addr_list
== NULL
)
331 if ((size_t) __readall (sock
, resultbuf
->h_aliases
[0], total_len
)
340 memcpy (resultbuf
->h_aliases
[0],
341 (const char *) addr_list
+ addr_list_len
, total_len
);
343 /* Try to detect corrupt databases. */
344 if (resultbuf
->h_name
[hst_resp
->h_name_len
- 1] != '\0'
345 || ({for (cnt
= 0; cnt
< hst_resp
->h_aliases_cnt
; ++cnt
)
346 if (resultbuf
->h_aliases
[cnt
][aliases_len
[cnt
] - 1]
349 cnt
< hst_resp
->h_aliases_cnt
; }))
350 /* We cannot use the database. */
359 /* Store the error number. */
360 *h_errnop
= hst_resp
->error
;
362 /* The `errno' to some value != ERANGE. */
363 __set_errno (ENOENT
);
364 /* Even though we have not found anything, the result is zero. */
370 close_not_cancel_no_status (sock
);
372 if (__nscd_drop_map_ref (mapped
, &gc_cycle
) != 0 && retval
!= -1)
374 /* When we come here this means there has been a GC cycle while we
375 were looking for the data. This means the data might have been
376 inconsistent. Retry if possible. */
377 if ((gc_cycle
& 1) != 0 || ++nretries
== 5)
379 /* nscd is just running gc now. Disable using the mapping. */
380 __nscd_unmap (mapped
);