xref: /dragonfly/lib/libc/net/gethostbyht.c (revision cf89a63b)
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.27 2007/01/09 00:28:02 imp Exp $
51  */
52 
53 #include <sys/param.h>
54 #include <sys/socket.h>
55 #include <netinet/in.h>
56 #include <arpa/inet.h>
57 #include <netdb.h>
58 #include <stdio.h>
59 #include <ctype.h>
60 #include <string.h>
61 #include <stdarg.h>
62 #include <nsswitch.h>
63 #include <arpa/nameser.h>	/* XXX */
64 #include <resolv.h>		/* XXX */
65 #include "netdb_private.h"
66 
67 void
68 _sethosthtent(int f, struct hostent_data *hed)
69 {
70 	if (!hed->hostf)
71 		hed->hostf = fopen(_PATH_HOSTS, "r");
72 	else
73 		rewind(hed->hostf);
74 	hed->stayopen = f;
75 }
76 
77 void
78 _endhosthtent(struct hostent_data *hed)
79 {
80 	if (hed->hostf && !hed->stayopen) {
81 		fclose(hed->hostf);
82 		hed->hostf = NULL;
83 	}
84 }
85 
86 static int
87 gethostent_p(struct hostent *he, struct hostent_data *hed, int mapped,
88 	     res_state statp)
89 {
90 	char *p, *bp, *ep;
91 	char *cp, **q;
92 	int af, len;
93 	char hostbuf[BUFSIZ + 1];
94 
95 	if (!hed->hostf && !(hed->hostf = fopen(_PATH_HOSTS, "r"))) {
96 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
97 		return (-1);
98 	}
99  again:
100 	if (!(p = fgets(hostbuf, sizeof hostbuf, hed->hostf))) {
101 		RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
102 		return (-1);
103 	}
104 	if (*p == '#')
105 		goto again;
106 	cp = strpbrk(p, "#\n");
107 	if (cp != NULL)
108 		*cp = '\0';
109 	if (!(cp = strpbrk(p, " \t")))
110 		goto again;
111 	*cp++ = '\0';
112 	if (inet_pton(AF_INET6, p, hed->host_addr) > 0) {
113 		af = AF_INET6;
114 		len = IN6ADDRSZ;
115 	} else if (inet_pton(AF_INET, p, hed->host_addr) > 0) {
116 		if (mapped) {
117 			_map_v4v6_address((char *)hed->host_addr,
118 			    (char *)hed->host_addr);
119 			af = AF_INET6;
120 			len = IN6ADDRSZ;
121 		} else {
122 			af = AF_INET;
123 			len = INADDRSZ;
124 		}
125 	} else {
126 		goto again;
127 	}
128 	hed->h_addr_ptrs[0] = (char *)hed->host_addr;
129 	hed->h_addr_ptrs[1] = NULL;
130 	he->h_addr_list = hed->h_addr_ptrs;
131 	he->h_length = len;
132 	he->h_addrtype = af;
133 	while (*cp == ' ' || *cp == '\t')
134 		cp++;
135 	bp = hed->hostbuf;
136 	ep = hed->hostbuf + sizeof hed->hostbuf;
137 	he->h_name = bp;
138 	q = he->h_aliases = hed->host_aliases;
139 	if ((p = strpbrk(cp, " \t")) != NULL)
140 		*p++ = '\0';
141 	len = strlen(cp) + 1;
142 	if (ep - bp < len) {
143 		RES_SET_H_ERRNO(statp, NO_RECOVERY);
144 		return (-1);
145 	}
146 	strlcpy(bp, cp, ep - bp);
147 	bp += len;
148 	cp = p;
149 	while (cp && *cp) {
150 		if (*cp == ' ' || *cp == '\t') {
151 			cp++;
152 			continue;
153 		}
154 		if (q >= &hed->host_aliases[_MAXALIASES - 1])
155 			break;
156 		if ((p = strpbrk(cp, " \t")) != NULL)
157 			*p++ = '\0';
158 		len = strlen(cp) + 1;
159 		if (ep - bp < len)
160 			break;
161 		strlcpy(bp, cp, ep - bp);
162 		*q++ = bp;
163 		bp += len;
164 		cp = p;
165 	}
166 	*q = NULL;
167 	RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
168 	return (0);
169 }
170 
171 int
172 gethostent_r(struct hostent *hptr, char *buffer, size_t buflen,
173 	     struct hostent **result, int *h_errnop)
174 {
175 	struct hostent_data *hed;
176 	struct hostent he;
177 	res_state statp;
178 
179 	statp = __res_state();
180 	if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
181 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
182 		*h_errnop = statp->res_h_errno;
183 		return (-1);
184 	}
185 	if ((hed = __hostent_data_init()) == NULL) {
186 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
187 		*h_errnop = statp->res_h_errno;
188 		return (-1);
189 	}
190 	if (gethostent_p(&he, hed, statp->options & RES_USE_INET6, statp) != 0)
191 		return (-1);
192 	if (__copy_hostent(&he, hptr, buffer, buflen) != 0)
193 		return (-1);
194 	*result = hptr;
195 	return (0);
196 }
197 
198 struct hostent *
199 gethostent(void)
200 {
201 	struct hostdata *hd;
202 	struct hostent *rval;
203 	int ret_h_errno;
204 
205 	if ((hd = __hostdata_init()) == NULL)
206 		return (NULL);
207 	if (gethostent_r(&hd->host, hd->data, sizeof(hd->data), &rval,
208 	    &ret_h_errno) != 0)
209 		return (NULL);
210 	return (rval);
211 }
212 
213 int
214 _ht_gethostbyname(void *rval, void *cb_data __unused, va_list ap)
215 {
216 	const char *name;
217 	int af;
218 	char *buffer;
219 	size_t buflen;
220 	int *errnop, *h_errnop;
221 	struct hostent *hptr, he;
222 	struct hostent_data *hed;
223 	char **cp;
224 	res_state statp;
225 	int error;
226 
227 	name = va_arg(ap, const char *);
228 	af = va_arg(ap, int);
229 	hptr = va_arg(ap, struct hostent *);
230 	buffer = va_arg(ap, char *);
231 	buflen = va_arg(ap, size_t);
232 	errnop = va_arg(ap, int *);
233 	h_errnop = va_arg(ap, int *);
234 
235 	*((struct hostent **)rval) = NULL;
236 
237 	statp = __res_state();
238 	if ((hed = __hostent_data_init()) == NULL) {
239 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
240 		*h_errnop = statp->res_h_errno;
241 		return (NS_NOTFOUND);
242 	}
243 
244 	_sethosthtent(0, hed);
245 	while ((error = gethostent_p(&he, hed, 0, statp)) == 0) {
246 		if (he.h_addrtype != af)
247 			continue;
248 		if (he.h_addrtype == AF_INET &&
249 		    statp->options & RES_USE_INET6) {
250 			_map_v4v6_address(he.h_addr, he.h_addr);
251 			he.h_length = IN6ADDRSZ;
252 			he.h_addrtype = AF_INET6;
253 		}
254 		if (strcasecmp(he.h_name, name) == 0)
255 			break;
256 		for (cp = he.h_aliases; *cp != NULL; cp++)
257 			if (strcasecmp(*cp, name) == 0)
258 				goto found;
259 	}
260 found:
261 	_endhosthtent(hed);
262 
263 	if (error != 0) {
264 		*h_errnop = statp->res_h_errno;
265 		return (NS_NOTFOUND);
266 	}
267 	if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
268 		*h_errnop = statp->res_h_errno;
269 		return (NS_NOTFOUND);
270 	}
271 	*((struct hostent **)rval) = hptr;
272 	return (NS_SUCCESS);
273 }
274 
275 int
276 _ht_gethostbyaddr(void *rval, void *cb_data __unused, va_list ap)
277 {
278 	const void *addr;
279 	socklen_t len;
280 	int af;
281 	char *buffer;
282 	size_t buflen;
283 	int *errnop, *h_errnop;
284 	struct hostent *hptr, he;
285 	struct hostent_data *hed;
286 	res_state statp;
287 	int error;
288 
289 	addr = va_arg(ap, const void *);
290 	len = va_arg(ap, socklen_t);
291 	af = va_arg(ap, int);
292 	hptr = va_arg(ap, struct hostent *);
293 	buffer = va_arg(ap, char *);
294 	buflen = va_arg(ap, size_t);
295 	errnop = va_arg(ap, int *);
296 	h_errnop = va_arg(ap, int *);
297 
298 	*((struct hostent **)rval) = NULL;
299 
300 	statp = __res_state();
301 	if ((hed = __hostent_data_init()) == NULL) {
302 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
303 		*h_errnop = statp->res_h_errno;
304 		return (NS_NOTFOUND);
305 	}
306 
307 	_sethosthtent(0, hed);
308 	while ((error = gethostent_p(&he, hed, 0, statp)) == 0)
309 		if (he.h_addrtype == af && !bcmp(he.h_addr, addr, len)) {
310 			if (he.h_addrtype == AF_INET &&
311 			    statp->options & RES_USE_INET6) {
312 				_map_v4v6_address(he.h_addr, he.h_addr);
313 				he.h_length = IN6ADDRSZ;
314 				he.h_addrtype = AF_INET6;
315 			}
316 			break;
317 		}
318 	_endhosthtent(hed);
319 
320 	if (error != 0)
321 		return (NS_NOTFOUND);
322 	if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
323 		*h_errnop = statp->res_h_errno;
324 		return (NS_NOTFOUND);
325 	}
326 	*((struct hostent **)rval) = hptr;
327 	return (NS_SUCCESS);
328 }
329