Debian Codegraph @

crt/aws-c-mqtt/include/aws/mqtt/private/v5/mqtt5_encoder.h

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #ifndef AWS_MQTT_MQTT5_ENCODER_H
7 #define AWS_MQTT_MQTT5_ENCODER_H
8
9 #include <aws/mqtt/mqtt.h>
10
11 #include <aws/common/array_list.h>
12 #include <aws/common/byte_buf.h>
13
14 #include <aws/mqtt/v5/mqtt5_types.h>
15
16 struct aws_mqtt5_client;
17 struct aws_mqtt5_encoder;
18 struct aws_mqtt5_outbound_topic_alias_resolver;
19
20 /**
21 * We encode packets by looking at all of the packet's values/properties and building a sequence of encoding steps.
22 * Each encoding step is a simple, primitive operation of which there are two types:
23 * (1) encode an integer in some fashion (fixed width or variable length)
24 * (2) encode a raw sequence of bytes (either a cursor or a stream)
25 *
26 * Once the encoding step sequence is constructed, we do the actual encoding by iterating the sequence, performing
27 * the steps. This is interruptible/resumable, so we can perform encodings that span multiple buffers easily.
28 */
29 enum aws_mqtt5_encoding_step_type {
30 /* encode a single byte */
31 AWS_MQTT5_EST_U8,
32
33 /* encode a 16 bit unsigned integer in network order */
34 AWS_MQTT5_EST_U16,
35
36 /* encode a 32 bit unsigned integer in network order */
37 AWS_MQTT5_EST_U32,
38
39 /*
40 * encode a 32 bit unsigned integer using MQTT variable length encoding. It is assumed that the 32 bit value has
41 * already been checked against the maximum allowed value for variable length encoding.
42 */
43 AWS_MQTT5_EST_VLI,
44
45 /*
46 * encode an array of bytes as referenced by a cursor. Most of the time this step is paired with either a prefix
47 * specifying the number of bytes or a preceding variable length integer from which the data length can be
48 * computed.
49 */
50 AWS_MQTT5_EST_CURSOR,
51
52 /* encode a stream of bytes. The same context that applies to cursor encoding above also applies here. */
53 AWS_MQTT5_EST_STREAM,
54 };
55
56 /**
57 * Elemental unit of packet encoding.
58 */
59 struct aws_mqtt5_encoding_step {
60 enum aws_mqtt5_encoding_step_type type;
61 union {
62 uint8_t value_u8;
63 uint16_t value_u16;
64 uint32_t value_u32;
65 struct aws_byte_cursor value_cursor;
66 struct aws_input_stream *value_stream;
67 } value;
68 };
69
70 /**
71 * signature of a function that can takes a view assumed to be a specific packet type and appends the encoding
72 * steps necessary to encode that packet into the encoder
73 */
74 typedef int(aws_mqtt5_encode_begin_packet_type_fn)(struct aws_mqtt5_encoder *encoder, const void *view);
75
76 /**
77 * Per-packet-type table of encoding functions
78 */
79 struct aws_mqtt5_encoder_function_table {
80 aws_mqtt5_encode_begin_packet_type_fn *encoders_by_packet_type[16];
81 };
82
83 /**
84 * Configuration options for an mqtt5 encoder. Everything is optional at this time.
85 */
86 struct aws_mqtt5_encoder_options {
87 struct aws_mqtt5_client *client;
88 const struct aws_mqtt5_encoder_function_table *encoders;
89 };
90
91 /**
92 * An encoder is just a list of steps and a current location for the encoding process within that list.
93 */
94 struct aws_mqtt5_encoder {
95 struct aws_mqtt5_encoder_options config;
96
97 struct aws_array_list encoding_steps;
98 size_t current_encoding_step_index;
99
100 struct aws_mqtt5_outbound_topic_alias_resolver *topic_alias_resolver;
101 };
102
103 /**
104 * Encoding proceeds until either
105 * (1) a fatal error is reached
106 * (2) the steps are done
107 * (3) no room is left in the buffer
108 */
109 enum aws_mqtt5_encoding_result {
110 /*
111 * A fatal error state was reached during encoding. This forces a connection shut down with no DISCONNECT.
112 * An error can arise from several sources:
113 * (1) Bug in the encoder (length calculations, step calculations)
114 * (2) Bug in the view validation logic that is assumed to have caught any illegal/forbidden situations like
115 * values-too-big, etc...
116 * (3) System error when reading from a stream that is more than just a memory buffer
117 *
118 * Regardless of the origin, the connection is in an unusable state once this happens.
119 *
120 * If the encode function returns this value, aws last error will have an error value in it
121 */
122 AWS_MQTT5_ER_ERROR,
123
124 /* All encoding steps in the encoder have been completed. The encoder is ready for a new packet. */
125 AWS_MQTT5_ER_FINISHED,
126
127 /*
128 * The buffer has been filled as closely to full as possible and there are still encoding steps remaining that
129 * have not been completed. It is technically possible to hit a permanent out-of-room state if the buffer size
130 * is less than 4. Don't do that.
131 */
132 AWS_MQTT5_ER_OUT_OF_ROOM,
133 };
134
135 AWS_EXTERN_C_BEGIN
136
137 /**
138 * Initializes an mqtt5 encoder
139 *
140 * @param encoder encoder to initialize
141 * @param allocator allocator to use for all memory allocation
142 * @param options encoder configuration options to use
143 * @return
144 */
145 AWS_MQTT_API int aws_mqtt5_encoder_init(
146 struct aws_mqtt5_encoder *encoder,
147 struct aws_allocator *allocator,
148 struct aws_mqtt5_encoder_options *options);
149
150 /**
151 * Cleans up an mqtt5 encoder
152 *
153 * @param encoder encoder to free up all resources for
154 */
155 AWS_MQTT_API void aws_mqtt5_encoder_clean_up(struct aws_mqtt5_encoder *encoder);
156
157 /**
158 * Resets the state on an mqtt5 encoder. Ok to call after a failure to a packet _begin_packet() function. Not ok to
159 * call after a failed call to aws_mqtt5_encoder_encode_to_buffer()
160 *
161 * @param encoder encoder to reset
162 * @return
163 */
164 AWS_MQTT_API void aws_mqtt5_encoder_reset(struct aws_mqtt5_encoder *encoder);
165
166 /**
167 * Adds all of the primitive encoding steps necessary to encode an MQTT5 packet
168 *
169 * @param encoder encoder to add encoding steps to
170 * @param packet_type type of packet to encode
171 * @param packet_view view into the corresponding packet type
172 * @return success/failure
173 */
174 AWS_MQTT_API int aws_mqtt5_encoder_append_packet_encoding(
175 struct aws_mqtt5_encoder *encoder,
176 enum aws_mqtt5_packet_type packet_type,
177 const void *packet_view);
178
179 /*
180 * We intend that the client implementation only submits one packet at a time to the encoder, corresponding to the
181 * current operation of the client. This is an important property to maintain to allow us to correlate socket
182 * completions with packets/operations sent. It's the client's responsibility though; the encoder is dumb.
183 *
184 * The client will greedily use as much of an iomsg's buffer as it can if there are multiple operations (packets)
185 * queued and there is sufficient room.
186 */
187
188 /**
189 * Asks the encoder to encode as much as it possibly can into the supplied buffer.
190 *
191 * @param encoder encoder to do the encoding
192 * @param buffer where to encode into
193 * @return result of the encoding process. aws last error will be set appropriately.
194 */
195 AWS_MQTT_API enum aws_mqtt5_encoding_result aws_mqtt5_encoder_encode_to_buffer(
196 struct aws_mqtt5_encoder *encoder,
197 struct aws_byte_buf *buffer);
198
199 /**
200 * Sets the outbound alias resolver that the encoder should use during the lifetime of a connection
201 *
202 * @param encoder encoder to apply outbound topic alias resolution to
203 * @param resolver outbound topic alias resolver
204 */
205 AWS_MQTT_API void aws_mqtt5_encoder_set_outbound_topic_alias_resolver(
206 struct aws_mqtt5_encoder *encoder,
207 struct aws_mqtt5_outbound_topic_alias_resolver *resolver);
208
209 /**
210 * Default encoder table. Tests copy it and augment with additional functions in order to do round-trip encode-decode
211 * tests for packets that are only encoded on the server.
212 */
213 AWS_MQTT_API extern const struct aws_mqtt5_encoder_function_table *g_aws_mqtt5_encoder_default_function_table;
214
215 AWS_EXTERN_C_END
216
217 /******************************************************************************************************************
218 * Encoding helper functions and macros - placed in header so that test-only encoding has access
219 ******************************************************************************************************************/
220
221 AWS_EXTERN_C_BEGIN
222
223 /**
224 * Utility function to calculate the encoded packet size of a given packet view. Used to validate operations
225 * against the server's maximum packet size.
226 *
227 * @param packet_type type of packet the view represents
228 * @param packet_view packet view
229 * @param packet_size output parameter, set if the size was successfully calculated
230 * @return success/failure
231 */
232 AWS_MQTT_API int aws_mqtt5_packet_view_get_encoded_size(
233 enum aws_mqtt5_packet_type packet_type,
234 const void *packet_view,
235 size_t *packet_size);
236
237 /**
238 * Encodes a variable length integer to a buffer. Assumes the buffer has been checked for sufficient room (this
239 * is not a streaming/resumable operation)
240 *
241 * @param buf buffer to encode to
242 * @param value value to encode
243 * @return success/failure
244 */
245 AWS_MQTT_API int aws_mqtt5_encode_variable_length_integer(struct aws_byte_buf *buf, uint32_t value);
246
247 /**
248 * Computes how many bytes are necessary to encode a value as a variable length integer
249 * @param value value to encode
250 * @param encode_size output parameter for the encoding size
251 * @return success/failure where failure is exclusively value-is-illegal-and-too-large-to-encode
252 */
253 AWS_MQTT_API int aws_mqtt5_get_variable_length_encode_size(size_t value, size_t *encode_size);
254
255 AWS_MQTT_API void aws_mqtt5_encoder_push_step_u8(struct aws_mqtt5_encoder *encoder, uint8_t value);
256
257 AWS_MQTT_API void aws_mqtt5_encoder_push_step_u16(struct aws_mqtt5_encoder *encoder, uint16_t value);
258
259 AWS_MQTT_API void aws_mqtt5_encoder_push_step_u32(struct aws_mqtt5_encoder *encoder, uint32_t value);
260
261 AWS_MQTT_API int aws_mqtt5_encoder_push_step_vli(struct aws_mqtt5_encoder *encoder, uint32_t value);
262
263 AWS_MQTT_API void aws_mqtt5_encoder_push_step_cursor(struct aws_mqtt5_encoder *encoder, struct aws_byte_cursor value);
264
265 AWS_MQTT_API size_t aws_mqtt5_compute_user_property_encode_length(
266 const struct aws_mqtt5_user_property *properties,
267 size_t user_property_count);
268
269 AWS_MQTT_API void aws_mqtt5_add_user_property_encoding_steps(
270 struct aws_mqtt5_encoder *encoder,
271 const struct aws_mqtt5_user_property *user_properties,
272 size_t user_property_count);
273
274 AWS_EXTERN_C_END
275
276 /* macros to simplify encoding step list construction */
277
278 #define ADD_ENCODE_STEP_U8(encoder, value) aws_mqtt5_encoder_push_step_u8(encoder, (uint8_t)(value))
279 #define ADD_ENCODE_STEP_U16(encoder, value) aws_mqtt5_encoder_push_step_u16(encoder, (uint16_t)(value))
280 #define ADD_ENCODE_STEP_U32(encoder, value) aws_mqtt5_encoder_push_step_u32(encoder, (uint32_t)(value))
281 #define ADD_ENCODE_STEP_CURSOR(encoder, cursor) aws_mqtt5_encoder_push_step_cursor(encoder, (cursor))
282
283 #define ADD_ENCODE_STEP_VLI(encoder, value) \
284 if (aws_mqtt5_encoder_push_step_vli(encoder, (value))) { \
285 return AWS_OP_ERR; \
286 }
287
288 #define ADD_ENCODE_STEP_LENGTH_PREFIXED_CURSOR(encoder, cursor) \
289 { \
290 aws_mqtt5_encoder_push_step_u16(encoder, (uint16_t)((cursor).len)); \
291 aws_mqtt5_encoder_push_step_cursor(encoder, (cursor)); \
292 }
293
294 #define ADD_ENCODE_STEP_OPTIONAL_LENGTH_PREFIXED_CURSOR(encoder, cursor_ptr) \
295 if (cursor_ptr != NULL) { \
296 ADD_ENCODE_STEP_LENGTH_PREFIXED_CURSOR(encoder, *cursor_ptr); \
297 }
298
299 /* Property-oriented macros for encode steps. Properties have an additional prefix byte saying what their type is. */
300
301 #define ADD_ENCODE_STEP_OPTIONAL_U8_PROPERTY(encoder, property_value, value_ptr) \
302 if ((value_ptr) != NULL) { \
303 ADD_ENCODE_STEP_U8(encoder, property_value); \
304 ADD_ENCODE_STEP_U8(encoder, *(value_ptr)); \
305 }
306
307 #define ADD_ENCODE_STEP_OPTIONAL_U16_PROPERTY(encoder, property_value, value_ptr) \
308 if ((value_ptr) != NULL) { \
309 ADD_ENCODE_STEP_U8(encoder, property_value); \
310 ADD_ENCODE_STEP_U16(encoder, *(value_ptr)); \
311 }
312
313 #define ADD_ENCODE_STEP_OPTIONAL_U32_PROPERTY(encoder, property_value, value_ptr) \
314 if ((value_ptr) != NULL) { \
315 ADD_ENCODE_STEP_U8(encoder, property_value); \
316 ADD_ENCODE_STEP_U32(encoder, *(value_ptr)); \
317 }
318
319 #define ADD_ENCODE_STEP_OPTIONAL_VLI_PROPERTY(encoder, property_value, value_ptr) \
320 if ((value_ptr) != NULL) { \
321 ADD_ENCODE_STEP_U8(encoder, property_value); \
322 ADD_ENCODE_STEP_VLI(encoder, *(value_ptr)); \
323 }
324
325 #define ADD_ENCODE_STEP_OPTIONAL_CURSOR_PROPERTY(encoder, property_type, cursor_ptr) \
326 if ((cursor_ptr) != NULL) { \
327 ADD_ENCODE_STEP_U8(encoder, property_type); \
328 ADD_ENCODE_STEP_U16(encoder, (cursor_ptr)->len); \
329 ADD_ENCODE_STEP_CURSOR(encoder, *(cursor_ptr)); \
330 }
331
332 /*
333 * Macros to simplify packet size calculations, which are significantly complicated by mqtt5's many optional
334 * properties.
335 */
336
337 #define ADD_OPTIONAL_U8_PROPERTY_LENGTH(property_ptr, length) \
338 if ((property_ptr) != NULL) { \
339 (length) += 2; \
340 }
341
342 #define ADD_OPTIONAL_U16_PROPERTY_LENGTH(property_ptr, length) \
343 if ((property_ptr) != NULL) { \
344 (length) += 3; \
345 }
346
347 #define ADD_OPTIONAL_U32_PROPERTY_LENGTH(property_ptr, length) \
348 if ((property_ptr) != NULL) { \
349 (length) += 5; \
350 }
351
352 #define ADD_OPTIONAL_CURSOR_PROPERTY_LENGTH(property_ptr, length) \
353 if ((property_ptr) != NULL) { \
354 (length) += 3 + ((property_ptr)->len); \
355 }
356
357 #endif /* AWS_MQTT_MQTT5_ENCODER_H */