1 /* Cache handling for host lookup.
2 Copyright (C) 1998, 1999 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. */
31 #include <arpa/inet.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,
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,
74 /* This is the struct describing how to write this record. */
75 static const struct iovec iov_notfound =
77 iov_base: (void *) ¬found,
78 iov_len: sizeof (notfound)
84 hst_response_header resp;
90 cache_addhst (struct database *db, int fd, request_header *req, void *key,
91 struct hostent *hst, uid_t owner)
95 time_t t = time (NULL);
99 /* We have no data. This means we send the standard reply for this
103 total = sizeof (notfound);
105 written = writev (fd, &iov_notfound, 1);
107 copy = malloc (req->key_len);
109 error (EXIT_FAILURE, errno, _("while allocating key copy"));
110 memcpy (copy, key, req->key_len);
112 /* Compute the timeout time. */
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, owner);
121 pthread_rwlock_unlock (&db->lock);
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;
134 char *key_copy = NULL;
138 /* Determine the number of aliases. */
140 for (cnt = 0; hst->h_aliases[cnt] != NULL; ++cnt)
142 /* Determine the length of all aliases. */
143 h_aliases_len = alloca (h_aliases_cnt * sizeof (size_t));
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. */
153 for (cnt = 0; hst->h_addr_list[cnt]; ++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)
160 + h_aliases_cnt * sizeof (size_t)
161 + h_addr_list_cnt * (hst->h_length
162 + (hst->h_length == INADDRSZ
165 data = (struct hostdata *) malloc (total + req->key_len);
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;
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. */
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. */
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. */
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, owner);
232 cache_add (GETHOSTBYNAMEv6, aliases, h_aliases_len[cnt], data, total,
233 data, 0, t, db, owner);
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, owner);
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, owner);
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, owner);
261 cache_add (GETHOSTBYNAMEv6, key_copy, req->key_len, data,
262 total, data, 0, t, db, owner);
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,
269 cache_add (GETHOSTBYNAMEv6, data->strdata, h_name_len, data,
270 total, data, 1, t, db, owner);
272 pthread_rwlock_unlock (&db->lock);
275 if (written != total)
278 dbg_log (_("short write in %s: %s"), __FUNCTION__,
279 strerror_r (errno, buf, sizeof (buf)));
285 addhstbyname (struct database *db, int fd, request_header *req,
286 void *key, uid_t uid)
288 /* Search for the entry matching the key. Please note that we don't
289 look again in the table whether the dataset is now available. We
290 simply insert it. It does not matter if it is in there twice. The
291 pruning function only will look at the timestamp. */
293 char *buffer = alloca (buflen);
294 struct hostent resultbuf;
299 dbg_log (_("Haven't found \"%s\" in hosts cache!"), key);
303 oldeuid = geteuid ();
307 while (gethostbyname2_r (key, AF_INET, &resultbuf, buffer, buflen, &hst,
309 && h_errno == NETDB_INTERNAL
314 buffer = alloca (buflen);
320 cache_addhst (db, fd, req, key, hst, uid);
325 addhstbyaddr (struct database *db, int fd, request_header *req,
326 void *key, uid_t uid)
328 /* Search for the entry matching the key. Please note that we don't
329 look again in the table whether the dataset is now available. We
330 simply insert it. It does not matter if it is in there twice. The
331 pruning function only will look at the timestamp. */
333 char *buffer = alloca (buflen);
334 struct hostent resultbuf;
340 char buf[INET_ADDRSTRLEN];
341 dbg_log (_("Haven't found \"%s\" in hosts cache!"),
342 inet_ntop (AF_INET, key, buf, sizeof (buf)));
347 oldeuid = geteuid ();
351 while (gethostbyaddr_r (key, INADDRSZ, AF_INET, &resultbuf, buffer, buflen,
353 && h_errno == NETDB_INTERNAL
358 buffer = alloca (buflen);
364 cache_addhst (db, fd, req, key, hst, uid);
369 addhstbynamev6 (struct database *db, int fd, request_header *req,
370 void *key, uid_t uid)
372 /* Search for the entry matching the key. Please note that we don't
373 look again in the table whether the dataset is now available. We
374 simply insert it. It does not matter if it is in there twice. The
375 pruning function only will look at the timestamp. */
377 char *buffer = alloca (buflen);
378 struct hostent resultbuf;
384 char buf[INET6_ADDRSTRLEN];
386 dbg_log (_("Haven't found \"%s\" in hosts cache!"),
387 inet_ntop (AF_INET6, key, buf, sizeof (buf)));
392 oldeuid = geteuid ();
396 while (gethostbyname2_r (key, AF_INET6, &resultbuf, buffer, buflen, &hst,
398 && h_errno == NETDB_INTERNAL
403 buffer = alloca (buflen);
409 cache_addhst (db, fd, req, key, hst, uid);
414 addhstbyaddrv6 (struct database *db, int fd, request_header *req,
415 void *key, uid_t uid)
417 /* Search for the entry matching the key. Please note that we don't
418 look again in the table whether the dataset is now available. We
419 simply insert it. It does not matter if it is in there twice. The
420 pruning function only will look at the timestamp. */
422 char *buffer = alloca (buflen);
423 struct hostent resultbuf;
429 char buf[INET6_ADDRSTRLEN];
430 dbg_log (_("Haven't found \"%s\" in hosts cache!"),
431 inet_ntop (AF_INET6, key, buf, sizeof (buf)));
436 oldeuid = geteuid ();
440 while (gethostbyaddr_r (key, IN6ADDRSZ, AF_INET6, &resultbuf, buffer, buflen,
442 && h_errno == NETDB_INTERNAL
447 buffer = alloca (buflen);
453 cache_addhst (db, fd, req, key, hst, uid);