xref: /freebsd/lib/libc/net/gethostbynis.c (revision e627b39baccd1ec9129690167cf5e6d860509655)
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: gethostbynis.c,v 1.3 1996/07/12 18:54:35 jkh Exp $";
28 static char rcsid[] = "$Id: gethostbynis.c,v 1.3 1996/07/12 18:54:35 jkh Exp $";
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];
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 	free(result);
89 	result = (char *)&ypbuf;
90 
91 	if ((cp = index(result, '\n')))
92 		*cp = '\0';
93 
94 	cp = strpbrk(result, " \t");
95 	*cp++ = '\0';
96 	h.h_addr_list = host_addrs;
97 	h.h_addr = hostaddr;
98 	*((u_long *)h.h_addr) = inet_addr(result);
99 	h.h_length = sizeof(u_long);
100 	h.h_addrtype = AF_INET;
101 	while (*cp == ' ' || *cp == '\t')
102 		cp++;
103 	h.h_name = cp;
104 	q = h.h_aliases = host_aliases;
105 	cp = strpbrk(cp, " \t");
106 	if (cp != NULL)
107 		*cp++ = '\0';
108 	while (cp && *cp) {
109 		if (*cp == ' ' || *cp == '\t') {
110 			cp++;
111 			continue;
112 		}
113 		if (q < &host_aliases[MAXALIASES - 1])
114 			*q++ = cp;
115 		cp = strpbrk(cp, " \t");
116 		if (cp != NULL)
117 			*cp++ = '\0';
118 	}
119 	*q = NULL;
120 	return (&h);
121 #else
122 	return (NULL);
123 #endif /* YP */
124 }
125 
126 struct hostent *
127 _gethostbynisname(name, af)
128 	const char *name;
129 	int af;
130 {
131 	return _gethostbynis(name, "hosts.byname", af);
132 }
133 
134 struct hostent *
135 _gethostbynisaddr(addr, len, af)
136 	const char *addr;
137 	int len;
138 	int af;
139 {
140 	return _gethostbynis(inet_ntoa(*(struct in_addr *)addr),"hosts.byaddr", af);
141 }
142