• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "src/core/tsi/alts/handshaker/alts_tsi_event.h"
22 
23 #include <grpc/grpc.h>
24 #include <grpc/support/alloc.h>
25 #include <grpc/support/log.h>
26 
27 #include "src/core/lib/slice/slice_internal.h"
28 
alts_tsi_event_create(alts_tsi_handshaker * handshaker,tsi_handshaker_on_next_done_cb cb,void * user_data,grpc_alts_credentials_options * options,grpc_slice target_name,alts_tsi_event ** event)29 tsi_result alts_tsi_event_create(alts_tsi_handshaker* handshaker,
30                                  tsi_handshaker_on_next_done_cb cb,
31                                  void* user_data,
32                                  grpc_alts_credentials_options* options,
33                                  grpc_slice target_name,
34                                  alts_tsi_event** event) {
35   if (event == nullptr || handshaker == nullptr || cb == nullptr) {
36     gpr_log(GPR_ERROR, "Invalid arguments to alts_tsi_event_create()");
37     return TSI_INVALID_ARGUMENT;
38   }
39   alts_tsi_event* e = static_cast<alts_tsi_event*>(gpr_zalloc(sizeof(*e)));
40   e->handshaker = handshaker;
41   e->cb = cb;
42   e->user_data = user_data;
43   e->options = grpc_alts_credentials_options_copy(options);
44   e->target_name = grpc_slice_copy(target_name);
45   grpc_metadata_array_init(&e->initial_metadata);
46   grpc_metadata_array_init(&e->trailing_metadata);
47   *event = e;
48   return TSI_OK;
49 }
50 
alts_tsi_event_dispatch_to_handshaker(alts_tsi_event * event,bool is_ok)51 void alts_tsi_event_dispatch_to_handshaker(alts_tsi_event* event, bool is_ok) {
52   if (event == nullptr) {
53     gpr_log(
54         GPR_ERROR,
55         "ALTS TSI event is nullptr in alts_tsi_event_dispatch_to_handshaker()");
56     return;
57   }
58   alts_tsi_handshaker_handle_response(event->handshaker, event->recv_buffer,
59                                       event->status, &event->details, event->cb,
60                                       event->user_data, is_ok);
61 }
62 
alts_tsi_event_destroy(alts_tsi_event * event)63 void alts_tsi_event_destroy(alts_tsi_event* event) {
64   if (event == nullptr) {
65     return;
66   }
67   grpc_byte_buffer_destroy(event->send_buffer);
68   grpc_byte_buffer_destroy(event->recv_buffer);
69   grpc_metadata_array_destroy(&event->initial_metadata);
70   grpc_metadata_array_destroy(&event->trailing_metadata);
71   grpc_slice_unref_internal(event->details);
72   grpc_slice_unref_internal(event->target_name);
73   grpc_alts_credentials_options_destroy(event->options);
74   gpr_free(event);
75 }
76