1 /* Copyright (C) 2004-2014 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, see
17 <http://www.gnu.org/licenses/>. */
25 #include <not-cancel.h>
27 #include "nscd-client.h"
28 #include "nscd_proto.h"
31 /* Define in nscd_gethst_r.c. */
32 extern int __nss_not_use_nscd_hosts
;
35 /* We use the mapping from nscd_gethst. */
36 libc_locked_map_ptr (extern, __hst_map_handle
) attribute_hidden
;
38 /* Defined in nscd_gethst_r.c. */
39 extern int __nss_have_localdomain attribute_hidden
;
43 __nscd_getai (const char *key
, struct nscd_ai_result
**result
, int *h_errnop
)
45 if (__glibc_unlikely (__nss_have_localdomain
>= 0))
47 if (__nss_have_localdomain
== 0)
48 __nss_have_localdomain
= getenv ("LOCALDOMAIN") != NULL
? 1 : -1;
49 if (__nss_have_localdomain
> 0)
51 __nss_not_use_nscd_hosts
= 1;
56 size_t keylen
= strlen (key
) + 1;
60 /* If the mapping is available, try to search there instead of
61 communicating with the nscd. */
62 struct mapped_database
*mapped
;
63 mapped
= __nscd_get_map_ref (GETFDHST
, "hosts", &__hst_map_handle
,
67 struct nscd_ai_result
*resultbuf
= NULL
;
68 const char *recend
= (const char *) ~UINTMAX_C (0);
69 char *respdata
= NULL
;
72 ai_response_header ai_resp
;
74 if (mapped
!= NO_MAPPING
)
76 struct datahead
*found
= __nscd_cache_search (GETAI
, key
, keylen
,
77 mapped
, sizeof ai_resp
);
80 respdata
= (char *) (&found
->data
[0].aidata
+ 1);
81 ai_resp
= found
->data
[0].aidata
;
82 recend
= (const char *) found
->data
+ found
->recsize
;
83 /* Now check if we can trust ai_resp fields. If GC is
84 in progress, it can contain anything. */
85 if (mapped
->head
->gc_cycle
!= gc_cycle
)
93 /* If we do not have the cache mapped, try to get the data over the
97 sock
= __nscd_open_socket (key
, keylen
, GETAI
, &ai_resp
,
101 /* nscd not running or wrong version. */
102 __nss_not_use_nscd_hosts
= 1;
107 if (ai_resp
.found
== 1)
109 size_t datalen
= ai_resp
.naddrs
+ ai_resp
.addrslen
+ ai_resp
.canonlen
;
111 /* This check really only affects the case where the data
112 comes from the mapped cache. */
113 if (respdata
+ datalen
> recend
)
120 resultbuf
= (struct nscd_ai_result
*) malloc (sizeof (*resultbuf
)
122 if (resultbuf
== NULL
)
124 *h_errnop
= NETDB_INTERNAL
;
128 /* Set up the data structure, including pointers. */
129 resultbuf
->naddrs
= ai_resp
.naddrs
;
130 resultbuf
->addrs
= (char *) (resultbuf
+ 1);
131 resultbuf
->family
= (uint8_t *) (resultbuf
->addrs
+ ai_resp
.addrslen
);
132 if (ai_resp
.canonlen
!= 0)
133 resultbuf
->canon
= (char *) (resultbuf
->family
+ resultbuf
->naddrs
);
135 resultbuf
->canon
= NULL
;
137 if (respdata
== NULL
)
139 /* Read the data from the socket. */
140 if ((size_t) __readall (sock
, resultbuf
+ 1, datalen
) == datalen
)
148 *h_errnop
= NETDB_INTERNAL
;
153 /* Copy the data in the block. */
154 memcpy (resultbuf
+ 1, respdata
, datalen
);
156 /* Try to detect corrupt databases. */
157 if (resultbuf
->canon
!= NULL
158 && resultbuf
->canon
[ai_resp
.canonlen
- 1] != '\0')
159 /* We cannot use the database. */
161 if (mapped
->head
->gc_cycle
!= gc_cycle
)
174 if (__glibc_unlikely (ai_resp
.found
== -1))
176 /* The daemon does not cache this database. */
177 __nss_not_use_nscd_hosts
= 1;
181 /* Store the error number. */
182 *h_errnop
= ai_resp
.error
;
184 /* Set errno to 0 to indicate no error, just no found record. */
186 /* Even though we have not found anything, the result is zero. */
192 close_not_cancel_no_status (sock
);
194 if (__nscd_drop_map_ref (mapped
, &gc_cycle
) != 0)
196 /* When we come here this means there has been a GC cycle while we
197 were looking for the data. This means the data might have been
198 inconsistent. Retry if possible. */
199 if ((gc_cycle
& 1) != 0 || ++nretries
== 5 || retval
== -1)
201 /* nscd is just running gc now. Disable using the mapping. */
202 if (atomic_decrement_val (&mapped
->counter
) == 0)
203 __nscd_unmap (mapped
);