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 #if defined(LIBC_SCCS) && !defined(lint)
27 static char sccsid[] = "@(#)$FreeBSD: src/lib/libc/net/gethostbynis.c,v 1.10.2.1 2000/10/01 16:39:47 nectar Exp $";
28 static char rcsid[] = "$FreeBSD: src/lib/libc/net/gethostbynis.c,v 1.10.2.1 2000/10/01 16:39:47 nectar Exp $";
29 #endif /* LIBC_SCCS and not lint */
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 <rpcsvc/yp_prot.h>
45 #include <rpcsvc/ypclnt.h>
54 static char *host_aliases[MAXALIASES];
55 static char hostaddr[MAXADDRS];
56 static char *host_addrs[2];
59 static struct hostent *
60 _gethostbynis(name, map, af)
66 register char *cp, **q;
69 static struct hostent h;
70 static char *domain = (char *)NULL;
71 static char ypbuf[YPMAXRECORD + 2];
81 h_errno = NETDB_INTERNAL;
85 if (domain == (char *)NULL)
86 if (yp_get_default_domain (&domain)) {
87 h_errno = NETDB_INTERNAL;
88 return ((struct hostent *)NULL);
91 if (yp_match(domain, map, name, strlen(name), &result, &resultlen)) {
92 h_errno = HOST_NOT_FOUND;
93 return ((struct hostent *)NULL);
96 /* avoid potential memory leak */
97 bcopy((char *)result, (char *)&ypbuf, resultlen);
98 ypbuf[resultlen] = '\0';
100 result = (char *)&ypbuf;
102 if ((cp = index(result, '\n')))
105 cp = strpbrk(result, " \t");
107 h.h_addr_list = host_addrs;
109 *((u_long *)h.h_addr) = inet_addr(result);
111 h.h_addrtype = AF_INET;
112 while (*cp == ' ' || *cp == '\t')
115 q = h.h_aliases = host_aliases;
116 cp = strpbrk(cp, " \t");
120 if (*cp == ' ' || *cp == '\t') {
124 if (q < &host_aliases[MAXALIASES - 1])
126 cp = strpbrk(cp, " \t");
138 _gethostbynisname(name, af)
142 return _gethostbynis(name, "hosts.byname", af);
146 _gethostbynisaddr(addr, len, af)
151 return _gethostbynis(inet_ntoa(*(struct in_addr *)addr),"hosts.byaddr", af);