kernel - Video - Add suppor for Intel IGD chipsets (netbook / N450 etc)
[dragonfly.git] / lib / libc / net / gethostbyht.c
blobead6f12839a4448c4b467bc710734c7d9471ed46
1 /*-
2 * Copyright (c) 1985, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
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 * 4. 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.
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.
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.
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--
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 * $DragonFly: src/lib/libc/net/gethostbyht.c,v 1.6 2005/11/13 02:04:47 swildner Exp $
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 <stdarg.h>
63 #include <nsswitch.h>
64 #include <arpa/nameser.h> /* XXX */
65 #include <resolv.h> /* XXX */
66 #include "netdb_private.h"
68 void
69 _sethosthtent(int f, struct hostent_data *hed)
71 if (!hed->hostf)
72 hed->hostf = fopen(_PATH_HOSTS, "r");
73 else
74 rewind(hed->hostf);
75 hed->stayopen = f;
78 void
79 _endhosthtent(struct hostent_data *hed)
81 if (hed->hostf && !hed->stayopen) {
82 fclose(hed->hostf);
83 hed->hostf = NULL;
87 static int
88 gethostent_p(struct hostent *he, struct hostent_data *hed, int mapped,
89 res_state statp)
91 char *p, *bp, *ep;
92 char *cp, **q;
93 int af, len;
94 char hostbuf[BUFSIZ + 1];
96 if (!hed->hostf && !(hed->hostf = fopen(_PATH_HOSTS, "r"))) {
97 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
98 return (-1);
100 again:
101 if (!(p = fgets(hostbuf, sizeof hostbuf, hed->hostf))) {
102 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
103 return (-1);
105 if (*p == '#')
106 goto again;
107 cp = strpbrk(p, "#\n");
108 if (cp != NULL)
109 *cp = '\0';
110 if (!(cp = strpbrk(p, " \t")))
111 goto again;
112 *cp++ = '\0';
113 if (inet_pton(AF_INET6, p, hed->host_addr) > 0) {
114 af = AF_INET6;
115 len = IN6ADDRSZ;
116 } else if (inet_pton(AF_INET, p, hed->host_addr) > 0) {
117 if (mapped) {
118 _map_v4v6_address((char *)hed->host_addr,
119 (char *)hed->host_addr);
120 af = AF_INET6;
121 len = IN6ADDRSZ;
122 } else {
123 af = AF_INET;
124 len = INADDRSZ;
126 } else {
127 goto again;
129 hed->h_addr_ptrs[0] = (char *)hed->host_addr;
130 hed->h_addr_ptrs[1] = NULL;
131 he->h_addr_list = hed->h_addr_ptrs;
132 he->h_length = len;
133 he->h_addrtype = af;
134 while (*cp == ' ' || *cp == '\t')
135 cp++;
136 bp = hed->hostbuf;
137 ep = hed->hostbuf + sizeof hed->hostbuf;
138 he->h_name = bp;
139 q = he->h_aliases = hed->host_aliases;
140 if ((p = strpbrk(cp, " \t")) != NULL)
141 *p++ = '\0';
142 len = strlen(cp) + 1;
143 if (ep - bp < len) {
144 RES_SET_H_ERRNO(statp, NO_RECOVERY);
145 return (-1);
147 strlcpy(bp, cp, ep - bp);
148 bp += len;
149 cp = p;
150 while (cp && *cp) {
151 if (*cp == ' ' || *cp == '\t') {
152 cp++;
153 continue;
155 if (q >= &hed->host_aliases[_MAXALIASES - 1])
156 break;
157 if ((p = strpbrk(cp, " \t")) != NULL)
158 *p++ = '\0';
159 len = strlen(cp) + 1;
160 if (ep - bp < len)
161 break;
162 strlcpy(bp, cp, ep - bp);
163 *q++ = bp;
164 bp += len;
165 cp = p;
167 *q = NULL;
168 RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
169 return (0);
173 gethostent_r(struct hostent *hptr, char *buffer, size_t buflen,
174 struct hostent **result, int *h_errnop)
176 struct hostent_data *hed;
177 struct hostent he;
178 res_state statp;
180 statp = __res_state();
181 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
182 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
183 *h_errnop = statp->res_h_errno;
184 return (-1);
186 if ((hed = __hostent_data_init()) == NULL) {
187 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
188 *h_errnop = statp->res_h_errno;
189 return (-1);
191 if (gethostent_p(&he, hed, statp->options & RES_USE_INET6, statp) != 0)
192 return (-1);
193 if (__copy_hostent(&he, hptr, buffer, buflen) != 0)
194 return (-1);
195 *result = hptr;
196 return (0);
199 struct hostent *
200 gethostent(void)
202 struct hostdata *hd;
203 struct hostent *rval;
204 int ret_h_errno;
206 if ((hd = __hostdata_init()) == NULL)
207 return (NULL);
208 if (gethostent_r(&hd->host, hd->data, sizeof(hd->data), &rval,
209 &ret_h_errno) != 0)
210 return (NULL);
211 return (rval);
215 _ht_gethostbyname(void *rval, void *cb_data, va_list ap)
217 const char *name;
218 int af;
219 char *buffer;
220 size_t buflen;
221 int *errnop, *h_errnop;
222 struct hostent *hptr, he;
223 struct hostent_data *hed;
224 char **cp;
225 res_state statp;
226 int error;
228 name = va_arg(ap, const char *);
229 af = va_arg(ap, int);
230 hptr = va_arg(ap, struct hostent *);
231 buffer = va_arg(ap, char *);
232 buflen = va_arg(ap, size_t);
233 errnop = va_arg(ap, int *);
234 h_errnop = va_arg(ap, int *);
236 *((struct hostent **)rval) = NULL;
238 statp = __res_state();
239 if ((hed = __hostent_data_init()) == NULL) {
240 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
241 *h_errnop = statp->res_h_errno;
242 return (NS_NOTFOUND);
245 _sethosthtent(0, hed);
246 while ((error = gethostent_p(&he, hed, 0, statp)) == 0) {
247 if (he.h_addrtype != af)
248 continue;
249 if (he.h_addrtype == AF_INET &&
250 statp->options & RES_USE_INET6) {
251 _map_v4v6_address(he.h_addr, he.h_addr);
252 he.h_length = IN6ADDRSZ;
253 he.h_addrtype = AF_INET6;
255 if (strcasecmp(he.h_name, name) == 0)
256 break;
257 for (cp = he.h_aliases; *cp != 0; cp++)
258 if (strcasecmp(*cp, name) == 0)
259 goto found;
261 found:
262 _endhosthtent(hed);
264 if (error != 0) {
265 *h_errnop = statp->res_h_errno;
266 return (NS_NOTFOUND);
268 if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
269 *h_errnop = statp->res_h_errno;
270 return (NS_NOTFOUND);
272 *((struct hostent **)rval) = hptr;
273 return (NS_SUCCESS);
277 _ht_gethostbyaddr(void *rval, void *cb_data, va_list ap)
279 const void *addr;
280 socklen_t len;
281 int af;
282 char *buffer;
283 size_t buflen;
284 int *errnop, *h_errnop;
285 struct hostent *hptr, he;
286 struct hostent_data *hed;
287 res_state statp;
288 int error;
290 addr = va_arg(ap, const void *);
291 len = va_arg(ap, socklen_t);
292 af = va_arg(ap, int);
293 hptr = va_arg(ap, struct hostent *);
294 buffer = va_arg(ap, char *);
295 buflen = va_arg(ap, size_t);
296 errnop = va_arg(ap, int *);
297 h_errnop = va_arg(ap, int *);
299 *((struct hostent **)rval) = NULL;
301 statp = __res_state();
302 if ((hed = __hostent_data_init()) == NULL) {
303 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
304 *h_errnop = statp->res_h_errno;
305 return (NS_NOTFOUND);
308 _sethosthtent(0, hed);
309 while ((error = gethostent_p(&he, hed, 0, statp)) == 0)
310 if (he.h_addrtype == af && !bcmp(he.h_addr, addr, len)) {
311 if (he.h_addrtype == AF_INET &&
312 statp->options & RES_USE_INET6) {
313 _map_v4v6_address(he.h_addr, he.h_addr);
314 he.h_length = IN6ADDRSZ;
315 he.h_addrtype = AF_INET6;
317 break;
319 _endhosthtent(hed);
321 if (error != 0)
322 return (NS_NOTFOUND);
323 if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
324 *h_errnop = statp->res_h_errno;
325 return (NS_NOTFOUND);
327 *((struct hostent **)rval) = hptr;
328 return (NS_SUCCESS);