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 
resolve_address_must_succeed(const char * target)141 static void resolve_address_must_succeed(const char* target) {
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       target, "1" /* port number */, args.pollset_set,
148       GRPC_CLOSURE_CREATE(must_succeed, &args, grpc_schedule_on_exec_ctx),
149       &args.addrs);
150   grpc_core::ExecCtx::Get()->Flush();
151   args_finish(&args);
152 }
153 
test_named_and_numeric_scope_ids(void)154 static void test_named_and_numeric_scope_ids(void) {
155   char* arbitrary_interface_name = static_cast<char*>(gpr_zalloc(IF_NAMESIZE));
156   int interface_index = 0;
157   // Probe candidate interface index numbers until we find one that the
158   // system recognizes, and then use that for the test.
159   for (size_t i = 1; i < 65536; i++) {
160     if (if_indextoname(i, arbitrary_interface_name) != nullptr) {
161       gpr_log(
162           GPR_DEBUG,
163           "Found interface at index %d named %s. Will use this for the test",
164           (int)i, arbitrary_interface_name);
165       interface_index = (int)i;
166       break;
167     }
168   }
169   GPR_ASSERT(strlen(arbitrary_interface_name) > 0);
170   // Test resolution of an ipv6 address with a named scope ID
171   gpr_log(GPR_DEBUG, "test resolution with a named scope ID");
172   std::string target_with_named_scope_id =
173       absl::StrFormat("fe80::1234%%%s", arbitrary_interface_name);
174   resolve_address_must_succeed(target_with_named_scope_id.c_str());
175   gpr_free(arbitrary_interface_name);
176   // Test resolution of an ipv6 address with a numeric scope ID
177   gpr_log(GPR_DEBUG, "test resolution with a numeric scope ID");
178   std::string target_with_numeric_scope_id =
179       absl::StrFormat("fe80::1234%%%d", interface_index);
180   resolve_address_must_succeed(target_with_numeric_scope_id.c_str());
181 }
182 
main(int argc,char ** argv)183 int main(int argc, char** argv) {
184   // First set the resolver type based off of --resolver
185   const char* resolver_type = nullptr;
186   gpr_cmdline* cl = gpr_cmdline_create("resolve address test");
187   gpr_cmdline_add_string(cl, "resolver", "Resolver type (ares or native)",
188                          &resolver_type);
189   // In case that there are more than one argument on the command line,
190   // --resolver will always be the first one, so only parse the first argument
191   // (other arguments may be unknown to cl)
192   gpr_cmdline_parse(cl, argc > 2 ? 2 : argc, argv);
193   grpc_core::UniquePtr<char> resolver =
194       GPR_GLOBAL_CONFIG_GET(grpc_dns_resolver);
195   if (strlen(resolver.get()) != 0) {
196     gpr_log(GPR_INFO, "Warning: overriding resolver setting of %s",
197             resolver.get());
198   }
199   if (resolver_type != nullptr && gpr_stricmp(resolver_type, "native") == 0) {
200     GPR_GLOBAL_CONFIG_SET(grpc_dns_resolver, "native");
201   } else if (resolver_type != nullptr &&
202              gpr_stricmp(resolver_type, "ares") == 0) {
203     GPR_GLOBAL_CONFIG_SET(grpc_dns_resolver, "ares");
204   } else {
205     gpr_log(GPR_ERROR, "--resolver_type was not set to ares or native");
206     abort();
207   }
208   grpc::testing::TestEnvironment env(argc, argv);
209   grpc_init();
210 
211   {
212     grpc_core::ExecCtx exec_ctx;
213     test_named_and_numeric_scope_ids();
214     // c-ares resolver doesn't support UDS (ability for native DNS resolver
215     // to handle this is only expected to be used by servers, which
216     // unconditionally use the native DNS resolver).
217     grpc_core::UniquePtr<char> resolver =
218         GPR_GLOBAL_CONFIG_GET(grpc_dns_resolver);
219   }
220   gpr_cmdline_destroy(cl);
221 
222   grpc_shutdown();
223   return 0;
224 }
225