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 <grpc/grpc.h>
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28 #include <grpc/support/string_util.h>
29 #include <grpc/support/sync.h>
30 #include <grpc/support/time.h>
31 
32 #include "src/core/ext/filters/client_channel/resolver/dns/dns_resolver_selection.h"
33 #include "src/core/lib/gpr/env.h"
34 #include "src/core/lib/gpr/string.h"
35 #include "src/core/lib/gpr/useful.h"
36 #include "src/core/lib/gprpp/thd.h"
37 #include "src/core/lib/iomgr/executor.h"
38 #include "src/core/lib/iomgr/iomgr.h"
39 #include "test/core/util/cmdline.h"
40 #include "test/core/util/test_config.h"
41 
test_deadline(void)42 static gpr_timespec test_deadline(void) {
43   return grpc_timeout_seconds_to_deadline(100);
44 }
45 
46 typedef struct args_struct {
47   grpc_core::Thread thd;
48   gpr_event ev;
49   grpc_resolved_addresses* addrs;
50   gpr_atm done_atm;
51   gpr_mu* mu;
52   grpc_pollset* pollset;
53   grpc_pollset_set* pollset_set;
54 } args_struct;
55 
do_nothing(void * arg,grpc_error * error)56 static void do_nothing(void* arg, grpc_error* error) {}
57 
args_init(args_struct * args)58 void args_init(args_struct* args) {
59   gpr_event_init(&args->ev);
60   args->pollset = static_cast<grpc_pollset*>(gpr_zalloc(grpc_pollset_size()));
61   grpc_pollset_init(args->pollset, &args->mu);
62   args->pollset_set = grpc_pollset_set_create();
63   grpc_pollset_set_add_pollset(args->pollset_set, args->pollset);
64   args->addrs = nullptr;
65 }
66 
args_finish(args_struct * args)67 void args_finish(args_struct* args) {
68   GPR_ASSERT(gpr_event_wait(&args->ev, test_deadline()));
69   args->thd.Join();
70   // Don't need to explicitly destruct args->thd since
71   // args is actually going to be destructed, not just freed
72   grpc_resolved_addresses_destroy(args->addrs);
73   grpc_pollset_set_del_pollset(args->pollset_set, args->pollset);
74   grpc_pollset_set_destroy(args->pollset_set);
75   grpc_closure do_nothing_cb;
76   GRPC_CLOSURE_INIT(&do_nothing_cb, do_nothing, nullptr,
77                     grpc_schedule_on_exec_ctx);
78   grpc_pollset_shutdown(args->pollset, &do_nothing_cb);
79   // exec_ctx needs to be flushed before calling grpc_pollset_destroy()
80   grpc_core::ExecCtx::Get()->Flush();
81   grpc_pollset_destroy(args->pollset);
82   gpr_free(args->pollset);
83 }
84 
n_sec_deadline(int seconds)85 static grpc_millis n_sec_deadline(int seconds) {
86   return grpc_timespec_to_millis_round_up(
87       grpc_timeout_seconds_to_deadline(seconds));
88 }
89 
actually_poll(void * argsp)90 static void actually_poll(void* argsp) {
91   args_struct* args = static_cast<args_struct*>(argsp);
92   grpc_millis deadline = n_sec_deadline(10);
93   while (true) {
94     grpc_core::ExecCtx exec_ctx;
95     bool done = gpr_atm_acq_load(&args->done_atm) != 0;
96     if (done) {
97       break;
98     }
99     grpc_millis time_left = deadline - grpc_core::ExecCtx::Get()->Now();
100     gpr_log(GPR_DEBUG, "done=%d, time_left=%" PRId64, done, time_left);
101     GPR_ASSERT(time_left >= 0);
102     grpc_pollset_worker* worker = nullptr;
103     gpr_mu_lock(args->mu);
104     GRPC_LOG_IF_ERROR("pollset_work", grpc_pollset_work(args->pollset, &worker,
105                                                         n_sec_deadline(1)));
106     gpr_mu_unlock(args->mu);
107     grpc_core::ExecCtx::Get()->Flush();
108   }
109   gpr_event_set(&args->ev, (void*)1);
110 }
111 
poll_pollset_until_request_done(args_struct * args)112 static void poll_pollset_until_request_done(args_struct* args) {
113   gpr_atm_rel_store(&args->done_atm, 0);
114   args->thd = grpc_core::Thread("grpc_poll_pollset", actually_poll, args);
115   args->thd.Start();
116 }
117 
must_succeed(void * argsp,grpc_error * err)118 static void must_succeed(void* argsp, grpc_error* err) {
119   args_struct* args = static_cast<args_struct*>(argsp);
120   GPR_ASSERT(err == GRPC_ERROR_NONE);
121   GPR_ASSERT(args->addrs != nullptr);
122   GPR_ASSERT(args->addrs->naddrs > 0);
123   gpr_atm_rel_store(&args->done_atm, 1);
124   gpr_mu_lock(args->mu);
125   GRPC_LOG_IF_ERROR("pollset_kick", grpc_pollset_kick(args->pollset, nullptr));
126   gpr_mu_unlock(args->mu);
127 }
128 
must_fail(void * argsp,grpc_error * err)129 static void must_fail(void* argsp, grpc_error* err) {
130   args_struct* args = static_cast<args_struct*>(argsp);
131   GPR_ASSERT(err != GRPC_ERROR_NONE);
132   gpr_atm_rel_store(&args->done_atm, 1);
133   gpr_mu_lock(args->mu);
134   GRPC_LOG_IF_ERROR("pollset_kick", grpc_pollset_kick(args->pollset, nullptr));
135   gpr_mu_unlock(args->mu);
136 }
137 
test_unix_socket(void)138 static void test_unix_socket(void) {
139   grpc_core::ExecCtx exec_ctx;
140   args_struct args;
141   args_init(&args);
142   poll_pollset_until_request_done(&args);
143   grpc_resolve_address(
144       "unix:/path/name", nullptr, args.pollset_set,
145       GRPC_CLOSURE_CREATE(must_succeed, &args, grpc_schedule_on_exec_ctx),
146       &args.addrs);
147   args_finish(&args);
148 }
149 
test_unix_socket_path_name_too_long(void)150 static void test_unix_socket_path_name_too_long(void) {
151   grpc_core::ExecCtx exec_ctx;
152   args_struct args;
153   args_init(&args);
154   const char prefix[] = "unix:/path/name";
155   size_t path_name_length =
156       GPR_ARRAY_SIZE(((struct sockaddr_un*)nullptr)->sun_path) + 6;
157   char* path_name =
158       static_cast<char*>(gpr_malloc(sizeof(char) * path_name_length));
159   memset(path_name, 'a', path_name_length);
160   memcpy(path_name, prefix, strlen(prefix) - 1);
161   path_name[path_name_length - 1] = '\0';
162 
163   poll_pollset_until_request_done(&args);
164   grpc_resolve_address(
165       path_name, nullptr, args.pollset_set,
166       GRPC_CLOSURE_CREATE(must_fail, &args, grpc_schedule_on_exec_ctx),
167       &args.addrs);
168   gpr_free(path_name);
169   args_finish(&args);
170 }
171 
resolve_address_must_succeed(const char * target)172 static void resolve_address_must_succeed(const char* target) {
173   grpc_core::ExecCtx exec_ctx;
174   args_struct args;
175   args_init(&args);
176   poll_pollset_until_request_done(&args);
177   grpc_resolve_address(
178       target, "1" /* port number */, args.pollset_set,
179       GRPC_CLOSURE_CREATE(must_succeed, &args, grpc_schedule_on_exec_ctx),
180       &args.addrs);
181   grpc_core::ExecCtx::Get()->Flush();
182   args_finish(&args);
183 }
184 
test_named_and_numeric_scope_ids(void)185 static void test_named_and_numeric_scope_ids(void) {
186   char* arbitrary_interface_name = static_cast<char*>(gpr_zalloc(IF_NAMESIZE));
187   int interface_index = 0;
188   // Probe candidate interface index numbers until we find one that the
189   // system recognizes, and then use that for the test.
190   for (size_t i = 1; i < 65536; i++) {
191     if (if_indextoname(i, arbitrary_interface_name) != nullptr) {
192       gpr_log(
193           GPR_DEBUG,
194           "Found interface at index %d named %s. Will use this for the test",
195           (int)i, arbitrary_interface_name);
196       interface_index = (int)i;
197       break;
198     }
199   }
200   GPR_ASSERT(strlen(arbitrary_interface_name) > 0);
201   // Test resolution of an ipv6 address with a named scope ID
202   gpr_log(GPR_DEBUG, "test resolution with a named scope ID");
203   char* target_with_named_scope_id = nullptr;
204   gpr_asprintf(&target_with_named_scope_id, "fe80::1234%%%s",
205                arbitrary_interface_name);
206   resolve_address_must_succeed(target_with_named_scope_id);
207   gpr_free(target_with_named_scope_id);
208   gpr_free(arbitrary_interface_name);
209   // Test resolution of an ipv6 address with a numeric scope ID
210   gpr_log(GPR_DEBUG, "test resolution with a numeric scope ID");
211   char* target_with_numeric_scope_id = nullptr;
212   gpr_asprintf(&target_with_numeric_scope_id, "fe80::1234%%%d",
213                interface_index);
214   resolve_address_must_succeed(target_with_numeric_scope_id);
215   gpr_free(target_with_numeric_scope_id);
216 }
217 
main(int argc,char ** argv)218 int main(int argc, char** argv) {
219   // First set the resolver type based off of --resolver
220   const char* resolver_type = nullptr;
221   gpr_cmdline* cl = gpr_cmdline_create("resolve address test");
222   gpr_cmdline_add_string(cl, "resolver", "Resolver type (ares or native)",
223                          &resolver_type);
224   // In case that there are more than one argument on the command line,
225   // --resolver will always be the first one, so only parse the first argument
226   // (other arguments may be unknown to cl)
227   gpr_cmdline_parse(cl, argc > 2 ? 2 : argc, argv);
228   grpc_core::UniquePtr<char> resolver =
229       GPR_GLOBAL_CONFIG_GET(grpc_dns_resolver);
230   if (strlen(resolver.get()) != 0) {
231     gpr_log(GPR_INFO, "Warning: overriding resolver setting of %s",
232             resolver.get());
233   }
234   if (gpr_stricmp(resolver_type, "native") == 0) {
235     GPR_GLOBAL_CONFIG_SET(grpc_dns_resolver, "native");
236   } else if (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