1 /* Copyright (C) 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2004.
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
26 #include <not-cancel.h>
28 #include "nscd-client.h"
29 #include "nscd_proto.h"
32 /* Define in nscd_gethst_r.c. */
33 extern int __nss_not_use_nscd_hosts
;
36 /* We use the mapping from nscd_gethst. */
37 libc_locked_map_ptr (extern, __hst_map_handle
) attribute_hidden
;
41 __nscd_getai (const char *key
, struct nscd_ai_result
**result
, int *h_errnop
)
43 size_t keylen
= strlen (key
) + 1;
47 /* If the mapping is available, try to search there instead of
48 communicating with the nscd. */
49 struct mapped_database
*mapped
;
50 mapped
= __nscd_get_map_ref (GETFDHST
, "hosts", &__hst_map_handle
,
54 struct nscd_ai_result
*resultbuf
= NULL
;
55 const char *recend
= (const char *) ~UINTMAX_C (0);
56 char *respdata
= NULL
;
59 ai_response_header ai_resp
;
61 if (mapped
!= NO_MAPPING
)
63 struct datahead
*found
= __nscd_cache_search (GETAI
, key
, keylen
,
67 respdata
= (char *) (&found
->data
[0].aidata
+ 1);
68 ai_resp
= found
->data
[0].aidata
;
69 recend
= (const char *) found
->data
+ found
->recsize
;
70 /* Now check if we can trust ai_resp fields. If GC is
71 in progress, it can contain anything. */
72 if (mapped
->head
->gc_cycle
!= gc_cycle
)
80 /* If we do not have the cache mapped, try to get the data over the
84 sock
= __nscd_open_socket (key
, keylen
, GETAI
, &ai_resp
,
88 /* nscd not running or wrong version. */
89 __nss_not_use_nscd_hosts
= 1;
94 if (ai_resp
.found
== 1)
96 size_t datalen
= ai_resp
.naddrs
+ ai_resp
.addrslen
+ ai_resp
.canonlen
;
98 /* This check really only affects the case where the data
99 comes from the mapped cache. */
100 if (respdata
+ datalen
> recend
)
107 resultbuf
= (struct nscd_ai_result
*) malloc (sizeof (*resultbuf
)
109 if (resultbuf
== NULL
)
111 *h_errnop
= NETDB_INTERNAL
;
115 /* Set up the data structure, including pointers. */
116 resultbuf
->naddrs
= ai_resp
.naddrs
;
117 resultbuf
->addrs
= (char *) (resultbuf
+ 1);
118 resultbuf
->family
= (uint8_t *) (resultbuf
->addrs
+ ai_resp
.addrslen
);
119 if (ai_resp
.canonlen
!= 0)
120 resultbuf
->canon
= (char *) (resultbuf
->family
+ resultbuf
->naddrs
);
122 resultbuf
->canon
= NULL
;
124 if (respdata
== NULL
)
126 /* Read the data from the socket. */
127 if ((size_t) __readall (sock
, resultbuf
+ 1, datalen
) == datalen
)
135 *h_errnop
= NETDB_INTERNAL
;
140 /* Copy the data in the block. */
141 memcpy (resultbuf
+ 1, respdata
, datalen
);
143 /* Try to detect corrupt databases. */
144 if (resultbuf
->canon
!= NULL
145 && resultbuf
->canon
[ai_resp
.canonlen
- 1] != '\0')
146 /* We cannot use the database. */
148 if (mapped
->head
->gc_cycle
!= gc_cycle
)
161 if (__builtin_expect (ai_resp
.found
== -1, 0))
163 /* The daemon does not cache this database. */
164 __nss_not_use_nscd_hosts
= 1;
168 /* Store the error number. */
169 *h_errnop
= ai_resp
.error
;
171 /* The `errno' to some value != ERANGE. */
172 __set_errno (ENOENT
);
173 /* Even though we have not found anything, the result is zero. */
179 close_not_cancel_no_status (sock
);
181 if (__nscd_drop_map_ref (mapped
, &gc_cycle
) != 0)
183 /* When we come here this means there has been a GC cycle while we
184 were looking for the data. This means the data might have been
185 inconsistent. Retry if possible. */
186 if ((gc_cycle
& 1) != 0 || ++nretries
== 5 || retval
== -1)
188 /* nscd is just running gc now. Disable using the mapping. */
189 if (atomic_decrement_val (&mapped
->counter
) == 0)
190 __nscd_unmap (mapped
);