Update.
[glibc.git] / nscd / nscd_gethst_r.c
blobe8d9ef2495e55284b2ff4dab7ee6b8af1f9316ea
1 /* Copyright (C) 1998, 1999 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <errno.h>
21 #include <netdb.h>
22 #include <resolv.h>
23 #include <stdint.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <arpa/nameser.h>
29 #include <sys/socket.h>
30 #include <sys/uio.h>
31 #include <sys/un.h>
33 #include "nscd-client.h"
34 #include "nscd_proto.h"
36 int __nss_not_use_nscd_hosts;
38 static int nscd_gethst_r (const char *key, size_t keylen, request_type type,
39 struct hostent *resultbuf, char *buffer,
40 size_t buflen, int *h_errnop);
43 int
44 __nscd_gethostbyname_r (const char *name, struct hostent *resultbuf,
45 char *buffer, size_t buflen, int *h_errnop)
47 request_type reqtype;
49 reqtype = (_res.options & RES_USE_INET6) ? GETHOSTBYNAMEv6 : GETHOSTBYNAME;
51 return nscd_gethst_r (name, strlen (name) + 1, reqtype, resultbuf,
52 buffer, buflen, h_errnop);
56 int
57 __nscd_gethostbyname2_r (const char *name, int af, struct hostent *resultbuf,
58 char *buffer, size_t buflen, int *h_errnop)
60 request_type reqtype;
62 reqtype = af == AF_INET6 ? GETHOSTBYNAMEv6 : GETHOSTBYNAME;
64 return nscd_gethst_r (name, strlen (name) + 1, reqtype, resultbuf,
65 buffer, buflen, h_errnop);
69 int
70 __nscd_gethostbyaddr_r (const char *addr, int len, int type,
71 struct hostent *resultbuf, char *buffer, size_t buflen,
72 int *h_errnop)
74 request_type reqtype;
76 if (!((len == INADDRSZ && type == AF_INET)
77 || (len == IN6ADDRSZ && type == AF_INET6)))
78 /* LEN and TYPE do not match. */
79 return 1;
81 reqtype = type == AF_INET6 ? GETHOSTBYADDRv6 : GETHOSTBYADDR;
83 return nscd_gethst_r (addr, len, reqtype, resultbuf, buffer, buflen,
84 h_errnop);
88 /* Create a socket connected to a name. */
89 static int
90 open_socket (void)
92 struct sockaddr_un addr;
93 int sock;
94 int saved_errno = errno;
96 sock = __socket (PF_UNIX, SOCK_STREAM, 0);
97 if (sock < 0)
99 __set_errno (saved_errno);
100 return -1;
103 addr.sun_family = AF_UNIX;
104 strcpy (addr.sun_path, _PATH_NSCDSOCKET);
105 if (__connect (sock, (struct sockaddr *) &addr, sizeof (addr)) < 0)
107 __close (sock);
108 __set_errno (saved_errno);
109 return -1;
112 return sock;
116 static int
117 nscd_gethst_r (const char *key, size_t keylen, request_type type,
118 struct hostent *resultbuf, char *buffer, size_t buflen,
119 int *h_errnop)
121 int sock = open_socket ();
122 hst_response_header hst_resp;
123 request_header req;
124 ssize_t nbytes;
126 if (sock == -1)
128 __nss_not_use_nscd_group = 1;
129 return 1;
132 req.version = NSCD_VERSION;
133 req.type = type;
134 req.key_len = keylen;
135 nbytes = __write (sock, &req, sizeof (request_header));
136 if (nbytes != sizeof (request_header))
138 __close (sock);
139 return 1;
142 nbytes = __write (sock, key, req.key_len);
143 if (nbytes != req.key_len)
145 __close (sock);
146 return 1;
149 nbytes = __read (sock, &hst_resp, sizeof (hst_response_header));
150 if (nbytes != sizeof (hst_response_header))
152 __close (sock);
153 return 1;
156 if (hst_resp.found == -1)
158 /* The daemon does not cache this database. */
159 __close (sock);
160 __nss_not_use_nscd_hosts = 1;
161 return 1;
164 if (hst_resp.found == 1)
166 struct iovec vec[4];
167 size_t *aliases_len;
168 char *cp = buffer;
169 uintptr_t align;
170 size_t total_len;
171 ssize_t cnt;
172 char *ignore;
173 int n;
175 /* A first check whether the buffer is sufficently large is possible. */
176 /* Now allocate the buffer the array for the group members. We must
177 align the pointer. */
178 align = ((__alignof__ (char *) - (cp - ((char *) 0)))
179 & (__alignof__ (char *) - 1));
180 if (buflen < (align + hst_resp.h_name_len
181 + ((hst_resp.h_aliases_cnt + hst_resp.h_addr_list_cnt + 2)
182 * sizeof (char *))
183 + hst_resp.h_addr_list_cnt * (type == AF_INET
184 ? INADDRSZ : IN6ADDRSZ)))
186 no_room:
187 __set_errno (ERANGE);
188 __close (sock);
189 return -1;
191 cp += align;
193 /* Prepare the result as far as we can. */
194 resultbuf->h_aliases = (char **) cp;
195 cp += (hst_resp.h_aliases_cnt + 1) * sizeof (char *);
196 resultbuf->h_addr_list = (char **) cp;
197 cp += (hst_resp.h_addr_list_cnt + 1) * sizeof (char *);
199 resultbuf->h_name = cp;
200 cp += hst_resp.h_name_len;
201 vec[0].iov_base = resultbuf->h_name;
202 vec[0].iov_len = hst_resp.h_name_len;
204 aliases_len = alloca (hst_resp.h_aliases_cnt * sizeof (size_t));
205 vec[1].iov_base = aliases_len;
206 vec[1].iov_len = hst_resp.h_aliases_cnt * sizeof (size_t);
208 total_len = (hst_resp.h_name_len
209 + hst_resp.h_aliases_cnt * sizeof (size_t));
211 n = 2;
212 if (type == GETHOSTBYADDR || type == GETHOSTBYNAME)
214 vec[2].iov_base = cp;
215 vec[2].iov_len = hst_resp.h_addr_list_cnt * INADDRSZ;
217 ignore = alloca (hst_resp.h_addr_list_cnt * IN6ADDRSZ);
218 vec[3].iov_base = ignore;
219 vec[3].iov_len = hst_resp.h_addr_list_cnt * IN6ADDRSZ;
221 for (cnt = 0; cnt < hst_resp.h_addr_list_cnt; ++cnt)
223 resultbuf->h_addr_list[cnt] = cp;
224 cp += INADDRSZ;
227 resultbuf->h_addrtype = AF_INET;
228 resultbuf->h_length = INADDRSZ;
230 total_len += hst_resp.h_addr_list_cnt * (INADDRSZ + IN6ADDRSZ);
232 n = 4;
234 else
236 if (hst_resp.h_length == INADDRSZ)
238 ignore = alloca (hst_resp.h_addr_list_cnt * INADDRSZ);
239 vec[2].iov_base = ignore;
240 vec[2].iov_len = hst_resp.h_addr_list_cnt * INADDRSZ;
242 total_len += hst_resp.h_addr_list_cnt * INADDRSZ;
244 n = 3;
247 vec[n].iov_base = cp;
248 vec[n].iov_len = hst_resp.h_addr_list_cnt * IN6ADDRSZ;
250 for (cnt = 0; cnt < hst_resp.h_addr_list_cnt; ++cnt)
252 resultbuf->h_addr_list[cnt] = cp;
253 cp += IN6ADDRSZ;
256 resultbuf->h_addrtype = AF_INET6;
257 resultbuf->h_length = IN6ADDRSZ;
259 total_len += hst_resp.h_addr_list_cnt * IN6ADDRSZ;
261 ++n;
263 resultbuf->h_addr_list[cnt] = NULL;
265 if (__readv (sock, vec, n) != total_len)
267 __close (sock);
268 return 1;
271 /* Now we also can read the aliases. */
272 total_len = 0;
273 for (cnt = 0; cnt < hst_resp.h_aliases_cnt; ++cnt)
275 resultbuf->h_aliases[cnt] = cp;
276 cp += aliases_len[cnt];
277 total_len += aliases_len[cnt];
279 resultbuf->h_aliases[cnt] = NULL;
281 /* See whether this would exceed the buffer capacity. */
282 if (cp > buffer + buflen)
283 goto no_room;
285 /* And finally read the aliases. */
286 if (__read (sock, resultbuf->h_aliases[0], total_len) != total_len)
288 __close (sock);
289 return 1;
292 __close (sock);
293 return 0;
295 else
297 /* Store the error number. */
298 *h_errnop = hst_resp.error;
300 __close (sock);
301 return -1;