.
[glibc/history.git] / nscd / nscd_getai.c
blob674a5e751463a12931076398bc9a785330df799d
1 /* Copyright (C) 2004, 2005, 2006, 2007, 2008 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
18 02111-1307 USA. */
20 #include <assert.h>
21 #include <errno.h>
22 #include <netdb.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
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;
39 /* Defined in nscd_gethst_r.c. */
40 extern int __nss_have_localdomain attribute_hidden;
43 int
44 __nscd_getai (const char *key, struct nscd_ai_result **result, int *h_errnop)
46 if (__builtin_expect (__nss_have_localdomain >= 0, 0))
48 if (__nss_have_localdomain == 0)
49 __nss_have_localdomain = getenv ("LOCALDOMAIN") != NULL ? 1 : -1;
50 if (__nss_have_localdomain > 0)
52 __nss_not_use_nscd_hosts = 1;
53 return -1;
57 size_t keylen = strlen (key) + 1;
58 int gc_cycle;
59 int nretries = 0;
61 /* If the mapping is available, try to search there instead of
62 communicating with the nscd. */
63 struct mapped_database *mapped;
64 mapped = __nscd_get_map_ref (GETFDHST, "hosts", &__hst_map_handle,
65 &gc_cycle);
67 retry:;
68 struct nscd_ai_result *resultbuf = NULL;
69 const char *recend = (const char *) ~UINTMAX_C (0);
70 char *respdata = NULL;
71 int retval = -1;
72 int sock = -1;
73 ai_response_header ai_resp;
75 if (mapped != NO_MAPPING)
77 struct datahead *found = __nscd_cache_search (GETAI, key, keylen,
78 mapped);
79 if (found != NULL)
81 respdata = (char *) (&found->data[0].aidata + 1);
82 ai_resp = found->data[0].aidata;
83 recend = (const char *) found->data + found->recsize;
84 /* Now check if we can trust ai_resp fields. If GC is
85 in progress, it can contain anything. */
86 if (mapped->head->gc_cycle != gc_cycle)
88 retval = -2;
89 goto out;
94 /* If we do not have the cache mapped, try to get the data over the
95 socket. */
96 if (respdata == NULL)
98 sock = __nscd_open_socket (key, keylen, GETAI, &ai_resp,
99 sizeof (ai_resp));
100 if (sock == -1)
102 /* nscd not running or wrong version. */
103 __nss_not_use_nscd_hosts = 1;
104 goto out;
108 if (ai_resp.found == 1)
110 size_t datalen = ai_resp.naddrs + ai_resp.addrslen + ai_resp.canonlen;
112 /* This check really only affects the case where the data
113 comes from the mapped cache. */
114 if (respdata + datalen > recend)
116 assert (sock == -1);
117 goto out;
120 /* Create result. */
121 resultbuf = (struct nscd_ai_result *) malloc (sizeof (*resultbuf)
122 + datalen);
123 if (resultbuf == NULL)
125 *h_errnop = NETDB_INTERNAL;
126 goto out_close;
129 /* Set up the data structure, including pointers. */
130 resultbuf->naddrs = ai_resp.naddrs;
131 resultbuf->addrs = (char *) (resultbuf + 1);
132 resultbuf->family = (uint8_t *) (resultbuf->addrs + ai_resp.addrslen);
133 if (ai_resp.canonlen != 0)
134 resultbuf->canon = (char *) (resultbuf->family + resultbuf->naddrs);
135 else
136 resultbuf->canon = NULL;
138 if (respdata == NULL)
140 /* Read the data from the socket. */
141 if ((size_t) __readall (sock, resultbuf + 1, datalen) == datalen)
143 retval = 0;
144 *result = resultbuf;
146 else
148 free (resultbuf);
149 *h_errnop = NETDB_INTERNAL;
152 else
154 /* Copy the data in the block. */
155 memcpy (resultbuf + 1, respdata, datalen);
157 /* Try to detect corrupt databases. */
158 if (resultbuf->canon != NULL
159 && resultbuf->canon[ai_resp.canonlen - 1] != '\0')
160 /* We cannot use the database. */
162 if (mapped->head->gc_cycle != gc_cycle)
163 retval = -2;
164 else
165 free (resultbuf);
166 goto out_close;
169 retval = 0;
170 *result = resultbuf;
173 else
175 if (__builtin_expect (ai_resp.found == -1, 0))
177 /* The daemon does not cache this database. */
178 __nss_not_use_nscd_hosts = 1;
179 goto out_close;
182 /* Store the error number. */
183 *h_errnop = ai_resp.error;
185 /* Set errno to 0 to indicate no error, just no found record. */
186 __set_errno (0);
187 /* Even though we have not found anything, the result is zero. */
188 retval = 0;
191 out_close:
192 if (sock != -1)
193 close_not_cancel_no_status (sock);
194 out:
195 if (__nscd_drop_map_ref (mapped, &gc_cycle) != 0)
197 /* When we come here this means there has been a GC cycle while we
198 were looking for the data. This means the data might have been
199 inconsistent. Retry if possible. */
200 if ((gc_cycle & 1) != 0 || ++nretries == 5 || retval == -1)
202 /* nscd is just running gc now. Disable using the mapping. */
203 if (atomic_decrement_val (&mapped->counter) == 0)
204 __nscd_unmap (mapped);
205 mapped = NO_MAPPING;
208 if (retval != -1)
210 *result = NULL;
211 free (resultbuf);
212 goto retry;
216 return retval;