1 /* Cache handling for host lookup.
2 Copyright (C) 1998-2014 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, see <http://www.gnu.org/licenses/>. */
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
80 /* This is the standard reply in case there are temporary problems. */
81 static const hst_response_header tryagain =
83 .version = NSCD_VERSION,
95 cache_addhst (struct database_dyn *db, int fd, request_header *req,
96 const void *key, struct hostent *hst, uid_t owner,
97 struct hashentry *const he, struct datahead *dh, int errval,
100 bool all_written = true;
101 time_t t = time (NULL);
103 /* We allocate all data in one memory block: the iov vector,
104 the response header and the dataset itself. */
107 struct datahead head;
108 hst_response_header resp;
112 assert (offsetof (struct dataset, resp) == offsetof (struct datahead, data));
114 time_t timeout = MAX_TIMEOUT_VALUE;
117 if (he != NULL && errval == EAGAIN)
119 /* If we have an old record available but cannot find one
120 now because the service is not available we keep the old
121 record and make sure it does not get removed. */
122 if (reload_count != UINT_MAX)
123 /* Do not reset the value if we never not reload the record. */
124 dh->nreloads = reload_count - 1;
126 /* Reload with the same time-to-live value. */
127 timeout = dh->timeout = t + dh->ttl;
131 /* We have no data. This means we send the standard reply for this
132 case. Possibly this is only temporary. */
133 ssize_t total = sizeof (notfound);
134 assert (sizeof (notfound) == sizeof (tryagain));
136 const hst_response_header *resp = (errval == EAGAIN
137 ? &tryagain : ¬found);
140 TEMP_FAILURE_RETRY (send (fd, resp, total,
141 MSG_NOSIGNAL)) != total)
144 /* If we have a transient error or cannot permanently store
145 the result, so be it. */
146 if (errval == EAGAIN || __builtin_expect (db->negtimeout == 0, 0))
148 /* Mark the old entry as obsolete. */
152 else if ((dataset = mempool_alloc (db, (sizeof (struct dataset)
153 + req->key_len), 1)) != NULL)
155 timeout = datahead_init_neg (&dataset->head,
156 (sizeof (struct dataset)
157 + req->key_len), total,
159 ? db->negtimeout : ttl));
161 /* This is the reply. */
162 memcpy (&dataset->resp, resp, total);
164 /* Copy the key data. */
165 memcpy (dataset->strdata, key, req->key_len);
167 /* If necessary, we also propagate the data to disk. */
171 uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
172 msync ((void *) pval,
173 ((uintptr_t) dataset & pagesize_m1)
174 + sizeof (struct dataset) + req->key_len, MS_ASYNC);
177 (void) cache_add (req->type, &dataset->strdata, req->key_len,
178 &dataset->head, true, db, owner, he == NULL);
180 pthread_rwlock_unlock (&db->lock);
182 /* Mark the old entry as obsolete. */
190 /* Determine the I/O structure. */
191 size_t h_name_len = strlen (hst->h_name) + 1;
192 size_t h_aliases_cnt;
193 uint32_t *h_aliases_len;
194 size_t h_addr_list_cnt;
197 char *key_copy = NULL;
202 /* Determine the number of aliases. */
204 for (cnt = 0; hst->h_aliases[cnt] != NULL; ++cnt)
206 /* Determine the length of all aliases. */
207 h_aliases_len = (uint32_t *) alloca (h_aliases_cnt * sizeof (uint32_t));
209 for (cnt = 0; cnt < h_aliases_cnt; ++cnt)
211 h_aliases_len[cnt] = strlen (hst->h_aliases[cnt]) + 1;
212 total += h_aliases_len[cnt];
215 /* Determine the number of addresses. */
217 while (hst->h_addr_list[h_addr_list_cnt] != NULL)
220 if (h_addr_list_cnt == 0)
222 return MAX_TIMEOUT_VALUE;
224 total += (sizeof (struct dataset)
226 + h_aliases_cnt * sizeof (uint32_t)
227 + h_addr_list_cnt * hst->h_length);
229 /* If we refill the cache, first assume the reconrd did not
230 change. Allocate memory on the cache since it is likely
231 discarded anyway. If it turns out to be necessary to have a
232 new record we can still allocate real memory. */
233 bool alloca_used = false;
236 /* If the record contains more than one IP address (used for
237 load balancing etc) don't cache the entry. This is something
238 the current cache handling cannot handle and it is more than
239 questionable whether it is worthwhile complicating the cache
240 handling just for handling such a special case. */
241 if (he == NULL && h_addr_list_cnt == 1)
242 dataset = (struct dataset *) mempool_alloc (db, total + req->key_len,
247 /* We cannot permanently add the result in the moment. But
248 we can provide the result as is. Store the data in some
250 dataset = (struct dataset *) alloca (total + req->key_len);
252 /* We cannot add this record to the permanent database. */
256 timeout = datahead_init_pos (&dataset->head, total + req->key_len,
257 total - offsetof (struct dataset, resp),
258 he == NULL ? 0 : dh->nreloads + 1,
259 ttl == INT32_MAX ? db->postimeout : ttl);
261 dataset->resp.version = NSCD_VERSION;
262 dataset->resp.found = 1;
263 dataset->resp.h_name_len = h_name_len;
264 dataset->resp.h_aliases_cnt = h_aliases_cnt;
265 dataset->resp.h_addrtype = hst->h_addrtype;
266 dataset->resp.h_length = hst->h_length;
267 dataset->resp.h_addr_list_cnt = h_addr_list_cnt;
268 dataset->resp.error = NETDB_SUCCESS;
270 /* Make sure there is no gap. */
271 assert ((char *) (&dataset->resp.error + 1) == dataset->strdata);
273 cp = dataset->strdata;
275 cp = mempcpy (cp, hst->h_name, h_name_len);
276 cp = mempcpy (cp, h_aliases_len, h_aliases_cnt * sizeof (uint32_t));
278 /* The normal addresses first. */
280 for (cnt = 0; cnt < h_addr_list_cnt; ++cnt)
281 cp = mempcpy (cp, hst->h_addr_list[cnt], hst->h_length);
283 /* Then the aliases. */
285 for (cnt = 0; cnt < h_aliases_cnt; ++cnt)
286 cp = mempcpy (cp, hst->h_aliases[cnt], h_aliases_len[cnt]);
289 == dataset->strdata + total - offsetof (struct dataset,
292 /* If we are adding a GETHOSTBYNAME{,v6} entry we must be prepared
293 that the answer we get from the NSS does not contain the key
294 itself. This is the case if the resolver is used and the name
295 is extended by the domainnames from /etc/resolv.conf. Therefore
296 we explicitly add the name here. */
297 key_copy = memcpy (cp, key, req->key_len);
299 assert ((char *) &dataset->resp + dataset->head.recsize == cp);
301 /* Now we can determine whether on refill we have to create a new
307 if (total + req->key_len == dh->allocsize
308 && total - offsetof (struct dataset, resp) == dh->recsize
309 && memcmp (&dataset->resp, dh->data,
310 dh->allocsize - offsetof (struct dataset, resp)) == 0)
312 /* The data has not changed. We will just bump the
313 timeout value. Note that the new record has been
314 allocated on the stack and need not be freed. */
315 assert (h_addr_list_cnt == 1);
316 dh->ttl = dataset->head.ttl;
317 dh->timeout = dataset->head.timeout;
322 if (h_addr_list_cnt == 1)
324 /* We have to create a new record. Just allocate
325 appropriate memory and copy it. */
327 = (struct dataset *) mempool_alloc (db,
328 total + req->key_len,
332 /* Adjust pointers into the memory block. */
333 addresses = (char *) newp + (addresses
335 aliases = (char *) newp + (aliases - (char *) dataset);
336 assert (key_copy != NULL);
337 key_copy = (char *) newp + (key_copy - (char *) dataset);
339 dataset = memcpy (newp, dataset, total + req->key_len);
344 /* Mark the old record as obsolete. */
350 /* We write the dataset before inserting it to the database
351 since while inserting this thread might block and so would
352 unnecessarily keep the receiver waiting. */
356 if (__builtin_expect (db->mmap_used, 1) && !alloca_used)
358 assert (db->wr_fd != -1);
359 assert ((char *) &dataset->resp > (char *) db->data);
360 assert ((char *) dataset - (char *) db->head
362 <= (sizeof (struct database_pers_head)
363 + db->head->module * sizeof (ref_t)
364 + db->head->data_size));
365 ssize_t written = sendfileall (fd, db->wr_fd,
366 (char *) &dataset->resp
368 dataset->head.recsize);
369 if (written != dataset->head.recsize)
371 # ifndef __ASSUME_SENDFILE
372 if (written == -1 && errno == ENOSYS)
379 # ifndef __ASSUME_SENDFILE
383 if (writeall (fd, &dataset->resp, dataset->head.recsize)
384 != dataset->head.recsize)
388 /* Add the record to the database. But only if it has not been
391 If the record contains more than one IP address (used for
392 load balancing etc) don't cache the entry. This is something
393 the current cache handling cannot handle and it is more than
394 questionable whether it is worthwhile complicating the cache
395 handling just for handling such a special case. */
398 /* If necessary, we also propagate the data to disk. */
402 uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
403 msync ((void *) pval,
404 ((uintptr_t) dataset & pagesize_m1)
405 + total + req->key_len, MS_ASYNC);
408 /* NB: the following code is really complicated. It has
409 seemlingly duplicated code paths which do the same. The
410 problem is that we always must add the hash table entry
411 with the FIRST flag set first. Otherwise we get dangling
412 pointers in case memory allocation fails. */
413 assert (hst->h_addr_list[1] == NULL);
415 /* Avoid adding names if more than one address is available. See
416 above for more info. */
417 assert (req->type == GETHOSTBYNAME
418 || req->type == GETHOSTBYNAMEv6
419 || req->type == GETHOSTBYADDR
420 || req->type == GETHOSTBYADDRv6);
422 (void) cache_add (req->type, key_copy, req->key_len,
423 &dataset->head, true, db, owner, he == NULL);
425 pthread_rwlock_unlock (&db->lock);
429 if (__builtin_expect (!all_written, 0) && debug_level > 0)
432 dbg_log (_("short write in %s: %s"), __FUNCTION__,
433 strerror_r (errno, buf, sizeof (buf)));
441 lookup (int type, void *key, struct hostent *resultbufp, char *buffer,
442 size_t buflen, struct hostent **hst, int32_t *ttlp)
444 if (type == GETHOSTBYNAME)
445 return __gethostbyname3_r (key, AF_INET, resultbufp, buffer, buflen, hst,
446 &h_errno, ttlp, NULL);
447 if (type == GETHOSTBYNAMEv6)
448 return __gethostbyname3_r (key, AF_INET6, resultbufp, buffer, buflen, hst,
449 &h_errno, ttlp, NULL);
450 if (type == GETHOSTBYADDR)
451 return __gethostbyaddr2_r (key, NS_INADDRSZ, AF_INET, resultbufp, buffer,
452 buflen, hst, &h_errno, ttlp);
453 return __gethostbyaddr2_r (key, NS_IN6ADDRSZ, AF_INET6, resultbufp, buffer,
454 buflen, hst, &h_errno, ttlp);
459 addhstbyX (struct database_dyn *db, int fd, request_header *req,
460 void *key, uid_t uid, struct hashentry *he, struct datahead *dh)
462 /* Search for the entry matching the key. Please note that we don't
463 look again in the table whether the dataset is now available. We
464 simply insert it. It does not matter if it is in there twice. The
465 pruning function only will look at the timestamp. */
467 char *buffer = (char *) alloca (buflen);
468 struct hostent resultbuf;
470 bool use_malloc = false;
472 int32_t ttl = INT32_MAX;
474 if (__glibc_unlikely (debug_level > 0))
477 char buf[INET6_ADDRSTRLEN + 1];
478 if (req->type == GETHOSTBYNAME || req->type == GETHOSTBYNAMEv6)
481 str = inet_ntop (req->type == GETHOSTBYADDR ? AF_INET : AF_INET6,
482 key, buf, sizeof (buf));
485 dbg_log (_("Haven't found \"%s\" in hosts cache!"), (char *) str);
487 dbg_log (_("Reloading \"%s\" in hosts cache!"), (char *) str);
490 while (lookup (req->type, key, &resultbuf, buffer, buflen, &hst, &ttl) != 0
491 && h_errno == NETDB_INTERNAL
492 && (errval = errno) == ERANGE)
496 if (__glibc_unlikely (buflen > 32768))
498 char *old_buffer = buffer;
500 buffer = (char *) realloc (use_malloc ? buffer : NULL, buflen);
503 /* We ran out of memory. We cannot do anything but
504 sending a negative response. In reality this should
509 /* We set the error to indicate this is (possibly) a
510 temporary error and that it does not mean the entry
511 is not available at all. */
519 /* Allocate a new buffer on the stack. If possible combine it
520 with the previously allocated buffer. */
521 buffer = (char *) extend_alloca (buffer, buflen, 2 * buflen);
524 time_t timeout = cache_addhst (db, fd, req, key, hst, uid, he, dh,
525 h_errno == TRY_AGAIN ? errval : 0, ttl);
535 addhstbyname (struct database_dyn *db, int fd, request_header *req,
536 void *key, uid_t uid)
538 addhstbyX (db, fd, req, key, uid, NULL, NULL);
543 readdhstbyname (struct database_dyn *db, struct hashentry *he,
548 .type = GETHOSTBYNAME,
552 return addhstbyX (db, -1, &req, db->data + he->key, he->owner, he, dh);
557 addhstbyaddr (struct database_dyn *db, int fd, request_header *req,
558 void *key, uid_t uid)
560 addhstbyX (db, fd, req, key, uid, NULL, NULL);
565 readdhstbyaddr (struct database_dyn *db, struct hashentry *he,
570 .type = GETHOSTBYADDR,
574 return addhstbyX (db, -1, &req, db->data + he->key, he->owner, he, dh);
579 addhstbynamev6 (struct database_dyn *db, int fd, request_header *req,
580 void *key, uid_t uid)
582 addhstbyX (db, fd, req, key, uid, NULL, NULL);
587 readdhstbynamev6 (struct database_dyn *db, struct hashentry *he,
592 .type = GETHOSTBYNAMEv6,
596 return addhstbyX (db, -1, &req, db->data + he->key, he->owner, he, dh);
601 addhstbyaddrv6 (struct database_dyn *db, int fd, request_header *req,
602 void *key, uid_t uid)
604 addhstbyX (db, fd, req, key, uid, NULL, NULL);
609 readdhstbyaddrv6 (struct database_dyn *db, struct hashentry *he,
614 .type = GETHOSTBYADDRv6,
618 return addhstbyX (db, -1, &req, db->data + he->key, he->owner, he, dh);