2 * Copyright (c) 1994, Garrett Wollman
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * $FreeBSD: src/lib/libc/net/gethostbynis.c,v 1.28 2006/05/12 15:37:23 ume Exp $
28 #include <sys/param.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <arpa/nameser.h>
41 #include <resolv.h> /* XXX */
44 #include <rpcsvc/yp_prot.h>
45 #include <rpcsvc/ypclnt.h>
47 #include "netdb_private.h"
51 _gethostbynis(const char *name, char *map, int af, struct hostent *he,
52 struct hostent_data *hed)
57 int resultlen, size, addrok = 0;
58 char ypbuf[YPMAXRECORD + 2];
61 statp = __res_state();
71 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
75 if (hed->yp_domain == NULL)
76 if (yp_get_default_domain (&hed->yp_domain)) {
77 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
81 if (yp_match(hed->yp_domain, map, name, strlen(name), &result,
83 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
87 /* avoid potential memory leak */
88 bcopy((char *)result, (char *)&ypbuf, resultlen);
89 ypbuf[resultlen] = '\0';
91 result = (char *)&ypbuf;
93 if ((cp = index(result, '\n')))
96 cp = strpbrk(result, " \t");
98 he->h_addr_list = hed->h_addr_ptrs;
99 he->h_addr = (char *)hed->host_addr;
102 addrok = inet_aton(result, (struct in_addr *)hed->host_addr);
105 if (statp->options & RES_USE_INET6) {
106 _map_v4v6_address((char *)hed->host_addr,
107 (char *)hed->host_addr);
113 addrok = inet_pton(af, result, hed->host_addr);
117 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
120 he->h_addr_list[1] = NULL;
123 while (*cp == ' ' || *cp == '\t')
126 ep = hed->hostbuf + sizeof hed->hostbuf;
128 q = he->h_aliases = hed->host_aliases;
129 p = strpbrk(cp, " \t");
132 size = strlen(cp) + 1;
133 if (ep - bp < size) {
134 RES_SET_H_ERRNO(statp, NO_RECOVERY);
137 strlcpy(bp, cp, ep - bp);
141 if (*cp == ' ' || *cp == '\t') {
145 if (q >= &hed->host_aliases[_MAXALIASES - 1])
147 p = strpbrk(cp, " \t");
150 size = strlen(cp) + 1;
153 strlcpy(bp, cp, ep - bp);
163 _gethostbynisname_r(const char *name, int af, struct hostent *he,
164 struct hostent_data *hed)
170 map = "hosts.byname";
173 map = "ipnodes.byname";
176 return (_gethostbynis(name, map, af, he, hed));
180 _gethostbynisaddr_r(const void *addr, socklen_t len __unused, int af,
181 struct hostent *he, struct hostent_data *hed)
188 map = "hosts.byaddr";
191 map = "ipnodes.byaddr";
194 if (inet_ntop(af, addr, numaddr, sizeof(numaddr)) == NULL)
196 return (_gethostbynis(numaddr, map, af, he, hed));
200 /* XXX _gethostbynisname/_gethostbynisaddr only used by getipnodeby*() */
202 _gethostbynisname(const char *name, int af)
206 struct hostent_data *hed;
211 statp = __res_state();
212 if ((he = __hostent_init()) == NULL ||
213 (hed = __hostent_data_init()) == NULL) {
214 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
218 oresopt = statp->options;
219 statp->options &= ~RES_USE_INET6;
220 error = _gethostbynisname_r(name, af, he, hed);
221 statp->options = oresopt;
222 return (error == 0) ? he : NULL;
229 _gethostbynisaddr(const void *addr, socklen_t len, int af)
233 struct hostent_data *hed;
238 statp = __res_state();
239 if ((he = __hostent_init()) == NULL ||
240 (hed = __hostent_data_init()) == NULL) {
241 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
245 oresopt = statp->options;
246 statp->options &= ~RES_USE_INET6;
247 error = _gethostbynisaddr_r(addr, len, af, he, hed);
248 statp->options = oresopt;
249 return (error == 0) ? he : NULL;
256 _nis_gethostbyname(void *rval, void *cb_data __unused, va_list ap)
263 int *errnop, *h_errnop;
264 struct hostent *hptr, he;
265 struct hostent_data *hed;
268 name = va_arg(ap, const char *);
269 af = va_arg(ap, int);
270 hptr = va_arg(ap, struct hostent *);
271 buffer = va_arg(ap, char *);
272 buflen = va_arg(ap, size_t);
273 errnop = va_arg(ap, int *);
274 h_errnop = va_arg(ap, int *);
276 *((struct hostent **)rval) = NULL;
278 statp = __res_state();
279 if ((hed = __hostent_data_init()) == NULL) {
280 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
281 *h_errnop = statp->res_h_errno;
282 return (NS_NOTFOUND);
285 if (_gethostbynisname_r(name, af, &he, hed) != 0) {
286 *h_errnop = statp->res_h_errno;
287 return (NS_NOTFOUND);
289 if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
290 *h_errnop = statp->res_h_errno;
291 return (NS_NOTFOUND);
293 *((struct hostent **)rval) = hptr;
296 *((struct hostent **)rval) = NULL;
302 _nis_gethostbyaddr(void *rval, void *cb_data __unused, va_list ap)
310 int *errnop, *h_errnop;
311 struct hostent *hptr, he;
312 struct hostent_data *hed;
315 addr = va_arg(ap, const void *);
316 len = va_arg(ap, socklen_t);
317 af = va_arg(ap, int);
318 hptr = va_arg(ap, struct hostent *);
319 buffer = va_arg(ap, char *);
320 buflen = va_arg(ap, size_t);
321 errnop = va_arg(ap, int *);
322 h_errnop = va_arg(ap, int *);
324 *((struct hostent **)rval) = NULL;
326 statp = __res_state();
327 if ((hed = __hostent_data_init()) == NULL) {
328 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
329 *h_errnop = statp->res_h_errno;
330 return (NS_NOTFOUND);
333 if (_gethostbynisaddr_r(addr, len, af, &he, hed) != 0) {
334 *h_errnop = statp->res_h_errno;
335 return (NS_NOTFOUND);
337 if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
338 *h_errnop = statp->res_h_errno;
339 return (NS_NOTFOUND);
341 *((struct hostent **)rval) = hptr;
344 *((struct hostent **)rval) = NULL;