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
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/param.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
33 #include <arpa/nameser.h>
42 #include <resolv.h> /* XXX */
45 #include <rpcsvc/yp_prot.h>
46 #include <rpcsvc/ypclnt.h>
48 #include "netdb_private.h"
52 _gethostbynis(const char *name, char *map, int af, struct hostent *he,
53 struct hostent_data *hed)
58 int resultlen, size, addrok = 0;
59 char ypbuf[YPMAXRECORD + 2];
70 h_errno = NETDB_INTERNAL;
74 if (hed->yp_domain == (char *)NULL)
75 if (yp_get_default_domain (&hed->yp_domain)) {
76 h_errno = NETDB_INTERNAL;
80 if (yp_match(hed->yp_domain, map, name, strlen(name), &result,
82 h_errno = HOST_NOT_FOUND;
86 /* avoid potential memory leak */
87 bcopy((char *)result, (char *)&ypbuf, resultlen);
88 ypbuf[resultlen] = '\0';
90 result = (char *)&ypbuf;
92 if ((cp = index(result, '\n')))
95 cp = strpbrk(result, " \t");
97 he->h_addr_list = hed->h_addr_ptrs;
98 he->h_addr = (char *)hed->host_addr;
101 addrok = inet_aton(result, (struct in_addr *)hed->host_addr);
104 if (_res.options & RES_USE_INET6) {
105 _map_v4v6_address((char *)hed->host_addr,
106 (char *)hed->host_addr);
112 addrok = inet_pton(af, result, hed->host_addr);
116 h_errno = HOST_NOT_FOUND;
119 he->h_addr_list[1] = NULL;
122 while (*cp == ' ' || *cp == '\t')
125 ep = hed->hostbuf + sizeof hed->hostbuf;
127 q = he->h_aliases = hed->host_aliases;
128 p = strpbrk(cp, " \t");
131 size = strlen(cp) + 1;
132 if (ep - bp < size) {
133 h_errno = NO_RECOVERY;
136 strlcpy(bp, cp, ep - bp);
140 if (*cp == ' ' || *cp == '\t') {
144 if (q >= &hed->host_aliases[_MAXALIASES - 1])
146 p = strpbrk(cp, " \t");
149 size = strlen(cp) + 1;
152 strlcpy(bp, cp, ep - bp);
162 _gethostbynisname_r(const char *name, int af, struct hostent *he,
163 struct hostent_data *hed)
169 map = "hosts.byname";
172 map = "ipnodes.byname";
175 return _gethostbynis(name, map, af, he, hed);
179 _gethostbynisaddr_r(const char *addr, int len, int af, struct hostent *he,
180 struct hostent_data *hed)
187 map = "hosts.byaddr";
190 map = "ipnodes.byaddr";
193 if (inet_ntop(af, addr, numaddr, sizeof(numaddr)) == NULL)
195 return _gethostbynis(numaddr, map, af, he, hed);
199 /* XXX _gethostbynisname/_gethostbynisaddr only used by getipnodeby*() */
201 _gethostbynisname(const char *name, int af)
208 if ((hd = __hostdata_init()) == NULL) {
209 h_errno = NETDB_INTERNAL;
212 oresopt = _res.options;
213 _res.options &= ~RES_USE_INET6;
214 error = _gethostbynisname_r(name, af, &hd->host, &hd->data);
215 _res.options = oresopt;
216 return (error == 0) ? &hd->host : NULL;
223 _gethostbynisaddr(const char *addr, int len, int af)
230 if ((hd = __hostdata_init()) == NULL) {
231 h_errno = NETDB_INTERNAL;
234 oresopt = _res.options;
235 _res.options &= ~RES_USE_INET6;
236 error = _gethostbynisaddr_r(addr, len, af, &hd->host, &hd->data);
237 _res.options = oresopt;
238 return (error == 0) ? &hd->host : NULL;
245 _nis_gethostbyname(void *rval, void *cb_data, va_list ap)
251 struct hostent_data *hed;
254 name = va_arg(ap, const char *);
255 af = va_arg(ap, int);
256 he = va_arg(ap, struct hostent *);
257 hed = va_arg(ap, struct hostent_data *);
259 error = _gethostbynisname_r(name, af, he, hed);
260 return (error == 0) ? NS_SUCCESS : NS_NOTFOUND;
267 _nis_gethostbyaddr(void *rval, void *cb_data, va_list ap)
274 struct hostent_data *hed;
277 addr = va_arg(ap, const char *);
278 len = va_arg(ap, int);
279 af = va_arg(ap, int);
280 he = va_arg(ap, struct hostent *);
281 hed = va_arg(ap, struct hostent_data *);
283 error = _gethostbynisaddr_r(addr, len, af, he, hed);
284 return (error == 0) ? NS_SUCCESS : NS_NOTFOUND;