alts_grpc_record_protocol_common.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 <string.h>
24 
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/log.h>
27 
31 
32 const size_t kInitialIovecBufferSize = 8;
33 
34 /* Makes sure iovec_buf in alts_grpc_record_protocol is large enough. */
36  const grpc_slice_buffer* sb) {
37  GPR_ASSERT(rp != nullptr && sb != nullptr);
38  if (sb->count <= rp->iovec_buf_length) {
39  return;
40  }
41  /* At least double the iovec buffer size. */
42  rp->iovec_buf_length = std::max(sb->count, 2 * rp->iovec_buf_length);
43  rp->iovec_buf = static_cast<iovec_t*>(
44  gpr_realloc(rp->iovec_buf, rp->iovec_buf_length * sizeof(iovec_t)));
45 }
46 
47 /* --- Implementation of methods defined in tsi_grpc_record_protocol_common.h.
48  * --- */
49 
52  GPR_ASSERT(rp != nullptr && sb != nullptr);
53  ensure_iovec_buf_size(rp, sb);
54  for (size_t i = 0; i < sb->count; i++) {
57  }
58 }
59 
61  unsigned char* dst) {
62  GPR_ASSERT(src != nullptr && dst != nullptr);
63  for (size_t i = 0; i < src->count; i++) {
64  size_t slice_length = GRPC_SLICE_LENGTH(src->slices[i]);
65  memcpy(dst, GRPC_SLICE_START_PTR(src->slices[i]), slice_length);
66  dst += slice_length;
67  }
68 }
69 
72  iovec_t header_iovec = {nullptr, 0};
73  if (rp == nullptr) {
74  return header_iovec;
75  }
76  header_iovec.iov_len = rp->header_length;
77  if (rp->header_sb.count == 1) {
78  header_iovec.iov_base = GRPC_SLICE_START_PTR(rp->header_sb.slices[0]);
79  } else {
80  /* Frame header is in multiple slices, copies the header bytes from slice
81  * buffer to a single flat buffer. */
83  header_iovec.iov_base = rp->header_buf;
84  }
85  return header_iovec;
86 }
87 
89  gsec_aead_crypter* crypter,
90  size_t overflow_size, bool is_client,
91  bool is_integrity_only,
92  bool is_protect) {
93  if (rp == nullptr || crypter == nullptr) {
95  "Invalid nullptr arguments to alts_grpc_record_protocol init.");
96  return TSI_INVALID_ARGUMENT;
97  }
98  /* Creates alts_iovec_record_protocol. */
99  char* error_details = nullptr;
101  crypter, overflow_size, is_client, is_integrity_only, is_protect,
102  &rp->iovec_rp, &error_details);
103  if (status != GRPC_STATUS_OK) {
104  gpr_log(GPR_ERROR, "Failed to create alts_iovec_record_protocol, %s.",
105  error_details);
106  gpr_free(error_details);
107  return TSI_INTERNAL_ERROR;
108  }
109  /* Allocates header slice buffer. */
111  /* Allocates header buffer. */
113  rp->header_buf = static_cast<unsigned char*>(gpr_malloc(rp->header_length));
115  /* Allocates iovec buffer. */
117  rp->iovec_buf =
118  static_cast<iovec_t*>(gpr_malloc(rp->iovec_buf_length * sizeof(iovec_t)));
119  return TSI_OK;
120 }
121 
122 /* --- Implementation of methods defined in tsi_grpc_record_protocol.h. --- */
124  alts_grpc_record_protocol* self, grpc_slice_buffer* unprotected_slices,
125  grpc_slice_buffer* protected_slices) {
126  if (grpc_core::ExecCtx::Get() == nullptr || self == nullptr ||
127  self->vtable == nullptr || unprotected_slices == nullptr ||
128  protected_slices == nullptr) {
129  return TSI_INVALID_ARGUMENT;
130  }
131  if (self->vtable->protect == nullptr) {
132  return TSI_UNIMPLEMENTED;
133  }
134  return self->vtable->protect(self, unprotected_slices, protected_slices);
135 }
136 
138  alts_grpc_record_protocol* self, grpc_slice_buffer* protected_slices,
139  grpc_slice_buffer* unprotected_slices) {
140  if (grpc_core::ExecCtx::Get() == nullptr || self == nullptr ||
141  self->vtable == nullptr || protected_slices == nullptr ||
142  unprotected_slices == nullptr) {
143  return TSI_INVALID_ARGUMENT;
144  }
145  if (self->vtable->unprotect == nullptr) {
146  return TSI_UNIMPLEMENTED;
147  }
148  return self->vtable->unprotect(self, protected_slices, unprotected_slices);
149 }
150 
152  if (self == nullptr) {
153  return;
154  }
155  if (self->vtable->destruct != nullptr) {
156  self->vtable->destruct(self);
157  }
160  gpr_free(self->header_buf);
161  gpr_free(self->iovec_buf);
162  gpr_free(self);
163 }
164 
165 /* Integrity-only and privacy-integrity share the same implementation. No need
166  * to call vtable. */
168  const alts_grpc_record_protocol* self, size_t max_protected_frame_size) {
169  if (self == nullptr) {
170  return 0;
171  }
173  self->iovec_rp, max_protected_frame_size);
174 }
alts_grpc_record_protocol::iovec_buf_length
size_t iovec_buf_length
Definition: alts_grpc_record_protocol_common.h:55
dst
static const char dst[]
Definition: test-fs-copyfile.c:37
alts_grpc_record_protocol_convert_slice_buffer_to_iovec
void alts_grpc_record_protocol_convert_slice_buffer_to_iovec(alts_grpc_record_protocol *rp, const grpc_slice_buffer *sb)
Definition: alts_grpc_record_protocol_common.cc:50
log.h
alts_grpc_record_protocol_protect
tsi_result alts_grpc_record_protocol_protect(alts_grpc_record_protocol *self, grpc_slice_buffer *unprotected_slices, grpc_slice_buffer *protected_slices)
Definition: alts_grpc_record_protocol_common.cc:123
alts_grpc_record_protocol_init
tsi_result alts_grpc_record_protocol_init(alts_grpc_record_protocol *rp, gsec_aead_crypter *crypter, size_t overflow_size, bool is_client, bool is_integrity_only, bool is_protect)
Definition: alts_grpc_record_protocol_common.cc:88
TSI_INTERNAL_ERROR
@ TSI_INTERNAL_ERROR
Definition: transport_security_interface.h:39
string.h
alts_grpc_record_protocol
Definition: alts_grpc_record_protocol_common.h:47
grpc_slice_buffer::slices
grpc_slice * slices
Definition: include/grpc/impl/codegen/slice.h:89
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
useful.h
grpc_status_code
grpc_status_code
Definition: include/grpc/impl/codegen/status.h:28
gpr_malloc
GPRAPI void * gpr_malloc(size_t size)
Definition: alloc.cc:29
alts_grpc_record_protocol_copy_slice_buffer
void alts_grpc_record_protocol_copy_slice_buffer(const grpc_slice_buffer *src, unsigned char *dst)
Definition: alts_grpc_record_protocol_common.cc:60
alts_iovec_record_protocol_destroy
void alts_iovec_record_protocol_destroy(alts_iovec_record_protocol *rp)
Definition: alts_iovec_record_protocol.cc:472
status
absl::Status status
Definition: rls.cc:251
alts_grpc_record_protocol_max_unprotected_data_size
size_t alts_grpc_record_protocol_max_unprotected_data_size(const alts_grpc_record_protocol *self, size_t max_protected_frame_size)
Definition: alts_grpc_record_protocol_common.cc:167
TSI_OK
@ TSI_OK
Definition: transport_security_interface.h:32
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
GRPC_STATUS_OK
@ GRPC_STATUS_OK
Definition: include/grpc/impl/codegen/status.h:30
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
gpr_realloc
GPRAPI void * gpr_realloc(void *p, size_t size)
Definition: alloc.cc:56
alts_grpc_record_protocol::iovec_buf
iovec_t * iovec_buf
Definition: alts_grpc_record_protocol_common.h:54
max
int max
Definition: bloaty/third_party/zlib/examples/enough.c:170
grpc_slice_buffer::count
size_t count
Definition: include/grpc/impl/codegen/slice.h:91
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
ensure_iovec_buf_size
static void ensure_iovec_buf_size(alts_grpc_record_protocol *rp, const grpc_slice_buffer *sb)
Definition: alts_grpc_record_protocol_common.cc:35
alts_grpc_record_protocol_common.h
tsi_result
tsi_result
Definition: transport_security_interface.h:31
GRPC_SLICE_START_PTR
#define GRPC_SLICE_START_PTR(slice)
Definition: include/grpc/impl/codegen/slice.h:101
alts_iovec_record_protocol_get_header_length
size_t alts_iovec_record_protocol_get_header_length()
Definition: alts_iovec_record_protocol.cc:165
alts_grpc_record_protocol::header_sb
grpc_slice_buffer header_sb
Definition: alts_grpc_record_protocol_common.h:50
alts_grpc_record_protocol::tag_length
size_t tag_length
Definition: alts_grpc_record_protocol_common.h:53
slice_internal.h
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
alts_grpc_record_protocol::header_buf
unsigned char * header_buf
Definition: alts_grpc_record_protocol_common.h:51
grpc_slice_buffer_init
GPRAPI void grpc_slice_buffer_init(grpc_slice_buffer *sb)
Definition: slice/slice_buffer.cc:116
TSI_UNIMPLEMENTED
@ TSI_UNIMPLEMENTED
Definition: transport_security_interface.h:38
GRPC_SLICE_LENGTH
#define GRPC_SLICE_LENGTH(slice)
Definition: include/grpc/impl/codegen/slice.h:104
alts_grpc_record_protocol_get_header_iovec
iovec_t alts_grpc_record_protocol_get_header_iovec(alts_grpc_record_protocol *rp)
Definition: alts_grpc_record_protocol_common.cc:70
alts_grpc_record_protocol_unprotect
tsi_result alts_grpc_record_protocol_unprotect(alts_grpc_record_protocol *self, grpc_slice_buffer *protected_slices, grpc_slice_buffer *unprotected_slices)
Definition: alts_grpc_record_protocol_common.cc:137
alts_iovec_record_protocol_get_tag_length
size_t alts_iovec_record_protocol_get_tag_length(const alts_iovec_record_protocol *rp)
Definition: alts_iovec_record_protocol.cc:169
iovec
Definition: gsec.h:33
iovec::iov_len
size_t iov_len
Definition: gsec.h:35
kInitialIovecBufferSize
const size_t kInitialIovecBufferSize
Definition: alts_grpc_record_protocol_common.cc:32
alts_grpc_record_protocol::header_length
size_t header_length
Definition: alts_grpc_record_protocol_common.h:52
alts_iovec_record_protocol_max_unprotected_data_size
size_t alts_iovec_record_protocol_max_unprotected_data_size(const alts_iovec_record_protocol *rp, size_t max_protected_frame_size)
Definition: alts_iovec_record_protocol.cc:177
alloc.h
alts_iovec_record_protocol_create
grpc_status_code alts_iovec_record_protocol_create(gsec_aead_crypter *crypter, size_t overflow_size, bool is_client, bool is_integrity_only, bool is_protect, alts_iovec_record_protocol **rp, char **error_details)
Definition: alts_iovec_record_protocol.cc:429
alts_grpc_record_protocol_destroy
void alts_grpc_record_protocol_destroy(alts_grpc_record_protocol *self)
Definition: alts_grpc_record_protocol_common.cc:151
TSI_INVALID_ARGUMENT
@ TSI_INVALID_ARGUMENT
Definition: transport_security_interface.h:34
exec_ctx.h
alts_grpc_record_protocol::iovec_rp
alts_iovec_record_protocol * iovec_rp
Definition: alts_grpc_record_protocol_common.h:49
grpc_slice_buffer_destroy_internal
void grpc_slice_buffer_destroy_internal(grpc_slice_buffer *sb)
Definition: slice/slice_buffer.cc:123
gsec_aead_crypter
Definition: gsec.h:178
grpc_slice_buffer
Definition: include/grpc/impl/codegen/slice.h:83
iovec::iov_base
void * iov_base
Definition: gsec.h:34
self
PHP_PROTO_OBJECT_FREE_END PHP_PROTO_OBJECT_DTOR_END intern self
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/map.c:543
grpc_core::ExecCtx::Get
static ExecCtx * Get()
Definition: exec_ctx.h:205
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
port_platform.h


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