1 /* Copyright (C) 1998-2002, 2003, 2004 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
28 #include <arpa/nameser.h>
29 #include <sys/socket.h>
32 #include <not-cancel.h>
34 #include "nscd-client.h"
35 #include "nscd_proto.h"
37 int __nss_not_use_nscd_hosts
;
39 static int nscd_gethst_r (const char *key
, size_t keylen
, request_type type
,
40 struct hostent
*resultbuf
, char *buffer
,
41 size_t buflen
, struct hostent
**result
,
42 int *h_errnop
) internal_function
;
46 __nscd_gethostbyname_r (const char *name
, struct hostent
*resultbuf
,
47 char *buffer
, size_t buflen
, struct hostent
**result
,
52 reqtype
= (_res
.options
& RES_USE_INET6
) ? GETHOSTBYNAMEv6
: GETHOSTBYNAME
;
54 return nscd_gethst_r (name
, strlen (name
) + 1, reqtype
, resultbuf
,
55 buffer
, buflen
, result
, h_errnop
);
60 __nscd_gethostbyname2_r (const char *name
, int af
, struct hostent
*resultbuf
,
61 char *buffer
, size_t buflen
, struct hostent
**result
,
66 reqtype
= af
== AF_INET6
? GETHOSTBYNAMEv6
: GETHOSTBYNAME
;
68 return nscd_gethst_r (name
, strlen (name
) + 1, reqtype
, resultbuf
,
69 buffer
, buflen
, result
, h_errnop
);
74 __nscd_gethostbyaddr_r (const void *addr
, socklen_t len
, int type
,
75 struct hostent
*resultbuf
, char *buffer
, size_t buflen
,
76 struct hostent
**result
, int *h_errnop
)
80 if (!((len
== INADDRSZ
&& type
== AF_INET
)
81 || (len
== IN6ADDRSZ
&& type
== AF_INET6
)))
82 /* LEN and TYPE do not match. */
85 reqtype
= type
== AF_INET6
? GETHOSTBYADDRv6
: GETHOSTBYADDR
;
87 return nscd_gethst_r (addr
, len
, reqtype
, resultbuf
, buffer
, buflen
, result
,
92 /* Create a socket connected to a name. */
94 __nscd_open_socket (const char *key
, size_t keylen
, request_type type
,
95 void *response
, size_t responselen
)
97 struct sockaddr_un addr
;
99 int saved_errno
= errno
;
101 sock
= __socket (PF_UNIX
, SOCK_STREAM
, 0);
104 __set_errno (saved_errno
);
108 addr
.sun_family
= AF_UNIX
;
109 strcpy (addr
.sun_path
, _PATH_NSCDSOCKET
);
110 if (__connect (sock
, (struct sockaddr
*) &addr
, sizeof (addr
)) < 0)
112 __set_errno (saved_errno
);
117 req
.version
= NSCD_VERSION
;
119 req
.key_len
= keylen
;
122 vec
[0].iov_base
= &req
;
123 vec
[0].iov_len
= sizeof (request_header
);
124 vec
[1].iov_base
= (void *) key
;
125 vec
[1].iov_len
= keylen
;
127 ssize_t nbytes
= TEMP_FAILURE_RETRY (__writev (sock
, vec
, 2));
128 if (nbytes
!= (ssize_t
) (sizeof (request_header
) + keylen
))
131 close_not_cancel_no_status (sock
);
136 nbytes
= TEMP_FAILURE_RETRY (__read (sock
, response
, responselen
));
137 if (nbytes
!= (ssize_t
) responselen
)
147 nscd_gethst_r (const char *key
, size_t keylen
, request_type type
,
148 struct hostent
*resultbuf
, char *buffer
, size_t buflen
,
149 struct hostent
**result
, int *h_errnop
)
151 hst_response_header hst_resp
;
152 int sock
= __nscd_open_socket (key
, keylen
, type
, &hst_resp
,
156 __nss_not_use_nscd_hosts
= 1;
160 /* No value found so far. */
164 if (hst_resp
.found
== -1)
166 /* The daemon does not cache this database. */
167 __nss_not_use_nscd_hosts
= 1;
171 if (hst_resp
.found
== 1)
174 uint32_t *aliases_len
;
183 /* A first check whether the buffer is sufficiently large is possible. */
184 /* Now allocate the buffer the array for the group members. We must
185 align the pointer and the base of the h_addr_list pointers. */
186 align1
= ((__alignof__ (char *) - (cp
- ((char *) 0)))
187 & (__alignof__ (char *) - 1));
188 align2
= ((__alignof__ (char *) - ((cp
+ align1
+ hst_resp
.h_name_len
)
190 & (__alignof__ (char *) - 1));
191 if (buflen
< (align1
+ hst_resp
.h_name_len
+ align2
192 + ((hst_resp
.h_aliases_cnt
+ hst_resp
.h_addr_list_cnt
+ 2)
194 + hst_resp
.h_addr_list_cnt
* (type
== AF_INET
195 ? INADDRSZ
: IN6ADDRSZ
)))
198 __set_errno (ERANGE
);
204 /* Prepare the result as far as we can. */
205 resultbuf
->h_aliases
= (char **) cp
;
206 cp
+= (hst_resp
.h_aliases_cnt
+ 1) * sizeof (char *);
207 resultbuf
->h_addr_list
= (char **) cp
;
208 cp
+= (hst_resp
.h_addr_list_cnt
+ 1) * sizeof (char *);
210 resultbuf
->h_name
= cp
;
211 cp
+= hst_resp
.h_name_len
+ align2
;
212 vec
[0].iov_base
= resultbuf
->h_name
;
213 vec
[0].iov_len
= hst_resp
.h_name_len
;
215 aliases_len
= alloca (hst_resp
.h_aliases_cnt
* sizeof (uint32_t));
216 vec
[1].iov_base
= aliases_len
;
217 vec
[1].iov_len
= hst_resp
.h_aliases_cnt
* sizeof (uint32_t);
219 total_len
= (hst_resp
.h_name_len
220 + hst_resp
.h_aliases_cnt
* sizeof (uint32_t));
223 if (type
== GETHOSTBYADDR
|| type
== GETHOSTBYNAME
)
225 vec
[2].iov_base
= cp
;
226 vec
[2].iov_len
= hst_resp
.h_addr_list_cnt
* INADDRSZ
;
228 for (cnt
= 0; cnt
< hst_resp
.h_addr_list_cnt
; ++cnt
)
230 resultbuf
->h_addr_list
[cnt
] = cp
;
234 resultbuf
->h_addrtype
= AF_INET
;
235 resultbuf
->h_length
= INADDRSZ
;
237 total_len
+= hst_resp
.h_addr_list_cnt
* INADDRSZ
;
243 if (hst_resp
.h_length
== INADDRSZ
)
245 ignore
= alloca (hst_resp
.h_addr_list_cnt
* INADDRSZ
);
246 vec
[2].iov_base
= ignore
;
247 vec
[2].iov_len
= hst_resp
.h_addr_list_cnt
* INADDRSZ
;
249 total_len
+= hst_resp
.h_addr_list_cnt
* INADDRSZ
;
254 vec
[n
].iov_base
= cp
;
255 vec
[n
].iov_len
= hst_resp
.h_addr_list_cnt
* IN6ADDRSZ
;
257 for (cnt
= 0; cnt
< hst_resp
.h_addr_list_cnt
; ++cnt
)
259 resultbuf
->h_addr_list
[cnt
] = cp
;
263 resultbuf
->h_addrtype
= AF_INET6
;
264 resultbuf
->h_length
= IN6ADDRSZ
;
266 total_len
+= hst_resp
.h_addr_list_cnt
* IN6ADDRSZ
;
270 resultbuf
->h_addr_list
[cnt
] = NULL
;
272 if ((size_t) TEMP_FAILURE_RETRY (__readv (sock
, vec
, n
)) != total_len
)
275 /* Now we also can read the aliases. */
277 for (cnt
= 0; cnt
< hst_resp
.h_aliases_cnt
; ++cnt
)
279 resultbuf
->h_aliases
[cnt
] = cp
;
280 cp
+= aliases_len
[cnt
];
281 total_len
+= aliases_len
[cnt
];
283 resultbuf
->h_aliases
[cnt
] = NULL
;
285 /* See whether this would exceed the buffer capacity. */
286 if (cp
> buffer
+ buflen
)
289 /* And finally read the aliases. */
290 if ((size_t) TEMP_FAILURE_RETRY (__read (sock
, resultbuf
->h_aliases
[0],
291 total_len
)) == total_len
)
299 /* Store the error number. */
300 *h_errnop
= hst_resp
.error
;
302 /* The `errno' to some value != ERANGE. */
303 __set_errno (ENOENT
);
304 /* Even though we have not found anything, the result is zero. */