Debian Codegraph @

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

raw symbols SCIP
1 #ifndef AWS_MQTT_MQTT5_UTILS_H
2 #define AWS_MQTT_MQTT5_UTILS_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/mqtt/v5/mqtt5_client.h>
12 #include <aws/mqtt/v5/mqtt5_types.h>
13
14 struct aws_byte_buf;
15 struct aws_mqtt5_negotiated_settings;
16
17 #define AWS_MQTT5_MAXIMUM_VARIABLE_LENGTH_INTEGER 268435455
18 #define AWS_MQTT5_MAXIMUM_PACKET_SIZE (5 + AWS_MQTT5_MAXIMUM_VARIABLE_LENGTH_INTEGER)
19 #define AWS_MQTT5_RECEIVE_MAXIMUM 65535
20 #define AWS_MQTT5_PINGREQ_ENCODED_SIZE 2
21
22 /* property type codes */
23 #define AWS_MQTT5_PROPERTY_TYPE_PAYLOAD_FORMAT_INDICATOR ((uint8_t)1)
24 #define AWS_MQTT5_PROPERTY_TYPE_MESSAGE_EXPIRY_INTERVAL ((uint8_t)2)
25 #define AWS_MQTT5_PROPERTY_TYPE_CONTENT_TYPE ((uint8_t)3)
26 #define AWS_MQTT5_PROPERTY_TYPE_RESPONSE_TOPIC ((uint8_t)8)
27 #define AWS_MQTT5_PROPERTY_TYPE_CORRELATION_DATA ((uint8_t)9)
28 #define AWS_MQTT5_PROPERTY_TYPE_SUBSCRIPTION_IDENTIFIER ((uint8_t)11)
29 #define AWS_MQTT5_PROPERTY_TYPE_SESSION_EXPIRY_INTERVAL ((uint8_t)17)
30 #define AWS_MQTT5_PROPERTY_TYPE_ASSIGNED_CLIENT_IDENTIFIER ((uint8_t)18)
31 #define AWS_MQTT5_PROPERTY_TYPE_SERVER_KEEP_ALIVE ((uint8_t)19)
32 #define AWS_MQTT5_PROPERTY_TYPE_AUTHENTICATION_METHOD ((uint8_t)21)
33 #define AWS_MQTT5_PROPERTY_TYPE_AUTHENTICATION_DATA ((uint8_t)22)
34 #define AWS_MQTT5_PROPERTY_TYPE_REQUEST_PROBLEM_INFORMATION ((uint8_t)23)
35 #define AWS_MQTT5_PROPERTY_TYPE_WILL_DELAY_INTERVAL ((uint8_t)24)
36 #define AWS_MQTT5_PROPERTY_TYPE_REQUEST_RESPONSE_INFORMATION ((uint8_t)25)
37 #define AWS_MQTT5_PROPERTY_TYPE_RESPONSE_INFORMATION ((uint8_t)26)
38 #define AWS_MQTT5_PROPERTY_TYPE_SERVER_REFERENCE ((uint8_t)28)
39 #define AWS_MQTT5_PROPERTY_TYPE_REASON_STRING ((uint8_t)31)
40 #define AWS_MQTT5_PROPERTY_TYPE_RECEIVE_MAXIMUM ((uint8_t)33)
41 #define AWS_MQTT5_PROPERTY_TYPE_TOPIC_ALIAS_MAXIMUM ((uint8_t)34)
42 #define AWS_MQTT5_PROPERTY_TYPE_TOPIC_ALIAS ((uint8_t)35)
43 #define AWS_MQTT5_PROPERTY_TYPE_MAXIMUM_QOS ((uint8_t)36)
44 #define AWS_MQTT5_PROPERTY_TYPE_RETAIN_AVAILABLE ((uint8_t)37)
45 #define AWS_MQTT5_PROPERTY_TYPE_USER_PROPERTY ((uint8_t)38)
46 #define AWS_MQTT5_PROPERTY_TYPE_MAXIMUM_PACKET_SIZE ((uint8_t)39)
47 #define AWS_MQTT5_PROPERTY_TYPE_WILDCARD_SUBSCRIPTIONS_AVAILABLE ((uint8_t)40)
48 #define AWS_MQTT5_PROPERTY_TYPE_SUBSCRIPTION_IDENTIFIERS_AVAILABLE ((uint8_t)41)
49 #define AWS_MQTT5_PROPERTY_TYPE_SHARED_SUBSCRIPTIONS_AVAILABLE ((uint8_t)42)
50
51 /* decode/encode bit masks and positions */
52 #define AWS_MQTT5_CONNECT_FLAGS_WILL_BIT (1U << 2)
53 #define AWS_MQTT5_CONNECT_FLAGS_CLEAN_START_BIT (1U << 1)
54 #define AWS_MQTT5_CONNECT_FLAGS_USER_NAME_BIT (1U << 7)
55 #define AWS_MQTT5_CONNECT_FLAGS_PASSWORD_BIT (1U << 6)
56 #define AWS_MQTT5_CONNECT_FLAGS_WILL_RETAIN_BIT (1U << 5)
57
58 #define AWS_MQTT5_CONNECT_FLAGS_WILL_QOS_BIT_POSITION 3
59 #define AWS_MQTT5_CONNECT_FLAGS_WILL_QOS_BIT_MASK 0x03
60
61 #define AWS_MQTT5_SUBSCRIBE_FLAGS_NO_LOCAL (1U << 2)
62 #define AWS_MQTT5_SUBSCRIBE_FLAGS_RETAIN_AS_PUBLISHED (1U << 3)
63
64 #define AWS_MQTT5_SUBSCRIBE_FLAGS_RETAIN_HANDLING_TYPE_BIT_POSITION 4
65 #define AWS_MQTT5_SUBSCRIBE_FLAGS_RETAIN_HANDLING_TYPE_BIT_MASK 0x03
66 #define AWS_MQTT5_SUBSCRIBE_FLAGS_QOS_BIT_POSITION 0
67 #define AWS_MQTT5_SUBSCRIBE_FLAGS_QOS_BIT_MASK 0x03
68
69 /* Static AWS IoT Core Limit/Quota Values */
70 #define AWS_IOT_CORE_MAXIMUM_TOPIC_LENGTH 256
71 #define AWS_IOT_CORE_MAXIMUM_TOPIC_SEGMENTS 8
72
73 /* Dynamic IoT Core Limits */
74 #define AWS_IOT_CORE_PUBLISH_PER_SECOND_LIMIT 100
75 #define AWS_IOT_CORE_THROUGHPUT_LIMIT (512 * 1024)
76
77 /* Client configuration defaults when parameter left zero */
78 #define AWS_MQTT5_DEFAULT_SOCKET_CONNECT_TIMEOUT_MS 10000
79 #define AWS_MQTT5_CLIENT_DEFAULT_MIN_RECONNECT_DELAY_MS 1000
80 #define AWS_MQTT5_CLIENT_DEFAULT_MAX_RECONNECT_DELAY_MS 120000
81 #define AWS_MQTT5_CLIENT_DEFAULT_MIN_CONNECTED_TIME_TO_RESET_RECONNECT_DELAY_MS 30000
82 #define AWS_MQTT5_CLIENT_DEFAULT_PING_TIMEOUT_MS 30000
83 #define AWS_MQTT5_CLIENT_DEFAULT_CONNACK_TIMEOUT_MS 20000
84 #define AWS_MQTT5_CLIENT_DEFAULT_OPERATION_TIMEOUNT_SECONDS 60
85 #define AWS_MQTT5_CLIENT_DEFAULT_INBOUND_TOPIC_ALIAS_CACHE_SIZE 25
86 #define AWS_MQTT5_CLIENT_DEFAULT_OUTBOUND_TOPIC_ALIAS_CACHE_SIZE 25
87
88 AWS_EXTERN_C_BEGIN
89
90 /**
91 * CONNECT packet MQTT5 prefix which includes "MQTT" encoded as a utf-8 string followed by the protocol number (5)
92 *
93 * {0x00, 0x04, "MQTT", 0x05}
94 */
95 AWS_MQTT_API extern struct aws_byte_cursor g_aws_mqtt5_connect_protocol_cursor;
96
97 /**
98 * Simple helper function to compute the first byte of an MQTT packet encoding as a function of 4 bit flags
99 * and the packet type.
100 *
101 * @param packet_type type of MQTT packet
102 * @param flags 4-bit wide flags, specific to each packet type, 0-valued for most
103 * @return the expected/required first byte of a packet of that type with flags set
104 */
105 AWS_MQTT_API uint8_t aws_mqtt5_compute_fixed_header_byte1(enum aws_mqtt5_packet_type packet_type, uint8_t flags);
106
107 AWS_MQTT_API void aws_mqtt5_negotiated_settings_log(
108 struct aws_mqtt5_negotiated_settings *negotiated_settings,
109 enum aws_log_level level);
110
111 /**
112 * Assigns and stores a client id for use on CONNECT
113 *
114 * @param negotiated_settings settings to apply client id to
115 * @param client_id client id to set
116 */
117 AWS_MQTT_API int aws_mqtt5_negotiated_settings_apply_client_id(
118 struct aws_mqtt5_negotiated_settings *negotiated_settings,
119 const struct aws_byte_cursor *client_id);
120
121 /**
122 * Resets negotiated_settings to defaults reconciled with client set properties.
123 * Called on init of mqtt5 Client and just prior to a CONNECT.
124 *
125 * @param negotiated_settings struct containing settings to be set
126 * @param packet_connect_view Read-only snapshot of a CONNECT packet
127 * @return void
128 */
129 AWS_MQTT_API void aws_mqtt5_negotiated_settings_reset(
130 struct aws_mqtt5_negotiated_settings *negotiated_settings,
131 const struct aws_mqtt5_packet_connect_view *packet_connect_view);
132
133 /**
134 * Checks properties received from Server CONNACK and reconcile with negotiated_settings
135 *
136 * @param negotiated_settings struct containing settings to be set
137 * @param connack_data Read-only snapshot of a CONNACK packet
138 * @return void
139 */
140 AWS_MQTT_API void aws_mqtt5_negotiated_settings_apply_connack(
141 struct aws_mqtt5_negotiated_settings *negotiated_settings,
142 const struct aws_mqtt5_packet_connack_view *connack_data);
143
144 /**
145 * Converts a disconnect reason code into the Reason Code Name, as it appears in the mqtt5 spec.
146 *
147 * @param reason_code a disconnect reason code
148 * @return name associated with the reason code
149 */
150 AWS_MQTT_API const char *aws_mqtt5_disconnect_reason_code_to_c_string(
151 enum aws_mqtt5_disconnect_reason_code reason_code,
152 bool *is_valid);
153
154 /**
155 * Converts a connect reason code into the Reason Code Name, as it appears in the mqtt5 spec.
156 *
157 * @param reason_code a connect reason code
158 * @return name associated with the reason code
159 */
160 AWS_MQTT_API const char *aws_mqtt5_connect_reason_code_to_c_string(enum aws_mqtt5_connect_reason_code reason_code);
161
162 /**
163 * Converts a publish reason code into the Reason Code Name, as it appears in the mqtt5 spec.
164 *
165 * @param reason_code a publish reason code
166 * @return name associated with the reason code
167 */
168 AWS_MQTT_API const char *aws_mqtt5_puback_reason_code_to_c_string(enum aws_mqtt5_puback_reason_code reason_code);
169
170 /**
171 * Converts a subscribe reason code into the Reason Code Name, as it appears in the mqtt5 spec.
172 *
173 * @param reason_code a subscribe reason code
174 * @return name associated with the reason code
175 */
176 AWS_MQTT_API const char *aws_mqtt5_suback_reason_code_to_c_string(enum aws_mqtt5_suback_reason_code reason_code);
177
178 /**
179 * Converts a unsubscribe reason code into the Reason Code Name, as it appears in the mqtt5 spec.
180 *
181 * @param reason_code an unsubscribe reason code
182 * @return name associated with the reason code
183 */
184 AWS_MQTT_API const char *aws_mqtt5_unsuback_reason_code_to_c_string(enum aws_mqtt5_unsuback_reason_code reason_code);
185
186 /**
187 * Converts a session behavior type value to a readable description.
188 *
189 * @param session_behavior type of session behavior
190 * @return short string describing the session behavior
191 */
192 AWS_MQTT_API const char *aws_mqtt5_client_session_behavior_type_to_c_string(
193 enum aws_mqtt5_client_session_behavior_type session_behavior);
194
195 /**
196 * Converts a session behavior type value to a final non-default value.
197 *
198 * @param session_behavior type of session behavior
199 * @return session behavior value where default has been mapped to its intended meaning
200 */
201 AWS_MQTT_API enum aws_mqtt5_client_session_behavior_type aws_mqtt5_client_session_behavior_type_to_non_default(
202 enum aws_mqtt5_client_session_behavior_type session_behavior);
203
204 /**
205 * Converts an outbound topic aliasing behavior type value to a readable description.
206 *
207 * @param outbound_aliasing_behavior type of outbound topic aliasing behavior
208 * @return short string describing the outbound topic aliasing behavior
209 */
210 AWS_MQTT_API const char *aws_mqtt5_outbound_topic_alias_behavior_type_to_c_string(
211 enum aws_mqtt5_client_outbound_topic_alias_behavior_type outbound_aliasing_behavior);
212
213 /**
214 * Checks an outbound aliasing behavior type value for validity
215 *
216 * @param outbound_aliasing_behavior value to check
217 * @return true if this is a valid value, false otherwise
218 */
219 AWS_MQTT_API bool aws_mqtt5_outbound_topic_alias_behavior_type_validate(
220 enum aws_mqtt5_client_outbound_topic_alias_behavior_type outbound_aliasing_behavior);
221
222 /**
223 * Converts an outbound topic aliasing behavior type value to a final non-default value.
224 *
225 * @param outbound_aliasing_behavior type of outbound topic aliasing behavior
226 * @return outbound topic aliasing value where default has been mapped to its intended meaning
227 */
228 AWS_MQTT_API enum aws_mqtt5_client_outbound_topic_alias_behavior_type
229 aws_mqtt5_outbound_topic_alias_behavior_type_to_non_default(
230 enum aws_mqtt5_client_outbound_topic_alias_behavior_type outbound_aliasing_behavior);
231
232 /**
233 * Converts an inbound topic aliasing behavior type value to a readable description.
234 *
235 * @param inbound_aliasing_behavior type of inbound topic aliasing behavior
236 * @return short string describing the inbound topic aliasing behavior
237 */
238 AWS_MQTT_API const char *aws_mqtt5_inbound_topic_alias_behavior_type_to_c_string(
239 enum aws_mqtt5_client_inbound_topic_alias_behavior_type inbound_aliasing_behavior);
240
241 /**
242 * Checks an inbound aliasing behavior type value for validity
243 *
244 * @param inbound_aliasing_behavior value to check
245 * @return true if this is a valid value, false otherwise
246 */
247 AWS_MQTT_API bool aws_mqtt5_inbound_topic_alias_behavior_type_validate(
248 enum aws_mqtt5_client_inbound_topic_alias_behavior_type inbound_aliasing_behavior);
249
250 /**
251 * Converts an inbound topic aliasing behavior type value to a final non-default value.
252 *
253 * @param inbound_aliasing_behavior type of inbound topic aliasing behavior
254 * @return inbound topic aliasing value where default has been mapped to its intended meaning
255 */
256 AWS_MQTT_API enum aws_mqtt5_client_inbound_topic_alias_behavior_type
257 aws_mqtt5_inbound_topic_alias_behavior_type_to_non_default(
258 enum aws_mqtt5_client_inbound_topic_alias_behavior_type inbound_aliasing_behavior);
259
260 /**
261 * Converts an extended validation and flow control options value to a readable description.
262 *
263 * @param extended_validation_behavior type of extended validation and flow control
264 * @return short string describing the extended validation and flow control behavior
265 */
266 AWS_MQTT_API const char *aws_mqtt5_extended_validation_and_flow_control_options_to_c_string(
267 enum aws_mqtt5_extended_validation_and_flow_control_options extended_validation_behavior);
268
269 /**
270 * Converts an offline queue behavior type value to a readable description.
271 *
272 * @param offline_queue_behavior type of offline queue behavior
273 * @return short string describing the offline queue behavior
274 */
275 AWS_MQTT_API const char *aws_mqtt5_client_operation_queue_behavior_type_to_c_string(
276 enum aws_mqtt5_client_operation_queue_behavior_type offline_queue_behavior);
277
278 /**
279 * Converts an offline queue behavior type value to a final non-default value.
280 *
281 * @param offline_queue_behavior type of offline queue behavior
282 * @return offline queue behavior value where default has been mapped to its intended meaning
283 */
284 AWS_MQTT_API enum aws_mqtt5_client_operation_queue_behavior_type
285 aws_mqtt5_client_operation_queue_behavior_type_to_non_default(
286 enum aws_mqtt5_client_operation_queue_behavior_type offline_queue_behavior);
287
288 /**
289 * Converts a lifecycle event type value to a readable description.
290 *
291 * @param lifecycle_event type of lifecycle event
292 * @return short string describing the lifecycle event type
293 */
294 AWS_MQTT_API const char *aws_mqtt5_client_lifecycle_event_type_to_c_string(
295 enum aws_mqtt5_client_lifecycle_event_type lifecycle_event);
296
297 /**
298 * Converts a payload format indicator value to a readable description.
299 *
300 * @param format_indicator type of payload format indicator
301 * @return short string describing the payload format indicator
302 */
303 AWS_MQTT_API const char *aws_mqtt5_payload_format_indicator_to_c_string(
304 enum aws_mqtt5_payload_format_indicator format_indicator);
305
306 /**
307 * Converts a retain handling type value to a readable description.
308 *
309 * @param retain_handling_type type of retain handling
310 * @return short string describing the retain handling type
311 */
312 AWS_MQTT_API const char *aws_mqtt5_retain_handling_type_to_c_string(
313 enum aws_mqtt5_retain_handling_type retain_handling_type);
314
315 /**
316 * Converts a packet type value to a readable description.
317 *
318 * @param packet_type type of packet
319 * @return short string describing the packet type
320 */
321 AWS_MQTT_API const char *aws_mqtt5_packet_type_to_c_string(enum aws_mqtt5_packet_type packet_type);
322
323 /**
324 * Computes a uniformly-distributed random number in the specified range. Not intended for cryptographic purposes.
325 *
326 * @param from one end of the range to sample from
327 * @param to other end of the range to sample from
328 * @return a random number from the supplied range, with roughly a uniform distribution
329 */
330 AWS_MQTT_API uint64_t aws_mqtt5_client_random_in_range(uint64_t from, uint64_t to);
331
332 /**
333 * Utility function to skip the "$aws/rules/<rule-name>/" prefix of a topic. Technically this works for topic
334 * filters too.
335 *
336 * @param topic_cursor topic to get the non-rules suffix for
337 * @return remaining part of the topic after the leading AWS IoT Rules prefix has been skipped, if present
338 */
339 AWS_MQTT_API struct aws_byte_cursor aws_mqtt5_topic_skip_aws_iot_core_uncounted_prefix(
340 struct aws_byte_cursor topic_cursor);
341
342 /**
343 * Computes the number of topic segments in a topic or topic filter
344 * @param topic_cursor topic or topic filter
345 * @return number of topic segments in the topic or topic filter
346 */
347 AWS_MQTT_API size_t aws_mqtt5_topic_get_segment_count(struct aws_byte_cursor topic_cursor);
348
349 /**
350 * Checks a topic filter for validity against AWS IoT Core rules
351 * @param topic_filter_cursor topic filter to check
352 * @return true if valid, false otherwise
353 */
354 AWS_MQTT_API bool aws_mqtt_is_valid_topic_filter_for_iot_core(struct aws_byte_cursor topic_filter_cursor);
355
356 /**
357 * Checks a topic for validity against AWS IoT Core rules
358 * @param topic_cursor topic to check
359 * @return true if valid, false otherwise
360 */
361 AWS_MQTT_API bool aws_mqtt_is_valid_topic_for_iot_core(struct aws_byte_cursor topic_cursor);
362
363 /**
364 * Checks if a topic filter matches a shared subscription according to the mqtt5 spec
365 * @param topic_cursor topic to check
366 * @return true if this matches the definition of a shared subscription, false otherwise
367 */
368 AWS_MQTT_API bool aws_mqtt_is_topic_filter_shared_subscription(struct aws_byte_cursor topic_cursor);
369
370 AWS_EXTERN_C_END
371
372 #endif /* AWS_MQTT_MQTT5_UTILS_H */