1 /*
2 *
3 * Copyright 2016 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include "src/core/lib/iomgr/resolve_address.h"
20
21 #include <net/if.h>
22 #include <string.h>
23 #include <sys/un.h>
24
25 #include <string>
26
27 #include "absl/strings/str_format.h"
28
29 #include <grpc/grpc.h>
30 #include <grpc/support/alloc.h>
31 #include <grpc/support/log.h>
32 #include <grpc/support/sync.h>
33 #include <grpc/support/time.h>
34
35 #include "src/core/ext/filters/client_channel/resolver/dns/dns_resolver_selection.h"
36 #include "src/core/lib/gpr/env.h"
37 #include "src/core/lib/gpr/string.h"
38 #include "src/core/lib/gpr/useful.h"
39 #include "src/core/lib/gprpp/thd.h"
40 #include "src/core/lib/iomgr/executor.h"
41 #include "src/core/lib/iomgr/iomgr.h"
42 #include "test/core/util/cmdline.h"
43 #include "test/core/util/test_config.h"
44
test_deadline(void)45 static gpr_timespec test_deadline(void) {
46 return grpc_timeout_seconds_to_deadline(100);
47 }
48
49 typedef struct args_struct {
50 grpc_core::Thread thd;
51 gpr_event ev;
52 grpc_resolved_addresses* addrs;
53 gpr_atm done_atm;
54 gpr_mu* mu;
55 grpc_pollset* pollset;
56 grpc_pollset_set* pollset_set;
57 } args_struct;
58
do_nothing(void *,grpc_error *)59 static void do_nothing(void* /*arg*/, grpc_error* /*error*/) {}
60
args_init(args_struct * args)61 void args_init(args_struct* args) {
62 gpr_event_init(&args->ev);
63 args->pollset = static_cast<grpc_pollset*>(gpr_zalloc(grpc_pollset_size()));
64 grpc_pollset_init(args->pollset, &args->mu);
65 args->pollset_set = grpc_pollset_set_create();
66 grpc_pollset_set_add_pollset(args->pollset_set, args->pollset);
67 args->addrs = nullptr;
68 }
69
args_finish(args_struct * args)70 void args_finish(args_struct* args) {
71 GPR_ASSERT(gpr_event_wait(&args->ev, test_deadline()));
72 args->thd.Join();
73 // Don't need to explicitly destruct args->thd since
74 // args is actually going to be destructed, not just freed
75 grpc_resolved_addresses_destroy(args->addrs);
76 grpc_pollset_set_del_pollset(args->pollset_set, args->pollset);
77 grpc_pollset_set_destroy(args->pollset_set);
78 grpc_closure do_nothing_cb;
79 GRPC_CLOSURE_INIT(&do_nothing_cb, do_nothing, nullptr,
80 grpc_schedule_on_exec_ctx);
81 grpc_pollset_shutdown(args->pollset, &do_nothing_cb);
82 // exec_ctx needs to be flushed before calling grpc_pollset_destroy()
83 grpc_core::ExecCtx::Get()->Flush();
84 grpc_pollset_destroy(args->pollset);
85 gpr_free(args->pollset);
86 }
87
n_sec_deadline(int seconds)88 static grpc_millis n_sec_deadline(int seconds) {
89 return grpc_timespec_to_millis_round_up(
90 grpc_timeout_seconds_to_deadline(seconds));
91 }
92
actually_poll(void * argsp)93 static void actually_poll(void* argsp) {
94 args_struct* args = static_cast<args_struct*>(argsp);
95 grpc_millis deadline = n_sec_deadline(10);
96 while (true) {
97 grpc_core::ExecCtx exec_ctx;
98 bool done = gpr_atm_acq_load(&args->done_atm) != 0;
99 if (done) {
100 break;
101 }
102 grpc_millis time_left = deadline - grpc_core::ExecCtx::Get()->Now();
103 gpr_log(GPR_DEBUG, "done=%d, time_left=%" PRId64, done, time_left);
104 GPR_ASSERT(time_left >= 0);
105 grpc_pollset_worker* worker = nullptr;
106 gpr_mu_lock(args->mu);
107 GRPC_LOG_IF_ERROR("pollset_work", grpc_pollset_work(args->pollset, &worker,
108 n_sec_deadline(1)));
109 gpr_mu_unlock(args->mu);
110 grpc_core::ExecCtx::Get()->Flush();
111 }
112 gpr_event_set(&args->ev, (void*)1);
113 }
114
poll_pollset_until_request_done(args_struct * args)115 static void poll_pollset_until_request_done(args_struct* args) {
116 gpr_atm_rel_store(&args->done_atm, 0);
117 args->thd = grpc_core::Thread("grpc_poll_pollset", actually_poll, args);
118 args->thd.Start();
119 }
120
must_succeed(void * argsp,grpc_error * err)121 static void must_succeed(void* argsp, grpc_error* err) {
122 args_struct* args = static_cast<args_struct*>(argsp);
123 GPR_ASSERT(err == GRPC_ERROR_NONE);
124 GPR_ASSERT(args->addrs != nullptr);
125 GPR_ASSERT(args->addrs->naddrs > 0);
126 gpr_atm_rel_store(&args->done_atm, 1);
127 gpr_mu_lock(args->mu);
128 GRPC_LOG_IF_ERROR("pollset_kick", grpc_pollset_kick(args->pollset, nullptr));
129 gpr_mu_unlock(args->mu);
130 }
131
must_fail(void * argsp,grpc_error * err)132 static void must_fail(void* argsp, grpc_error* err) {
133 args_struct* args = static_cast<args_struct*>(argsp);
134 GPR_ASSERT(err != GRPC_ERROR_NONE);
135 gpr_atm_rel_store(&args->done_atm, 1);
136 gpr_mu_lock(args->mu);
137 GRPC_LOG_IF_ERROR("pollset_kick", grpc_pollset_kick(args->pollset, nullptr));
138 gpr_mu_unlock(args->mu);
139 }
140
test_unix_socket(void)141 static void test_unix_socket(void) {
142 grpc_core::ExecCtx exec_ctx;
143 args_struct args;
144 args_init(&args);
145 poll_pollset_until_request_done(&args);
146 grpc_resolve_address(
147 "unix:/path/name", nullptr, args.pollset_set,
148 GRPC_CLOSURE_CREATE(must_succeed, &args, grpc_schedule_on_exec_ctx),
149 &args.addrs);
150 args_finish(&args);
151 }
152
test_unix_socket_path_name_too_long(void)153 static void test_unix_socket_path_name_too_long(void) {
154 grpc_core::ExecCtx exec_ctx;
155 args_struct args;
156 args_init(&args);
157 const char prefix[] = "unix:/path/name";
158 size_t path_name_length =
159 GPR_ARRAY_SIZE(((struct sockaddr_un*)nullptr)->sun_path) + 6;
160 char* path_name =
161 static_cast<char*>(gpr_malloc(sizeof(char) * path_name_length));
162 memset(path_name, 'a', path_name_length);
163 memcpy(path_name, prefix, strlen(prefix) - 1);
164 path_name[path_name_length - 1] = '\0';
165
166 poll_pollset_until_request_done(&args);
167 grpc_resolve_address(
168 path_name, nullptr, args.pollset_set,
169 GRPC_CLOSURE_CREATE(must_fail, &args, grpc_schedule_on_exec_ctx),
170 &args.addrs);
171 gpr_free(path_name);
172 args_finish(&args);
173 }
174
resolve_address_must_succeed(const char * target)175 static void resolve_address_must_succeed(const char* target) {
176 grpc_core::ExecCtx exec_ctx;
177 args_struct args;
178 args_init(&args);
179 poll_pollset_until_request_done(&args);
180 grpc_resolve_address(
181 target, "1" /* port number */, args.pollset_set,
182 GRPC_CLOSURE_CREATE(must_succeed, &args, grpc_schedule_on_exec_ctx),
183 &args.addrs);
184 grpc_core::ExecCtx::Get()->Flush();
185 args_finish(&args);
186 }
187
test_named_and_numeric_scope_ids(void)188 static void test_named_and_numeric_scope_ids(void) {
189 char* arbitrary_interface_name = static_cast<char*>(gpr_zalloc(IF_NAMESIZE));
190 int interface_index = 0;
191 // Probe candidate interface index numbers until we find one that the
192 // system recognizes, and then use that for the test.
193 for (size_t i = 1; i < 65536; i++) {
194 if (if_indextoname(i, arbitrary_interface_name) != nullptr) {
195 gpr_log(
196 GPR_DEBUG,
197 "Found interface at index %d named %s. Will use this for the test",
198 (int)i, arbitrary_interface_name);
199 interface_index = (int)i;
200 break;
201 }
202 }
203 GPR_ASSERT(strlen(arbitrary_interface_name) > 0);
204 // Test resolution of an ipv6 address with a named scope ID
205 gpr_log(GPR_DEBUG, "test resolution with a named scope ID");
206 std::string target_with_named_scope_id =
207 absl::StrFormat("fe80::1234%%%s", arbitrary_interface_name);
208 resolve_address_must_succeed(target_with_named_scope_id.c_str());
209 gpr_free(arbitrary_interface_name);
210 // Test resolution of an ipv6 address with a numeric scope ID
211 gpr_log(GPR_DEBUG, "test resolution with a numeric scope ID");
212 std::string target_with_numeric_scope_id =
213 absl::StrFormat("fe80::1234%%%d", interface_index);
214 resolve_address_must_succeed(target_with_numeric_scope_id.c_str());
215 }
216
main(int argc,char ** argv)217 int main(int argc, char** argv) {
218 // First set the resolver type based off of --resolver
219 const char* resolver_type = nullptr;
220 gpr_cmdline* cl = gpr_cmdline_create("resolve address test");
221 gpr_cmdline_add_string(cl, "resolver", "Resolver type (ares or native)",
222 &resolver_type);
223 // In case that there are more than one argument on the command line,
224 // --resolver will always be the first one, so only parse the first argument
225 // (other arguments may be unknown to cl)
226 gpr_cmdline_parse(cl, argc > 2 ? 2 : argc, argv);
227 grpc_core::UniquePtr<char> resolver =
228 GPR_GLOBAL_CONFIG_GET(grpc_dns_resolver);
229 if (strlen(resolver.get()) != 0) {
230 gpr_log(GPR_INFO, "Warning: overriding resolver setting of %s",
231 resolver.get());
232 }
233 if (resolver_type != nullptr && gpr_stricmp(resolver_type, "native") == 0) {
234 GPR_GLOBAL_CONFIG_SET(grpc_dns_resolver, "native");
235 } else if (resolver_type != nullptr &&
236 gpr_stricmp(resolver_type, "ares") == 0) {
237 GPR_GLOBAL_CONFIG_SET(grpc_dns_resolver, "ares");
238 } else {
239 gpr_log(GPR_ERROR, "--resolver_type was not set to ares or native");
240 abort();
241 }
242 grpc::testing::TestEnvironment env(argc, argv);
243 grpc_init();
244
245 {
246 grpc_core::ExecCtx exec_ctx;
247 test_named_and_numeric_scope_ids();
248 // c-ares resolver doesn't support UDS (ability for native DNS resolver
249 // to handle this is only expected to be used by servers, which
250 // unconditionally use the native DNS resolver).
251 grpc_core::UniquePtr<char> resolver =
252 GPR_GLOBAL_CONFIG_GET(grpc_dns_resolver);
253 if (gpr_stricmp(resolver.get(), "native") == 0) {
254 test_unix_socket();
255 test_unix_socket_path_name_too_long();
256 }
257 }
258 gpr_cmdline_destroy(cl);
259
260 grpc_shutdown();
261 return 0;
262 }
263