xref: /freebsd/lib/libc/net/gethostbynis.c (revision d82e286489da73321a47e329d98a98817b0438b6)
1 /*-
2  * Copyright (c) 1994, Garrett Wollman
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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
23  * SUCH DAMAGE.
24  */
25 
26 #if defined(LIBC_SCCS) && !defined(lint)
27 static char sccsid[] = "@(#)$Id$";
28 static char rcsid[] = "$Id$";
29 #endif /* LIBC_SCCS and not lint */
30 
31 #include <sys/param.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
35 #include <netdb.h>
36 #include <stdio.h>
37 #include <ctype.h>
38 #include <errno.h>
39 #include <string.h>
40 
41 #define	MAXALIASES	35
42 #define	MAXADDRS	35
43 
44 #ifdef YP
45 static char *host_aliases[MAXALIASES];
46 static char hostaddr[MAXADDRS];
47 static char *host_addrs[2];
48 #endif /* YP */
49 
50 static struct hostent *
51 _gethostbynis(name, map)
52 	char *name, *map;
53 {
54 #ifdef YP
55 	register char *cp, **q;
56 	char *result;
57 	int resultlen;
58 	static struct hostent h;
59 	static char *domain = (char *)NULL;
60 
61 	if (domain == (char *)NULL)
62 		if (yp_get_default_domain (&domain))
63 			return ((struct hostent *)NULL);
64 
65 	if (yp_match(domain, map, name, strlen(name), &result, &resultlen))
66 		return ((struct hostent *)NULL);
67 
68 	if ((cp = index(result, '\n')))
69 		*cp = '\0';
70 
71 	cp = strpbrk(result, " \t");
72 	*cp++ = '\0';
73 	h.h_addr_list = host_addrs;
74 	h.h_addr = hostaddr;
75 	*((u_long *)h.h_addr) = inet_addr(result);
76 	h.h_length = sizeof(u_long);
77 	h.h_addrtype = AF_INET;
78 	while (*cp == ' ' || *cp == '\t')
79 		cp++;
80 	h.h_name = cp;
81 	q = h.h_aliases = host_aliases;
82 	cp = strpbrk(cp, " \t");
83 	if (cp != NULL)
84 		*cp++ = '\0';
85 	while (cp && *cp) {
86 		if (*cp == ' ' || *cp == '\t') {
87 			cp++;
88 			continue;
89 		}
90 		if (q < &host_aliases[MAXALIASES - 1])
91 			*q++ = cp;
92 		cp = strpbrk(cp, " \t");
93 		if (cp != NULL)
94 			*cp++ = '\0';
95 	}
96 	*q = NULL;
97 	return (&h);
98 #else
99 	return (NULL);
100 #endif /* YP */
101 }
102 
103 struct hostent *
104 _gethostbynisname(name)
105 	char *name;
106 {
107 	return _gethostbynis(name, "hosts.byname");
108 }
109 
110 struct hostent *
111 _gethostbynisaddr(name)
112 	char *name;
113 {
114 	return _gethostbynis(name, "hosts.byaddr");
115 }
116