892cdeb27b75adf65e13919bae55d1bc171683ed
[motion2.git] /
1 /*
2  *
3  * Copyright 2017 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 <grpc/support/port_platform.h>
20
21 #include "src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_channel.h"
22
23 #include <string.h>
24
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/string_util.h>
27
28 #include "src/core/ext/filters/client_channel/client_channel.h"
29 #include "src/core/ext/filters/client_channel/server_address.h"
30 #include "src/core/lib/channel/channel_args.h"
31 #include "src/core/lib/gpr/string.h"
32 #include "src/core/lib/iomgr/sockaddr_utils.h"
33 #include "src/core/lib/security/credentials/credentials.h"
34 #include "src/core/lib/security/transport/target_authority_table.h"
35 #include "src/core/lib/slice/slice_internal.h"
36
37 namespace grpc_core {
38 namespace {
39
40 int BalancerNameCmp(const grpc_core::UniquePtr<char>& a,
41                     const grpc_core::UniquePtr<char>& b) {
42   return strcmp(a.get(), b.get());
43 }
44
45 RefCountedPtr<TargetAuthorityTable> CreateTargetAuthorityTable(
46     const ServerAddressList& addresses) {
47   TargetAuthorityTable::Entry* target_authority_entries =
48       static_cast<TargetAuthorityTable::Entry*>(
49           gpr_zalloc(sizeof(*target_authority_entries) * addresses.size()));
50   for (size_t i = 0; i < addresses.size(); ++i) {
51     char* addr_str;
52     GPR_ASSERT(
53         grpc_sockaddr_to_string(&addr_str, &addresses[i].address(), true) > 0);
54     target_authority_entries[i].key = grpc_slice_from_copied_string(addr_str);
55     gpr_free(addr_str);
56     char* balancer_name = grpc_channel_arg_get_string(grpc_channel_args_find(
57         addresses[i].args(), GRPC_ARG_ADDRESS_BALANCER_NAME));
58     target_authority_entries[i].value.reset(gpr_strdup(balancer_name));
59   }
60   RefCountedPtr<TargetAuthorityTable> target_authority_table =
61       TargetAuthorityTable::Create(addresses.size(), target_authority_entries,
62                                    BalancerNameCmp);
63   gpr_free(target_authority_entries);
64   return target_authority_table;
65 }
66
67 }  // namespace
68 }  // namespace grpc_core
69
70 grpc_channel_args* grpc_lb_policy_grpclb_modify_lb_channel_args(
71     const grpc_core::ServerAddressList& addresses, grpc_channel_args* args) {
72   const char* args_to_remove[1];
73   size_t num_args_to_remove = 0;
74   grpc_arg args_to_add[2];
75   size_t num_args_to_add = 0;
76   // Add arg for targets info table.
77   grpc_core::RefCountedPtr<grpc_core::TargetAuthorityTable>
78       target_authority_table = grpc_core::CreateTargetAuthorityTable(addresses);
79   args_to_add[num_args_to_add++] =
80       grpc_core::CreateTargetAuthorityTableChannelArg(
81           target_authority_table.get());
82   // Substitute the channel credentials with a version without call
83   // credentials: the load balancer is not necessarily trusted to handle
84   // bearer token credentials.
85   grpc_channel_credentials* channel_credentials =
86       grpc_channel_credentials_find_in_args(args);
87   grpc_core::RefCountedPtr<grpc_channel_credentials> creds_sans_call_creds;
88   if (channel_credentials != nullptr) {
89     creds_sans_call_creds =
90         channel_credentials->duplicate_without_call_credentials();
91     GPR_ASSERT(creds_sans_call_creds != nullptr);
92     args_to_remove[num_args_to_remove++] = GRPC_ARG_CHANNEL_CREDENTIALS;
93     args_to_add[num_args_to_add++] =
94         grpc_channel_credentials_to_arg(creds_sans_call_creds.get());
95   }
96   grpc_channel_args* result = grpc_channel_args_copy_and_add_and_remove(
97       args, args_to_remove, num_args_to_remove, args_to_add, num_args_to_add);
98   // Clean up.
99   grpc_channel_args_destroy(args);
100   return result;
101 }