| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/mqtt/private/v5/mqtt5_utils.h> |
| 7 |
|
| 8 |
#include <aws/common/byte_buf.h> |
| 9 |
#include <aws/common/device_random.h> |
| 10 |
#include <inttypes.h> |
| 11 |
|
| 12 |
uint8_t aws_mqtt5_compute_fixed_header_byte1(enum aws_mqtt5_packet_type packet_type, uint8_t flags) { |
| 13 |
return flags | ((uint8_t)packet_type << 4); |
| 14 |
} |
| 15 |
|
| 16 |
|
| 17 |
static uint8_t s_connect_variable_length_header_prefix[7] = {0x00, 0x04, 0x4D, 0x51, 0x54, 0x54, 0x05}; |
| 18 |
|
| 19 |
struct aws_byte_cursor g_aws_mqtt5_connect_protocol_cursor = { |
| 20 |
.ptr = &s_connect_variable_length_header_prefix[0], |
| 21 |
.len = AWS_ARRAY_SIZE(s_connect_variable_length_header_prefix), |
| 22 |
}; |
| 23 |
|
| 24 |
void aws_mqtt5_negotiated_settings_log( |
| 25 |
struct aws_mqtt5_negotiated_settings *negotiated_settings, |
| 26 |
enum aws_log_level level) { |
| 27 |
|
| 28 |
struct aws_logger *temp_logger = aws_logger_get(); |
| 29 |
if (temp_logger == NULL || temp_logger->vtable->get_log_level(temp_logger, AWS_LS_MQTT5_GENERAL) < level) { |
| 30 |
return; |
| 31 |
} |
| 32 |
|
| 33 |
AWS_LOGF( |
| 34 |
level, |
| 35 |
AWS_LS_MQTT5_GENERAL, |
| 36 |
"id=%p: aws_mqtt5_negotiated_settings maxiumum qos set to %d", |
| 37 |
(void *)negotiated_settings, |
| 38 |
negotiated_settings->maximum_qos); |
| 39 |
|
| 40 |
AWS_LOGF( |
| 41 |
level, |
| 42 |
AWS_LS_MQTT5_GENERAL, |
| 43 |
"id=%p: aws_mqtt5_negotiated_settings session expiry interval set to %" PRIu32, |
| 44 |
(void *)negotiated_settings, |
| 45 |
negotiated_settings->session_expiry_interval); |
| 46 |
|
| 47 |
AWS_LOGF( |
| 48 |
level, |
| 49 |
AWS_LS_MQTT5_GENERAL, |
| 50 |
"id=%p: aws_mqtt5_negotiated_settings receive maximum from server set to %" PRIu16, |
| 51 |
(void *)negotiated_settings, |
| 52 |
negotiated_settings->receive_maximum_from_server); |
| 53 |
|
| 54 |
AWS_LOGF( |
| 55 |
level, |
| 56 |
AWS_LS_MQTT5_GENERAL, |
| 57 |
"id=%p: aws_mqtt5_negotiated_settings maximum packet size to server set to %" PRIu32, |
| 58 |
(void *)negotiated_settings, |
| 59 |
negotiated_settings->maximum_packet_size_to_server); |
| 60 |
|
| 61 |
AWS_LOGF( |
| 62 |
level, |
| 63 |
AWS_LS_MQTT5_GENERAL, |
| 64 |
"id=%p: aws_mqtt5_negotiated_settings topic alias maximum to server set to %" PRIu16, |
| 65 |
(void *)negotiated_settings, |
| 66 |
negotiated_settings->topic_alias_maximum_to_server); |
| 67 |
|
| 68 |
AWS_LOGF( |
| 69 |
level, |
| 70 |
AWS_LS_MQTT5_GENERAL, |
| 71 |
"id=%p: aws_mqtt5_negotiated_settings topic alias maximum to client set to %" PRIu16, |
| 72 |
(void *)negotiated_settings, |
| 73 |
negotiated_settings->topic_alias_maximum_to_client); |
| 74 |
|
| 75 |
AWS_LOGF( |
| 76 |
level, |
| 77 |
AWS_LS_MQTT5_GENERAL, |
| 78 |
"id=%p: aws_mqtt5_negotiated_settings server keep alive set to %" PRIu16, |
| 79 |
(void *)negotiated_settings, |
| 80 |
negotiated_settings->server_keep_alive); |
| 81 |
|
| 82 |
AWS_LOGF( |
| 83 |
level, |
| 84 |
AWS_LS_MQTT5_GENERAL, |
| 85 |
"id=%p: aws_mqtt5_negotiated_settings retain available set to %s", |
| 86 |
(void *)negotiated_settings, |
| 87 |
negotiated_settings->retain_available ? "true" : "false"); |
| 88 |
|
| 89 |
AWS_LOGF( |
| 90 |
level, |
| 91 |
AWS_LS_MQTT5_GENERAL, |
| 92 |
"id=%p: aws_mqtt5_negotiated_settings wildcard subscriptions available set to %s", |
| 93 |
(void *)negotiated_settings, |
| 94 |
negotiated_settings->wildcard_subscriptions_available ? "true" : "false"); |
| 95 |
|
| 96 |
AWS_LOGF( |
| 97 |
level, |
| 98 |
AWS_LS_MQTT5_GENERAL, |
| 99 |
"id=%p: aws_mqtt5_negotiated_settings subscription identifiers available set to %s", |
| 100 |
(void *)negotiated_settings, |
| 101 |
negotiated_settings->subscription_identifiers_available ? "true" : "false"); |
| 102 |
|
| 103 |
AWS_LOGF( |
| 104 |
level, |
| 105 |
AWS_LS_MQTT5_GENERAL, |
| 106 |
"id=%p: aws_mqtt5_negotiated_settings shared subscriptions available set to %s", |
| 107 |
(void *)negotiated_settings, |
| 108 |
negotiated_settings->shared_subscriptions_available ? "true" : "false"); |
| 109 |
} |
| 110 |
|
| 111 |
int aws_mqtt5_negotiated_settings_init( |
| 112 |
struct aws_allocator *allocator, |
| 113 |
struct aws_mqtt5_negotiated_settings *negotiated_settings, |
| 114 |
const struct aws_byte_cursor *client_id) { |
| 115 |
if (aws_byte_buf_init(&negotiated_settings->client_id_storage, allocator, client_id->len)) { |
| 116 |
return AWS_OP_ERR; |
| 117 |
} |
| 118 |
|
| 119 |
if (aws_byte_buf_append_dynamic(&negotiated_settings->client_id_storage, client_id)) { |
| 120 |
return AWS_OP_ERR; |
| 121 |
} |
| 122 |
|
| 123 |
return AWS_OP_SUCCESS; |
| 124 |
} |
| 125 |
|
| 126 |
int aws_mqtt5_negotiated_settings_copy( |
| 127 |
const struct aws_mqtt5_negotiated_settings *source, |
| 128 |
struct aws_mqtt5_negotiated_settings *dest) { |
| 129 |
aws_mqtt5_negotiated_settings_clean_up(dest); |
| 130 |
|
| 131 |
*dest = *source; |
| 132 |
AWS_ZERO_STRUCT(dest->client_id_storage); |
| 133 |
|
| 134 |
if (source->client_id_storage.allocator != NULL) { |
| 135 |
return aws_byte_buf_init_copy_from_cursor( |
| 136 |
&dest->client_id_storage, |
| 137 |
source->client_id_storage.allocator, |
| 138 |
aws_byte_cursor_from_buf(&source->client_id_storage)); |
| 139 |
} |
| 140 |
|
| 141 |
return AWS_OP_SUCCESS; |
| 142 |
} |
| 143 |
|
| 144 |
int aws_mqtt5_negotiated_settings_apply_client_id( |
| 145 |
struct aws_mqtt5_negotiated_settings *negotiated_settings, |
| 146 |
const struct aws_byte_cursor *client_id) { |
| 147 |
|
| 148 |
if (negotiated_settings->client_id_storage.len == 0) { |
| 149 |
if (aws_byte_buf_append_dynamic(&negotiated_settings->client_id_storage, client_id)) { |
| 150 |
return AWS_OP_ERR; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
return AWS_OP_SUCCESS; |
| 155 |
} |
| 156 |
|
| 157 |
void aws_mqtt5_negotiated_settings_clean_up(struct aws_mqtt5_negotiated_settings *negotiated_settings) { |
| 158 |
aws_byte_buf_clean_up(&negotiated_settings->client_id_storage); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
void aws_mqtt5_negotiated_settings_reset( |
| 163 |
struct aws_mqtt5_negotiated_settings *negotiated_settings, |
| 164 |
const struct aws_mqtt5_packet_connect_view *packet_connect_view) { |
| 165 |
AWS_PRECONDITION(negotiated_settings != NULL); |
| 166 |
AWS_PRECONDITION(packet_connect_view != NULL); |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
negotiated_settings->server_keep_alive = packet_connect_view->keep_alive_interval_seconds; |
| 172 |
negotiated_settings->session_expiry_interval = 0; |
| 173 |
negotiated_settings->receive_maximum_from_server = AWS_MQTT5_RECEIVE_MAXIMUM; |
| 174 |
negotiated_settings->maximum_packet_size_to_server = AWS_MQTT5_MAXIMUM_PACKET_SIZE; |
| 175 |
negotiated_settings->topic_alias_maximum_to_client = 0; |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
negotiated_settings->maximum_qos = AWS_MQTT5_QOS_AT_LEAST_ONCE; |
| 180 |
negotiated_settings->topic_alias_maximum_to_server = 0; |
| 181 |
|
| 182 |
|
| 183 |
negotiated_settings->retain_available = true; |
| 184 |
negotiated_settings->wildcard_subscriptions_available = true; |
| 185 |
negotiated_settings->subscription_identifiers_available = true; |
| 186 |
negotiated_settings->shared_subscriptions_available = true; |
| 187 |
|
| 188 |
negotiated_settings->rejoined_session = false; |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
if (packet_connect_view->session_expiry_interval_seconds != NULL) { |
| 196 |
negotiated_settings->session_expiry_interval = *packet_connect_view->session_expiry_interval_seconds; |
| 197 |
} |
| 198 |
|
| 199 |
if (packet_connect_view->topic_alias_maximum != NULL) { |
| 200 |
negotiated_settings->topic_alias_maximum_to_client = *packet_connect_view->topic_alias_maximum; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
void aws_mqtt5_negotiated_settings_apply_connack( |
| 205 |
struct aws_mqtt5_negotiated_settings *negotiated_settings, |
| 206 |
const struct aws_mqtt5_packet_connack_view *connack_data) { |
| 207 |
AWS_PRECONDITION(negotiated_settings != NULL); |
| 208 |
AWS_PRECONDITION(connack_data != NULL); |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
if (connack_data->session_expiry_interval != NULL) { |
| 216 |
negotiated_settings->session_expiry_interval = *connack_data->session_expiry_interval; |
| 217 |
} |
| 218 |
|
| 219 |
if (connack_data->receive_maximum != NULL) { |
| 220 |
negotiated_settings->receive_maximum_from_server = *connack_data->receive_maximum; |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
if (connack_data->maximum_qos != NULL) { |
| 225 |
if (*connack_data->maximum_qos < negotiated_settings->maximum_qos) { |
| 226 |
negotiated_settings->maximum_qos = *connack_data->maximum_qos; |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
if (connack_data->retain_available != NULL) { |
| 231 |
negotiated_settings->retain_available = *connack_data->retain_available; |
| 232 |
} |
| 233 |
|
| 234 |
if (connack_data->maximum_packet_size != NULL) { |
| 235 |
negotiated_settings->maximum_packet_size_to_server = *connack_data->maximum_packet_size; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
if (connack_data->topic_alias_maximum != NULL) { |
| 240 |
negotiated_settings->topic_alias_maximum_to_server = *connack_data->topic_alias_maximum; |
| 241 |
} |
| 242 |
|
| 243 |
if (connack_data->wildcard_subscriptions_available != NULL) { |
| 244 |
negotiated_settings->wildcard_subscriptions_available = *connack_data->wildcard_subscriptions_available; |
| 245 |
} |
| 246 |
|
| 247 |
if (connack_data->subscription_identifiers_available != NULL) { |
| 248 |
negotiated_settings->subscription_identifiers_available = *connack_data->subscription_identifiers_available; |
| 249 |
} |
| 250 |
|
| 251 |
if (connack_data->shared_subscriptions_available != NULL) { |
| 252 |
negotiated_settings->shared_subscriptions_available = *connack_data->shared_subscriptions_available; |
| 253 |
} |
| 254 |
|
| 255 |
if (connack_data->server_keep_alive != NULL) { |
| 256 |
negotiated_settings->server_keep_alive = *connack_data->server_keep_alive; |
| 257 |
} |
| 258 |
|
| 259 |
if (connack_data->assigned_client_identifier != NULL) { |
| 260 |
aws_mqtt5_negotiated_settings_apply_client_id(negotiated_settings, connack_data->assigned_client_identifier); |
| 261 |
} |
| 262 |
|
| 263 |
negotiated_settings->rejoined_session = connack_data->session_present; |
| 264 |
} |
| 265 |
|
| 266 |
const char *aws_mqtt5_client_session_behavior_type_to_c_string( |
| 267 |
enum aws_mqtt5_client_session_behavior_type session_behavior) { |
| 268 |
switch (aws_mqtt5_client_session_behavior_type_to_non_default(session_behavior)) { |
| 269 |
case AWS_MQTT5_CSBT_CLEAN: |
| 270 |
return "Clean session always"; |
| 271 |
case AWS_MQTT5_CSBT_REJOIN_POST_SUCCESS: |
| 272 |
return "Attempt to resume a session after initial connection success"; |
| 273 |
case AWS_MQTT5_CSBT_REJOIN_ALWAYS: |
| 274 |
return "Always attempt to resume a session"; |
| 275 |
default: |
| 276 |
return "Unknown session behavior"; |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
enum aws_mqtt5_client_session_behavior_type aws_mqtt5_client_session_behavior_type_to_non_default( |
| 281 |
enum aws_mqtt5_client_session_behavior_type session_behavior) { |
| 282 |
if (session_behavior == AWS_MQTT5_CSBT_DEFAULT) { |
| 283 |
return AWS_MQTT5_CSBT_CLEAN; |
| 284 |
} |
| 285 |
|
| 286 |
return session_behavior; |
| 287 |
} |
| 288 |
|
| 289 |
const char *aws_mqtt5_outbound_topic_alias_behavior_type_to_c_string( |
| 290 |
enum aws_mqtt5_client_outbound_topic_alias_behavior_type outbound_aliasing_behavior) { |
| 291 |
switch (aws_mqtt5_outbound_topic_alias_behavior_type_to_non_default(outbound_aliasing_behavior)) { |
| 292 |
case AWS_MQTT5_COTABT_MANUAL: |
| 293 |
return "User-controlled outbound topic aliasing behavior"; |
| 294 |
case AWS_MQTT5_COTABT_LRU: |
| 295 |
return "LRU caching outbound topic aliasing behavior"; |
| 296 |
case AWS_MQTT5_COTABT_DISABLED: |
| 297 |
return "Outbound topic aliasing disabled"; |
| 298 |
|
| 299 |
default: |
| 300 |
return "Unknown outbound topic aliasing behavior"; |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
bool aws_mqtt5_outbound_topic_alias_behavior_type_validate( |
| 305 |
enum aws_mqtt5_client_outbound_topic_alias_behavior_type outbound_aliasing_behavior) { |
| 306 |
|
| 307 |
return outbound_aliasing_behavior >= AWS_MQTT5_COTABT_DEFAULT && |
| 308 |
outbound_aliasing_behavior <= AWS_MQTT5_COTABT_DISABLED; |
| 309 |
} |
| 310 |
|
| 311 |
enum aws_mqtt5_client_outbound_topic_alias_behavior_type aws_mqtt5_outbound_topic_alias_behavior_type_to_non_default( |
| 312 |
enum aws_mqtt5_client_outbound_topic_alias_behavior_type outbound_aliasing_behavior) { |
| 313 |
if (outbound_aliasing_behavior == AWS_MQTT5_COTABT_DEFAULT) { |
| 314 |
return AWS_MQTT5_COTABT_DISABLED; |
| 315 |
} |
| 316 |
|
| 317 |
return outbound_aliasing_behavior; |
| 318 |
} |
| 319 |
|
| 320 |
const char *aws_mqtt5_inbound_topic_alias_behavior_type_to_c_string( |
| 321 |
enum aws_mqtt5_client_inbound_topic_alias_behavior_type inbound_aliasing_behavior) { |
| 322 |
switch (aws_mqtt5_inbound_topic_alias_behavior_type_to_non_default(inbound_aliasing_behavior)) { |
| 323 |
case AWS_MQTT5_CITABT_ENABLED: |
| 324 |
return "Inbound topic aliasing behavior enabled"; |
| 325 |
case AWS_MQTT5_CITABT_DISABLED: |
| 326 |
return "Inbound topic aliasing behavior disabled"; |
| 327 |
default: |
| 328 |
return "Unknown inbound topic aliasing behavior"; |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
bool aws_mqtt5_inbound_topic_alias_behavior_type_validate( |
| 333 |
enum aws_mqtt5_client_inbound_topic_alias_behavior_type inbound_aliasing_behavior) { |
| 334 |
|
| 335 |
return inbound_aliasing_behavior >= AWS_MQTT5_CITABT_DEFAULT && |
| 336 |
inbound_aliasing_behavior <= AWS_MQTT5_CITABT_DISABLED; |
| 337 |
} |
| 338 |
|
| 339 |
enum aws_mqtt5_client_inbound_topic_alias_behavior_type aws_mqtt5_inbound_topic_alias_behavior_type_to_non_default( |
| 340 |
enum aws_mqtt5_client_inbound_topic_alias_behavior_type inbound_aliasing_behavior) { |
| 341 |
if (inbound_aliasing_behavior == AWS_MQTT5_CITABT_DEFAULT) { |
| 342 |
return AWS_MQTT5_CITABT_DISABLED; |
| 343 |
} |
| 344 |
|
| 345 |
return inbound_aliasing_behavior; |
| 346 |
} |
| 347 |
|
| 348 |
const char *aws_mqtt5_extended_validation_and_flow_control_options_to_c_string( |
| 349 |
enum aws_mqtt5_extended_validation_and_flow_control_options extended_validation_behavior) { |
| 350 |
switch (extended_validation_behavior) { |
| 351 |
case AWS_MQTT5_EVAFCO_NONE: |
| 352 |
return "No additional flow control or packet validation"; |
| 353 |
case AWS_MQTT5_EVAFCO_AWS_IOT_CORE_DEFAULTS: |
| 354 |
return "AWS IoT Core flow control and packet validation"; |
| 355 |
default: |
| 356 |
return "Unknown extended validation behavior"; |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
const char *aws_mqtt5_client_operation_queue_behavior_type_to_c_string( |
| 361 |
enum aws_mqtt5_client_operation_queue_behavior_type offline_queue_behavior) { |
| 362 |
switch (aws_mqtt5_client_operation_queue_behavior_type_to_non_default(offline_queue_behavior)) { |
| 363 |
case AWS_MQTT5_COQBT_FAIL_NON_QOS1_PUBLISH_ON_DISCONNECT: |
| 364 |
return "Fail all incomplete operations except QoS 1 publishes"; |
| 365 |
case AWS_MQTT5_COQBT_FAIL_QOS0_PUBLISH_ON_DISCONNECT: |
| 366 |
return "Fail incomplete QoS 0 publishes"; |
| 367 |
case AWS_MQTT5_COQBT_FAIL_ALL_ON_DISCONNECT: |
| 368 |
return "Fail all incomplete operations"; |
| 369 |
default: |
| 370 |
return "Unknown operation queue behavior type"; |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
enum aws_mqtt5_client_operation_queue_behavior_type aws_mqtt5_client_operation_queue_behavior_type_to_non_default( |
| 375 |
enum aws_mqtt5_client_operation_queue_behavior_type offline_queue_behavior) { |
| 376 |
if (offline_queue_behavior == AWS_MQTT5_COQBT_DEFAULT) { |
| 377 |
return AWS_MQTT5_COQBT_FAIL_QOS0_PUBLISH_ON_DISCONNECT; |
| 378 |
} |
| 379 |
|
| 380 |
return offline_queue_behavior; |
| 381 |
} |
| 382 |
|
| 383 |
const char *aws_mqtt5_client_lifecycle_event_type_to_c_string( |
| 384 |
enum aws_mqtt5_client_lifecycle_event_type lifecycle_event) { |
| 385 |
switch (lifecycle_event) { |
| 386 |
case AWS_MQTT5_CLET_ATTEMPTING_CONNECT: |
| 387 |
return "Connection establishment attempt"; |
| 388 |
case AWS_MQTT5_CLET_CONNECTION_SUCCESS: |
| 389 |
return "Connection establishment success"; |
| 390 |
case AWS_MQTT5_CLET_CONNECTION_FAILURE: |
| 391 |
return "Connection establishment failure"; |
| 392 |
case AWS_MQTT5_CLET_DISCONNECTION: |
| 393 |
return "Disconnection"; |
| 394 |
case AWS_MQTT5_CLET_STOPPED: |
| 395 |
return "Client stopped"; |
| 396 |
} |
| 397 |
|
| 398 |
return "Unknown lifecycle event"; |
| 399 |
} |
| 400 |
|
| 401 |
uint64_t aws_mqtt5_client_random_in_range(uint64_t from, uint64_t to) { |
| 402 |
uint64_t max = aws_max_u64(from, to); |
| 403 |
uint64_t min = aws_min_u64(from, to); |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
uint64_t diff = max - min; |
| 411 |
if (!diff) { |
| 412 |
return min; |
| 413 |
} |
| 414 |
|
| 415 |
uint64_t random_value = 0; |
| 416 |
if (aws_device_random_u64(&random_value)) { |
| 417 |
return min; |
| 418 |
} |
| 419 |
|
| 420 |
if (diff == UINT64_MAX) { |
| 421 |
return random_value; |
| 422 |
} |
| 423 |
|
| 424 |
return min + random_value % (diff + 1); |
| 425 |
} |
| 426 |
|
| 427 |
static uint8_t s_aws_iot_core_rules_prefix[] = "$aws/rules/"; |
| 428 |
|
| 429 |
static struct aws_byte_cursor s_aws_mqtt5_topic_skip_aws_iot_rules_prefix(struct aws_byte_cursor topic_cursor) { |
| 430 |
size_t prefix_length = AWS_ARRAY_SIZE(s_aws_iot_core_rules_prefix) - 1; |
| 431 |
|
| 432 |
struct aws_byte_cursor rules_prefix = { |
| 433 |
.ptr = s_aws_iot_core_rules_prefix, |
| 434 |
.len = prefix_length, |
| 435 |
}; |
| 436 |
|
| 437 |
if (topic_cursor.len < rules_prefix.len) { |
| 438 |
return topic_cursor; |
| 439 |
} |
| 440 |
|
| 441 |
struct aws_byte_cursor topic_cursor_copy = topic_cursor; |
| 442 |
struct aws_byte_cursor topic_prefix = topic_cursor; |
| 443 |
topic_prefix.len = rules_prefix.len; |
| 444 |
|
| 445 |
if (!aws_byte_cursor_eq_ignore_case(&rules_prefix, &topic_prefix)) { |
| 446 |
return topic_cursor; |
| 447 |
} |
| 448 |
|
| 449 |
aws_byte_cursor_advance(&topic_cursor_copy, prefix_length); |
| 450 |
if (topic_cursor_copy.len == 0) { |
| 451 |
return topic_cursor; |
| 452 |
} |
| 453 |
|
| 454 |
struct aws_byte_cursor rule_name_segment_cursor; |
| 455 |
AWS_ZERO_STRUCT(rule_name_segment_cursor); |
| 456 |
|
| 457 |
if (!aws_byte_cursor_next_split(&topic_cursor_copy, '/', &rule_name_segment_cursor)) { |
| 458 |
return topic_cursor; |
| 459 |
} |
| 460 |
|
| 461 |
if (topic_cursor_copy.len < rule_name_segment_cursor.len + 1) { |
| 462 |
return topic_cursor; |
| 463 |
} |
| 464 |
|
| 465 |
aws_byte_cursor_advance(&topic_cursor_copy, rule_name_segment_cursor.len + 1); |
| 466 |
|
| 467 |
return topic_cursor_copy; |
| 468 |
} |
| 469 |
|
| 470 |
static uint8_t s_shared_subscription_prefix[] = "$share"; |
| 471 |
|
| 472 |
static bool s_is_not_hash_or_plus(uint8_t byte) { |
| 473 |
return byte != '+' && byte != '#'; |
| 474 |
} |
| 475 |
|
| 476 |
static struct aws_byte_cursor s_aws_mqtt5_topic_skip_shared_prefix(struct aws_byte_cursor topic_cursor) { |
| 477 |
|
| 478 |
struct aws_byte_cursor first_segment_cursor; |
| 479 |
AWS_ZERO_STRUCT(first_segment_cursor); |
| 480 |
if (!aws_byte_cursor_next_split(&topic_cursor, '/', &first_segment_cursor)) { |
| 481 |
return topic_cursor; |
| 482 |
} |
| 483 |
|
| 484 |
struct aws_byte_cursor share_prefix_cursor = { |
| 485 |
.ptr = s_shared_subscription_prefix, |
| 486 |
.len = AWS_ARRAY_SIZE(s_shared_subscription_prefix) - 1, |
| 487 |
}; |
| 488 |
|
| 489 |
if (!aws_byte_cursor_eq_ignore_case(&share_prefix_cursor, &first_segment_cursor)) { |
| 490 |
return topic_cursor; |
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
struct aws_byte_cursor second_segment_cursor = first_segment_cursor; |
| 498 |
if (!aws_byte_cursor_next_split(&topic_cursor, '/', &second_segment_cursor)) { |
| 499 |
return topic_cursor; |
| 500 |
} |
| 501 |
|
| 502 |
if (second_segment_cursor.len == 0 || |
| 503 |
!aws_byte_cursor_satisfies_pred(&second_segment_cursor, s_is_not_hash_or_plus)) { |
| 504 |
return topic_cursor; |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
struct aws_byte_cursor remaining_cursor = topic_cursor; |
| 511 |
size_t remaining_length = |
| 512 |
topic_cursor.ptr + topic_cursor.len - (second_segment_cursor.len + second_segment_cursor.ptr); |
| 513 |
if (remaining_length == 0) { |
| 514 |
return topic_cursor; |
| 515 |
} |
| 516 |
|
| 517 |
aws_byte_cursor_advance(&remaining_cursor, topic_cursor.len - remaining_length + 1); |
| 518 |
|
| 519 |
return remaining_cursor; |
| 520 |
} |
| 521 |
|
| 522 |
struct aws_byte_cursor aws_mqtt5_topic_skip_aws_iot_core_uncounted_prefix(struct aws_byte_cursor topic_cursor) { |
| 523 |
struct aws_byte_cursor skip_shared = s_aws_mqtt5_topic_skip_shared_prefix(topic_cursor); |
| 524 |
struct aws_byte_cursor skip_rules = s_aws_mqtt5_topic_skip_aws_iot_rules_prefix(skip_shared); |
| 525 |
|
| 526 |
return skip_rules; |
| 527 |
} |
| 528 |
|
| 529 |
size_t aws_mqtt5_topic_get_segment_count(struct aws_byte_cursor topic_cursor) { |
| 530 |
size_t segment_count = 0; |
| 531 |
|
| 532 |
struct aws_byte_cursor segment_cursor; |
| 533 |
AWS_ZERO_STRUCT(segment_cursor); |
| 534 |
|
| 535 |
while (aws_byte_cursor_next_split(&topic_cursor, '/', &segment_cursor)) { |
| 536 |
++segment_count; |
| 537 |
} |
| 538 |
|
| 539 |
return segment_count; |
| 540 |
} |
| 541 |
|
| 542 |
bool aws_mqtt_is_valid_topic_filter_for_iot_core(struct aws_byte_cursor topic_filter_cursor) { |
| 543 |
struct aws_byte_cursor post_rule_suffix = aws_mqtt5_topic_skip_aws_iot_core_uncounted_prefix(topic_filter_cursor); |
| 544 |
return aws_mqtt5_topic_get_segment_count(post_rule_suffix) <= AWS_IOT_CORE_MAXIMUM_TOPIC_SEGMENTS; |
| 545 |
} |
| 546 |
|
| 547 |
bool aws_mqtt_is_valid_topic_for_iot_core(struct aws_byte_cursor topic_cursor) { |
| 548 |
struct aws_byte_cursor post_rule_suffix = aws_mqtt5_topic_skip_aws_iot_core_uncounted_prefix(topic_cursor); |
| 549 |
if (aws_mqtt5_topic_get_segment_count(post_rule_suffix) > AWS_IOT_CORE_MAXIMUM_TOPIC_SEGMENTS) { |
| 550 |
return false; |
| 551 |
} |
| 552 |
|
| 553 |
return post_rule_suffix.len <= AWS_IOT_CORE_MAXIMUM_TOPIC_LENGTH; |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
bool aws_mqtt_is_topic_filter_shared_subscription(struct aws_byte_cursor topic_cursor) { |
| 558 |
struct aws_byte_cursor remaining_cursor = s_aws_mqtt5_topic_skip_shared_prefix(topic_cursor); |
| 559 |
if (remaining_cursor.len == topic_cursor.len) { |
| 560 |
return false; |
| 561 |
} |
| 562 |
|
| 563 |
if (!aws_mqtt_is_valid_topic_filter(&remaining_cursor)) { |
| 564 |
return false; |
| 565 |
} |
| 566 |
|
| 567 |
return true; |
| 568 |
} |