Update.
[glibc.git] / nscd / hstcache.c
blob4e3af9ca40c0e17e4985603f6c215270d8e98487
1 /* Cache handling for host lookup.
2 Copyright (C) 1998 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include <assert.h>
22 #include <errno.h>
23 #include <error.h>
24 #include <netdb.h>
25 #include <stddef.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <time.h>
30 #include <unistd.h>
31 #include <arpa/inet.h>
33 #include "nscd.h"
34 #include "dbg_log.h"
36 /* Get implementation for some internal functions. */
37 #include "../resolv/mapv4v6addr.h"
40 /* This is the standard reply in case the service is disabled. */
41 static const hst_response_header disabled =
43 version: NSCD_VERSION,
44 found: -1,
45 h_name_len: 0,
46 h_aliases_cnt: 0,
47 h_addrtype: -1,
48 h_length: -1,
49 h_addr_list_cnt: 0,
50 error: NETDB_INTERNAL
53 /* This is the struct describing how to write this record. */
54 const struct iovec hst_iov_disabled =
56 iov_base: (void *) &disabled,
57 iov_len: sizeof (disabled)
61 /* This is the standard reply in case we haven't found the dataset. */
62 static const hst_response_header notfound =
64 version: NSCD_VERSION,
65 found: 0,
66 h_name_len: 0,
67 h_aliases_cnt: 0,
68 h_addrtype: -1,
69 h_length: -1,
70 h_addr_list_cnt: 0,
71 error: HOST_NOT_FOUND
74 /* This is the struct describing how to write this record. */
75 static const struct iovec iov_notfound =
77 iov_base: (void *) &notfound,
78 iov_len: sizeof (notfound)
82 struct hostdata
84 hst_response_header resp;
85 char strdata[0];
89 static void
90 cache_addhst (struct database *db, int fd, request_header *req, void *key,
91 struct hostent *hst)
93 ssize_t total;
94 ssize_t written;
95 time_t t = time (NULL);
97 if (hst == NULL)
99 /* We have no data. This means we send the standard reply for this
100 case. */
101 void *copy;
103 total = sizeof (notfound);
105 written = writev (fd, &iov_notfound, 1);
107 copy = malloc (req->key_len);
108 if (copy == NULL)
109 error (EXIT_FAILURE, errno, _("while allocating key copy"));
110 memcpy (copy, key, req->key_len);
112 /* Compute the timeout time. */
113 t += db->negtimeout;
115 /* Now get the lock to safely insert the records. */
116 pthread_rwlock_rdlock (&db->lock);
118 cache_add (req->type, copy, req->key_len, &iov_notfound,
119 sizeof (notfound), (void *) -1, 0, t, db);
121 pthread_rwlock_unlock (&db->lock);
123 else
125 /* Determine the I/O structure. */
126 struct hostdata *data;
127 size_t h_name_len = strlen (hst->h_name) + 1;
128 size_t h_aliases_cnt;
129 size_t *h_aliases_len;
130 size_t h_addr_list_cnt;
131 int addr_list_type;
132 char *addresses;
133 char *aliases;
134 char *key_copy = NULL;
135 char *cp;
136 size_t cnt;
138 /* Determine the number of aliases. */
139 h_aliases_cnt = 0;
140 for (cnt = 0; hst->h_aliases[cnt] != NULL; ++cnt)
141 ++h_aliases_cnt;
142 /* Determine the length of all aliases. */
143 h_aliases_len = alloca (h_aliases_cnt * sizeof (size_t));
144 total = 0;
145 for (cnt = 0; cnt < h_aliases_cnt; ++cnt)
147 h_aliases_len[cnt] = strlen (hst->h_aliases[cnt]) + 1;
148 total += h_aliases_len[cnt];
151 /* Determine the number of addresses. */
152 h_addr_list_cnt = 0;
153 for (cnt = 0; hst->h_addr_list[cnt]; ++cnt)
154 ++h_addr_list_cnt;
156 /* We allocate all data in one memory block: the iov vector,
157 the response header and the dataset itself. */
158 total += (sizeof (struct hostdata)
159 + h_name_len
160 + h_aliases_cnt * sizeof (size_t)
161 + h_addr_list_cnt * (hst->h_length
162 + (hst->h_length == INADDRSZ
163 ? IN6ADDRSZ : 0)));
165 data = (struct hostdata *) malloc (total + req->key_len);
166 if (data == NULL)
167 /* There is no reason to go on. */
168 error (EXIT_FAILURE, errno, _("while allocating cache entry"));
170 data->resp.found = 1;
171 data->resp.h_name_len = h_name_len;
172 data->resp.h_aliases_cnt = h_aliases_cnt;
173 data->resp.h_addrtype = hst->h_addrtype;
174 data->resp.h_length = hst->h_length;
175 data->resp.h_addr_list_cnt = h_addr_list_cnt;
176 data->resp.error = NETDB_SUCCESS;
178 cp = data->strdata;
180 cp = mempcpy (cp, hst->h_name, h_name_len);
181 cp = mempcpy (cp, h_aliases_len, h_aliases_cnt * sizeof (size_t));
183 /* The normal addresses first. */
184 addresses = cp;
185 for (cnt = 0; cnt < h_addr_list_cnt; ++cnt)
186 cp = mempcpy (cp, hst->h_addr_list[cnt], hst->h_length);
188 /* And the generated IPv6 addresses if necessary. */
189 if (hst->h_length == INADDRSZ)
191 /* Generate the IPv6 addresses. */
192 for (cnt = 0; cnt < h_addr_list_cnt; cp += IN6ADDRSZ, ++cnt)
193 map_v4v6_address (hst->h_addr_list[cnt], cp);
196 /* Then the aliases. */
197 aliases = cp;
198 for (cnt = 0; cnt < h_aliases_cnt; ++cnt)
199 cp = mempcpy (cp, hst->h_aliases[cnt], h_aliases_len[cnt]);
201 assert (cp == data->strdata + total - sizeof (hst_response_header));
203 /* If we are adding a GETHOSTBYNAME{,v6} entry we must be prepared
204 that the answer we get from the NSS does not contain the key
205 itself. This is the case if the resolver is used and the name
206 is extended by the domainnames from /etc/resolv.conf. Therefore
207 we explicitly add the name here. */
208 if (req->type == GETHOSTBYNAME || req->type == GETHOSTBYNAMEv6)
209 key_copy = memcpy (cp, key, req->key_len);
211 /* We write the dataset before inserting it to the database
212 since while inserting this thread might block and so would
213 unnecessarily let the receiver wait. */
214 written = write (fd, data, total);
216 addr_list_type = (hst->h_length == INADDRSZ
217 ? GETHOSTBYADDR : GETHOSTBYADDRv6);
219 /* Compute the timeout time. */
220 t += db->postimeout;
222 /* Now get the lock to safely insert the records. */
223 pthread_rwlock_rdlock (&db->lock);
225 /* First add all the aliases. */
226 for (cnt = 0; cnt < h_aliases_cnt; ++cnt)
228 if (addr_list_type == GETHOSTBYADDR)
229 cache_add (GETHOSTBYNAME, aliases, h_aliases_len[cnt], data, total,
230 data, 0, t, db);
232 cache_add (GETHOSTBYNAMEv6, aliases, h_aliases_len[cnt], data, total,
233 data, 0, t, db);
235 aliases += h_aliases_len[cnt];
238 /* Next the normal addresses. */
239 for (cnt = 0; cnt < h_addr_list_cnt; ++cnt)
241 cache_add (addr_list_type, addresses, hst->h_length, data, total,
242 data, 0, t, db);
243 addresses += hst->h_length;
246 /* If necessary the IPv6 addresses. */
247 if (addr_list_type == GETHOSTBYADDR)
248 for (cnt = 0; cnt < h_addr_list_cnt; ++cnt)
250 cache_add (GETHOSTBYADDRv6, addresses, IN6ADDRSZ, data, total,
251 data, 0, t, db);
252 addresses += IN6ADDRSZ;
255 /* If necessary add the key for this request. */
256 if (req->type == GETHOSTBYNAME || req->type == GETHOSTBYNAMEv6)
258 if (addr_list_type == GETHOSTBYADDR)
259 cache_add (GETHOSTBYNAME, key_copy, req->key_len, data, total,
260 data, 0, t, db);
261 cache_add (GETHOSTBYNAMEv6, key_copy, req->key_len, data,
262 total, data, 0, t, db);
265 /* And finally the name. We mark this as the last entry. */
266 if (addr_list_type == GETHOSTBYADDR)
267 cache_add (GETHOSTBYNAME, data->strdata, h_name_len, data, total, data,
268 0, t, db);
269 cache_add (GETHOSTBYNAMEv6, data->strdata, h_name_len, data,
270 total, data, 1, t, db);
272 pthread_rwlock_unlock (&db->lock);
275 if (written != total)
277 char buf[256];
278 dbg_log (_("short write in %s: %s"), __FUNCTION__,
279 strerror_r (errno, buf, sizeof (buf)));
284 void
285 addhstbyname (struct database *db, int fd, request_header *req, void *key)
287 /* Search for the entry matching the key. Please note that we don't
288 look again in the table whether the dataset is now available. We
289 simply insert it. It does not matter if it is in there twice. The
290 pruning function only will look at the timestamp. */
291 int buflen = 512;
292 char *buffer = alloca (buflen);
293 struct hostent resultbuf;
294 struct hostent *hst;
296 if (debug_level > 0)
297 dbg_log (_("Haven't found \"%s\" in hosts cache!"), key);
299 while (gethostbyname2_r (key, AF_INET, &resultbuf, buffer, buflen, &hst,
300 &h_errno) != 0
301 && h_errno == NETDB_INTERNAL
302 && errno == ERANGE)
304 errno = 0;
305 buflen += 256;
306 buffer = alloca (buflen);
309 cache_addhst (db, fd, req, key, hst);
313 void
314 addhstbyaddr (struct database *db, int fd, request_header *req, void *key)
316 /* Search for the entry matching the key. Please note that we don't
317 look again in the table whether the dataset is now available. We
318 simply insert it. It does not matter if it is in there twice. The
319 pruning function only will look at the timestamp. */
320 int buflen = 512;
321 char *buffer = alloca (buflen);
322 struct hostent resultbuf;
323 struct hostent *hst;
325 if (debug_level > 0)
327 char buf[64];
328 dbg_log (_("Haven't found \"%s\" in hosts cache!"),
329 inet_ntop (AF_INET, key, buf, sizeof (buf)));
332 while (gethostbyaddr_r (key, INADDRSZ, AF_INET, &resultbuf, buffer, buflen,
333 &hst, &h_errno) != 0
334 && h_errno == NETDB_INTERNAL
335 && errno == ERANGE)
337 errno = 0;
338 buflen += 256;
339 buffer = alloca (buflen);
342 cache_addhst (db, fd, req, key, hst);
346 void
347 addhstbynamev6 (struct database *db, int fd, request_header *req, void *key)
349 /* Search for the entry matching the key. Please note that we don't
350 look again in the table whether the dataset is now available. We
351 simply insert it. It does not matter if it is in there twice. The
352 pruning function only will look at the timestamp. */
353 int buflen = 512;
354 char *buffer = alloca (buflen);
355 struct hostent resultbuf;
356 struct hostent *hst;
358 if (debug_level > 0)
359 dbg_log (_("Haven't found \"%s\" in hosts cache!"), key);
361 while (gethostbyname2_r (key, AF_INET6, &resultbuf, buffer, buflen, &hst,
362 &h_errno) != 0
363 && h_errno == NETDB_INTERNAL
364 && errno == ERANGE)
366 errno = 0;
367 buflen += 256;
368 buffer = alloca (buflen);
371 cache_addhst (db, fd, req, key, hst);
375 void
376 addhstbyaddrv6 (struct database *db, int fd, request_header *req, void *key)
378 /* Search for the entry matching the key. Please note that we don't
379 look again in the table whether the dataset is now available. We
380 simply insert it. It does not matter if it is in there twice. The
381 pruning function only will look at the timestamp. */
382 int buflen = 512;
383 char *buffer = alloca (buflen);
384 struct hostent resultbuf;
385 struct hostent *hst;
387 if (debug_level > 0)
389 char buf[64];
390 dbg_log (_("Haven't found \"%s\" in hosts cache!"),
391 inet_ntop (AF_INET6, key, buf, sizeof (buf)));
394 while (gethostbyaddr_r (key, IN6ADDRSZ, AF_INET6, &resultbuf, buffer, buflen,
395 &hst, &h_errno) != 0
396 && h_errno == NETDB_INTERNAL
397 && errno == ERANGE)
399 errno = 0;
400 buflen += 256;
401 buffer = alloca (buflen);
404 cache_addhst (db, fd, req, key, hst);