blob: 5c4bdb5abb7a0c9a1bff8c6d42b5d66d1d8b6f17 [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
Yabin Cui58d33a52014-12-16 17:03:44 -080058#ifndef _REENTRANT
59void res_close(void);
60#endif
61
62static struct hostent *_hf_gethtbyname2(const char *, int, struct getnamaddr *);
63
Elliott Hughes50339182017-10-13 17:52:01 -070064void
65/*ARGSUSED*/
66sethostent(int stayopen)
67{
Elliott Hughesd0bbfa82021-04-08 11:58:51 -070068 struct res_static* rs = __res_get_static();
Elliott Hughes50339182017-10-13 17:52:01 -070069 if (rs) sethostent_r(&rs->hostf);
70}
71
72void
73endhostent(void)
74{
Elliott Hughesd0bbfa82021-04-08 11:58:51 -070075 struct res_static* rs = __res_get_static();
Elliott Hughes50339182017-10-13 17:52:01 -070076 if (rs) endhostent_r(&rs->hostf);
77}
Yabin Cui58d33a52014-12-16 17:03:44 -080078
79void
80sethostent_r(FILE **hf)
81{
82 if (!*hf)
Elliott Hughes50339182017-10-13 17:52:01 -070083 *hf = fopen(_PATH_HOSTS, "re");
Yabin Cui58d33a52014-12-16 17:03:44 -080084 else
85 rewind(*hf);
86}
87
88void
89endhostent_r(FILE **hf)
90{
91 if (*hf) {
92 (void)fclose(*hf);
93 *hf = NULL;
94 }
95}
96
97/*ARGSUSED*/
98int
99_hf_gethtbyname(void *rv, void *cb_data, va_list ap)
100{
101 struct hostent *hp;
102 const char *name;
103 int af;
104 struct getnamaddr *info = rv;
105
106 _DIAGASSERT(rv != NULL);
107
108 name = va_arg(ap, char *);
109 /* NOSTRICT skip string len */(void)va_arg(ap, int);
110 af = va_arg(ap, int);
111
112#if 0
113 {
114 res_state res = __res_get_state();
115 if (res == NULL)
116 return NS_NOTFOUND;
117 if (res->options & RES_USE_INET6)
118 hp = _hf_gethtbyname2(name, AF_INET6, info);
119 else
120 hp = NULL;
121 if (hp == NULL)
122 hp = _hf_gethtbyname2(name, AF_INET, info);
123 __res_put_state(res);
124 }
125#else
126 hp = _hf_gethtbyname2(name, af, info);
127#endif
128 if (hp == NULL) {
Elliott Hughesbb7d9fb2017-10-23 17:38:35 -0700129 if (*info->he == NETDB_INTERNAL && errno == ENOSPC) {
130 return NS_UNAVAIL; // glibc compatibility.
131 }
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;
wuhaitao312f8ec42023-12-04 17:29:12 +0800160 endhostent_r(&hf);
Yabin Cui58d33a52014-12-16 17:03:44 -0800161 return NULL;
162 }
163
164 anum = 0; /* XXX: gcc */
165 hent.h_name = NULL; /* XXX: gcc */
166 hent.h_addrtype = 0; /* XXX: gcc */
167 hent.h_length = 0; /* XXX: gcc */
168
169 for (num = 0; num < MAXADDRS;) {
170 info->hp->h_addrtype = af;
171 info->hp->h_length = 0;
172
173 hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen,
174 info->he);
Elliott Hughesbb7d9fb2017-10-23 17:38:35 -0700175 if (hp == NULL) {
176 if (*info->he == NETDB_INTERNAL && errno == ENOSPC) {
177 goto nospc; // glibc compatibility.
178 }
Yabin Cui58d33a52014-12-16 17:03:44 -0800179 break;
Elliott Hughesbb7d9fb2017-10-23 17:38:35 -0700180 }
Yabin Cui58d33a52014-12-16 17:03:44 -0800181
182 if (strcasecmp(hp->h_name, name) != 0) {
183 char **cp;
184 for (cp = hp->h_aliases; *cp != NULL; cp++)
185 if (strcasecmp(*cp, name) == 0)
186 break;
187 if (*cp == NULL) continue;
188 }
189
190 if (num == 0) {
191 hent.h_addrtype = af = hp->h_addrtype;
192 hent.h_length = hp->h_length;
193
194 HENT_SCOPY(hent.h_name, hp->h_name, ptr, len);
195 for (anum = 0; hp->h_aliases[anum]; anum++) {
196 if (anum >= MAXALIASES)
197 goto nospc;
198 HENT_SCOPY(aliases[anum], hp->h_aliases[anum],
199 ptr, len);
200 }
201 ptr = (void *)ALIGN(ptr);
202 if ((size_t)(ptr - buf) >= info->buflen)
203 goto nospc;
204 }
205
206 if (num >= MAXADDRS)
207 goto nospc;
208 HENT_COPY(addr_ptrs[num], hp->h_addr_list[0], hp->h_length, ptr,
209 len);
210 num++;
211 }
212 endhostent_r(&hf);
213
214 if (num == 0) {
215 *info->he = HOST_NOT_FOUND;
216 free(buf);
217 return NULL;
218 }
219
220 hp = info->hp;
221 ptr = info->buf;
222 len = info->buflen;
223
224 hp->h_addrtype = hent.h_addrtype;
225 hp->h_length = hent.h_length;
226
227 HENT_ARRAY(hp->h_aliases, anum, ptr, len);
228 HENT_ARRAY(hp->h_addr_list, num, ptr, len);
229
230 for (i = 0; i < num; i++)
231 HENT_COPY(hp->h_addr_list[i], addr_ptrs[i], hp->h_length, ptr,
232 len);
233 hp->h_addr_list[num] = NULL;
234
235 HENT_SCOPY(hp->h_name, hent.h_name, ptr, len);
236
237 for (i = 0; i < anum; i++)
238 HENT_SCOPY(hp->h_aliases[i], aliases[i], ptr, len);
239 hp->h_aliases[anum] = NULL;
240
241 free(buf);
242 return hp;
243nospc:
Yabin Cui58d33a52014-12-16 17:03:44 -0800244 *info->he = NETDB_INTERNAL;
wuhaitao312f8ec42023-12-04 17:29:12 +0800245 endhostent_r(&hf);
Yabin Cui58d33a52014-12-16 17:03:44 -0800246 free(buf);
247 errno = ENOSPC;
248 return NULL;
249}
250
251/*ARGSUSED*/
252int
253_hf_gethtbyaddr(void *rv, void *cb_data, va_list ap)
254{
255 struct hostent *hp;
256 const unsigned char *addr;
257 struct getnamaddr *info = rv;
258 FILE *hf;
259
260 _DIAGASSERT(rv != NULL);
261
262 addr = va_arg(ap, unsigned char *);
263 info->hp->h_length = va_arg(ap, int);
264 info->hp->h_addrtype = va_arg(ap, int);
265
266 hf = NULL;
267 sethostent_r(&hf);
268 if (hf == NULL) {
269 *info->he = NETDB_INTERNAL;
270 return NS_UNAVAIL;
271 }
272 while ((hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen,
273 info->he)) != NULL)
274 if (!memcmp(hp->h_addr_list[0], addr, (size_t)hp->h_length))
275 break;
276 endhostent_r(&hf);
277
278 if (hp == NULL) {
Elliott Hughesbb7d9fb2017-10-23 17:38:35 -0700279 if (errno == ENOSPC) return NS_UNAVAIL; // glibc compatibility.
Yabin Cui58d33a52014-12-16 17:03:44 -0800280 *info->he = HOST_NOT_FOUND;
281 return NS_NOTFOUND;
282 }
283 return NS_SUCCESS;
284}