Debian Codegraph @

crt/aws-c-io/source/posix/host_resolver.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/io/host_resolver.h>
7
8 #include <aws/io/logging.h>
9
10 #include <aws/common/string.h>
11
12 #include <arpa/inet.h>
13 #include <netdb.h>
14 #include <sys/socket.h>
15 #include <sys/types.h>
16
17 int aws_default_dns_resolve(
18 struct aws_allocator *allocator,
19 const struct aws_string *host_name,
20 struct aws_array_list *output_addresses,
21 void *user_data) {
22
23 (void)user_data;
24 struct addrinfo *result = NULL;
25 struct addrinfo *iter = NULL;
26 /* max string length for ipv6. */
27 socklen_t max_len = INET6_ADDRSTRLEN;
28 char address_buffer[max_len];
29
30 const char *hostname_cstr = aws_string_c_str(host_name);
31 AWS_LOGF_DEBUG(AWS_LS_IO_DNS, "static: resolving host %s", hostname_cstr);
32
33 /* Android would prefer NO HINTS IF YOU DON'T MIND, SIR */
34 #if defined(ANDROID)
35 int err_code = getaddrinfo(hostname_cstr, NULL, NULL, &result);
36 #else
37 struct addrinfo hints;
38 AWS_ZERO_STRUCT(hints);
39 hints.ai_family = AF_UNSPEC;
40 hints.ai_socktype = SOCK_STREAM;
41 # if !defined(__OpenBSD__)
42 hints.ai_flags = AI_ALL | AI_V4MAPPED;
43 # endif /* __OpenBSD__ */
44
45 int err_code = getaddrinfo(hostname_cstr, NULL, &hints, &result);
46 #endif
47
48 if (err_code) {
49 AWS_LOGF_ERROR(
50 AWS_LS_IO_DNS, "static: getaddrinfo failed with error_code %d: %s", err_code, gai_strerror(err_code));
51 goto clean_up;
52 }
53
54 for (iter = result; iter != NULL; iter = iter->ai_next) {
55 struct aws_host_address host_address;
56
57 AWS_ZERO_ARRAY(address_buffer);
58
59 if (iter->ai_family == AF_INET6) {
60 host_address.record_type = AWS_ADDRESS_RECORD_TYPE_AAAA;
61 inet_ntop(iter->ai_family, &((struct sockaddr_in6 *)iter->ai_addr)->sin6_addr, address_buffer, max_len);
62 } else {
63 host_address.record_type = AWS_ADDRESS_RECORD_TYPE_A;
64 inet_ntop(iter->ai_family, &((struct sockaddr_in *)iter->ai_addr)->sin_addr, address_buffer, max_len);
65 }
66
67 size_t address_len = strlen(address_buffer);
68 const struct aws_string *address =
69 aws_string_new_from_array(allocator, (const uint8_t *)address_buffer, address_len);
70
71 if (!address) {
72 goto clean_up;
73 }
74
75 const struct aws_string *host_cpy = aws_string_new_from_string(allocator, host_name);
76
77 if (!host_cpy) {
78 aws_string_destroy((void *)address);
79 goto clean_up;
80 }
81
82 AWS_LOGF_DEBUG(AWS_LS_IO_DNS, "static: resolved record: %s", address_buffer);
83
84 host_address.address = address;
85 host_address.weight = 0;
86 host_address.allocator = allocator;
87 host_address.use_count = 0;
88 host_address.connection_failure_count = 0;
89 host_address.host = host_cpy;
90
91 if (aws_array_list_push_back(output_addresses, &host_address)) {
92 aws_host_address_clean_up(&host_address);
93 goto clean_up;
94 }
95 }
96
97 freeaddrinfo(result);
98 return AWS_OP_SUCCESS;
99
100 clean_up:
101 if (result) {
102 freeaddrinfo(result);
103 }
104
105 if (err_code) {
106 switch (err_code) {
107 case EAI_FAIL:
108 case EAI_AGAIN:
109 return aws_raise_error(AWS_IO_DNS_QUERY_FAILED);
110 case EAI_MEMORY:
111 return aws_raise_error(AWS_ERROR_OOM);
112 case EAI_NONAME:
113 case EAI_SERVICE:
114 return aws_raise_error(AWS_IO_DNS_INVALID_NAME);
115 default:
116 return aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE);
117 }
118 }
119
120 return AWS_OP_ERR;
121 }