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. */
32 #include <arpa/inet.h>
37 /* Get implementation for some internal functions. */
38 #include "../resolv/mapv4v6addr.h"
41 /* This is the standard reply in case the service is disabled. */
42 static const hst_response_header disabled =
44 version: NSCD_VERSION,
54 /* This is the struct describing how to write this record. */
55 const struct iovec hst_iov_disabled =
57 iov_base: (void *) &disabled,
58 iov_len: sizeof (disabled)
62 /* This is the standard reply in case we haven't found the dataset. */
63 static const hst_response_header notfound =
65 version: NSCD_VERSION,
75 /* This is the struct describing how to write this record. */
76 static const struct iovec iov_notfound =
78 iov_base: (void *) ¬found,
79 iov_len: sizeof (notfound)
85 hst_response_header resp;
91 cache_addhst (struct database *db, int fd, request_header *req, void *key,
92 struct hostent *hst, uid_t owner)
96 time_t t = time (NULL);
100 /* We have no data. This means we send the standard reply for this
104 total = sizeof (notfound);
106 written = writev (fd, &iov_notfound, 1);
108 copy = malloc (req->key_len);
110 error (EXIT_FAILURE, errno, _("while allocating key copy"));
111 memcpy (copy, key, req->key_len);
113 /* Compute the timeout time. */
116 /* Now get the lock to safely insert the records. */
117 pthread_rwlock_rdlock (&db->lock);
119 cache_add (req->type, copy, req->key_len, ¬found,
120 sizeof (notfound), (void *) -1, 0, t, db, owner);
122 pthread_rwlock_unlock (&db->lock);
126 /* Determine the I/O structure. */
127 struct hostdata *data;
128 size_t h_name_len = strlen (hst->h_name) + 1;
129 size_t h_aliases_cnt;
130 size_t *h_aliases_len;
131 size_t h_addr_list_cnt;
135 char *key_copy = NULL;
139 /* Determine the number of aliases. */
141 for (cnt = 0; hst->h_aliases[cnt] != NULL; ++cnt)
143 /* Determine the length of all aliases. */
144 h_aliases_len = alloca (h_aliases_cnt * sizeof (size_t));
146 for (cnt = 0; cnt < h_aliases_cnt; ++cnt)
148 h_aliases_len[cnt] = strlen (hst->h_aliases[cnt]) + 1;
149 total += h_aliases_len[cnt];
152 /* Determine the number of addresses. */
154 for (cnt = 0; hst->h_addr_list[cnt]; ++cnt)
157 /* We allocate all data in one memory block: the iov vector,
158 the response header and the dataset itself. */
159 total += (sizeof (struct hostdata)
161 + h_aliases_cnt * sizeof (size_t)
162 + h_addr_list_cnt * (hst->h_length
163 + (hst->h_length == INADDRSZ
166 data = (struct hostdata *) malloc (total + req->key_len);
168 /* There is no reason to go on. */
169 error (EXIT_FAILURE, errno, _("while allocating cache entry"));
171 data->resp.found = 1;
172 data->resp.h_name_len = h_name_len;
173 data->resp.h_aliases_cnt = h_aliases_cnt;
174 data->resp.h_addrtype = hst->h_addrtype;
175 data->resp.h_length = hst->h_length;
176 data->resp.h_addr_list_cnt = h_addr_list_cnt;
177 data->resp.error = NETDB_SUCCESS;
181 cp = mempcpy (cp, hst->h_name, h_name_len);
182 cp = mempcpy (cp, h_aliases_len, h_aliases_cnt * sizeof (size_t));
184 /* The normal addresses first. */
186 for (cnt = 0; cnt < h_addr_list_cnt; ++cnt)
187 cp = mempcpy (cp, hst->h_addr_list[cnt], hst->h_length);
189 /* And the generated IPv6 addresses if necessary. */
190 if (hst->h_length == INADDRSZ)
192 /* Generate the IPv6 addresses. */
193 for (cnt = 0; cnt < h_addr_list_cnt; cp += IN6ADDRSZ, ++cnt)
194 map_v4v6_address (hst->h_addr_list[cnt], cp);
197 /* Then the aliases. */
199 for (cnt = 0; cnt < h_aliases_cnt; ++cnt)
200 cp = mempcpy (cp, hst->h_aliases[cnt], h_aliases_len[cnt]);
202 assert (cp == data->strdata + total - sizeof (hst_response_header));
204 /* If we are adding a GETHOSTBYNAME{,v6} entry we must be prepared
205 that the answer we get from the NSS does not contain the key
206 itself. This is the case if the resolver is used and the name
207 is extended by the domainnames from /etc/resolv.conf. Therefore
208 we explicitly add the name here. */
209 if (req->type == GETHOSTBYNAME || req->type == GETHOSTBYNAMEv6)
210 key_copy = memcpy (cp, key, req->key_len);
212 /* We write the dataset before inserting it to the database
213 since while inserting this thread might block and so would
214 unnecessarily let the receiver wait. */
215 written = write (fd, data, total);
217 addr_list_type = (hst->h_length == INADDRSZ
218 ? GETHOSTBYADDR : GETHOSTBYADDRv6);
220 /* Compute the timeout time. */
223 /* Now get the lock to safely insert the records. */
224 pthread_rwlock_rdlock (&db->lock);
226 /* First add all the aliases. */
227 for (cnt = 0; cnt < h_aliases_cnt; ++cnt)
229 if (addr_list_type == GETHOSTBYADDR)
230 cache_add (GETHOSTBYNAME, aliases, h_aliases_len[cnt], data, total,
231 data, 0, t, db, owner);
233 cache_add (GETHOSTBYNAMEv6, aliases, h_aliases_len[cnt], data, total,
234 data, 0, t, db, owner);
236 aliases += h_aliases_len[cnt];
239 /* Next the normal addresses. */
240 for (cnt = 0; cnt < h_addr_list_cnt; ++cnt)
242 cache_add (addr_list_type, addresses, hst->h_length, data, total,
243 data, 0, t, db, owner);
244 addresses += hst->h_length;
247 /* If necessary the IPv6 addresses. */
248 if (addr_list_type == GETHOSTBYADDR)
249 for (cnt = 0; cnt < h_addr_list_cnt; ++cnt)
251 cache_add (GETHOSTBYADDRv6, addresses, IN6ADDRSZ, data, total,
252 data, 0, t, db, owner);
253 addresses += IN6ADDRSZ;
256 /* If necessary add the key for this request. */
257 if (req->type == GETHOSTBYNAME || req->type == GETHOSTBYNAMEv6)
259 if (addr_list_type == GETHOSTBYADDR)
260 cache_add (GETHOSTBYNAME, key_copy, req->key_len, data, total,
261 data, 0, t, db, owner);
262 cache_add (GETHOSTBYNAMEv6, key_copy, req->key_len, data,
263 total, data, 0, t, db, owner);
266 /* And finally the name. We mark this as the last entry. */
267 if (addr_list_type == GETHOSTBYADDR)
268 cache_add (GETHOSTBYNAME, data->strdata, h_name_len, data, total, data,
270 cache_add (GETHOSTBYNAMEv6, data->strdata, h_name_len, data,
271 total, data, 1, t, db, owner);
273 pthread_rwlock_unlock (&db->lock);
276 if (written != total)
279 dbg_log (_("short write in %s: %s"), __FUNCTION__,
280 strerror_r (errno, buf, sizeof (buf)));
286 addhstbyname (struct database *db, int fd, request_header *req,
287 void *key, uid_t uid)
289 /* Search for the entry matching the key. Please note that we don't
290 look again in the table whether the dataset is now available. We
291 simply insert it. It does not matter if it is in there twice. The
292 pruning function only will look at the timestamp. */
294 char *buffer = alloca (buflen);
295 struct hostent resultbuf;
300 dbg_log (_("Haven't found \"%s\" in hosts cache!"), key);
304 oldeuid = geteuid ();
308 while (__gethostbyname2_r (key, AF_INET, &resultbuf, buffer, buflen,
310 && h_errno == NETDB_INTERNAL
315 buffer = alloca (buflen);
321 cache_addhst (db, fd, req, key, hst, uid);
326 addhstbyaddr (struct database *db, int fd, request_header *req,
327 void *key, uid_t uid)
329 /* Search for the entry matching the key. Please note that we don't
330 look again in the table whether the dataset is now available. We
331 simply insert it. It does not matter if it is in there twice. The
332 pruning function only will look at the timestamp. */
334 char *buffer = alloca (buflen);
335 struct hostent resultbuf;
341 char buf[INET_ADDRSTRLEN];
342 dbg_log (_("Haven't found \"%s\" in hosts cache!"),
343 inet_ntop (AF_INET, key, buf, sizeof (buf)));
348 oldeuid = geteuid ();
352 while (__gethostbyaddr_r (key, INADDRSZ, AF_INET, &resultbuf, buffer,
353 buflen, &hst, &h_errno) != 0
354 && h_errno == NETDB_INTERNAL
359 buffer = alloca (buflen);
365 cache_addhst (db, fd, req, key, hst, uid);
370 addhstbynamev6 (struct database *db, int fd, request_header *req,
371 void *key, uid_t uid)
373 /* Search for the entry matching the key. Please note that we don't
374 look again in the table whether the dataset is now available. We
375 simply insert it. It does not matter if it is in there twice. The
376 pruning function only will look at the timestamp. */
378 char *buffer = alloca (buflen);
379 struct hostent resultbuf;
385 char buf[INET6_ADDRSTRLEN];
387 dbg_log (_("Haven't found \"%s\" in hosts cache!"),
388 inet_ntop (AF_INET6, key, buf, sizeof (buf)));
393 oldeuid = geteuid ();
397 while (__gethostbyname2_r (key, AF_INET6, &resultbuf, buffer, buflen,
399 && h_errno == NETDB_INTERNAL
404 buffer = alloca (buflen);
410 cache_addhst (db, fd, req, key, hst, uid);
415 addhstbyaddrv6 (struct database *db, int fd, request_header *req,
416 void *key, uid_t uid)
418 /* Search for the entry matching the key. Please note that we don't
419 look again in the table whether the dataset is now available. We
420 simply insert it. It does not matter if it is in there twice. The
421 pruning function only will look at the timestamp. */
423 char *buffer = alloca (buflen);
424 struct hostent resultbuf;
430 char buf[INET6_ADDRSTRLEN];
431 dbg_log (_("Haven't found \"%s\" in hosts cache!"),
432 inet_ntop (AF_INET6, key, buf, sizeof (buf)));
437 oldeuid = geteuid ();
441 while (__gethostbyaddr_r (key, IN6ADDRSZ, AF_INET6, &resultbuf,
442 buffer, buflen, &hst, &h_errno) != 0
443 && h_errno == NETDB_INTERNAL
448 buffer = alloca (buflen);
454 cache_addhst (db, fd, req, key, hst, uid);