tools/llvm: Do not build with symbols
[minix3.git] / lib / libc / net / sethostent.c
blobd8b931f1eac40d52d3ba1378a2e045a80267b9d4
1 /* $NetBSD: sethostent.c,v 1.19 2013/08/27 09:56:12 christos Exp $ */
3 /*
4 * Copyright (c) 1985, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 #if 0
35 static char sccsid[] = "@(#)sethostent.c 8.1 (Berkeley) 6/4/93";
36 static char rcsid[] = "Id: sethostent.c,v 8.5 1996/09/28 06:51:07 vixie Exp ";
37 #else
38 __RCSID("$NetBSD: sethostent.c,v 1.19 2013/08/27 09:56:12 christos Exp $");
39 #endif
40 #endif /* LIBC_SCCS and not lint */
42 #include "namespace.h"
43 #include <sys/param.h>
44 #include <netinet/in.h>
45 #include <arpa/nameser.h>
46 #include <arpa/inet.h>
47 #include <assert.h>
48 #include <string.h>
49 #include <nsswitch.h>
50 #include <netdb.h>
51 #include <resolv.h>
52 #include <errno.h>
53 #include <stdlib.h>
55 #include "hostent.h"
57 #ifdef __weak_alias
58 __weak_alias(sethostent,_sethostent)
59 __weak_alias(endhostent,_endhostent)
60 #endif
62 #ifndef _REENTRANT
63 void res_close(void);
64 #endif
66 static struct hostent *_hf_gethtbyname2(const char *, int, struct getnamaddr *);
68 void
69 /*ARGSUSED*/
70 sethostent(int stayopen)
72 #ifndef _REENTRANT
73 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
74 return;
75 if (stayopen)
76 _res.options |= RES_STAYOPEN | RES_USEVC;
77 #endif
78 sethostent_r(&_h_file);
81 void
82 endhostent(void)
84 #ifndef _REENTRANT
85 _res.options &= ~(RES_STAYOPEN | RES_USEVC);
86 res_close();
87 #endif
88 endhostent_r(&_h_file);
90 static const char *_h_hosts = _PATH_HOSTS;
92 void
93 _hf_sethostsfile(const char *f) {
94 _h_hosts = f;
97 void
98 sethostent_r(FILE **hf)
100 if (!*hf)
101 *hf = fopen(_h_hosts, "re");
102 else
103 rewind(*hf);
106 void
107 endhostent_r(FILE **hf)
109 if (*hf) {
110 (void)fclose(*hf);
111 *hf = NULL;
115 /*ARGSUSED*/
117 _hf_gethtbyname(void *rv, void *cb_data, va_list ap)
119 struct hostent *hp;
120 const char *name;
121 int af;
122 struct getnamaddr *info = rv;
124 _DIAGASSERT(rv != NULL);
126 name = va_arg(ap, char *);
127 /* NOSTRICT skip string len */(void)va_arg(ap, int);
128 af = va_arg(ap, int);
130 #if 0
132 res_state res = __res_get_state();
133 if (res == NULL)
134 return NS_NOTFOUND;
135 if (res->options & RES_USE_INET6)
136 hp = _hf_gethtbyname2(name, AF_INET6, info);
137 else
138 hp = NULL;
139 if (hp == NULL)
140 hp = _hf_gethtbyname2(name, AF_INET, info);
141 __res_put_state(res);
143 #else
144 hp = _hf_gethtbyname2(name, af, info);
145 #endif
146 if (hp == NULL) {
147 *info->he = HOST_NOT_FOUND;
148 return NS_NOTFOUND;
150 return NS_SUCCESS;
153 struct hostent *
154 _hf_gethtbyname2(const char *name, int af, struct getnamaddr *info)
156 struct hostent *hp, hent;
157 char *buf, *ptr;
158 size_t len, anum, num, i;
159 FILE *hf;
160 char *aliases[MAXALIASES];
161 char *addr_ptrs[MAXADDRS];
163 _DIAGASSERT(name != NULL);
165 hf = NULL;
166 sethostent_r(&hf);
167 if (hf == NULL) {
168 errno = EINVAL;
169 *info->he = NETDB_INTERNAL;
170 return NULL;
173 if ((ptr = buf = malloc(len = info->buflen)) == NULL) {
174 *info->he = NETDB_INTERNAL;
175 return NULL;
178 anum = 0; /* XXX: gcc */
179 hent.h_name = NULL; /* XXX: gcc */
180 hent.h_addrtype = 0; /* XXX: gcc */
181 hent.h_length = 0; /* XXX: gcc */
183 for (num = 0; num < MAXADDRS;) {
184 info->hp->h_addrtype = af;
185 info->hp->h_length = 0;
187 hp = gethostent_r(hf, info->hp, info->buf, info->buflen,
188 info->he);
189 if (hp == NULL)
190 break;
192 if (strcasecmp(hp->h_name, name) != 0) {
193 char **cp;
194 for (cp = hp->h_aliases; *cp != NULL; cp++)
195 if (strcasecmp(*cp, name) == 0)
196 break;
197 if (*cp == NULL) continue;
200 if (num == 0) {
201 hent.h_addrtype = af = hp->h_addrtype;
202 hent.h_length = hp->h_length;
204 HENT_SCOPY(hent.h_name, hp->h_name, ptr, len);
205 for (anum = 0; hp->h_aliases[anum]; anum++) {
206 if (anum >= __arraycount(aliases))
207 goto nospc;
208 HENT_SCOPY(aliases[anum], hp->h_aliases[anum],
209 ptr, len);
211 ptr = (void *)ALIGN(ptr);
212 if ((size_t)(ptr - buf) >= info->buflen)
213 goto nospc;
216 if (num >= __arraycount(addr_ptrs))
217 goto nospc;
218 HENT_COPY(addr_ptrs[num], hp->h_addr_list[0], hp->h_length, ptr,
219 len);
220 num++;
222 endhostent_r(&hf);
224 if (num == 0) {
225 *info->he = HOST_NOT_FOUND;
226 return NULL;
229 hp = info->hp;
230 ptr = info->buf;
231 len = info->buflen;
233 hp->h_addrtype = hent.h_addrtype;
234 hp->h_length = hent.h_length;
236 HENT_ARRAY(hp->h_aliases, anum, ptr, len);
237 HENT_ARRAY(hp->h_addr_list, num, ptr, len);
239 for (i = 0; i < num; i++)
240 HENT_COPY(hp->h_addr_list[i], addr_ptrs[i], hp->h_length, ptr,
241 len);
242 hp->h_addr_list[num] = NULL;
244 HENT_SCOPY(hp->h_name, hent.h_name, ptr, len);
246 for (i = 0; i < anum; i++)
247 HENT_SCOPY(hp->h_aliases[i], aliases[i], ptr, len);
248 hp->h_aliases[anum] = NULL;
250 return hp;
251 nospc:
252 *info->he = NETDB_INTERNAL;
253 errno = ENOSPC;
254 return NULL;
257 /*ARGSUSED*/
259 _hf_gethtbyaddr(void *rv, void *cb_data, va_list ap)
261 struct hostent *hp;
262 const unsigned char *addr;
263 struct getnamaddr *info = rv;
264 FILE *hf;
266 _DIAGASSERT(rv != NULL);
268 addr = va_arg(ap, unsigned char *);
269 info->hp->h_length = va_arg(ap, int);
270 info->hp->h_addrtype = va_arg(ap, int);
272 hf = NULL;
273 sethostent_r(&hf);
274 if (hf == NULL) {
275 *info->he = NETDB_INTERNAL;
276 return NS_UNAVAIL;
278 while ((hp = gethostent_r(hf, info->hp, info->buf, info->buflen,
279 info->he)) != NULL)
280 if (!memcmp(hp->h_addr_list[0], addr, (size_t)hp->h_length))
281 break;
282 endhostent_r(&hf);
284 if (hp == NULL) {
285 *info->he = HOST_NOT_FOUND;
286 return NS_NOTFOUND;
288 return NS_SUCCESS;