blob: 66c305f881d094b95e6a6e1af7871bf281ee27cc [file] [log] [blame]
Yabin Cui58d33a52014-12-16 17:03:44 -08001/* $NetBSD: sethostent.c,v 1.20 2014/03/17 13:24:23 christos Exp $ */
2
3/*
4 * Copyright (c) 1985, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
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.
18 *
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.
30 */
31
32#include <sys/cdefs.h>
33#if defined(LIBC_SCCS) && !defined(lint)
34#if 0
35static char sccsid[] = "@(#)sethostent.c 8.1 (Berkeley) 6/4/93";
36static 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.20 2014/03/17 13:24:23 christos Exp $");
39#endif
40#endif /* LIBC_SCCS and not lint */
41
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>
54
55#include "hostent.h"
56#include "resolv_private.h"
57
58#define ALIGNBYTES (sizeof(uintptr_t) - 1)
59#define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) &~ ALIGNBYTES)
60
61#ifndef _REENTRANT
62void res_close(void);
63#endif
64
65static struct hostent *_hf_gethtbyname2(const char *, int, struct getnamaddr *);
66
Elliott Hughes50339182017-10-13 17:52:01 -070067void
68/*ARGSUSED*/
69sethostent(int stayopen)
70{
71 res_static rs = __res_get_static();
72 if (rs) sethostent_r(&rs->hostf);
73}
74
75void
76endhostent(void)
77{
78 res_static rs = __res_get_static();
79 if (rs) endhostent_r(&rs->hostf);
80}
Yabin Cui58d33a52014-12-16 17:03:44 -080081
82void
83sethostent_r(FILE **hf)
84{
85 if (!*hf)
Elliott Hughes50339182017-10-13 17:52:01 -070086 *hf = fopen(_PATH_HOSTS, "re");
Yabin Cui58d33a52014-12-16 17:03:44 -080087 else
88 rewind(*hf);
89}
90
91void
92endhostent_r(FILE **hf)
93{
94 if (*hf) {
95 (void)fclose(*hf);
96 *hf = NULL;
97 }
98}
99
100/*ARGSUSED*/
101int
102_hf_gethtbyname(void *rv, void *cb_data, va_list ap)
103{
104 struct hostent *hp;
105 const char *name;
106 int af;
107 struct getnamaddr *info = rv;
108
109 _DIAGASSERT(rv != NULL);
110
111 name = va_arg(ap, char *);
112 /* NOSTRICT skip string len */(void)va_arg(ap, int);
113 af = va_arg(ap, int);
114
115#if 0
116 {
117 res_state res = __res_get_state();
118 if (res == NULL)
119 return NS_NOTFOUND;
120 if (res->options & RES_USE_INET6)
121 hp = _hf_gethtbyname2(name, AF_INET6, info);
122 else
123 hp = NULL;
124 if (hp == NULL)
125 hp = _hf_gethtbyname2(name, AF_INET, info);
126 __res_put_state(res);
127 }
128#else
129 hp = _hf_gethtbyname2(name, af, info);
130#endif
131 if (hp == NULL) {
Elliott Hughes50339182017-10-13 17:52:01 -0700132 *info->he = HOST_NOT_FOUND;
Yabin Cui58d33a52014-12-16 17:03:44 -0800133 return NS_NOTFOUND;
134 }
135 return NS_SUCCESS;
136}
137
Elliott Hughes50339182017-10-13 17:52:01 -0700138struct hostent *
Yabin Cui58d33a52014-12-16 17:03:44 -0800139_hf_gethtbyname2(const char *name, int af, struct getnamaddr *info)
140{
141 struct hostent *hp, hent;
142 char *buf, *ptr;
143 size_t len, anum, num, i;
144 FILE *hf;
145 char *aliases[MAXALIASES];
146 char *addr_ptrs[MAXADDRS];
147
148 _DIAGASSERT(name != NULL);
149
150 hf = NULL;
151 sethostent_r(&hf);
152 if (hf == NULL) {
153 errno = EINVAL;
154 *info->he = NETDB_INTERNAL;
155 return NULL;
156 }
157
158 if ((ptr = buf = malloc(len = info->buflen)) == NULL) {
Yabin Cui58d33a52014-12-16 17:03:44 -0800159 *info->he = NETDB_INTERNAL;
160 return NULL;
161 }
162
163 anum = 0; /* XXX: gcc */
164 hent.h_name = NULL; /* XXX: gcc */
165 hent.h_addrtype = 0; /* XXX: gcc */
166 hent.h_length = 0; /* XXX: gcc */
167
168 for (num = 0; num < MAXADDRS;) {
169 info->hp->h_addrtype = af;
170 info->hp->h_length = 0;
171
172 hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen,
173 info->he);
Elliott Hughes50339182017-10-13 17:52:01 -0700174 if (hp == NULL)
Yabin Cui58d33a52014-12-16 17:03:44 -0800175 break;
176
177 if (strcasecmp(hp->h_name, name) != 0) {
178 char **cp;
179 for (cp = hp->h_aliases; *cp != NULL; cp++)
180 if (strcasecmp(*cp, name) == 0)
181 break;
182 if (*cp == NULL) continue;
183 }
184
185 if (num == 0) {
186 hent.h_addrtype = af = hp->h_addrtype;
187 hent.h_length = hp->h_length;
188
189 HENT_SCOPY(hent.h_name, hp->h_name, ptr, len);
190 for (anum = 0; hp->h_aliases[anum]; anum++) {
191 if (anum >= MAXALIASES)
192 goto nospc;
193 HENT_SCOPY(aliases[anum], hp->h_aliases[anum],
194 ptr, len);
195 }
196 ptr = (void *)ALIGN(ptr);
197 if ((size_t)(ptr - buf) >= info->buflen)
198 goto nospc;
199 }
200
201 if (num >= MAXADDRS)
202 goto nospc;
203 HENT_COPY(addr_ptrs[num], hp->h_addr_list[0], hp->h_length, ptr,
204 len);
205 num++;
206 }
207 endhostent_r(&hf);
208
209 if (num == 0) {
210 *info->he = HOST_NOT_FOUND;
211 free(buf);
212 return NULL;
213 }
214
215 hp = info->hp;
216 ptr = info->buf;
217 len = info->buflen;
218
219 hp->h_addrtype = hent.h_addrtype;
220 hp->h_length = hent.h_length;
221
222 HENT_ARRAY(hp->h_aliases, anum, ptr, len);
223 HENT_ARRAY(hp->h_addr_list, num, ptr, len);
224
225 for (i = 0; i < num; i++)
226 HENT_COPY(hp->h_addr_list[i], addr_ptrs[i], hp->h_length, ptr,
227 len);
228 hp->h_addr_list[num] = NULL;
229
230 HENT_SCOPY(hp->h_name, hent.h_name, ptr, len);
231
232 for (i = 0; i < anum; i++)
233 HENT_SCOPY(hp->h_aliases[i], aliases[i], ptr, len);
234 hp->h_aliases[anum] = NULL;
235
236 free(buf);
237 return hp;
238nospc:
Yabin Cui58d33a52014-12-16 17:03:44 -0800239 *info->he = NETDB_INTERNAL;
240 free(buf);
241 errno = ENOSPC;
242 return NULL;
243}
244
245/*ARGSUSED*/
246int
247_hf_gethtbyaddr(void *rv, void *cb_data, va_list ap)
248{
249 struct hostent *hp;
250 const unsigned char *addr;
251 struct getnamaddr *info = rv;
252 FILE *hf;
253
254 _DIAGASSERT(rv != NULL);
255
256 addr = va_arg(ap, unsigned char *);
257 info->hp->h_length = va_arg(ap, int);
258 info->hp->h_addrtype = va_arg(ap, int);
259
260 hf = NULL;
261 sethostent_r(&hf);
262 if (hf == NULL) {
263 *info->he = NETDB_INTERNAL;
264 return NS_UNAVAIL;
265 }
266 while ((hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen,
267 info->he)) != NULL)
268 if (!memcmp(hp->h_addr_list[0], addr, (size_t)hp->h_length))
269 break;
270 endhostent_r(&hf);
271
272 if (hp == NULL) {
Yabin Cui58d33a52014-12-16 17:03:44 -0800273 *info->he = HOST_NOT_FOUND;
274 return NS_NOTFOUND;
275 }
276 return NS_SUCCESS;
277}