File: mqtt5_decoder.h

package info (click to toggle)
aws-crt-python 0.20.4%2Bdfsg-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 72,656 kB
  • sloc: ansic: 381,805; python: 23,008; makefile: 6,251; sh: 4,536; cpp: 699; ruby: 208; java: 77; perl: 73; javascript: 46; xml: 11
file content (264 lines) | stat: -rw-r--r-- 11,726 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

#ifndef AWS_MQTT_MQTT5_DECODER_H
#define AWS_MQTT_MQTT5_DECODER_H

#include <aws/mqtt/mqtt.h>

#include <aws/common/atomics.h>
#include <aws/common/byte_buf.h>
#include <aws/mqtt/private/v5/mqtt5_options_storage.h>
#include <aws/mqtt/v5/mqtt5_types.h>

struct aws_mqtt5_client;
struct aws_mqtt5_decoder;
struct aws_mqtt5_inbound_topic_alias_resolver;

/**
 * Overall decoder state.  We read the packet type and the remaining length, and then buffer the
 * entire packet before decoding.
 */
enum aws_mqtt5_decoder_state {
    AWS_MQTT5_DS_READ_PACKET_TYPE,
    AWS_MQTT5_DS_READ_REMAINING_LENGTH,
    AWS_MQTT5_DS_READ_PACKET,
    AWS_MQTT5_DS_FATAL_ERROR,
};

/*
 * Basic return value for a number of different decoding operations.  Error is always fatal and implies the
 * connection needs to be torn down.
 */
enum aws_mqtt5_decode_result_type {
    AWS_MQTT5_DRT_MORE_DATA,
    AWS_MQTT5_DRT_SUCCESS,
    AWS_MQTT5_DRT_ERROR,
};

/*
 * Callbacks the decoder should invoke.  We don't invoke functions directly on the client because
 * we want to test the decoder's correctness in isolation.
 */
typedef int(aws_mqtt5_on_packet_received_fn)(
    enum aws_mqtt5_packet_type type,
    void *packet_view,
    void *decoder_callback_user_data);

typedef int(aws_mqtt5_on_publish_payload_data_fn)(
    struct aws_mqtt5_packet_publish_view *publish_view,
    struct aws_byte_cursor payload,
    void *decoder_callback_user_data);

/**
 * per-packet-type decoding function signature
 */
typedef int(aws_mqtt5_decoding_fn)(struct aws_mqtt5_decoder *decoder);

/**
 * table of decoding functions.  Tests use an augmented version that includes decoders for packet types normally
 * only decoded by an mqtt server.
 */
struct aws_mqtt5_decoder_function_table {
    aws_mqtt5_decoding_fn *decoders_by_packet_type[16];
};

/**
 * Basic decoder configuration.
 */
struct aws_mqtt5_decoder_options {
    void *callback_user_data;
    aws_mqtt5_on_packet_received_fn *on_packet_received;
    const struct aws_mqtt5_decoder_function_table *decoder_table;
};

struct aws_mqtt5_decoder {
    struct aws_allocator *allocator;
    struct aws_mqtt5_decoder_options options;

    enum aws_mqtt5_decoder_state state;

    /*
     * decode scratch space: packets may get fully buffered here before decode
     * Exceptions:
     *   when the incoming io message buffer contains the entire packet, we decode directly from it instead
     */
    struct aws_byte_buf scratch_space;

    /*
     * packet type and flags
     */
    uint8_t packet_first_byte;

    uint32_t remaining_length;

    /*
     * Packet decoders work from this cursor.  It may point to scratch_space (for packets that were delivered
     * in more than one fragment) or to an io message buffer that contains the entire packet.
     */
    struct aws_byte_cursor packet_cursor;

    struct aws_mqtt5_inbound_topic_alias_resolver *topic_alias_resolver;
};

AWS_EXTERN_C_BEGIN

/**
 * One-time initialization for an mqtt5 decoder
 *
 * @param decoder decoder to initialize
 * @param allocator allocator to use for memory allocation
 * @param options configuration options
 * @return success/failure
 */
AWS_MQTT_API int aws_mqtt5_decoder_init(
    struct aws_mqtt5_decoder *decoder,
    struct aws_allocator *allocator,
    struct aws_mqtt5_decoder_options *options);

/**
 * Cleans up an mqtt5 decoder
 *
 * @param decoder decoder to clean up
 */
AWS_MQTT_API void aws_mqtt5_decoder_clean_up(struct aws_mqtt5_decoder *decoder);

/**
 * Resets the state of an mqtt5 decoder.  Used whenever a new connection is established
 *
 * @param decoder decoder to reset state for
 */
AWS_MQTT_API void aws_mqtt5_decoder_reset(struct aws_mqtt5_decoder *decoder);

/**
 * Basic entry point for all incoming mqtt5 data once the basic connection has been established
 *
 * @param decoder decoder to decode data with
 * @param data the data to decode
 * @return success/failure - failure implies a need to shut down the connection
 */
AWS_MQTT_API int aws_mqtt5_decoder_on_data_received(struct aws_mqtt5_decoder *decoder, struct aws_byte_cursor data);

/**
 * Sets the optional inbound alias resolver that the decoder should use during the lifetime of a connection
 *
 * @param decoder decoder to apply inbound topic alias resolution to
 * @param resolver inbound topic alias resolver
 */
AWS_MQTT_API void aws_mqtt5_decoder_set_inbound_topic_alias_resolver(
    struct aws_mqtt5_decoder *decoder,
    struct aws_mqtt5_inbound_topic_alias_resolver *resolver);

/**
 * Default decoding table; tests use an augmented version with decoders for packets that only the server needs to
 * decode.
 */
AWS_MQTT_API extern const struct aws_mqtt5_decoder_function_table *g_aws_mqtt5_default_decoder_table;

