xref: /freebsd/lib/libc/net/gethostbynis.c (revision 952d112864d8008aa87278a30a539d888a8493cd)
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 <stdlib.h>
38 #include <ctype.h>
39 #include <errno.h>
40 #include <string.h>
41 #ifdef YP
42 #include <rpc/rpc.h>
43 #include <rpcsvc/yp_prot.h>
44 #include <rpcsvc/ypclnt.h>
45 #endif
46 
47 #define	MAXALIASES	35
48 #define	MAXADDRS	35
49 
50 #ifdef YP
51 static char *host_aliases[MAXALIASES];
52 static char hostaddr[MAXADDRS];
53 static char *host_addrs[2];
54 #endif /* YP */
55 
56 static struct hostent *
57 _gethostbynis(name, map, af)
58 	const char *name;
59 	char *map;
60 	int af;
61 {
62 #ifdef YP
63 	register char *cp, **q;
64 	char *result;
65 	int resultlen;
66 	static struct hostent h;
67 	static char *domain = (char *)NULL;
68 	static char ypbuf[YPMAXRECORD + 2];
69 
70 	switch(af) {
71 	case AF_INET:
72 		break;
73 	default:
74 	case AF_INET6:
75 		errno = EAFNOSUPPORT;
76 		return NULL;
77 	}
78 
79 	if (domain == (char *)NULL)
80 		if (yp_get_default_domain (&domain))
81 			return ((struct hostent *)NULL);
82 
83 	if (yp_match(domain, map, name, strlen(name), &result, &resultlen))
84 		return ((struct hostent *)NULL);
85 
86 	/* avoid potential memory leak */
87 	bcopy((char *)result, (char *)&ypbuf, resultlen);
88 	ypbuf[resultlen] = '\0';
89 	free(result);
90 	result = (char *)&ypbuf;
91 
92 	if ((cp = index(result, '\n')))
93 		*cp = '\0';
94 
95 	cp = strpbrk(result, " \t");
96 	*cp++ = '\0';
97 	h.h_addr_list = host_addrs;
98 	h.h_addr = hostaddr;
99 	*((u_long *)h.h_addr) = inet_addr(result);
100 	h.h_length = sizeof(u_long);
101 	h.h_addrtype = AF_INET;
102 	while (*cp == ' ' || *cp == '\t')
103 		cp++;
104 	h.h_name = cp;
105 	q = h.h_aliases = host_aliases;
106 	cp = strpbrk(cp, " \t");
107 	if (cp != NULL)
108 		*cp++ = '\0';
109 	while (cp && *cp) {
110 		if (*cp == ' ' || *cp == '\t') {
111 			cp++;
112 			continue;
113 		}
114 		if (q < &host_aliases[MAXALIASES - 1])
115 			*q++ = cp;
116 		cp = strpbrk(cp, " \t");
117 		if (cp != NULL)
118 			*cp++ = '\0';
119 	}
120 	*q = NULL;
121 	return (&h);
122 #else
123 	return (NULL);
124 #endif /* YP */
125 }
126 
127 struct hostent *
128 _gethostbynisname(name, af)
129 	const char *name;
130 	int af;
131 {
132 	return _gethostbynis(name, "hosts.byname", af);
133 }
134 
135 struct hostent *
136 _gethostbynisaddr(addr, len, af)
137 	const char *addr;
138 	int len;
139 	int af;
140 {
141 	return _gethostbynis(inet_ntoa(*(struct in_addr *)addr),"hosts.byaddr", af);
142 }
143