| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/mqtt/private/v5/mqtt5_options_storage.h> |
| 7 |
|
| 8 |
#include <aws/common/clock.h> |
| 9 |
#include <aws/common/encoding.h> |
| 10 |
#include <aws/common/string.h> |
| 11 |
#include <aws/common/uuid.h> |
| 12 |
#include <aws/io/channel_bootstrap.h> |
| 13 |
#include <aws/io/event_loop.h> |
| 14 |
#include <aws/mqtt/private/mqtt_iot_metrics.h> |
| 15 |
#include <aws/mqtt/private/v5/mqtt5_client_impl.h> |
| 16 |
#include <aws/mqtt/private/v5/mqtt5_utils.h> |
| 17 |
#include <aws/mqtt/v5/mqtt5_client.h> |
| 18 |
|
| 19 |
#include <inttypes.h> |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
int aws_mqtt5_user_property_set_init( |
| 26 |
struct aws_mqtt5_user_property_set *property_set, |
| 27 |
struct aws_allocator *allocator) { |
| 28 |
AWS_ZERO_STRUCT(*property_set); |
| 29 |
|
| 30 |
if (aws_array_list_init_dynamic(&property_set->properties, allocator, 0, sizeof(struct aws_mqtt5_user_property))) { |
| 31 |
return AWS_OP_ERR; |
| 32 |
} |
| 33 |
|
| 34 |
return AWS_OP_SUCCESS; |
| 35 |
} |
| 36 |
|
| 37 |
int aws_mqtt5_user_property_set_init_with_storage( |
| 38 |
struct aws_mqtt5_user_property_set *property_set, |
| 39 |
struct aws_allocator *allocator, |
| 40 |
struct aws_byte_buf *storage, |
| 41 |
size_t property_count, |
| 42 |
const struct aws_mqtt5_user_property *properties) { |
| 43 |
AWS_ZERO_STRUCT(*property_set); |
| 44 |
|
| 45 |
if (aws_array_list_init_dynamic( |
| 46 |
&property_set->properties, allocator, property_count, sizeof(struct aws_mqtt5_user_property))) { |
| 47 |
goto error; |
| 48 |
} |
| 49 |
|
| 50 |
for (size_t i = 0; i < property_count; ++i) { |
| 51 |
const struct aws_mqtt5_user_property *property = &properties[i]; |
| 52 |
struct aws_mqtt5_user_property property_clone = *property; |
| 53 |
|
| 54 |
if (aws_byte_buf_append_and_update(storage, &property_clone.name)) { |
| 55 |
goto error; |
| 56 |
} |
| 57 |
|
| 58 |
if (aws_byte_buf_append_and_update(storage, &property_clone.value)) { |
| 59 |
goto error; |
| 60 |
} |
| 61 |
|
| 62 |
if (aws_array_list_push_back(&property_set->properties, &property_clone)) { |
| 63 |
goto error; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
return AWS_OP_SUCCESS; |
| 68 |
|
| 69 |
error: |
| 70 |
|
| 71 |
aws_mqtt5_user_property_set_clean_up(property_set); |
| 72 |
|
| 73 |
return AWS_OP_ERR; |
| 74 |
} |
| 75 |
|
| 76 |
void aws_mqtt5_user_property_set_clean_up(struct aws_mqtt5_user_property_set *property_set) { |
| 77 |
aws_array_list_clean_up(&property_set->properties); |
| 78 |
} |
| 79 |
|
| 80 |
size_t aws_mqtt5_user_property_set_size(const struct aws_mqtt5_user_property_set *property_set) { |
| 81 |
return aws_array_list_length(&property_set->properties); |
| 82 |
} |
| 83 |
|
| 84 |
static void s_aws_mqtt5_user_property_set_log( |
| 85 |
struct aws_logger *log_handle, |
| 86 |
const struct aws_mqtt5_user_property *properties, |
| 87 |
size_t property_count, |
| 88 |
void *log_context, |
| 89 |
enum aws_log_level level, |
| 90 |
const char *log_prefix) { |
| 91 |
|
| 92 |
if (property_count == 0) { |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
AWS_LOGUF( |
| 97 |
log_handle, |
| 98 |
level, |
| 99 |
AWS_LS_MQTT5_GENERAL, |
| 100 |
"id=%p: %s with %zu user properties:", |
| 101 |
log_context, |
| 102 |
log_prefix, |
| 103 |
property_count); |
| 104 |
|
| 105 |
for (size_t i = 0; i < property_count; ++i) { |
| 106 |
const struct aws_mqtt5_user_property *property = &properties[i]; |
| 107 |
|
| 108 |
AWS_LOGUF( |
| 109 |
log_handle, |
| 110 |
level, |
| 111 |
AWS_LS_MQTT5_GENERAL, |
| 112 |
"id=%p: %s user property %zu with name \"" PRInSTR "\", value \"" PRInSTR "\"", |
| 113 |
log_context, |
| 114 |
log_prefix, |
| 115 |
i, |
| 116 |
AWS_BYTE_CURSOR_PRI(property->name), |
| 117 |
AWS_BYTE_CURSOR_PRI(property->value)); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
static size_t s_aws_mqtt5_user_property_set_compute_storage_size( |
| 122 |
const struct aws_mqtt5_user_property *properties, |
| 123 |
size_t property_count) { |
| 124 |
size_t storage_size = 0; |
| 125 |
for (size_t i = 0; i < property_count; ++i) { |
| 126 |
const struct aws_mqtt5_user_property *property = &properties[i]; |
| 127 |
storage_size += property->name.len; |
| 128 |
storage_size += property->value.len; |
| 129 |
} |
| 130 |
|
| 131 |
return storage_size; |
| 132 |
} |
| 133 |
|
| 134 |
static int s_aws_mqtt5_user_property_set_validate( |
| 135 |
const struct aws_mqtt5_user_property *properties, |
| 136 |
size_t property_count, |
| 137 |
const char *log_prefix, |
| 138 |
void *log_context) { |
| 139 |
if (properties == NULL) { |
| 140 |
if (property_count == 0) { |
| 141 |
return AWS_OP_SUCCESS; |
| 142 |
} else { |
| 143 |
AWS_LOGF_ERROR( |
| 144 |
AWS_LS_MQTT5_GENERAL, |
| 145 |
"id=%p: %s - Invalid user property configuration, null properties, non-zero property count", |
| 146 |
log_context, |
| 147 |
log_prefix); |
| 148 |
return aws_raise_error(AWS_ERROR_MQTT5_USER_PROPERTY_VALIDATION); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
if (property_count > AWS_MQTT5_CLIENT_MAXIMUM_USER_PROPERTIES) { |
| 153 |
AWS_LOGF_ERROR( |
| 154 |
AWS_LS_MQTT5_GENERAL, |
| 155 |
"id=%p: %s - user property limit (%d) exceeded (%zu)", |
| 156 |
log_context, |
| 157 |
log_prefix, |
| 158 |
(int)AWS_MQTT5_CLIENT_MAXIMUM_USER_PROPERTIES, |
| 159 |
property_count); |
| 160 |
return aws_raise_error(AWS_ERROR_MQTT5_USER_PROPERTY_VALIDATION); |
| 161 |
} |
| 162 |
|
| 163 |
for (size_t i = 0; i < property_count; ++i) { |
| 164 |
const struct aws_mqtt5_user_property *property = &properties[i]; |
| 165 |
if (property->name.len > UINT16_MAX) { |
| 166 |
AWS_LOGF_ERROR( |
| 167 |
AWS_LS_MQTT5_GENERAL, |
| 168 |
"id=%p: %s - user property #%zu name too long (%zu)", |
| 169 |
log_context, |
| 170 |
log_prefix, |
| 171 |
i, |
| 172 |
property->name.len); |
| 173 |
return aws_raise_error(AWS_ERROR_MQTT5_USER_PROPERTY_VALIDATION); |
| 174 |
} |
| 175 |
|
| 176 |
if (aws_mqtt_validate_utf8_text(property->name)) { |
| 177 |
AWS_LOGF_ERROR( |
| 178 |
AWS_LS_MQTT5_GENERAL, "id=%p: %s - user property #%zu name not valid UTF8", log_context, log_prefix, i); |
| 179 |
return aws_raise_error(AWS_ERROR_MQTT5_USER_PROPERTY_VALIDATION); |
| 180 |
} |
| 181 |
if (property->value.len > UINT16_MAX) { |
| 182 |
AWS_LOGF_ERROR( |
| 183 |
AWS_LS_MQTT5_GENERAL, |
| 184 |
"id=%p: %s - user property #%zu value too long (%zu)", |
| 185 |
log_context, |
| 186 |
log_prefix, |
| 187 |
i, |
| 188 |
property->value.len); |
| 189 |
return aws_raise_error(AWS_ERROR_MQTT5_USER_PROPERTY_VALIDATION); |
| 190 |
} |
| 191 |
if (aws_mqtt_validate_utf8_text(property->value)) { |
| 192 |
AWS_LOGF_ERROR( |
| 193 |
AWS_LS_MQTT5_GENERAL, |
| 194 |
"id=%p: %s - user property #%zu value not valid UTF8", |
| 195 |
log_context, |
| 196 |
log_prefix, |
| 197 |
i); |
| 198 |
return aws_raise_error(AWS_ERROR_MQTT5_USER_PROPERTY_VALIDATION); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
return AWS_OP_SUCCESS; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
struct aws_mqtt5_operation *aws_mqtt5_operation_acquire(struct aws_mqtt5_operation *operation) { |
| 210 |
if (operation == NULL) { |
| 211 |
return NULL; |
| 212 |
} |
| 213 |
|
| 214 |
aws_ref_count_acquire(&operation->ref_count); |
| 215 |
|
| 216 |
return operation; |
| 217 |
} |
| 218 |
|
| 219 |
struct aws_mqtt5_operation *aws_mqtt5_operation_release(struct aws_mqtt5_operation *operation) { |
| 220 |
if (operation != NULL) { |
| 221 |
aws_ref_count_release(&operation->ref_count); |
| 222 |
} |
| 223 |
|
| 224 |
return NULL; |
| 225 |
} |
| 226 |
|
| 227 |
void aws_mqtt5_operation_complete( |
| 228 |
struct aws_mqtt5_operation *operation, |
| 229 |
int error_code, |
| 230 |
enum aws_mqtt5_packet_type packet_type, |
| 231 |
const void *associated_view) { |
| 232 |
AWS_FATAL_ASSERT(operation->vtable != NULL); |
| 233 |
if (operation->vtable->aws_mqtt5_operation_completion_fn != NULL) { |
| 234 |
(*operation->vtable->aws_mqtt5_operation_completion_fn)(operation, error_code, packet_type, associated_view); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
void aws_mqtt5_operation_set_packet_id(struct aws_mqtt5_operation *operation, aws_mqtt5_packet_id_t packet_id) { |
| 239 |
AWS_FATAL_ASSERT(operation->vtable != NULL); |
| 240 |
if (operation->vtable->aws_mqtt5_operation_set_packet_id_fn != NULL) { |
| 241 |
(*operation->vtable->aws_mqtt5_operation_set_packet_id_fn)(operation, packet_id); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
aws_mqtt5_packet_id_t aws_mqtt5_operation_get_packet_id(const struct aws_mqtt5_operation *operation) { |
| 246 |
AWS_FATAL_ASSERT(operation->vtable != NULL); |
| 247 |
if (operation->vtable->aws_mqtt5_operation_get_packet_id_address_fn != NULL) { |
| 248 |
aws_mqtt5_packet_id_t *packet_id_ptr = |
| 249 |
(*operation->vtable->aws_mqtt5_operation_get_packet_id_address_fn)(operation); |
| 250 |
if (packet_id_ptr != NULL) { |
| 251 |
return *packet_id_ptr; |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
return 0; |
| 256 |
} |
| 257 |
|
| 258 |
aws_mqtt5_packet_id_t *aws_mqtt5_operation_get_packet_id_address(const struct aws_mqtt5_operation *operation) { |
| 259 |
AWS_FATAL_ASSERT(operation->vtable != NULL); |
| 260 |
if (operation->vtable->aws_mqtt5_operation_get_packet_id_address_fn != NULL) { |
| 261 |
return (*operation->vtable->aws_mqtt5_operation_get_packet_id_address_fn)(operation); |
| 262 |
} |
| 263 |
|
| 264 |
return NULL; |
| 265 |
} |
| 266 |
|
| 267 |
int aws_mqtt5_operation_validate_vs_connection_settings( |
| 268 |
const struct aws_mqtt5_operation *operation, |
| 269 |
const struct aws_mqtt5_client *client) { |
| 270 |
|
| 271 |
AWS_FATAL_ASSERT(operation->vtable != NULL); |
| 272 |
AWS_FATAL_ASSERT(client->loop == NULL || aws_event_loop_thread_is_callers_thread(client->loop)); |
| 273 |
|
| 274 |
|
| 275 |
if (aws_mqtt5_client_are_negotiated_settings_valid(client)) { |
| 276 |
const struct aws_mqtt5_negotiated_settings *settings = &client->negotiated_settings; |
| 277 |
|
| 278 |
size_t packet_size_in_bytes = 0; |
| 279 |
if (aws_mqtt5_packet_view_get_encoded_size( |
| 280 |
operation->packet_type, operation->packet_view, &packet_size_in_bytes)) { |
| 281 |
int error_code = aws_last_error(); |
| 282 |
AWS_LOGF_ERROR( |
| 283 |
AWS_LS_MQTT5_CLIENT, |
| 284 |
"id=%p: error %d (%s) computing %s packet size", |
| 285 |
(void *)client, |
| 286 |
error_code, |
| 287 |
aws_error_debug_str(error_code), |
| 288 |
aws_mqtt5_packet_type_to_c_string(operation->packet_type)); |
| 289 |
return aws_raise_error(AWS_ERROR_MQTT5_PACKET_VALIDATION); |
| 290 |
} |
| 291 |
|
| 292 |
if (packet_size_in_bytes > settings->maximum_packet_size_to_server) { |
| 293 |
AWS_LOGF_ERROR( |
| 294 |
AWS_LS_MQTT5_CLIENT, |
| 295 |
"id=%p: encoded %s packet size (%zu) exceeds server's maximum " |
| 296 |
"packet size (%" PRIu32 ")", |
| 297 |
(void *)client, |
| 298 |
aws_mqtt5_packet_type_to_c_string(operation->packet_type), |
| 299 |
packet_size_in_bytes, |
| 300 |
settings->maximum_packet_size_to_server); |
| 301 |
return aws_raise_error(AWS_ERROR_MQTT5_PACKET_VALIDATION); |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
if (operation->vtable->aws_mqtt5_operation_validate_vs_connection_settings_fn != NULL) { |
| 306 |
return (*operation->vtable->aws_mqtt5_operation_validate_vs_connection_settings_fn)( |
| 307 |
operation->packet_view, client); |
| 308 |
} |
| 309 |
|
| 310 |
return AWS_OP_SUCCESS; |
| 311 |
} |
| 312 |
|
| 313 |
uint32_t aws_mqtt5_operation_get_ack_timeout_override(const struct aws_mqtt5_operation *operation) { |
| 314 |
if (operation->vtable->aws_mqtt5_operation_get_ack_timeout_override_fn != NULL) { |
| 315 |
return (*operation->vtable->aws_mqtt5_operation_get_ack_timeout_override_fn)(operation); |
| 316 |
} |
| 317 |
|
| 318 |
return 0; |
| 319 |
} |
| 320 |
|
| 321 |
static struct aws_mqtt5_operation_vtable s_empty_operation_vtable = { |
| 322 |
.aws_mqtt5_operation_completion_fn = NULL, |
| 323 |
.aws_mqtt5_operation_set_packet_id_fn = NULL, |
| 324 |
.aws_mqtt5_operation_get_packet_id_address_fn = NULL, |
| 325 |
.aws_mqtt5_operation_validate_vs_connection_settings_fn = NULL, |
| 326 |
.aws_mqtt5_operation_get_ack_timeout_override_fn = NULL, |
| 327 |
}; |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
int aws_mqtt5_packet_connect_view_validate(const struct aws_mqtt5_packet_connect_view *connect_options) { |
| 334 |
if (connect_options == NULL) { |
| 335 |
AWS_LOGF_ERROR(AWS_LS_MQTT5_GENERAL, "Null CONNECT options"); |
| 336 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 337 |
} |
| 338 |
|
| 339 |
if (connect_options->client_id.len > UINT16_MAX) { |
| 340 |
AWS_LOGF_ERROR( |
| 341 |
AWS_LS_MQTT5_GENERAL, "id=%p: aws_mqtt5_packet_connect_view - client id too long", (void *)connect_options); |
| 342 |
return aws_raise_error(AWS_ERROR_MQTT5_CONNECT_OPTIONS_VALIDATION); |
| 343 |
} |
| 344 |
|
| 345 |
if (aws_mqtt_validate_utf8_text(connect_options->client_id)) { |
| 346 |
AWS_LOGF_ERROR( |
| 347 |
AWS_LS_MQTT5_GENERAL, |
| 348 |
"id=%p: aws_mqtt5_packet_connect_view - client id not valid UTF-8", |
| 349 |
(void *)connect_options); |
| 350 |
return aws_raise_error(AWS_ERROR_MQTT5_CONNECT_OPTIONS_VALIDATION); |
| 351 |
} |
| 352 |
|
| 353 |
if (connect_options->username != NULL) { |
| 354 |
if (connect_options->username->len > UINT16_MAX) { |
| 355 |
AWS_LOGF_ERROR( |
| 356 |
AWS_LS_MQTT5_GENERAL, |
| 357 |
"id=%p: aws_mqtt5_packet_connect_view - username too long", |
| 358 |
(void *)connect_options); |
| 359 |
return aws_raise_error(AWS_ERROR_MQTT5_CONNECT_OPTIONS_VALIDATION); |
| 360 |
} |
| 361 |
|
| 362 |
if (aws_mqtt_validate_utf8_text(*connect_options->username)) { |
| 363 |
AWS_LOGF_ERROR( |
| 364 |
AWS_LS_MQTT5_GENERAL, |
| 365 |
"id=%p: aws_mqtt5_packet_connect_view - username not valid UTF-8", |
| 366 |
(void *)connect_options); |
| 367 |
return aws_raise_error(AWS_ERROR_MQTT5_CONNECT_OPTIONS_VALIDATION); |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
if (connect_options->password != NULL) { |
| 372 |
if (connect_options->password->len > UINT16_MAX) { |
| 373 |
AWS_LOGF_ERROR( |
| 374 |
AWS_LS_MQTT5_GENERAL, |
| 375 |
"id=%p: aws_mqtt5_packet_connect_view - password too long", |
| 376 |
(void *)connect_options); |
| 377 |
return aws_raise_error(AWS_ERROR_MQTT5_CONNECT_OPTIONS_VALIDATION); |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
if (connect_options->receive_maximum != NULL) { |
| 382 |
if (*connect_options->receive_maximum == 0) { |
| 383 |
AWS_LOGF_ERROR( |
| 384 |
AWS_LS_MQTT5_GENERAL, |
| 385 |
"id=%p: aws_mqtt5_packet_connect_view - receive maximum property of CONNECT packet may not be zero.", |
| 386 |
(void *)connect_options); |
| 387 |
return aws_raise_error(AWS_ERROR_MQTT5_CONNECT_OPTIONS_VALIDATION); |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
if (connect_options->maximum_packet_size_bytes != NULL) { |
| 392 |
if (*connect_options->maximum_packet_size_bytes == 0) { |
| 393 |
AWS_LOGF_ERROR( |
| 394 |
AWS_LS_MQTT5_GENERAL, |
| 395 |
"id=%p: aws_mqtt5_packet_connect_view - maximum packet size property of CONNECT packet may not be " |
| 396 |
"zero.", |
| 397 |
(void *)connect_options); |
| 398 |
return aws_raise_error(AWS_ERROR_MQTT5_CONNECT_OPTIONS_VALIDATION); |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
if (connect_options->will != NULL) { |
| 403 |
const struct aws_mqtt5_packet_publish_view *will_options = connect_options->will; |
| 404 |
if (aws_mqtt5_packet_publish_view_validate(will_options)) { |
| 405 |
AWS_LOGF_ERROR( |
| 406 |
AWS_LS_MQTT5_GENERAL, |
| 407 |
"id=%p: aws_mqtt5_packet_connect_view - CONNECT packet Will message failed validation", |
| 408 |
(void *)connect_options); |
| 409 |
return AWS_OP_ERR; |
| 410 |
} |
| 411 |
|
| 412 |
if (will_options->payload.len > UINT16_MAX) { |
| 413 |
AWS_LOGF_ERROR( |
| 414 |
AWS_LS_MQTT5_GENERAL, |
| 415 |
"id=%p: aws_mqtt5_packet_connect_view - will payload larger than %d", |
| 416 |
(void *)connect_options, |
| 417 |
(int)UINT16_MAX); |
| 418 |
return aws_raise_error(AWS_ERROR_MQTT5_CONNECT_OPTIONS_VALIDATION); |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
if (connect_options->request_problem_information != NULL) { |
| 423 |
if (*connect_options->request_problem_information > 1) { |
| 424 |
AWS_LOGF_ERROR( |
| 425 |
AWS_LS_MQTT5_GENERAL, |
| 426 |
"id=%p: aws_mqtt5_packet_connect_view - CONNECT packet request problem information has invalid value", |
| 427 |
(void *)connect_options); |
| 428 |
return aws_raise_error(AWS_ERROR_MQTT5_CONNECT_OPTIONS_VALIDATION); |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
if (connect_options->request_response_information != NULL) { |
| 433 |
if (*connect_options->request_response_information > 1) { |
| 434 |
AWS_LOGF_ERROR( |
| 435 |
AWS_LS_MQTT5_GENERAL, |
| 436 |
"id=%p: aws_mqtt5_packet_connect_view - CONNECT packet request response information has invalid value", |
| 437 |
(void *)connect_options); |
| 438 |
return aws_raise_error(AWS_ERROR_MQTT5_CONNECT_OPTIONS_VALIDATION); |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
if (s_aws_mqtt5_user_property_set_validate( |
| 443 |
connect_options->user_properties, |
| 444 |
connect_options->user_property_count, |
| 445 |
"aws_mqtt5_packet_connect_view", |
| 446 |
(void *)connect_options)) { |
| 447 |
return AWS_OP_ERR; |
| 448 |
} |
| 449 |
|
| 450 |
if (connect_options->authentication_method != NULL || connect_options->authentication_data != NULL) { |
| 451 |
AWS_LOGF_ERROR( |
| 452 |
AWS_LS_MQTT5_GENERAL, |
| 453 |
"id=%p: aws_mqtt5_packet_connect_view - CONNECT packet has unsupported authentication fields set.", |
| 454 |
(void *)connect_options); |
| 455 |
return aws_raise_error(AWS_ERROR_MQTT5_CONNECT_OPTIONS_VALIDATION); |
| 456 |
|
| 457 |
|
| 458 |
} |
| 459 |
|
| 460 |
return AWS_OP_SUCCESS; |
| 461 |
} |
| 462 |
|
| 463 |
void aws_mqtt5_packet_connect_view_log( |
| 464 |
const struct aws_mqtt5_packet_connect_view *connect_view, |
| 465 |
enum aws_log_level level) { |
| 466 |
struct aws_logger *log_handle = aws_logger_get_conditional(AWS_LS_MQTT5_GENERAL, level); |
| 467 |
if (log_handle == NULL) { |
| 468 |
return; |
| 469 |
} |
| 470 |
|
| 471 |
AWS_LOGUF( |
| 472 |
log_handle, |
| 473 |
level, |
| 474 |
AWS_LS_MQTT5_GENERAL, |
| 475 |
"id=%p: aws_mqtt5_packet_connect_view keep alive interval set to %" PRIu16, |
| 476 |
(void *)connect_view, |
| 477 |
connect_view->keep_alive_interval_seconds); |
| 478 |
|
| 479 |
AWS_LOGUF( |
| 480 |
log_handle, |
| 481 |
level, |
| 482 |
AWS_LS_MQTT5_GENERAL, |
| 483 |
"id=%p: aws_mqtt5_packet_connect_view client id set to \"" PRInSTR "\"", |
| 484 |
(void *)connect_view, |
| 485 |
AWS_BYTE_CURSOR_PRI(connect_view->client_id)); |
| 486 |
|
| 487 |
if (connect_view->username != NULL) { |
| 488 |
|
| 489 |
AWS_LOGUF( |
| 490 |
log_handle, |
| 491 |
level, |
| 492 |
AWS_LS_MQTT5_GENERAL, |
| 493 |
"id=%p: aws_mqtt5_packet_connect_view username set", |
| 494 |
(void *)connect_view); |
| 495 |
} |
| 496 |
|
| 497 |
if (connect_view->password != NULL) { |
| 498 |
AWS_LOGUF( |
| 499 |
log_handle, |
| 500 |
level, |
| 501 |
AWS_LS_MQTT5_GENERAL, |
| 502 |
"id=%p: aws_mqtt5_packet_connect_view password set", |
| 503 |
(void *)connect_view); |
| 504 |
} |
| 505 |
|
| 506 |
AWS_LOGUF( |
| 507 |
log_handle, |
| 508 |
level, |
| 509 |
AWS_LS_MQTT5_GENERAL, |
| 510 |
"id=%p: aws_mqtt5_packet_connect_view clean start set to %d", |
| 511 |
(void *)connect_view, |
| 512 |
(int)(connect_view->clean_start ? 1 : 0)); |
| 513 |
|
| 514 |
if (connect_view->session_expiry_interval_seconds != NULL) { |
| 515 |
AWS_LOGUF( |
| 516 |
log_handle, |
| 517 |
level, |
| 518 |
AWS_LS_MQTT5_GENERAL, |
| 519 |
"id=%p: aws_mqtt5_packet_connect_view session expiry interval set to %" PRIu32, |
| 520 |
(void *)connect_view, |
| 521 |
*connect_view->session_expiry_interval_seconds); |
| 522 |
} |
| 523 |
|
| 524 |
if (connect_view->request_response_information != NULL) { |
| 525 |
AWS_LOGUF( |
| 526 |
log_handle, |
| 527 |
level, |
| 528 |
AWS_LS_MQTT5_GENERAL, |
| 529 |
"id=%p: aws_mqtt5_packet_connect_view request response information set to %d", |
| 530 |
(void *)connect_view, |
| 531 |
(int)*connect_view->request_response_information); |
| 532 |
} |
| 533 |
|
| 534 |
if (connect_view->request_problem_information) { |
| 535 |
AWS_LOGUF( |
| 536 |
log_handle, |
| 537 |
level, |
| 538 |
AWS_LS_MQTT5_GENERAL, |
| 539 |
"id=%p: aws_mqtt5_packet_connect_view request problem information set to %d", |
| 540 |
(void *)connect_view, |
| 541 |
(int)*connect_view->request_problem_information); |
| 542 |
} |
| 543 |
|
| 544 |
if (connect_view->receive_maximum != NULL) { |
| 545 |
AWS_LOGUF( |
| 546 |
log_handle, |
| 547 |
level, |
| 548 |
AWS_LS_MQTT5_GENERAL, |
| 549 |
"id=%p: aws_mqtt5_packet_connect_view receive maximum set to %" PRIu16, |
| 550 |
(void *)connect_view, |
| 551 |
*connect_view->receive_maximum); |
| 552 |
} |
| 553 |
|
| 554 |
if (connect_view->topic_alias_maximum != NULL) { |
| 555 |
AWS_LOGUF( |
| 556 |
log_handle, |
| 557 |
level, |
| 558 |
AWS_LS_MQTT5_GENERAL, |
| 559 |
"id=%p: aws_mqtt5_packet_connect_view topic alias maximum set to %" PRIu16, |
| 560 |
(void *)connect_view, |
| 561 |
*connect_view->topic_alias_maximum); |
| 562 |
} |
| 563 |
|
| 564 |
if (connect_view->maximum_packet_size_bytes != NULL) { |
| 565 |
AWS_LOGUF( |
| 566 |
log_handle, |
| 567 |
level, |
| 568 |
AWS_LS_MQTT5_GENERAL, |
| 569 |
"id=%p: aws_mqtt5_packet_connect_view maximum packet size set to %" PRIu32, |
| 570 |
(void *)connect_view, |
| 571 |
*connect_view->maximum_packet_size_bytes); |
| 572 |
} |
| 573 |
|
| 574 |
AWS_LOGUF( |
| 575 |
log_handle, |
| 576 |
level, |
| 577 |
AWS_LS_MQTT5_GENERAL, |
| 578 |
"id=%p: aws_mqtt5_packet_connect_view set will to (%p)", |
| 579 |
(void *)connect_view, |
| 580 |
(void *)connect_view->will); |
| 581 |
|
| 582 |
if (connect_view->will != NULL) { |
| 583 |
aws_mqtt5_packet_publish_view_log(connect_view->will, level); |
| 584 |
} |
| 585 |
|
| 586 |
if (connect_view->will_delay_interval_seconds != NULL) { |
| 587 |
AWS_LOGUF( |
| 588 |
log_handle, |
| 589 |
level, |
| 590 |
AWS_LS_MQTT5_GENERAL, |
| 591 |
"id=%p: aws_mqtt5_packet_connect_view will delay interval set to %" PRIu32, |
| 592 |
(void *)connect_view, |
| 593 |
*connect_view->will_delay_interval_seconds); |
| 594 |
} |
| 595 |
|
| 596 |
s_aws_mqtt5_user_property_set_log( |
| 597 |
log_handle, |
| 598 |
connect_view->user_properties, |
| 599 |
connect_view->user_property_count, |
| 600 |
(void *)connect_view, |
| 601 |
level, |
| 602 |
"aws_mqtt5_packet_connect_view"); |
| 603 |
|
| 604 |
if (connect_view->authentication_method != NULL) { |
| 605 |
AWS_LOGUF( |
| 606 |
log_handle, |
| 607 |
level, |
| 608 |
AWS_LS_MQTT5_GENERAL, |
| 609 |
"id=%p: aws_mqtt5_packet_connect_view authentication method set", |
| 610 |
(void *)connect_view); |
| 611 |
} |
| 612 |
|
| 613 |
if (connect_view->password != NULL) { |
| 614 |
AWS_LOGUF( |
| 615 |
log_handle, |
| 616 |
level, |
| 617 |
AWS_LS_MQTT5_GENERAL, |
| 618 |
"id=%p: aws_mqtt5_packet_connect_view authentication data set", |
| 619 |
(void *)connect_view); |
| 620 |
} |
| 621 |
} |
| 622 |
|
| 623 |
void aws_mqtt5_packet_connect_storage_clean_up(struct aws_mqtt5_packet_connect_storage *storage) { |
| 624 |
if (storage == NULL) { |
| 625 |
return; |
| 626 |
} |
| 627 |
|
| 628 |
if (storage->will != NULL) { |
| 629 |
aws_mqtt5_packet_publish_storage_clean_up(storage->will); |
| 630 |
aws_mem_release(storage->allocator, storage->will); |
| 631 |
} |
| 632 |
|
| 633 |
aws_mqtt5_user_property_set_clean_up(&storage->user_properties); |
| 634 |
|
| 635 |
aws_byte_buf_clean_up_secure(&storage->storage); |
| 636 |
} |
| 637 |
|
| 638 |
static size_t s_aws_mqtt5_packet_connect_compute_storage_size( |
| 639 |
const struct aws_mqtt5_packet_connect_view *view, |
| 640 |
const struct aws_mqtt5_client_options_storage *options) { |
| 641 |
if (view == NULL) { |
| 642 |
return 0; |
| 643 |
} |
| 644 |
|
| 645 |
size_t storage_size = 0; |
| 646 |
|
| 647 |
storage_size += view->client_id.len; |
| 648 |
|
| 649 |
if (aws_mqtt_has_non_empty_username(view->username, options ? options->metrics_storage : NULL)) { |
| 650 |
|
| 651 |
if (options) { |
| 652 |
size_t username_size = 0; |
| 653 |
aws_mqtt_append_sdk_metrics_to_username( |
| 654 |
aws_default_allocator(), |
| 655 |
view->username, |
| 656 |
options->metrics_storage ? &options->metrics_storage->storage_view : NULL, |
| 657 |
NULL, |
| 658 |
&username_size); |
| 659 |
storage_size += username_size; |
| 660 |
} else { |
| 661 |
storage_size += view->username->len; |
| 662 |
} |
| 663 |
} |
| 664 |
if (view->password != NULL) { |
| 665 |
storage_size += view->password->len; |
| 666 |
} |
| 667 |
|
| 668 |
storage_size += |
| 669 |
s_aws_mqtt5_user_property_set_compute_storage_size(view->user_properties, view->user_property_count); |
| 670 |
|
| 671 |
if (view->authentication_method != NULL) { |
| 672 |
storage_size += view->authentication_method->len; |
| 673 |
} |
| 674 |
|
| 675 |
if (view->authentication_data != NULL) { |
| 676 |
storage_size += view->authentication_data->len; |
| 677 |
} |
| 678 |
|
| 679 |
return storage_size; |
| 680 |
} |
| 681 |
|
| 682 |
int aws_mqtt5_packet_connect_storage_init( |
| 683 |
struct aws_mqtt5_packet_connect_storage *storage, |
| 684 |
struct aws_allocator *allocator, |
| 685 |
const struct aws_mqtt5_packet_connect_view *view, |
| 686 |
const struct aws_mqtt5_client_options_storage *client_options_storage) { |
| 687 |
AWS_ZERO_STRUCT(*storage); |
| 688 |
|
| 689 |
struct aws_mqtt5_packet_connect_view *storage_view = &storage->storage_view; |
| 690 |
|
| 691 |
size_t storage_capacity = s_aws_mqtt5_packet_connect_compute_storage_size(view, client_options_storage); |
| 692 |
if (aws_byte_buf_init(&storage->storage, allocator, storage_capacity)) { |
| 693 |
return AWS_OP_ERR; |
| 694 |
} |
| 695 |
|
| 696 |
storage->allocator = allocator; |
| 697 |
storage_view->keep_alive_interval_seconds = view->keep_alive_interval_seconds; |
| 698 |
|
| 699 |
storage_view->client_id = view->client_id; |
| 700 |
if (aws_byte_buf_append_and_update(&storage->storage, &storage_view->client_id)) { |
| 701 |
return AWS_OP_ERR; |
| 702 |
} |
| 703 |
|
| 704 |
if (aws_mqtt_has_non_empty_username( |
| 705 |
view->username, client_options_storage ? client_options_storage->metrics_storage : NULL)) { |
| 706 |
if (view->username) { |
| 707 |
storage->username = *view->username; |
| 708 |
} |
| 709 |
struct aws_byte_buf metrics_username_buf; |
| 710 |
AWS_ZERO_STRUCT(metrics_username_buf); |
| 711 |
|
| 712 |
|
| 713 |
if (client_options_storage) { |
| 714 |
if (aws_mqtt_append_sdk_metrics_to_username( |
| 715 |
allocator, |
| 716 |
&storage->username, |
| 717 |
client_options_storage->metrics_storage ? &client_options_storage->metrics_storage->storage_view |
| 718 |
: NULL, |
| 719 |
&metrics_username_buf, |
| 720 |
NULL)) { |
| 721 |
return AWS_OP_ERR; |
| 722 |
} |
| 723 |
storage->username = aws_byte_cursor_from_buf(&metrics_username_buf); |
| 724 |
} |
| 725 |
|
| 726 |
int result = aws_byte_buf_append_and_update(&storage->storage, &storage->username); |
| 727 |
aws_byte_buf_clean_up(&metrics_username_buf); |
| 728 |
if (result == AWS_OP_ERR) { |
| 729 |
return AWS_OP_ERR; |
| 730 |
} |
| 731 |
storage_view->username = &storage->username; |
| 732 |
} |
| 733 |
|
| 734 |
if (view->password != NULL) { |
| 735 |
storage->password = *view->password; |
| 736 |
if (aws_byte_buf_append_and_update(&storage->storage, &storage->password)) { |
| 737 |
return AWS_OP_ERR; |
| 738 |
} |
| 739 |
|
| 740 |
storage_view->password = &storage->password; |
| 741 |
} |
| 742 |
|
| 743 |
storage_view->clean_start = view->clean_start; |
| 744 |
|
| 745 |
if (view->session_expiry_interval_seconds != NULL) { |
| 746 |
storage->session_expiry_interval_seconds = *view->session_expiry_interval_seconds; |
| 747 |
storage_view->session_expiry_interval_seconds = &storage->session_expiry_interval_seconds; |
| 748 |
} |
| 749 |
|
| 750 |
if (view->request_response_information != NULL) { |
| 751 |
storage->request_response_information = *view->request_response_information; |
| 752 |
storage_view->request_response_information = &storage->request_response_information; |
| 753 |
} |
| 754 |
|
| 755 |
if (view->request_problem_information != NULL) { |
| 756 |
storage->request_problem_information = *view->request_problem_information; |
| 757 |
storage_view->request_problem_information = &storage->request_problem_information; |
| 758 |
} |
| 759 |
|
| 760 |
if (view->receive_maximum != NULL) { |
| 761 |
storage->receive_maximum = *view->receive_maximum; |
| 762 |
storage_view->receive_maximum = &storage->receive_maximum; |
| 763 |
} |
| 764 |
|
| 765 |
if (view->topic_alias_maximum != NULL) { |
| 766 |
storage->topic_alias_maximum = *view->topic_alias_maximum; |
| 767 |
storage_view->topic_alias_maximum = &storage->topic_alias_maximum; |
| 768 |
} |
| 769 |
|
| 770 |
if (view->maximum_packet_size_bytes != NULL) { |
| 771 |
storage->maximum_packet_size_bytes = *view->maximum_packet_size_bytes; |
| 772 |
storage_view->maximum_packet_size_bytes = &storage->maximum_packet_size_bytes; |
| 773 |
} |
| 774 |
|
| 775 |
if (view->will != NULL) { |
| 776 |
storage->will = aws_mem_calloc(allocator, 1, sizeof(struct aws_mqtt5_packet_publish_storage)); |
| 777 |
if (storage->will == NULL) { |
| 778 |
return AWS_OP_ERR; |
| 779 |
} |
| 780 |
|
| 781 |
if (aws_mqtt5_packet_publish_storage_init(storage->will, allocator, view->will)) { |
| 782 |
return AWS_OP_ERR; |
| 783 |
} |
| 784 |
|
| 785 |
storage_view->will = &storage->will->storage_view; |
| 786 |
} |
| 787 |
|
| 788 |
if (view->will_delay_interval_seconds != 0) { |
| 789 |
storage->will_delay_interval_seconds = *view->will_delay_interval_seconds; |
| 790 |
storage_view->will_delay_interval_seconds = &storage->will_delay_interval_seconds; |
| 791 |
} |
| 792 |
|
| 793 |
if (aws_mqtt5_user_property_set_init_with_storage( |
| 794 |
&storage->user_properties, |
| 795 |
allocator, |
| 796 |
&storage->storage, |
| 797 |
view->user_property_count, |
| 798 |
view->user_properties)) { |
| 799 |
return AWS_OP_ERR; |
| 800 |
} |
| 801 |
|
| 802 |
storage_view->user_property_count = aws_mqtt5_user_property_set_size(&storage->user_properties); |
| 803 |
storage_view->user_properties = storage->user_properties.properties.data; |
| 804 |
|
| 805 |
if (view->authentication_method != NULL) { |
| 806 |
storage->authentication_method = *view->authentication_method; |
| 807 |
if (aws_byte_buf_append_and_update(&storage->storage, &storage->authentication_method)) { |
| 808 |
return AWS_OP_ERR; |
| 809 |
} |
| 810 |
|
| 811 |
storage_view->authentication_method = &storage->authentication_method; |
| 812 |
} |
| 813 |
|
| 814 |
if (view->authentication_data != NULL) { |
| 815 |
storage->authentication_data = *view->authentication_data; |
| 816 |
if (aws_byte_buf_append_and_update(&storage->storage, &storage->authentication_data)) { |
| 817 |
return AWS_OP_ERR; |
| 818 |
} |
| 819 |
|
| 820 |
storage_view->authentication_data = &storage->authentication_data; |
| 821 |
} |
| 822 |
|
| 823 |
return AWS_OP_SUCCESS; |
| 824 |
} |
| 825 |
|
| 826 |
int aws_mqtt5_packet_connect_storage_init_from_external_storage( |
| 827 |
struct aws_mqtt5_packet_connect_storage *connect_storage, |
| 828 |
struct aws_allocator *allocator) { |
| 829 |
AWS_ZERO_STRUCT(*connect_storage); |
| 830 |
|
| 831 |
if (aws_mqtt5_user_property_set_init(&connect_storage->user_properties, allocator)) { |
| 832 |
return AWS_OP_ERR; |
| 833 |
} |
| 834 |
|
| 835 |
return AWS_OP_SUCCESS; |
| 836 |
} |
| 837 |
|
| 838 |
static void s_destroy_operation_connect(void *object) { |
| 839 |
if (object == NULL) { |
| 840 |
return; |
| 841 |
} |
| 842 |
|
| 843 |
struct aws_mqtt5_operation_connect *connect_op = object; |
| 844 |
|
| 845 |
aws_mqtt5_packet_connect_storage_clean_up(&connect_op->options_storage); |
| 846 |
|
| 847 |
aws_mem_release(connect_op->allocator, connect_op); |
| 848 |
} |
| 849 |
|
| 850 |
struct aws_mqtt5_operation_connect *aws_mqtt5_operation_connect_new( |
| 851 |
struct aws_allocator *allocator, |
| 852 |
const struct aws_mqtt5_packet_connect_view *connect_options, |
| 853 |
const struct aws_mqtt5_client_options_storage *options_storage) { |
| 854 |
AWS_PRECONDITION(allocator != NULL); |
| 855 |
AWS_PRECONDITION(connect_options != NULL); |
| 856 |
|
| 857 |
if (aws_mqtt5_packet_connect_view_validate(connect_options)) { |
| 858 |
return NULL; |
| 859 |
} |
| 860 |
|
| 861 |
struct aws_mqtt5_operation_connect *connect_op = |
| 862 |
aws_mem_calloc(allocator, 1, sizeof(struct aws_mqtt5_operation_connect)); |
| 863 |
if (connect_op == NULL) { |
| 864 |
return NULL; |
| 865 |
} |
| 866 |
|
| 867 |
connect_op->allocator = allocator; |
| 868 |
connect_op->base.vtable = &s_empty_operation_vtable; |
| 869 |
connect_op->base.packet_type = AWS_MQTT5_PT_CONNECT; |
| 870 |
aws_ref_count_init(&connect_op->base.ref_count, connect_op, s_destroy_operation_connect); |
| 871 |
aws_priority_queue_node_init(&connect_op->base.priority_queue_node); |
| 872 |
connect_op->base.impl = connect_op; |
| 873 |
|
| 874 |
if (aws_mqtt5_packet_connect_storage_init( |
| 875 |
&connect_op->options_storage, allocator, connect_options, options_storage)) { |
| 876 |
goto error; |
| 877 |
} |
| 878 |
|
| 879 |
connect_op->base.packet_view = &connect_op->options_storage.storage_view; |
| 880 |
|
| 881 |
return connect_op; |
| 882 |
|
| 883 |
error: |
| 884 |
|
| 885 |
aws_mqtt5_operation_release(&connect_op->base); |
| 886 |
|
| 887 |
return NULL; |
| 888 |
} |
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
static size_t s_aws_mqtt5_packet_connack_compute_storage_size(const struct aws_mqtt5_packet_connack_view *view) { |
| 895 |
if (view == NULL) { |
| 896 |
return 0; |
| 897 |
} |
| 898 |
|
| 899 |
size_t storage_size = 0; |
| 900 |
|
| 901 |
if (view->assigned_client_identifier != NULL) { |
| 902 |
storage_size += view->assigned_client_identifier->len; |
| 903 |
} |
| 904 |
|
| 905 |
if (view->reason_string != NULL) { |
| 906 |
storage_size += view->reason_string->len; |
| 907 |
} |
| 908 |
|
| 909 |
if (view->response_information != NULL) { |
| 910 |
storage_size += view->response_information->len; |
| 911 |
} |
| 912 |
|
| 913 |
if (view->server_reference != NULL) { |
| 914 |
storage_size += view->server_reference->len; |
| 915 |
} |
| 916 |
|
| 917 |
if (view->authentication_method != NULL) { |
| 918 |
storage_size += view->authentication_method->len; |
| 919 |
} |
| 920 |
|
| 921 |
if (view->authentication_data != NULL) { |
| 922 |
storage_size += view->authentication_data->len; |
| 923 |
} |
| 924 |
|
| 925 |
storage_size += |
| 926 |
s_aws_mqtt5_user_property_set_compute_storage_size(view->user_properties, view->user_property_count); |
| 927 |
|
| 928 |
return storage_size; |
| 929 |
} |
| 930 |
|
| 931 |
int aws_mqtt5_packet_connack_storage_init( |
| 932 |
struct aws_mqtt5_packet_connack_storage *connack_storage, |
| 933 |
struct aws_allocator *allocator, |
| 934 |
const struct aws_mqtt5_packet_connack_view *connack_view) { |
| 935 |
|
| 936 |
AWS_ZERO_STRUCT(*connack_storage); |
| 937 |
size_t storage_capacity = s_aws_mqtt5_packet_connack_compute_storage_size(connack_view); |
| 938 |
if (aws_byte_buf_init(&connack_storage->storage, allocator, storage_capacity)) { |
| 939 |
return AWS_OP_ERR; |
| 940 |
} |
| 941 |
|
| 942 |
struct aws_mqtt5_packet_connack_view *stored_view = &connack_storage->storage_view; |
| 943 |
|
| 944 |
connack_storage->allocator = allocator; |
| 945 |
|
| 946 |
stored_view->session_present = connack_view->session_present; |
| 947 |
stored_view->reason_code = connack_view->reason_code; |
| 948 |
|
| 949 |
if (connack_view->session_expiry_interval != NULL) { |
| 950 |
connack_storage->session_expiry_interval = *connack_view->session_expiry_interval; |
| 951 |
stored_view->session_expiry_interval = &connack_storage->session_expiry_interval; |
| 952 |
} |
| 953 |
|
| 954 |
if (connack_view->receive_maximum != NULL) { |
| 955 |
connack_storage->receive_maximum = *connack_view->receive_maximum; |
| 956 |
stored_view->receive_maximum = &connack_storage->receive_maximum; |
| 957 |
} |
| 958 |
|
| 959 |
if (connack_view->maximum_qos != NULL) { |
| 960 |
connack_storage->maximum_qos = *connack_view->maximum_qos; |
| 961 |
stored_view->maximum_qos = &connack_storage->maximum_qos; |
| 962 |
} |
| 963 |
|
| 964 |
if (connack_view->retain_available != NULL) { |
| 965 |
connack_storage->retain_available = *connack_view->retain_available; |
| 966 |
stored_view->retain_available = &connack_storage->retain_available; |
| 967 |
} |
| 968 |
|
| 969 |
if (connack_view->maximum_packet_size != NULL) { |
| 970 |
connack_storage->maximum_packet_size = *connack_view->maximum_packet_size; |
| 971 |
stored_view->maximum_packet_size = &connack_storage->maximum_packet_size; |
| 972 |
} |
| 973 |
|
| 974 |
if (connack_view->assigned_client_identifier != NULL) { |
| 975 |
connack_storage->assigned_client_identifier = *connack_view->assigned_client_identifier; |
| 976 |
if (aws_byte_buf_append_and_update(&connack_storage->storage, &connack_storage->assigned_client_identifier)) { |
| 977 |
return AWS_OP_ERR; |
| 978 |
} |
| 979 |
|
| 980 |
stored_view->assigned_client_identifier = &connack_storage->assigned_client_identifier; |
| 981 |
} |
| 982 |
|
| 983 |
if (connack_view->topic_alias_maximum != NULL) { |
| 984 |
connack_storage->topic_alias_maximum = *connack_view->topic_alias_maximum; |
| 985 |
stored_view->topic_alias_maximum = &connack_storage->topic_alias_maximum; |
| 986 |
} |
| 987 |
|
| 988 |
if (connack_view->reason_string != NULL) { |
| 989 |
connack_storage->reason_string = *connack_view->reason_string; |
| 990 |
if (aws_byte_buf_append_and_update(&connack_storage->storage, &connack_storage->reason_string)) { |
| 991 |
return AWS_OP_ERR; |
| 992 |
} |
| 993 |
|
| 994 |
stored_view->reason_string = &connack_storage->reason_string; |
| 995 |
} |
| 996 |
|
| 997 |
if (connack_view->wildcard_subscriptions_available != NULL) { |
| 998 |
connack_storage->wildcard_subscriptions_available = *connack_view->wildcard_subscriptions_available; |
| 999 |
stored_view->wildcard_subscriptions_available = &connack_storage->wildcard_subscriptions_available; |
| 1000 |
} |
| 1001 |
|
| 1002 |
if (connack_view->subscription_identifiers_available != NULL) { |
| 1003 |
connack_storage->subscription_identifiers_available = *connack_view->subscription_identifiers_available; |
| 1004 |
stored_view->subscription_identifiers_available = &connack_storage->subscription_identifiers_available; |
| 1005 |
} |
| 1006 |
|
| 1007 |
if (connack_view->shared_subscriptions_available != NULL) { |
| 1008 |
connack_storage->shared_subscriptions_available = *connack_view->shared_subscriptions_available; |
| 1009 |
stored_view->shared_subscriptions_available = &connack_storage->shared_subscriptions_available; |
| 1010 |
} |
| 1011 |
|
| 1012 |
if (connack_view->server_keep_alive != NULL) { |
| 1013 |
connack_storage->server_keep_alive = *connack_view->server_keep_alive; |
| 1014 |
stored_view->server_keep_alive = &connack_storage->server_keep_alive; |
| 1015 |
} |
| 1016 |
|
| 1017 |
if (connack_view->response_information != NULL) { |
| 1018 |
connack_storage->response_information = *connack_view->response_information; |
| 1019 |
if (aws_byte_buf_append_and_update(&connack_storage->storage, &connack_storage->response_information)) { |
| 1020 |
return AWS_OP_ERR; |
| 1021 |
} |
| 1022 |
|
| 1023 |
stored_view->response_information = &connack_storage->response_information; |
| 1024 |
} |
| 1025 |
|
| 1026 |
if (connack_view->server_reference != NULL) { |
| 1027 |
connack_storage->server_reference = *connack_view->server_reference; |
| 1028 |
if (aws_byte_buf_append_and_update(&connack_storage->storage, &connack_storage->server_reference)) { |
| 1029 |
return AWS_OP_ERR; |
| 1030 |
} |
| 1031 |
|
| 1032 |
stored_view->server_reference = &connack_storage->server_reference; |
| 1033 |
} |
| 1034 |
|
| 1035 |
if (connack_view->authentication_method != NULL) { |
| 1036 |
connack_storage->authentication_method = *connack_view->authentication_method; |
| 1037 |
if (aws_byte_buf_append_and_update(&connack_storage->storage, &connack_storage->authentication_method)) { |
| 1038 |
return AWS_OP_ERR; |
| 1039 |
} |
| 1040 |
|
| 1041 |
stored_view->authentication_method = &connack_storage->authentication_method; |
| 1042 |
} |
| 1043 |
|
| 1044 |
if (connack_view->authentication_data != NULL) { |
| 1045 |
connack_storage->authentication_data = *connack_view->authentication_data; |
| 1046 |
if (aws_byte_buf_append_and_update(&connack_storage->storage, &connack_storage->authentication_data)) { |
| 1047 |
return AWS_OP_ERR; |
| 1048 |
} |
| 1049 |
|
| 1050 |
stored_view->authentication_data = &connack_storage->authentication_data; |
| 1051 |
} |
| 1052 |
|
| 1053 |
if (aws_mqtt5_user_property_set_init_with_storage( |
| 1054 |
&connack_storage->user_properties, |
| 1055 |
allocator, |
| 1056 |
&connack_storage->storage, |
| 1057 |
connack_view->user_property_count, |
| 1058 |
connack_view->user_properties)) { |
| 1059 |
return AWS_OP_ERR; |
| 1060 |
} |
| 1061 |
|
| 1062 |
stored_view->user_property_count = aws_mqtt5_user_property_set_size(&connack_storage->user_properties); |
| 1063 |
stored_view->user_properties = connack_storage->user_properties.properties.data; |
| 1064 |
|
| 1065 |
return AWS_OP_SUCCESS; |
| 1066 |
} |
| 1067 |
|
| 1068 |
int aws_mqtt5_packet_connack_storage_init_from_external_storage( |
| 1069 |
struct aws_mqtt5_packet_connack_storage *connack_storage, |
| 1070 |
struct aws_allocator *allocator) { |
| 1071 |
AWS_ZERO_STRUCT(*connack_storage); |
| 1072 |
|
| 1073 |
if (aws_mqtt5_user_property_set_init(&connack_storage->user_properties, allocator)) { |
| 1074 |
return AWS_OP_ERR; |
| 1075 |
} |
| 1076 |
|
| 1077 |
return AWS_OP_SUCCESS; |
| 1078 |
} |
| 1079 |
|
| 1080 |
void aws_mqtt5_packet_connack_storage_clean_up(struct aws_mqtt5_packet_connack_storage *connack_storage) { |
| 1081 |
if (connack_storage == NULL) { |
| 1082 |
return; |
| 1083 |
} |
| 1084 |
|
| 1085 |
aws_mqtt5_user_property_set_clean_up(&connack_storage->user_properties); |
| 1086 |
aws_byte_buf_clean_up(&connack_storage->storage); |
| 1087 |
} |
| 1088 |
|
| 1089 |
void aws_mqtt5_packet_connack_view_log( |
| 1090 |
const struct aws_mqtt5_packet_connack_view *connack_view, |
| 1091 |
enum aws_log_level level) { |
| 1092 |
struct aws_logger *log_handle = aws_logger_get_conditional(AWS_LS_MQTT5_GENERAL, level); |
| 1093 |
if (log_handle == NULL) { |
| 1094 |
return; |
| 1095 |
} |
| 1096 |
|
| 1097 |
AWS_LOGUF( |
| 1098 |
log_handle, |
| 1099 |
level, |
| 1100 |
AWS_LS_MQTT5_GENERAL, |
| 1101 |
"id=%p: aws_mqtt5_packet_connack_view reason code set to %d (%s)", |
| 1102 |
(void *)connack_view, |
| 1103 |
(int)connack_view->reason_code, |
| 1104 |
aws_mqtt5_connect_reason_code_to_c_string(connack_view->reason_code)); |
| 1105 |
|
| 1106 |
AWS_LOGUF( |
| 1107 |
log_handle, |
| 1108 |
level, |
| 1109 |
AWS_LS_MQTT5_GENERAL, |
| 1110 |
"id=%p: aws_mqtt5_packet_connack_view session present set to %d", |
| 1111 |
(void *)connack_view, |
| 1112 |
(int)connack_view->session_present); |
| 1113 |
|
| 1114 |
if (connack_view->session_expiry_interval != NULL) { |
| 1115 |
AWS_LOGUF( |
| 1116 |
log_handle, |
| 1117 |
level, |
| 1118 |
AWS_LS_MQTT5_GENERAL, |
| 1119 |
"id=%p: aws_mqtt5_packet_connack_view session expiry interval set to %" PRIu32, |
| 1120 |
(void *)connack_view, |
| 1121 |
*connack_view->session_expiry_interval); |
| 1122 |
} |
| 1123 |
|
| 1124 |
if (connack_view->receive_maximum != NULL) { |
| 1125 |
AWS_LOGUF( |
| 1126 |
log_handle, |
| 1127 |
level, |
| 1128 |
AWS_LS_MQTT5_GENERAL, |
| 1129 |
"id=%p: aws_mqtt5_packet_connack_view receive maximum set to %" PRIu16, |
| 1130 |
(void *)connack_view, |
| 1131 |
*connack_view->receive_maximum); |
| 1132 |
} |
| 1133 |
|
| 1134 |
if (connack_view->maximum_qos != NULL) { |
| 1135 |
AWS_LOGUF( |
| 1136 |
log_handle, |
| 1137 |
level, |
| 1138 |
AWS_LS_MQTT5_GENERAL, |
| 1139 |
"id=%p: aws_mqtt5_packet_connack_view maximum qos set to %d", |
| 1140 |
(void *)connack_view, |
| 1141 |
(int)(*connack_view->maximum_qos)); |
| 1142 |
} |
| 1143 |
|
| 1144 |
if (connack_view->retain_available != NULL) { |
| 1145 |
AWS_LOGUF( |
| 1146 |
log_handle, |
| 1147 |
level, |
| 1148 |
AWS_LS_MQTT5_GENERAL, |
| 1149 |
"id=%p: aws_mqtt5_packet_connack_view retain available set to %d", |
| 1150 |
(void *)connack_view, |
| 1151 |
(int)(*connack_view->retain_available)); |
| 1152 |
} |
| 1153 |
|
| 1154 |
if (connack_view->maximum_packet_size != NULL) { |
| 1155 |
AWS_LOGUF( |
| 1156 |
log_handle, |
| 1157 |
level, |
| 1158 |
AWS_LS_MQTT5_GENERAL, |
| 1159 |
"id=%p: aws_mqtt5_packet_connack_view maximum packet size set to %" PRIu32, |
| 1160 |
(void *)connack_view, |
| 1161 |
*connack_view->maximum_packet_size); |
| 1162 |
} |
| 1163 |
|
| 1164 |
if (connack_view->assigned_client_identifier != NULL) { |
| 1165 |
AWS_LOGUF( |
| 1166 |
log_handle, |
| 1167 |
level, |
| 1168 |
AWS_LS_MQTT5_GENERAL, |
| 1169 |
"id=%p: aws_mqtt5_packet_connack_view assigned client identifier set to \"" PRInSTR "\"", |
| 1170 |
(void *)connack_view, |
| 1171 |
AWS_BYTE_CURSOR_PRI(*connack_view->assigned_client_identifier)); |
| 1172 |
} |
| 1173 |
|
| 1174 |
if (connack_view->topic_alias_maximum != NULL) { |
| 1175 |
AWS_LOGUF( |
| 1176 |
log_handle, |
| 1177 |
level, |
| 1178 |
AWS_LS_MQTT5_GENERAL, |
| 1179 |
"id=%p: aws_mqtt5_packet_connack_view topic alias maximum set to %" PRIu16, |
| 1180 |
(void *)connack_view, |
| 1181 |
*connack_view->topic_alias_maximum); |
| 1182 |
} |
| 1183 |
|
| 1184 |
if (connack_view->reason_string != NULL) { |
| 1185 |
AWS_LOGUF( |
| 1186 |
log_handle, |
| 1187 |
level, |
| 1188 |
AWS_LS_MQTT5_GENERAL, |
| 1189 |
"id=%p: aws_mqtt5_packet_connack_view reason string set to \"" PRInSTR "\"", |
| 1190 |
(void *)connack_view, |
| 1191 |
AWS_BYTE_CURSOR_PRI(*connack_view->reason_string)); |
| 1192 |
} |
| 1193 |
|
| 1194 |
if (connack_view->wildcard_subscriptions_available != NULL) { |
| 1195 |
AWS_LOGUF( |
| 1196 |
log_handle, |
| 1197 |
level, |
| 1198 |
AWS_LS_MQTT5_GENERAL, |
| 1199 |
"id=%p: aws_mqtt5_packet_connack_view wildcard subscriptions available set to %d", |
| 1200 |
(void *)connack_view, |
| 1201 |
(int)(*connack_view->wildcard_subscriptions_available)); |
| 1202 |
} |
| 1203 |
|
| 1204 |
if (connack_view->subscription_identifiers_available != NULL) { |
| 1205 |
AWS_LOGUF( |
| 1206 |
log_handle, |
| 1207 |
level, |
| 1208 |
AWS_LS_MQTT5_GENERAL, |
| 1209 |
"id=%p: aws_mqtt5_packet_connack_view subscription identifiers available set to %d", |
| 1210 |
(void *)connack_view, |
| 1211 |
(int)(*connack_view->subscription_identifiers_available)); |
| 1212 |
} |
| 1213 |
|
| 1214 |
if (connack_view->shared_subscriptions_available != NULL) { |
| 1215 |
AWS_LOGUF( |
| 1216 |
log_handle, |
| 1217 |
level, |
| 1218 |
AWS_LS_MQTT5_GENERAL, |
| 1219 |
"id=%p: aws_mqtt5_packet_connack_view shared subscriptions available set to %d", |
| 1220 |
(void *)connack_view, |
| 1221 |
(int)(*connack_view->shared_subscriptions_available)); |
| 1222 |
} |
| 1223 |
|
| 1224 |
if (connack_view->server_keep_alive != NULL) { |
| 1225 |
AWS_LOGUF( |
| 1226 |
log_handle, |
| 1227 |
level, |
| 1228 |
AWS_LS_MQTT5_GENERAL, |
| 1229 |
"id=%p: aws_mqtt5_packet_connack_view server keep alive set to %" PRIu16, |
| 1230 |
(void *)connack_view, |
| 1231 |
*connack_view->server_keep_alive); |
| 1232 |
} |
| 1233 |
|
| 1234 |
if (connack_view->response_information != NULL) { |
| 1235 |
AWS_LOGUF( |
| 1236 |
log_handle, |
| 1237 |
level, |
| 1238 |
AWS_LS_MQTT5_GENERAL, |
| 1239 |
"id=%p: aws_mqtt5_packet_connack_view response information set to \"" PRInSTR "\"", |
| 1240 |
(void *)connack_view, |
| 1241 |
AWS_BYTE_CURSOR_PRI(*connack_view->response_information)); |
| 1242 |
} |
| 1243 |
|
| 1244 |
if (connack_view->server_reference != NULL) { |
| 1245 |
AWS_LOGUF( |
| 1246 |
log_handle, |
| 1247 |
level, |
| 1248 |
AWS_LS_MQTT5_GENERAL, |
| 1249 |
"id=%p: aws_mqtt5_packet_connack_view server reference set to \"" PRInSTR "\"", |
| 1250 |
(void *)connack_view, |
| 1251 |
AWS_BYTE_CURSOR_PRI(*connack_view->server_reference)); |
| 1252 |
} |
| 1253 |
|
| 1254 |
if (connack_view->authentication_method != NULL) { |
| 1255 |
AWS_LOGUF( |
| 1256 |
log_handle, |
| 1257 |
level, |
| 1258 |
AWS_LS_MQTT5_GENERAL, |
| 1259 |
"id=%p: aws_mqtt5_packet_connack_view authentication method set", |
| 1260 |
(void *)connack_view); |
| 1261 |
} |
| 1262 |
|
| 1263 |
if (connack_view->authentication_data != NULL) { |
| 1264 |
AWS_LOGUF( |
| 1265 |
log_handle, |
| 1266 |
level, |
| 1267 |
AWS_LS_MQTT5_GENERAL, |
| 1268 |
"id=%p: aws_mqtt5_packet_connack_view authentication data set", |
| 1269 |
(void *)connack_view); |
| 1270 |
} |
| 1271 |
|
| 1272 |
s_aws_mqtt5_user_property_set_log( |
| 1273 |
log_handle, |
| 1274 |
connack_view->user_properties, |
| 1275 |
connack_view->user_property_count, |
| 1276 |
(void *)connack_view, |
| 1277 |
level, |
| 1278 |
"aws_mqtt5_packet_connack_view"); |
| 1279 |
} |
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
int aws_mqtt5_packet_disconnect_view_validate(const struct aws_mqtt5_packet_disconnect_view *disconnect_view) { |
| 1286 |
|
| 1287 |
if (disconnect_view == NULL) { |
| 1288 |
AWS_LOGF_ERROR(AWS_LS_MQTT5_GENERAL, "null DISCONNECT packet options"); |
| 1289 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 1290 |
} |
| 1291 |
|
| 1292 |
bool is_valid_reason_code = true; |
| 1293 |
aws_mqtt5_disconnect_reason_code_to_c_string(disconnect_view->reason_code, &is_valid_reason_code); |
| 1294 |
if (!is_valid_reason_code) { |
| 1295 |
AWS_LOGF_ERROR( |
| 1296 |
AWS_LS_MQTT5_GENERAL, |
| 1297 |
"id=%p: aws_mqtt5_packet_disconnect_view - invalid DISCONNECT reason code:%d", |
| 1298 |
(void *)disconnect_view, |
| 1299 |
(int)disconnect_view->reason_code); |
| 1300 |
return aws_raise_error(AWS_ERROR_MQTT5_DISCONNECT_OPTIONS_VALIDATION); |
| 1301 |
} |
| 1302 |
|
| 1303 |
if (disconnect_view->reason_string != NULL) { |
| 1304 |
if (disconnect_view->reason_string->len > UINT16_MAX) { |
| 1305 |
AWS_LOGF_ERROR( |
| 1306 |
AWS_LS_MQTT5_GENERAL, |
| 1307 |
"id=%p: aws_mqtt5_packet_disconnect_view - reason string too long", |
| 1308 |
(void *)disconnect_view); |
| 1309 |
return aws_raise_error(AWS_ERROR_MQTT5_DISCONNECT_OPTIONS_VALIDATION); |
| 1310 |
} |
| 1311 |
|
| 1312 |
if (aws_mqtt_validate_utf8_text(*disconnect_view->reason_string)) { |
| 1313 |
AWS_LOGF_ERROR( |
| 1314 |
AWS_LS_MQTT5_GENERAL, |
| 1315 |
"id=%p: aws_mqtt5_packet_disconnect_view - reason string not valid UTF-8", |
| 1316 |
(void *)disconnect_view); |
| 1317 |
return aws_raise_error(AWS_ERROR_MQTT5_DISCONNECT_OPTIONS_VALIDATION); |
| 1318 |
} |
| 1319 |
} |
| 1320 |
|
| 1321 |
if (disconnect_view->server_reference != NULL) { |
| 1322 |
AWS_LOGF_ERROR( |
| 1323 |
AWS_LS_MQTT5_GENERAL, |
| 1324 |
"id=%p: aws_mqtt5_packet_disconnect_view - sending a server reference with a client-sourced DISCONNECT is " |
| 1325 |
"not allowed", |
| 1326 |
(void *)disconnect_view); |
| 1327 |
return aws_raise_error(AWS_ERROR_MQTT5_DISCONNECT_OPTIONS_VALIDATION); |
| 1328 |
} |
| 1329 |
|
| 1330 |
if (s_aws_mqtt5_user_property_set_validate( |
| 1331 |
disconnect_view->user_properties, |
| 1332 |
disconnect_view->user_property_count, |
| 1333 |
"aws_mqtt5_packet_disconnect_view", |
| 1334 |
(void *)disconnect_view)) { |
| 1335 |
return AWS_OP_ERR; |
| 1336 |
} |
| 1337 |
|
| 1338 |
return AWS_OP_SUCCESS; |
| 1339 |
} |
| 1340 |
|
| 1341 |
static int s_aws_mqtt5_packet_disconnect_view_validate_vs_connection_settings( |
| 1342 |
const void *packet_view, |
| 1343 |
const struct aws_mqtt5_client *client) { |
| 1344 |
|
| 1345 |
const struct aws_mqtt5_packet_disconnect_view *disconnect_view = packet_view; |
| 1346 |
|
| 1347 |
if (disconnect_view->session_expiry_interval_seconds != NULL) { |
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
const uint32_t *session_expiry_ptr = client->config->connect->storage_view.session_expiry_interval_seconds; |
| 1354 |
if (*disconnect_view->session_expiry_interval_seconds > 0 && |
| 1355 |
(session_expiry_ptr == NULL || *session_expiry_ptr == 0)) { |
| 1356 |
AWS_LOGF_ERROR( |
| 1357 |
AWS_LS_MQTT5_GENERAL, |
| 1358 |
"id=%p: aws_mqtt5_packet_disconnect_view - cannot specify a positive session expiry after " |
| 1359 |
"committing " |
| 1360 |
"to 0-valued session expiry in CONNECT", |
| 1361 |
(void *)disconnect_view); |
| 1362 |
return aws_raise_error(AWS_ERROR_MQTT5_DISCONNECT_OPTIONS_VALIDATION); |
| 1363 |
} |
| 1364 |
} |
| 1365 |
|
| 1366 |
return AWS_OP_SUCCESS; |
| 1367 |
} |
| 1368 |
|
| 1369 |
void aws_mqtt5_packet_disconnect_view_log( |
| 1370 |
const struct aws_mqtt5_packet_disconnect_view *disconnect_view, |
| 1371 |
enum aws_log_level level) { |
| 1372 |
struct aws_logger *log_handle = aws_logger_get_conditional(AWS_LS_MQTT5_GENERAL, level); |
| 1373 |
if (log_handle == NULL) { |
| 1374 |
return; |
| 1375 |
} |
| 1376 |
|
| 1377 |
AWS_LOGUF( |
| 1378 |
log_handle, |
| 1379 |
level, |
| 1380 |
AWS_LS_MQTT5_GENERAL, |
| 1381 |
"id=%p: aws_mqtt5_packet_disconnect_view reason code set to %d (%s)", |
| 1382 |
(void *)disconnect_view, |
| 1383 |
(int)disconnect_view->reason_code, |
| 1384 |
aws_mqtt5_disconnect_reason_code_to_c_string(disconnect_view->reason_code, NULL)); |
| 1385 |
|
| 1386 |
if (disconnect_view->session_expiry_interval_seconds != NULL) { |
| 1387 |
AWS_LOGUF( |
| 1388 |
log_handle, |
| 1389 |
level, |
| 1390 |
AWS_LS_MQTT5_GENERAL, |
| 1391 |
"id=%p: aws_mqtt5_packet_disconnect_view session expiry interval set to %" PRIu32, |
| 1392 |
(void *)disconnect_view, |
| 1393 |
*disconnect_view->session_expiry_interval_seconds); |
| 1394 |
} |
| 1395 |
|
| 1396 |
if (disconnect_view->reason_string != NULL) { |
| 1397 |
AWS_LOGUF( |
| 1398 |
log_handle, |
| 1399 |
level, |
| 1400 |
AWS_LS_MQTT5_GENERAL, |
| 1401 |
"id=%p: aws_mqtt5_packet_disconnect_view reason string set to \"" PRInSTR "\"", |
| 1402 |
(void *)disconnect_view, |
| 1403 |
AWS_BYTE_CURSOR_PRI(*disconnect_view->reason_string)); |
| 1404 |
} |
| 1405 |
|
| 1406 |
if (disconnect_view->server_reference != NULL) { |
| 1407 |
AWS_LOGUF( |
| 1408 |
log_handle, |
| 1409 |
level, |
| 1410 |
AWS_LS_MQTT5_GENERAL, |
| 1411 |
"id=%p: aws_mqtt5_packet_disconnect_view server reference set to \"" PRInSTR "\"", |
| 1412 |
(void *)disconnect_view, |
| 1413 |
AWS_BYTE_CURSOR_PRI(*disconnect_view->server_reference)); |
| 1414 |
} |
| 1415 |
|
| 1416 |
s_aws_mqtt5_user_property_set_log( |
| 1417 |
log_handle, |
| 1418 |
disconnect_view->user_properties, |
| 1419 |
disconnect_view->user_property_count, |
| 1420 |
(void *)disconnect_view, |
| 1421 |
level, |
| 1422 |
"aws_mqtt5_packet_disconnect_view"); |
| 1423 |
} |
| 1424 |
|
| 1425 |
void aws_mqtt5_packet_disconnect_storage_clean_up(struct aws_mqtt5_packet_disconnect_storage *disconnect_storage) { |
| 1426 |
if (disconnect_storage == NULL) { |
| 1427 |
return; |
| 1428 |
} |
| 1429 |
|
| 1430 |
aws_mqtt5_user_property_set_clean_up(&disconnect_storage->user_properties); |
| 1431 |
aws_byte_buf_clean_up(&disconnect_storage->storage); |
| 1432 |
} |
| 1433 |
|
| 1434 |
static size_t s_aws_mqtt5_packet_disconnect_compute_storage_size( |
| 1435 |
const struct aws_mqtt5_packet_disconnect_view *disconnect_view) { |
| 1436 |
size_t storage_size = s_aws_mqtt5_user_property_set_compute_storage_size( |
| 1437 |
disconnect_view->user_properties, disconnect_view->user_property_count); |
| 1438 |
|
| 1439 |
if (disconnect_view->reason_string != NULL) { |
| 1440 |
storage_size += disconnect_view->reason_string->len; |
| 1441 |
} |
| 1442 |
|
| 1443 |
if (disconnect_view->server_reference != NULL) { |
| 1444 |
storage_size += disconnect_view->server_reference->len; |
| 1445 |
} |
| 1446 |
|
| 1447 |
return storage_size; |
| 1448 |
} |
| 1449 |
|
| 1450 |
int aws_mqtt5_packet_disconnect_storage_init( |
| 1451 |
struct aws_mqtt5_packet_disconnect_storage *disconnect_storage, |
| 1452 |
struct aws_allocator *allocator, |
| 1453 |
const struct aws_mqtt5_packet_disconnect_view *disconnect_options) { |
| 1454 |
|
| 1455 |
AWS_ZERO_STRUCT(*disconnect_storage); |
| 1456 |
size_t storage_capacity = s_aws_mqtt5_packet_disconnect_compute_storage_size(disconnect_options); |
| 1457 |
if (aws_byte_buf_init(&disconnect_storage->storage, allocator, storage_capacity)) { |
| 1458 |
return AWS_OP_ERR; |
| 1459 |
} |
| 1460 |
|
| 1461 |
struct aws_mqtt5_packet_disconnect_view *storage_view = &disconnect_storage->storage_view; |
| 1462 |
|
| 1463 |
storage_view->reason_code = disconnect_options->reason_code; |
| 1464 |
|
| 1465 |
if (disconnect_options->session_expiry_interval_seconds != NULL) { |
| 1466 |
disconnect_storage->session_expiry_interval_seconds = *disconnect_options->session_expiry_interval_seconds; |
| 1467 |
storage_view->session_expiry_interval_seconds = &disconnect_storage->session_expiry_interval_seconds; |
| 1468 |
} |
| 1469 |
|
| 1470 |
if (disconnect_options->reason_string != NULL) { |
| 1471 |
disconnect_storage->reason_string = *disconnect_options->reason_string; |
| 1472 |
if (aws_byte_buf_append_and_update(&disconnect_storage->storage, &disconnect_storage->reason_string)) { |
| 1473 |
return AWS_OP_ERR; |
| 1474 |
} |
| 1475 |
|
| 1476 |
storage_view->reason_string = &disconnect_storage->reason_string; |
| 1477 |
} |
| 1478 |
|
| 1479 |
if (disconnect_options->server_reference != NULL) { |
| 1480 |
disconnect_storage->server_reference = *disconnect_options->server_reference; |
| 1481 |
if (aws_byte_buf_append_and_update(&disconnect_storage->storage, &disconnect_storage->server_reference)) { |
| 1482 |
return AWS_OP_ERR; |
| 1483 |
} |
| 1484 |
|
| 1485 |
storage_view->server_reference = &disconnect_storage->server_reference; |
| 1486 |
} |
| 1487 |
|
| 1488 |
if (aws_mqtt5_user_property_set_init_with_storage( |
| 1489 |
&disconnect_storage->user_properties, |
| 1490 |
allocator, |
| 1491 |
&disconnect_storage->storage, |
| 1492 |
disconnect_options->user_property_count, |
| 1493 |
disconnect_options->user_properties)) { |
| 1494 |
return AWS_OP_ERR; |
| 1495 |
} |
| 1496 |
|
| 1497 |
storage_view->user_property_count = aws_mqtt5_user_property_set_size(&disconnect_storage->user_properties); |
| 1498 |
storage_view->user_properties = disconnect_storage->user_properties.properties.data; |
| 1499 |
|
| 1500 |
return AWS_OP_SUCCESS; |
| 1501 |
} |
| 1502 |
|
| 1503 |
int aws_mqtt5_packet_disconnect_storage_init_from_external_storage( |
| 1504 |
struct aws_mqtt5_packet_disconnect_storage *disconnect_storage, |
| 1505 |
struct aws_allocator *allocator) { |
| 1506 |
AWS_ZERO_STRUCT(*disconnect_storage); |
| 1507 |
|
| 1508 |
if (aws_mqtt5_user_property_set_init(&disconnect_storage->user_properties, allocator)) { |
| 1509 |
return AWS_OP_ERR; |
| 1510 |
} |
| 1511 |
|
| 1512 |
return AWS_OP_SUCCESS; |
| 1513 |
} |
| 1514 |
|
| 1515 |
static void s_destroy_operation_disconnect(void *object) { |
| 1516 |
if (object == NULL) { |
| 1517 |
return; |
| 1518 |
} |
| 1519 |
|
| 1520 |
struct aws_mqtt5_operation_disconnect *disconnect_op = object; |
| 1521 |
|
| 1522 |
aws_mqtt5_packet_disconnect_storage_clean_up(&disconnect_op->options_storage); |
| 1523 |
|
| 1524 |
aws_mem_release(disconnect_op->allocator, disconnect_op); |
| 1525 |
} |
| 1526 |
|
| 1527 |
static void s_aws_mqtt5_disconnect_operation_completion( |
| 1528 |
struct aws_mqtt5_operation *operation, |
| 1529 |
int error_code, |
| 1530 |
enum aws_mqtt5_packet_type packet_type, |
| 1531 |
const void *completion_view) { |
| 1532 |
|
| 1533 |
(void)completion_view; |
| 1534 |
(void)packet_type; |
| 1535 |
|
| 1536 |
struct aws_mqtt5_operation_disconnect *disconnect_op = operation->impl; |
| 1537 |
|
| 1538 |
if (disconnect_op->internal_completion_options.completion_callback != NULL) { |
| 1539 |
(*disconnect_op->internal_completion_options.completion_callback)( |
| 1540 |
error_code, disconnect_op->internal_completion_options.completion_user_data); |
| 1541 |
} |
| 1542 |
|
| 1543 |
if (disconnect_op->external_completion_options.completion_callback != NULL) { |
| 1544 |
(*disconnect_op->external_completion_options.completion_callback)( |
| 1545 |
error_code, disconnect_op->external_completion_options.completion_user_data); |
| 1546 |
} |
| 1547 |
} |
| 1548 |
|
| 1549 |
static struct aws_mqtt5_operation_vtable s_disconnect_operation_vtable = { |
| 1550 |
.aws_mqtt5_operation_completion_fn = s_aws_mqtt5_disconnect_operation_completion, |
| 1551 |
.aws_mqtt5_operation_set_packet_id_fn = NULL, |
| 1552 |
.aws_mqtt5_operation_get_packet_id_address_fn = NULL, |
| 1553 |
.aws_mqtt5_operation_validate_vs_connection_settings_fn = |
| 1554 |
s_aws_mqtt5_packet_disconnect_view_validate_vs_connection_settings, |
| 1555 |
.aws_mqtt5_operation_get_ack_timeout_override_fn = NULL, |
| 1556 |
}; |
| 1557 |
|
| 1558 |
struct aws_mqtt5_operation_disconnect *aws_mqtt5_operation_disconnect_new( |
| 1559 |
struct aws_allocator *allocator, |
| 1560 |
const struct aws_mqtt5_packet_disconnect_view *disconnect_options, |
| 1561 |
const struct aws_mqtt5_disconnect_completion_options *external_completion_options, |
| 1562 |
const struct aws_mqtt5_disconnect_completion_options *internal_completion_options) { |
| 1563 |
AWS_PRECONDITION(allocator != NULL); |
| 1564 |
|
| 1565 |
if (aws_mqtt5_packet_disconnect_view_validate(disconnect_options)) { |
| 1566 |
return NULL; |
| 1567 |
} |
| 1568 |
|
| 1569 |
struct aws_mqtt5_operation_disconnect *disconnect_op = |
| 1570 |
aws_mem_calloc(allocator, 1, sizeof(struct aws_mqtt5_operation_disconnect)); |
| 1571 |
if (disconnect_op == NULL) { |
| 1572 |
return NULL; |
| 1573 |
} |
| 1574 |
|
| 1575 |
disconnect_op->allocator = allocator; |
| 1576 |
disconnect_op->base.vtable = &s_disconnect_operation_vtable; |
| 1577 |
disconnect_op->base.packet_type = AWS_MQTT5_PT_DISCONNECT; |
| 1578 |
aws_ref_count_init(&disconnect_op->base.ref_count, disconnect_op, s_destroy_operation_disconnect); |
| 1579 |
aws_priority_queue_node_init(&disconnect_op->base.priority_queue_node); |
| 1580 |
disconnect_op->base.impl = disconnect_op; |
| 1581 |
|
| 1582 |
if (aws_mqtt5_packet_disconnect_storage_init(&disconnect_op->options_storage, allocator, disconnect_options)) { |
| 1583 |
goto error; |
| 1584 |
} |
| 1585 |
|
| 1586 |
disconnect_op->base.packet_view = &disconnect_op->options_storage.storage_view; |
| 1587 |
if (external_completion_options != NULL) { |
| 1588 |
disconnect_op->external_completion_options = *external_completion_options; |
| 1589 |
} |
| 1590 |
|
| 1591 |
if (internal_completion_options != NULL) { |
| 1592 |
disconnect_op->internal_completion_options = *internal_completion_options; |
| 1593 |
} |
| 1594 |
|
| 1595 |
return disconnect_op; |
| 1596 |
|
| 1597 |
error: |
| 1598 |
|
| 1599 |
aws_mqtt5_operation_release(&disconnect_op->base); |
| 1600 |
|
| 1601 |
return NULL; |
| 1602 |
} |
| 1603 |
|
| 1604 |
|
| 1605 |
|
| 1606 |
|
| 1607 |
|
| 1608 |
int aws_mqtt5_packet_publish_view_validate(const struct aws_mqtt5_packet_publish_view *publish_view) { |
| 1609 |
|
| 1610 |
if (publish_view == NULL) { |
| 1611 |
AWS_LOGF_ERROR(AWS_LS_MQTT5_GENERAL, "null PUBLISH packet options"); |
| 1612 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 1613 |
} |
| 1614 |
|
| 1615 |
if (publish_view->qos < AWS_MQTT5_QOS_AT_MOST_ONCE || publish_view->qos > AWS_MQTT5_QOS_EXACTLY_ONCE) { |
| 1616 |
AWS_LOGF_ERROR( |
| 1617 |
AWS_LS_MQTT5_GENERAL, |
| 1618 |
"id=%p: aws_mqtt5_packet_publish_view - unsupported QoS value in PUBLISH packet options: %d", |
| 1619 |
(void *)publish_view, |
| 1620 |
(int)publish_view->qos); |
| 1621 |
return aws_raise_error(AWS_ERROR_MQTT5_PUBLISH_OPTIONS_VALIDATION); |
| 1622 |
} |
| 1623 |
|
| 1624 |
if (publish_view->qos == AWS_MQTT5_QOS_AT_MOST_ONCE) { |
| 1625 |
if (publish_view->duplicate) { |
| 1626 |
AWS_LOGF_ERROR( |
| 1627 |
AWS_LS_MQTT5_GENERAL, |
| 1628 |
"id=%p: aws_mqtt5_packet_publish_view - duplicate flag must be set to 0 for QoS 0 messages", |
| 1629 |
(void *)publish_view); |
| 1630 |
return aws_raise_error(AWS_ERROR_MQTT5_PUBLISH_OPTIONS_VALIDATION); |
| 1631 |
} |
| 1632 |
if (publish_view->packet_id != 0) { |
| 1633 |
AWS_LOGF_ERROR( |
| 1634 |
AWS_LS_MQTT5_GENERAL, |
| 1635 |
"id=%p: aws_mqtt5_packet_publish_view - Packet ID must not be set for QoS 0 messages", |
| 1636 |
(void *)publish_view); |
| 1637 |
return aws_raise_error(AWS_ERROR_MQTT5_PUBLISH_OPTIONS_VALIDATION); |
| 1638 |
} |
| 1639 |
} |
| 1640 |
|
| 1641 |
|
| 1642 |
if (publish_view->topic.len == 0) { |
| 1643 |
AWS_LOGF_ERROR( |
| 1644 |
AWS_LS_MQTT5_GENERAL, "id=%p: aws_mqtt5_packet_publish_view - missing topic", (void *)publish_view); |
| 1645 |
return aws_raise_error(AWS_ERROR_MQTT5_PUBLISH_OPTIONS_VALIDATION); |
| 1646 |
} else if (aws_mqtt_validate_utf8_text(publish_view->topic)) { |
| 1647 |
AWS_LOGF_ERROR( |
| 1648 |
AWS_LS_MQTT5_GENERAL, "id=%p: aws_mqtt5_packet_publish_view - topic not valid UTF-8", (void *)publish_view); |
| 1649 |
return aws_raise_error(AWS_ERROR_MQTT5_PUBLISH_OPTIONS_VALIDATION); |
| 1650 |
} else if (!aws_mqtt_is_valid_topic(&publish_view->topic)) { |
| 1651 |
AWS_LOGF_ERROR( |
| 1652 |
AWS_LS_MQTT5_GENERAL, |
| 1653 |
"id=%p: aws_mqtt5_packet_publish_view - invalid topic: \"" PRInSTR "\"", |
| 1654 |
(void *)publish_view, |
| 1655 |
AWS_BYTE_CURSOR_PRI(publish_view->topic)); |
| 1656 |
return aws_raise_error(AWS_ERROR_MQTT5_PUBLISH_OPTIONS_VALIDATION); |
| 1657 |
} |
| 1658 |
|
| 1659 |
if (publish_view->topic_alias != NULL) { |
| 1660 |
if (*publish_view->topic_alias == 0) { |
| 1661 |
AWS_LOGF_ERROR( |
| 1662 |
AWS_LS_MQTT5_GENERAL, |
| 1663 |
"id=%p: aws_mqtt5_packet_publish_view - topic alias may not be zero", |
| 1664 |
(void *)publish_view); |
| 1665 |
return aws_raise_error(AWS_ERROR_MQTT5_PUBLISH_OPTIONS_VALIDATION); |
| 1666 |
} |
| 1667 |
} |
| 1668 |
|
| 1669 |
if (publish_view->payload_format != NULL) { |
| 1670 |
if (*publish_view->payload_format < AWS_MQTT5_PFI_BYTES || *publish_view->payload_format > AWS_MQTT5_PFI_UTF8) { |
| 1671 |
AWS_LOGF_ERROR( |
| 1672 |
AWS_LS_MQTT5_GENERAL, |
| 1673 |
"id=%p: aws_mqtt5_packet_publish_view - invalid payload format value: %d", |
| 1674 |
(void *)publish_view, |
| 1675 |
(int)*publish_view->payload_format); |
| 1676 |
return aws_raise_error(AWS_ERROR_MQTT5_PUBLISH_OPTIONS_VALIDATION); |
| 1677 |
} |
| 1678 |
|
| 1679 |
|
| 1680 |
if (*publish_view->payload_format == AWS_MQTT5_PFI_UTF8) { |
| 1681 |
if (aws_mqtt_validate_utf8_text(publish_view->payload)) { |
| 1682 |
AWS_LOGF_ERROR( |
| 1683 |
AWS_LS_MQTT5_GENERAL, |
| 1684 |
"id=%p: aws_mqtt5_packet_publish_view - payload value is not valid UTF-8 while payload format " |
| 1685 |
"set to UTF-8", |
| 1686 |
(void *)publish_view); |
| 1687 |
return aws_raise_error(AWS_ERROR_MQTT5_PUBLISH_OPTIONS_VALIDATION); |
| 1688 |
} |
| 1689 |
} |
| 1690 |
} |
| 1691 |
|
| 1692 |
if (publish_view->response_topic != NULL) { |
| 1693 |
if (publish_view->response_topic->len >= UINT16_MAX) { |
| 1694 |
AWS_LOGF_ERROR( |
| 1695 |
AWS_LS_MQTT5_GENERAL, |
| 1696 |
"id=%p: aws_mqtt5_packet_publish_view - response topic too long", |
| 1697 |
(void *)publish_view); |
| 1698 |
return aws_raise_error(AWS_ERROR_MQTT5_PUBLISH_OPTIONS_VALIDATION); |
| 1699 |
} |
| 1700 |
|
| 1701 |
if (aws_mqtt_validate_utf8_text(*publish_view->response_topic)) { |
| 1702 |
AWS_LOGF_ERROR( |
| 1703 |
AWS_LS_MQTT5_GENERAL, |
| 1704 |
"id=%p: aws_mqtt5_packet_publish_view - response topic not valid UTF-8", |
| 1705 |
(void *)publish_view); |
| 1706 |
return aws_raise_error(AWS_ERROR_MQTT5_PUBLISH_OPTIONS_VALIDATION); |
| 1707 |
} |
| 1708 |
|
| 1709 |
if (!aws_mqtt_is_valid_topic(publish_view->response_topic)) { |
| 1710 |
AWS_LOGF_ERROR( |
| 1711 |
AWS_LS_MQTT5_GENERAL, |
| 1712 |
"id=%p: aws_mqtt5_packet_publish_view - response topic must be a valid mqtt topic", |
| 1713 |
(void *)publish_view); |
| 1714 |
return aws_raise_error(AWS_ERROR_MQTT5_PUBLISH_OPTIONS_VALIDATION); |
| 1715 |
} |
| 1716 |
} |
| 1717 |
|
| 1718 |
if (publish_view->correlation_data != NULL) { |
| 1719 |
if (publish_view->correlation_data->len >= UINT16_MAX) { |
| 1720 |
AWS_LOGF_ERROR( |
| 1721 |
AWS_LS_MQTT5_GENERAL, |
| 1722 |
"id=%p: aws_mqtt5_packet_publish_view - correlation data too long", |
| 1723 |
(void *)publish_view); |
| 1724 |
return aws_raise_error(AWS_ERROR_MQTT5_PUBLISH_OPTIONS_VALIDATION); |
| 1725 |
} |
| 1726 |
} |
| 1727 |
|
| 1728 |
|
| 1729 |
|
| 1730 |
|
| 1731 |
|
| 1732 |
if (publish_view->subscription_identifier_count != 0) { |
| 1733 |
AWS_LOGF_ERROR( |
| 1734 |
AWS_LS_MQTT5_GENERAL, "Client-initiated PUBLISH packets may not contain subscription identifiers"); |
| 1735 |
return aws_raise_error(AWS_ERROR_MQTT5_PUBLISH_OPTIONS_VALIDATION); |
| 1736 |
} |
| 1737 |
|
| 1738 |
if (publish_view->content_type != NULL) { |
| 1739 |
if (publish_view->content_type->len >= UINT16_MAX) { |
| 1740 |
AWS_LOGF_ERROR( |
| 1741 |
AWS_LS_MQTT5_GENERAL, |
| 1742 |
"id=%p: aws_mqtt5_packet_publish_view - content type too long", |
| 1743 |
(void *)publish_view); |
| 1744 |
return aws_raise_error(AWS_ERROR_MQTT5_PUBLISH_OPTIONS_VALIDATION); |
| 1745 |
} |
| 1746 |
|
| 1747 |
if (aws_mqtt_validate_utf8_text(*publish_view->content_type)) { |
| 1748 |
AWS_LOGF_ERROR( |
| 1749 |
AWS_LS_MQTT5_GENERAL, |
| 1750 |
"id=%p: aws_mqtt5_packet_publish_view - content type not valid UTF-8", |
| 1751 |
(void *)publish_view); |
| 1752 |
return aws_raise_error(AWS_ERROR_MQTT5_PUBLISH_OPTIONS_VALIDATION); |
| 1753 |
} |
| 1754 |
} |
| 1755 |
|
| 1756 |
if (s_aws_mqtt5_user_property_set_validate( |
| 1757 |
publish_view->user_properties, |
| 1758 |
publish_view->user_property_count, |
| 1759 |
"aws_mqtt5_packet_publish_view", |
| 1760 |
(void *)publish_view)) { |
| 1761 |
return AWS_OP_ERR; |
| 1762 |
} |
| 1763 |
|
| 1764 |
return AWS_OP_SUCCESS; |
| 1765 |
} |
| 1766 |
|
| 1767 |
static int s_aws_mqtt5_packet_publish_view_validate_vs_connection_settings( |
| 1768 |
const void *packet_view, |
| 1769 |
const struct aws_mqtt5_client *client) { |
| 1770 |
const struct aws_mqtt5_packet_publish_view *publish_view = packet_view; |
| 1771 |
|
| 1772 |
|
| 1773 |
if (aws_mqtt5_client_are_negotiated_settings_valid(client)) { |
| 1774 |
const struct aws_mqtt5_negotiated_settings *settings = &client->negotiated_settings; |
| 1775 |
|
| 1776 |
if (publish_view->qos > settings->maximum_qos) { |
| 1777 |
AWS_LOGF_ERROR( |
| 1778 |
AWS_LS_MQTT5_GENERAL, |
| 1779 |
"id=%p: aws_mqtt5_packet_publish_view - QoS value %d exceeds negotiated maximum qos %d", |
| 1780 |
(void *)publish_view, |
| 1781 |
(int)publish_view->qos, |
| 1782 |
(int)settings->maximum_qos); |
| 1783 |
return aws_raise_error(AWS_ERROR_MQTT5_PUBLISH_OPTIONS_VALIDATION); |
| 1784 |
} |
| 1785 |
|
| 1786 |
if (publish_view->retain && settings->retain_available == false) { |
| 1787 |
AWS_LOGF_ERROR( |
| 1788 |
AWS_LS_MQTT5_GENERAL, |
| 1789 |
"id=%p: aws_mqtt5_packet_publish_view - server does not support Retain", |
| 1790 |
(void *)publish_view); |
| 1791 |
return aws_raise_error(AWS_ERROR_MQTT5_PUBLISH_OPTIONS_VALIDATION); |
| 1792 |
} |
| 1793 |
} |
| 1794 |
|
| 1795 |
return AWS_OP_SUCCESS; |
| 1796 |
} |
| 1797 |
|
| 1798 |
void aws_mqtt5_packet_publish_view_log( |
| 1799 |
const struct aws_mqtt5_packet_publish_view *publish_view, |
| 1800 |
enum aws_log_level level) { |
| 1801 |
struct aws_logger *log_handle = aws_logger_get_conditional(AWS_LS_MQTT5_GENERAL, level); |
| 1802 |
if (log_handle == NULL) { |
| 1803 |
return; |
| 1804 |
} |
| 1805 |
|
| 1806 |
AWS_LOGUF( |
| 1807 |
log_handle, |
| 1808 |
level, |
| 1809 |
AWS_LS_MQTT5_GENERAL, |
| 1810 |
"id=%p: aws_mqtt5_packet_publish_view packet id set to %d", |
| 1811 |
(void *)publish_view, |
| 1812 |
(int)publish_view->packet_id); |
| 1813 |
|
| 1814 |
AWS_LOGUF( |
| 1815 |
log_handle, |
| 1816 |
level, |
| 1817 |
AWS_LS_MQTT5_GENERAL, |
| 1818 |
"id=%p: aws_mqtt5_packet_publish_view payload set containing %zu bytes", |
| 1819 |
(void *)publish_view, |
| 1820 |
publish_view->payload.len); |
| 1821 |
|
| 1822 |
AWS_LOGUF( |
| 1823 |
log_handle, |
| 1824 |
level, |
| 1825 |
AWS_LS_MQTT5_GENERAL, |
| 1826 |
"id=%p: aws_mqtt5_packet_publish_view qos set to %d", |
| 1827 |
(void *)publish_view, |
| 1828 |
(int)publish_view->qos); |
| 1829 |
|
| 1830 |
AWS_LOGUF( |
| 1831 |
log_handle, |
| 1832 |
level, |
| 1833 |
AWS_LS_MQTT5_GENERAL, |
| 1834 |
"id=%p: aws_mqtt5_packet_publish_view retain set to %d", |
| 1835 |
(void *)publish_view, |
| 1836 |
(int)publish_view->retain); |
| 1837 |
|
| 1838 |
AWS_LOGUF( |
| 1839 |
log_handle, |
| 1840 |
level, |
| 1841 |
AWS_LS_MQTT5_GENERAL, |
| 1842 |
"id=%p: aws_mqtt5_packet_publish_view topic set to \"" PRInSTR "\"", |
| 1843 |
(void *)publish_view, |
| 1844 |
AWS_BYTE_CURSOR_PRI(publish_view->topic)); |
| 1845 |
|
| 1846 |
if (publish_view->payload_format != NULL) { |
| 1847 |
AWS_LOGUF( |
| 1848 |
log_handle, |
| 1849 |
level, |
| 1850 |
AWS_LS_MQTT5_GENERAL, |
| 1851 |
"id=%p: aws_mqtt5_packet_publish_view payload format indicator set to %d (%s)", |
| 1852 |
(void *)publish_view, |
| 1853 |
(int)*publish_view->payload_format, |
| 1854 |
aws_mqtt5_payload_format_indicator_to_c_string(*publish_view->payload_format)); |
| 1855 |
} |
| 1856 |
|
| 1857 |
if (publish_view->message_expiry_interval_seconds != NULL) { |
| 1858 |
AWS_LOGUF( |
| 1859 |
log_handle, |
| 1860 |
level, |
| 1861 |
AWS_LS_MQTT5_GENERAL, |
| 1862 |
"id=%p: aws_mqtt5_packet_publish_view message expiry interval set to %" PRIu32, |
| 1863 |
(void *)publish_view, |
| 1864 |
*publish_view->message_expiry_interval_seconds); |
| 1865 |
} |
| 1866 |
|
| 1867 |
if (publish_view->topic_alias != NULL) { |
| 1868 |
AWS_LOGUF( |
| 1869 |
log_handle, |
| 1870 |
level, |
| 1871 |
AWS_LS_MQTT5_GENERAL, |
| 1872 |
"id=%p: aws_mqtt5_packet_publish_view topic alias set to %" PRIu16, |
| 1873 |
(void *)publish_view, |
| 1874 |
*publish_view->topic_alias); |
| 1875 |
} |
| 1876 |
|
| 1877 |
if (publish_view->response_topic != NULL) { |
| 1878 |
AWS_LOGUF( |
| 1879 |
log_handle, |
| 1880 |
level, |
| 1881 |
AWS_LS_MQTT5_GENERAL, |
| 1882 |
"id=%p: aws_mqtt5_packet_publish_view response topic set to \"" PRInSTR "\"", |
| 1883 |
(void *)publish_view, |
| 1884 |
AWS_BYTE_CURSOR_PRI(*publish_view->response_topic)); |
| 1885 |
} |
| 1886 |
|
| 1887 |
if (publish_view->correlation_data != NULL) { |
| 1888 |
AWS_LOGUF( |
| 1889 |
log_handle, |
| 1890 |
level, |
| 1891 |
AWS_LS_MQTT5_GENERAL, |
| 1892 |
"id=%p: aws_mqtt5_packet_publish_view - set correlation data", |
| 1893 |
(void *)publish_view); |
| 1894 |
} |
| 1895 |
|
| 1896 |
if (publish_view->content_type != NULL) { |
| 1897 |
AWS_LOGUF( |
| 1898 |
log_handle, |
| 1899 |
level, |
| 1900 |
AWS_LS_MQTT5_GENERAL, |
| 1901 |
"id=%p: aws_mqtt5_packet_publish_view content type set to \"" PRInSTR "\"", |
| 1902 |
(void *)publish_view, |
| 1903 |
AWS_BYTE_CURSOR_PRI(*publish_view->content_type)); |
| 1904 |
} |
| 1905 |
|
| 1906 |
for (size_t i = 0; i < publish_view->subscription_identifier_count; ++i) { |
| 1907 |
AWS_LOGUF( |
| 1908 |
log_handle, |
| 1909 |
level, |
| 1910 |
AWS_LS_MQTT5_GENERAL, |
| 1911 |
"id=%p: aws_mqtt5_packet_publish_view subscription identifier %d: %" PRIu32, |
| 1912 |
(void *)publish_view, |
| 1913 |
(int)i, |
| 1914 |
publish_view->subscription_identifiers[i]); |
| 1915 |
} |
| 1916 |
|
| 1917 |
s_aws_mqtt5_user_property_set_log( |
| 1918 |
log_handle, |
| 1919 |
publish_view->user_properties, |
| 1920 |
publish_view->user_property_count, |
| 1921 |
(void *)publish_view, |
| 1922 |
level, |
| 1923 |
"aws_mqtt5_packet_publish_view"); |
| 1924 |
} |
| 1925 |
|
| 1926 |
static size_t s_aws_mqtt5_packet_publish_compute_storage_size( |
| 1927 |
const struct aws_mqtt5_packet_publish_view *publish_view) { |
| 1928 |
size_t storage_size = s_aws_mqtt5_user_property_set_compute_storage_size( |
| 1929 |
publish_view->user_properties, publish_view->user_property_count); |
| 1930 |
|
| 1931 |
storage_size += publish_view->topic.len; |
| 1932 |
storage_size += publish_view->payload.len; |
| 1933 |
|
| 1934 |
if (publish_view->response_topic != NULL) { |
| 1935 |
storage_size += publish_view->response_topic->len; |
| 1936 |
} |
| 1937 |
|
| 1938 |
if (publish_view->correlation_data != NULL) { |
| 1939 |
storage_size += publish_view->correlation_data->len; |
| 1940 |
} |
| 1941 |
|
| 1942 |
if (publish_view->content_type != NULL) { |
| 1943 |
storage_size += publish_view->content_type->len; |
| 1944 |
} |
| 1945 |
|
| 1946 |
return storage_size; |
| 1947 |
} |
| 1948 |
|
| 1949 |
int aws_mqtt5_packet_publish_storage_init( |
| 1950 |
struct aws_mqtt5_packet_publish_storage *publish_storage, |
| 1951 |
struct aws_allocator *allocator, |
| 1952 |
const struct aws_mqtt5_packet_publish_view *publish_options) { |
| 1953 |
|
| 1954 |
AWS_ZERO_STRUCT(*publish_storage); |
| 1955 |
size_t storage_capacity = s_aws_mqtt5_packet_publish_compute_storage_size(publish_options); |
| 1956 |
if (aws_byte_buf_init(&publish_storage->storage, allocator, storage_capacity)) { |
| 1957 |
return AWS_OP_ERR; |
| 1958 |
} |
| 1959 |
|
| 1960 |
if (aws_array_list_init_dynamic(&publish_storage->subscription_identifiers, allocator, 0, sizeof(uint32_t))) { |
| 1961 |
return AWS_OP_ERR; |
| 1962 |
} |
| 1963 |
|
| 1964 |
struct aws_mqtt5_packet_publish_view *storage_view = &publish_storage->storage_view; |
| 1965 |
|
| 1966 |
storage_view->packet_id = publish_options->packet_id; |
| 1967 |
|
| 1968 |
storage_view->payload = publish_options->payload; |
| 1969 |
if (aws_byte_buf_append_and_update(&publish_storage->storage, &storage_view->payload)) { |
| 1970 |
return AWS_OP_ERR; |
| 1971 |
} |
| 1972 |
|
| 1973 |
storage_view->qos = publish_options->qos; |
| 1974 |
storage_view->retain = publish_options->retain; |
| 1975 |
storage_view->duplicate = publish_options->duplicate; |
| 1976 |
|
| 1977 |
storage_view->topic = publish_options->topic; |
| 1978 |
if (aws_byte_buf_append_and_update(&publish_storage->storage, &storage_view->topic)) { |
| 1979 |
return AWS_OP_ERR; |
| 1980 |
} |
| 1981 |
|
| 1982 |
if (publish_options->payload_format != NULL) { |
| 1983 |
publish_storage->payload_format = *publish_options->payload_format; |
| 1984 |
storage_view->payload_format = &publish_storage->payload_format; |
| 1985 |
} |
| 1986 |
|
| 1987 |
if (publish_options->message_expiry_interval_seconds != NULL) { |
| 1988 |
publish_storage->message_expiry_interval_seconds = *publish_options->message_expiry_interval_seconds; |
| 1989 |
storage_view->message_expiry_interval_seconds = &publish_storage->message_expiry_interval_seconds; |
| 1990 |
} |
| 1991 |
|
| 1992 |
if (publish_options->topic_alias != NULL) { |
| 1993 |
publish_storage->topic_alias = *publish_options->topic_alias; |
| 1994 |
storage_view->topic_alias = &publish_storage->topic_alias; |
| 1995 |
} |
| 1996 |
|
| 1997 |
if (publish_options->response_topic != NULL) { |
| 1998 |
publish_storage->response_topic = *publish_options->response_topic; |
| 1999 |
if (aws_byte_buf_append_and_update(&publish_storage->storage, &publish_storage->response_topic)) { |
| 2000 |
return AWS_OP_ERR; |
| 2001 |
} |
| 2002 |
|
| 2003 |
storage_view->response_topic = &publish_storage->response_topic; |
| 2004 |
} |
| 2005 |
|
| 2006 |
if (publish_options->correlation_data != NULL) { |
| 2007 |
publish_storage->correlation_data = *publish_options->correlation_data; |
| 2008 |
if (aws_byte_buf_append_and_update(&publish_storage->storage, &publish_storage->correlation_data)) { |
| 2009 |
return AWS_OP_ERR; |
| 2010 |
} |
| 2011 |
|
| 2012 |
storage_view->correlation_data = &publish_storage->correlation_data; |
| 2013 |
} |
| 2014 |
|
| 2015 |
for (size_t i = 0; i < publish_options->subscription_identifier_count; ++i) { |
| 2016 |
aws_array_list_push_back( |
| 2017 |
&publish_storage->subscription_identifiers, &publish_options->subscription_identifiers[i]); |
| 2018 |
} |
| 2019 |
|
| 2020 |
storage_view->subscription_identifier_count = aws_array_list_length(&publish_storage->subscription_identifiers); |
| 2021 |
storage_view->subscription_identifiers = publish_storage->subscription_identifiers.data; |
| 2022 |
|
| 2023 |
if (publish_options->content_type != NULL) { |
| 2024 |
publish_storage->content_type = *publish_options->content_type; |
| 2025 |
if (aws_byte_buf_append_and_update(&publish_storage->storage, &publish_storage->content_type)) { |
| 2026 |
return AWS_OP_ERR; |
| 2027 |
} |
| 2028 |
|
| 2029 |
storage_view->content_type = &publish_storage->content_type; |
| 2030 |
} |
| 2031 |
|
| 2032 |
if (aws_mqtt5_user_property_set_init_with_storage( |
| 2033 |
&publish_storage->user_properties, |
| 2034 |
allocator, |
| 2035 |
&publish_storage->storage, |
| 2036 |
publish_options->user_property_count, |
| 2037 |
publish_options->user_properties)) { |
| 2038 |
return AWS_OP_ERR; |
| 2039 |
} |
| 2040 |
storage_view->user_property_count = aws_mqtt5_user_property_set_size(&publish_storage->user_properties); |
| 2041 |
storage_view->user_properties = publish_storage->user_properties.properties.data; |
| 2042 |
|
| 2043 |
return AWS_OP_SUCCESS; |
| 2044 |
} |
| 2045 |
|
| 2046 |
int aws_mqtt5_packet_publish_storage_init_from_external_storage( |
| 2047 |
struct aws_mqtt5_packet_publish_storage *publish_storage, |
| 2048 |
struct aws_allocator *allocator) { |
| 2049 |
AWS_ZERO_STRUCT(*publish_storage); |
| 2050 |
|
| 2051 |
if (aws_mqtt5_user_property_set_init(&publish_storage->user_properties, allocator)) { |
| 2052 |
return AWS_OP_ERR; |
| 2053 |
} |
| 2054 |
|
| 2055 |
if (aws_array_list_init_dynamic(&publish_storage->subscription_identifiers, allocator, 0, sizeof(uint32_t))) { |
| 2056 |
return AWS_OP_ERR; |
| 2057 |
} |
| 2058 |
|
| 2059 |
return AWS_OP_SUCCESS; |
| 2060 |
} |
| 2061 |
|
| 2062 |
void aws_mqtt5_packet_publish_storage_clean_up(struct aws_mqtt5_packet_publish_storage *publish_storage) { |
| 2063 |
aws_mqtt5_user_property_set_clean_up(&publish_storage->user_properties); |
| 2064 |
aws_array_list_clean_up(&publish_storage->subscription_identifiers); |
| 2065 |
aws_byte_buf_clean_up(&publish_storage->storage); |
| 2066 |
} |
| 2067 |
|
| 2068 |
static void s_aws_mqtt5_operation_publish_complete( |
| 2069 |
struct aws_mqtt5_operation *operation, |
| 2070 |
int error_code, |
| 2071 |
enum aws_mqtt5_packet_type packet_type, |
| 2072 |
const void *completion_view) { |
| 2073 |
struct aws_mqtt5_operation_publish *publish_op = operation->impl; |
| 2074 |
|
| 2075 |
if (publish_op->completion_options.completion_callback != NULL) { |
| 2076 |
(*publish_op->completion_options.completion_callback)( |
| 2077 |
packet_type, completion_view, error_code, publish_op->completion_options.completion_user_data); |
| 2078 |
} |
| 2079 |
} |
| 2080 |
|
| 2081 |
static void s_aws_mqtt5_operation_publish_set_packet_id( |
| 2082 |
struct aws_mqtt5_operation *operation, |
| 2083 |
aws_mqtt5_packet_id_t packet_id) { |
| 2084 |
struct aws_mqtt5_operation_publish *publish_op = operation->impl; |
| 2085 |
publish_op->options_storage.storage_view.packet_id = packet_id; |
| 2086 |
} |
| 2087 |
|
| 2088 |
static aws_mqtt5_packet_id_t *s_aws_mqtt5_operation_publish_get_packet_id_address( |
| 2089 |
const struct aws_mqtt5_operation *operation) { |
| 2090 |
struct aws_mqtt5_operation_publish *publish_op = operation->impl; |
| 2091 |
return &publish_op->options_storage.storage_view.packet_id; |
| 2092 |
} |
| 2093 |
|
| 2094 |
static uint32_t s_aws_mqtt5_operation_publish_get_ack_timeout_override(const struct aws_mqtt5_operation *operation) { |
| 2095 |
struct aws_mqtt5_operation_publish *publish_op = operation->impl; |
| 2096 |
return publish_op->completion_options.ack_timeout_seconds_override; |
| 2097 |
} |
| 2098 |
|
| 2099 |
static struct aws_mqtt5_operation_vtable s_publish_operation_vtable = { |
| 2100 |
.aws_mqtt5_operation_completion_fn = s_aws_mqtt5_operation_publish_complete, |
| 2101 |
.aws_mqtt5_operation_set_packet_id_fn = s_aws_mqtt5_operation_publish_set_packet_id, |
| 2102 |
.aws_mqtt5_operation_get_packet_id_address_fn = s_aws_mqtt5_operation_publish_get_packet_id_address, |
| 2103 |
.aws_mqtt5_operation_validate_vs_connection_settings_fn = |
| 2104 |
s_aws_mqtt5_packet_publish_view_validate_vs_connection_settings, |
| 2105 |
.aws_mqtt5_operation_get_ack_timeout_override_fn = s_aws_mqtt5_operation_publish_get_ack_timeout_override}; |
| 2106 |
|
| 2107 |
static void s_destroy_operation_publish(void *object) { |
| 2108 |
if (object == NULL) { |
| 2109 |
return; |
| 2110 |
} |
| 2111 |
|
| 2112 |
struct aws_mqtt5_operation_publish *publish_op = object; |
| 2113 |
|
| 2114 |
aws_mqtt5_packet_publish_storage_clean_up(&publish_op->options_storage); |
| 2115 |
|
| 2116 |
aws_mem_release(publish_op->allocator, publish_op); |
| 2117 |
} |
| 2118 |
|
| 2119 |
struct aws_mqtt5_operation_publish *aws_mqtt5_operation_publish_new( |
| 2120 |
struct aws_allocator *allocator, |
| 2121 |
const struct aws_mqtt5_client *client, |
| 2122 |
const struct aws_mqtt5_packet_publish_view *publish_options, |
| 2123 |
const struct aws_mqtt5_publish_completion_options *completion_options) { |
| 2124 |
(void)client; |
| 2125 |
AWS_PRECONDITION(allocator != NULL); |
| 2126 |
AWS_PRECONDITION(publish_options != NULL); |
| 2127 |
|
| 2128 |
if (aws_mqtt5_packet_publish_view_validate(publish_options)) { |
| 2129 |
return NULL; |
| 2130 |
} |
| 2131 |
|
| 2132 |
if (publish_options->packet_id != 0) { |
| 2133 |
AWS_LOGF_DEBUG( |
| 2134 |
AWS_LS_MQTT5_GENERAL, |
| 2135 |
"id=%p: aws_mqtt5_packet_publish_view packet id must be zero", |
| 2136 |
(void *)publish_options); |
| 2137 |
aws_raise_error(AWS_ERROR_MQTT5_PUBLISH_OPTIONS_VALIDATION); |
| 2138 |
return NULL; |
| 2139 |
} |
| 2140 |
|
| 2141 |
struct aws_mqtt5_operation_publish *publish_op = |
| 2142 |
aws_mem_calloc(allocator, 1, sizeof(struct aws_mqtt5_operation_publish)); |
| 2143 |
if (publish_op == NULL) { |
| 2144 |
return NULL; |
| 2145 |
} |
| 2146 |
|
| 2147 |
publish_op->allocator = allocator; |
| 2148 |
publish_op->base.vtable = &s_publish_operation_vtable; |
| 2149 |
publish_op->base.packet_type = AWS_MQTT5_PT_PUBLISH; |
| 2150 |
aws_ref_count_init(&publish_op->base.ref_count, publish_op, s_destroy_operation_publish); |
| 2151 |
aws_priority_queue_node_init(&publish_op->base.priority_queue_node); |
| 2152 |
publish_op->base.impl = publish_op; |
| 2153 |
|
| 2154 |
if (aws_mqtt5_packet_publish_storage_init(&publish_op->options_storage, allocator, publish_options)) { |
| 2155 |
goto error; |
| 2156 |
} |
| 2157 |
|
| 2158 |
publish_op->base.packet_view = &publish_op->options_storage.storage_view; |
| 2159 |
|
| 2160 |
if (completion_options != NULL) { |
| 2161 |
publish_op->completion_options = *completion_options; |
| 2162 |
} |
| 2163 |
|
| 2164 |
return publish_op; |
| 2165 |
|
| 2166 |
error: |
| 2167 |
|
| 2168 |
aws_mqtt5_operation_release(&publish_op->base); |
| 2169 |
|
| 2170 |
return NULL; |
| 2171 |
} |
| 2172 |
|
| 2173 |
|
| 2174 |
|
| 2175 |
|
| 2176 |
|
| 2177 |
static size_t s_aws_mqtt5_packet_puback_compute_storage_size(const struct aws_mqtt5_packet_puback_view *puback_view) { |
| 2178 |
size_t storage_size = s_aws_mqtt5_user_property_set_compute_storage_size( |
| 2179 |
puback_view->user_properties, puback_view->user_property_count); |
| 2180 |
|
| 2181 |
if (puback_view->reason_string != NULL) { |
| 2182 |
storage_size += puback_view->reason_string->len; |
| 2183 |
} |
| 2184 |
|
| 2185 |
return storage_size; |
| 2186 |
} |
| 2187 |
|
| 2188 |
AWS_MQTT_API int aws_mqtt5_packet_puback_storage_init( |
| 2189 |
struct aws_mqtt5_packet_puback_storage *puback_storage, |
| 2190 |
struct aws_allocator *allocator, |
| 2191 |
const struct aws_mqtt5_packet_puback_view *puback_view) { |
| 2192 |
AWS_ZERO_STRUCT(*puback_storage); |
| 2193 |
size_t storage_capacity = s_aws_mqtt5_packet_puback_compute_storage_size(puback_view); |
| 2194 |
if (aws_byte_buf_init(&puback_storage->storage, allocator, storage_capacity)) { |
| 2195 |
return AWS_OP_ERR; |
| 2196 |
} |
| 2197 |
|
| 2198 |
struct aws_mqtt5_packet_puback_view *storage_view = &puback_storage->storage_view; |
| 2199 |
|
| 2200 |
storage_view->packet_id = puback_view->packet_id; |
| 2201 |
storage_view->reason_code = puback_view->reason_code; |
| 2202 |
|
| 2203 |
if (puback_view->reason_string != NULL) { |
| 2204 |
puback_storage->reason_string = *puback_view->reason_string; |
| 2205 |
if (aws_byte_buf_append_and_update(&puback_storage->storage, &puback_storage->reason_string)) { |
| 2206 |
return AWS_OP_ERR; |
| 2207 |
} |
| 2208 |
|
| 2209 |
storage_view->reason_string = &puback_storage->reason_string; |
| 2210 |
} |
| 2211 |
|
| 2212 |
if (aws_mqtt5_user_property_set_init_with_storage( |
| 2213 |
&puback_storage->user_properties, |
| 2214 |
allocator, |
| 2215 |
&puback_storage->storage, |
| 2216 |
puback_view->user_property_count, |
| 2217 |
puback_view->user_properties)) { |
| 2218 |
return AWS_OP_ERR; |
| 2219 |
} |
| 2220 |
storage_view->user_property_count = aws_mqtt5_user_property_set_size(&puback_storage->user_properties); |
| 2221 |
storage_view->user_properties = puback_storage->user_properties.properties.data; |
| 2222 |
|
| 2223 |
return AWS_OP_SUCCESS; |
| 2224 |
} |
| 2225 |
|
| 2226 |
int aws_mqtt5_packet_puback_storage_init_from_external_storage( |
| 2227 |
struct aws_mqtt5_packet_puback_storage *puback_storage, |
| 2228 |
struct aws_allocator *allocator) { |
| 2229 |
AWS_ZERO_STRUCT(*puback_storage); |
| 2230 |
|
| 2231 |
if (aws_mqtt5_user_property_set_init(&puback_storage->user_properties, allocator)) { |
| 2232 |
return AWS_OP_ERR; |
| 2233 |
} |
| 2234 |
|
| 2235 |
return AWS_OP_SUCCESS; |
| 2236 |
} |
| 2237 |
|
| 2238 |
void aws_mqtt5_packet_puback_storage_clean_up(struct aws_mqtt5_packet_puback_storage *puback_storage) { |
| 2239 |
|
| 2240 |
if (puback_storage == NULL) { |
| 2241 |
return; |
| 2242 |
} |
| 2243 |
|
| 2244 |
aws_mqtt5_user_property_set_clean_up(&puback_storage->user_properties); |
| 2245 |
|
| 2246 |
aws_byte_buf_clean_up(&puback_storage->storage); |
| 2247 |
} |
| 2248 |
|
| 2249 |
void aws_mqtt5_packet_puback_view_log( |
| 2250 |
const struct aws_mqtt5_packet_puback_view *puback_view, |
| 2251 |
enum aws_log_level level) { |
| 2252 |
struct aws_logger *log_handle = aws_logger_get_conditional(AWS_LS_MQTT5_GENERAL, level); |
| 2253 |
if (log_handle == NULL) { |
| 2254 |
return; |
| 2255 |
} |
| 2256 |
|
| 2257 |
AWS_LOGUF( |
| 2258 |
log_handle, |
| 2259 |
level, |
| 2260 |
AWS_LS_MQTT5_GENERAL, |
| 2261 |
"id=%p: aws_mqtt5_packet_puback_view packet id set to %d", |
| 2262 |
(void *)puback_view, |
| 2263 |
(int)puback_view->packet_id); |
| 2264 |
|
| 2265 |
enum aws_mqtt5_puback_reason_code reason_code = puback_view->reason_code; |
| 2266 |
AWS_LOGUF( |
| 2267 |
log_handle, |
| 2268 |
level, |
| 2269 |
AWS_LS_MQTT5_GENERAL, |
| 2270 |
"id=%p: puback %d reason code: %s", |
| 2271 |
(void *)puback_view, |
| 2272 |
(int)reason_code, |
| 2273 |
aws_mqtt5_puback_reason_code_to_c_string(reason_code)); |
| 2274 |
|
| 2275 |
if (puback_view->reason_string != NULL) { |
| 2276 |
AWS_LOGUF( |
| 2277 |
log_handle, |
| 2278 |
level, |
| 2279 |
AWS_LS_MQTT5_GENERAL, |
| 2280 |
"id=%p: aws_mqtt5_packet_puback_view reason string set to \"" PRInSTR "\"", |
| 2281 |
(void *)puback_view, |
| 2282 |
AWS_BYTE_CURSOR_PRI(*puback_view->reason_string)); |
| 2283 |
} |
| 2284 |
|
| 2285 |
s_aws_mqtt5_user_property_set_log( |
| 2286 |
log_handle, |
| 2287 |
puback_view->user_properties, |
| 2288 |
puback_view->user_property_count, |
| 2289 |
(void *)puback_view, |
| 2290 |
level, |
| 2291 |
"aws_mqtt5_packet_puback_view"); |
| 2292 |
} |
| 2293 |
|
| 2294 |
static void s_aws_mqtt5_operation_puback_manual_completion( |
| 2295 |
struct aws_mqtt5_operation *operation, |
| 2296 |
int error_code, |
| 2297 |
enum aws_mqtt5_packet_type packet_type, |
| 2298 |
const void *completion_view) { |
| 2299 |
(void)packet_type; |
| 2300 |
(void)completion_view; |
| 2301 |
struct aws_mqtt5_operation_puback *puback_op = operation->impl; |
| 2302 |
|
| 2303 |
|
| 2304 |
|
| 2305 |
|
| 2306 |
|
| 2307 |
|
| 2308 |
|
| 2309 |
|
| 2310 |
|
| 2311 |
|
| 2312 |
if (puback_op->completion_options.completion_callback != NULL) { |
| 2313 |
|
| 2314 |
enum aws_mqtt5_manual_publish_acknowledgement_result puback_result = AWS_MQTT5_MPAR_SUCCESS; |
| 2315 |
if (error_code != AWS_OP_SUCCESS) { |
| 2316 |
|
| 2317 |
|
| 2318 |
|
| 2319 |
|
| 2320 |
puback_result = AWS_MQTT5_MPAR_CRT_FAILURE; |
| 2321 |
} |
| 2322 |
puback_op->completion_options.completion_callback( |
| 2323 |
puback_result, puback_op->completion_options.completion_user_data); |
| 2324 |
} |
| 2325 |
} |
| 2326 |
|
| 2327 |
static struct aws_mqtt5_operation_vtable s_puback_operation_vtable = { |
| 2328 |
.aws_mqtt5_operation_completion_fn = s_aws_mqtt5_operation_puback_manual_completion, |
| 2329 |
.aws_mqtt5_operation_set_packet_id_fn = NULL, |
| 2330 |
.aws_mqtt5_operation_get_packet_id_address_fn = NULL, |
| 2331 |
.aws_mqtt5_operation_validate_vs_connection_settings_fn = NULL, |
| 2332 |
.aws_mqtt5_operation_get_ack_timeout_override_fn = NULL, |
| 2333 |
}; |
| 2334 |
|
| 2335 |
static void s_destroy_operation_puback(void *object) { |
| 2336 |
if (object == NULL) { |
| 2337 |
return; |
| 2338 |
} |
| 2339 |
|
| 2340 |
struct aws_mqtt5_operation_puback *puback_op = object; |
| 2341 |
|
| 2342 |
aws_mqtt5_packet_puback_storage_clean_up(&puback_op->options_storage); |
| 2343 |
|
| 2344 |
aws_mem_release(puback_op->allocator, puback_op); |
| 2345 |
} |
| 2346 |
|
| 2347 |
struct aws_mqtt5_operation_puback *aws_mqtt5_operation_puback_new( |
| 2348 |
struct aws_allocator *allocator, |
| 2349 |
const struct aws_mqtt5_packet_puback_view *puback_options, |
| 2350 |
const struct aws_mqtt5_manual_publish_acknowledgement_completion_options *completion_options) { |
| 2351 |
AWS_PRECONDITION(allocator != NULL); |
| 2352 |
AWS_PRECONDITION(puback_options != NULL); |
| 2353 |
|
| 2354 |
struct aws_mqtt5_operation_puback *puback_op = |
| 2355 |
aws_mem_calloc(allocator, 1, sizeof(struct aws_mqtt5_operation_puback)); |
| 2356 |
if (puback_op == NULL) { |
| 2357 |
return NULL; |
| 2358 |
} |
| 2359 |
|
| 2360 |
puback_op->allocator = allocator; |
| 2361 |
puback_op->base.vtable = &s_puback_operation_vtable; |
| 2362 |
puback_op->base.packet_type = AWS_MQTT5_PT_PUBACK; |
| 2363 |
if (completion_options != NULL) { |
| 2364 |
puback_op->completion_options = *completion_options; |
| 2365 |
} |
| 2366 |
aws_ref_count_init(&puback_op->base.ref_count, puback_op, s_destroy_operation_puback); |
| 2367 |
aws_priority_queue_node_init(&puback_op->base.priority_queue_node); |
| 2368 |
puback_op->base.impl = puback_op; |
| 2369 |
|
| 2370 |
if (aws_mqtt5_packet_puback_storage_init(&puback_op->options_storage, allocator, puback_options)) { |
| 2371 |
goto error; |
| 2372 |
} |
| 2373 |
|
| 2374 |
puback_op->base.packet_view = &puback_op->options_storage.storage_view; |
| 2375 |
|
| 2376 |
return puback_op; |
| 2377 |
|
| 2378 |
error: |
| 2379 |
|
| 2380 |
aws_mqtt5_operation_release(&puback_op->base); |
| 2381 |
|
| 2382 |
return NULL; |
| 2383 |
} |
| 2384 |
|
| 2385 |
|
| 2386 |
|
| 2387 |
|
| 2388 |
|
| 2389 |
int aws_mqtt5_packet_unsubscribe_view_validate(const struct aws_mqtt5_packet_unsubscribe_view *unsubscribe_view) { |
| 2390 |
|
| 2391 |
if (unsubscribe_view == NULL) { |
| 2392 |
AWS_LOGF_ERROR(AWS_LS_MQTT5_GENERAL, "null UNSUBSCRIBE packet options"); |
| 2393 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 2394 |
} |
| 2395 |
|
| 2396 |
if (unsubscribe_view->topic_filter_count == 0) { |
| 2397 |
AWS_LOGF_ERROR( |
| 2398 |
AWS_LS_MQTT5_GENERAL, |
| 2399 |
"id=%p: aws_mqtt5_packet_unsubscribe_view - must contain at least one topic", |
| 2400 |
(void *)unsubscribe_view); |
| 2401 |
return aws_raise_error(AWS_ERROR_MQTT5_UNSUBSCRIBE_OPTIONS_VALIDATION); |
| 2402 |
} |
| 2403 |
|
| 2404 |
if (unsubscribe_view->topic_filter_count > AWS_MQTT5_CLIENT_MAXIMUM_TOPIC_FILTERS_PER_UNSUBSCRIBE) { |
| 2405 |
AWS_LOGF_ERROR( |
| 2406 |
AWS_LS_MQTT5_GENERAL, |
| 2407 |
"id=%p: aws_mqtt5_packet_unsubscribe_view - contains too many topics (%zu)", |
| 2408 |
(void *)unsubscribe_view, |
| 2409 |
unsubscribe_view->topic_filter_count); |
| 2410 |
return aws_raise_error(AWS_ERROR_MQTT5_UNSUBSCRIBE_OPTIONS_VALIDATION); |
| 2411 |
} |
| 2412 |
|
| 2413 |
for (size_t i = 0; i < unsubscribe_view->topic_filter_count; ++i) { |
| 2414 |
const struct aws_byte_cursor *topic_filter = &unsubscribe_view->topic_filters[i]; |
| 2415 |
if (aws_mqtt_validate_utf8_text(*topic_filter)) { |
| 2416 |
AWS_LOGF_ERROR( |
| 2417 |
AWS_LS_MQTT5_GENERAL, |
| 2418 |
"id=%p: aws_mqtt5_packet_unsubscribe_view - topic filter not valid UTF-8: \"" PRInSTR "\"", |
| 2419 |
(void *)unsubscribe_view, |
| 2420 |
AWS_BYTE_CURSOR_PRI(*topic_filter)); |
| 2421 |
return aws_raise_error(AWS_ERROR_MQTT5_UNSUBSCRIBE_OPTIONS_VALIDATION); |
| 2422 |
} |
| 2423 |
if (!aws_mqtt_is_valid_topic_filter(topic_filter)) { |
| 2424 |
AWS_LOGF_ERROR( |
| 2425 |
AWS_LS_MQTT5_GENERAL, |
| 2426 |
"id=%p: aws_mqtt5_packet_unsubscribe_view - invalid topic filter: \"" PRInSTR "\"", |
| 2427 |
(void *)unsubscribe_view, |
| 2428 |
AWS_BYTE_CURSOR_PRI(*topic_filter)); |
| 2429 |
return aws_raise_error(AWS_ERROR_MQTT5_UNSUBSCRIBE_OPTIONS_VALIDATION); |
| 2430 |
} |
| 2431 |
} |
| 2432 |
|
| 2433 |
if (s_aws_mqtt5_user_property_set_validate( |
| 2434 |
unsubscribe_view->user_properties, |
| 2435 |
unsubscribe_view->user_property_count, |
| 2436 |
"aws_mqtt5_packet_unsubscribe_view", |
| 2437 |
(void *)unsubscribe_view)) { |
| 2438 |
return AWS_OP_ERR; |
| 2439 |
} |
| 2440 |
|
| 2441 |
return AWS_OP_SUCCESS; |
| 2442 |
} |
| 2443 |
|
| 2444 |
void aws_mqtt5_packet_unsubscribe_view_log( |
| 2445 |
const struct aws_mqtt5_packet_unsubscribe_view *unsubscribe_view, |
| 2446 |
enum aws_log_level level) { |
| 2447 |
struct aws_logger *log_handle = aws_logger_get_conditional(AWS_LS_MQTT5_GENERAL, level); |
| 2448 |
if (log_handle == NULL) { |
| 2449 |
return; |
| 2450 |
} |
| 2451 |
|
| 2452 |
size_t topic_count = unsubscribe_view->topic_filter_count; |
| 2453 |
for (size_t i = 0; i < topic_count; ++i) { |
| 2454 |
const struct aws_byte_cursor *topic_cursor = &unsubscribe_view->topic_filters[i]; |
| 2455 |
|
| 2456 |
AWS_LOGUF( |
| 2457 |
log_handle, |
| 2458 |
level, |
| 2459 |
AWS_LS_MQTT5_GENERAL, |
| 2460 |
"id=%p: aws_mqtt5_packet_unsubscribe_view topic #%zu: \"" PRInSTR "\"", |
| 2461 |
(void *)unsubscribe_view, |
| 2462 |
i, |
| 2463 |
AWS_BYTE_CURSOR_PRI(*topic_cursor)); |
| 2464 |
} |
| 2465 |
|
| 2466 |
s_aws_mqtt5_user_property_set_log( |
| 2467 |
log_handle, |
| 2468 |
unsubscribe_view->user_properties, |
| 2469 |
unsubscribe_view->user_property_count, |
| 2470 |
(void *)unsubscribe_view, |
| 2471 |
level, |
| 2472 |
"aws_mqtt5_packet_unsubscribe_view"); |
| 2473 |
} |
| 2474 |
|
| 2475 |
void aws_mqtt5_packet_unsubscribe_storage_clean_up(struct aws_mqtt5_packet_unsubscribe_storage *unsubscribe_storage) { |
| 2476 |
if (unsubscribe_storage == NULL) { |
| 2477 |
return; |
| 2478 |
} |
| 2479 |
|
| 2480 |
aws_array_list_clean_up(&unsubscribe_storage->topic_filters); |
| 2481 |
aws_mqtt5_user_property_set_clean_up(&unsubscribe_storage->user_properties); |
| 2482 |
aws_byte_buf_clean_up(&unsubscribe_storage->storage); |
| 2483 |
} |
| 2484 |
|
| 2485 |
static int s_aws_mqtt5_packet_unsubscribe_build_topic_list( |
| 2486 |
struct aws_mqtt5_packet_unsubscribe_storage *unsubscribe_storage, |
| 2487 |
struct aws_allocator *allocator, |
| 2488 |
size_t topic_count, |
| 2489 |
const struct aws_byte_cursor *topics) { |
| 2490 |
|
| 2491 |
if (aws_array_list_init_dynamic( |
| 2492 |
&unsubscribe_storage->topic_filters, allocator, topic_count, sizeof(struct aws_byte_cursor))) { |
| 2493 |
return AWS_OP_ERR; |
| 2494 |
} |
| 2495 |
|
| 2496 |
for (size_t i = 0; i < topic_count; ++i) { |
| 2497 |
const struct aws_byte_cursor *topic_cursor_ptr = &topics[i]; |
| 2498 |
struct aws_byte_cursor topic_cursor = *topic_cursor_ptr; |
| 2499 |
|
| 2500 |
if (aws_byte_buf_append_and_update(&unsubscribe_storage->storage, &topic_cursor)) { |
| 2501 |
return AWS_OP_ERR; |
| 2502 |
} |
| 2503 |
|
| 2504 |
if (aws_array_list_push_back(&unsubscribe_storage->topic_filters, &topic_cursor)) { |
| 2505 |
return AWS_OP_ERR; |
| 2506 |
} |
| 2507 |
} |
| 2508 |
|
| 2509 |
return AWS_OP_SUCCESS; |
| 2510 |
} |
| 2511 |
|
| 2512 |
static size_t s_aws_mqtt5_packet_unsubscribe_compute_storage_size( |
| 2513 |
const struct aws_mqtt5_packet_unsubscribe_view *unsubscribe_view) { |
| 2514 |
size_t storage_size = s_aws_mqtt5_user_property_set_compute_storage_size( |
| 2515 |
unsubscribe_view->user_properties, unsubscribe_view->user_property_count); |
| 2516 |
|
| 2517 |
for (size_t i = 0; i < unsubscribe_view->topic_filter_count; ++i) { |
| 2518 |
const struct aws_byte_cursor *topic = &unsubscribe_view->topic_filters[i]; |
| 2519 |
storage_size += topic->len; |
| 2520 |
} |
| 2521 |
|
| 2522 |
return storage_size; |
| 2523 |
} |
| 2524 |
|
| 2525 |
int aws_mqtt5_packet_unsubscribe_storage_init( |
| 2526 |
struct aws_mqtt5_packet_unsubscribe_storage *unsubscribe_storage, |
| 2527 |
struct aws_allocator *allocator, |
| 2528 |
const struct aws_mqtt5_packet_unsubscribe_view *unsubscribe_options) { |
| 2529 |
|
| 2530 |
AWS_ZERO_STRUCT(*unsubscribe_storage); |
| 2531 |
size_t storage_capacity = s_aws_mqtt5_packet_unsubscribe_compute_storage_size(unsubscribe_options); |
| 2532 |
if (aws_byte_buf_init(&unsubscribe_storage->storage, allocator, storage_capacity)) { |
| 2533 |
return AWS_OP_ERR; |
| 2534 |
} |
| 2535 |
|
| 2536 |
struct aws_mqtt5_packet_unsubscribe_view *storage_view = &unsubscribe_storage->storage_view; |
| 2537 |
|
| 2538 |
if (s_aws_mqtt5_packet_unsubscribe_build_topic_list( |
| 2539 |
unsubscribe_storage, |
| 2540 |
allocator, |
| 2541 |
unsubscribe_options->topic_filter_count, |
| 2542 |
unsubscribe_options->topic_filters)) { |
| 2543 |
return AWS_OP_ERR; |
| 2544 |
} |
| 2545 |
storage_view->topic_filter_count = aws_array_list_length(&unsubscribe_storage->topic_filters); |
| 2546 |
storage_view->topic_filters = unsubscribe_storage->topic_filters.data; |
| 2547 |
|
| 2548 |
if (aws_mqtt5_user_property_set_init_with_storage( |
| 2549 |
&unsubscribe_storage->user_properties, |
| 2550 |
allocator, |
| 2551 |
&unsubscribe_storage->storage, |
| 2552 |
unsubscribe_options->user_property_count, |
| 2553 |
unsubscribe_options->user_properties)) { |
| 2554 |
return AWS_OP_ERR; |
| 2555 |
} |
| 2556 |
storage_view->user_property_count = aws_mqtt5_user_property_set_size(&unsubscribe_storage->user_properties); |
| 2557 |
storage_view->user_properties = unsubscribe_storage->user_properties.properties.data; |
| 2558 |
|
| 2559 |
return AWS_OP_SUCCESS; |
| 2560 |
} |
| 2561 |
|
| 2562 |
int aws_mqtt5_packet_unsubscribe_storage_init_from_external_storage( |
| 2563 |
struct aws_mqtt5_packet_unsubscribe_storage *unsubscribe_storage, |
| 2564 |
struct aws_allocator *allocator) { |
| 2565 |
AWS_ZERO_STRUCT(*unsubscribe_storage); |
| 2566 |
|
| 2567 |
if (aws_mqtt5_user_property_set_init(&unsubscribe_storage->user_properties, allocator)) { |
| 2568 |
return AWS_OP_ERR; |
| 2569 |
} |
| 2570 |
|
| 2571 |
if (aws_array_list_init_dynamic( |
| 2572 |
&unsubscribe_storage->topic_filters, allocator, 0, sizeof(struct aws_byte_cursor))) { |
| 2573 |
return AWS_OP_ERR; |
| 2574 |
} |
| 2575 |
|
| 2576 |
return AWS_OP_SUCCESS; |
| 2577 |
} |
| 2578 |
|
| 2579 |
static void s_aws_mqtt5_operation_unsubscribe_complete( |
| 2580 |
struct aws_mqtt5_operation *operation, |
| 2581 |
int error_code, |
| 2582 |
enum aws_mqtt5_packet_type packet_type, |
| 2583 |
const void *completion_view) { |
| 2584 |
struct aws_mqtt5_operation_unsubscribe *unsubscribe_op = operation->impl; |
| 2585 |
(void)packet_type; |
| 2586 |
|
| 2587 |
if (unsubscribe_op->completion_options.completion_callback != NULL) { |
| 2588 |
(*unsubscribe_op->completion_options.completion_callback)( |
| 2589 |
completion_view, error_code, unsubscribe_op->completion_options.completion_user_data); |
| 2590 |
} |
| 2591 |
} |
| 2592 |
|
| 2593 |
static void s_aws_mqtt5_operation_unsubscribe_set_packet_id( |
| 2594 |
struct aws_mqtt5_operation *operation, |
| 2595 |
aws_mqtt5_packet_id_t packet_id) { |
| 2596 |
struct aws_mqtt5_operation_unsubscribe *unsubscribe_op = operation->impl; |
| 2597 |
unsubscribe_op->options_storage.storage_view.packet_id = packet_id; |
| 2598 |
} |
| 2599 |
|
| 2600 |
static aws_mqtt5_packet_id_t *s_aws_mqtt5_operation_unsubscribe_get_packet_id_address( |
| 2601 |
const struct aws_mqtt5_operation *operation) { |
| 2602 |
struct aws_mqtt5_operation_unsubscribe *unsubscribe_op = operation->impl; |
| 2603 |
return &unsubscribe_op->options_storage.storage_view.packet_id; |
| 2604 |
} |
| 2605 |
|
| 2606 |
static uint32_t s_aws_mqtt5_operation_unsubscribe_get_ack_timeout_override( |
| 2607 |
const struct aws_mqtt5_operation *operation) { |
| 2608 |
struct aws_mqtt5_operation_unsubscribe *unsubscribe_op = operation->impl; |
| 2609 |
return unsubscribe_op->completion_options.ack_timeout_seconds_override; |
| 2610 |
} |
| 2611 |
|
| 2612 |
static struct aws_mqtt5_operation_vtable s_unsubscribe_operation_vtable = { |
| 2613 |
.aws_mqtt5_operation_completion_fn = s_aws_mqtt5_operation_unsubscribe_complete, |
| 2614 |
.aws_mqtt5_operation_set_packet_id_fn = s_aws_mqtt5_operation_unsubscribe_set_packet_id, |
| 2615 |
.aws_mqtt5_operation_get_packet_id_address_fn = s_aws_mqtt5_operation_unsubscribe_get_packet_id_address, |
| 2616 |
.aws_mqtt5_operation_validate_vs_connection_settings_fn = NULL, |
| 2617 |
.aws_mqtt5_operation_get_ack_timeout_override_fn = s_aws_mqtt5_operation_unsubscribe_get_ack_timeout_override, |
| 2618 |
}; |
| 2619 |
|
| 2620 |
static void s_destroy_operation_unsubscribe(void *object) { |
| 2621 |
if (object == NULL) { |
| 2622 |
return; |
| 2623 |
} |
| 2624 |
|
| 2625 |
struct aws_mqtt5_operation_unsubscribe *unsubscribe_op = object; |
| 2626 |
|
| 2627 |
aws_mqtt5_packet_unsubscribe_storage_clean_up(&unsubscribe_op->options_storage); |
| 2628 |
|
| 2629 |
aws_mem_release(unsubscribe_op->allocator, unsubscribe_op); |
| 2630 |
} |
| 2631 |
|
| 2632 |
struct aws_mqtt5_operation_unsubscribe *aws_mqtt5_operation_unsubscribe_new( |
| 2633 |
struct aws_allocator *allocator, |
| 2634 |
const struct aws_mqtt5_client *client, |
| 2635 |
const struct aws_mqtt5_packet_unsubscribe_view *unsubscribe_options, |
| 2636 |
const struct aws_mqtt5_unsubscribe_completion_options *completion_options) { |
| 2637 |
(void)client; |
| 2638 |
AWS_PRECONDITION(allocator != NULL); |
| 2639 |
AWS_PRECONDITION(unsubscribe_options != NULL); |
| 2640 |
|
| 2641 |
if (aws_mqtt5_packet_unsubscribe_view_validate(unsubscribe_options)) { |
| 2642 |
return NULL; |
| 2643 |
} |
| 2644 |
|
| 2645 |
if (unsubscribe_options->packet_id != 0) { |
| 2646 |
AWS_LOGF_DEBUG( |
| 2647 |
AWS_LS_MQTT5_GENERAL, |
| 2648 |
"id=%p: aws_mqtt5_packet_unsubscribe_view packet id must be zero", |
| 2649 |
(void *)unsubscribe_options); |
| 2650 |
aws_raise_error(AWS_ERROR_MQTT5_UNSUBSCRIBE_OPTIONS_VALIDATION); |
| 2651 |
return NULL; |
| 2652 |
} |
| 2653 |
|
| 2654 |
struct aws_mqtt5_operation_unsubscribe *unsubscribe_op = |
| 2655 |
aws_mem_calloc(allocator, 1, sizeof(struct aws_mqtt5_operation_unsubscribe)); |
| 2656 |
if (unsubscribe_op == NULL) { |
| 2657 |
return NULL; |
| 2658 |
} |
| 2659 |
|
| 2660 |
unsubscribe_op->allocator = allocator; |
| 2661 |
unsubscribe_op->base.vtable = &s_unsubscribe_operation_vtable; |
| 2662 |
unsubscribe_op->base.packet_type = AWS_MQTT5_PT_UNSUBSCRIBE; |
| 2663 |
aws_ref_count_init(&unsubscribe_op->base.ref_count, unsubscribe_op, s_destroy_operation_unsubscribe); |
| 2664 |
aws_priority_queue_node_init(&unsubscribe_op->base.priority_queue_node); |
| 2665 |
unsubscribe_op->base.impl = unsubscribe_op; |
| 2666 |
|
| 2667 |
if (aws_mqtt5_packet_unsubscribe_storage_init(&unsubscribe_op->options_storage, allocator, unsubscribe_options)) { |
| 2668 |
goto error; |
| 2669 |
} |
| 2670 |
|
| 2671 |
unsubscribe_op->base.packet_view = &unsubscribe_op->options_storage.storage_view; |
| 2672 |
|
| 2673 |
if (completion_options != NULL) { |
| 2674 |
unsubscribe_op->completion_options = *completion_options; |
| 2675 |
} |
| 2676 |
|
| 2677 |
return unsubscribe_op; |
| 2678 |
|
| 2679 |
error: |
| 2680 |
|
| 2681 |
aws_mqtt5_operation_release(&unsubscribe_op->base); |
| 2682 |
|
| 2683 |
return NULL; |
| 2684 |
} |
| 2685 |
|
| 2686 |
|
| 2687 |
|
| 2688 |
|
| 2689 |
|
| 2690 |
static int s_aws_mqtt5_validate_subscription( |
| 2691 |
const struct aws_mqtt5_subscription_view *subscription, |
| 2692 |
void *log_context) { |
| 2693 |
|
| 2694 |
if (aws_mqtt_validate_utf8_text(subscription->topic_filter)) { |
| 2695 |
AWS_LOGF_ERROR( |
| 2696 |
AWS_LS_MQTT5_GENERAL, |
| 2697 |
"id=%p: aws_mqtt5_packet_subscribe_view - topic filter \"" PRInSTR "\" not valid UTF-8 in subscription", |
| 2698 |
log_context, |
| 2699 |
AWS_BYTE_CURSOR_PRI(subscription->topic_filter)); |
| 2700 |
return aws_raise_error(AWS_ERROR_MQTT5_SUBSCRIBE_OPTIONS_VALIDATION); |
| 2701 |
} |
| 2702 |
|
| 2703 |
if (!aws_mqtt_is_valid_topic_filter(&subscription->topic_filter)) { |
| 2704 |
AWS_LOGF_ERROR( |
| 2705 |
AWS_LS_MQTT5_GENERAL, |
| 2706 |
"id=%p: aws_mqtt5_packet_subscribe_view - invalid topic filter \"" PRInSTR "\" in subscription", |
| 2707 |
log_context, |
| 2708 |
AWS_BYTE_CURSOR_PRI(subscription->topic_filter)); |
| 2709 |
return aws_raise_error(AWS_ERROR_MQTT5_SUBSCRIBE_OPTIONS_VALIDATION); |
| 2710 |
} |
| 2711 |
|
| 2712 |
if (subscription->topic_filter.len > UINT16_MAX) { |
| 2713 |
AWS_LOGF_ERROR( |
| 2714 |
AWS_LS_MQTT5_GENERAL, |
| 2715 |
"id=%p: aws_mqtt5_packet_subscribe_view - subscription contains too-long topic filter", |
| 2716 |
log_context); |
| 2717 |
return aws_raise_error(AWS_ERROR_MQTT5_SUBSCRIBE_OPTIONS_VALIDATION); |
| 2718 |
} |
| 2719 |
|
| 2720 |
if (subscription->qos < AWS_MQTT5_QOS_AT_MOST_ONCE || subscription->qos > AWS_MQTT5_QOS_AT_LEAST_ONCE) { |
| 2721 |
AWS_LOGF_ERROR( |
| 2722 |
AWS_LS_MQTT5_GENERAL, |
| 2723 |
"id=%p: aws_mqtt5_packet_subscribe_view - unsupported QoS value: %d", |
| 2724 |
log_context, |
| 2725 |
(int)subscription->qos); |
| 2726 |
return aws_raise_error(AWS_ERROR_MQTT5_SUBSCRIBE_OPTIONS_VALIDATION); |
| 2727 |
} |
| 2728 |
|
| 2729 |
if (subscription->retain_handling_type < AWS_MQTT5_RHT_SEND_ON_SUBSCRIBE || |
| 2730 |
subscription->retain_handling_type > AWS_MQTT5_RHT_DONT_SEND) { |
| 2731 |
AWS_LOGF_ERROR( |
| 2732 |
AWS_LS_MQTT5_GENERAL, |
| 2733 |
"id=%p: aws_mqtt5_packet_subscribe_view - unsupported retain handling value: %d", |
| 2734 |
log_context, |
| 2735 |
(int)subscription->retain_handling_type); |
| 2736 |
return aws_raise_error(AWS_ERROR_MQTT5_SUBSCRIBE_OPTIONS_VALIDATION); |
| 2737 |
} |
| 2738 |
|
| 2739 |
|
| 2740 |
if (subscription->no_local) { |
| 2741 |
if (aws_mqtt_is_topic_filter_shared_subscription(subscription->topic_filter)) { |
| 2742 |
AWS_LOGF_ERROR( |
| 2743 |
AWS_LS_MQTT5_GENERAL, |
| 2744 |
"id=%p: aws_mqtt5_packet_subscribe_view - no_local cannot be 1 if the topic filter is a shared" |
| 2745 |
"subscription", |
| 2746 |
log_context); |
| 2747 |
return aws_raise_error(AWS_ERROR_MQTT5_SUBSCRIBE_OPTIONS_VALIDATION); |
| 2748 |
} |
| 2749 |
} |
| 2750 |
|
| 2751 |
return AWS_OP_SUCCESS; |
| 2752 |
} |
| 2753 |
|
| 2754 |
int aws_mqtt5_packet_subscribe_view_validate(const struct aws_mqtt5_packet_subscribe_view *subscribe_view) { |
| 2755 |
|
| 2756 |
if (subscribe_view == NULL) { |
| 2757 |
AWS_LOGF_ERROR(AWS_LS_MQTT5_GENERAL, "null SUBSCRIBE packet options"); |
| 2758 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 2759 |
} |
| 2760 |
|
| 2761 |
if (subscribe_view->subscription_count == 0) { |
| 2762 |
AWS_LOGF_ERROR( |
| 2763 |
AWS_LS_MQTT5_GENERAL, |
| 2764 |
"id=%p: aws_mqtt5_packet_subscribe_view - must contain at least one subscription", |
| 2765 |
(void *)subscribe_view); |
| 2766 |
return aws_raise_error(AWS_ERROR_MQTT5_SUBSCRIBE_OPTIONS_VALIDATION); |
| 2767 |
} |
| 2768 |
|
| 2769 |
if (subscribe_view->subscription_count > AWS_MQTT5_CLIENT_MAXIMUM_SUBSCRIPTIONS_PER_SUBSCRIBE) { |
| 2770 |
AWS_LOGF_ERROR( |
| 2771 |
AWS_LS_MQTT5_GENERAL, |
| 2772 |
"id=%p: aws_mqtt5_packet_subscribe_view - too many subscriptions", |
| 2773 |
(void *)subscribe_view); |
| 2774 |
return aws_raise_error(AWS_ERROR_MQTT5_SUBSCRIBE_OPTIONS_VALIDATION); |
| 2775 |
} |
| 2776 |
|
| 2777 |
for (size_t i = 0; i < subscribe_view->subscription_count; ++i) { |
| 2778 |
const struct aws_mqtt5_subscription_view *subscription = &subscribe_view->subscriptions[i]; |
| 2779 |
if (s_aws_mqtt5_validate_subscription(subscription, (void *)subscribe_view)) { |
| 2780 |
AWS_LOGF_ERROR( |
| 2781 |
AWS_LS_MQTT5_GENERAL, |
| 2782 |
"id=%p: aws_mqtt5_packet_subscribe_view - invalid subscription", |
| 2783 |
(void *)subscribe_view); |
| 2784 |
return aws_raise_error(AWS_ERROR_MQTT5_SUBSCRIBE_OPTIONS_VALIDATION); |
| 2785 |
} |
| 2786 |
} |
| 2787 |
|
| 2788 |
if (subscribe_view->subscription_identifier != NULL) { |
| 2789 |
if (*subscribe_view->subscription_identifier > AWS_MQTT5_MAXIMUM_VARIABLE_LENGTH_INTEGER) { |
| 2790 |
AWS_LOGF_ERROR( |
| 2791 |
AWS_LS_MQTT5_GENERAL, |
| 2792 |
"id=%p: aws_mqtt5_packet_subscribe_view - subscription identifier (%" PRIu32 ") too large", |
| 2793 |
(void *)subscribe_view, |
| 2794 |
*subscribe_view->subscription_identifier); |
| 2795 |
return aws_raise_error(AWS_ERROR_MQTT5_SUBSCRIBE_OPTIONS_VALIDATION); |
| 2796 |
} |
| 2797 |
} |
| 2798 |
|
| 2799 |
if (s_aws_mqtt5_user_property_set_validate( |
| 2800 |
subscribe_view->user_properties, |
| 2801 |
subscribe_view->user_property_count, |
| 2802 |
"aws_mqtt5_packet_subscribe_view", |
| 2803 |
(void *)subscribe_view)) { |
| 2804 |
return AWS_OP_ERR; |
| 2805 |
} |
| 2806 |
|
| 2807 |
return AWS_OP_SUCCESS; |
| 2808 |
} |
| 2809 |
|
| 2810 |
void aws_mqtt5_packet_subscribe_view_log( |
| 2811 |
const struct aws_mqtt5_packet_subscribe_view *subscribe_view, |
| 2812 |
enum aws_log_level level) { |
| 2813 |
struct aws_logger *log_handle = aws_logger_get_conditional(AWS_LS_MQTT5_GENERAL, level); |
| 2814 |
if (log_handle == NULL) { |
| 2815 |
return; |
| 2816 |
} |
| 2817 |
|
| 2818 |
size_t subscription_count = subscribe_view->subscription_count; |
| 2819 |
for (size_t i = 0; i < subscription_count; ++i) { |
| 2820 |
const struct aws_mqtt5_subscription_view *view = &subscribe_view->subscriptions[i]; |
| 2821 |
|
| 2822 |
AWS_LOGUF( |
| 2823 |
log_handle, |
| 2824 |
level, |
| 2825 |
AWS_LS_MQTT5_GENERAL, |
| 2826 |
"id=%p: aws_mqtt5_packet_subscribe_view subscription #%zu, topic filter \"" PRInSTR |
| 2827 |
"\", qos %d, no local %d, retain as " |
| 2828 |
"published %d, retain handling %d (%s)", |
| 2829 |
(void *)subscribe_view, |
| 2830 |
i, |
| 2831 |
AWS_BYTE_CURSOR_PRI(view->topic_filter), |
| 2832 |
(int)view->qos, |
| 2833 |
(int)view->no_local, |
| 2834 |
(int)view->retain_as_published, |
| 2835 |
(int)view->retain_handling_type, |
| 2836 |
aws_mqtt5_retain_handling_type_to_c_string(view->retain_handling_type)); |
| 2837 |
} |
| 2838 |
|
| 2839 |
if (subscribe_view->subscription_identifier != NULL) { |
| 2840 |
AWS_LOGUF( |
| 2841 |
log_handle, |
| 2842 |
level, |
| 2843 |
AWS_LS_MQTT5_GENERAL, |
| 2844 |
"id=%p: aws_mqtt5_packet_subscribe_view subscription identifier set to %" PRIu32, |
| 2845 |
(void *)subscribe_view, |
| 2846 |
*subscribe_view->subscription_identifier); |
| 2847 |
} |
| 2848 |
|
| 2849 |
s_aws_mqtt5_user_property_set_log( |
| 2850 |
log_handle, |
| 2851 |
subscribe_view->user_properties, |
| 2852 |
subscribe_view->user_property_count, |
| 2853 |
(void *)subscribe_view, |
| 2854 |
level, |
| 2855 |
"aws_mqtt5_packet_subscribe_view"); |
| 2856 |
} |
| 2857 |
|
| 2858 |
void aws_mqtt5_packet_subscribe_storage_clean_up(struct aws_mqtt5_packet_subscribe_storage *subscribe_storage) { |
| 2859 |
if (subscribe_storage == NULL) { |
| 2860 |
return; |
| 2861 |
} |
| 2862 |
|
| 2863 |
aws_array_list_clean_up(&subscribe_storage->subscriptions); |
| 2864 |
|
| 2865 |
aws_mqtt5_user_property_set_clean_up(&subscribe_storage->user_properties); |
| 2866 |
aws_byte_buf_clean_up(&subscribe_storage->storage); |
| 2867 |
} |
| 2868 |
|
| 2869 |
static int s_aws_mqtt5_packet_subscribe_storage_init_subscriptions( |
| 2870 |
struct aws_mqtt5_packet_subscribe_storage *subscribe_storage, |
| 2871 |
struct aws_allocator *allocator, |
| 2872 |
size_t subscription_count, |
| 2873 |
const struct aws_mqtt5_subscription_view *subscriptions) { |
| 2874 |
|
| 2875 |
if (aws_array_list_init_dynamic( |
| 2876 |
&subscribe_storage->subscriptions, |
| 2877 |
allocator, |
| 2878 |
subscription_count, |
| 2879 |
sizeof(struct aws_mqtt5_subscription_view))) { |
| 2880 |
return AWS_OP_ERR; |
| 2881 |
} |
| 2882 |
|
| 2883 |
for (size_t i = 0; i < subscription_count; ++i) { |
| 2884 |
const struct aws_mqtt5_subscription_view *source = &subscriptions[i]; |
| 2885 |
struct aws_mqtt5_subscription_view copy = *source; |
| 2886 |
|
| 2887 |
if (aws_byte_buf_append_and_update(&subscribe_storage->storage, ©.topic_filter)) { |
| 2888 |
return AWS_OP_ERR; |
| 2889 |
} |
| 2890 |
|
| 2891 |
if (aws_array_list_push_back(&subscribe_storage->subscriptions, ©)) { |
| 2892 |
return AWS_OP_ERR; |
| 2893 |
} |
| 2894 |
} |
| 2895 |
|
| 2896 |
return AWS_OP_SUCCESS; |
| 2897 |
} |
| 2898 |
|
| 2899 |
static size_t s_aws_mqtt5_packet_subscribe_compute_storage_size( |
| 2900 |
const struct aws_mqtt5_packet_subscribe_view *subscribe_view) { |
| 2901 |
size_t storage_size = s_aws_mqtt5_user_property_set_compute_storage_size( |
| 2902 |
subscribe_view->user_properties, subscribe_view->user_property_count); |
| 2903 |
|
| 2904 |
for (size_t i = 0; i < subscribe_view->subscription_count; ++i) { |
| 2905 |
const struct aws_mqtt5_subscription_view *subscription = &subscribe_view->subscriptions[i]; |
| 2906 |
storage_size += subscription->topic_filter.len; |
| 2907 |
} |
| 2908 |
|
| 2909 |
return storage_size; |
| 2910 |
} |
| 2911 |
|
| 2912 |
int aws_mqtt5_packet_subscribe_storage_init( |
| 2913 |
struct aws_mqtt5_packet_subscribe_storage *subscribe_storage, |
| 2914 |
struct aws_allocator *allocator, |
| 2915 |
const struct aws_mqtt5_packet_subscribe_view *subscribe_options) { |
| 2916 |
|
| 2917 |
AWS_ZERO_STRUCT(*subscribe_storage); |
| 2918 |
size_t storage_capacity = s_aws_mqtt5_packet_subscribe_compute_storage_size(subscribe_options); |
| 2919 |
if (aws_byte_buf_init(&subscribe_storage->storage, allocator, storage_capacity)) { |
| 2920 |
return AWS_OP_ERR; |
| 2921 |
} |
| 2922 |
|
| 2923 |
struct aws_mqtt5_packet_subscribe_view *storage_view = &subscribe_storage->storage_view; |
| 2924 |
storage_view->packet_id = subscribe_options->packet_id; |
| 2925 |
|
| 2926 |
if (subscribe_options->subscription_identifier != NULL) { |
| 2927 |
subscribe_storage->subscription_identifier = *subscribe_options->subscription_identifier; |
| 2928 |
storage_view->subscription_identifier = &subscribe_storage->subscription_identifier; |
| 2929 |
} |
| 2930 |
|
| 2931 |
if (s_aws_mqtt5_packet_subscribe_storage_init_subscriptions( |
| 2932 |
subscribe_storage, allocator, subscribe_options->subscription_count, subscribe_options->subscriptions)) { |
| 2933 |
return AWS_OP_ERR; |
| 2934 |
} |
| 2935 |
storage_view->subscription_count = aws_array_list_length(&subscribe_storage->subscriptions); |
| 2936 |
storage_view->subscriptions = subscribe_storage->subscriptions.data; |
| 2937 |
|
| 2938 |
if (aws_mqtt5_user_property_set_init_with_storage( |
| 2939 |
&subscribe_storage->user_properties, |
| 2940 |
allocator, |
| 2941 |
&subscribe_storage->storage, |
| 2942 |
subscribe_options->user_property_count, |
| 2943 |
subscribe_options->user_properties)) { |
| 2944 |
return AWS_OP_ERR; |
| 2945 |
} |
| 2946 |
storage_view->user_property_count = aws_mqtt5_user_property_set_size(&subscribe_storage->user_properties); |
| 2947 |
storage_view->user_properties = subscribe_storage->user_properties.properties.data; |
| 2948 |
|
| 2949 |
return AWS_OP_SUCCESS; |
| 2950 |
} |
| 2951 |
|
| 2952 |
int aws_mqtt5_packet_subscribe_storage_init_from_external_storage( |
| 2953 |
struct aws_mqtt5_packet_subscribe_storage *subscribe_storage, |
| 2954 |
struct aws_allocator *allocator) { |
| 2955 |
AWS_ZERO_STRUCT(*subscribe_storage); |
| 2956 |
|
| 2957 |
if (aws_mqtt5_user_property_set_init(&subscribe_storage->user_properties, allocator)) { |
| 2958 |
return AWS_OP_ERR; |
| 2959 |
} |
| 2960 |
|
| 2961 |
if (aws_array_list_init_dynamic( |
| 2962 |
&subscribe_storage->subscriptions, allocator, 0, sizeof(struct aws_mqtt5_subscription_view))) { |
| 2963 |
return AWS_OP_ERR; |
| 2964 |
} |
| 2965 |
|
| 2966 |
return AWS_OP_SUCCESS; |
| 2967 |
} |
| 2968 |
|
| 2969 |
static void s_aws_mqtt5_operation_subscribe_complete( |
| 2970 |
struct aws_mqtt5_operation *operation, |
| 2971 |
int error_code, |
| 2972 |
enum aws_mqtt5_packet_type packet_type, |
| 2973 |
const void *completion_view) { |
| 2974 |
(void)packet_type; |
| 2975 |
|
| 2976 |
struct aws_mqtt5_operation_subscribe *subscribe_op = operation->impl; |
| 2977 |
|
| 2978 |
if (subscribe_op->completion_options.completion_callback != NULL) { |
| 2979 |
(*subscribe_op->completion_options.completion_callback)( |
| 2980 |
completion_view, error_code, subscribe_op->completion_options.completion_user_data); |
| 2981 |
} |
| 2982 |
} |
| 2983 |
|
| 2984 |
static void s_aws_mqtt5_operation_subscribe_set_packet_id( |
| 2985 |
struct aws_mqtt5_operation *operation, |
| 2986 |
aws_mqtt5_packet_id_t packet_id) { |
| 2987 |
struct aws_mqtt5_operation_subscribe *subscribe_op = operation->impl; |
| 2988 |
subscribe_op->options_storage.storage_view.packet_id = packet_id; |
| 2989 |
} |
| 2990 |
|
| 2991 |
static aws_mqtt5_packet_id_t *s_aws_mqtt5_operation_subscribe_get_packet_id_address( |
| 2992 |
const struct aws_mqtt5_operation *operation) { |
| 2993 |
struct aws_mqtt5_operation_subscribe *subscribe_op = operation->impl; |
| 2994 |
return &subscribe_op->options_storage.storage_view.packet_id; |
| 2995 |
} |
| 2996 |
|
| 2997 |
static uint32_t s_aws_mqtt5_operation_subscribe_get_ack_timeout_override(const struct aws_mqtt5_operation *operation) { |
| 2998 |
struct aws_mqtt5_operation_subscribe *subscribe_op = operation->impl; |
| 2999 |
return subscribe_op->completion_options.ack_timeout_seconds_override; |
| 3000 |
} |
| 3001 |
|
| 3002 |
static struct aws_mqtt5_operation_vtable s_subscribe_operation_vtable = { |
| 3003 |
.aws_mqtt5_operation_completion_fn = s_aws_mqtt5_operation_subscribe_complete, |
| 3004 |
.aws_mqtt5_operation_set_packet_id_fn = s_aws_mqtt5_operation_subscribe_set_packet_id, |
| 3005 |
.aws_mqtt5_operation_get_packet_id_address_fn = s_aws_mqtt5_operation_subscribe_get_packet_id_address, |
| 3006 |
.aws_mqtt5_operation_validate_vs_connection_settings_fn = NULL, |
| 3007 |
.aws_mqtt5_operation_get_ack_timeout_override_fn = s_aws_mqtt5_operation_subscribe_get_ack_timeout_override, |
| 3008 |
}; |
| 3009 |
|
| 3010 |
static void s_destroy_operation_subscribe(void *object) { |
| 3011 |
if (object == NULL) { |
| 3012 |
return; |
| 3013 |
} |
| 3014 |
|
| 3015 |
struct aws_mqtt5_operation_subscribe *subscribe_op = object; |
| 3016 |
|
| 3017 |
aws_mqtt5_packet_subscribe_storage_clean_up(&subscribe_op->options_storage); |
| 3018 |
|
| 3019 |
aws_mem_release(subscribe_op->allocator, subscribe_op); |
| 3020 |
} |
| 3021 |
|
| 3022 |
struct aws_mqtt5_operation_subscribe *aws_mqtt5_operation_subscribe_new( |
| 3023 |
struct aws_allocator *allocator, |
| 3024 |
const struct aws_mqtt5_client *client, |
| 3025 |
const struct aws_mqtt5_packet_subscribe_view *subscribe_options, |
| 3026 |
const struct aws_mqtt5_subscribe_completion_options *completion_options) { |
| 3027 |
(void)client; |
| 3028 |
AWS_PRECONDITION(allocator != NULL); |
| 3029 |
AWS_PRECONDITION(subscribe_options != NULL); |
| 3030 |
|
| 3031 |
if (aws_mqtt5_packet_subscribe_view_validate(subscribe_options)) { |
| 3032 |
return NULL; |
| 3033 |
} |
| 3034 |
|
| 3035 |
if (subscribe_options->packet_id != 0) { |
| 3036 |
AWS_LOGF_DEBUG( |
| 3037 |
AWS_LS_MQTT5_GENERAL, |
| 3038 |
"id=%p: aws_mqtt5_packet_subscribe_view packet id must be zero", |
| 3039 |
(void *)subscribe_options); |
| 3040 |
aws_raise_error(AWS_ERROR_MQTT5_SUBSCRIBE_OPTIONS_VALIDATION); |
| 3041 |
return NULL; |
| 3042 |
} |
| 3043 |
|
| 3044 |
struct aws_mqtt5_operation_subscribe *subscribe_op = |
| 3045 |
aws_mem_calloc(allocator, 1, sizeof(struct aws_mqtt5_operation_subscribe)); |
| 3046 |
if (subscribe_op == NULL) { |
| 3047 |
return NULL; |
| 3048 |
} |
| 3049 |
|
| 3050 |
subscribe_op->allocator = allocator; |
| 3051 |
subscribe_op->base.vtable = &s_subscribe_operation_vtable; |
| 3052 |
subscribe_op->base.packet_type = AWS_MQTT5_PT_SUBSCRIBE; |
| 3053 |
aws_ref_count_init(&subscribe_op->base.ref_count, subscribe_op, s_destroy_operation_subscribe); |
| 3054 |
aws_priority_queue_node_init(&subscribe_op->base.priority_queue_node); |
| 3055 |
subscribe_op->base.impl = subscribe_op; |
| 3056 |
|
| 3057 |
if (aws_mqtt5_packet_subscribe_storage_init(&subscribe_op->options_storage, allocator, subscribe_options)) { |
| 3058 |
goto error; |
| 3059 |
} |
| 3060 |
|
| 3061 |
subscribe_op->base.packet_view = &subscribe_op->options_storage.storage_view; |
| 3062 |
|
| 3063 |
if (completion_options != NULL) { |
| 3064 |
subscribe_op->completion_options = *completion_options; |
| 3065 |
} |
| 3066 |
|
| 3067 |
return subscribe_op; |
| 3068 |
|
| 3069 |
error: |
| 3070 |
|
| 3071 |
aws_mqtt5_operation_release(&subscribe_op->base); |
| 3072 |
|
| 3073 |
return NULL; |
| 3074 |
} |
| 3075 |
|
| 3076 |
|
| 3077 |
|
| 3078 |
|
| 3079 |
|
| 3080 |
static size_t s_aws_mqtt5_packet_suback_compute_storage_size(const struct aws_mqtt5_packet_suback_view *suback_view) { |
| 3081 |
size_t storage_size = s_aws_mqtt5_user_property_set_compute_storage_size( |
| 3082 |
suback_view->user_properties, suback_view->user_property_count); |
| 3083 |
|
| 3084 |
if (suback_view->reason_string != NULL) { |
| 3085 |
storage_size += suback_view->reason_string->len; |
| 3086 |
} |
| 3087 |
|
| 3088 |
return storage_size; |
| 3089 |
} |
| 3090 |
|
| 3091 |
static int s_aws_mqtt5_packet_suback_storage_init_reason_codes( |
| 3092 |
struct aws_mqtt5_packet_suback_storage *suback_storage, |
| 3093 |
struct aws_allocator *allocator, |
| 3094 |
size_t reason_code_count, |
| 3095 |
const enum aws_mqtt5_suback_reason_code *reason_codes) { |
| 3096 |
|
| 3097 |
if (aws_array_list_init_dynamic( |
| 3098 |
&suback_storage->reason_codes, allocator, reason_code_count, sizeof(enum aws_mqtt5_suback_reason_code))) { |
| 3099 |
return AWS_OP_ERR; |
| 3100 |
} |
| 3101 |
|
| 3102 |
for (size_t i = 0; i < reason_code_count; ++i) { |
| 3103 |
aws_array_list_push_back(&suback_storage->reason_codes, &reason_codes[i]); |
| 3104 |
} |
| 3105 |
|
| 3106 |
return AWS_OP_SUCCESS; |
| 3107 |
} |
| 3108 |
|
| 3109 |
AWS_MQTT_API int aws_mqtt5_packet_suback_storage_init( |
| 3110 |
struct aws_mqtt5_packet_suback_storage *suback_storage, |
| 3111 |
struct aws_allocator *allocator, |
| 3112 |
const struct aws_mqtt5_packet_suback_view *suback_view) { |
| 3113 |
AWS_ZERO_STRUCT(*suback_storage); |
| 3114 |
size_t storage_capacity = s_aws_mqtt5_packet_suback_compute_storage_size(suback_view); |
| 3115 |
if (aws_byte_buf_init(&suback_storage->storage, allocator, storage_capacity)) { |
| 3116 |
return AWS_OP_ERR; |
| 3117 |
} |
| 3118 |
|
| 3119 |
struct aws_mqtt5_packet_suback_view *storage_view = &suback_storage->storage_view; |
| 3120 |
|
| 3121 |
storage_view->packet_id = suback_view->packet_id; |
| 3122 |
|
| 3123 |
if (suback_view->reason_string != NULL) { |
| 3124 |
suback_storage->reason_string = *suback_view->reason_string; |
| 3125 |
if (aws_byte_buf_append_and_update(&suback_storage->storage, &suback_storage->reason_string)) { |
| 3126 |
return AWS_OP_ERR; |
| 3127 |
} |
| 3128 |
|
| 3129 |
storage_view->reason_string = &suback_storage->reason_string; |
| 3130 |
} |
| 3131 |
|
| 3132 |
if (s_aws_mqtt5_packet_suback_storage_init_reason_codes( |
| 3133 |
suback_storage, allocator, suback_view->reason_code_count, suback_view->reason_codes)) { |
| 3134 |
return AWS_OP_ERR; |
| 3135 |
} |
| 3136 |
storage_view->reason_code_count = aws_array_list_length(&suback_storage->reason_codes); |
| 3137 |
storage_view->reason_codes = suback_storage->reason_codes.data; |
| 3138 |
|
| 3139 |
if (aws_mqtt5_user_property_set_init_with_storage( |
| 3140 |
&suback_storage->user_properties, |
| 3141 |
allocator, |
| 3142 |
&suback_storage->storage, |
| 3143 |
suback_view->user_property_count, |
| 3144 |
suback_view->user_properties)) { |
| 3145 |
return AWS_OP_ERR; |
| 3146 |
} |
| 3147 |
storage_view->user_property_count = aws_mqtt5_user_property_set_size(&suback_storage->user_properties); |
| 3148 |
storage_view->user_properties = suback_storage->user_properties.properties.data; |
| 3149 |
|
| 3150 |
return AWS_OP_SUCCESS; |
| 3151 |
} |
| 3152 |
|
| 3153 |
int aws_mqtt5_packet_suback_storage_init_from_external_storage( |
| 3154 |
struct aws_mqtt5_packet_suback_storage *suback_storage, |
| 3155 |
struct aws_allocator *allocator) { |
| 3156 |
AWS_ZERO_STRUCT(*suback_storage); |
| 3157 |
|
| 3158 |
if (aws_mqtt5_user_property_set_init(&suback_storage->user_properties, allocator)) { |
| 3159 |
return AWS_OP_ERR; |
| 3160 |
} |
| 3161 |
|
| 3162 |
if (aws_array_list_init_dynamic( |
| 3163 |
&suback_storage->reason_codes, allocator, 0, sizeof(enum aws_mqtt5_suback_reason_code))) { |
| 3164 |
return AWS_OP_ERR; |
| 3165 |
} |
| 3166 |
|
| 3167 |
return AWS_OP_SUCCESS; |
| 3168 |
} |
| 3169 |
|
| 3170 |
void aws_mqtt5_packet_suback_storage_clean_up(struct aws_mqtt5_packet_suback_storage *suback_storage) { |
| 3171 |
if (suback_storage == NULL) { |
| 3172 |
return; |
| 3173 |
} |
| 3174 |
aws_mqtt5_user_property_set_clean_up(&suback_storage->user_properties); |
| 3175 |
|
| 3176 |
aws_array_list_clean_up(&suback_storage->reason_codes); |
| 3177 |
|
| 3178 |
aws_byte_buf_clean_up(&suback_storage->storage); |
| 3179 |
} |
| 3180 |
|
| 3181 |
void aws_mqtt5_packet_suback_view_log( |
| 3182 |
const struct aws_mqtt5_packet_suback_view *suback_view, |
| 3183 |
enum aws_log_level level) { |
| 3184 |
struct aws_logger *log_handle = aws_logger_get_conditional(AWS_LS_MQTT5_GENERAL, level); |
| 3185 |
if (log_handle == NULL) { |
| 3186 |
return; |
| 3187 |
} |
| 3188 |
|
| 3189 |
AWS_LOGUF( |
| 3190 |
log_handle, |
| 3191 |
level, |
| 3192 |
AWS_LS_MQTT5_GENERAL, |
| 3193 |
"id=%p: aws_mqtt5_packet_suback_view packet id set to %d", |
| 3194 |
(void *)suback_view, |
| 3195 |
(int)suback_view->packet_id); |
| 3196 |
|
| 3197 |
for (size_t i = 0; i < suback_view->reason_code_count; ++i) { |
| 3198 |
enum aws_mqtt5_suback_reason_code reason_code = suback_view->reason_codes[i]; |
| 3199 |
AWS_LOGUF( |
| 3200 |
log_handle, |
| 3201 |
level, |
| 3202 |
AWS_LS_MQTT5_GENERAL, |
| 3203 |
"id=%p: aws_mqtt5_packet_suback_view topic #%zu, reason code %d (%s)", |
| 3204 |
(void *)suback_view, |
| 3205 |
i, |
| 3206 |
(int)reason_code, |
| 3207 |
aws_mqtt5_suback_reason_code_to_c_string(reason_code)); |
| 3208 |
} |
| 3209 |
|
| 3210 |
s_aws_mqtt5_user_property_set_log( |
| 3211 |
log_handle, |
| 3212 |
suback_view->user_properties, |
| 3213 |
suback_view->user_property_count, |
| 3214 |
(void *)suback_view, |
| 3215 |
level, |
| 3216 |
"aws_mqtt5_packet_suback_view"); |
| 3217 |
} |
| 3218 |
|
| 3219 |
|
| 3220 |
|
| 3221 |
|
| 3222 |
|
| 3223 |
static size_t s_aws_mqtt5_packet_unsuback_compute_storage_size( |
| 3224 |
const struct aws_mqtt5_packet_unsuback_view *unsuback_view) { |
| 3225 |
size_t storage_size = s_aws_mqtt5_user_property_set_compute_storage_size( |
| 3226 |
unsuback_view->user_properties, unsuback_view->user_property_count); |
| 3227 |
|
| 3228 |
if (unsuback_view->reason_string != NULL) { |
| 3229 |
storage_size += unsuback_view->reason_string->len; |
| 3230 |
} |
| 3231 |
|
| 3232 |
return storage_size; |
| 3233 |
} |
| 3234 |
|
| 3235 |
static int s_aws_mqtt5_packet_unsuback_storage_init_reason_codes( |
| 3236 |
struct aws_mqtt5_packet_unsuback_storage *unsuback_storage, |
| 3237 |
struct aws_allocator *allocator, |
| 3238 |
size_t reason_code_count, |
| 3239 |
const enum aws_mqtt5_unsuback_reason_code *reason_codes) { |
| 3240 |
|
| 3241 |
if (aws_array_list_init_dynamic( |
| 3242 |
&unsuback_storage->reason_codes, |
| 3243 |
allocator, |
| 3244 |
reason_code_count, |
| 3245 |
sizeof(enum aws_mqtt5_unsuback_reason_code))) { |
| 3246 |
return AWS_OP_ERR; |
| 3247 |
} |
| 3248 |
|
| 3249 |
for (size_t i = 0; i < reason_code_count; ++i) { |
| 3250 |
aws_array_list_push_back(&unsuback_storage->reason_codes, &reason_codes[i]); |
| 3251 |
} |
| 3252 |
|
| 3253 |
return AWS_OP_SUCCESS; |
| 3254 |
} |
| 3255 |
|
| 3256 |
AWS_MQTT_API int aws_mqtt5_packet_unsuback_storage_init( |
| 3257 |
struct aws_mqtt5_packet_unsuback_storage *unsuback_storage, |
| 3258 |
struct aws_allocator *allocator, |
| 3259 |
const struct aws_mqtt5_packet_unsuback_view *unsuback_view) { |
| 3260 |
AWS_ZERO_STRUCT(*unsuback_storage); |
| 3261 |
size_t storage_capacity = s_aws_mqtt5_packet_unsuback_compute_storage_size(unsuback_view); |
| 3262 |
if (aws_byte_buf_init(&unsuback_storage->storage, allocator, storage_capacity)) { |
| 3263 |
return AWS_OP_ERR; |
| 3264 |
} |
| 3265 |
|
| 3266 |
struct aws_mqtt5_packet_unsuback_view *storage_view = &unsuback_storage->storage_view; |
| 3267 |
|
| 3268 |
storage_view->packet_id = unsuback_view->packet_id; |
| 3269 |
|
| 3270 |
if (unsuback_view->reason_string != NULL) { |
| 3271 |
unsuback_storage->reason_string = *unsuback_view->reason_string; |
| 3272 |
if (aws_byte_buf_append_and_update(&unsuback_storage->storage, &unsuback_storage->reason_string)) { |
| 3273 |
return AWS_OP_ERR; |
| 3274 |
} |
| 3275 |
|
| 3276 |
storage_view->reason_string = &unsuback_storage->reason_string; |
| 3277 |
} |
| 3278 |
|
| 3279 |
if (s_aws_mqtt5_packet_unsuback_storage_init_reason_codes( |
| 3280 |
unsuback_storage, allocator, unsuback_view->reason_code_count, unsuback_view->reason_codes)) { |
| 3281 |
return AWS_OP_ERR; |
| 3282 |
} |
| 3283 |
storage_view->reason_code_count = aws_array_list_length(&unsuback_storage->reason_codes); |
| 3284 |
storage_view->reason_codes = unsuback_storage->reason_codes.data; |
| 3285 |
|
| 3286 |
if (aws_mqtt5_user_property_set_init_with_storage( |
| 3287 |
&unsuback_storage->user_properties, |
| 3288 |
allocator, |
| 3289 |
&unsuback_storage->storage, |
| 3290 |
unsuback_view->user_property_count, |
| 3291 |
unsuback_view->user_properties)) { |
| 3292 |
return AWS_OP_ERR; |
| 3293 |
} |
| 3294 |
storage_view->user_property_count = aws_mqtt5_user_property_set_size(&unsuback_storage->user_properties); |
| 3295 |
storage_view->user_properties = unsuback_storage->user_properties.properties.data; |
| 3296 |
|
| 3297 |
return AWS_OP_SUCCESS; |
| 3298 |
} |
| 3299 |
|
| 3300 |
int aws_mqtt5_packet_unsuback_storage_init_from_external_storage( |
| 3301 |
struct aws_mqtt5_packet_unsuback_storage *unsuback_storage, |
| 3302 |
struct aws_allocator *allocator) { |
| 3303 |
AWS_ZERO_STRUCT(*unsuback_storage); |
| 3304 |
|
| 3305 |
if (aws_mqtt5_user_property_set_init(&unsuback_storage->user_properties, allocator)) { |
| 3306 |
return AWS_OP_ERR; |
| 3307 |
} |
| 3308 |
|
| 3309 |
if (aws_array_list_init_dynamic( |
| 3310 |
&unsuback_storage->reason_codes, allocator, 0, sizeof(enum aws_mqtt5_unsuback_reason_code))) { |
| 3311 |
return AWS_OP_ERR; |
| 3312 |
} |
| 3313 |
|
| 3314 |
return AWS_OP_SUCCESS; |
| 3315 |
} |
| 3316 |
|
| 3317 |
void aws_mqtt5_packet_unsuback_storage_clean_up(struct aws_mqtt5_packet_unsuback_storage *unsuback_storage) { |
| 3318 |
if (unsuback_storage == NULL) { |
| 3319 |
return; |
| 3320 |
} |
| 3321 |
aws_mqtt5_user_property_set_clean_up(&unsuback_storage->user_properties); |
| 3322 |
|
| 3323 |
aws_array_list_clean_up(&unsuback_storage->reason_codes); |
| 3324 |
|
| 3325 |
aws_byte_buf_clean_up(&unsuback_storage->storage); |
| 3326 |
} |
| 3327 |
|
| 3328 |
void aws_mqtt5_packet_unsuback_view_log( |
| 3329 |
const struct aws_mqtt5_packet_unsuback_view *unsuback_view, |
| 3330 |
enum aws_log_level level) { |
| 3331 |
struct aws_logger *log_handle = aws_logger_get_conditional(AWS_LS_MQTT5_GENERAL, level); |
| 3332 |
if (log_handle == NULL) { |
| 3333 |
return; |
| 3334 |
} |
| 3335 |
|
| 3336 |
AWS_LOGUF( |
| 3337 |
log_handle, |
| 3338 |
level, |
| 3339 |
AWS_LS_MQTT5_GENERAL, |
| 3340 |
"id=%p: aws_mqtt5_packet_unsuback_view packet id set to %d", |
| 3341 |
(void *)unsuback_view, |
| 3342 |
(int)unsuback_view->packet_id); |
| 3343 |
|
| 3344 |
for (size_t i = 0; i < unsuback_view->reason_code_count; ++i) { |
| 3345 |
enum aws_mqtt5_unsuback_reason_code reason_code = unsuback_view->reason_codes[i]; |
| 3346 |
AWS_LOGUF( |
| 3347 |
log_handle, |
| 3348 |
level, |
| 3349 |
AWS_LS_MQTT5_GENERAL, |
| 3350 |
"id=%p: aws_mqtt5_packet_unsuback_view topic #%zu, reason code %d (%s)", |
| 3351 |
(void *)unsuback_view, |
| 3352 |
i, |
| 3353 |
(int)reason_code, |
| 3354 |
aws_mqtt5_unsuback_reason_code_to_c_string(reason_code)); |
| 3355 |
} |
| 3356 |
|
| 3357 |
s_aws_mqtt5_user_property_set_log( |
| 3358 |
log_handle, |
| 3359 |
unsuback_view->user_properties, |
| 3360 |
unsuback_view->user_property_count, |
| 3361 |
(void *)unsuback_view, |
| 3362 |
level, |
| 3363 |
"aws_mqtt5_packet_unsuback_view"); |
| 3364 |
} |
| 3365 |
|
| 3366 |
|
| 3367 |
|
| 3368 |
|
| 3369 |
|
| 3370 |
static void s_destroy_operation_pingreq(void *object) { |
| 3371 |
if (object == NULL) { |
| 3372 |
return; |
| 3373 |
} |
| 3374 |
|
| 3375 |
struct aws_mqtt5_operation_pingreq *pingreq_op = object; |
| 3376 |
aws_mem_release(pingreq_op->allocator, pingreq_op); |
| 3377 |
} |
| 3378 |
|
| 3379 |
struct aws_mqtt5_operation_pingreq *aws_mqtt5_operation_pingreq_new(struct aws_allocator *allocator) { |
| 3380 |
struct aws_mqtt5_operation_pingreq *pingreq_op = |
| 3381 |
aws_mem_calloc(allocator, 1, sizeof(struct aws_mqtt5_operation_pingreq)); |
| 3382 |
if (pingreq_op == NULL) { |
| 3383 |
return NULL; |
| 3384 |
} |
| 3385 |
|
| 3386 |
pingreq_op->allocator = allocator; |
| 3387 |
pingreq_op->base.vtable = &s_empty_operation_vtable; |
| 3388 |
pingreq_op->base.packet_type = AWS_MQTT5_PT_PINGREQ; |
| 3389 |
aws_ref_count_init(&pingreq_op->base.ref_count, pingreq_op, s_destroy_operation_pingreq); |
| 3390 |
aws_priority_queue_node_init(&pingreq_op->base.priority_queue_node); |
| 3391 |
pingreq_op->base.impl = pingreq_op; |
| 3392 |
|
| 3393 |
return pingreq_op; |
| 3394 |
} |
| 3395 |
|
| 3396 |
|
| 3397 |
|
| 3398 |
|
| 3399 |
|
| 3400 |
int aws_mqtt5_client_options_validate(const struct aws_mqtt5_client_options *options) { |
| 3401 |
if (options == NULL) { |
| 3402 |
AWS_LOGF_ERROR(AWS_LS_MQTT5_GENERAL, "null mqtt5 client configuration options"); |
| 3403 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 3404 |
} |
| 3405 |
|
| 3406 |
if (options->host_name.len == 0) { |
| 3407 |
AWS_LOGF_ERROR(AWS_LS_MQTT5_GENERAL, "host name not set in mqtt5 client configuration"); |
| 3408 |
return aws_raise_error(AWS_ERROR_MQTT5_CLIENT_OPTIONS_VALIDATION); |
| 3409 |
} |
| 3410 |
|
| 3411 |
if (options->bootstrap == NULL) { |
| 3412 |
AWS_LOGF_ERROR(AWS_LS_MQTT5_GENERAL, "client bootstrap not set in mqtt5 client configuration"); |
| 3413 |
return aws_raise_error(AWS_ERROR_MQTT5_CLIENT_OPTIONS_VALIDATION); |
| 3414 |
} |
| 3415 |
|
| 3416 |
|
| 3417 |
if (options->socket_options != NULL) { |
| 3418 |
if (options->socket_options->type == AWS_SOCKET_DGRAM || options->socket_options->connect_timeout_ms == 0) { |
| 3419 |
AWS_LOGF_ERROR(AWS_LS_MQTT5_GENERAL, "invalid socket options in mqtt5 client configuration"); |
| 3420 |
return aws_raise_error(AWS_ERROR_MQTT5_CLIENT_OPTIONS_VALIDATION); |
| 3421 |
} |
| 3422 |
} |
| 3423 |
|
| 3424 |
if (aws_socket_validate_port_for_connect( |
| 3425 |
options->port, options->socket_options ? options->socket_options->domain : AWS_SOCKET_IPV4)) { |
| 3426 |
AWS_LOGF_ERROR(AWS_LS_MQTT5_GENERAL, "invalid port in mqtt5 client configuration"); |
| 3427 |
return aws_raise_error(AWS_ERROR_MQTT5_CLIENT_OPTIONS_VALIDATION); |
| 3428 |
} |
| 3429 |
|
| 3430 |
if (options->http_proxy_options != NULL) { |
| 3431 |
if (options->http_proxy_options->host.len == 0) { |
| 3432 |
AWS_LOGF_ERROR(AWS_LS_MQTT5_GENERAL, "proxy host name not set in mqtt5 client configuration"); |
| 3433 |
return aws_raise_error(AWS_ERROR_MQTT5_CLIENT_OPTIONS_VALIDATION); |
| 3434 |
} |
| 3435 |
|
| 3436 |
if (aws_socket_validate_port_for_connect(options->http_proxy_options->port, AWS_SOCKET_IPV4)) { |
| 3437 |
AWS_LOGF_ERROR(AWS_LS_MQTT5_GENERAL, "invalid proxy port in mqtt5 client configuration"); |
| 3438 |
return aws_raise_error(AWS_ERROR_MQTT5_CLIENT_OPTIONS_VALIDATION); |
| 3439 |
} |
| 3440 |
} |
| 3441 |
|
| 3442 |
|
| 3443 |
if (options->lifecycle_event_handler == NULL) { |
| 3444 |
AWS_LOGF_ERROR(AWS_LS_MQTT5_GENERAL, "lifecycle event handler not set in mqtt5 client configuration"); |
| 3445 |
return aws_raise_error(AWS_ERROR_MQTT5_CLIENT_OPTIONS_VALIDATION); |
| 3446 |
} |
| 3447 |
|
| 3448 |
if (options->publish_received_handler == NULL) { |
| 3449 |
AWS_LOGF_ERROR(AWS_LS_MQTT5_GENERAL, "publish received not set in mqtt5 client configuration"); |
| 3450 |
return aws_raise_error(AWS_ERROR_MQTT5_CLIENT_OPTIONS_VALIDATION); |
| 3451 |
} |
| 3452 |
|
| 3453 |
if (aws_mqtt5_packet_connect_view_validate(options->connect_options)) { |
| 3454 |
AWS_LOGF_ERROR(AWS_LS_MQTT5_GENERAL, "invalid CONNECT options in mqtt5 client configuration"); |
| 3455 |
|
| 3456 |
return AWS_OP_ERR; |
| 3457 |
} |
| 3458 |
|
| 3459 |
if (options->topic_aliasing_options != NULL) { |
| 3460 |
if (!aws_mqtt5_outbound_topic_alias_behavior_type_validate( |
| 3461 |
options->topic_aliasing_options->outbound_topic_alias_behavior)) { |
| 3462 |
AWS_LOGF_ERROR(AWS_LS_MQTT5_GENERAL, "invalid outbound topic alias behavior type value"); |
| 3463 |
return aws_raise_error(AWS_ERROR_MQTT5_CLIENT_OPTIONS_VALIDATION); |
| 3464 |
} |
| 3465 |
|
| 3466 |
if (!aws_mqtt5_inbound_topic_alias_behavior_type_validate( |
| 3467 |
options->topic_aliasing_options->inbound_topic_alias_behavior)) { |
| 3468 |
AWS_LOGF_ERROR(AWS_LS_MQTT5_GENERAL, "invalid inbound topic alias behavior type value"); |
| 3469 |
return aws_raise_error(AWS_ERROR_MQTT5_CLIENT_OPTIONS_VALIDATION); |
| 3470 |
} |
| 3471 |
} |
| 3472 |
|
| 3473 |
if (options->metrics != NULL) { |
| 3474 |
if (aws_mqtt_validate_iot_metrics(options->metrics)) { |
| 3475 |
AWS_LOGF_ERROR(AWS_LS_MQTT5_GENERAL, "invalid metrics in mqtt5 client configuration"); |
| 3476 |
return aws_raise_error(AWS_ERROR_MQTT5_CLIENT_OPTIONS_VALIDATION); |
| 3477 |
} |
| 3478 |
} |
| 3479 |
|
| 3480 |
return AWS_OP_SUCCESS; |
| 3481 |
} |
| 3482 |
|
| 3483 |
static void s_log_tls_connection_options( |
| 3484 |
struct aws_logger *log_handle, |
| 3485 |
const struct aws_mqtt5_client_options_storage *options_storage, |
| 3486 |
const struct aws_tls_connection_options *tls_options, |
| 3487 |
enum aws_log_level level, |
| 3488 |
const char *log_text) { |
| 3489 |
AWS_LOGUF( |
| 3490 |
log_handle, |
| 3491 |
level, |
| 3492 |
AWS_LS_MQTT5_GENERAL, |
| 3493 |
"id=%p: aws_mqtt5_client_options_storage %s tls options set:", |
| 3494 |
(void *)options_storage, |
| 3495 |
log_text); |
| 3496 |
|
| 3497 |
if (tls_options->advertise_alpn_message && tls_options->alpn_list) { |
| 3498 |
AWS_LOGUF( |
| 3499 |
log_handle, |
| 3500 |
level, |
| 3501 |
AWS_LS_MQTT5_GENERAL, |
| 3502 |
"id=%p: aws_mqtt5_client_options_storage %s tls options alpn protocol list set to \"%s\"", |
| 3503 |
(void *)options_storage, |
| 3504 |
log_text, |
| 3505 |
aws_string_c_str(tls_options->alpn_list)); |
| 3506 |
} else { |
| 3507 |
AWS_LOGUF( |
| 3508 |
log_handle, |
| 3509 |
level, |
| 3510 |
AWS_LS_MQTT5_GENERAL, |
| 3511 |
"id=%p: aws_mqtt5_client_options_storage %s tls options alpn not used", |
| 3512 |
(void *)options_storage, |
| 3513 |
log_text); |
| 3514 |
} |
| 3515 |
|
| 3516 |
if (tls_options->server_name) { |
| 3517 |
AWS_LOGUF( |
| 3518 |
log_handle, |
| 3519 |
level, |
| 3520 |
AWS_LS_MQTT5_GENERAL, |
| 3521 |
"id=%p: aws_mqtt5_client_options_storage %s tls options SNI value set to \"%s\"", |
| 3522 |
(void *)options_storage, |
| 3523 |
log_text, |
| 3524 |
aws_string_c_str(tls_options->server_name)); |
| 3525 |
} else { |
| 3526 |
AWS_LOGUF( |
| 3527 |
log_handle, |
| 3528 |
level, |
| 3529 |
AWS_LS_MQTT5_GENERAL, |
| 3530 |
"id=%p: aws_mqtt5_client_options_storage %s tls options SNI not used", |
| 3531 |
(void *)options_storage, |
| 3532 |
log_text); |
| 3533 |
} |
| 3534 |
|
| 3535 |
AWS_LOGUF( |
| 3536 |
log_handle, |
| 3537 |
level, |
| 3538 |
AWS_LS_MQTT5_GENERAL, |
| 3539 |
"id=%p: aws_mqtt5_client_options_storage %s tls options tls context set to (%p)", |
| 3540 |
(void *)options_storage, |
| 3541 |
log_text, |
| 3542 |
(void *)(tls_options->ctx)); |
| 3543 |
|
| 3544 |
AWS_LOGUF( |
| 3545 |
log_handle, |
| 3546 |
level, |
| 3547 |
AWS_LS_MQTT5_GENERAL, |
| 3548 |
"id=%p: aws_mqtt5_client_options_storage %s tls options handshake timeout set to %" PRIu32, |
| 3549 |
(void *)options_storage, |
| 3550 |
log_text, |
| 3551 |
tls_options->timeout_ms); |
| 3552 |
} |
| 3553 |
|
| 3554 |
static void s_log_topic_aliasing_options( |
| 3555 |
struct aws_logger *log_handle, |
| 3556 |
const struct aws_mqtt5_client_options_storage *options_storage, |
| 3557 |
const struct aws_mqtt5_client_topic_alias_options *topic_aliasing_options, |
| 3558 |
enum aws_log_level level) { |
| 3559 |
|
| 3560 |
AWS_LOGUF( |
| 3561 |
log_handle, |
| 3562 |
level, |
| 3563 |
AWS_LS_MQTT5_GENERAL, |
| 3564 |
"id=%p: aws_mqtt5_client_options_storage outbound topic aliasing behavior set to %d (%s)", |
| 3565 |
(void *)options_storage, |
| 3566 |
(int)topic_aliasing_options->outbound_topic_alias_behavior, |
| 3567 |
aws_mqtt5_outbound_topic_alias_behavior_type_to_c_string( |
| 3568 |
topic_aliasing_options->outbound_topic_alias_behavior)); |
| 3569 |
|
| 3570 |
AWS_LOGUF( |
| 3571 |
log_handle, |
| 3572 |
level, |
| 3573 |
AWS_LS_MQTT5_GENERAL, |
| 3574 |
"id=%p: aws_mqtt5_client_options_storage maximum outbound topic alias cache size set to %" PRIu16, |
| 3575 |
(void *)options_storage, |
| 3576 |
topic_aliasing_options->outbound_alias_cache_max_size); |
| 3577 |
|
| 3578 |
AWS_LOGUF( |
| 3579 |
log_handle, |
| 3580 |
level, |
| 3581 |
AWS_LS_MQTT5_GENERAL, |
| 3582 |
"id=%p: aws_mqtt5_client_options_storage inbound topic aliasing behavior set to %d (%s)", |
| 3583 |
(void *)options_storage, |
| 3584 |
(int)topic_aliasing_options->inbound_topic_alias_behavior, |
| 3585 |
aws_mqtt5_inbound_topic_alias_behavior_type_to_c_string(topic_aliasing_options->inbound_topic_alias_behavior)); |
| 3586 |
|
| 3587 |
AWS_LOGUF( |
| 3588 |
log_handle, |
| 3589 |
level, |
| 3590 |
AWS_LS_MQTT5_GENERAL, |
| 3591 |
"id=%p: aws_mqtt5_client_options_storage inbound topic alias cache size set to %" PRIu16, |
| 3592 |
(void *)options_storage, |
| 3593 |
topic_aliasing_options->inbound_alias_cache_size); |
| 3594 |
} |
| 3595 |
|
| 3596 |
void aws_mqtt5_client_options_storage_log( |
| 3597 |
const struct aws_mqtt5_client_options_storage *options_storage, |
| 3598 |
enum aws_log_level level) { |
| 3599 |
struct aws_logger *log_handle = aws_logger_get_conditional(AWS_LS_MQTT5_GENERAL, level); |
| 3600 |
if (log_handle == NULL) { |
| 3601 |
return; |
| 3602 |
} |
| 3603 |
|
| 3604 |
AWS_LOGUF( |
| 3605 |
log_handle, |
| 3606 |
level, |
| 3607 |
AWS_LS_MQTT5_GENERAL, |
| 3608 |
"id=%p: aws_mqtt5_client_options_storage host name set to %s", |
| 3609 |
(void *)options_storage, |
| 3610 |
aws_string_c_str(options_storage->host_name)); |
| 3611 |
|
| 3612 |
AWS_LOGUF( |
| 3613 |
log_handle, |
| 3614 |
level, |
| 3615 |
AWS_LS_MQTT5_GENERAL, |
| 3616 |
"id=%p: aws_mqtt5_client_options_storage port set to %" PRIu32, |
| 3617 |
(void *)options_storage, |
| 3618 |
options_storage->port); |
| 3619 |
|
| 3620 |
AWS_LOGUF( |
| 3621 |
log_handle, |
| 3622 |
level, |
| 3623 |
AWS_LS_MQTT5_GENERAL, |
| 3624 |
"id=%p: aws_mqtt5_client_options_storage client bootstrap set to (%p)", |
| 3625 |
(void *)options_storage, |
| 3626 |
(void *)options_storage->bootstrap); |
| 3627 |
|
| 3628 |
AWS_LOGUF( |
| 3629 |
log_handle, |
| 3630 |
level, |
| 3631 |
AWS_LS_MQTT5_GENERAL, |
| 3632 |
"id=%p: aws_mqtt5_client_options_storage socket options set to: type = %d, domain = %d, connect_timeout_ms = " |
| 3633 |
"%" PRIu32, |
| 3634 |
(void *)options_storage, |
| 3635 |
(int)options_storage->socket_options.type, |
| 3636 |
(int)options_storage->socket_options.domain, |
| 3637 |
options_storage->socket_options.connect_timeout_ms); |
| 3638 |
|
| 3639 |
if (options_storage->socket_options.keepalive) { |
| 3640 |
AWS_LOGUF( |
| 3641 |
log_handle, |
| 3642 |
level, |
| 3643 |
AWS_LS_MQTT5_GENERAL, |
| 3644 |
"id=%p: aws_mqtt5_client_options_storage socket keepalive options set to: keep_alive_interval_sec = " |
| 3645 |
"%" PRIu16 ", " |
| 3646 |
"keep_alive_timeout_sec = %" PRIu16 ", keep_alive_max_failed_probes = %" PRIu16, |
| 3647 |
(void *)options_storage, |
| 3648 |
options_storage->socket_options.keep_alive_interval_sec, |
| 3649 |
options_storage->socket_options.keep_alive_timeout_sec, |
| 3650 |
options_storage->socket_options.keep_alive_max_failed_probes); |
| 3651 |
} |
| 3652 |
|
| 3653 |
if (options_storage->tls_options_ptr != NULL) { |
| 3654 |
s_log_tls_connection_options(log_handle, options_storage, options_storage->tls_options_ptr, level, ""); |
| 3655 |
} |
| 3656 |
|
| 3657 |
if (options_storage->http_proxy_config != NULL) { |
| 3658 |
AWS_LOGUF( |
| 3659 |
log_handle, |
| 3660 |
level, |
| 3661 |
AWS_LS_MQTT5_GENERAL, |
| 3662 |
"id=%p: aws_mqtt5_client_options_storage using http proxy:", |
| 3663 |
(void *)options_storage); |
| 3664 |
|
| 3665 |
AWS_LOGUF( |
| 3666 |
log_handle, |
| 3667 |
level, |
| 3668 |
AWS_LS_MQTT5_GENERAL, |
| 3669 |
"id=%p: aws_mqtt5_client_options_storage http proxy host name set to " PRInSTR, |
| 3670 |
(void *)options_storage, |
| 3671 |
AWS_BYTE_CURSOR_PRI(options_storage->http_proxy_options.host)); |
| 3672 |
|
| 3673 |
AWS_LOGUF( |
| 3674 |
log_handle, |
| 3675 |
level, |
| 3676 |
AWS_LS_MQTT5_GENERAL, |
| 3677 |
"id=%p: aws_mqtt5_client_options_storage http proxy port set to %" PRIu32, |
| 3678 |
(void *)options_storage, |
| 3679 |
options_storage->http_proxy_options.port); |
| 3680 |
|
| 3681 |
if (options_storage->http_proxy_options.tls_options != NULL) { |
| 3682 |
s_log_tls_connection_options( |
| 3683 |
log_handle, options_storage, options_storage->tls_options_ptr, level, "http proxy"); |
| 3684 |
} |
| 3685 |
|
| 3686 |
|
| 3687 |
if (options_storage->http_proxy_options.proxy_strategy != NULL) { |
| 3688 |
AWS_LOGUF( |
| 3689 |
log_handle, |
| 3690 |
level, |
| 3691 |
AWS_LS_MQTT5_GENERAL, |
| 3692 |
"id=%p: aws_mqtt5_client_options_storage http proxy strategy set to (%p)", |
| 3693 |
(void *)options_storage, |
| 3694 |
(void *)options_storage->http_proxy_options.proxy_strategy); |
| 3695 |
} |
| 3696 |
} |
| 3697 |
|
| 3698 |
if (options_storage->websocket_handshake_transform != NULL) { |
| 3699 |
AWS_LOGUF( |
| 3700 |
log_handle, |
| 3701 |
level, |
| 3702 |
AWS_LS_MQTT5_GENERAL, |
| 3703 |
"id=%p: aws_mqtt5_client_options_storage enabling websockets", |
| 3704 |
(void *)options_storage); |
| 3705 |
|
| 3706 |
AWS_LOGUF( |
| 3707 |
log_handle, |
| 3708 |
level, |
| 3709 |
AWS_LS_MQTT5_GENERAL, |
| 3710 |
"id=%p: aws_mqtt5_client_options_storage websocket handshake transform user data set to (%p)", |
| 3711 |
(void *)options_storage, |
| 3712 |
options_storage->websocket_handshake_transform_user_data); |
| 3713 |
} else { |
| 3714 |
AWS_LOGUF( |
| 3715 |
log_handle, |
| 3716 |
level, |
| 3717 |
AWS_LS_MQTT5_GENERAL, |
| 3718 |
"id=%p: aws_mqtt5_client_options_storage disabling websockets", |
| 3719 |
(void *)options_storage); |
| 3720 |
} |
| 3721 |
|
| 3722 |
AWS_LOGUF( |
| 3723 |
log_handle, |
| 3724 |
level, |
| 3725 |
AWS_LS_MQTT5_GENERAL, |
| 3726 |
"id=%p: aws_mqtt5_client_options_storage session behavior set to %d (%s)", |
| 3727 |
(void *)options_storage, |
| 3728 |
(int)options_storage->session_behavior, |
| 3729 |
aws_mqtt5_client_session_behavior_type_to_c_string(options_storage->session_behavior)); |
| 3730 |
|
| 3731 |
s_log_topic_aliasing_options(log_handle, options_storage, &options_storage->topic_aliasing_options, level); |
| 3732 |
|
| 3733 |
AWS_LOGUF( |
| 3734 |
log_handle, |
| 3735 |
level, |
| 3736 |
AWS_LS_MQTT5_GENERAL, |
| 3737 |
"id=%p: aws_mqtt5_client_options_storage extended validation and flow control options set to %d (%s)", |
| 3738 |
(void *)options_storage, |
| 3739 |
(int)options_storage->extended_validation_and_flow_control_options, |
| 3740 |
aws_mqtt5_extended_validation_and_flow_control_options_to_c_string( |
| 3741 |
options_storage->extended_validation_and_flow_control_options)); |
| 3742 |
|
| 3743 |
AWS_LOGUF( |
| 3744 |
log_handle, |
| 3745 |
level, |
| 3746 |
AWS_LS_MQTT5_GENERAL, |
| 3747 |
"id=%p: aws_mqtt5_client_options_storage operation queue behavior set to %d (%s)", |
| 3748 |
(void *)options_storage, |
| 3749 |
(int)options_storage->offline_queue_behavior, |
| 3750 |
aws_mqtt5_client_operation_queue_behavior_type_to_c_string(options_storage->offline_queue_behavior)); |
| 3751 |
|
| 3752 |
AWS_LOGUF( |
| 3753 |
log_handle, |
| 3754 |
level, |
| 3755 |
AWS_LS_MQTT5_GENERAL, |
| 3756 |
"id=%p: aws_mqtt5_client_options_storage reconnect jitter mode set to %d", |
| 3757 |
(void *)options_storage, |
| 3758 |
(int)options_storage->retry_jitter_mode); |
| 3759 |
|
| 3760 |
AWS_LOGUF( |
| 3761 |
log_handle, |
| 3762 |
level, |
| 3763 |
AWS_LS_MQTT5_GENERAL, |
| 3764 |
"id=%p: aws_mqtt5_client_options_storage reconnect delay min set to %" PRIu64 " ms, max set to %" PRIu64 " ms", |
| 3765 |
(void *)options_storage, |
| 3766 |
options_storage->min_reconnect_delay_ms, |
| 3767 |
options_storage->max_reconnect_delay_ms); |
| 3768 |
|
| 3769 |
AWS_LOGUF( |
| 3770 |
log_handle, |
| 3771 |
level, |
| 3772 |
AWS_LS_MQTT5_GENERAL, |
| 3773 |
"id=%p: aws_mqtt5_client_options_storage minimum necessary connection time in order to reset the reconnect " |
| 3774 |
"delay " |
| 3775 |
"set " |
| 3776 |
"to %" PRIu64 " ms", |
| 3777 |
(void *)options_storage, |
| 3778 |
options_storage->min_connected_time_to_reset_reconnect_delay_ms); |
| 3779 |
|
| 3780 |
AWS_LOGUF( |
| 3781 |
log_handle, |
| 3782 |
level, |
| 3783 |
AWS_LS_MQTT5_GENERAL, |
| 3784 |
"id=%p: aws_mqtt5_client_options_storage ping timeout interval set to %" PRIu32 " ms", |
| 3785 |
(void *)options_storage, |
| 3786 |
options_storage->ping_timeout_ms); |
| 3787 |
|
| 3788 |
AWS_LOGUF( |
| 3789 |
log_handle, |
| 3790 |
level, |
| 3791 |
AWS_LS_MQTT5_GENERAL, |
| 3792 |
"id=%p: aws_mqtt5_client_options_storage connack timeout interval set to %" PRIu32 " ms", |
| 3793 |
(void *)options_storage, |
| 3794 |
options_storage->connack_timeout_ms); |
| 3795 |
|
| 3796 |
AWS_LOGUF( |
| 3797 |
log_handle, |
| 3798 |
level, |
| 3799 |
AWS_LS_MQTT5_GENERAL, |
| 3800 |
"id=%p: aws_mqtt5_client_options_storage QoS 1 packet ack timeout interval set to %" PRIu32 " seconds", |
| 3801 |
(void *)options_storage, |
| 3802 |
options_storage->ack_timeout_seconds); |
| 3803 |
|
| 3804 |
AWS_LOGUF( |
| 3805 |
log_handle, |
| 3806 |
level, |
| 3807 |
AWS_LS_MQTT5_GENERAL, |
| 3808 |
"id=%p: aws_mqtt5_client_options_storage connect options:", |
| 3809 |
(void *)options_storage); |
| 3810 |
|
| 3811 |
aws_mqtt5_packet_connect_view_log(&options_storage->connect->storage_view, level); |
| 3812 |
|
| 3813 |
AWS_LOGUF( |
| 3814 |
log_handle, |
| 3815 |
level, |
| 3816 |
AWS_LS_MQTT5_GENERAL, |
| 3817 |
"id=%p: aws_mqtt5_client_options_storage lifecycle event handler user data set to (%p)", |
| 3818 |
(void *)options_storage, |
| 3819 |
options_storage->lifecycle_event_handler_user_data); |
| 3820 |
} |
| 3821 |
|
| 3822 |
void aws_mqtt5_client_options_storage_destroy(struct aws_mqtt5_client_options_storage *options_storage) { |
| 3823 |
if (options_storage == NULL) { |
| 3824 |
return; |
| 3825 |
} |
| 3826 |
|
| 3827 |
aws_string_destroy(options_storage->host_name); |
| 3828 |
aws_client_bootstrap_release(options_storage->bootstrap); |
| 3829 |
|
| 3830 |
aws_tls_connection_options_clean_up(&options_storage->tls_options); |
| 3831 |
aws_http_proxy_config_destroy(options_storage->http_proxy_config); |
| 3832 |
|
| 3833 |
if (options_storage->connect != NULL) { |
| 3834 |
aws_mqtt5_packet_connect_storage_clean_up(options_storage->connect); |
| 3835 |
aws_mem_release(options_storage->connect->allocator, options_storage->connect); |
| 3836 |
} |
| 3837 |
|
| 3838 |
aws_mqtt_iot_metrics_storage_destroy(options_storage->metrics_storage); |
| 3839 |
aws_mem_release(options_storage->allocator, options_storage); |
| 3840 |
} |
| 3841 |
|
| 3842 |
static void s_apply_zero_valued_defaults_to_client_options_storage( |
| 3843 |
struct aws_mqtt5_client_options_storage *options_storage) { |
| 3844 |
if (options_storage->min_reconnect_delay_ms == 0) { |
| 3845 |
options_storage->min_reconnect_delay_ms = AWS_MQTT5_CLIENT_DEFAULT_MIN_RECONNECT_DELAY_MS; |
| 3846 |
} |
| 3847 |
|
| 3848 |
if (options_storage->max_reconnect_delay_ms == 0) { |
| 3849 |
options_storage->max_reconnect_delay_ms = AWS_MQTT5_CLIENT_DEFAULT_MAX_RECONNECT_DELAY_MS; |
| 3850 |
} |
| 3851 |
|
| 3852 |
if (options_storage->min_connected_time_to_reset_reconnect_delay_ms == 0) { |
| 3853 |
options_storage->min_connected_time_to_reset_reconnect_delay_ms = |
| 3854 |
AWS_MQTT5_CLIENT_DEFAULT_MIN_CONNECTED_TIME_TO_RESET_RECONNECT_DELAY_MS; |
| 3855 |
} |
| 3856 |
|
| 3857 |
if (options_storage->ping_timeout_ms == 0) { |
| 3858 |
options_storage->ping_timeout_ms = AWS_MQTT5_CLIENT_DEFAULT_PING_TIMEOUT_MS; |
| 3859 |
} |
| 3860 |
|
| 3861 |
if (options_storage->connack_timeout_ms == 0) { |
| 3862 |
options_storage->connack_timeout_ms = AWS_MQTT5_CLIENT_DEFAULT_CONNACK_TIMEOUT_MS; |
| 3863 |
} |
| 3864 |
|
| 3865 |
if (options_storage->ack_timeout_seconds == 0) { |
| 3866 |
options_storage->ack_timeout_seconds = AWS_MQTT5_CLIENT_DEFAULT_OPERATION_TIMEOUNT_SECONDS; |
| 3867 |
} |
| 3868 |
|
| 3869 |
if (options_storage->topic_aliasing_options.inbound_alias_cache_size == 0) { |
| 3870 |
options_storage->topic_aliasing_options.inbound_alias_cache_size = |
| 3871 |
AWS_MQTT5_CLIENT_DEFAULT_INBOUND_TOPIC_ALIAS_CACHE_SIZE; |
| 3872 |
} |
| 3873 |
|
| 3874 |
if (options_storage->topic_aliasing_options.outbound_alias_cache_max_size == 0) { |
| 3875 |
options_storage->topic_aliasing_options.outbound_alias_cache_max_size = |
| 3876 |
AWS_MQTT5_CLIENT_DEFAULT_OUTBOUND_TOPIC_ALIAS_CACHE_SIZE; |
| 3877 |
} |
| 3878 |
} |
| 3879 |
|
| 3880 |
struct aws_mqtt5_client_options_storage *aws_mqtt5_client_options_storage_new( |
| 3881 |
struct aws_allocator *allocator, |
| 3882 |
const struct aws_mqtt5_client_options *options) { |
| 3883 |
AWS_PRECONDITION(allocator != NULL); |
| 3884 |
AWS_PRECONDITION(options != NULL); |
| 3885 |
|
| 3886 |
if (aws_mqtt5_client_options_validate(options)) { |
| 3887 |
return NULL; |
| 3888 |
} |
| 3889 |
|
| 3890 |
struct aws_mqtt5_client_options_storage *options_storage = |
| 3891 |
aws_mem_calloc(allocator, 1, sizeof(struct aws_mqtt5_client_options_storage)); |
| 3892 |
if (options_storage == NULL) { |
| 3893 |
return NULL; |
| 3894 |
} |
| 3895 |
|
| 3896 |
options_storage->allocator = allocator; |
| 3897 |
options_storage->host_name = aws_string_new_from_cursor(allocator, &options->host_name); |
| 3898 |
if (options_storage->host_name == NULL) { |
| 3899 |
goto error; |
| 3900 |
} |
| 3901 |
|
| 3902 |
options_storage->port = options->port; |
| 3903 |
|
| 3904 |
options_storage->bootstrap = aws_client_bootstrap_acquire(options->bootstrap); |
| 3905 |
|
| 3906 |
if (options->socket_options != NULL) { |
| 3907 |
options_storage->socket_options = *options->socket_options; |
| 3908 |
} else { |
| 3909 |
options_storage->socket_options.type = AWS_SOCKET_STREAM; |
| 3910 |
options_storage->socket_options.connect_timeout_ms = AWS_MQTT5_DEFAULT_SOCKET_CONNECT_TIMEOUT_MS; |
| 3911 |
} |
| 3912 |
|
| 3913 |
if (options->tls_options != NULL) { |
| 3914 |
if (aws_tls_connection_options_copy(&options_storage->tls_options, options->tls_options)) { |
| 3915 |
goto error; |
| 3916 |
} |
| 3917 |
options_storage->tls_options_ptr = &options_storage->tls_options; |
| 3918 |
|
| 3919 |
if (!options_storage->tls_options.server_name) { |
| 3920 |
struct aws_byte_cursor host_name_cur = aws_byte_cursor_from_string(options_storage->host_name); |
| 3921 |
if (aws_tls_connection_options_set_server_name(&options_storage->tls_options, allocator, &host_name_cur)) { |
| 3922 |
|
| 3923 |
AWS_LOGF_ERROR(AWS_LS_MQTT5_GENERAL, "Failed to set TLS Connection Options server name"); |
| 3924 |
goto error; |
| 3925 |
} |
| 3926 |
} |
| 3927 |
} |
| 3928 |
|
| 3929 |
if (options->http_proxy_options != NULL) { |
| 3930 |
|
| 3931 |
options_storage->http_proxy_config = |
| 3932 |
aws_http_proxy_config_new_tunneling_from_proxy_options(allocator, options->http_proxy_options); |
| 3933 |
if (options_storage->http_proxy_config == NULL) { |
| 3934 |
goto error; |
| 3935 |
} |
| 3936 |
|
| 3937 |
aws_http_proxy_options_init_from_config( |
| 3938 |
&options_storage->http_proxy_options, options_storage->http_proxy_config); |
| 3939 |
} |
| 3940 |
|
| 3941 |
options_storage->websocket_handshake_transform = options->websocket_handshake_transform; |
| 3942 |
options_storage->websocket_handshake_transform_user_data = options->websocket_handshake_transform_user_data; |
| 3943 |
|
| 3944 |
options_storage->publish_received_handler = options->publish_received_handler; |
| 3945 |
options_storage->publish_received_handler_user_data = options->publish_received_handler_user_data; |
| 3946 |
|
| 3947 |
options_storage->session_behavior = options->session_behavior; |
| 3948 |
options_storage->extended_validation_and_flow_control_options = |
| 3949 |
options->extended_validation_and_flow_control_options; |
| 3950 |
options_storage->offline_queue_behavior = options->offline_queue_behavior; |
| 3951 |
|
| 3952 |
options_storage->retry_jitter_mode = options->retry_jitter_mode; |
| 3953 |
options_storage->min_reconnect_delay_ms = options->min_reconnect_delay_ms; |
| 3954 |
options_storage->max_reconnect_delay_ms = options->max_reconnect_delay_ms; |
| 3955 |
options_storage->min_connected_time_to_reset_reconnect_delay_ms = |
| 3956 |
options->min_connected_time_to_reset_reconnect_delay_ms; |
| 3957 |
|
| 3958 |
options_storage->ping_timeout_ms = options->ping_timeout_ms; |
| 3959 |
options_storage->connack_timeout_ms = options->connack_timeout_ms; |
| 3960 |
|
| 3961 |
options_storage->ack_timeout_seconds = options->ack_timeout_seconds; |
| 3962 |
|
| 3963 |
if (options->topic_aliasing_options != NULL) { |
| 3964 |
options_storage->topic_aliasing_options = *options->topic_aliasing_options; |
| 3965 |
} |
| 3966 |
|
| 3967 |
struct aws_byte_buf auto_assign_id_buf; |
| 3968 |
AWS_ZERO_STRUCT(auto_assign_id_buf); |
| 3969 |
|
| 3970 |
struct aws_mqtt5_packet_connect_view connect_options = *options->connect_options; |
| 3971 |
if (connect_options.client_id.len == 0) { |
| 3972 |
if (options->extended_validation_and_flow_control_options == AWS_MQTT5_EVAFCO_AWS_IOT_CORE_DEFAULTS) { |
| 3973 |
|
| 3974 |
|
| 3975 |
|
| 3976 |
|
| 3977 |
|
| 3978 |
|
| 3979 |
|
| 3980 |
|
| 3981 |
|
| 3982 |
|
| 3983 |
|
| 3984 |
|
| 3985 |
|
| 3986 |
|
| 3987 |
|
| 3988 |
|
| 3989 |
|
| 3990 |
|
| 3991 |
|
| 3992 |
|
| 3993 |
|
| 3994 |
|
| 3995 |
aws_byte_buf_init(&auto_assign_id_buf, allocator, 64); |
| 3996 |
struct aws_byte_cursor auto_assign_prefix_cursor = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("gen"); |
| 3997 |
|
| 3998 |
aws_byte_buf_append(&auto_assign_id_buf, &auto_assign_prefix_cursor); |
| 3999 |
|
| 4000 |
struct aws_uuid uuid; |
| 4001 |
AWS_FATAL_ASSERT(aws_uuid_init(&uuid) == AWS_OP_SUCCESS); |
| 4002 |
|
| 4003 |
aws_uuid_to_str_compact(&uuid, &auto_assign_id_buf); |
| 4004 |
|
| 4005 |
connect_options.client_id = aws_byte_cursor_from_buf(&auto_assign_id_buf); |
| 4006 |
|
| 4007 |
AWS_LOGF_INFO( |
| 4008 |
AWS_LS_MQTT5_GENERAL, |
| 4009 |
"Inferring the server is IoT Core and no client id has been set. IoT Core's auto-assigned client ids " |
| 4010 |
"cannot be reconnected with. Avoiding this issue by locally setting the client id to \"" PRInSTR "\"", |
| 4011 |
AWS_BYTE_CURSOR_PRI(connect_options.client_id)); |
| 4012 |
} else { |
| 4013 |
|
| 4014 |
|
| 4015 |
|
| 4016 |
|
| 4017 |
AWS_LOGF_WARN( |
| 4018 |
AWS_LS_MQTT5_GENERAL, |
| 4019 |
"Letting server assign the client id. If reconnects fail with a connect reason code of " |
| 4020 |
"AWS_MQTT5_CRC_CLIENT_IDENTIFIER_NOT_VALID (133), then consider manually setting the client id to a " |
| 4021 |
"UUID."); |
| 4022 |
} |
| 4023 |
} |
| 4024 |
|
| 4025 |
options_storage->connect = aws_mem_calloc(allocator, 1, sizeof(struct aws_mqtt5_packet_connect_storage)); |
| 4026 |
|
| 4027 |
int connect_storage_result = |
| 4028 |
aws_mqtt5_packet_connect_storage_init(options_storage->connect, allocator, &connect_options, NULL); |
| 4029 |
|
| 4030 |
aws_byte_buf_clean_up(&auto_assign_id_buf); |
| 4031 |
if (connect_storage_result != AWS_OP_SUCCESS) { |
| 4032 |
goto error; |
| 4033 |
} |
| 4034 |
|
| 4035 |
options_storage->lifecycle_event_handler = options->lifecycle_event_handler; |
| 4036 |
options_storage->lifecycle_event_handler_user_data = options->lifecycle_event_handler_user_data; |
| 4037 |
|
| 4038 |
options_storage->client_termination_handler = options->client_termination_handler; |
| 4039 |
options_storage->client_termination_handler_user_data = options->client_termination_handler_user_data; |
| 4040 |
|
| 4041 |
s_apply_zero_valued_defaults_to_client_options_storage(options_storage); |
| 4042 |
|
| 4043 |
|
| 4044 |
if (options->host_resolution_override) { |
| 4045 |
options_storage->host_resolution_override = *options->host_resolution_override; |
| 4046 |
} else { |
| 4047 |
options_storage->host_resolution_override = aws_host_resolver_init_default_resolution_config(); |
| 4048 |
options_storage->host_resolution_override.resolve_frequency_ns = aws_timestamp_convert( |
| 4049 |
options_storage->max_reconnect_delay_ms, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL); |
| 4050 |
} |
| 4051 |
|
| 4052 |
options_storage->metrics_storage = aws_mqtt_iot_metrics_storage_new(allocator, options->metrics); |
| 4053 |
|
| 4054 |
return options_storage; |
| 4055 |
|
| 4056 |
error: |
| 4057 |
|
| 4058 |
aws_mqtt5_client_options_storage_destroy(options_storage); |
| 4059 |
|
| 4060 |
return NULL; |
| 4061 |
} |
| 4062 |
|
| 4063 |
struct aws_mqtt5_operation_disconnect *aws_mqtt5_operation_disconnect_acquire( |
| 4064 |
struct aws_mqtt5_operation_disconnect *disconnect_op) { |
| 4065 |
if (disconnect_op != NULL) { |
| 4066 |
aws_mqtt5_operation_acquire(&disconnect_op->base); |
| 4067 |
} |
| 4068 |
|
| 4069 |
return disconnect_op; |
| 4070 |
} |
| 4071 |
|
| 4072 |
struct aws_mqtt5_operation_disconnect *aws_mqtt5_operation_disconnect_release( |
| 4073 |
struct aws_mqtt5_operation_disconnect *disconnect_op) { |
| 4074 |
if (disconnect_op != NULL) { |
| 4075 |
aws_mqtt5_operation_release(&disconnect_op->base); |
| 4076 |
} |
| 4077 |
|
| 4078 |
return NULL; |
| 4079 |
} |