Debian Codegraph @

crt/aws-c-mqtt/include/aws/mqtt/private/v5/mqtt5_decoder.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_DECODER_H
7 #define AWS_MQTT_MQTT5_DECODER_H
8
9 #include <aws/mqtt/mqtt.h>
10
11 #include <aws/common/atomics.h>
12 #include <aws/common/byte_buf.h>
13 #include <aws/mqtt/private/v5/mqtt5_options_storage.h>
14 #include <aws/mqtt/v5/mqtt5_types.h>
15
16 struct aws_mqtt5_client;
17 struct aws_mqtt5_decoder;
18 struct aws_mqtt5_inbound_topic_alias_resolver;
19
20 /**
21 * Overall decoder state. We read the packet type and the remaining length, and then buffer the
22 * entire packet before decoding.
23 */
24 enum aws_mqtt5_decoder_state {
25 AWS_MQTT5_DS_READ_PACKET_TYPE,
26 AWS_MQTT5_DS_READ_REMAINING_LENGTH,
27 AWS_MQTT5_DS_READ_PACKET,
28 AWS_MQTT5_DS_FATAL_ERROR,
29 };
30
31 /*
32 * Basic return value for a number of different decoding operations. Error is always fatal and implies the
33 * connection needs to be torn down.
34 */
35 enum aws_mqtt5_decode_result_type {
36 AWS_MQTT5_DRT_MORE_DATA,
37 AWS_MQTT5_DRT_SUCCESS,
38 AWS_MQTT5_DRT_ERROR,
39 };
40
41 /*
42 * Callbacks the decoder should invoke. We don't invoke functions directly on the client because
43 * we want to test the decoder's correctness in isolation.
44 */
45 typedef int(aws_mqtt5_on_packet_received_fn)(
46 enum aws_mqtt5_packet_type type,
47 void *packet_view,
48 void *decoder_callback_user_data);
49
50 typedef int(aws_mqtt5_on_publish_payload_data_fn)(
51 struct aws_mqtt5_packet_publish_view *publish_view,
52 struct aws_byte_cursor payload,
53 void *decoder_callback_user_data);
54
55 /**
56 * per-packet-type decoding function signature
57 */
58 typedef int(aws_mqtt5_decoding_fn)(struct aws_mqtt5_decoder *decoder);
59
60 /**
61 * table of decoding functions. Tests use an augmented version that includes decoders for packet types normally
62 * only decoded by an mqtt server.
63 */
64 struct aws_mqtt5_decoder_function_table {
65 aws_mqtt5_decoding_fn *decoders_by_packet_type[16];
66 };
67
68 /**
69 * Basic decoder configuration.
70 */
71 struct aws_mqtt5_decoder_options {
72 void *callback_user_data;
73 aws_mqtt5_on_packet_received_fn *on_packet_received;
74 const struct aws_mqtt5_decoder_function_table *decoder_table;
75 };
76
77 struct aws_mqtt5_decoder {
78 struct aws_allocator *allocator;
79 struct aws_mqtt5_decoder_options options;
80
81 enum aws_mqtt5_decoder_state state;
82
83 /*
84 * decode scratch space: packets may get fully buffered here before decode
85 * Exceptions:
86 * when the incoming io message buffer contains the entire packet, we decode directly from it instead
87 */
88 struct aws_byte_buf scratch_space;
89
90 /*
91 * packet type and flags
92 */
93 uint8_t packet_first_byte;
94
95 uint32_t remaining_length;
96
97 /*
98 * Packet decoders work from this cursor. It may point to scratch_space (for packets that were delivered
99 * in more than one fragment) or to an io message buffer that contains the entire packet.
100 */
101 struct aws_byte_cursor packet_cursor;
102
103 struct aws_mqtt5_inbound_topic_alias_resolver *topic_alias_resolver;
104 };
105
106 AWS_EXTERN_C_BEGIN
107
108 /**
109 * One-time initialization for an mqtt5 decoder
110 *
111 * @param decoder decoder to initialize
112 * @param allocator allocator to use for memory allocation
113 * @param options configuration options
114 * @return success/failure
115 */
116 AWS_MQTT_API int aws_mqtt5_decoder_init(
117 struct aws_mqtt5_decoder *decoder,
118 struct aws_allocator *allocator,
119 struct aws_mqtt5_decoder_options *options);
120
121 /**
122 * Cleans up an mqtt5 decoder
123 *
124 * @param decoder decoder to clean up
125 */
126 AWS_MQTT_API void aws_mqtt5_decoder_clean_up(struct aws_mqtt5_decoder *decoder);
127
128 /**
129 * Resets the state of an mqtt5 decoder. Used whenever a new connection is established
130 *
131 * @param decoder decoder to reset state for
132 */
133 AWS_MQTT_API void aws_mqtt5_decoder_reset(struct aws_mqtt5_decoder *decoder);
134
135 /**
136 * Basic entry point for all incoming mqtt5 data once the basic connection has been established
137 *
138 * @param decoder decoder to decode data with
139 * @param data the data to decode
140 * @return success/failure - failure implies a need to shut down the connection
141 */
142 AWS_MQTT_API int aws_mqtt5_decoder_on_data_received(struct aws_mqtt5_decoder *decoder, struct aws_byte_cursor data);
143
144 /**
145 * Sets the optional inbound alias resolver that the decoder should use during the lifetime of a connection
146 *
147 * @param decoder decoder to apply inbound topic alias resolution to
148 * @param resolver inbound topic alias resolver
149 */
150 AWS_MQTT_API void aws_mqtt5_decoder_set_inbound_topic_alias_resolver(
151 struct aws_mqtt5_decoder *decoder,
152 struct aws_mqtt5_inbound_topic_alias_resolver *resolver);
153
154 /**
155 * Default decoding table; tests use an augmented version with decoders for packets that only the server needs to
156 * decode.
157 */
158 AWS_MQTT_API extern const struct aws_mqtt5_decoder_function_table *g_aws_mqtt5_default_decoder_table;
159
160 AWS_EXTERN_C_END
161
162 /* Decode helpers */
163
164 AWS_EXTERN_C_BEGIN
165
166 /**
167 * Decodes, if possible, a variable length integer from a cursor. If the decode is successful, the cursor is advanced
168 * past the variable length integer encoding. This can be used both for streaming and non-streaming decode operations.
169 *
170 * @param cursor data to decode from
171 * @param dest where to put a successfully decoded variable length integer
172 * @return the result of attempting the decode: {success, error, not enough data} Does not set aws_last_error.
173 */
174 AWS_MQTT_API enum aws_mqtt5_decode_result_type aws_mqtt5_decode_vli(struct aws_byte_cursor *cursor, uint32_t *dest);
175
176 /**
177 * Decodes an MQTT5 user property from a cursor
178 *
179 * @param packet_cursor data to decode from
180 * @param properties property set to add the decoded property to
181 * @return success/failure - failures implies connection termination
182 */
183 AWS_MQTT_API int aws_mqtt5_decode_user_property(
184 struct aws_byte_cursor *packet_cursor,
185 struct aws_mqtt5_user_property_set *properties);
186
187 AWS_EXTERN_C_END
188
189 /* Decode helper macros operating on a cursor */
190
191 /*
192 * u8 and u16 decode are a little different in order to support encoded values that are widened to larger storage.
193 * To make that safe, we decode to a local and then assign the local to the final spot. There should be no
194 * complaints as long as the implicit conversion is the same size or wider.
195 *
196 * Some u8 examples include qos (one byte encode -> int-based enum) and various reason codes
197 * Some u16 examples include cursor lengths decoded directly into a cursor's len field (u16 -> size_t)
198 */
199 #define AWS_MQTT5_DECODE_U8(cursor_ptr, u8_ptr, error_label) \
200 { \
201 uint8_t decoded_value = 0; \
202 if (!aws_byte_cursor_read_u8((cursor_ptr), (&decoded_value))) { \
203 goto error_label; \
204 } \
205 *u8_ptr = decoded_value; \
206 }
207
208 #define AWS_MQTT5_DECODE_U8_OPTIONAL(cursor_ptr, u8_ptr, u8_ptr_ptr, error_label) \
209 AWS_MQTT5_DECODE_U8(cursor_ptr, u8_ptr, error_label); \
210 *(u8_ptr_ptr) = (u8_ptr);
211
212 #define AWS_MQTT5_DECODE_U16(cursor_ptr, u16_ptr, error_label) \
213 { \
214 uint16_t decoded_value = 0; \
215 if (!aws_byte_cursor_read_be16((cursor_ptr), (&decoded_value))) { \
216 goto error_label; \
217 } \
218 *u16_ptr = decoded_value; \
219 }
220
221 /*
222 * In addition to decoding a length prefix, this also verifies that the length prefix does not exceed the source
223 * cursor length.
224 */
225 #define AWS_MQTT5_DECODE_U16_PREFIX(cursor_ptr, u16_ptr, error_label) \
226 AWS_MQTT5_DECODE_U16((cursor_ptr), (u16_ptr), error_label); \
227 if (cursor_ptr->len < *(u16_ptr)) { \
228 aws_raise_error(AWS_ERROR_MQTT5_DECODE_PROTOCOL_ERROR); \
229 goto error_label; \
230 }
231
232 #define AWS_MQTT5_DECODE_U16_OPTIONAL(cursor_ptr, u16_ptr, u16_ptr_ptr, error_label) \
233 AWS_MQTT5_DECODE_U16((cursor_ptr), u16_ptr, error_label); \
234 *(u16_ptr_ptr) = (u16_ptr);
235
236 #define AWS_MQTT5_DECODE_U32(cursor_ptr, u32_ptr, error_label) \
237 if (!aws_byte_cursor_read_be32((cursor_ptr), (u32_ptr))) { \
238 goto error_label; \
239 }
240
241 #define AWS_MQTT5_DECODE_U32_OPTIONAL(cursor_ptr, u32_ptr, u32_ptr_ptr, error_label) \
242 AWS_MQTT5_DECODE_U32((cursor_ptr), u32_ptr, error_label); \
243 *(u32_ptr_ptr) = (u32_ptr);
244
245 #define AWS_MQTT5_DECODE_VLI(cursor_ptr, u32_ptr, error_label) \
246 if (AWS_MQTT5_DRT_SUCCESS != aws_mqtt5_decode_vli((cursor_ptr), (u32_ptr))) { \
247 goto error_label; \
248 }
249
250 /* decodes both the length prefix and the following cursor field */
251 #define AWS_MQTT5_DECODE_LENGTH_PREFIXED_CURSOR(cursor_ptr, dest_cursor_ptr, error_label) \
252 { \
253 uint16_t prefix_length = 0; \
254 AWS_MQTT5_DECODE_U16_PREFIX((cursor_ptr), &prefix_length, error_label) \
255 \
256 *(dest_cursor_ptr) = aws_byte_cursor_advance((cursor_ptr), prefix_length); \
257 }
258
259 #define AWS_MQTT5_DECODE_LENGTH_PREFIXED_CURSOR_OPTIONAL( \
260 cursor_ptr, dest_cursor_ptr, dest_cursor_ptr_ptr, error_label) \
261 AWS_MQTT5_DECODE_LENGTH_PREFIXED_CURSOR((cursor_ptr), (dest_cursor_ptr), error_label) \
262 *(dest_cursor_ptr_ptr) = (dest_cursor_ptr);
263
264 #endif /* AWS_MQTT_MQTT5_DECODER_H */