1 /* Cache handling for host lookup.
2 Copyright (C) 2004 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@redhat.com>, 2004.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the 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 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
33 typedef enum nss_status (*nss_gethostbyname3_r
)
34 (const char *name
, int af
, struct hostent
*host
,
35 char *buffer
, size_t buflen
, int *errnop
,
36 int *h_errnop
, int32_t *, char **);
37 typedef enum nss_status (*nss_getcanonname_r
)
38 (const char *name
, char *buffer
, size_t buflen
, char **result
,
39 int *errnop
, int *h_errnop
);
42 static const ai_response_header notfound
=
44 .version
= NSCD_VERSION
,
54 addhstaiX (struct database_dyn
*db
, int fd
, request_header
*req
,
55 void *key
, uid_t uid
, struct hashentry
*he
, struct datahead
*dh
)
57 /* Search for the entry matching the key. Please note that we don't
58 look again in the table whether the dataset is now available. We
59 simply insert it. It does not matter if it is in there twice. The
60 pruning function only will look at the timestamp. */
63 /* We allocate all data in one memory block: the iov vector,
64 the response header and the dataset itself. */
68 ai_response_header resp
;
72 if (__builtin_expect (debug_level
> 0, 0))
75 dbg_log (_("Haven't found \"%s\" in hosts cache!"), (char *) key
);
77 dbg_log (_("Reloading \"%s\" in hosts cache!"), (char *) key
);
86 static service_user
*hosts_database
;
87 service_user
*nip
= NULL
;
93 if (hosts_database
!= NULL
)
99 no_more
= __nss_database_lookup ("hosts", NULL
,
100 "dns [!UNAVAIL=return] files", &nip
);
102 if (__res_maybe_init (&_res
, 0) == -1)
105 /* If we are looking for both IPv4 and IPv6 address we don't want
106 the lookup functions to automatically promote IPv4 addresses to
107 IPv6 addresses. Currently this is decided by setting the
108 RES_USE_INET6 bit in _res.options. */
109 int old_res_options
= _res
.options
;
110 _res
.options
&= ~RES_USE_INET6
;
112 size_t tmpbuf6len
= 512;
113 char *tmpbuf6
= alloca (tmpbuf6len
);
114 size_t tmpbuf4len
= 0;
115 char *tmpbuf4
= NULL
;
117 int32_t ttl
= UINT32_MAX
;
119 char *key_copy
= NULL
;
120 bool alloca_used
= false;
124 int status
[2] = { NSS_STATUS_UNAVAIL
, NSS_STATUS_UNAVAIL
};
126 /* Prefer the function which also returns the TTL and canonical name. */
127 nss_gethostbyname3_r fct
= __nss_lookup_function (nip
,
130 fct
= __nss_lookup_function (nip
, "gethostbyname2_r");
134 struct hostent th
[2];
136 /* Collect IPv6 information first. */
140 status
[0] = DL_CALL_FCT (fct
, (key
, AF_INET6
, &th
[0], tmpbuf6
,
141 tmpbuf6len
, &rc6
, &herrno
,
143 if (rc6
!= ERANGE
|| herrno
!= NETDB_INTERNAL
)
145 tmpbuf6
= extend_alloca (tmpbuf6
, tmpbuf6len
, 2 * tmpbuf6len
);
148 if (rc6
!= 0 && herrno
== NETDB_INTERNAL
)
151 /* If the IPv6 lookup has been successful do not use the
152 buffer used in that lookup, use a new one. */
153 if (status
[0] == NSS_STATUS_SUCCESS
&& rc6
== 0)
156 tmpbuf4
= alloca (tmpbuf4len
);
160 tmpbuf4len
= tmpbuf6len
;
164 /* Next collect IPv4 information first. */
168 status
[1] = DL_CALL_FCT (fct
, (key
, AF_INET
, &th
[1], tmpbuf4
,
169 tmpbuf4len
, &rc4
, &herrno
,
170 ttl
== UINT32_MAX
? &ttl
: NULL
,
171 canon
== NULL
? &canon
: NULL
));
172 if (rc4
!= ERANGE
|| herrno
!= NETDB_INTERNAL
)
174 tmpbuf4
= extend_alloca (tmpbuf4
, tmpbuf4len
, 2 * tmpbuf4len
);
177 if (rc4
!= 0 || herrno
== NETDB_INTERNAL
)
180 if (status
[0] == NSS_STATUS_SUCCESS
181 || status
[1] == NSS_STATUS_SUCCESS
)
183 /* We found the data. Count the addresses and the size. */
186 for (int j
= 0; j
< 2; ++j
)
187 if (status
[j
] == NSS_STATUS_SUCCESS
)
188 for (int i
= 0; th
[j
].h_addr_list
[i
] != NULL
; ++i
)
191 addrslen
+= th
[j
].h_length
;
196 /* Determine the canonical name. */
197 nss_getcanonname_r cfct
;
198 cfct
= __nss_lookup_function (nip
, "getcanonname_r");
201 const size_t max_fqdn_len
= 256;
202 char *buf
= alloca (max_fqdn_len
);
206 if (DL_CALL_FCT (cfct
, (key
, buf
, max_fqdn_len
, &s
, &rc
,
207 &herrno
)) == NSS_STATUS_SUCCESS
)
210 /* Set to name now to avoid using gethostbyaddr. */
215 struct hostent
*he
= NULL
;
217 struct hostent he_mem
;
222 if (status
[1] == NSS_STATUS_SUCCESS
)
224 addr
= th
[1].h_addr_list
[0];
225 addrlen
= sizeof (struct in_addr
);
226 addrfamily
= AF_INET
;
230 addr
= th
[0].h_addr_list
[0];
231 addrlen
= sizeof (struct in6_addr
);
232 addrfamily
= AF_INET6
;
235 size_t tmpbuflen
= 512;
236 char *tmpbuf
= alloca (tmpbuflen
);
240 rc
= __gethostbyaddr_r (addr
, addrlen
, addrfamily
,
241 &he_mem
, tmpbuf
, tmpbuflen
,
243 if (rc
!= ERANGE
|| herrno
!= NETDB_INTERNAL
)
245 tmpbuf
= extend_alloca (tmpbuf
, tmpbuflen
,
258 size_t canonlen
= canon
== NULL
? 0 : (strlen (canon
) + 1);
260 total
= sizeof (*dataset
) + naddrs
+ addrslen
+ canonlen
;
262 /* Now we can allocate the data structure. */
265 dataset
= (struct dataset
*) mempool_alloc (db
,
269 ++db
->head
->addfailed
;
274 /* We cannot permanently add the result in the moment. But
275 we can provide the result as is. Store the data in some
277 dataset
= (struct dataset
*) alloca (total
+ req
->key_len
);
279 /* We cannot add this record to the permanent database. */
283 dataset
->head
.allocsize
= total
+ req
->key_len
;
284 dataset
->head
.recsize
= total
- offsetof (struct dataset
, resp
);
285 dataset
->head
.notfound
= false;
286 dataset
->head
.nreloads
= he
== NULL
? 0 : (dh
->nreloads
+ 1);
287 dataset
->head
.usable
= true;
289 /* Compute the timeout time. */
290 dataset
->head
.timeout
= time (NULL
) + MIN (db
->postimeout
, ttl
);
292 dataset
->resp
.version
= NSCD_VERSION
;
293 dataset
->resp
.found
= 1;
294 dataset
->resp
.naddrs
= naddrs
;
295 dataset
->resp
.addrslen
= addrslen
;
296 dataset
->resp
.canonlen
= canonlen
;
297 dataset
->resp
.error
= NETDB_SUCCESS
;
299 char *addrs
= (char *) (&dataset
->resp
+ 1);
300 uint8_t *family
= (uint8_t *) (addrs
+ addrslen
);
302 for (int j
= 0; j
< 2; ++j
)
303 if (status
[j
] == NSS_STATUS_SUCCESS
)
304 for (int i
= 0; th
[j
].h_addr_list
[i
] != NULL
; ++i
)
306 addrs
= mempcpy (addrs
, th
[j
].h_addr_list
[i
],
308 *family
++ = th
[j
].h_addrtype
;
313 cp
= mempcpy (cp
, canon
, canonlen
);
315 key_copy
= memcpy (cp
, key
, req
->key_len
);
317 /* Now we can determine whether on refill we have to
318 create a new record or not. */
323 if (total
+ req
->key_len
== dh
->allocsize
324 && total
- offsetof (struct dataset
, resp
) == dh
->recsize
325 && memcmp (&dataset
->resp
, dh
->data
,
327 - offsetof (struct dataset
, resp
)) == 0)
329 /* The data has not changed. We will just bump the
330 timeout value. Note that the new record has been
331 allocated on the stack and need not be freed. */
332 dh
->timeout
= dataset
->head
.timeout
;
337 /* We have to create a new record. Just allocate
338 appropriate memory and copy it. */
340 = (struct dataset
*) mempool_alloc (db
,
345 /* Adjust pointer into the memory block. */
346 key_copy
= (char *) newp
+ (key_copy
349 dataset
= memcpy (newp
, dataset
,
350 total
+ req
->key_len
);
354 /* Mark the old record as obsolete. */
360 /* We write the dataset before inserting it to the
361 database since while inserting this thread might
362 block and so would unnecessarily let the receiver
366 TEMP_FAILURE_RETRY (write (fd
, &dataset
->resp
, total
));
374 if (nss_next_action (nip
, status
[1]) == NSS_ACTION_RETURN
)
377 if (nip
->next
== NULL
)
383 /* No result found. Create a negative result record. */
384 if (he
!= NULL
&& rc4
== EAGAIN
)
386 /* If we have an old record available but cannot find one now
387 because the service is not available we keep the old record
388 and make sure it does not get removed. */
389 if (reload_count
!= UINT_MAX
&& dh
->nreloads
== reload_count
)
390 /* Do not reset the value if we never not reload the record. */
391 dh
->nreloads
= reload_count
- 1;
395 /* We have no data. This means we send the standard reply for
397 total
= sizeof (notfound
);
400 TEMP_FAILURE_RETRY (write (fd
, ¬found
, total
));
402 dataset
= mempool_alloc (db
, sizeof (struct dataset
) + req
->key_len
);
403 /* If we cannot permanently store the result, so be it. */
406 dataset
->head
.allocsize
= sizeof (struct dataset
) + req
->key_len
;
407 dataset
->head
.recsize
= total
;
408 dataset
->head
.notfound
= true;
409 dataset
->head
.nreloads
= 0;
410 dataset
->head
.usable
= true;
412 /* Compute the timeout time. */
413 dataset
->head
.timeout
= time (NULL
) + db
->negtimeout
;
415 /* This is the reply. */
416 memcpy (&dataset
->resp
, ¬found
, total
);
418 /* Copy the key data. */
419 key_copy
= memcpy (dataset
->strdata
, key
, req
->key_len
);
422 ++db
->head
->addfailed
;
426 _res
.options
= old_res_options
;
431 if (dataset
!= NULL
&& !alloca_used
)
433 /* If necessary, we also propagate the data to disk. */
437 uintptr_t pval
= (uintptr_t) dataset
& ~pagesize_m1
;
438 msync ((void *) pval
,
439 ((uintptr_t) dataset
& pagesize_m1
) + total
+ req
->key_len
,
443 /* Now get the lock to safely insert the records. */
444 pthread_rwlock_rdlock (&db
->lock
);
446 if (cache_add (req
->type
, key_copy
, req
->key_len
, &dataset
->head
, true,
448 /* Ensure the data can be recovered. */
449 dataset
->head
.usable
= false;
451 pthread_rwlock_unlock (&db
->lock
);
453 /* Mark the old entry as obsolete. */
461 addhstai (struct database_dyn
*db
, int fd
, request_header
*req
, void *key
,
464 addhstaiX (db
, fd
, req
, key
, uid
, NULL
, NULL
);
469 readdhstai (struct database_dyn
*db
, struct hashentry
*he
, struct datahead
*dh
)
477 addhstaiX (db
, -1, &req
, db
->data
+ he
->key
, he
->owner
, he
, dh
);