1 //
2 //
3 // Copyright 2018 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/tsi/alts/handshaker/alts_shared_resource.h"
20
21 #include <grpc/support/port_platform.h>
22
23 #include "absl/log/check.h"
24 #include "src/core/lib/channel/channel_args.h"
25 #include "src/core/tsi/alts/handshaker/alts_handshaker_client.h"
26 #include "src/core/util/crash.h"
27
28 static alts_shared_resource_dedicated g_alts_resource_dedicated;
29
grpc_alts_get_shared_resource_dedicated(void)30 alts_shared_resource_dedicated* grpc_alts_get_shared_resource_dedicated(void) {
31 return &g_alts_resource_dedicated;
32 }
33
thread_worker(void *)34 static void thread_worker(void* /*arg*/) {
35 while (true) {
36 grpc_event event =
37 grpc_completion_queue_next(g_alts_resource_dedicated.cq,
38 gpr_inf_future(GPR_CLOCK_REALTIME), nullptr);
39 CHECK(event.type != GRPC_QUEUE_TIMEOUT);
40 if (event.type == GRPC_QUEUE_SHUTDOWN) {
41 break;
42 }
43 CHECK(event.type == GRPC_OP_COMPLETE);
44 alts_handshaker_client* client =
45 static_cast<alts_handshaker_client*>(event.tag);
46 alts_handshaker_client_handle_response(client, event.success);
47 }
48 }
49
grpc_alts_shared_resource_dedicated_init()50 void grpc_alts_shared_resource_dedicated_init() {
51 g_alts_resource_dedicated.cq = nullptr;
52 gpr_mu_init(&g_alts_resource_dedicated.mu);
53 }
54
grpc_alts_shared_resource_dedicated_start(const char * handshaker_service_url)55 void grpc_alts_shared_resource_dedicated_start(
56 const char* handshaker_service_url) {
57 gpr_mu_lock(&g_alts_resource_dedicated.mu);
58 if (g_alts_resource_dedicated.cq == nullptr) {
59 grpc_channel_credentials* creds = grpc_insecure_credentials_create();
60 // Disable retries so that we quickly get a signal when the
61 // handshake server is not reachable.
62 grpc_arg disable_retries_arg = grpc_channel_arg_integer_create(
63 const_cast<char*>(GRPC_ARG_ENABLE_RETRIES), 0);
64 grpc_channel_args args = {1, &disable_retries_arg};
65 g_alts_resource_dedicated.channel =
66 grpc_channel_create(handshaker_service_url, creds, &args);
67 grpc_channel_credentials_release(creds);
68 g_alts_resource_dedicated.cq =
69 grpc_completion_queue_create_for_next(nullptr);
70 g_alts_resource_dedicated.thread =
71 grpc_core::Thread("alts_tsi_handshaker", &thread_worker, nullptr);
72 g_alts_resource_dedicated.interested_parties = grpc_pollset_set_create();
73 grpc_pollset_set_add_pollset(g_alts_resource_dedicated.interested_parties,
74 grpc_cq_pollset(g_alts_resource_dedicated.cq));
75 g_alts_resource_dedicated.thread.Start();
76 }
77 gpr_mu_unlock(&g_alts_resource_dedicated.mu);
78 }
79
grpc_alts_shared_resource_dedicated_shutdown()80 void grpc_alts_shared_resource_dedicated_shutdown() {
81 if (g_alts_resource_dedicated.cq != nullptr) {
82 grpc_pollset_set_del_pollset(g_alts_resource_dedicated.interested_parties,
83 grpc_cq_pollset(g_alts_resource_dedicated.cq));
84 grpc_completion_queue_shutdown(g_alts_resource_dedicated.cq);
85 g_alts_resource_dedicated.thread.Join();
86 grpc_pollset_set_destroy(g_alts_resource_dedicated.interested_parties);
87 grpc_completion_queue_destroy(g_alts_resource_dedicated.cq);
88 grpc_channel_destroy(g_alts_resource_dedicated.channel);
89 }
90 gpr_mu_destroy(&g_alts_resource_dedicated.mu);
91 }
92