Debian Codegraph @

crt/aws-c-mqtt/source/mqtt311_decoder.c

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 #include <aws/mqtt/private/mqtt311_decoder.h>
7
8 #include <aws/mqtt/private/fixed_header.h>
9
10 static void s_aws_mqtt311_decoder_reset(struct aws_mqtt311_decoder *decoder) {
11 decoder->state = AWS_MDST_READ_FIRST_BYTE;
12 decoder->total_packet_length = 0;
13 aws_byte_buf_reset(&decoder->packet_buffer, false);
14 }
15
16 void aws_mqtt311_decoder_init(
17 struct aws_mqtt311_decoder *decoder,
18 struct aws_allocator *allocator,
19 const struct aws_mqtt311_decoder_options *options) {
20
21 aws_byte_buf_init(&decoder->packet_buffer, allocator, 5);
22 decoder->config = *options;
23
24 s_aws_mqtt311_decoder_reset(decoder);
25 }
26
27 void aws_mqtt311_decoder_clean_up(struct aws_mqtt311_decoder *decoder) {
28 aws_byte_buf_clean_up(&decoder->packet_buffer);
29 }
30
31 static void s_aws_mqtt311_decoder_reset_for_new_packet(struct aws_mqtt311_decoder *decoder) {
32 if (decoder->state != AWS_MDST_PROTOCOL_ERROR) {
33 s_aws_mqtt311_decoder_reset(decoder);
34 }
35 }
36
37 enum aws_mqtt311_decoding_directive { AWS_MDD_CONTINUE, AWS_MDD_OUT_OF_DATA, AWS_MDD_PROTOCOL_ERROR };
38
39 static enum aws_mqtt311_decoding_directive aws_result_to_mqtt311_decoding_directive(int result) {
40 return (result == AWS_OP_SUCCESS) ? AWS_MDD_CONTINUE : AWS_MDD_PROTOCOL_ERROR;
41 }
42
43 static int s_aws_mqtt311_decoder_safe_packet_handle(
44 struct aws_mqtt311_decoder *decoder,
45 enum aws_mqtt_packet_type packet_type,
46 struct aws_byte_cursor packet_cursor) {
47 packet_handler_fn *handler = decoder->config.packet_handlers->handlers_by_packet_type[packet_type];
48 if (handler != NULL) {
49 return handler(packet_cursor, decoder->config.handler_user_data);
50 } else {
51 return aws_raise_error(AWS_ERROR_MQTT_PROTOCOL_ERROR);
52 }
53 }
54
55 static enum aws_mqtt311_decoding_directive s_handle_decoder_read_first_byte(
56 struct aws_mqtt311_decoder *decoder,
57 struct aws_byte_cursor *data) {
58 AWS_FATAL_ASSERT(decoder->packet_buffer.len == 0);
59 if (data->len == 0) {
60 return AWS_MDD_OUT_OF_DATA;
61 }
62
63 /*
64 * Do a greedy check to see if the whole MQTT packet is contained within the received data. If it is, decode it
65 * directly from the incoming data cursor without buffering it first. Otherwise, the packet is fragmented
66 * across multiple received data calls, and so we must use the packet buffer and copy everything first via the
67 * full decoder state machine.
68 *
69 * A corollary of this is that the decoder is only ever in the AWS_MDST_READ_REMAINING_LENGTH or AWS_MDST_READ_BODY
70 * states if the current MQTT packet was received in a fragmented manner.
71 */
72 struct aws_byte_cursor temp_cursor = *data;
73 struct aws_mqtt_fixed_header packet_header;
74 AWS_ZERO_STRUCT(packet_header);
75 if (!aws_mqtt_fixed_header_decode(&temp_cursor, &packet_header) &&
76 temp_cursor.len >= packet_header.remaining_length) {
77
78 /* figure out the cursor that spans the full packet */
79 size_t fixed_header_length = temp_cursor.ptr - data->ptr;
80 struct aws_byte_cursor whole_packet_cursor = *data;
81 whole_packet_cursor.len = fixed_header_length + packet_header.remaining_length;
82
83 /* advance the external, mutable data cursor to the start of the next packet */
84 aws_byte_cursor_advance(data, whole_packet_cursor.len);
85
86 /*
87 * if this fails, the decoder goes into an error state. If it succeeds we'll loop again into the same state
88 * because we'll be back at the beginning of the next packet (if it exists).
89 */
90 enum aws_mqtt_packet_type packet_type = aws_mqtt_get_packet_type(whole_packet_cursor.ptr);
91 return aws_result_to_mqtt311_decoding_directive(
92 s_aws_mqtt311_decoder_safe_packet_handle(decoder, packet_type, whole_packet_cursor));
93 }
94
95 /*
96 * The packet is fragmented, spanning more than this io message. So we buffer it and use the
97 * simple state machine to decode.
98 */
99 uint8_t byte = *data->ptr;
100 aws_byte_cursor_advance(data, 1);
101 aws_byte_buf_append_byte_dynamic(&decoder->packet_buffer, byte);
102
103 decoder->state = AWS_MDST_READ_REMAINING_LENGTH;
104
105 return AWS_MDD_CONTINUE;
106 }
107
108 static enum aws_mqtt311_decoding_directive s_handle_decoder_read_remaining_length(
109 struct aws_mqtt311_decoder *decoder,
110 struct aws_byte_cursor *data) {
111 AWS_FATAL_ASSERT(decoder->total_packet_length == 0);
112 if (data->len == 0) {
113 return AWS_MDD_OUT_OF_DATA;
114 }
115
116 uint8_t byte = *data->ptr;
117 aws_byte_cursor_advance(data, 1);
118 aws_byte_buf_append_byte_dynamic(&decoder->packet_buffer, byte);
119
120 struct aws_byte_cursor vli_cursor = aws_byte_cursor_from_buf(&decoder->packet_buffer);
121 aws_byte_cursor_advance(&vli_cursor, 1);
122
123 size_t remaining_length = 0;
124 if (aws_mqtt311_decode_remaining_length(&vli_cursor, &remaining_length) == AWS_OP_ERR) {
125 /* anything other than a short buffer error (not enough data yet) is a terminal error */
126 if (aws_last_error() == AWS_ERROR_SHORT_BUFFER) {
127 return AWS_MDD_CONTINUE;
128 } else {
129 return AWS_MDD_PROTOCOL_ERROR;
130 }
131 }
132
133 /*
134 * If we successfully decoded a variable-length integer, we now know exactly how many bytes we need to receive in
135 * order to have the full packet.
136 */
137 decoder->total_packet_length = remaining_length + decoder->packet_buffer.len;
138 AWS_FATAL_ASSERT(decoder->total_packet_length > 0);
139 decoder->state = AWS_MDST_READ_BODY;
140
141 return AWS_MDD_CONTINUE;
142 }
143
144 static enum aws_mqtt311_decoding_directive s_handle_decoder_read_body(
145 struct aws_mqtt311_decoder *decoder,
146 struct aws_byte_cursor *data) {
147 AWS_FATAL_ASSERT(decoder->total_packet_length > 0);
148
149 size_t buffer_length = decoder->packet_buffer.len;
150 size_t amount_to_read = aws_min_size(decoder->total_packet_length - buffer_length, data->len);
151
152 struct aws_byte_cursor copy_cursor = aws_byte_cursor_advance(data, amount_to_read);
153 aws_byte_buf_append_dynamic(&decoder->packet_buffer, &copy_cursor);
154
155 if (decoder->packet_buffer.len == decoder->total_packet_length) {
156
157 /* We have the full packet in the scratch buffer, invoke the correct handler to decode and process it */
158 struct aws_byte_cursor packet_data = aws_byte_cursor_from_buf(&decoder->packet_buffer);
159 enum aws_mqtt_packet_type packet_type = aws_mqtt_get_packet_type(packet_data.ptr);
160 if (s_aws_mqtt311_decoder_safe_packet_handle(decoder, packet_type, packet_data) == AWS_OP_ERR) {
161 return AWS_MDD_PROTOCOL_ERROR;
162 }
163
164 s_aws_mqtt311_decoder_reset_for_new_packet(decoder);
165 return AWS_MDD_CONTINUE;
166 }
167
168 return AWS_MDD_OUT_OF_DATA;
169 }
170
171 int aws_mqtt311_decoder_on_bytes_received(struct aws_mqtt311_decoder *decoder, struct aws_byte_cursor data) {
172 struct aws_byte_cursor data_cursor = data;
173
174 enum aws_mqtt311_decoding_directive decode_directive = AWS_MDD_CONTINUE;
175 while (decode_directive == AWS_MDD_CONTINUE) {
176 switch (decoder->state) {
177 case AWS_MDST_READ_FIRST_BYTE:
178 decode_directive = s_handle_decoder_read_first_byte(decoder, &data_cursor);
179 break;
180
181 case AWS_MDST_READ_REMAINING_LENGTH:
182 decode_directive = s_handle_decoder_read_remaining_length(decoder, &data_cursor);
183 break;
184
185 case AWS_MDST_READ_BODY:
186 decode_directive = s_handle_decoder_read_body(decoder, &data_cursor);
187 break;
188
189 default:
190 decode_directive = AWS_MDD_PROTOCOL_ERROR;
191 break;
192 }
193
194 /*
195 * Protocol error is a terminal failure state until aws_mqtt311_decoder_reset_for_new_connection() is called.
196 */
197 if (decode_directive == AWS_MDD_PROTOCOL_ERROR) {
198 decoder->state = AWS_MDST_PROTOCOL_ERROR;
199 if (aws_last_error() == AWS_ERROR_SUCCESS) {
200 aws_raise_error(AWS_ERROR_MQTT_PROTOCOL_ERROR);
201 }
202 return AWS_OP_ERR;
203 }
204 }
205
206 return AWS_OP_SUCCESS;
207 }
208
209 void aws_mqtt311_decoder_reset_for_new_connection(struct aws_mqtt311_decoder *decoder) {
210 s_aws_mqtt311_decoder_reset(decoder);
211 }