2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1994, Garrett Wollman
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
35 #include <arpa/nameser.h>
44 #include <resolv.h> /* XXX */
47 #include <rpcsvc/yp_prot.h>
48 #include <rpcsvc/ypclnt.h>
50 #include "netdb_private.h"
54 _gethostbynis(const char *name, char *map, int af, struct hostent *he,
55 struct hostent_data *hed)
60 int resultlen, size, addrok = 0;
64 statp = __res_state();
74 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
78 if (hed->yp_domain == (char *)NULL)
79 if (yp_get_default_domain (&hed->yp_domain)) {
80 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
84 if (yp_match(hed->yp_domain, map, name, strlen(name), &result,
86 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
90 /* avoid potential memory leak */
91 ypbuf = alloca(resultlen + 2);
92 bcopy(result, ypbuf, resultlen);
93 ypbuf[resultlen] = '\0';
97 if ((cp = strchr(result, '\n')))
100 cp = strpbrk(result, " \t");
102 he->h_addr_list = hed->h_addr_ptrs;
103 he->h_addr = (char *)hed->host_addr;
106 addrok = inet_aton(result, (struct in_addr *)hed->host_addr);
109 if (statp->options & RES_USE_INET6) {
110 _map_v4v6_address((char *)hed->host_addr,
111 (char *)hed->host_addr);
117 addrok = inet_pton(af, result, hed->host_addr);
121 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
124 he->h_addr_list[1] = NULL;
127 while (*cp == ' ' || *cp == '\t')
130 ep = hed->hostbuf + sizeof hed->hostbuf;
132 q = he->h_aliases = hed->host_aliases;
133 p = strpbrk(cp, " \t");
136 size = strlen(cp) + 1;
137 if (ep - bp < size) {
138 RES_SET_H_ERRNO(statp, NO_RECOVERY);
141 strlcpy(bp, cp, ep - bp);
145 if (*cp == ' ' || *cp == '\t') {
149 if (q >= &hed->host_aliases[_MAXALIASES - 1])
151 p = strpbrk(cp, " \t");
154 size = strlen(cp) + 1;
157 strlcpy(bp, cp, ep - bp);
167 _gethostbynisname_r(const char *name, int af, struct hostent *he,
168 struct hostent_data *hed)
174 map = "hosts.byname";
177 map = "ipnodes.byname";
180 return (_gethostbynis(name, map, af, he, hed));
184 _gethostbynisaddr_r(const void *addr, socklen_t len, int af,
185 struct hostent *he, struct hostent_data *hed)
192 map = "hosts.byaddr";
195 map = "ipnodes.byaddr";
198 if (inet_ntop(af, addr, numaddr, sizeof(numaddr)) == NULL)
200 return (_gethostbynis(numaddr, map, af, he, hed));
205 _nis_gethostbyname(void *rval, void *cb_data, va_list ap)
212 int *errnop, *h_errnop;
213 struct hostent *hptr, he;
214 struct hostent_data *hed;
217 name = va_arg(ap, const char *);
218 af = va_arg(ap, int);
219 hptr = va_arg(ap, struct hostent *);
220 buffer = va_arg(ap, char *);
221 buflen = va_arg(ap, size_t);
222 errnop = va_arg(ap, int *);
223 h_errnop = va_arg(ap, int *);
225 *((struct hostent **)rval) = NULL;
227 statp = __res_state();
228 if ((hed = __hostent_data_init()) == NULL) {
229 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
230 *h_errnop = statp->res_h_errno;
231 return (NS_NOTFOUND);
234 if (_gethostbynisname_r(name, af, &he, hed) != 0) {
235 *h_errnop = statp->res_h_errno;
236 return (NS_NOTFOUND);
238 if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
240 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
241 *h_errnop = statp->res_h_errno;
244 *((struct hostent **)rval) = hptr;
247 *((struct hostent **)rval) = NULL;
253 _nis_gethostbyaddr(void *rval, void *cb_data, va_list ap)
261 int *errnop, *h_errnop;
262 struct hostent *hptr, he;
263 struct hostent_data *hed;
266 addr = va_arg(ap, const void *);
267 len = va_arg(ap, socklen_t);
268 af = va_arg(ap, int);
269 hptr = va_arg(ap, struct hostent *);
270 buffer = va_arg(ap, char *);
271 buflen = va_arg(ap, size_t);
272 errnop = va_arg(ap, int *);
273 h_errnop = va_arg(ap, int *);
275 *((struct hostent **)rval) = NULL;
277 statp = __res_state();
278 if ((hed = __hostent_data_init()) == NULL) {
279 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
280 *h_errnop = statp->res_h_errno;
281 return (NS_NOTFOUND);
284 if (_gethostbynisaddr_r(addr, len, af, &he, hed) != 0) {
285 *h_errnop = statp->res_h_errno;
286 return (NS_NOTFOUND);
288 if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
290 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
291 *h_errnop = statp->res_h_errno;
294 *((struct hostent **)rval) = hptr;
297 *((struct hostent **)rval) = NULL;