1 /* Cache handling for host lookup.
2 Copyright (C) 1998-2005, 2006, 2007 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; version 2 of the License, or
9 (at your option) any later version.
11 This program 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
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
33 #include <arpa/inet.h>
34 #include <arpa/nameser.h>
36 #include <stackinfo.h>
41 # include <kernel-features.h>
45 /* This is the standard reply in case the service is disabled. */
46 static const hst_response_header disabled =
48 .version = NSCD_VERSION,
55 .error = NETDB_INTERNAL
58 /* This is the struct describing how to write this record. */
59 const struct iovec hst_iov_disabled =
61 .iov_base = (void *) &disabled,
62 .iov_len = sizeof (disabled)
66 /* This is the standard reply in case we haven't found the dataset. */
67 static const hst_response_header notfound =
69 .version = NSCD_VERSION,
76 .error = HOST_NOT_FOUND
81 cache_addhst (struct database_dyn *db, int fd, request_header *req,
82 const void *key, struct hostent *hst, uid_t owner,
83 struct hashentry *he, struct datahead *dh, int errval)
87 time_t t = time (NULL);
89 /* We allocate all data in one memory block: the iov vector,
90 the response header and the dataset itself. */
94 hst_response_header resp;
98 assert (offsetof (struct dataset, resp) == offsetof (struct datahead, data));
102 if (he != NULL && errval == EAGAIN)
104 /* If we have an old record available but cannot find one
105 now because the service is not available we keep the old
106 record and make sure it does not get removed. */
107 if (reload_count != UINT_MAX)
108 /* Do not reset the value if we never not reload the record. */
109 dh->nreloads = reload_count - 1;
115 /* We have no data. This means we send the standard reply for this
117 written = total = sizeof (notfound);
120 written = TEMP_FAILURE_RETRY (send (fd, ¬found, total,
123 dataset = mempool_alloc (db, sizeof (struct dataset) + req->key_len);
124 /* If we cannot permanently store the result, so be it. */
127 dataset->head.allocsize = sizeof (struct dataset) + req->key_len;
128 dataset->head.recsize = total;
129 dataset->head.notfound = true;
130 dataset->head.nreloads = 0;
131 dataset->head.usable = true;
133 /* Compute the timeout time. */
134 dataset->head.timeout = t + db->negtimeout;
136 /* This is the reply. */
137 memcpy (&dataset->resp, ¬found, total);
139 /* Copy the key data. */
140 memcpy (dataset->strdata, key, req->key_len);
142 /* If necessary, we also propagate the data to disk. */
146 uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
147 msync ((void *) pval,
148 ((uintptr_t) dataset & pagesize_m1)
149 + sizeof (struct dataset) + req->key_len, MS_ASYNC);
152 /* Now get the lock to safely insert the records. */
153 pthread_rwlock_rdlock (&db->lock);
155 if (cache_add (req->type, &dataset->strdata, req->key_len,
156 &dataset->head, true, db, owner) < 0)
157 /* Ensure the data can be recovered. */
158 dataset->head.usable = false;
160 pthread_rwlock_unlock (&db->lock);
162 /* Mark the old entry as obsolete. */
167 ++db->head->addfailed;
172 /* Determine the I/O structure. */
173 size_t h_name_len = strlen (hst->h_name) + 1;
174 size_t h_aliases_cnt;
175 uint32_t *h_aliases_len;
176 size_t h_addr_list_cnt;
180 char *key_copy = NULL;
184 /* Determine the number of aliases. */
186 for (cnt = 0; hst->h_aliases[cnt] != NULL; ++cnt)
188 /* Determine the length of all aliases. */
189 h_aliases_len = (uint32_t *) alloca (h_aliases_cnt * sizeof (uint32_t));
191 for (cnt = 0; cnt < h_aliases_cnt; ++cnt)
193 h_aliases_len[cnt] = strlen (hst->h_aliases[cnt]) + 1;
194 total += h_aliases_len[cnt];
197 /* Determine the number of addresses. */
199 for (cnt = 0; hst->h_addr_list[cnt]; ++cnt)
202 if (h_addr_list_cnt == 0)
206 total += (sizeof (struct dataset)
208 + h_aliases_cnt * sizeof (uint32_t)
209 + h_addr_list_cnt * hst->h_length);
212 /* If we refill the cache, first assume the reconrd did not
213 change. Allocate memory on the cache since it is likely
214 discarded anyway. If it turns out to be necessary to have a
215 new record we can still allocate real memory. */
216 bool alloca_used = false;
219 /* If the record contains more than one IP address (used for
220 load balancing etc) don't cache the entry. This is something
221 the current cache handling cannot handle and it is more than
222 questionable whether it is worthwhile complicating the cache
223 handling just for handling such a special case. */
224 if (he == NULL && h_addr_list_cnt == 1)
226 dataset = (struct dataset *) mempool_alloc (db,
227 total + req->key_len);
229 ++db->head->addfailed;
234 /* We cannot permanently add the result in the moment. But
235 we can provide the result as is. Store the data in some
237 dataset = (struct dataset *) alloca (total + req->key_len);
239 /* We cannot add this record to the permanent database. */
243 dataset->head.allocsize = total + req->key_len;
244 dataset->head.recsize = total - offsetof (struct dataset, resp);
245 dataset->head.notfound = false;
246 dataset->head.nreloads = he == NULL ? 0 : (dh->nreloads + 1);
247 dataset->head.usable = true;
249 /* Compute the timeout time. */
250 dataset->head.timeout = t + db->postimeout;
252 dataset->resp.version = NSCD_VERSION;
253 dataset->resp.found = 1;
254 dataset->resp.h_name_len = h_name_len;
255 dataset->resp.h_aliases_cnt = h_aliases_cnt;
256 dataset->resp.h_addrtype = hst->h_addrtype;
257 dataset->resp.h_length = hst->h_length;
258 dataset->resp.h_addr_list_cnt = h_addr_list_cnt;
259 dataset->resp.error = NETDB_SUCCESS;
261 cp = dataset->strdata;
263 cp = mempcpy (cp, hst->h_name, h_name_len);
264 cp = mempcpy (cp, h_aliases_len, h_aliases_cnt * sizeof (uint32_t));
266 /* The normal addresses first. */
268 for (cnt = 0; cnt < h_addr_list_cnt; ++cnt)
269 cp = mempcpy (cp, hst->h_addr_list[cnt], hst->h_length);
271 /* Then the aliases. */
273 for (cnt = 0; cnt < h_aliases_cnt; ++cnt)
274 cp = mempcpy (cp, hst->h_aliases[cnt], h_aliases_len[cnt]);
277 == dataset->strdata + total - offsetof (struct dataset,
280 /* If we are adding a GETHOSTBYNAME{,v6} entry we must be prepared
281 that the answer we get from the NSS does not contain the key
282 itself. This is the case if the resolver is used and the name
283 is extended by the domainnames from /etc/resolv.conf. Therefore
284 we explicitly add the name here. */
285 key_copy = memcpy (cp, key, req->key_len);
287 /* Now we can determine whether on refill we have to create a new
293 if (total + req->key_len == dh->allocsize
294 && total - offsetof (struct dataset, resp) == dh->recsize
295 && memcmp (&dataset->resp, dh->data,
296 dh->allocsize - offsetof (struct dataset, resp)) == 0)
298 /* The data has not changed. We will just bump the
299 timeout value. Note that the new record has been
300 allocated on the stack and need not be freed. */
301 dh->timeout = dataset->head.timeout;
306 /* We have to create a new record. Just allocate
307 appropriate memory and copy it. */
309 = (struct dataset *) mempool_alloc (db, total + req->key_len);
312 /* Adjust pointers into the memory block. */
313 addresses = (char *) newp + (addresses - (char *) dataset);
314 aliases = (char *) newp + (aliases - (char *) dataset);
315 assert (key_copy != NULL);
316 key_copy = (char *) newp + (key_copy - (char *) dataset);
318 dataset = memcpy (newp, dataset, total + req->key_len);
322 /* Mark the old record as obsolete. */
328 /* We write the dataset before inserting it to the database
329 since while inserting this thread might block and so would
330 unnecessarily keep the receiver waiting. */
334 if (__builtin_expect (db->mmap_used, 1) && !alloca_used)
336 assert (db->wr_fd != -1);
337 assert ((char *) &dataset->resp > (char *) db->data);
338 assert ((char *) &dataset->resp - (char *) db->head
340 <= (sizeof (struct database_pers_head)
341 + db->head->module * sizeof (ref_t)
342 + db->head->data_size));
343 written = sendfileall (fd, db->wr_fd,
344 (char *) &dataset->resp
345 - (char *) db->head, total);
346 # ifndef __ASSUME_SENDFILE
347 if (written == -1 && errno == ENOSYS)
352 # ifndef __ASSUME_SENDFILE
356 written = writeall (fd, &dataset->resp, total);
359 /* Add the record to the database. But only if it has not been
362 If the record contains more than one IP address (used for
363 load balancing etc) don't cache the entry. This is something
364 the current cache handling cannot handle and it is more than
365 questionable whether it is worthwhile complicating the cache
366 handling just for handling such a special case. */
369 /* If necessary, we also propagate the data to disk. */
373 uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
374 msync ((void *) pval,
375 ((uintptr_t) dataset & pagesize_m1)
376 + total + req->key_len, MS_ASYNC);
379 addr_list_type = (hst->h_length == NS_INADDRSZ
380 ? GETHOSTBYADDR : GETHOSTBYADDRv6);
382 /* Now get the lock to safely insert the records. */
383 pthread_rwlock_rdlock (&db->lock);
385 /* NB: the following code is really complicated. It has
386 seemlingly duplicated code paths which do the same. The
387 problem is that we always must add the hash table entry
388 with the FIRST flag set first. Otherwise we get dangling
389 pointers in case memory allocation fails. */
390 assert (hst->h_addr_list[1] == NULL);
392 /* Avoid adding names if more than one address is available. See
393 above for more info. */
394 assert (req->type == GETHOSTBYNAME
395 || req->type == GETHOSTBYNAMEv6
396 || req->type == GETHOSTBYADDR
397 || req->type == GETHOSTBYADDRv6);
399 if (cache_add (req->type, key_copy, req->key_len,
400 &dataset->head, true, db, owner) < 0)
401 /* Could not allocate memory. Make sure the
402 data gets discarded. */
403 dataset->head.usable = false;
405 pthread_rwlock_unlock (&db->lock);
409 if (__builtin_expect (written != total, 0) && debug_level > 0)
412 dbg_log (_("short write in %s: %s"), __FUNCTION__,
413 strerror_r (errno, buf, sizeof (buf)));
419 lookup (int type, void *key, struct hostent *resultbufp, char *buffer,
420 size_t buflen, struct hostent **hst)
422 if (type == GETHOSTBYNAME)
423 return __gethostbyname2_r (key, AF_INET, resultbufp, buffer, buflen, hst,
425 if (type == GETHOSTBYNAMEv6)
426 return __gethostbyname2_r (key, AF_INET6, resultbufp, buffer, buflen, hst,
428 if (type == GETHOSTBYADDR)
429 return __gethostbyaddr_r (key, NS_INADDRSZ, AF_INET, resultbufp, buffer,
430 buflen, hst, &h_errno);
431 return __gethostbyaddr_r (key, NS_IN6ADDRSZ, AF_INET6, resultbufp, buffer,
432 buflen, hst, &h_errno);
437 addhstbyX (struct database_dyn *db, int fd, request_header *req,
438 void *key, uid_t uid, struct hashentry *he, struct datahead *dh)
440 /* Search for the entry matching the key. Please note that we don't
441 look again in the table whether the dataset is now available. We
442 simply insert it. It does not matter if it is in there twice. The
443 pruning function only will look at the timestamp. */
445 char *buffer = (char *) alloca (buflen);
446 struct hostent resultbuf;
448 bool use_malloc = false;
451 if (__builtin_expect (debug_level > 0, 0))
454 char buf[INET6_ADDRSTRLEN + 1];
455 if (req->type == GETHOSTBYNAME || req->type == GETHOSTBYNAMEv6)
458 str = inet_ntop (req->type == GETHOSTBYADDR ? AF_INET : AF_INET6,
459 key, buf, sizeof (buf));
462 dbg_log (_("Haven't found \"%s\" in hosts cache!"), (char *) str);
464 dbg_log (_("Reloading \"%s\" in hosts cache!"), (char *) str);
467 while (lookup (req->type, key, &resultbuf, buffer, buflen, &hst) != 0
468 && h_errno == NETDB_INTERNAL
469 && (errval = errno) == ERANGE)
473 if (__builtin_expect (buflen > 32768, 0))
475 char *old_buffer = buffer;
477 buffer = (char *) realloc (use_malloc ? buffer : NULL, buflen);
480 /* We ran out of memory. We cannot do anything but
481 sending a negative response. In reality this should
486 /* We set the error to indicate this is (possibly) a
487 temporary error and that it does not mean the entry
488 is not available at all. */
495 /* Allocate a new buffer on the stack. If possible combine it
496 with the previously allocated buffer. */
497 buffer = (char *) extend_alloca (buffer, buflen, 2 * buflen);
500 cache_addhst (db, fd, req, key, hst, uid, he, dh,
501 h_errno == TRY_AGAIN ? errval : 0);
509 addhstbyname (struct database_dyn *db, int fd, request_header *req,
510 void *key, uid_t uid)
512 addhstbyX (db, fd, req, key, uid, NULL, NULL);
517 readdhstbyname (struct database_dyn *db, struct hashentry *he,
522 .type = GETHOSTBYNAME,
526 addhstbyX (db, -1, &req, db->data + he->key, he->owner, he, dh);
531 addhstbyaddr (struct database_dyn *db, int fd, request_header *req,
532 void *key, uid_t uid)
534 addhstbyX (db, fd, req, key, uid, NULL, NULL);
539 readdhstbyaddr (struct database_dyn *db, struct hashentry *he,
544 .type = GETHOSTBYADDR,
548 addhstbyX (db, -1, &req, db->data + he->key, he->owner, he, dh);
553 addhstbynamev6 (struct database_dyn *db, int fd, request_header *req,
554 void *key, uid_t uid)
556 addhstbyX (db, fd, req, key, uid, NULL, NULL);
561 readdhstbynamev6 (struct database_dyn *db, struct hashentry *he,
566 .type = GETHOSTBYNAMEv6,
570 addhstbyX (db, -1, &req, db->data + he->key, he->owner, he, dh);
575 addhstbyaddrv6 (struct database_dyn *db, int fd, request_header *req,
576 void *key, uid_t uid)
578 addhstbyX (db, fd, req, key, uid, NULL, NULL);
583 readdhstbyaddrv6 (struct database_dyn *db, struct hashentry *he,
588 .type = GETHOSTBYADDRv6,
592 addhstbyX (db, -1, &req, db->data + he->key, he->owner, he, dh);