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
29 #include <arpa/nameser.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
;
45 __nscd_gethostbyname_r (const char *name
, struct hostent
*resultbuf
,
46 char *buffer
, size_t buflen
, struct hostent
**result
,
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
);
59 __nscd_gethostbyname2_r (const char *name
, int af
, struct hostent
*resultbuf
,
60 char *buffer
, size_t buflen
, struct hostent
**result
,
65 reqtype
= af
== AF_INET6
? GETHOSTBYNAMEv6
: GETHOSTBYNAME
;
67 return nscd_gethst_r (name
, strlen (name
) + 1, reqtype
, resultbuf
,
68 buffer
, buflen
, result
, h_errnop
);
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
)
79 if (!((len
== INADDRSZ
&& type
== AF_INET
)
80 || (len
== IN6ADDRSZ
&& type
== AF_INET6
)))
81 /* LEN and TYPE do not match. */
84 reqtype
= type
== AF_INET6
? GETHOSTBYADDRv6
: GETHOSTBYADDR
;
86 return nscd_gethst_r (addr
, len
, reqtype
, resultbuf
, buffer
, buflen
, result
,
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
95 libc_freeres_fn (gr_map_free
)
98 if (map_handle
.mapped
!= NO_MAPPING
)
99 free (map_handle
.mapped
);
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;
116 const char *recend
= (const char *) ~UINTMAX_C (0);
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
);
124 if (mapped
!= MAP_FAILED
)
126 const struct datahead
*found
= __nscd_cache_search (type
, key
, keylen
,
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))
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));
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
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
));
172 __nss_not_use_nscd_hosts
= 1;
176 hst_resp
= &hst_resp_mem
;
179 /* No value found so far. */
182 if (__builtin_expect (hst_resp
->found
== -1, 0))
184 /* The daemon does not cache this database. */
185 __nss_not_use_nscd_hosts
= 1;
189 if (hst_resp
->found
== 1)
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
)
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
212 + hst_resp
->h_addr_list_cnt
* (type
== AF_INET
213 ? INADDRSZ
: IN6ADDRSZ
)))
216 __set_errno (ERANGE
);
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
;
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
;
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
;
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);
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
;
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
;
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
;
296 if ((size_t) TEMP_FAILURE_RETRY (__readv (sock
, vec
, n
))
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. */
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))
319 /* See whether this would exceed the buffer capacity. */
320 if (__builtin_expect (cp
> buffer
+ buflen
, 0))
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
)
336 memcpy (resultbuf
->h_aliases
[0],
337 (const char *) addr_list
+ addr_list_len
, total_len
);
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. */
356 close_not_cancel_no_status (sock
);
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. */