Bug Summary

File:src/lib/libc/asr/getaddrinfo_async.c
Warning:line 528, column 25
The right operand of '!=' is a garbage value

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name getaddrinfo_async.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 1 -pic-is-pie -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/lib/libc/obj -resource-dir /usr/local/lib/clang/13.0.0 -include namespace.h -I /usr/src/lib/libc/include -I /usr/src/lib/libc/hidden -D __LIBC__ -D APIWARN -D YP -I /usr/src/lib/libc/yp -I /usr/src/lib/libc -I /usr/src/lib/libc/gdtoa -I /usr/src/lib/libc/arch/amd64/gdtoa -D INFNAN_CHECK -D MULTIPLE_THREADS -D NO_FENV_H -D USE_LOCALE -I /usr/src/lib/libc -I /usr/src/lib/libc/citrus -D RESOLVSORT -D FLOATING_POINT -D PRINTF_WIDE_CHAR -D SCANF_WIDE_CHAR -D FUTEX -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir=/usr/src/lib/libc/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c /usr/src/lib/libc/asr/getaddrinfo_async.c
1/* $OpenBSD: getaddrinfo_async.c,v 1.57 2021/01/26 12:27:28 florian Exp $ */
2/*
3 * Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <sys/types.h>
19#include <sys/socket.h>
20#include <sys/uio.h>
21#include <netinet/in.h>
22#include <arpa/nameser.h>
23#include <net/if.h>
24#include <netdb.h>
25
26#include <asr.h>
27#include <errno(*__errno()).h>
28#include <ifaddrs.h>
29#include <resolv.h>
30#include <stdlib.h>
31#include <string.h>
32#include <unistd.h>
33#include <limits.h>
34
35#include "asr_private.h"
36
37struct match {
38 int family;
39 int socktype;
40 int protocol;
41};
42
43static int getaddrinfo_async_run(struct asr_query *, struct asr_result *);
44static int get_port(const char *, const char *, int);
45static int iter_family(struct asr_query *, int);
46static int addrinfo_add(struct asr_query *, const struct sockaddr *, const char *);
47static int addrinfo_from_file(struct asr_query *, int, FILE *);
48static int addrinfo_from_pkt(struct asr_query *, char *, size_t);
49static int addrconfig_setup(struct asr_query *);
50
51static const struct match matches[] = {
52 { PF_INET2, SOCK_DGRAM2, IPPROTO_UDP17 },
53 { PF_INET2, SOCK_STREAM1, IPPROTO_TCP6 },
54 { PF_INET2, SOCK_RAW3, 0 },
55 { PF_INET624, SOCK_DGRAM2, IPPROTO_UDP17 },
56 { PF_INET624, SOCK_STREAM1, IPPROTO_TCP6 },
57 { PF_INET624, SOCK_RAW3, 0 },
58 { -1, 0, 0, },
59};
60
61#define MATCH_FAMILY(a, b)((a) == matches[(b)].family || (a) == 0) ((a) == matches[(b)].family || (a) == PF_UNSPEC0)
62#define MATCH_PROTO(a, b)((a) == matches[(b)].protocol || (a) == 0 || matches[(b)].protocol
== 0)
((a) == matches[(b)].protocol || (a) == 0 || matches[(b)].protocol == 0)
63/* Do not match SOCK_RAW unless explicitly specified */
64#define MATCH_SOCKTYPE(a, b)((a) == matches[(b)].socktype || ((a) == 0 && matches
[(b)].socktype != 3))
((a) == matches[(b)].socktype || ((a) == 0 && \
65 matches[(b)].socktype != SOCK_RAW3))
66
67enum {
68 DOM_INIT,
69 DOM_DOMAIN,
70 DOM_DONE
71};
72
73struct asr_query *
74getaddrinfo_async(const char *hostname, const char *servname,
75 const struct addrinfo *hints, void *asr)
76{
77 struct asr_ctx *ac;
78 struct asr_query *as;
79
80 if (hints == NULL((void *)0) || (hints->ai_flags & AI_NUMERICHOST4) == 0)
81 ac = _asr_use_resolver(asr);
82 else
83 ac = _asr_no_resolver();
84 if ((as = _asr_async_new(ac, ASR_GETADDRINFO)) == NULL((void *)0))
85 goto abort; /* errno set */
86 as->as_run = getaddrinfo_async_run;
87
88 if (hostname) {
89 if ((as->as.ai.hostname = strdup(hostname)) == NULL((void *)0))
90 goto abort; /* errno set */
91 }
92 if (servname && (as->as.ai.servname = strdup(servname)) == NULL((void *)0))
93 goto abort; /* errno set */
94 if (hints)
95 memmove(&as->as.ai.hints, hints, sizeof *hints);
96 else {
97 memset(&as->as.ai.hints, 0, sizeof as->as.ai.hints);
98 as->as.ai.hints.ai_family = PF_UNSPEC0;
99 as->as.ai.hints.ai_flags = AI_ADDRCONFIG64;
100 }
101
102 _asr_ctx_unref(ac);
103 return (as);
104 abort:
105 if (as)
106 _asr_async_free(as);
107 _asr_ctx_unref(ac);
108 return (NULL((void *)0));
109}
110DEF_WEAK(getaddrinfo_async)__asm__(".weak " "getaddrinfo_async" " ; " "getaddrinfo_async"
" = " "_libc_getaddrinfo_async")
;
111
112static int
113getaddrinfo_async_run(struct asr_query *as, struct asr_result *ar)
114{
115 char fqdn[MAXDNAME1025];
116 const char *str;
117 struct addrinfo *ai;
118 int i, family, r;
119 FILE *f;
120 union {
121 struct sockaddr sa;
122 struct sockaddr_in sain;
123 struct sockaddr_in6 sain6;
124 } sa;
125
126 next:
127 switch (as->as_state) {
1
Control jumps to 'case ASR_STATE_SAME_DB:' at line 343
128
129 case ASR_STATE_INIT:
130
131 /*
132 * First, make sure the parameters are valid.
133 */
134
135 as->as_count = 0;
136
137 if (as->as.ai.hostname == NULL((void *)0) &&
138 as->as.ai.servname == NULL((void *)0)) {
139 ar->ar_gai_errno = EAI_NONAME-2;
140 async_set_state(as, ASR_STATE_HALT)do { ; (as)->as_state = (ASR_STATE_HALT); } while (0);
141 break;
142 }
143
144 if (as->as.ai.hostname && as->as.ai.hostname[0] == '\0') {
145 ar->ar_gai_errno = EAI_NODATA-5;
146 async_set_state(as, ASR_STATE_HALT)do { ; (as)->as_state = (ASR_STATE_HALT); } while (0);
147 break;
148 }
149
150 ai = &as->as.ai.hints;
151
152 if (ai->ai_addrlen ||
153 ai->ai_canonname ||
154 ai->ai_addr ||
155 ai->ai_next) {
156 ar->ar_gai_errno = EAI_BADHINTS-12;
157 async_set_state(as, ASR_STATE_HALT)do { ; (as)->as_state = (ASR_STATE_HALT); } while (0);
158 break;
159 }
160
161 if (ai->ai_flags & ~AI_MASK(1 | 2 | 4 | 16 | 32 | 64) ||
162 (ai->ai_flags & AI_CANONNAME2 && ai->ai_flags & AI_FQDN32)) {
163 ar->ar_gai_errno = EAI_BADFLAGS-1;
164 async_set_state(as, ASR_STATE_HALT)do { ; (as)->as_state = (ASR_STATE_HALT); } while (0);
165 break;
166 }
167
168 if (ai->ai_family != PF_UNSPEC0 &&
169 ai->ai_family != PF_INET2 &&
170 ai->ai_family != PF_INET624) {
171 ar->ar_gai_errno = EAI_FAMILY-6;
172 async_set_state(as, ASR_STATE_HALT)do { ; (as)->as_state = (ASR_STATE_HALT); } while (0);
173 break;
174 }
175
176 if (ai->ai_socktype &&
177 ai->ai_socktype != SOCK_DGRAM2 &&
178 ai->ai_socktype != SOCK_STREAM1 &&
179 ai->ai_socktype != SOCK_RAW3) {
180 ar->ar_gai_errno = EAI_SOCKTYPE-7;
181 async_set_state(as, ASR_STATE_HALT)do { ; (as)->as_state = (ASR_STATE_HALT); } while (0);
182 break;
183 }
184
185 if (ai->ai_socktype == SOCK_RAW3 &&
186 get_port(as->as.ai.servname, NULL((void *)0), 1) != 0) {
187 ar->ar_gai_errno = EAI_SERVICE-8;
188 async_set_state(as, ASR_STATE_HALT)do { ; (as)->as_state = (ASR_STATE_HALT); } while (0);
189 break;
190 }
191
192 /* Restrict result set to configured address families */
193 if (ai->ai_flags & AI_ADDRCONFIG64) {
194 if (addrconfig_setup(as) == -1) {
195 ar->ar_errno = errno(*__errno());
196 ar->ar_gai_errno = EAI_SYSTEM-11;
197 async_set_state(as, ASR_STATE_HALT)do { ; (as)->as_state = (ASR_STATE_HALT); } while (0);
198 break;
199 }
200 }
201
202 /* Make sure there is at least a valid combination */
203 for (i = 0; matches[i].family != -1; i++)
204 if (MATCH_FAMILY(ai->ai_family, i)((ai->ai_family) == matches[(i)].family || (ai->ai_family
) == 0)
&&
205 MATCH_SOCKTYPE(ai->ai_socktype, i)((ai->ai_socktype) == matches[(i)].socktype || ((ai->ai_socktype
) == 0 && matches[(i)].socktype != 3))
&&
206 MATCH_PROTO(ai->ai_protocol, i)((ai->ai_protocol) == matches[(i)].protocol || (ai->ai_protocol
) == 0 || matches[(i)].protocol == 0)
)
207 break;
208 if (matches[i].family == -1) {
209 ar->ar_gai_errno = EAI_BADHINTS-12;
210 async_set_state(as, ASR_STATE_HALT)do { ; (as)->as_state = (ASR_STATE_HALT); } while (0);
211 break;
212 }
213
214 if (ai->ai_protocol == 0 || ai->ai_protocol == IPPROTO_UDP17)
215 as->as.ai.port_udp = get_port(as->as.ai.servname, "udp",
216 as->as.ai.hints.ai_flags & AI_NUMERICSERV16);
217 if (ai->ai_protocol == 0 || ai->ai_protocol == IPPROTO_TCP6)
218 as->as.ai.port_tcp = get_port(as->as.ai.servname, "tcp",
219 as->as.ai.hints.ai_flags & AI_NUMERICSERV16);
220 if (as->as.ai.port_tcp == -2 || as->as.ai.port_udp == -2 ||
221 (as->as.ai.port_tcp == -1 && as->as.ai.port_udp == -1) ||
222 (ai->ai_protocol && (as->as.ai.port_udp == -1 ||
223 as->as.ai.port_tcp == -1))) {
224 ar->ar_gai_errno = EAI_SERVICE-8;
225 async_set_state(as, ASR_STATE_HALT)do { ; (as)->as_state = (ASR_STATE_HALT); } while (0);
226 break;
227 }
228
229 ar->ar_gai_errno = 0;
230
231 /* If hostname is NULL, use local address */
232 if (as->as.ai.hostname == NULL((void *)0)) {
233 for (family = iter_family(as, 1);
234 family != -1;
235 family = iter_family(as, 0)) {
236 /*
237 * We could use statically built sockaddrs for
238 * those, rather than parsing over and over.
239 */
240 if (family == PF_INET2)
241 str = (ai->ai_flags & AI_PASSIVE1) ? \
242 "0.0.0.0" : "127.0.0.1";
243 else /* PF_INET6 */
244 str = (ai->ai_flags & AI_PASSIVE1) ? \
245 "::" : "::1";
246 /* This can't fail */
247 _asr_sockaddr_from_str(&sa.sa, family, str);
248 if ((r = addrinfo_add(as, &sa.sa, NULL((void *)0)))) {
249 ar->ar_gai_errno = r;
250 break;
251 }
252 }
253 if (ar->ar_gai_errno == 0 && as->as_count == 0) {
254 ar->ar_gai_errno = EAI_NODATA-5;
255 }
256 async_set_state(as, ASR_STATE_HALT)do { ; (as)->as_state = (ASR_STATE_HALT); } while (0);
257 break;
258 }
259
260 /* Try numeric addresses first */
261 for (family = iter_family(as, 1);
262 family != -1;
263 family = iter_family(as, 0)) {
264
265 if (_asr_sockaddr_from_str(&sa.sa, family,
266 as->as.ai.hostname) == -1)
267 continue;
268
269 if ((r = addrinfo_add(as, &sa.sa, NULL((void *)0))))
270 ar->ar_gai_errno = r;
271 break;
272 }
273 if (ar->ar_gai_errno || as->as_count) {
274 async_set_state(as, ASR_STATE_HALT)do { ; (as)->as_state = (ASR_STATE_HALT); } while (0);
275 break;
276 }
277
278 if (ai->ai_flags & AI_NUMERICHOST4) {
279 ar->ar_gai_errno = EAI_NONAME-2;
280 async_set_state(as, ASR_STATE_HALT)do { ; (as)->as_state = (ASR_STATE_HALT); } while (0);
281 break;
282 }
283
284 async_set_state(as, ASR_STATE_NEXT_DB)do { ; (as)->as_state = (ASR_STATE_NEXT_DB); } while (0);
285 break;
286
287 case ASR_STATE_NEXT_DB:
288 if (_asr_iter_db(as) == -1) {
289 async_set_state(as, ASR_STATE_NOT_FOUND)do { ; (as)->as_state = (ASR_STATE_NOT_FOUND); } while (0);
290 break;
291 }
292 as->as_family_idx = 0;
293 async_set_state(as, ASR_STATE_SAME_DB)do { ; (as)->as_state = (ASR_STATE_SAME_DB); } while (0);
294 break;
295
296 case ASR_STATE_NEXT_FAMILY:
297 as->as_family_idx += 1;
298 if (as->as.ai.hints.ai_family != AF_UNSPEC0 ||
299 AS_FAMILY(as)((as)->as_ctx->ac_family[(as)->as_family_idx]) == -1) {
300 /* The family was specified, or we have tried all
301 * families with this DB.
302 */
303 if (as->as_count) {
304 ar->ar_gai_errno = 0;
305 async_set_state(as, ASR_STATE_HALT)do { ; (as)->as_state = (ASR_STATE_HALT); } while (0);
306 } else
307 async_set_state(as, ASR_STATE_NEXT_DOMAIN)do { ; (as)->as_state = (ASR_STATE_NEXT_DOMAIN); } while (
0)
;
308 break;
309 }
310 async_set_state(as, ASR_STATE_SAME_DB)do { ; (as)->as_state = (ASR_STATE_SAME_DB); } while (0);
311 break;
312
313 case ASR_STATE_NEXT_DOMAIN:
314 /* domain search is only for dns */
315 if (AS_DB(as)((as)->as_ctx->ac_db[(as)->as_db_idx - 1]) != ASR_DB_DNS'b') {
316 async_set_state(as, ASR_STATE_NEXT_DB)do { ; (as)->as_state = (ASR_STATE_NEXT_DB); } while (0);
317 break;
318 }
319 as->as_family_idx = 0;
320
321 free(as->as.ai.fqdn);
322 as->as.ai.fqdn = NULL((void *)0);
323 r = _asr_iter_domain(as, as->as.ai.hostname, fqdn, sizeof(fqdn));
324 if (r == -1) {
325 async_set_state(as, ASR_STATE_NEXT_DB)do { ; (as)->as_state = (ASR_STATE_NEXT_DB); } while (0);
326 break;
327 }
328 if (r == 0) {
329 ar->ar_gai_errno = EAI_FAIL-4;
330 async_set_state(as, ASR_STATE_HALT)do { ; (as)->as_state = (ASR_STATE_HALT); } while (0);
331 break;
332 }
333 as->as.ai.fqdn = strdup(fqdn);
334 if (as->as.ai.fqdn == NULL((void *)0)) {
335 ar->ar_gai_errno = EAI_MEMORY-10;
336 async_set_state(as, ASR_STATE_HALT)do { ; (as)->as_state = (ASR_STATE_HALT); } while (0);
337 break;
338 }
339
340 async_set_state(as, ASR_STATE_SAME_DB)do { ; (as)->as_state = (ASR_STATE_SAME_DB); } while (0);
341 break;
342
343 case ASR_STATE_SAME_DB:
344 /* query the current DB again */
345 switch (AS_DB(as)((as)->as_ctx->ac_db[(as)->as_db_idx - 1])) {
2
Control jumps to 'case 102:' at line 383
346 case ASR_DB_DNS'b':
347 if (as->as.ai.fqdn == NULL((void *)0)) {
348 /* First try, initialize domain iteration */
349 as->as_dom_flags = 0;
350 as->as_dom_step = DOM_INIT;
351 async_set_state(as, ASR_STATE_NEXT_DOMAIN)do { ; (as)->as_state = (ASR_STATE_NEXT_DOMAIN); } while (
0)
;
352 break;
353 }
354
355 family = (as->as.ai.hints.ai_family == AF_UNSPEC0) ?
356 AS_FAMILY(as)((as)->as_ctx->ac_family[(as)->as_family_idx]) : as->as.ai.hints.ai_family;
357
358 if (family == AF_INET2 &&
359 as->as_flags & ASYNC_NO_INET0x00010000) {
360 async_set_state(as, ASR_STATE_NEXT_FAMILY)do { ; (as)->as_state = (ASR_STATE_NEXT_FAMILY); } while (
0)
;
361 break;
362 } else if (family == AF_INET624 &&
363 as->as_flags & ASYNC_NO_INET60x00020000) {
364 async_set_state(as, ASR_STATE_NEXT_FAMILY)do { ; (as)->as_state = (ASR_STATE_NEXT_FAMILY); } while (
0)
;
365 break;
366 }
367
368 as->as_subq = _res_query_async_ctx(as->as.ai.fqdn,
369 C_IN1, (family == AF_INET624) ? T_AAAA28 : T_A1,
370 as->as_ctx);
371
372 if (as->as_subq == NULL((void *)0)) {
373 if (errno(*__errno()) == ENOMEM12)
374 ar->ar_gai_errno = EAI_MEMORY-10;
375 else
376 ar->ar_gai_errno = EAI_FAIL-4;
377 async_set_state(as, ASR_STATE_HALT)do { ; (as)->as_state = (ASR_STATE_HALT); } while (0);
378 break;
379 }
380 async_set_state(as, ASR_STATE_SUBQUERY)do { ; (as)->as_state = (ASR_STATE_SUBQUERY); } while (0);
381 break;
382
383 case ASR_DB_FILE'f':
384 f = fopen(_PATH_HOSTS"/etc/hosts", "re");
385 if (f == NULL((void *)0)) {
3
Assuming 'f' is not equal to NULL
4
Taking false branch
386 async_set_state(as, ASR_STATE_NEXT_DB)do { ; (as)->as_state = (ASR_STATE_NEXT_DB); } while (0);
387 break;
388 }
389 family = (as->as.ai.hints.ai_family == AF_UNSPEC0) ?
5
Assuming field 'ai_family' is not equal to AF_UNSPEC
6
'?' condition is false
390 AS_FAMILY(as)((as)->as_ctx->ac_family[(as)->as_family_idx]) : as->as.ai.hints.ai_family;
391
392 r = addrinfo_from_file(as, family, f);
7
Calling 'addrinfo_from_file'
393 if (r == -1) {
394 if (errno(*__errno()) == ENOMEM12)
395 ar->ar_gai_errno = EAI_MEMORY-10;
396 else
397 ar->ar_gai_errno = EAI_FAIL-4;
398 async_set_state(as, ASR_STATE_HALT)do { ; (as)->as_state = (ASR_STATE_HALT); } while (0);
399 } else
400 async_set_state(as, ASR_STATE_NEXT_FAMILY)do { ; (as)->as_state = (ASR_STATE_NEXT_FAMILY); } while (
0)
;
401 fclose(f);
402 break;
403
404 default:
405 async_set_state(as, ASR_STATE_NEXT_DB)do { ; (as)->as_state = (ASR_STATE_NEXT_DB); } while (0);
406 }
407 break;
408
409 case ASR_STATE_SUBQUERY:
410 if ((r = asr_run(as->as_subq, ar)) == ASYNC_COND0)
411 return (ASYNC_COND0);
412
413 as->as_subq = NULL((void *)0);
414
415 if (ar->ar_datalen == -1) {
416 async_set_state(as, ASR_STATE_NEXT_FAMILY)do { ; (as)->as_state = (ASR_STATE_NEXT_FAMILY); } while (
0)
;
417 break;
418 }
419
420 r = addrinfo_from_pkt(as, ar->ar_data, ar->ar_datalen);
421 if (r == -1) {
422 if (errno(*__errno()) == ENOMEM12)
423 ar->ar_gai_errno = EAI_MEMORY-10;
424 else
425 ar->ar_gai_errno = EAI_FAIL-4;
426 async_set_state(as, ASR_STATE_HALT)do { ; (as)->as_state = (ASR_STATE_HALT); } while (0);
427 } else
428 async_set_state(as, ASR_STATE_NEXT_FAMILY)do { ; (as)->as_state = (ASR_STATE_NEXT_FAMILY); } while (
0)
;
429 free(ar->ar_data);
430 break;
431
432 case ASR_STATE_NOT_FOUND:
433 /* No result found. Maybe we can try again. */
434 if (as->as_flags & ASYNC_AGAIN0x00000200)
435 ar->ar_gai_errno = EAI_AGAIN-3;
436 else
437 ar->ar_gai_errno = EAI_NODATA-5;
438 async_set_state(as, ASR_STATE_HALT)do { ; (as)->as_state = (ASR_STATE_HALT); } while (0);
439 break;
440
441 case ASR_STATE_HALT:
442 if (ar->ar_gai_errno == 0) {
443 ar->ar_count = as->as_count;
444 ar->ar_addrinfo = as->as.ai.aifirst;
445 as->as.ai.aifirst = NULL((void *)0);
446 } else {
447 ar->ar_count = 0;
448 ar->ar_addrinfo = NULL((void *)0);
449 }
450 return (ASYNC_DONE1);
451
452 default:
453 ar->ar_errno = EOPNOTSUPP45;
454 ar->ar_gai_errno = EAI_SYSTEM-11;
455 async_set_state(as, ASR_STATE_HALT)do { ; (as)->as_state = (ASR_STATE_HALT); } while (0);
456 break;
457 }
458 goto next;
459}
460
461/*
462 * Retreive the port number for the service name "servname" and
463 * the protocol "proto".
464 */
465static int
466get_port(const char *servname, const char *proto, int numonly)
467{
468 struct servent se;
469 struct servent_data sed;
470 int port;
471 const char *e;
472
473 if (servname == NULL((void *)0))
474 return (0);
475
476 e = NULL((void *)0);
477 port = strtonum(servname, 0, USHRT_MAX(32767 *2 +1), &e);
478 if (e == NULL((void *)0))
479 return (port);
480 if (errno(*__errno()) == ERANGE34)
481 return (-2); /* invalid */
482 if (numonly)
483 return (-2);
484
485 port = -1;
486 memset(&sed, 0, sizeof(sed));
487 if (getservbyname_r(servname, proto, &se, &sed) != -1)
488 port = ntohs(se.s_port)(__uint16_t)(__builtin_constant_p(se.s_port) ? (__uint16_t)((
(__uint16_t)(se.s_port) & 0xffU) << 8 | ((__uint16_t
)(se.s_port) & 0xff00U) >> 8) : __swap16md(se.s_port
))
;
489 endservent_r(&sed);
490
491 return (port);
492}
493
494/*
495 * Iterate over the address families that are to be queried. Use the
496 * list on the async context, unless a specific family was given in hints.
497 */
498static int
499iter_family(struct asr_query *as, int first)
500{
501 if (first) {
502 as->as_family_idx = 0;
503 if (as->as.ai.hints.ai_family != PF_UNSPEC0)
504 return as->as.ai.hints.ai_family;
505 return AS_FAMILY(as)((as)->as_ctx->ac_family[(as)->as_family_idx]);
506 }
507
508 if (as->as.ai.hints.ai_family != PF_UNSPEC0)
509 return (-1);
510
511 as->as_family_idx++;
512
513 return AS_FAMILY(as)((as)->as_ctx->ac_family[(as)->as_family_idx]);
514}
515
516/*
517 * Use the sockaddr at "sa" to extend the result list on the "as" context,
518 * with the specified canonical name "cname". This function adds one
519 * entry per protocol/socktype match.
520 */
521static int
522addrinfo_add(struct asr_query *as, const struct sockaddr *sa, const char *cname)
523{
524 struct addrinfo *ai;
525 int i, port, proto;
526
527 for (i = 0; matches[i].family != -1; i++) {
18
Assuming the condition is true
19
Loop condition is true. Entering loop body
528 if (matches[i].family != sa->sa_family ||
20
The right operand of '!=' is a garbage value
529 !MATCH_SOCKTYPE(as->as.ai.hints.ai_socktype, i)((as->as.ai.hints.ai_socktype) == matches[(i)].socktype ||
((as->as.ai.hints.ai_socktype) == 0 && matches[(i
)].socktype != 3))
||
530 !MATCH_PROTO(as->as.ai.hints.ai_protocol, i)((as->as.ai.hints.ai_protocol) == matches[(i)].protocol ||
(as->as.ai.hints.ai_protocol) == 0 || matches[(i)].protocol
== 0)
)
531 continue;
532
533 proto = as->as.ai.hints.ai_protocol;
534 if (!proto)
535 proto = matches[i].protocol;
536
537 if (proto == IPPROTO_TCP6)
538 port = as->as.ai.port_tcp;
539 else if (proto == IPPROTO_UDP17)
540 port = as->as.ai.port_udp;
541 else
542 port = 0;
543
544 /* servname specified, but not defined for this protocol */
545 if (port == -1)
546 continue;
547
548 ai = calloc(1, sizeof(*ai) + sa->sa_len);
549 if (ai == NULL((void *)0))
550 return (EAI_MEMORY-10);
551 ai->ai_family = sa->sa_family;
552 ai->ai_socktype = matches[i].socktype;
553 ai->ai_protocol = proto;
554 ai->ai_flags = as->as.ai.hints.ai_flags;
555 ai->ai_addrlen = sa->sa_len;
556 ai->ai_addr = (void *)(ai + 1);
557 if (cname &&
558 as->as.ai.hints.ai_flags & (AI_CANONNAME2 | AI_FQDN32)) {
559 if ((ai->ai_canonname = strdup(cname)) == NULL((void *)0)) {
560 free(ai);
561 return (EAI_MEMORY-10);
562 }
563 }
564 memmove(ai->ai_addr, sa, sa->sa_len);
565 if (sa->sa_family == PF_INET2)
566 ((struct sockaddr_in *)ai->ai_addr)->sin_port =
567 htons(port)(__uint16_t)(__builtin_constant_p(port) ? (__uint16_t)(((__uint16_t
)(port) & 0xffU) << 8 | ((__uint16_t)(port) & 0xff00U
) >> 8) : __swap16md(port))
;
568 else if (sa->sa_family == PF_INET624)
569 ((struct sockaddr_in6 *)ai->ai_addr)->sin6_port =
570 htons(port)(__uint16_t)(__builtin_constant_p(port) ? (__uint16_t)(((__uint16_t
)(port) & 0xffU) << 8 | ((__uint16_t)(port) & 0xff00U
) >> 8) : __swap16md(port))
;
571
572 if (as->as.ai.aifirst == NULL((void *)0))
573 as->as.ai.aifirst = ai;
574 if (as->as.ai.ailast)
575 as->as.ai.ailast->ai_next = ai;
576 as->as.ai.ailast = ai;
577 as->as_count += 1;
578 }
579
580 return (0);
581}
582
583static int
584addrinfo_from_file(struct asr_query *as, int family, FILE *f)
585{
586 char *tokens[MAXTOKEN10], *c, buf[BUFSIZ1024 + 1];
587 int n, i;
588 union {
589 struct sockaddr sa;
590 struct sockaddr_in sain;
591 struct sockaddr_in6 sain6;
592 } u;
593
594 for (;;) {
8
Loop condition is true. Entering loop body
595 n = _asr_parse_namedb_line(f, tokens, MAXTOKEN10, buf, sizeof(buf));
596 if (n == -1)
9
Assuming the condition is false
10
Taking false branch
597 break; /* ignore errors reading the file */
598
599 for (i = 1; i < n; i++) {
11
Assuming 'i' is >= 'n'
12
Loop condition is false. Execution continues on line 606
600 if (strcasecmp(as->as.ai.hostname, tokens[i]))
601 continue;
602 if (_asr_sockaddr_from_str(&u.sa, family, tokens[0]) == -1)
603 continue;
604 break;
605 }
606 if (i == n)
13
Assuming 'i' is not equal to 'n'
14
Taking false branch
607 continue;
608
609 if (as->as.ai.hints.ai_flags & (AI_CANONNAME2 | AI_FQDN32))
15
Assuming the condition is false
16
Taking false branch
610 c = tokens[1];
611 else
612 c = NULL((void *)0);
613
614 if (addrinfo_add(as, &u.sa, c))
17
Calling 'addrinfo_add'
615 return (-1); /* errno set */
616 }
617 return (0);
618}
619
620static int
621addrinfo_from_pkt(struct asr_query *as, char *pkt, size_t pktlen)
622{
623 struct asr_unpack p;
624 struct asr_dns_header h;
625 struct asr_dns_query q;
626 struct asr_dns_rr rr;
627 int i;
628 union {
629 struct sockaddr sa;
630 struct sockaddr_in sain;
631 struct sockaddr_in6 sain6;
632 } u;
633 char buf[MAXDNAME1025], *c;
634
635 _asr_unpack_init(&p, pkt, pktlen);
636 _asr_unpack_header(&p, &h);
637 for (; h.qdcount; h.qdcount--)
638 _asr_unpack_query(&p, &q);
639
640 for (i = 0; i < h.ancount; i++) {
641 _asr_unpack_rr(&p, &rr);
642 if (rr.rr_type != q.q_type ||
643 rr.rr_class != q.q_class)
644 continue;
645
646 memset(&u, 0, sizeof u);
647 if (rr.rr_type == T_A1) {
648 u.sain.sin_len = sizeof u.sain;
649 u.sain.sin_family = AF_INET2;
650 u.sain.sin_addr = rr.rr.in_a.addr;
651 u.sain.sin_port = 0;
652 } else if (rr.rr_type == T_AAAA28) {
653 u.sain6.sin6_len = sizeof u.sain6;
654 u.sain6.sin6_family = AF_INET624;
655 u.sain6.sin6_addr = rr.rr.in_aaaa.addr6;
656 u.sain6.sin6_port = 0;
657 } else
658 continue;
659
660 if (as->as.ai.hints.ai_flags & AI_CANONNAME2) {
661 _asr_strdname(rr.rr_dname, buf, sizeof buf);
662 buf[strlen(buf) - 1] = '\0';
663 c = res_hnok__res_hnok(buf) ? buf : NULL((void *)0);
664 } else if (as->as.ai.hints.ai_flags & AI_FQDN32)
665 c = as->as.ai.fqdn;
666 else
667 c = NULL((void *)0);
668
669 if (addrinfo_add(as, &u.sa, c))
670 return (-1); /* errno set */
671 }
672 return (0);
673}
674
675static int
676addrconfig_setup(struct asr_query *as)
677{
678 struct ifaddrs *ifa, *ifa0;
679 struct if_data *ifa_data;
680 struct sockaddr_in *sinp;
681 struct sockaddr_in6 *sin6p;
682 int rtable, ifa_rtable = -1;
683
684 if (getifaddrs(&ifa0) == -1)
685 return (-1);
686
687 rtable = getrtable();
688
689 as->as_flags |= ASYNC_NO_INET0x00010000 | ASYNC_NO_INET60x00020000;
690
691 for (ifa = ifa0; ifa != NULL((void *)0); ifa = ifa->ifa_next) {
692 if (ifa->ifa_addr == NULL((void *)0))
693 continue;
694
695 switch (ifa->ifa_addr->sa_family) {
696 case PF_LINK18:
697 /* AF_LINK comes before inet / inet6 on an interface */
698 ifa_data = (struct if_data *)ifa->ifa_data;
699 ifa_rtable = ifa_data->ifi_rdomain;
700 break;
701 case PF_INET2:
702 if (ifa_rtable != rtable)
703 continue;
704
705 sinp = (struct sockaddr_in *)ifa->ifa_addr;
706
707 if (sinp->sin_addr.s_addr == htonl(INADDR_LOOPBACK)(__uint32_t)(__builtin_constant_p(((u_int32_t)(0x7f000001))) ?
(__uint32_t)(((__uint32_t)(((u_int32_t)(0x7f000001))) & 0xff
) << 24 | ((__uint32_t)(((u_int32_t)(0x7f000001))) &
0xff00) << 8 | ((__uint32_t)(((u_int32_t)(0x7f000001))
) & 0xff0000) >> 8 | ((__uint32_t)(((u_int32_t)(0x7f000001
))) & 0xff000000) >> 24) : __swap32md(((u_int32_t)(
0x7f000001))))
)
708 continue;
709
710 as->as_flags &= ~ASYNC_NO_INET0x00010000;
711 break;
712 case PF_INET624:
713 if (ifa_rtable != rtable)
714 continue;
715
716 sin6p = (struct sockaddr_in6 *)ifa->ifa_addr;
717
718 if (IN6_IS_ADDR_LOOPBACK(&sin6p->sin6_addr)((*(const u_int32_t *)(const void *)(&(&sin6p->sin6_addr
)->__u6_addr.__u6_addr8[0]) == 0) && (*(const u_int32_t
*)(const void *)(&(&sin6p->sin6_addr)->__u6_addr
.__u6_addr8[4]) == 0) && (*(const u_int32_t *)(const void
*)(&(&sin6p->sin6_addr)->__u6_addr.__u6_addr8[
8]) == 0) && (*(const u_int32_t *)(const void *)(&
(&sin6p->sin6_addr)->__u6_addr.__u6_addr8[12]) == (
__uint32_t)(__builtin_constant_p(1) ? (__uint32_t)(((__uint32_t
)(1) & 0xff) << 24 | ((__uint32_t)(1) & 0xff00)
<< 8 | ((__uint32_t)(1) & 0xff0000) >> 8 | (
(__uint32_t)(1) & 0xff000000) >> 24) : __swap32md(1
))))
)
719 continue;
720
721 if (IN6_IS_ADDR_LINKLOCAL(&sin6p->sin6_addr)(((&sin6p->sin6_addr)->__u6_addr.__u6_addr8[0] == 0xfe
) && (((&sin6p->sin6_addr)->__u6_addr.__u6_addr8
[1] & 0xc0) == 0x80))
)
722 continue;
723
724 as->as_flags &= ~ASYNC_NO_INET60x00020000;
725 break;
726 }
727 }
728
729 freeifaddrs(ifa0);
730
731 return (0);
732}