1 /* Cache handling for host lookup.
2 Copyright (C) 1998, 1999, 2000 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>
33 #include <arpa/nameser.h>
39 /* This is the standard reply in case the service is disabled. */
40 static const hst_response_header disabled =
42 version: NSCD_VERSION,
52 /* This is the struct describing how to write this record. */
53 const struct iovec hst_iov_disabled =
55 iov_base: (void *) &disabled,
56 iov_len: sizeof (disabled)
60 /* This is the standard reply in case we haven't found the dataset. */
61 static const hst_response_header notfound =
63 version: NSCD_VERSION,
73 /* This is the struct describing how to write this record. */
74 static const struct iovec iov_notfound =
76 iov_base: (void *) ¬found,
77 iov_len: sizeof (notfound)
83 hst_response_header resp;
89 cache_addhst (struct database *db, int fd, request_header *req, void *key,
90 struct hostent *hst, uid_t owner)
94 time_t t = time (NULL);
98 /* We have no data. This means we send the standard reply for this
102 total = sizeof (notfound);
104 written = writev (fd, &iov_notfound, 1);
106 copy = malloc (req->key_len);
108 error (EXIT_FAILURE, errno, _("while allocating key copy"));
109 memcpy (copy, key, req->key_len);
111 /* Compute the timeout time. */
114 /* Now get the lock to safely insert the records. */
115 pthread_rwlock_rdlock (&db->lock);
117 cache_add (req->type, copy, req->key_len, ¬found,
118 sizeof (notfound), (void *) -1, 0, t, db, owner);
120 pthread_rwlock_unlock (&db->lock);
124 /* Determine the I/O structure. */
125 struct hostdata *data;
126 size_t h_name_len = strlen (hst->h_name) + 1;
127 size_t h_aliases_cnt;
128 size_t *h_aliases_len;
129 size_t h_addr_list_cnt;
133 char *key_copy = NULL;
137 /* Determine the number of aliases. */
139 for (cnt = 0; hst->h_aliases[cnt] != NULL; ++cnt)
141 /* Determine the length of all aliases. */
142 h_aliases_len = alloca (h_aliases_cnt * sizeof (size_t));
144 for (cnt = 0; cnt < h_aliases_cnt; ++cnt)
146 h_aliases_len[cnt] = strlen (hst->h_aliases[cnt]) + 1;
147 total += h_aliases_len[cnt];
150 /* Determine the number of addresses. */
152 for (cnt = 0; hst->h_addr_list[cnt]; ++cnt)
155 /* We allocate all data in one memory block: the iov vector,
156 the response header and the dataset itself. */
157 total += (sizeof (struct hostdata)
159 + h_aliases_cnt * sizeof (size_t)
160 + h_addr_list_cnt * (hst->h_length
161 + (hst->h_length == NS_INADDRSZ
162 ? NS_IN6ADDRSZ : 0)));
164 data = (struct hostdata *) malloc (total + req->key_len);
166 /* There is no reason to go on. */
167 error (EXIT_FAILURE, errno, _("while allocating cache entry"));
169 data->resp.found = 1;
170 data->resp.h_name_len = h_name_len;
171 data->resp.h_aliases_cnt = h_aliases_cnt;
172 data->resp.h_addrtype = hst->h_addrtype;
173 data->resp.h_length = hst->h_length;
174 data->resp.h_addr_list_cnt = h_addr_list_cnt;
175 data->resp.error = NETDB_SUCCESS;
179 cp = mempcpy (cp, hst->h_name, h_name_len);
180 cp = mempcpy (cp, h_aliases_len, h_aliases_cnt * sizeof (size_t));
182 /* The normal addresses first. */
184 for (cnt = 0; cnt < h_addr_list_cnt; ++cnt)
185 cp = mempcpy (cp, hst->h_addr_list[cnt], hst->h_length);
187 /* Then the aliases. */
189 for (cnt = 0; cnt < h_aliases_cnt; ++cnt)
190 cp = mempcpy (cp, hst->h_aliases[cnt], h_aliases_len[cnt]);
192 assert (cp == data->strdata + total - sizeof (hst_response_header));
194 /* If we are adding a GETHOSTBYNAME{,v6} entry we must be prepared
195 that the answer we get from the NSS does not contain the key
196 itself. This is the case if the resolver is used and the name
197 is extended by the domainnames from /etc/resolv.conf. Therefore
198 we explicitly add the name here. */
199 if (req->type == GETHOSTBYNAME || req->type == GETHOSTBYNAMEv6)
200 key_copy = memcpy (cp, key, req->key_len);
202 /* We write the dataset before inserting it to the database
203 since while inserting this thread might block and so would
204 unnecessarily let the receiver wait. */
205 written = write (fd, data, total);
207 addr_list_type = (hst->h_length == NS_INADDRSZ
208 ? GETHOSTBYADDR : GETHOSTBYADDRv6);
210 /* Compute the timeout time. */
213 /* Now get the lock to safely insert the records. */
214 pthread_rwlock_rdlock (&db->lock);
216 /* First add all the aliases. */
217 for (cnt = 0; cnt < h_aliases_cnt; ++cnt)
219 if (addr_list_type == GETHOSTBYADDR)
220 cache_add (GETHOSTBYNAME, aliases, h_aliases_len[cnt], data, total,
221 data, 0, t, db, owner);
223 cache_add (GETHOSTBYNAMEv6, aliases, h_aliases_len[cnt], data, total,
224 data, 0, t, db, owner);
226 aliases += h_aliases_len[cnt];
229 /* Next the normal addresses. */
230 for (cnt = 0; cnt < h_addr_list_cnt; ++cnt)
232 cache_add (addr_list_type, addresses, hst->h_length, data, total,
233 data, 0, t, db, owner);
234 addresses += hst->h_length;
237 /* If necessary the IPv6 addresses. */
238 if (addr_list_type == GETHOSTBYADDR)
239 for (cnt = 0; cnt < h_addr_list_cnt; ++cnt)
241 cache_add (GETHOSTBYADDRv6, addresses, IN6ADDRSZ, data, total,
242 data, 0, t, db, owner);
243 addresses += IN6ADDRSZ;
246 /* If necessary add the key for this request. */
247 if (req->type == GETHOSTBYNAME || req->type == GETHOSTBYNAMEv6)
249 if (addr_list_type == GETHOSTBYADDR)
250 cache_add (GETHOSTBYNAME, key_copy, req->key_len, data, total,
251 data, 0, t, db, owner);
252 cache_add (GETHOSTBYNAMEv6, key_copy, req->key_len, data,
253 total, data, 0, t, db, owner);
256 /* And finally the name. We mark this as the last entry. */
257 if (addr_list_type == GETHOSTBYADDR)
258 cache_add (GETHOSTBYNAME, data->strdata, h_name_len, data, total, data,
260 cache_add (GETHOSTBYNAMEv6, data->strdata, h_name_len, data,
261 total, data, 1, t, db, owner);
263 pthread_rwlock_unlock (&db->lock);
266 if (written != total)
269 dbg_log (_("short write in %s: %s"), __FUNCTION__,
270 strerror_r (errno, buf, sizeof (buf)));
276 addhstbyname (struct database *db, int fd, request_header *req,
277 void *key, uid_t uid)
279 /* Search for the entry matching the key. Please note that we don't
280 look again in the table whether the dataset is now available. We
281 simply insert it. It does not matter if it is in there twice. The
282 pruning function only will look at the timestamp. */
284 char *buffer = alloca (buflen);
285 struct hostent resultbuf;
290 dbg_log (_("Haven't found \"%s\" in hosts cache!"), key);
294 oldeuid = geteuid ();
298 while (__gethostbyname2_r (key, AF_INET, &resultbuf, buffer, buflen,
300 && h_errno == NETDB_INTERNAL
305 buffer = alloca (buflen);
311 cache_addhst (db, fd, req, key, hst, uid);
316 addhstbyaddr (struct database *db, int fd, request_header *req,
317 void *key, uid_t uid)
319 /* Search for the entry matching the key. Please note that we don't
320 look again in the table whether the dataset is now available. We
321 simply insert it. It does not matter if it is in there twice. The
322 pruning function only will look at the timestamp. */
324 char *buffer = alloca (buflen);
325 struct hostent resultbuf;
331 char buf[INET_ADDRSTRLEN];
332 dbg_log (_("Haven't found \"%s\" in hosts cache!"),
333 inet_ntop (AF_INET, key, buf, sizeof (buf)));
338 oldeuid = geteuid ();
342 while (__gethostbyaddr_r (key, NS_INADDRSZ, AF_INET, &resultbuf, buffer,
343 buflen, &hst, &h_errno) != 0
344 && h_errno == NETDB_INTERNAL
349 buffer = alloca (buflen);
355 cache_addhst (db, fd, req, key, hst, uid);
360 addhstbynamev6 (struct database *db, int fd, request_header *req,
361 void *key, uid_t uid)
363 /* Search for the entry matching the key. Please note that we don't
364 look again in the table whether the dataset is now available. We
365 simply insert it. It does not matter if it is in there twice. The
366 pruning function only will look at the timestamp. */
368 char *buffer = alloca (buflen);
369 struct hostent resultbuf;
375 char buf[INET6_ADDRSTRLEN];
377 dbg_log (_("Haven't found \"%s\" in hosts cache!"),
378 inet_ntop (AF_INET6, key, buf, sizeof (buf)));
383 oldeuid = geteuid ();
387 while (__gethostbyname2_r (key, AF_INET6, &resultbuf, buffer, buflen,
389 && h_errno == NETDB_INTERNAL
394 buffer = alloca (buflen);
400 cache_addhst (db, fd, req, key, hst, uid);
405 addhstbyaddrv6 (struct database *db, int fd, request_header *req,
406 void *key, uid_t uid)
408 /* Search for the entry matching the key. Please note that we don't
409 look again in the table whether the dataset is now available. We
410 simply insert it. It does not matter if it is in there twice. The
411 pruning function only will look at the timestamp. */
413 char *buffer = alloca (buflen);
414 struct hostent resultbuf;
420 char buf[INET6_ADDRSTRLEN];
421 dbg_log (_("Haven't found \"%s\" in hosts cache!"),
422 inet_ntop (AF_INET6, key, buf, sizeof (buf)));
427 oldeuid = geteuid ();
431 while (__gethostbyaddr_r (key, NS_IN6ADDRSZ, AF_INET6, &resultbuf,
432 buffer, buflen, &hst, &h_errno) != 0
433 && h_errno == NETDB_INTERNAL
438 buffer = alloca (buflen);
444 cache_addhst (db, fd, req, key, hst, uid);