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