* sysdeps/unix/sysv/linux/opensock.c: Include <string.h>.
[glibc.git] / nscd / nscd_gethst_r.c
blobd37192243083a97387dcef19a03f7d900d5e28a4
1 /* Copyright (C) 1998, 1999, 2000, 2001 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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 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) internal_function;
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 void *addr, socklen_t 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 internal_function
118 nscd_gethst_r (const char *key, size_t keylen, request_type type,
119 struct hostent *resultbuf, char *buffer, size_t buflen,
120 int *h_errnop)
122 int sock = open_socket ();
123 hst_response_header hst_resp;
124 request_header req;
125 ssize_t nbytes;
127 if (sock == -1)
129 __nss_not_use_nscd_group = 1;
130 return -1;
133 req.version = NSCD_VERSION;
134 req.type = type;
135 req.key_len = keylen;
136 nbytes = __write (sock, &req, sizeof (request_header));
137 if (nbytes != sizeof (request_header))
139 __close (sock);
140 return -1;
143 nbytes = __write (sock, key, req.key_len);
144 if (nbytes != req.key_len)
146 __close (sock);
147 return -1;
150 nbytes = __read (sock, &hst_resp, sizeof (hst_response_header));
151 if (nbytes != sizeof (hst_response_header))
153 __close (sock);
154 return -1;
157 if (hst_resp.found == -1)
159 /* The daemon does not cache this database. */
160 __close (sock);
161 __nss_not_use_nscd_hosts = 1;
162 return -1;
165 if (hst_resp.found == 1)
167 struct iovec vec[4];
168 uint32_t *aliases_len;
169 char *cp = buffer;
170 uintptr_t align1;
171 uintptr_t align2;
172 size_t total_len;
173 ssize_t cnt;
174 char *ignore;
175 int n;
177 /* A first check whether the buffer is sufficiently large is possible. */
178 /* Now allocate the buffer the array for the group members. We must
179 align the pointer and the base of the h_addr_list pointers. */
180 align1 = ((__alignof__ (char *) - (cp - ((char *) 0)))
181 & (__alignof__ (char *) - 1));
182 align2 = ((__alignof__ (char *) - ((cp + align1 + hst_resp.h_name_len)
183 - ((char *) 0)))
184 & (__alignof__ (char *) - 1));
185 if (buflen < (align1 + hst_resp.h_name_len + align2
186 + ((hst_resp.h_aliases_cnt + hst_resp.h_addr_list_cnt + 2)
187 * sizeof (char *))
188 + hst_resp.h_addr_list_cnt * (type == AF_INET
189 ? INADDRSZ : IN6ADDRSZ)))
191 no_room:
192 __set_errno (ERANGE);
193 __close (sock);
194 return ERANGE;
196 cp += align1;
198 /* Prepare the result as far as we can. */
199 resultbuf->h_aliases = (char **) cp;
200 cp += (hst_resp.h_aliases_cnt + 1) * sizeof (char *);
201 resultbuf->h_addr_list = (char **) cp;
202 cp += (hst_resp.h_addr_list_cnt + 1) * sizeof (char *);
204 resultbuf->h_name = cp;
205 cp += hst_resp.h_name_len + align2;
206 vec[0].iov_base = resultbuf->h_name;
207 vec[0].iov_len = hst_resp.h_name_len;
209 aliases_len = alloca (hst_resp.h_aliases_cnt * sizeof (uint32_t));
210 vec[1].iov_base = aliases_len;
211 vec[1].iov_len = hst_resp.h_aliases_cnt * sizeof (uint32_t);
213 total_len = (hst_resp.h_name_len
214 + hst_resp.h_aliases_cnt * sizeof (uint32_t));
216 n = 2;
217 if (type == GETHOSTBYADDR || type == GETHOSTBYNAME)
219 vec[2].iov_base = cp;
220 vec[2].iov_len = hst_resp.h_addr_list_cnt * INADDRSZ;
222 for (cnt = 0; cnt < hst_resp.h_addr_list_cnt; ++cnt)
224 resultbuf->h_addr_list[cnt] = cp;
225 cp += INADDRSZ;
228 resultbuf->h_addrtype = AF_INET;
229 resultbuf->h_length = INADDRSZ;
231 total_len += hst_resp.h_addr_list_cnt * INADDRSZ;
233 n = 3;
235 else
237 if (hst_resp.h_length == INADDRSZ)
239 ignore = alloca (hst_resp.h_addr_list_cnt * INADDRSZ);
240 vec[2].iov_base = ignore;
241 vec[2].iov_len = hst_resp.h_addr_list_cnt * INADDRSZ;
243 total_len += hst_resp.h_addr_list_cnt * INADDRSZ;
245 n = 3;
248 vec[n].iov_base = cp;
249 vec[n].iov_len = hst_resp.h_addr_list_cnt * IN6ADDRSZ;
251 for (cnt = 0; cnt < hst_resp.h_addr_list_cnt; ++cnt)
253 resultbuf->h_addr_list[cnt] = cp;
254 cp += IN6ADDRSZ;
257 resultbuf->h_addrtype = AF_INET6;
258 resultbuf->h_length = IN6ADDRSZ;
260 total_len += hst_resp.h_addr_list_cnt * IN6ADDRSZ;
262 ++n;
264 resultbuf->h_addr_list[cnt] = NULL;
266 if (__readv (sock, vec, n) != total_len)
268 __close (sock);
269 return -1;
272 /* Now we also can read the aliases. */
273 total_len = 0;
274 for (cnt = 0; cnt < hst_resp.h_aliases_cnt; ++cnt)
276 resultbuf->h_aliases[cnt] = cp;
277 cp += aliases_len[cnt];
278 total_len += aliases_len[cnt];
280 resultbuf->h_aliases[cnt] = NULL;
282 /* See whether this would exceed the buffer capacity. */
283 if (cp > buffer + buflen)
284 goto no_room;
286 /* And finally read the aliases. */
287 if (__read (sock, resultbuf->h_aliases[0], total_len) != total_len)
289 __close (sock);
290 return -1;
293 __close (sock);
294 return 0;
296 else
298 /* Store the error number. */
299 *h_errnop = hst_resp.error;
301 __close (sock);
302 /* The `errno' to some value != ERANGE. */
303 __set_errno (ENOENT);
304 return ENOENT;