| 1 | #ifndef AWS_MQTT_PRIVATE_MQTT311_DECODER_H |
| 2 | #define AWS_MQTT_PRIVATE_MQTT311_DECODER_H |
| 3 | |
| 4 | /** |
| 5 | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 6 | * SPDX-License-Identifier: Apache-2.0. |
| 7 | */ |
| 8 | |
| 9 | #include <aws/mqtt/mqtt.h> |
| 10 | |
| 11 | #include <aws/common/byte_buf.h> |
| 12 | |
| 13 | /* |
| 14 | * Per-packet-type callback signature. message_cursor contains the entire packet's data. |
| 15 | */ |
| 16 | typedef int(packet_handler_fn)(struct aws_byte_cursor message_cursor, void *user_data); |
| 17 | |
| 18 | /* |
| 19 | * Wrapper for a set of packet handlers for each possible MQTT packet type. Some values are invalid in 311 (15), and |
| 20 | * some values are only valid from the perspective of the server or client. |
| 21 | */ |
| 22 | struct aws_mqtt_client_connection_packet_handlers { |
| 23 | packet_handler_fn *handlers_by_packet_type[16]; |
| 24 | }; |
| 25 | |
| 26 | /* |
| 27 | * Internal state of the 311 decoder/framing logic. |
| 28 | * |
| 29 | * When a packet is fragmented across multiple io buffers, state moves circularly: |
| 30 | * first byte -> remaining length -> body -> first byte etc... |
| 31 | * |
| 32 | * When a packet is completely contained inside a single io buffer, the entire packet is processed within |
| 33 | * the READ_FIRST_BYTE state. |
| 34 | */ |
| 35 | enum aws_mqtt_311_decoder_state_type { |
| 36 | |
| 37 | /* |
| 38 | * The decoder is expecting the first byte of the fixed header of an MQTT control packet |
| 39 | */ |
| 40 | AWS_MDST_READ_FIRST_BYTE, |
| 41 | |
| 42 | /* |
| 43 | * The decoder is expecting the vli-encoded total remaining length field of the fixed header on an MQTT control |
| 44 | * packet. |
| 45 | */ |
| 46 | AWS_MDST_READ_REMAINING_LENGTH, |
| 47 | |
| 48 | /* |
| 49 | * The decoder is expecting the "rest" of the MQTT packet's data based on the remaining length value that has |
| 50 | * already been read. |
| 51 | */ |
| 52 | AWS_MDST_READ_BODY, |
| 53 | |
| 54 | /* |
| 55 | * Terminal state for when a protocol error has been encountered by the decoder. The only way to leave this |
| 56 | * state is to reset the decoder via the aws_mqtt311_decoder_reset_for_new_connection() API. |
| 57 | */ |
| 58 | AWS_MDST_PROTOCOL_ERROR, |
| 59 | }; |
| 60 | |
| 61 | /* |
| 62 | * Configuration options for the decoder. When used by the actual implementation, handler_user_data is the |
| 63 | * connection object and the packet handlers are channel functions that hook into reactionary behavior and user |
| 64 | * callbacks. |
| 65 | */ |
| 66 | struct aws_mqtt311_decoder_options { |
| 67 | const struct aws_mqtt_client_connection_packet_handlers *packet_handlers; |
| 68 | void *handler_user_data; |
| 69 | }; |
| 70 | |
| 71 | /* |
| 72 | * Simple MQTT311 decoder structure. Actual decoding is deferred to per-packet functions that expect the whole |
| 73 | * packet in a buffer. The primary function of this sub-system is correctly framing a stream of bytes into the |
| 74 | * constituent packets. |
| 75 | */ |
| 76 | struct aws_mqtt311_decoder { |
| 77 | struct aws_mqtt311_decoder_options config; |
| 78 | |
| 79 | enum aws_mqtt_311_decoder_state_type state; |
| 80 | |
| 81 | /* |
| 82 | * If zero, not valid. If non-zero, represents the number of bytes that need to be read to finish the packet. |
| 83 | * This includes the total encoding size of the fixed header. |
| 84 | */ |
| 85 | size_t total_packet_length; |
| 86 | |
| 87 | /* scratch buffer to hold individual packets when they fragment across incoming data frame boundaries */ |
| 88 | struct aws_byte_buf packet_buffer; |
| 89 | }; |
| 90 | |
| 91 | AWS_EXTERN_C_BEGIN |
| 92 | |
| 93 | /** |
| 94 | * Initialize function for the MQTT311 decoder |
| 95 | * |
| 96 | * @param decoder decoder to initialize |
| 97 | * @param allocator memory allocator to use |
| 98 | * @param options additional decoder configuration options |
| 99 | */ |
| 100 | AWS_MQTT_API void aws_mqtt311_decoder_init( |
| 101 | struct aws_mqtt311_decoder *decoder, |
| 102 | struct aws_allocator *allocator, |
| 103 | const struct aws_mqtt311_decoder_options *options); |
| 104 | |
| 105 | /** |
| 106 | * Clean up function for an MQTT311 decoder |
| 107 | * |
| 108 | * @param decoder decoder to release resources for |
| 109 | */ |
| 110 | AWS_MQTT_API void aws_mqtt311_decoder_clean_up(struct aws_mqtt311_decoder *decoder); |
| 111 | |
| 112 | /** |
| 113 | * Callback function to decode the incoming data stream of an MQTT311 connection. Handles packet framing and |
| 114 | * correct decoder/handler function dispatch. |
| 115 | * |
| 116 | * @param decoder decoder to decode with |
| 117 | * @param data raw plaintext bytes of a connection operating on the MQTT311 protocol |
| 118 | * @return success/failure, failure represents a protocol error and implies the connection must be shut down |
| 119 | */ |
| 120 | AWS_MQTT_API int aws_mqtt311_decoder_on_bytes_received( |
| 121 | struct aws_mqtt311_decoder *decoder, |
| 122 | struct aws_byte_cursor data); |
| 123 | |
| 124 | /** |
| 125 | * Resets a decoder's state to its initial values. If using a decoder across multiple network connections (within |
| 126 | * the same client), you must invoke this when setting up a new connection, before any MQTT protocol bytes are |
| 127 | * processed. Breaks the decoder out of any previous protocol error terminal state. |
| 128 | * |
| 129 | * @param decoder decoder to reset |
| 130 | */ |
| 131 | AWS_MQTT_API void aws_mqtt311_decoder_reset_for_new_connection(struct aws_mqtt311_decoder *decoder); |
| 132 | |
| 133 | AWS_EXTERN_C_END |
| 134 | |
| 135 | #endif /* AWS_MQTT_PRIVATE_MQTT311_DECODER_H */ |