Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc / deps / grpc / src / core / lib / security / credentials / alts / grpc_alts_credentials_client_options.cc
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 <grpc/support/port_platform.h>
20
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include <grpc/support/alloc.h>
25 #include <grpc/support/log.h>
26 #include <grpc/support/string_util.h>
27
28 #include "src/core/lib/security/credentials/alts/grpc_alts_credentials_options.h"
29 #include "src/core/tsi/alts/handshaker/transport_security_common_api.h"
30
31 static grpc_alts_credentials_options* alts_client_options_copy(
32     const grpc_alts_credentials_options* options);
33
34 static void alts_client_options_destroy(grpc_alts_credentials_options* options);
35
36 static target_service_account* target_service_account_create(
37     const char* service_account) {
38   if (service_account == nullptr) {
39     return nullptr;
40   }
41   auto* sa = static_cast<target_service_account*>(
42       gpr_zalloc(sizeof(target_service_account)));
43   sa->data = gpr_strdup(service_account);
44   return sa;
45 }
46
47 void grpc_alts_credentials_client_options_add_target_service_account(
48     grpc_alts_credentials_options* options, const char* service_account) {
49   if (options == nullptr || service_account == nullptr) {
50     gpr_log(
51         GPR_ERROR,
52         "Invalid nullptr arguments to "
53         "grpc_alts_credentials_client_options_add_target_service_account()");
54     return;
55   }
56   auto client_options =
57       reinterpret_cast<grpc_alts_credentials_client_options*>(options);
58   target_service_account* node = target_service_account_create(service_account);
59   node->next = client_options->target_account_list_head;
60   client_options->target_account_list_head = node;
61 }
62
63 static void target_service_account_destroy(
64     target_service_account* service_account) {
65   if (service_account == nullptr) {
66     return;
67   }
68   gpr_free(service_account->data);
69   gpr_free(service_account);
70 }
71
72 static const grpc_alts_credentials_options_vtable vtable = {
73     alts_client_options_copy, alts_client_options_destroy};
74
75 grpc_alts_credentials_options* grpc_alts_credentials_client_options_create(
76     void) {
77   auto client_options = static_cast<grpc_alts_credentials_client_options*>(
78       gpr_zalloc(sizeof(grpc_alts_credentials_client_options)));
79   client_options->base.vtable = &vtable;
80   return &client_options->base;
81 }
82
83 static grpc_alts_credentials_options* alts_client_options_copy(
84     const grpc_alts_credentials_options* options) {
85   if (options == nullptr) {
86     return nullptr;
87   }
88   grpc_alts_credentials_options* new_options =
89       grpc_alts_credentials_client_options_create();
90   auto new_client_options =
91       reinterpret_cast<grpc_alts_credentials_client_options*>(new_options);
92   /* Copy target service accounts. */
93   target_service_account* prev = nullptr;
94   auto node =
95       (reinterpret_cast<const grpc_alts_credentials_client_options*>(options))
96           ->target_account_list_head;
97   while (node != nullptr) {
98     target_service_account* new_node =
99         target_service_account_create(node->data);
100     if (prev == nullptr) {
101       new_client_options->target_account_list_head = new_node;
102     } else {
103       prev->next = new_node;
104     }
105     prev = new_node;
106     node = node->next;
107   }
108   /* Copy rpc protocol versions. */
109   grpc_gcp_rpc_protocol_versions_copy(&options->rpc_versions,
110                                       &new_options->rpc_versions);
111   return new_options;
112 }
113
114 static void alts_client_options_destroy(
115     grpc_alts_credentials_options* options) {
116   if (options == nullptr) {
117     return;
118   }
119   auto* client_options =
120       reinterpret_cast<grpc_alts_credentials_client_options*>(options);
121   target_service_account* node = client_options->target_account_list_head;
122   while (node != nullptr) {
123     target_service_account* next_node = node->next;
124     target_service_account_destroy(node);
125     node = next_node;
126   }
127 }