local_transport_security.cc
Go to the documentation of this file.
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 
20 
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 
27 #include <grpc/support/alloc.h>
28 #include <grpc/support/log.h>
30 
33 
34 namespace {
35 
36 /* Main struct for local TSI zero-copy frame protector. */
37 typedef struct local_zero_copy_grpc_protector {
39 } local_zero_copy_grpc_protector;
40 
41 /* Main struct for local TSI handshaker result. */
42 typedef struct local_tsi_handshaker_result {
44  unsigned char* unused_bytes;
45  size_t unused_bytes_size;
46 } local_tsi_handshaker_result;
47 
48 /* Main struct for local TSI handshaker. */
49 typedef struct local_tsi_handshaker {
51 } local_tsi_handshaker;
52 
53 /* --- tsi_handshaker_result methods implementation. --- */
54 
56  tsi_peer* /*peer*/) {
57  return TSI_OK;
58 }
59 
61  const tsi_handshaker_result* /*self*/,
62  tsi_frame_protector_type* frame_protector_type) {
63  *frame_protector_type = TSI_FRAME_PROTECTOR_NONE;
64  return TSI_OK;
65 }
66 
68  const unsigned char** bytes,
69  size_t* bytes_size) {
70  if (self == nullptr || bytes == nullptr || bytes_size == nullptr) {
71  gpr_log(GPR_ERROR, "Invalid arguments to get_unused_bytes()");
72  return TSI_INVALID_ARGUMENT;
73  }
74  auto* result = reinterpret_cast<local_tsi_handshaker_result*>(
75  const_cast<tsi_handshaker_result*>(self));
76  *bytes_size = result->unused_bytes_size;
77  *bytes = result->unused_bytes;
78  return TSI_OK;
79 }
80 
82  if (self == nullptr) {
83  return;
84  }
85  local_tsi_handshaker_result* result =
86  reinterpret_cast<local_tsi_handshaker_result*>(
87  const_cast<tsi_handshaker_result*>(self));
88  gpr_free(result->unused_bytes);
90 }
91 
95  nullptr, /* handshaker_result_create_zero_copy_grpc_protector */
96  nullptr, /* handshaker_result_create_frame_protector */
99 
100 tsi_result create_handshaker_result(const unsigned char* received_bytes,
101  size_t received_bytes_size,
102  tsi_handshaker_result** self) {
103  if (self == nullptr) {
104  gpr_log(GPR_ERROR, "Invalid arguments to create_handshaker_result()");
105  return TSI_INVALID_ARGUMENT;
106  }
107  local_tsi_handshaker_result* result =
108  grpc_core::Zalloc<local_tsi_handshaker_result>();
109  if (received_bytes_size > 0) {
110  result->unused_bytes =
111  static_cast<unsigned char*>(gpr_malloc(received_bytes_size));
112  memcpy(result->unused_bytes, received_bytes, received_bytes_size);
113  }
114  result->unused_bytes_size = received_bytes_size;
115  result->base.vtable = &result_vtable;
116  *self = &result->base;
117  return TSI_OK;
118 }
119 
120 /* --- tsi_handshaker methods implementation. --- */
121 
123  tsi_handshaker* self, const unsigned char* received_bytes,
124  size_t received_bytes_size, const unsigned char** /*bytes_to_send*/,
125  size_t* bytes_to_send_size, tsi_handshaker_result** result,
126  tsi_handshaker_on_next_done_cb /*cb*/, void* /*user_data*/) {
127  if (self == nullptr) {
128  gpr_log(GPR_ERROR, "Invalid arguments to handshaker_next()");
129  return TSI_INVALID_ARGUMENT;
130  }
131  /* Note that there is no interaction between TSI peers, and all operations are
132  * local.
133  */
134  *bytes_to_send_size = 0;
135  create_handshaker_result(received_bytes, received_bytes_size, result);
136  return TSI_OK;
137 }
138 
140  if (self == nullptr) {
141  return;
142  }
143  local_tsi_handshaker* handshaker =
144  reinterpret_cast<local_tsi_handshaker*>(self);
145  gpr_free(handshaker);
146 }
147 
149  nullptr, /* get_bytes_to_send_to_peer -- deprecated */
150  nullptr, /* process_bytes_from_peer -- deprecated */
151  nullptr, /* get_result -- deprecated */
152  nullptr, /* extract_peer -- deprecated */
153  nullptr, /* create_frame_protector -- deprecated */
156  nullptr, /* shutdown */
157 };
158 
159 } // namespace
160 
162  if (self == nullptr) {
163  gpr_log(GPR_ERROR, "Invalid arguments to local_tsi_handshaker_create()");
164  return TSI_INVALID_ARGUMENT;
165  }
166  local_tsi_handshaker* handshaker = grpc_core::Zalloc<local_tsi_handshaker>();
167  handshaker->base.vtable = &handshaker_vtable;
168  *self = &handshaker->base;
169  return TSI_OK;
170 }
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
log.h
tsi_handshaker_vtable
Definition: transport_security.h:57
tsi_handshaker
Definition: transport_security.h:84
string.h
local_transport_security.h
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
gpr_malloc
GPRAPI void * gpr_malloc(size_t size)
Definition: alloc.cc:29
handshaker_vtable
static const tsi_handshaker_vtable handshaker_vtable
Definition: alts_tsi_handshaker.cc:617
TSI_OK
@ TSI_OK
Definition: transport_security_interface.h:32
handshaker_next
static tsi_result handshaker_next(tsi_handshaker *self, const unsigned char *received_bytes, size_t received_bytes_size, const unsigned char **, size_t *, tsi_handshaker_result **, tsi_handshaker_on_next_done_cb cb, void *user_data)
Definition: alts_tsi_handshaker.cc:521
string_util.h
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
handshaker_result_destroy
static void handshaker_result_destroy(tsi_handshaker_result *self)
Definition: alts_tsi_handshaker.cc:242
tsi_frame_protector_type
tsi_frame_protector_type
Definition: transport_security_interface.h:69
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
handshaker_destroy
static void handshaker_destroy(tsi_handshaker *self)
Definition: alts_tsi_handshaker.cc:601
tsi_handshaker_on_next_done_cb
void(* tsi_handshaker_on_next_done_cb)(tsi_result status, void *user_data, const unsigned char *bytes_to_send, size_t bytes_to_send_size, tsi_handshaker_result *handshaker_result)
Definition: transport_security_interface.h:462
tsi_result
tsi_result
Definition: transport_security_interface.h:31
gen_synthetic_protos.base
base
Definition: gen_synthetic_protos.py:31
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
tsi_local_handshaker_create
tsi_result tsi_local_handshaker_create(tsi_handshaker **self)
Definition: local_transport_security.cc:161
handshaker_result_get_frame_protector_type
static tsi_result handshaker_result_get_frame_protector_type(const tsi_handshaker_result *, tsi_frame_protector_type *frame_protector_type)
Definition: alts_tsi_handshaker.cc:156
tsi_peer
Definition: transport_security_interface.h:238
bytes
uint8 bytes[10]
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/coded_stream_unittest.cc:153
alloc.h
TSI_INVALID_ARGUMENT
@ TSI_INVALID_ARGUMENT
Definition: transport_security_interface.h:34
exec_ctx.h
tsi_handshaker_result
Definition: transport_security.h:121
handshaker_result_extract_peer
static tsi_result handshaker_result_extract_peer(const tsi_handshaker_result *self, tsi_peer *peer)
Definition: alts_tsi_handshaker.cc:88
handshaker_result_get_unused_bytes
static tsi_result handshaker_result_get_unused_bytes(const tsi_handshaker_result *self, const unsigned char **bytes, size_t *bytes_size)
Definition: alts_tsi_handshaker.cc:226
tsi_zero_copy_grpc_protector
Definition: transport_security_grpc.h:79
tsi_handshaker_result_vtable
Definition: transport_security.h:100
result_vtable
static const tsi_handshaker_result_vtable result_vtable
Definition: alts_tsi_handshaker.cc:257
transport_security_grpc.h
port_platform.h
TSI_FRAME_PROTECTOR_NONE
@ TSI_FRAME_PROTECTOR_NONE
Definition: transport_security_interface.h:86


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:16