xref: /dragonfly/lib/libc/net/gethostbyht.c (revision e3146d3a)
1 /*-
2  * Copyright (c) 1985, 1988, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  * -
29  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
30  *
31  * Permission to use, copy, modify, and distribute this software for any
32  * purpose with or without fee is hereby granted, provided that the above
33  * copyright notice and this permission notice appear in all copies, and that
34  * the name of Digital Equipment Corporation not be used in advertising or
35  * publicity pertaining to distribution of the document or software without
36  * specific, written prior permission.
37  *
38  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
39  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
40  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
41  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
42  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
43  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
44  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
45  * SOFTWARE.
46  * -
47  * --Copyright--
48  *
49  * @(#)gethostnamadr.c	8.1 (Berkeley) 6/4/93
50  * $FreeBSD: src/lib/libc/net/gethostbyht.c,v 1.12 1999/08/28 00:00:05 peter Exp $
51  * $DragonFly: src/lib/libc/net/gethostbyht.c,v 1.5 2005/09/19 09:34:53 asmodai Exp $
52  */
53 
54 #include <sys/param.h>
55 #include <sys/socket.h>
56 #include <netinet/in.h>
57 #include <arpa/inet.h>
58 #include <netdb.h>
59 #include <stdio.h>
60 #include <ctype.h>
61 #include <string.h>
62 #include <arpa/nameser.h>	/* XXX */
63 #include <resolv.h>		/* XXX */
64 
65 #define	MAXALIASES	35
66 
67 static struct hostent host;
68 static char *host_aliases[MAXALIASES];
69 static char hostbuf[BUFSIZ+1];
70 static FILE *hostf = NULL;
71 static u_int32_t host_addr[4];	/* IPv4 or IPv6 */
72 static char *h_addr_ptrs[2];
73 static int stayopen = 0;
74 
75 void
76 _sethosthtent(f)
77 	int f;
78 {
79 	if (!hostf)
80 		hostf = fopen(_PATH_HOSTS, "r" );
81 	else
82 		rewind(hostf);
83 	stayopen = f;
84 }
85 
86 void
87 _endhosthtent()
88 {
89 	if (hostf && !stayopen) {
90 		(void) fclose(hostf);
91 		hostf = NULL;
92 	}
93 }
94 
95 struct hostent *
96 gethostent()
97 {
98 	char *p;
99 	char *cp, **q;
100 	int af, len;
101 
102 	if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" ))) {
103 		h_errno = NETDB_INTERNAL;
104 		return (NULL);
105 	}
106  again:
107 	if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) {
108 		h_errno = HOST_NOT_FOUND;
109 		return (NULL);
110 	}
111 	if (*p == '#')
112 		goto again;
113 	if (!(cp = strpbrk(p, "#\n")))
114 		goto again;
115 	*cp = '\0';
116 	if (!(cp = strpbrk(p, " \t")))
117 		goto again;
118 	*cp++ = '\0';
119 	if (inet_pton(AF_INET6, p, host_addr) > 0) {
120 		af = AF_INET6;
121 		len = IN6ADDRSZ;
122 	} else if (inet_pton(AF_INET, p, host_addr) > 0) {
123 		if (_res.options & RES_USE_INET6) {
124 			_map_v4v6_address((char*)host_addr, (char*)host_addr);
125 			af = AF_INET6;
126 			len = IN6ADDRSZ;
127 		} else {
128 			af = AF_INET;
129 			len = INADDRSZ;
130 		}
131 	} else {
132 		goto again;
133 	}
134 	h_addr_ptrs[0] = (char *)host_addr;
135 	h_addr_ptrs[1] = NULL;
136 	host.h_addr_list = h_addr_ptrs;
137 	host.h_length = len;
138 	host.h_addrtype = af;
139 	while (*cp == ' ' || *cp == '\t')
140 		cp++;
141 	host.h_name = cp;
142 	q = host.h_aliases = host_aliases;
143 	if ((cp = strpbrk(cp, " \t")) != NULL)
144 		*cp++ = '\0';
145 	while (cp && *cp) {
146 		if (*cp == ' ' || *cp == '\t') {
147 			cp++;
148 			continue;
149 		}
150 		if (q < &host_aliases[MAXALIASES - 1])
151 			*q++ = cp;
152 		if ((cp = strpbrk(cp, " \t")) != NULL)
153 			*cp++ = '\0';
154 	}
155 	*q = NULL;
156 	h_errno = NETDB_SUCCESS;
157 	return (&host);
158 }
159 
160 struct hostent *
161 _gethostbyhtname(name, af)
162 	const char *name;
163 	int af;
164 {
165 	struct hostent *p;
166 	char **cp;
167 
168 	sethostent(0);
169 	while ((p = gethostent()) != NULL) {
170 		if (p->h_addrtype != af)
171 			continue;
172 		if (strcasecmp(p->h_name, name) == 0)
173 			break;
174 		for (cp = p->h_aliases; *cp != 0; cp++)
175 			if (strcasecmp(*cp, name) == 0)
176 				goto found;
177 	}
178 found:
179 	endhostent();
180 	return (p);
181 }
182 
183 struct hostent *
184 _gethostbyhtaddr(addr, len, af)
185 	const char *addr;
186 	int len, af;
187 {
188 	struct hostent *p;
189 
190 	sethostent(0);
191 	while ((p = gethostent()) != NULL)
192 		if (p->h_addrtype == af && !bcmp(p->h_addr, addr, len))
193 			break;
194 	endhostent();
195 	return (p);
196 }
197