1 24fb1a39 2025-06-02 kirill /* $OpenBSD: gethostnamadr_async.c,v 1.50 2024/09/03 18:20:35 op Exp $ */
3 24fb1a39 2025-06-02 kirill * Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
5 24fb1a39 2025-06-02 kirill * Permission to use, copy, modify, and distribute this software for any
6 24fb1a39 2025-06-02 kirill * purpose with or without fee is hereby granted, provided that the above
7 24fb1a39 2025-06-02 kirill * copyright notice and this permission notice appear in all copies.
9 24fb1a39 2025-06-02 kirill * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 24fb1a39 2025-06-02 kirill * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 24fb1a39 2025-06-02 kirill * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 24fb1a39 2025-06-02 kirill * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 24fb1a39 2025-06-02 kirill * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 24fb1a39 2025-06-02 kirill * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 24fb1a39 2025-06-02 kirill * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 24fb1a39 2025-06-02 kirill #include "includes.h"
20 24fb1a39 2025-06-02 kirill #include <sys/types.h>
21 24fb1a39 2025-06-02 kirill #include <sys/socket.h>
22 24fb1a39 2025-06-02 kirill #include <netinet/in.h>
23 24fb1a39 2025-06-02 kirill #include <arpa/inet.h>
24 24fb1a39 2025-06-02 kirill #include <arpa/nameser.h>
25 24fb1a39 2025-06-02 kirill #ifdef HAVE_ARPA_NAMESER_COMPAT_H
26 24fb1a39 2025-06-02 kirill #include <arpa/nameser_compat.h>
28 24fb1a39 2025-06-02 kirill #include <netdb.h>
30 24fb1a39 2025-06-02 kirill #include <asr.h>
31 24fb1a39 2025-06-02 kirill #include <ctype.h>
32 24fb1a39 2025-06-02 kirill #include <errno.h>
33 24fb1a39 2025-06-02 kirill #include <resolv.h> /* for res_hnok */
34 24fb1a39 2025-06-02 kirill #include <stdlib.h>
35 24fb1a39 2025-06-02 kirill #include <string.h>
36 24fb1a39 2025-06-02 kirill #include <unistd.h>
37 24fb1a39 2025-06-02 kirill #include <limits.h>
39 24fb1a39 2025-06-02 kirill #include "asr_private.h"
41 24fb1a39 2025-06-02 kirill #define MAXALIASES 35
42 24fb1a39 2025-06-02 kirill #define MAXADDRS 35
44 24fb1a39 2025-06-02 kirill struct hostent_ext {
45 24fb1a39 2025-06-02 kirill struct hostent h;
46 24fb1a39 2025-06-02 kirill char *aliases[MAXALIASES + 1];
47 24fb1a39 2025-06-02 kirill char *addrs[MAXADDRS + 1];
48 24fb1a39 2025-06-02 kirill char *end;
49 24fb1a39 2025-06-02 kirill char *pos;
52 24fb1a39 2025-06-02 kirill struct netent_ext {
53 24fb1a39 2025-06-02 kirill struct netent n;
54 24fb1a39 2025-06-02 kirill char *aliases[MAXALIASES + 1];
55 24fb1a39 2025-06-02 kirill char *end;
56 24fb1a39 2025-06-02 kirill char *pos;
59 24fb1a39 2025-06-02 kirill static int gethostnamadr_async_run(struct asr_query *, struct asr_result *);
60 24fb1a39 2025-06-02 kirill static struct hostent_ext *hostent_alloc(int);
61 24fb1a39 2025-06-02 kirill static int hostent_set_cname(struct hostent_ext *, const char *, int);
62 24fb1a39 2025-06-02 kirill static int hostent_add_alias(struct hostent_ext *, const char *, int);
63 24fb1a39 2025-06-02 kirill static int hostent_add_addr(struct hostent_ext *, const void *, size_t);
64 24fb1a39 2025-06-02 kirill static struct hostent_ext *hostent_from_addr(int, const char *, const char *);
65 24fb1a39 2025-06-02 kirill static struct hostent_ext *hostent_file_match(FILE *, int, int, const char *,
67 24fb1a39 2025-06-02 kirill static struct hostent_ext *hostent_from_packet(int, int, char *, size_t);
68 24fb1a39 2025-06-02 kirill static void netent_from_hostent(struct asr_result *ar);
70 24fb1a39 2025-06-02 kirill struct asr_query *
71 24fb1a39 2025-06-02 kirill gethostbyname_async(const char *name, void *asr)
73 24fb1a39 2025-06-02 kirill return gethostbyname2_async(name, AF_INET, asr);
75 24fb1a39 2025-06-02 kirill DEF_WEAK(gethostbyname_async);
77 24fb1a39 2025-06-02 kirill struct asr_query *
78 24fb1a39 2025-06-02 kirill gethostbyname2_async(const char *name, int af, void *asr)
80 24fb1a39 2025-06-02 kirill struct asr_ctx *ac;
81 24fb1a39 2025-06-02 kirill struct asr_query *as;
83 24fb1a39 2025-06-02 kirill /* the original segfaults */
84 24fb1a39 2025-06-02 kirill if (name == NULL) {
85 24fb1a39 2025-06-02 kirill errno = EINVAL;
86 24fb1a39 2025-06-02 kirill return (NULL);
89 24fb1a39 2025-06-02 kirill ac = _asr_use_resolver(asr);
90 24fb1a39 2025-06-02 kirill if ((as = _asr_async_new(ac, ASR_GETHOSTBYNAME)) == NULL)
91 24fb1a39 2025-06-02 kirill goto abort; /* errno set */
92 24fb1a39 2025-06-02 kirill as->as_run = gethostnamadr_async_run;
94 24fb1a39 2025-06-02 kirill as->as.hostnamadr.family = af;
95 24fb1a39 2025-06-02 kirill if (af == AF_INET)
96 24fb1a39 2025-06-02 kirill as->as.hostnamadr.addrlen = INADDRSZ;
97 24fb1a39 2025-06-02 kirill else if (af == AF_INET6)
98 24fb1a39 2025-06-02 kirill as->as.hostnamadr.addrlen = IN6ADDRSZ;
99 24fb1a39 2025-06-02 kirill as->as.hostnamadr.name = strdup(name);
100 24fb1a39 2025-06-02 kirill if (as->as.hostnamadr.name == NULL)
101 24fb1a39 2025-06-02 kirill goto abort; /* errno set */
103 24fb1a39 2025-06-02 kirill _asr_ctx_unref(ac);
104 24fb1a39 2025-06-02 kirill return (as);
108 24fb1a39 2025-06-02 kirill _asr_async_free(as);
109 24fb1a39 2025-06-02 kirill _asr_ctx_unref(ac);
110 24fb1a39 2025-06-02 kirill return (NULL);
112 24fb1a39 2025-06-02 kirill DEF_WEAK(gethostbyname2_async);
114 24fb1a39 2025-06-02 kirill struct asr_query *
115 24fb1a39 2025-06-02 kirill gethostbyaddr_async(const void *addr, socklen_t len, int af, void *asr)
117 24fb1a39 2025-06-02 kirill struct asr_ctx *ac;
118 24fb1a39 2025-06-02 kirill struct asr_query *as;
120 24fb1a39 2025-06-02 kirill ac = _asr_use_resolver(asr);
121 24fb1a39 2025-06-02 kirill as = _gethostbyaddr_async_ctx(addr, len, af, ac);
122 24fb1a39 2025-06-02 kirill _asr_ctx_unref(ac);
124 24fb1a39 2025-06-02 kirill return (as);
126 24fb1a39 2025-06-02 kirill DEF_WEAK(gethostbyaddr_async);
128 24fb1a39 2025-06-02 kirill struct asr_query *
129 24fb1a39 2025-06-02 kirill _gethostbyaddr_async_ctx(const void *addr, socklen_t len, int af,
130 24fb1a39 2025-06-02 kirill struct asr_ctx *ac)
132 24fb1a39 2025-06-02 kirill struct asr_query *as;
134 24fb1a39 2025-06-02 kirill if ((as = _asr_async_new(ac, ASR_GETHOSTBYADDR)) == NULL)
135 24fb1a39 2025-06-02 kirill goto abort; /* errno set */
136 24fb1a39 2025-06-02 kirill as->as_run = gethostnamadr_async_run;
138 24fb1a39 2025-06-02 kirill as->as.hostnamadr.family = af;
139 24fb1a39 2025-06-02 kirill as->as.hostnamadr.addrlen = len;
140 24fb1a39 2025-06-02 kirill if (len > 0)
141 24fb1a39 2025-06-02 kirill memmove(as->as.hostnamadr.addr, addr, (len > 16) ? 16 : len);
143 24fb1a39 2025-06-02 kirill return (as);
147 24fb1a39 2025-06-02 kirill _asr_async_free(as);
148 24fb1a39 2025-06-02 kirill return (NULL);
151 24fb1a39 2025-06-02 kirill static int
152 24fb1a39 2025-06-02 kirill gethostnamadr_async_run(struct asr_query *as, struct asr_result *ar)
154 24fb1a39 2025-06-02 kirill struct hostent_ext *h;
155 24fb1a39 2025-06-02 kirill int r, type, saved_errno;
157 24fb1a39 2025-06-02 kirill char name[MAXDNAME], *data, addr[16], *c;
160 24fb1a39 2025-06-02 kirill switch (as->as_state) {
162 24fb1a39 2025-06-02 kirill case ASR_STATE_INIT:
164 24fb1a39 2025-06-02 kirill if (as->as.hostnamadr.family != AF_INET &&
165 24fb1a39 2025-06-02 kirill as->as.hostnamadr.family != AF_INET6) {
166 24fb1a39 2025-06-02 kirill ar->ar_h_errno = NETDB_INTERNAL;
167 24fb1a39 2025-06-02 kirill ar->ar_errno = EAFNOSUPPORT;
168 24fb1a39 2025-06-02 kirill async_set_state(as, ASR_STATE_HALT);
172 24fb1a39 2025-06-02 kirill if ((as->as.hostnamadr.family == AF_INET &&
173 24fb1a39 2025-06-02 kirill as->as.hostnamadr.addrlen != INADDRSZ) ||
174 24fb1a39 2025-06-02 kirill (as->as.hostnamadr.family == AF_INET6 &&
175 24fb1a39 2025-06-02 kirill as->as.hostnamadr.addrlen != IN6ADDRSZ)) {
176 24fb1a39 2025-06-02 kirill ar->ar_h_errno = NETDB_INTERNAL;
177 24fb1a39 2025-06-02 kirill ar->ar_errno = EINVAL;
178 24fb1a39 2025-06-02 kirill async_set_state(as, ASR_STATE_HALT);
182 24fb1a39 2025-06-02 kirill if (as->as_type == ASR_GETHOSTBYNAME) {
184 24fb1a39 2025-06-02 kirill if (as->as.hostnamadr.name[0] == '\0') {
185 24fb1a39 2025-06-02 kirill ar->ar_h_errno = NO_DATA;
186 24fb1a39 2025-06-02 kirill async_set_state(as, ASR_STATE_HALT);
190 24fb1a39 2025-06-02 kirill /* Name might be an IP address string */
191 24fb1a39 2025-06-02 kirill for (c = as->as.hostnamadr.name; *c; c++)
192 24fb1a39 2025-06-02 kirill if (!isdigit((unsigned char)*c) &&
193 24fb1a39 2025-06-02 kirill *c != '.' && *c != ':')
195 24fb1a39 2025-06-02 kirill if (*c == 0 &&
196 24fb1a39 2025-06-02 kirill inet_pton(as->as.hostnamadr.family,
197 24fb1a39 2025-06-02 kirill as->as.hostnamadr.name, addr) == 1) {
198 24fb1a39 2025-06-02 kirill h = hostent_from_addr(as->as.hostnamadr.family,
199 24fb1a39 2025-06-02 kirill as->as.hostnamadr.name, addr);
200 24fb1a39 2025-06-02 kirill if (h == NULL) {
201 24fb1a39 2025-06-02 kirill ar->ar_errno = errno;
202 24fb1a39 2025-06-02 kirill ar->ar_h_errno = NETDB_INTERNAL;
205 24fb1a39 2025-06-02 kirill ar->ar_hostent = &h->h;
206 24fb1a39 2025-06-02 kirill ar->ar_h_errno = NETDB_SUCCESS;
208 24fb1a39 2025-06-02 kirill async_set_state(as, ASR_STATE_HALT);
212 24fb1a39 2025-06-02 kirill if (!hnok_lenient(as->as.hostnamadr.name)) {
213 24fb1a39 2025-06-02 kirill ar->ar_h_errno = NETDB_INTERNAL;
214 24fb1a39 2025-06-02 kirill ar->ar_errno = EINVAL;
215 24fb1a39 2025-06-02 kirill async_set_state(as, ASR_STATE_HALT);
220 24fb1a39 2025-06-02 kirill * If hostname is "localhost" or falls within the
221 24fb1a39 2025-06-02 kirill * ".localhost." domain, use local address.
222 24fb1a39 2025-06-02 kirill * RFC 6761, 6.3:
223 24fb1a39 2025-06-02 kirill * 3. Name resolution APIs and libraries SHOULD
224 24fb1a39 2025-06-02 kirill * recognize localhost names as special and SHOULD
225 24fb1a39 2025-06-02 kirill * always return the IP loopback address for address
226 24fb1a39 2025-06-02 kirill * queries and negative responses for all other query
227 24fb1a39 2025-06-02 kirill * types. Name resolution APIs SHOULD NOT send queries
228 24fb1a39 2025-06-02 kirill * for localhost names to their configured caching DNS
229 24fb1a39 2025-06-02 kirill * server(s).
232 24fb1a39 2025-06-02 kirill if (_asr_is_localhost(as->as.hostnamadr.name)) {
233 24fb1a39 2025-06-02 kirill inet_pton(as->as.hostnamadr.family,
234 24fb1a39 2025-06-02 kirill as->as.hostnamadr.family == AF_INET ?
235 24fb1a39 2025-06-02 kirill "127.0.0.1" : "::1", addr);
236 24fb1a39 2025-06-02 kirill h = hostent_from_addr(as->as.hostnamadr.family,
237 24fb1a39 2025-06-02 kirill as->as.hostnamadr.name, addr);
238 24fb1a39 2025-06-02 kirill if (h == NULL) {
239 24fb1a39 2025-06-02 kirill ar->ar_errno = errno;
240 24fb1a39 2025-06-02 kirill ar->ar_h_errno = NETDB_INTERNAL;
243 24fb1a39 2025-06-02 kirill ar->ar_hostent = &h->h;
244 24fb1a39 2025-06-02 kirill ar->ar_h_errno = NETDB_SUCCESS;
246 24fb1a39 2025-06-02 kirill async_set_state(as, ASR_STATE_HALT);
250 24fb1a39 2025-06-02 kirill async_set_state(as, ASR_STATE_NEXT_DB);
253 24fb1a39 2025-06-02 kirill case ASR_STATE_NEXT_DB:
255 24fb1a39 2025-06-02 kirill if (_asr_iter_db(as) == -1) {
256 24fb1a39 2025-06-02 kirill async_set_state(as, ASR_STATE_NOT_FOUND);
260 24fb1a39 2025-06-02 kirill switch (AS_DB(as)) {
262 24fb1a39 2025-06-02 kirill case ASR_DB_DNS:
264 24fb1a39 2025-06-02 kirill /* Create a subquery to do the DNS lookup */
266 24fb1a39 2025-06-02 kirill if (as->as_type == ASR_GETHOSTBYNAME) {
267 24fb1a39 2025-06-02 kirill type = (as->as.hostnamadr.family == AF_INET) ?
268 24fb1a39 2025-06-02 kirill T_A : T_AAAA;
269 24fb1a39 2025-06-02 kirill as->as_subq = _res_search_async_ctx(
270 24fb1a39 2025-06-02 kirill as->as.hostnamadr.name,
271 24fb1a39 2025-06-02 kirill C_IN, type, as->as_ctx);
273 24fb1a39 2025-06-02 kirill _asr_addr_as_fqdn(as->as.hostnamadr.addr,
274 24fb1a39 2025-06-02 kirill as->as.hostnamadr.family,
275 24fb1a39 2025-06-02 kirill name, sizeof(name));
276 24fb1a39 2025-06-02 kirill as->as_subq = _res_query_async_ctx(
277 24fb1a39 2025-06-02 kirill name, C_IN, T_PTR, as->as_ctx);
280 24fb1a39 2025-06-02 kirill if (as->as_subq == NULL) {
281 24fb1a39 2025-06-02 kirill ar->ar_errno = errno;
282 24fb1a39 2025-06-02 kirill ar->ar_h_errno = NETDB_INTERNAL;
283 24fb1a39 2025-06-02 kirill async_set_state(as, ASR_STATE_HALT);
287 24fb1a39 2025-06-02 kirill async_set_state(as, ASR_STATE_SUBQUERY);
290 24fb1a39 2025-06-02 kirill case ASR_DB_FILE:
292 24fb1a39 2025-06-02 kirill /* Try to find a match in the host file */
294 24fb1a39 2025-06-02 kirill if ((f = fopen(_PATH_HOSTS, "re")) == NULL)
297 24fb1a39 2025-06-02 kirill if (as->as_type == ASR_GETHOSTBYNAME)
298 24fb1a39 2025-06-02 kirill data = as->as.hostnamadr.name;
300 24fb1a39 2025-06-02 kirill data = as->as.hostnamadr.addr;
302 24fb1a39 2025-06-02 kirill h = hostent_file_match(f, as->as_type,
303 24fb1a39 2025-06-02 kirill as->as.hostnamadr.family, data,
304 24fb1a39 2025-06-02 kirill as->as.hostnamadr.addrlen);
305 24fb1a39 2025-06-02 kirill saved_errno = errno;
306 24fb1a39 2025-06-02 kirill fclose(f);
307 24fb1a39 2025-06-02 kirill errno = saved_errno;
309 24fb1a39 2025-06-02 kirill if (h == NULL) {
310 24fb1a39 2025-06-02 kirill if (errno) {
311 24fb1a39 2025-06-02 kirill ar->ar_errno = errno;
312 24fb1a39 2025-06-02 kirill ar->ar_h_errno = NETDB_INTERNAL;
313 24fb1a39 2025-06-02 kirill async_set_state(as, ASR_STATE_HALT);
315 24fb1a39 2025-06-02 kirill /* otherwise not found */
318 24fb1a39 2025-06-02 kirill ar->ar_hostent = &h->h;
319 24fb1a39 2025-06-02 kirill ar->ar_h_errno = NETDB_SUCCESS;
320 24fb1a39 2025-06-02 kirill async_set_state(as, ASR_STATE_HALT);
325 24fb1a39 2025-06-02 kirill case ASR_STATE_SUBQUERY:
327 24fb1a39 2025-06-02 kirill /* Run the DNS subquery. */
329 24fb1a39 2025-06-02 kirill if ((r = asr_run(as->as_subq, ar)) == ASYNC_COND)
330 24fb1a39 2025-06-02 kirill return (ASYNC_COND);
332 24fb1a39 2025-06-02 kirill /* Done. */
333 24fb1a39 2025-06-02 kirill as->as_subq = NULL;
336 24fb1a39 2025-06-02 kirill * We either got no packet or a packet without an answer.
337 24fb1a39 2025-06-02 kirill * Safeguard the h_errno and use the next DB.
339 24fb1a39 2025-06-02 kirill if (ar->ar_count == 0) {
340 24fb1a39 2025-06-02 kirill free(ar->ar_data);
341 24fb1a39 2025-06-02 kirill as->as.hostnamadr.subq_h_errno = ar->ar_h_errno;
342 24fb1a39 2025-06-02 kirill async_set_state(as, ASR_STATE_NEXT_DB);
346 24fb1a39 2025-06-02 kirill /* Read the hostent from the packet. */
348 24fb1a39 2025-06-02 kirill h = hostent_from_packet(as->as_type,
349 24fb1a39 2025-06-02 kirill as->as.hostnamadr.family, ar->ar_data, ar->ar_datalen);
350 24fb1a39 2025-06-02 kirill free(ar->ar_data);
351 24fb1a39 2025-06-02 kirill if (h == NULL) {
352 24fb1a39 2025-06-02 kirill ar->ar_errno = errno;
353 24fb1a39 2025-06-02 kirill ar->ar_h_errno = NETDB_INTERNAL;
354 24fb1a39 2025-06-02 kirill async_set_state(as, ASR_STATE_HALT);
358 24fb1a39 2025-06-02 kirill if (as->as_type == ASR_GETHOSTBYADDR) {
359 24fb1a39 2025-06-02 kirill if (hostent_add_addr(h, as->as.hostnamadr.addr,
360 24fb1a39 2025-06-02 kirill as->as.hostnamadr.addrlen) == -1) {
362 24fb1a39 2025-06-02 kirill ar->ar_errno = errno;
363 24fb1a39 2025-06-02 kirill ar->ar_h_errno = NETDB_INTERNAL;
364 24fb1a39 2025-06-02 kirill async_set_state(as, ASR_STATE_HALT);
370 24fb1a39 2025-06-02 kirill * No valid hostname or address found in the dns packet.
371 24fb1a39 2025-06-02 kirill * Ignore it.
373 24fb1a39 2025-06-02 kirill if ((as->as_type == ASR_GETHOSTBYNAME &&
374 24fb1a39 2025-06-02 kirill h->h.h_addr_list[0] == NULL) ||
375 24fb1a39 2025-06-02 kirill h->h.h_name == NULL) {
377 24fb1a39 2025-06-02 kirill async_set_state(as, ASR_STATE_NEXT_DB);
381 24fb1a39 2025-06-02 kirill ar->ar_hostent = &h->h;
382 24fb1a39 2025-06-02 kirill ar->ar_h_errno = NETDB_SUCCESS;
383 24fb1a39 2025-06-02 kirill async_set_state(as, ASR_STATE_HALT);
386 24fb1a39 2025-06-02 kirill case ASR_STATE_NOT_FOUND:
387 24fb1a39 2025-06-02 kirill ar->ar_errno = 0;
388 24fb1a39 2025-06-02 kirill if (as->as.hostnamadr.subq_h_errno)
389 24fb1a39 2025-06-02 kirill ar->ar_h_errno = as->as.hostnamadr.subq_h_errno;
391 24fb1a39 2025-06-02 kirill ar->ar_h_errno = HOST_NOT_FOUND;
392 24fb1a39 2025-06-02 kirill async_set_state(as, ASR_STATE_HALT);
395 24fb1a39 2025-06-02 kirill case ASR_STATE_HALT:
396 24fb1a39 2025-06-02 kirill if (ar->ar_h_errno == NETDB_SUCCESS &&
397 24fb1a39 2025-06-02 kirill as->as_flags & ASYNC_GETNET)
398 24fb1a39 2025-06-02 kirill netent_from_hostent(ar);
399 24fb1a39 2025-06-02 kirill if (ar->ar_h_errno) {
400 24fb1a39 2025-06-02 kirill ar->ar_hostent = NULL;
401 24fb1a39 2025-06-02 kirill ar->ar_netent = NULL;
403 24fb1a39 2025-06-02 kirill ar->ar_errno = 0;
404 24fb1a39 2025-06-02 kirill return (ASYNC_DONE);
407 24fb1a39 2025-06-02 kirill ar->ar_errno = EOPNOTSUPP;
408 24fb1a39 2025-06-02 kirill ar->ar_h_errno = NETDB_INTERNAL;
409 24fb1a39 2025-06-02 kirill ar->ar_gai_errno = EAI_SYSTEM;
410 24fb1a39 2025-06-02 kirill async_set_state(as, ASR_STATE_HALT);
413 24fb1a39 2025-06-02 kirill goto next;
417 24fb1a39 2025-06-02 kirill * Create a hostent from a numeric address string.
419 24fb1a39 2025-06-02 kirill static struct hostent_ext *
420 24fb1a39 2025-06-02 kirill hostent_from_addr(int family, const char *name, const char *addr)
422 24fb1a39 2025-06-02 kirill struct hostent_ext *h;
424 24fb1a39 2025-06-02 kirill if ((h = hostent_alloc(family)) == NULL)
425 24fb1a39 2025-06-02 kirill return (NULL);
426 24fb1a39 2025-06-02 kirill if (hostent_set_cname(h, name, 0) == -1)
427 24fb1a39 2025-06-02 kirill goto fail;
428 24fb1a39 2025-06-02 kirill if (hostent_add_addr(h, addr, h->h.h_length) == -1)
429 24fb1a39 2025-06-02 kirill goto fail;
430 24fb1a39 2025-06-02 kirill return (h);
433 24fb1a39 2025-06-02 kirill return (NULL);
437 24fb1a39 2025-06-02 kirill * Lookup the first matching entry in the hostfile, either by address or by
438 24fb1a39 2025-06-02 kirill * name depending on reqtype, and build a hostent from the line.
440 24fb1a39 2025-06-02 kirill static struct hostent_ext *
441 24fb1a39 2025-06-02 kirill hostent_file_match(FILE *f, int reqtype, int family, const char *data,
442 24fb1a39 2025-06-02 kirill int datalen)
444 24fb1a39 2025-06-02 kirill char *tokens[MAXTOKEN], addr[16], buf[ASR_BUFSIZ + 1];
445 24fb1a39 2025-06-02 kirill struct hostent_ext *h;
446 24fb1a39 2025-06-02 kirill int n, i;
448 24fb1a39 2025-06-02 kirill for (;;) {
449 24fb1a39 2025-06-02 kirill n = _asr_parse_namedb_line(f, tokens, MAXTOKEN, buf, sizeof(buf));
450 24fb1a39 2025-06-02 kirill if (n == -1) {
451 24fb1a39 2025-06-02 kirill errno = 0; /* ignore errors reading the file */
452 24fb1a39 2025-06-02 kirill return (NULL);
455 24fb1a39 2025-06-02 kirill /* there must be an address and at least one name */
456 24fb1a39 2025-06-02 kirill if (n < 2)
457 24fb1a39 2025-06-02 kirill continue;
459 24fb1a39 2025-06-02 kirill if (reqtype == ASR_GETHOSTBYNAME) {
460 24fb1a39 2025-06-02 kirill for (i = 1; i < n; i++) {
461 24fb1a39 2025-06-02 kirill if (strcasecmp(data, tokens[i]))
462 24fb1a39 2025-06-02 kirill continue;
463 24fb1a39 2025-06-02 kirill if (inet_pton(family, tokens[0], addr) == 1)
464 24fb1a39 2025-06-02 kirill goto found;
467 24fb1a39 2025-06-02 kirill if (inet_pton(family, tokens[0], addr) == 1 &&
468 24fb1a39 2025-06-02 kirill memcmp(addr, data, datalen) == 0)
469 24fb1a39 2025-06-02 kirill goto found;
474 24fb1a39 2025-06-02 kirill if ((h = hostent_alloc(family)) == NULL)
475 24fb1a39 2025-06-02 kirill return (NULL);
476 24fb1a39 2025-06-02 kirill if (hostent_set_cname(h, tokens[1], 0) == -1)
477 24fb1a39 2025-06-02 kirill goto fail;
478 24fb1a39 2025-06-02 kirill for (i = 2; i < n; i ++)
479 24fb1a39 2025-06-02 kirill if (hostent_add_alias(h, tokens[i], 0) == -1)
480 24fb1a39 2025-06-02 kirill goto fail;
481 24fb1a39 2025-06-02 kirill if (hostent_add_addr(h, addr, h->h.h_length) == -1)
482 24fb1a39 2025-06-02 kirill goto fail;
483 24fb1a39 2025-06-02 kirill return (h);
486 24fb1a39 2025-06-02 kirill return (NULL);
490 24fb1a39 2025-06-02 kirill * Fill the hostent from the given DNS packet.
492 24fb1a39 2025-06-02 kirill static struct hostent_ext *
493 24fb1a39 2025-06-02 kirill hostent_from_packet(int reqtype, int family, char *pkt, size_t pktlen)
495 24fb1a39 2025-06-02 kirill struct hostent_ext *h;
496 24fb1a39 2025-06-02 kirill struct asr_unpack p;
497 24fb1a39 2025-06-02 kirill struct asr_dns_header hdr;
498 24fb1a39 2025-06-02 kirill struct asr_dns_query q;
499 24fb1a39 2025-06-02 kirill struct asr_dns_rr rr;
500 24fb1a39 2025-06-02 kirill char dname[MAXDNAME];
502 24fb1a39 2025-06-02 kirill if ((h = hostent_alloc(family)) == NULL)
503 24fb1a39 2025-06-02 kirill return (NULL);
505 24fb1a39 2025-06-02 kirill _asr_unpack_init(&p, pkt, pktlen);
506 24fb1a39 2025-06-02 kirill _asr_unpack_header(&p, &hdr);
507 24fb1a39 2025-06-02 kirill for (; hdr.qdcount; hdr.qdcount--)
508 24fb1a39 2025-06-02 kirill _asr_unpack_query(&p, &q);
509 24fb1a39 2025-06-02 kirill strlcpy(dname, q.q_dname, sizeof(dname));
511 24fb1a39 2025-06-02 kirill for (; hdr.ancount; hdr.ancount--) {
512 24fb1a39 2025-06-02 kirill _asr_unpack_rr(&p, &rr);
513 24fb1a39 2025-06-02 kirill if (rr.rr_class != C_IN)
514 24fb1a39 2025-06-02 kirill continue;
515 24fb1a39 2025-06-02 kirill switch (rr.rr_type) {
517 24fb1a39 2025-06-02 kirill case T_CNAME:
518 24fb1a39 2025-06-02 kirill if (reqtype == ASR_GETHOSTBYNAME) {
519 24fb1a39 2025-06-02 kirill if (hostent_add_alias(h, rr.rr_dname, 1) == -1)
520 24fb1a39 2025-06-02 kirill goto fail;
522 24fb1a39 2025-06-02 kirill if (strcasecmp(rr.rr_dname, dname) == 0)
523 24fb1a39 2025-06-02 kirill strlcpy(dname, rr.rr.cname.cname,
524 24fb1a39 2025-06-02 kirill sizeof(dname));
528 24fb1a39 2025-06-02 kirill case T_PTR:
529 24fb1a39 2025-06-02 kirill if (reqtype != ASR_GETHOSTBYADDR)
531 24fb1a39 2025-06-02 kirill if (strcasecmp(rr.rr_dname, dname) != 0)
532 24fb1a39 2025-06-02 kirill continue;
533 24fb1a39 2025-06-02 kirill if (hostent_set_cname(h, rr.rr.ptr.ptrname, 1) == -1)
534 24fb1a39 2025-06-02 kirill hostent_add_alias(h, rr.rr.ptr.ptrname, 1);
537 24fb1a39 2025-06-02 kirill case T_A:
538 24fb1a39 2025-06-02 kirill if (reqtype != ASR_GETHOSTBYNAME)
540 24fb1a39 2025-06-02 kirill if (family != AF_INET)
542 24fb1a39 2025-06-02 kirill if (hostent_set_cname(h, rr.rr_dname, 1) == -1)
544 24fb1a39 2025-06-02 kirill if (hostent_add_addr(h, &rr.rr.in_a.addr, 4) == -1)
545 24fb1a39 2025-06-02 kirill goto fail;
548 24fb1a39 2025-06-02 kirill case T_AAAA:
549 24fb1a39 2025-06-02 kirill if (reqtype != ASR_GETHOSTBYNAME)
551 24fb1a39 2025-06-02 kirill if (family != AF_INET6)
553 24fb1a39 2025-06-02 kirill if (hostent_set_cname(h, rr.rr_dname, 1) == -1)
555 24fb1a39 2025-06-02 kirill if (hostent_add_addr(h, &rr.rr.in_aaaa.addr6, 16) == -1)
556 24fb1a39 2025-06-02 kirill goto fail;
561 24fb1a39 2025-06-02 kirill return (h);
564 24fb1a39 2025-06-02 kirill return (NULL);
567 24fb1a39 2025-06-02 kirill static struct hostent_ext *
568 24fb1a39 2025-06-02 kirill hostent_alloc(int family)
570 24fb1a39 2025-06-02 kirill struct hostent_ext *h;
571 24fb1a39 2025-06-02 kirill size_t alloc;
573 24fb1a39 2025-06-02 kirill alloc = sizeof(*h) + 1024;
574 24fb1a39 2025-06-02 kirill if ((h = calloc(1, alloc)) == NULL)
575 24fb1a39 2025-06-02 kirill return (NULL);
577 24fb1a39 2025-06-02 kirill h->h.h_addrtype = family;
578 24fb1a39 2025-06-02 kirill h->h.h_length = (family == AF_INET) ? 4 : 16;
579 24fb1a39 2025-06-02 kirill h->h.h_aliases = h->aliases;
580 24fb1a39 2025-06-02 kirill h->h.h_addr_list = h->addrs;
581 24fb1a39 2025-06-02 kirill h->pos = (char *)(h) + sizeof(*h);
582 24fb1a39 2025-06-02 kirill h->end = h->pos + 1024;
584 24fb1a39 2025-06-02 kirill return (h);
587 24fb1a39 2025-06-02 kirill static int
588 24fb1a39 2025-06-02 kirill hostent_set_cname(struct hostent_ext *h, const char *name, int isdname)
590 24fb1a39 2025-06-02 kirill char buf[MAXDNAME];
591 24fb1a39 2025-06-02 kirill size_t n;
593 24fb1a39 2025-06-02 kirill if (h->h.h_name)
594 24fb1a39 2025-06-02 kirill return (-1);
596 24fb1a39 2025-06-02 kirill if (isdname) {
597 24fb1a39 2025-06-02 kirill _asr_strdname(name, buf, sizeof buf);
598 24fb1a39 2025-06-02 kirill buf[strlen(buf) - 1] = '\0';
599 24fb1a39 2025-06-02 kirill if (!res_hnok(buf))
600 24fb1a39 2025-06-02 kirill return (-1);
601 24fb1a39 2025-06-02 kirill name = buf;
604 24fb1a39 2025-06-02 kirill n = strlen(name) + 1;
605 24fb1a39 2025-06-02 kirill if (h->pos + n >= h->end)
606 24fb1a39 2025-06-02 kirill return (-1);
608 24fb1a39 2025-06-02 kirill h->h.h_name = h->pos;
609 24fb1a39 2025-06-02 kirill memmove(h->pos, name, n);
610 24fb1a39 2025-06-02 kirill h->pos += n;
611 24fb1a39 2025-06-02 kirill return (0);
614 24fb1a39 2025-06-02 kirill static int
615 24fb1a39 2025-06-02 kirill hostent_add_alias(struct hostent_ext *h, const char *name, int isdname)
617 24fb1a39 2025-06-02 kirill char buf[MAXDNAME];
618 24fb1a39 2025-06-02 kirill size_t i, n;
620 24fb1a39 2025-06-02 kirill for (i = 0; i < MAXALIASES; i++)
621 24fb1a39 2025-06-02 kirill if (h->aliases[i] == NULL)
623 24fb1a39 2025-06-02 kirill if (i == MAXALIASES)
624 24fb1a39 2025-06-02 kirill return (0);
626 24fb1a39 2025-06-02 kirill if (isdname) {
627 24fb1a39 2025-06-02 kirill _asr_strdname(name, buf, sizeof buf);
628 24fb1a39 2025-06-02 kirill buf[strlen(buf)-1] = '\0';
629 24fb1a39 2025-06-02 kirill if (!res_hnok(buf))
630 24fb1a39 2025-06-02 kirill return (-1);
631 24fb1a39 2025-06-02 kirill name = buf;
634 24fb1a39 2025-06-02 kirill n = strlen(name) + 1;
635 24fb1a39 2025-06-02 kirill if (h->pos + n >= h->end)
636 24fb1a39 2025-06-02 kirill return (0);
638 24fb1a39 2025-06-02 kirill h->aliases[i] = h->pos;
639 24fb1a39 2025-06-02 kirill memmove(h->pos, name, n);
640 24fb1a39 2025-06-02 kirill h->pos += n;
641 24fb1a39 2025-06-02 kirill return (0);
644 24fb1a39 2025-06-02 kirill static int
645 24fb1a39 2025-06-02 kirill hostent_add_addr(struct hostent_ext *h, const void *addr, size_t size)
649 24fb1a39 2025-06-02 kirill for (i = 0; i < MAXADDRS; i++)
650 24fb1a39 2025-06-02 kirill if (h->addrs[i] == NULL)
652 24fb1a39 2025-06-02 kirill if (i == MAXADDRS)
653 24fb1a39 2025-06-02 kirill return (0);
655 24fb1a39 2025-06-02 kirill if (h->pos + size >= h->end)
656 24fb1a39 2025-06-02 kirill return (0);
658 24fb1a39 2025-06-02 kirill h->addrs[i] = h->pos;
659 24fb1a39 2025-06-02 kirill memmove(h->pos, addr, size);
660 24fb1a39 2025-06-02 kirill h->pos += size;
661 24fb1a39 2025-06-02 kirill return (0);
664 24fb1a39 2025-06-02 kirill static void
665 24fb1a39 2025-06-02 kirill netent_from_hostent(struct asr_result *ar)
667 24fb1a39 2025-06-02 kirill struct in_addr *addr;
668 24fb1a39 2025-06-02 kirill struct netent_ext *n;
669 24fb1a39 2025-06-02 kirill struct hostent_ext *h;
670 24fb1a39 2025-06-02 kirill char **na, **ha;
671 24fb1a39 2025-06-02 kirill size_t sz;
673 24fb1a39 2025-06-02 kirill /* Allocate and initialize the output. */
674 24fb1a39 2025-06-02 kirill if ((n = calloc(1, sizeof(*n) + 1024)) == NULL) {
675 24fb1a39 2025-06-02 kirill ar->ar_h_errno = NETDB_INTERNAL;
676 24fb1a39 2025-06-02 kirill ar->ar_errno = errno;
677 24fb1a39 2025-06-02 kirill goto out;
679 24fb1a39 2025-06-02 kirill n->pos = (char *)(n) + sizeof(*n);
680 24fb1a39 2025-06-02 kirill n->end = n->pos + 1024;
681 24fb1a39 2025-06-02 kirill n->n.n_name = n->pos;
682 24fb1a39 2025-06-02 kirill n->n.n_aliases = n->aliases;
684 24fb1a39 2025-06-02 kirill /* Copy the fixed-size data. */
685 24fb1a39 2025-06-02 kirill h = (struct hostent_ext *)ar->ar_hostent;
686 24fb1a39 2025-06-02 kirill addr = (struct in_addr *)h->h.h_addr;
687 24fb1a39 2025-06-02 kirill n->n.n_net = ntohl(addr->s_addr);
688 24fb1a39 2025-06-02 kirill n->n.n_addrtype = h->h.h_addrtype;
690 24fb1a39 2025-06-02 kirill /* Copy the network name. */
691 24fb1a39 2025-06-02 kirill sz = strlen(h->h.h_name) + 1;
692 24fb1a39 2025-06-02 kirill memcpy(n->pos, h->h.h_name, sz);
693 24fb1a39 2025-06-02 kirill n->pos += sz;
696 24fb1a39 2025-06-02 kirill * Copy the aliases.
697 24fb1a39 2025-06-02 kirill * No overflow check is needed because we are merely copying
698 24fb1a39 2025-06-02 kirill * a part of the data from a structure of the same size.
700 24fb1a39 2025-06-02 kirill na = n->aliases;
701 24fb1a39 2025-06-02 kirill for (ha = h->aliases; *ha != NULL; ha++) {
702 24fb1a39 2025-06-02 kirill sz = strlen(*ha) + 1;
703 24fb1a39 2025-06-02 kirill memcpy(n->pos, *ha, sz);
704 24fb1a39 2025-06-02 kirill *na++ = n->pos;
705 24fb1a39 2025-06-02 kirill n->pos += sz;
707 24fb1a39 2025-06-02 kirill *na = NULL;
709 24fb1a39 2025-06-02 kirill /* Handle the return values. */
710 24fb1a39 2025-06-02 kirill ar->ar_netent = &n->n;
712 24fb1a39 2025-06-02 kirill free(ar->ar_hostent);
713 24fb1a39 2025-06-02 kirill ar->ar_hostent = NULL;