3 * Copyright 2018 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #ifndef GRPC_CORE_TSI_ALTS_FRAME_PROTECTOR_FRAME_HANDLER_H
20 #define GRPC_CORE_TSI_ALTS_FRAME_PROTECTOR_FRAME_HANDLER_H
22 #include <grpc/support/port_platform.h>
27 const size_t kFrameMessageType = 0x06;
28 const size_t kFrameLengthFieldSize = 4;
29 const size_t kFrameMessageTypeFieldSize = 4;
30 const size_t kFrameMaxSize = 1024 * 1024;
31 const size_t kFrameHeaderSize =
32 kFrameLengthFieldSize + kFrameMessageTypeFieldSize;
35 * Implementation of frame reader and frame writer. All APIs in the
36 * header are thread-compatible.
40 * Main struct for a frame writer. It reads frames from an input buffer, and
41 * writes the contents as raw bytes. It does not own the input buffer.
43 typedef struct alts_frame_writer {
44 const unsigned char* input_buffer;
45 unsigned char header_buffer[kFrameHeaderSize];
46 size_t input_bytes_written;
47 size_t header_bytes_written;
52 * Main struct for a frame reader. It reads raw bytes and puts the framed
53 * result into an output buffer. It does not own the output buffer.
55 typedef struct alts_frame_reader {
56 unsigned char* output_buffer;
57 unsigned char header_buffer[kFrameHeaderSize];
58 size_t header_bytes_read;
59 size_t output_bytes_read;
60 size_t bytes_remaining;
64 * This method creates a frame writer instance and initializes its internal
67 alts_frame_writer* alts_create_frame_writer();
70 * This method resets internal states of a frame writer and prepares to write
71 * a single frame. It does not take ownership of payload_buffer.
72 * The payload_buffer must outlive the writer.
74 * - writer: a frame writer instance.
75 * - buffer: a buffer storing full payload data to be framed.
76 * - length: size of payload data.
78 * The method returns true on success and false otherwise.
80 bool alts_reset_frame_writer(alts_frame_writer* writer,
81 const unsigned char* buffer, size_t length);
84 * This method writes up to bytes_size bytes of a frame to output.
86 * - writer: a frame writer instance.
87 * - output: an output buffer used to store the frame.
88 * - bytes_size: an in/out parameter that stores the size of output buffer
89 * before the call, and gets written the number of frame bytes written to the
92 * The method returns true on success and false otherwise.
94 bool alts_write_frame_bytes(alts_frame_writer* writer, unsigned char* output,
98 * This method checks if a reset can be called to write a new frame. It returns
99 * true if it's the first time to frame a payload, or the current frame has
100 * been finished processing. It returns false if it's not ready yet to start a
101 * new frame (e.g., more payload data needs to be accumulated to process the
104 * if (alts_is_frame_writer_done(writer)) {
105 * // a new frame can be written, call reset.
106 * alts_reset_frame_writer(writer, payload_buffer, payload_size);
108 * // accumulate more payload data until a full frame can be written.
111 * - writer: a frame writer instance.
113 bool alts_is_frame_writer_done(alts_frame_writer* writer);
116 * This method returns the number of bytes left to write before a complete frame
119 * - writer: a frame writer instance.
121 size_t alts_get_num_writer_bytes_remaining(alts_frame_writer* writer);
124 * This method destroys a frame writer instance.
126 * - writer: a frame writer instance.
128 void alts_destroy_frame_writer(alts_frame_writer* writer);
131 * This method creates a frame reader instance and initializes its internal
134 alts_frame_reader* alts_create_frame_reader();
137 * This method resets internal states of a frame reader (including setting its
138 * output_buffer with buffer), and prepares to write processed bytes to
139 * an output_buffer. It does not take ownership of buffer. The buffer must
142 * - reader: a frame reader instance.
143 * - buffer: an output buffer used to store deframed results.
145 * The method returns true on success and false otherwise.
147 bool alts_reset_frame_reader(alts_frame_reader* reader, unsigned char* buffer);
150 * This method processes up to the number of bytes given in bytes_size. It may
151 * choose not to process all the bytes, if, for instance, more bytes are
152 * given to the method than required to complete the current frame.
154 * - reader: a frame reader instance.
155 * - bytes: a buffer that stores data to be processed.
156 * - bytes_size: an in/out parameter that stores the size of bytes before the
157 * call and gets written the number of bytes processed.
159 * The method returns true on success and false otherwise.
161 bool alts_read_frame_bytes(alts_frame_reader* reader,
162 const unsigned char* bytes, size_t* bytes_size);
165 * This method checks if a frame length has been read.
167 * - reader: a frame reader instance.
169 * The method returns true if a frame length has been read and false otherwise.
171 bool alts_has_read_frame_length(alts_frame_reader* reader);
174 * This method returns the number of bytes the frame reader intends to write.
175 * It may only be called if alts_has_read_frame_length() returns true.
177 * - reader: a frame reader instance.
179 size_t alts_get_reader_bytes_remaining(alts_frame_reader* reader);
182 * This method resets output_buffer but does not otherwise modify other internal
183 * states of a frame reader instance. After being set, the new output_buffer
184 * will hold the deframed payload held by the original output_buffer. It does
185 * not take ownership of buffer. The buffer must outlive the reader.
186 * To distinguish between two reset methods on a frame reader,
188 * if (alts_fh_is_frame_reader_done(reader)) {
189 * // if buffer contains a full payload to be deframed, call reset.
190 * alts_reset_frame_reader(reader, buffer);
193 * // if remaining buffer space is not enough to hold a full payload
194 * if (buffer_space_remaining < alts_get_reader_bytes_remaining(reader)) {
195 * // allocate enough space for a new buffer, copy back data processed so far,
197 * alts_reset_reader_output_buffer(reader, new_buffer).
200 * - reader: a frame reader instance.
201 * - buffer: a buffer used to set reader's output_buffer.
203 void alts_reset_reader_output_buffer(alts_frame_reader* reader,
204 unsigned char* buffer);
207 * This method checks if reset can be called to start processing a new frame.
208 * If true and reset was previously called, a full frame has been processed and
209 * the content of the frame is available in output_buffer.
211 * - reader: a frame reader instance.
213 bool alts_is_frame_reader_done(alts_frame_reader* reader);
216 * This method returns output_bytes_read of a frame reader instance.
218 * - reader: a frame reader instance.
220 size_t alts_get_output_bytes_read(alts_frame_reader* reader);
223 * This method returns output_buffer of a frame reader instance.
225 * - reader: a frame reader instance.
227 unsigned char* alts_get_output_buffer(alts_frame_reader* reader);
230 * This method destroys a frame reader instance.
232 * - reader: a frame reader instance.
234 void alts_destroy_frame_reader(alts_frame_reader* reader);
236 #endif /* GRPC_CORE_TSI_ALTS_FRAME_PROTECTOR_FRAME_HANDLER_H */