AWS_EXTERN_C_END

/* Decode helpers */

AWS_EXTERN_C_BEGIN

/**
 * Decodes, if possible, a variable length integer from a cursor.  If the decode is successful, the cursor is advanced
 * past the variable length integer encoding. This can be used both for streaming and non-streaming decode operations.
 *
 * @param cursor data to decode from
 * @param dest where to put a successfully decoded variable length integer
 * @return the result of attempting the decode: {success, error, not enough data}  Does not set aws_last_error.
 */
AWS_MQTT_API enum aws_mqtt5_decode_result_type aws_mqtt5_decode_vli(struct aws_byte_cursor *cursor, uint32_t *dest);

/**
 * Decodes an MQTT5 user property from a cursor
 *
 * @param packet_cursor data to decode from
 * @param properties property set to add the decoded property to
 * @return success/failure - failures implies connection termination
 */
AWS_MQTT_API int aws_mqtt5_decode_user_property(
    struct aws_byte_cursor *packet_cursor,
    struct aws_mqtt5_user_property_set *properties);

AWS_EXTERN_C_END

/* Decode helper macros operating on a cursor */

/*
 * u8 and u16 decode are a little different in order to support encoded values that are widened to larger storage.
 * To make that safe, we decode to a local and then assign the local to the final spot.  There should be no
 * complaints as long as the implicit conversion is the same size or wider.
 *
 * Some u8 examples include qos (one byte encode -> int-based enum) and various reason codes
 * Some u16 examples include cursor lengths decoded directly into a cursor's len field (u16 -> size_t)
 */
#define AWS_MQTT5_DECODE_U8(cursor_ptr, u8_ptr, error_label)                                                           \
    {                                                                                                                  \
        uint8_t decoded_value = 0;                                                                                     \
        if (!aws_byte_cursor_read_u8((cursor_ptr), (&decoded_value))) {                                                \
            goto error_label;                                                                                          \
        }                                                                                                              \
        *u8_ptr = decoded_value;                                                                                       \
    }

#define AWS_MQTT5_DECODE_U8_OPTIONAL(cursor_ptr, u8_ptr, u8_ptr_ptr, error_label)                                      \
    AWS_MQTT5_DECODE_U8(cursor_ptr, u8_ptr, error_label);                                                              \
    *(u8_ptr_ptr) = (u8_ptr);

#define AWS_MQTT5_DECODE_U16(cursor_ptr, u16_ptr, error_label)                                                         \
    {                                                                                                                  \
        uint16_t decoded_value = 0;                                                                                    \
        if (!aws_byte_cursor_read_be16((cursor_ptr), (&decoded_value))) {                                              \
            goto error_label;                                                                                          \
        }                                                                                                              \
        *u16_ptr = decoded_value;                                                                                      \
    }

/*
 * In addition to decoding a length prefix, this also verifies that the length prefix does not exceed the source
 * cursor length.
 */
#define AWS_MQTT5_DECODE_U16_PREFIX(cursor_ptr, u16_ptr, error_label)                                                  \
    AWS_MQTT5_DECODE_U16((cursor_ptr), (u16_ptr), error_label);                                                        \
    if (cursor_ptr->len < *(u16_ptr)) {                                                                                \
        aws_raise_error(AWS_ERROR_MQTT5_DECODE_PROTOCOL_ERROR);                                                        \
        goto error_label;                                                                                              \
    }

#define AWS_MQTT5_DECODE_U16_OPTIONAL(cursor_ptr, u16_ptr, u16_ptr_ptr, error_label)                                   \
    AWS_MQTT5_DECODE_U16((cursor_ptr), u16_ptr, error_label);                                                          \
    *(u16_ptr_ptr) = (u16_ptr);

#define AWS_MQTT5_DECODE_U32(cursor_ptr, u32_ptr, error_label)                                                         \
    if (!aws_byte_cursor_read_be32((cursor_ptr), (u32_ptr))) {                                                         \
        goto error_label;                                                                                              \
    }

#define AWS_MQTT5_DECODE_U32_OPTIONAL(cursor_ptr, u32_ptr, u32_ptr_ptr, error_label)                                   \
    AWS_MQTT5_DECODE_U32((cursor_ptr), u32_ptr, error_label);                                                          \
    *(u32_ptr_ptr) = (u32_ptr);

#define AWS_MQTT5_DECODE_VLI(cursor_ptr, u32_ptr, error_label)                                                         \
    if (AWS_MQTT5_DRT_SUCCESS != aws_mqtt5_decode_vli((cursor_ptr), (u32_ptr))) {                                      \
        goto error_label;                                                                                              \
    }

/* decodes both the length prefix and the following cursor field */
#define AWS_MQTT5_DECODE_LENGTH_PREFIXED_CURSOR(cursor_ptr, dest_cursor_ptr, error_label)                              \
    {                                                                                                                  \
        uint16_t prefix_length = 0;                                                                                    \
        AWS_MQTT5_DECODE_U16_PREFIX((cursor_ptr), &prefix_length, error_label)                                         \
                                                                                                                       \
        *(dest_cursor_ptr) = aws_byte_cursor_advance((cursor_ptr), prefix_length);                                     \
    }

#define AWS_MQTT5_DECODE_LENGTH_PREFIXED_CURSOR_OPTIONAL(                                                              \
    cursor_ptr, dest_cursor_ptr, dest_cursor_ptr_ptr, error_label)                                                     \
    AWS_MQTT5_DECODE_LENGTH_PREFIXED_CURSOR((cursor_ptr), (dest_cursor_ptr), error_label)                              \
    *(dest_cursor_ptr_ptr) = (dest_cursor_ptr);

#endif /* AWS_MQTT_MQTT5_DECODER_H */