1 /* Cache handling for host lookup.
2 Copyright (C) 1998 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
,
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
);
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
,
232 cache_add (GETHOSTBYNAMEv6
, aliases
, h_aliases_len
[cnt
], data
, total
,
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
,
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
,
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
,
261 cache_add (GETHOSTBYNAMEv6
, key_copy
, req
->key_len
, data
,
262 total
, data
, 0, t
, db
);
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
);
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
, void *key
)
287 /* Search for the entry matching the key. Please note that we don't
288 look again in the table whether the dataset is now available. We
289 simply insert it. It does not matter if it is in there twice. The
290 pruning function only will look at the timestamp. */
292 char *buffer
= alloca (buflen
);
293 struct hostent resultbuf
;
297 dbg_log (_("Haven't found \"%s\" in hosts cache!"), key
);
299 while (gethostbyname2_r (key
, AF_INET
, &resultbuf
, buffer
, buflen
, &hst
,
301 && h_errno
== NETDB_INTERNAL
306 buffer
= alloca (buflen
);
309 cache_addhst (db
, fd
, req
, key
, hst
);
314 addhstbyaddr (struct database
*db
, int fd
, request_header
*req
, void *key
)
316 /* Search for the entry matching the key. Please note that we don't
317 look again in the table whether the dataset is now available. We
318 simply insert it. It does not matter if it is in there twice. The
319 pruning function only will look at the timestamp. */
321 char *buffer
= alloca (buflen
);
322 struct hostent resultbuf
;
328 dbg_log (_("Haven't found \"%s\" in hosts cache!"),
329 inet_ntop (AF_INET
, key
, buf
, sizeof (buf
)));
332 while (gethostbyaddr_r (key
, INADDRSZ
, AF_INET
, &resultbuf
, buffer
, buflen
,
334 && h_errno
== NETDB_INTERNAL
339 buffer
= alloca (buflen
);
342 cache_addhst (db
, fd
, req
, key
, hst
);
347 addhstbynamev6 (struct database
*db
, int fd
, request_header
*req
, void *key
)
349 /* Search for the entry matching the key. Please note that we don't
350 look again in the table whether the dataset is now available. We
351 simply insert it. It does not matter if it is in there twice. The
352 pruning function only will look at the timestamp. */
354 char *buffer
= alloca (buflen
);
355 struct hostent resultbuf
;
359 dbg_log (_("Haven't found \"%s\" in hosts cache!"), key
);
361 while (gethostbyname2_r (key
, AF_INET6
, &resultbuf
, buffer
, buflen
, &hst
,
363 && h_errno
== NETDB_INTERNAL
368 buffer
= alloca (buflen
);
371 cache_addhst (db
, fd
, req
, key
, hst
);
376 addhstbyaddrv6 (struct database
*db
, int fd
, request_header
*req
, void *key
)
378 /* Search for the entry matching the key. Please note that we don't
379 look again in the table whether the dataset is now available. We
380 simply insert it. It does not matter if it is in there twice. The
381 pruning function only will look at the timestamp. */
383 char *buffer
= alloca (buflen
);
384 struct hostent resultbuf
;
390 dbg_log (_("Haven't found \"%s\" in hosts cache!"),
391 inet_ntop (AF_INET6
, key
, buf
, sizeof (buf
)));
394 while (gethostbyaddr_r (key
, IN6ADDRSZ
, AF_INET6
, &resultbuf
, buffer
, buflen
,
396 && h_errno
== NETDB_INTERNAL
401 buffer
= alloca (buflen
);
404 cache_addhst (db
, fd
, req
, key
, hst
);