Linux/ARM implementation of pread.
[glibc.git] / nscd / nscd_gethst_r.c
blob2e56d7d61c75750446a484dd4d2e11f0b58e2353
1 /* Copyright (C) 1998 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 <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <arpa/nameser.h>
28 #include <sys/socket.h>
29 #include <sys/uio.h>
30 #include <sys/un.h>
32 #include "nscd-client.h"
33 #include "nscd_proto.h"
35 int __nss_not_use_nscd_hosts;
37 static int nscd_gethst_r (const char *key, size_t keylen, request_type type,
38 struct hostent *resultbuf, char *buffer,
39 size_t buflen, int *h_errnop);
42 int
43 __nscd_gethostbyname_r (const char *name, struct hostent *resultbuf,
44 char *buffer, size_t buflen, int *h_errnop)
46 request_type reqtype;
48 reqtype = (_res.options & RES_USE_INET6) ? GETHOSTBYNAMEv6 : GETHOSTBYNAME;
50 return nscd_gethst_r (name, strlen (name) + 1, reqtype, resultbuf,
51 buffer, buflen, h_errnop);
55 int
56 __nscd_gethostbyname2_r (const char *name, int af, struct hostent *resultbuf,
57 char *buffer, size_t buflen, int *h_errnop)
59 request_type reqtype;
61 reqtype = af == AF_INET6 ? GETHOSTBYNAMEv6 : GETHOSTBYNAME;
63 return nscd_gethst_r (name, strlen (name) + 1, reqtype, resultbuf,
64 buffer, buflen, h_errnop);
68 int
69 __nscd_gethostbyaddr_r (const char *addr, int len, int type,
70 struct hostent *resultbuf, char *buffer, size_t buflen,
71 int *h_errnop)
73 request_type reqtype;
75 if (!((len == INADDRSZ && type == AF_INET)
76 || (len == IN6ADDRSZ && type == AF_INET6)))
77 /* LEN and TYPE do not match. */
78 return 1;
80 reqtype = type == AF_INET6 ? GETHOSTBYADDRv6 : GETHOSTBYADDR;
82 return nscd_gethst_r (addr, len, reqtype, resultbuf, buffer, buflen,
83 h_errnop);
87 /* Create a socket connected to a name. */
88 static int
89 open_socket (void)
91 struct sockaddr_un addr;
92 int sock;
93 int saved_errno = errno;
95 sock = __socket (PF_UNIX, SOCK_STREAM, 0);
96 if (sock < 0)
98 __set_errno (saved_errno);
99 return -1;
102 addr.sun_family = AF_UNIX;
103 strcpy (addr.sun_path, _PATH_NSCDSOCKET);
104 if (__connect (sock, (struct sockaddr *) &addr, sizeof (addr)) < 0)
106 __close (sock);
107 __set_errno (saved_errno);
108 return -1;
111 return sock;
115 static int
116 nscd_gethst_r (const char *key, size_t keylen, request_type type,
117 struct hostent *resultbuf, char *buffer, size_t buflen,
118 int *h_errnop)
120 int sock = open_socket ();
121 hst_response_header hst_resp;
122 request_header req;
123 ssize_t nbytes;
125 if (sock == -1)
127 __nss_not_use_nscd_group = 1;
128 return 1;
131 req.version = NSCD_VERSION;
132 req.type = type;
133 req.key_len = keylen;
134 nbytes = __write (sock, &req, sizeof (request_header));
135 if (nbytes != sizeof (request_header))
137 __close (sock);
138 return 1;
141 nbytes = __write (sock, key, req.key_len);
142 if (nbytes != req.key_len)
144 __close (sock);
145 return 1;
148 nbytes = __read (sock, &hst_resp, sizeof (hst_response_header));
149 if (nbytes != sizeof (hst_response_header))
151 __close (sock);
152 return 1;
155 if (hst_resp.found == -1)
157 /* The daemon does not cache this database. */
158 __close (sock);
159 __nss_not_use_nscd_hosts = 1;
160 return 1;
163 if (hst_resp.found == 1)
165 struct iovec vec[4];
166 size_t *aliases_len;
167 char *cp = buffer;
168 uintptr_t align;
169 size_t total_len;
170 ssize_t cnt;
171 char *ignore;
172 int n;
174 /* A first check whether the buffer is sufficently large is possible. */
175 /* Now allocate the buffer the array for the group members. We must
176 align the pointer. */
177 align = ((__alignof__ (char *) - (cp - ((char *) 0)))
178 & (__alignof__ (char *) - 1));
179 if (buflen < (align + hst_resp.h_name_len
180 + ((hst_resp.h_aliases_cnt + hst_resp.h_addr_list_cnt + 2)
181 * sizeof (char *))
182 + hst_resp.h_addr_list_cnt * (type == AF_INET
183 ? INADDRSZ : IN6ADDRSZ)))
185 no_room:
186 __set_errno (ERANGE);
187 __close (sock);
188 return -1;
190 cp += align;
192 /* Prepare the result as far as we can. */
193 resultbuf->h_aliases = (char **) cp;
194 cp += (hst_resp.h_aliases_cnt + 1) * sizeof (char *);
195 resultbuf->h_addr_list = (char **) cp;
196 cp += (hst_resp.h_addr_list_cnt + 1) * sizeof (char *);
198 resultbuf->h_name = cp;
199 cp += hst_resp.h_name_len;
200 vec[0].iov_base = resultbuf->h_name;
201 vec[0].iov_len = hst_resp.h_name_len;
203 aliases_len = alloca (hst_resp.h_aliases_cnt * sizeof (size_t));
204 vec[1].iov_base = aliases_len;
205 vec[1].iov_len = hst_resp.h_aliases_cnt * sizeof (size_t);
207 total_len = (hst_resp.h_name_len
208 + hst_resp.h_aliases_cnt * sizeof (size_t));
210 n = 2;
211 if (type == GETHOSTBYADDR || type == GETHOSTBYNAME)
213 vec[2].iov_base = cp;
214 vec[2].iov_len = hst_resp.h_addr_list_cnt * INADDRSZ;
216 ignore = alloca (hst_resp.h_addr_list_cnt * IN6ADDRSZ);
217 vec[3].iov_base = ignore;
218 vec[3].iov_len = hst_resp.h_addr_list_cnt * IN6ADDRSZ;
220 for (cnt = 0; cnt < hst_resp.h_addr_list_cnt; ++cnt)
222 resultbuf->h_addr_list[cnt] = cp;
223 cp += INADDRSZ;
226 resultbuf->h_addrtype = AF_INET;
227 resultbuf->h_length = INADDRSZ;
229 total_len += hst_resp.h_addr_list_cnt * (INADDRSZ + IN6ADDRSZ);
231 n = 4;
233 else
235 if (hst_resp.h_length == INADDRSZ)
237 ignore = alloca (hst_resp.h_addr_list_cnt * INADDRSZ);
238 vec[2].iov_base = ignore;
239 vec[2].iov_len = hst_resp.h_addr_list_cnt * INADDRSZ;
241 total_len += hst_resp.h_addr_list_cnt * INADDRSZ;
243 n = 3;
246 vec[n].iov_base = cp;
247 vec[n].iov_len = hst_resp.h_addr_list_cnt * IN6ADDRSZ;
249 for (cnt = 0; cnt < hst_resp.h_addr_list_cnt; ++cnt)
251 resultbuf->h_addr_list[cnt] = cp;
252 cp += IN6ADDRSZ;
255 resultbuf->h_addrtype = AF_INET6;
256 resultbuf->h_length = IN6ADDRSZ;
258 total_len += hst_resp.h_addr_list_cnt * IN6ADDRSZ;
260 ++n;
262 resultbuf->h_addr_list[cnt] = NULL;
264 if (__readv (sock, vec, n) != total_len)
266 __close (sock);
267 return 1;
270 /* Now we also can read the aliases. */
271 total_len = 0;
272 for (cnt = 0; cnt < hst_resp.h_aliases_cnt; ++cnt)
274 resultbuf->h_aliases[cnt] = cp;
275 cp += aliases_len[cnt];
276 total_len += aliases_len[cnt];
278 resultbuf->h_aliases[cnt] = NULL;
280 /* See whether this would exceed the buffer capacity. */
281 if (cp > buffer + buflen)
282 goto no_room;
284 /* And finally read the aliases. */
285 if (__read (sock, resultbuf->h_aliases[0], total_len) != total_len)
287 __close (sock);
288 return 1;
291 __close (sock);
292 return 0;
294 else
296 /* Store the error number. */
297 *h_errnop = hst_resp.error;
299 __close (sock);
300 return -1;