| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/mqtt/private/v5/mqtt5_encoder.h> |
| 7 |
|
| 8 |
#include <aws/io/stream.h> |
| 9 |
#include <aws/mqtt/private/v5/mqtt5_topic_alias.h> |
| 10 |
#include <aws/mqtt/private/v5/mqtt5_utils.h> |
| 11 |
#include <aws/mqtt/v5/mqtt5_types.h> |
| 12 |
|
| 13 |
#include <inttypes.h> |
| 14 |
|
| 15 |
#define INITIAL_ENCODING_STEP_COUNT 64 |
| 16 |
#define SUBSCRIBE_PACKET_FIXED_HEADER_RESERVED_BITS 2 |
| 17 |
#define UNSUBSCRIBE_PACKET_FIXED_HEADER_RESERVED_BITS 2 |
| 18 |
|
| 19 |
int aws_mqtt5_encode_variable_length_integer(struct aws_byte_buf *buf, uint32_t value) { |
| 20 |
AWS_PRECONDITION(buf); |
| 21 |
|
| 22 |
if (value > AWS_MQTT5_MAXIMUM_VARIABLE_LENGTH_INTEGER) { |
| 23 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 24 |
} |
| 25 |
|
| 26 |
do { |
| 27 |
uint8_t encoded_byte = value % 128; |
| 28 |
value /= 128; |
| 29 |
if (value) { |
| 30 |
encoded_byte |= 128; |
| 31 |
} |
| 32 |
if (!aws_byte_buf_write_u8(buf, encoded_byte)) { |
| 33 |
return aws_raise_error(AWS_ERROR_SHORT_BUFFER); |
| 34 |
} |
| 35 |
} while (value); |
| 36 |
|
| 37 |
return AWS_OP_SUCCESS; |
| 38 |
} |
| 39 |
|
| 40 |
int aws_mqtt5_get_variable_length_encode_size(size_t value, size_t *encode_size) { |
| 41 |
if (value > AWS_MQTT5_MAXIMUM_VARIABLE_LENGTH_INTEGER) { |
| 42 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 43 |
} |
| 44 |
|
| 45 |
if (value < 128) { |
| 46 |
*encode_size = 1; |
| 47 |
} else if (value < 16384) { |
| 48 |
*encode_size = 2; |
| 49 |
} else if (value < 2097152) { |
| 50 |
*encode_size = 3; |
| 51 |
} else { |
| 52 |
*encode_size = 4; |
| 53 |
} |
| 54 |
|
| 55 |
return AWS_OP_SUCCESS; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
void aws_mqtt5_encoder_push_step_u8(struct aws_mqtt5_encoder *encoder, uint8_t value) { |
| 61 |
struct aws_mqtt5_encoding_step step; |
| 62 |
AWS_ZERO_STRUCT(step); |
| 63 |
|
| 64 |
step.type = AWS_MQTT5_EST_U8; |
| 65 |
step.value.value_u8 = value; |
| 66 |
|
| 67 |
aws_array_list_push_back(&encoder->encoding_steps, &step); |
| 68 |
} |
| 69 |
|
| 70 |
void aws_mqtt5_encoder_push_step_u16(struct aws_mqtt5_encoder *encoder, uint16_t value) { |
| 71 |
struct aws_mqtt5_encoding_step step; |
| 72 |
AWS_ZERO_STRUCT(step); |
| 73 |
|
| 74 |
step.type = AWS_MQTT5_EST_U16; |
| 75 |
step.value.value_u16 = value; |
| 76 |
|
| 77 |
aws_array_list_push_back(&encoder->encoding_steps, &step); |
| 78 |
} |
| 79 |
|
| 80 |
void aws_mqtt5_encoder_push_step_u32(struct aws_mqtt5_encoder *encoder, uint32_t value) { |
| 81 |
struct aws_mqtt5_encoding_step step; |
| 82 |
AWS_ZERO_STRUCT(step); |
| 83 |
|
| 84 |
step.type = AWS_MQTT5_EST_U32; |
| 85 |
step.value.value_u32 = value; |
| 86 |
|
| 87 |
aws_array_list_push_back(&encoder->encoding_steps, &step); |
| 88 |
} |
| 89 |
|
| 90 |
int aws_mqtt5_encoder_push_step_vli(struct aws_mqtt5_encoder *encoder, uint32_t value) { |
| 91 |
if (value > AWS_MQTT5_MAXIMUM_VARIABLE_LENGTH_INTEGER) { |
| 92 |
return aws_raise_error(AWS_ERROR_MQTT5_ENCODE_FAILURE); |
| 93 |
} |
| 94 |
|
| 95 |
struct aws_mqtt5_encoding_step step; |
| 96 |
AWS_ZERO_STRUCT(step); |
| 97 |
|
| 98 |
step.type = AWS_MQTT5_EST_VLI; |
| 99 |
step.value.value_u32 = value; |
| 100 |
|
| 101 |
aws_array_list_push_back(&encoder->encoding_steps, &step); |
| 102 |
|
| 103 |
return AWS_OP_SUCCESS; |
| 104 |
} |
| 105 |
|
| 106 |
void aws_mqtt5_encoder_push_step_cursor(struct aws_mqtt5_encoder *encoder, struct aws_byte_cursor value) { |
| 107 |
struct aws_mqtt5_encoding_step step; |
| 108 |
AWS_ZERO_STRUCT(step); |
| 109 |
|
| 110 |
step.type = AWS_MQTT5_EST_CURSOR; |
| 111 |
step.value.value_cursor = value; |
| 112 |
|
| 113 |
aws_array_list_push_back(&encoder->encoding_steps, &step); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
size_t aws_mqtt5_compute_user_property_encode_length( |
| 125 |
const struct aws_mqtt5_user_property *properties, |
| 126 |
size_t user_property_count) { |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
size_t length = 5 * user_property_count; |
| 134 |
|
| 135 |
for (size_t i = 0; i < user_property_count; ++i) { |
| 136 |
const struct aws_mqtt5_user_property *property = &properties[i]; |
| 137 |
|
| 138 |
length += property->name.len; |
| 139 |
length += property->value.len; |
| 140 |
} |
| 141 |
|
| 142 |
return length; |
| 143 |
} |
| 144 |
|
| 145 |
void aws_mqtt5_add_user_property_encoding_steps( |
| 146 |
struct aws_mqtt5_encoder *encoder, |
| 147 |
const struct aws_mqtt5_user_property *user_properties, |
| 148 |
size_t user_property_count) { |
| 149 |
for (size_t i = 0; i < user_property_count; ++i) { |
| 150 |
const struct aws_mqtt5_user_property *property = &user_properties[i]; |
| 151 |
|
| 152 |
|
| 153 |
ADD_ENCODE_STEP_U8(encoder, AWS_MQTT5_PROPERTY_TYPE_USER_PROPERTY); |
| 154 |
ADD_ENCODE_STEP_U16(encoder, (uint16_t)property->name.len); |
| 155 |
ADD_ENCODE_STEP_CURSOR(encoder, property->name); |
| 156 |
ADD_ENCODE_STEP_U16(encoder, (uint16_t)property->value.len); |
| 157 |
ADD_ENCODE_STEP_CURSOR(encoder, property->value); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
static int s_aws_mqtt5_encoder_begin_pingreq(struct aws_mqtt5_encoder *encoder, const void *view) { |
| 162 |
(void)view; |
| 163 |
|
| 164 |
AWS_LOGF_DEBUG( |
| 165 |
AWS_LS_MQTT5_CLIENT, "id=%p: setting up encode for a PINGREQ packet", (void *)encoder->config.client); |
| 166 |
|
| 167 |
|
| 168 |
ADD_ENCODE_STEP_U8(encoder, aws_mqtt5_compute_fixed_header_byte1(AWS_MQTT5_PT_PINGREQ, 0)); |
| 169 |
ADD_ENCODE_STEP_U8(encoder, 0); |
| 170 |
|
| 171 |
return AWS_OP_SUCCESS; |
| 172 |
} |
| 173 |
|
| 174 |
static int s_compute_disconnect_variable_length_fields( |
| 175 |
const struct aws_mqtt5_packet_disconnect_view *disconnect_view, |
| 176 |
size_t *total_remaining_length, |
| 177 |
size_t *property_length) { |
| 178 |
size_t local_property_length = aws_mqtt5_compute_user_property_encode_length( |
| 179 |
disconnect_view->user_properties, disconnect_view->user_property_count); |
| 180 |
|
| 181 |
ADD_OPTIONAL_U32_PROPERTY_LENGTH(disconnect_view->session_expiry_interval_seconds, local_property_length); |
| 182 |
ADD_OPTIONAL_CURSOR_PROPERTY_LENGTH(disconnect_view->server_reference, local_property_length); |
| 183 |
ADD_OPTIONAL_CURSOR_PROPERTY_LENGTH(disconnect_view->reason_string, local_property_length); |
| 184 |
|
| 185 |
*property_length = local_property_length; |
| 186 |
|
| 187 |
size_t property_length_encoding_length = 0; |
| 188 |
if (aws_mqtt5_get_variable_length_encode_size(local_property_length, &property_length_encoding_length)) { |
| 189 |
return AWS_OP_ERR; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
*total_remaining_length = 1 + *property_length + property_length_encoding_length; |
| 194 |
|
| 195 |
return AWS_OP_SUCCESS; |
| 196 |
} |
| 197 |
|
| 198 |
static int s_aws_mqtt5_encoder_begin_disconnect(struct aws_mqtt5_encoder *encoder, const void *view) { |
| 199 |
|
| 200 |
const struct aws_mqtt5_packet_disconnect_view *disconnect_view = view; |
| 201 |
|
| 202 |
size_t total_remaining_length = 0; |
| 203 |
size_t property_length = 0; |
| 204 |
if (s_compute_disconnect_variable_length_fields(disconnect_view, &total_remaining_length, &property_length)) { |
| 205 |
int error_code = aws_last_error(); |
| 206 |
AWS_LOGF_ERROR( |
| 207 |
AWS_LS_MQTT5_CLIENT, |
| 208 |
"id=%p: failed to compute variable length values for DISCONNECT packet with error " |
| 209 |
"%d(%s)", |
| 210 |
(void *)encoder->config.client, |
| 211 |
error_code, |
| 212 |
aws_error_debug_str(error_code)); |
| 213 |
return AWS_OP_ERR; |
| 214 |
} |
| 215 |
|
| 216 |
uint32_t total_remaining_length_u32 = (uint32_t)total_remaining_length; |
| 217 |
uint32_t property_length_u32 = (uint32_t)property_length; |
| 218 |
|
| 219 |
AWS_LOGF_DEBUG( |
| 220 |
AWS_LS_MQTT5_CLIENT, |
| 221 |
"id=%p: setting up encode for a DISCONNECT packet with remaining length %" PRIu32, |
| 222 |
(void *)encoder->config.client, |
| 223 |
total_remaining_length_u32); |
| 224 |
|
| 225 |
ADD_ENCODE_STEP_U8(encoder, aws_mqtt5_compute_fixed_header_byte1(AWS_MQTT5_PT_DISCONNECT, 0)); |
| 226 |
ADD_ENCODE_STEP_VLI(encoder, total_remaining_length_u32); |
| 227 |
ADD_ENCODE_STEP_U8(encoder, (uint8_t)disconnect_view->reason_code); |
| 228 |
ADD_ENCODE_STEP_VLI(encoder, property_length_u32); |
| 229 |
|
| 230 |
if (property_length > 0) { |
| 231 |
ADD_ENCODE_STEP_OPTIONAL_U32_PROPERTY( |
| 232 |
encoder, AWS_MQTT5_PROPERTY_TYPE_SESSION_EXPIRY_INTERVAL, disconnect_view->session_expiry_interval_seconds); |
| 233 |
ADD_ENCODE_STEP_OPTIONAL_CURSOR_PROPERTY( |
| 234 |
encoder, AWS_MQTT5_PROPERTY_TYPE_REASON_STRING, disconnect_view->reason_string); |
| 235 |
ADD_ENCODE_STEP_OPTIONAL_CURSOR_PROPERTY( |
| 236 |
encoder, AWS_MQTT5_PROPERTY_TYPE_SERVER_REFERENCE, disconnect_view->server_reference); |
| 237 |
|
| 238 |
aws_mqtt5_add_user_property_encoding_steps( |
| 239 |
encoder, disconnect_view->user_properties, disconnect_view->user_property_count); |
| 240 |
} |
| 241 |
|
| 242 |
return AWS_OP_SUCCESS; |
| 243 |
} |
| 244 |
|
| 245 |
static int s_compute_connect_variable_length_fields( |
| 246 |
const struct aws_mqtt5_packet_connect_view *connect_view, |
| 247 |
size_t *total_remaining_length, |
| 248 |
size_t *connect_property_length, |
| 249 |
size_t *will_property_length) { |
| 250 |
|
| 251 |
size_t connect_property_section_length = |
| 252 |
aws_mqtt5_compute_user_property_encode_length(connect_view->user_properties, connect_view->user_property_count); |
| 253 |
|
| 254 |
ADD_OPTIONAL_U32_PROPERTY_LENGTH(connect_view->session_expiry_interval_seconds, connect_property_section_length); |
| 255 |
ADD_OPTIONAL_U16_PROPERTY_LENGTH(connect_view->receive_maximum, connect_property_section_length); |
| 256 |
ADD_OPTIONAL_U32_PROPERTY_LENGTH(connect_view->maximum_packet_size_bytes, connect_property_section_length); |
| 257 |
ADD_OPTIONAL_U16_PROPERTY_LENGTH(connect_view->topic_alias_maximum, connect_property_section_length); |
| 258 |
ADD_OPTIONAL_U8_PROPERTY_LENGTH(connect_view->request_response_information, connect_property_section_length); |
| 259 |
ADD_OPTIONAL_U8_PROPERTY_LENGTH(connect_view->request_problem_information, connect_property_section_length); |
| 260 |
ADD_OPTIONAL_CURSOR_PROPERTY_LENGTH(connect_view->authentication_method, connect_property_section_length); |
| 261 |
ADD_OPTIONAL_CURSOR_PROPERTY_LENGTH(connect_view->authentication_data, connect_property_section_length); |
| 262 |
|
| 263 |
*connect_property_length = (uint32_t)connect_property_section_length; |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
size_t variable_header_length = 0; |
| 271 |
if (aws_mqtt5_get_variable_length_encode_size(connect_property_section_length, &variable_header_length)) { |
| 272 |
return AWS_OP_ERR; |
| 273 |
} |
| 274 |
|
| 275 |
variable_header_length += 10 + connect_property_section_length; |
| 276 |
|
| 277 |
size_t payload_length = 2 + connect_view->client_id.len; |
| 278 |
|
| 279 |
*will_property_length = 0; |
| 280 |
if (connect_view->will != NULL) { |
| 281 |
const struct aws_mqtt5_packet_publish_view *publish_view = connect_view->will; |
| 282 |
|
| 283 |
*will_property_length = aws_mqtt5_compute_user_property_encode_length( |
| 284 |
publish_view->user_properties, publish_view->user_property_count); |
| 285 |
|
| 286 |
ADD_OPTIONAL_U32_PROPERTY_LENGTH(connect_view->will_delay_interval_seconds, *will_property_length); |
| 287 |
ADD_OPTIONAL_U8_PROPERTY_LENGTH(publish_view->payload_format, *will_property_length); |
| 288 |
ADD_OPTIONAL_U32_PROPERTY_LENGTH(publish_view->message_expiry_interval_seconds, *will_property_length); |
| 289 |
ADD_OPTIONAL_CURSOR_PROPERTY_LENGTH(publish_view->content_type, *will_property_length); |
| 290 |
ADD_OPTIONAL_CURSOR_PROPERTY_LENGTH(publish_view->response_topic, *will_property_length); |
| 291 |
ADD_OPTIONAL_CURSOR_PROPERTY_LENGTH(publish_view->correlation_data, *will_property_length); |
| 292 |
|
| 293 |
size_t will_properties_length_encode_size = 0; |
| 294 |
if (aws_mqtt5_get_variable_length_encode_size( |
| 295 |
(uint32_t)*will_property_length, &will_properties_length_encode_size)) { |
| 296 |
return AWS_OP_ERR; |
| 297 |
} |
| 298 |
|
| 299 |
payload_length += *will_property_length; |
| 300 |
payload_length += will_properties_length_encode_size; |
| 301 |
|
| 302 |
payload_length += 2 + publish_view->topic.len; |
| 303 |
payload_length += 2 + publish_view->payload.len; |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
if (connect_view->username != NULL) { |
| 308 |
payload_length += connect_view->username->len + 2; |
| 309 |
} |
| 310 |
|
| 311 |
if (connect_view->password != NULL) { |
| 312 |
payload_length += connect_view->password->len + 2; |
| 313 |
} |
| 314 |
|
| 315 |
*total_remaining_length = payload_length + variable_header_length; |
| 316 |
|
| 317 |
return AWS_OP_SUCCESS; |
| 318 |
} |
| 319 |
|
| 320 |
static uint8_t s_aws_mqtt5_connect_compute_connect_flags(const struct aws_mqtt5_packet_connect_view *connect_view) { |
| 321 |
uint8_t flags = 0; |
| 322 |
|
| 323 |
if (connect_view->clean_start) { |
| 324 |
flags |= 1 << 1; |
| 325 |
} |
| 326 |
|
| 327 |
const struct aws_mqtt5_packet_publish_view *will = connect_view->will; |
| 328 |
if (will != NULL) { |
| 329 |
flags |= 1 << 2; |
| 330 |
flags |= ((uint8_t)will->qos) << 3; |
| 331 |
|
| 332 |
if (will->retain) { |
| 333 |
flags |= 1 << 5; |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
if (connect_view->password != NULL) { |
| 338 |
flags |= 1 << 6; |
| 339 |
} |
| 340 |
|
| 341 |
if (connect_view->username != NULL) { |
| 342 |
flags |= 1 << 7; |
| 343 |
} |
| 344 |
|
| 345 |
return flags; |
| 346 |
} |
| 347 |
|
| 348 |
static int s_aws_mqtt5_encoder_begin_connect(struct aws_mqtt5_encoder *encoder, const void *view) { |
| 349 |
|
| 350 |
const struct aws_mqtt5_packet_connect_view *connect_view = view; |
| 351 |
const struct aws_mqtt5_packet_publish_view *will = connect_view->will; |
| 352 |
|
| 353 |
size_t total_remaining_length = 0; |
| 354 |
size_t connect_property_length = 0; |
| 355 |
size_t will_property_length = 0; |
| 356 |
if (s_compute_connect_variable_length_fields( |
| 357 |
connect_view, &total_remaining_length, &connect_property_length, &will_property_length)) { |
| 358 |
int error_code = aws_last_error(); |
| 359 |
AWS_LOGF_ERROR( |
| 360 |
AWS_LS_MQTT5_CLIENT, |
| 361 |
"id=%p: failed to compute variable length values for CONNECT packet with error %d(%s)", |
| 362 |
(void *)encoder->config.client, |
| 363 |
error_code, |
| 364 |
aws_error_debug_str(error_code)); |
| 365 |
return AWS_OP_ERR; |
| 366 |
} |
| 367 |
|
| 368 |
AWS_LOGF_DEBUG( |
| 369 |
AWS_LS_MQTT5_CLIENT, |
| 370 |
"id=%p: setting up encode for a CONNECT packet with remaining length %zu", |
| 371 |
(void *)encoder->config.client, |
| 372 |
total_remaining_length); |
| 373 |
|
| 374 |
uint32_t total_remaining_length_u32 = (uint32_t)total_remaining_length; |
| 375 |
uint32_t connect_property_length_u32 = (uint32_t)connect_property_length; |
| 376 |
uint32_t will_property_length_u32 = (uint32_t)will_property_length; |
| 377 |
|
| 378 |
ADD_ENCODE_STEP_U8(encoder, aws_mqtt5_compute_fixed_header_byte1(AWS_MQTT5_PT_CONNECT, 0)); |
| 379 |
ADD_ENCODE_STEP_VLI(encoder, total_remaining_length_u32); |
| 380 |
ADD_ENCODE_STEP_CURSOR(encoder, g_aws_mqtt5_connect_protocol_cursor); |
| 381 |
ADD_ENCODE_STEP_U8(encoder, s_aws_mqtt5_connect_compute_connect_flags(connect_view)); |
| 382 |
ADD_ENCODE_STEP_U16(encoder, connect_view->keep_alive_interval_seconds); |
| 383 |
|
| 384 |
ADD_ENCODE_STEP_VLI(encoder, connect_property_length_u32); |
| 385 |
ADD_ENCODE_STEP_OPTIONAL_U32_PROPERTY( |
| 386 |
encoder, AWS_MQTT5_PROPERTY_TYPE_SESSION_EXPIRY_INTERVAL, connect_view->session_expiry_interval_seconds); |
| 387 |
ADD_ENCODE_STEP_OPTIONAL_U16_PROPERTY( |
| 388 |
encoder, AWS_MQTT5_PROPERTY_TYPE_RECEIVE_MAXIMUM, connect_view->receive_maximum); |
| 389 |
ADD_ENCODE_STEP_OPTIONAL_U32_PROPERTY( |
| 390 |
encoder, AWS_MQTT5_PROPERTY_TYPE_MAXIMUM_PACKET_SIZE, connect_view->maximum_packet_size_bytes); |
| 391 |
ADD_ENCODE_STEP_OPTIONAL_U16_PROPERTY( |
| 392 |
encoder, AWS_MQTT5_PROPERTY_TYPE_TOPIC_ALIAS_MAXIMUM, connect_view->topic_alias_maximum); |
| 393 |
ADD_ENCODE_STEP_OPTIONAL_U8_PROPERTY( |
| 394 |
encoder, AWS_MQTT5_PROPERTY_TYPE_REQUEST_RESPONSE_INFORMATION, connect_view->request_response_information); |
| 395 |
ADD_ENCODE_STEP_OPTIONAL_U8_PROPERTY( |
| 396 |
encoder, AWS_MQTT5_PROPERTY_TYPE_REQUEST_PROBLEM_INFORMATION, connect_view->request_problem_information); |
| 397 |
ADD_ENCODE_STEP_OPTIONAL_CURSOR_PROPERTY( |
| 398 |
encoder, AWS_MQTT5_PROPERTY_TYPE_AUTHENTICATION_METHOD, connect_view->authentication_method); |
| 399 |
ADD_ENCODE_STEP_OPTIONAL_CURSOR_PROPERTY( |
| 400 |
encoder, AWS_MQTT5_PROPERTY_TYPE_AUTHENTICATION_DATA, connect_view->authentication_data); |
| 401 |
|
| 402 |
aws_mqtt5_add_user_property_encoding_steps( |
| 403 |
encoder, connect_view->user_properties, connect_view->user_property_count); |
| 404 |
|
| 405 |
ADD_ENCODE_STEP_LENGTH_PREFIXED_CURSOR(encoder, connect_view->client_id); |
| 406 |
|
| 407 |
if (will != NULL) { |
| 408 |
ADD_ENCODE_STEP_VLI(encoder, will_property_length_u32); |
| 409 |
ADD_ENCODE_STEP_OPTIONAL_U32_PROPERTY( |
| 410 |
encoder, AWS_MQTT5_PROPERTY_TYPE_WILL_DELAY_INTERVAL, connect_view->will_delay_interval_seconds); |
| 411 |
ADD_ENCODE_STEP_OPTIONAL_U8_PROPERTY( |
| 412 |
encoder, AWS_MQTT5_PROPERTY_TYPE_PAYLOAD_FORMAT_INDICATOR, will->payload_format); |
| 413 |
ADD_ENCODE_STEP_OPTIONAL_U32_PROPERTY( |
| 414 |
encoder, AWS_MQTT5_PROPERTY_TYPE_MESSAGE_EXPIRY_INTERVAL, will->message_expiry_interval_seconds); |
| 415 |
ADD_ENCODE_STEP_OPTIONAL_CURSOR_PROPERTY(encoder, AWS_MQTT5_PROPERTY_TYPE_CONTENT_TYPE, will->content_type); |
| 416 |
ADD_ENCODE_STEP_OPTIONAL_CURSOR_PROPERTY(encoder, AWS_MQTT5_PROPERTY_TYPE_RESPONSE_TOPIC, will->response_topic); |
| 417 |
ADD_ENCODE_STEP_OPTIONAL_CURSOR_PROPERTY( |
| 418 |
encoder, AWS_MQTT5_PROPERTY_TYPE_CORRELATION_DATA, will->correlation_data); |
| 419 |
|
| 420 |
aws_mqtt5_add_user_property_encoding_steps(encoder, will->user_properties, will->user_property_count); |
| 421 |
|
| 422 |
ADD_ENCODE_STEP_LENGTH_PREFIXED_CURSOR(encoder, will->topic); |
| 423 |
ADD_ENCODE_STEP_U16(encoder, (uint16_t)will->payload.len); |
| 424 |
ADD_ENCODE_STEP_CURSOR(encoder, will->payload); |
| 425 |
} |
| 426 |
|
| 427 |
ADD_ENCODE_STEP_OPTIONAL_LENGTH_PREFIXED_CURSOR(encoder, connect_view->username); |
| 428 |
ADD_ENCODE_STEP_OPTIONAL_LENGTH_PREFIXED_CURSOR(encoder, connect_view->password); |
| 429 |
|
| 430 |
return AWS_OP_SUCCESS; |
| 431 |
} |
| 432 |
|
| 433 |
static uint8_t s_aws_mqtt5_subscribe_compute_subscription_flags( |
| 434 |
const struct aws_mqtt5_subscription_view *subscription_view) { |
| 435 |
|
| 436 |
uint8_t flags = (uint8_t)subscription_view->qos; |
| 437 |
|
| 438 |
if (subscription_view->no_local) { |
| 439 |
flags |= 1 << 2; |
| 440 |
} |
| 441 |
|
| 442 |
if (subscription_view->retain_as_published) { |
| 443 |
flags |= 1 << 3; |
| 444 |
} |
| 445 |
|
| 446 |
flags |= ((uint8_t)subscription_view->retain_handling_type) << 4; |
| 447 |
|
| 448 |
return flags; |
| 449 |
} |
| 450 |
|
| 451 |
static void aws_mqtt5_add_subscribe_topic_filter_encoding_steps( |
| 452 |
struct aws_mqtt5_encoder *encoder, |
| 453 |
const struct aws_mqtt5_subscription_view *subscriptions, |
| 454 |
size_t subscription_count) { |
| 455 |
|
| 456 |
for (size_t i = 0; i < subscription_count; ++i) { |
| 457 |
const struct aws_mqtt5_subscription_view *subscription = &subscriptions[i]; |
| 458 |
ADD_ENCODE_STEP_LENGTH_PREFIXED_CURSOR(encoder, subscription->topic_filter); |
| 459 |
ADD_ENCODE_STEP_U8(encoder, s_aws_mqtt5_subscribe_compute_subscription_flags(subscription)); |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
static void aws_mqtt5_add_unsubscribe_topic_filter_encoding_steps( |
| 464 |
struct aws_mqtt5_encoder *encoder, |
| 465 |
const struct aws_byte_cursor *topics, |
| 466 |
size_t unsubscription_count) { |
| 467 |
|
| 468 |
for (size_t i = 0; i < unsubscription_count; ++i) { |
| 469 |
const struct aws_byte_cursor topic_filter = topics[i]; |
| 470 |
ADD_ENCODE_STEP_LENGTH_PREFIXED_CURSOR(encoder, topic_filter); |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
static int s_compute_subscribe_variable_length_fields( |
| 475 |
const struct aws_mqtt5_packet_subscribe_view *subscribe_view, |
| 476 |
size_t *total_remaining_length, |
| 477 |
size_t *subscribe_properties_length) { |
| 478 |
|
| 479 |
size_t subscribe_variable_header_property_length = aws_mqtt5_compute_user_property_encode_length( |
| 480 |
subscribe_view->user_properties, subscribe_view->user_property_count); |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
if (subscribe_view->subscription_identifier != 0) { |
| 487 |
size_t subscription_identifier_length = 0; |
| 488 |
aws_mqtt5_get_variable_length_encode_size( |
| 489 |
*subscribe_view->subscription_identifier, &subscription_identifier_length); |
| 490 |
subscribe_variable_header_property_length += subscription_identifier_length + 1; |
| 491 |
} |
| 492 |
|
| 493 |
*subscribe_properties_length = subscribe_variable_header_property_length; |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
size_t variable_header_length = 0; |
| 501 |
if (aws_mqtt5_get_variable_length_encode_size(subscribe_variable_header_property_length, &variable_header_length)) { |
| 502 |
return AWS_OP_ERR; |
| 503 |
} |
| 504 |
variable_header_length += 2 + subscribe_variable_header_property_length; |
| 505 |
|
| 506 |
size_t payload_length = 0; |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
for (size_t i = 0; i < subscribe_view->subscription_count; ++i) { |
| 516 |
const struct aws_mqtt5_subscription_view *subscription = &subscribe_view->subscriptions[i]; |
| 517 |
payload_length += subscription->topic_filter.len; |
| 518 |
} |
| 519 |
payload_length += (3 * subscribe_view->subscription_count); |
| 520 |
|
| 521 |
*total_remaining_length = variable_header_length + payload_length; |
| 522 |
|
| 523 |
return AWS_OP_SUCCESS; |
| 524 |
} |
| 525 |
|
| 526 |
static int s_aws_mqtt5_encoder_begin_subscribe(struct aws_mqtt5_encoder *encoder, const void *view) { |
| 527 |
|
| 528 |
const struct aws_mqtt5_packet_subscribe_view *subscription_view = view; |
| 529 |
|
| 530 |
size_t total_remaining_length = 0; |
| 531 |
size_t subscribe_properties_length = 0; |
| 532 |
|
| 533 |
if (s_compute_subscribe_variable_length_fields( |
| 534 |
subscription_view, &total_remaining_length, &subscribe_properties_length)) { |
| 535 |
int error_code = aws_last_error(); |
| 536 |
AWS_LOGF_ERROR( |
| 537 |
AWS_LS_MQTT5_GENERAL, |
| 538 |
"(%p) mqtt5 client encoder - failed to compute variable length values for SUBSCRIBE packet with error " |
| 539 |
"%d(%s)", |
| 540 |
(void *)encoder->config.client, |
| 541 |
error_code, |
| 542 |
aws_error_debug_str(error_code)); |
| 543 |
return AWS_OP_ERR; |
| 544 |
} |
| 545 |
|
| 546 |
AWS_LOGF_DEBUG( |
| 547 |
AWS_LS_MQTT5_GENERAL, |
| 548 |
"(%p) mqtt5 client encoder - setting up encode for a SUBSCRIBE packet with remaining length %zu", |
| 549 |
(void *)encoder->config.client, |
| 550 |
total_remaining_length); |
| 551 |
|
| 552 |
uint32_t total_remaining_length_u32 = (uint32_t)total_remaining_length; |
| 553 |
uint32_t subscribe_property_length_u32 = (uint32_t)subscribe_properties_length; |
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
ADD_ENCODE_STEP_U8( |
| 563 |
encoder, |
| 564 |
aws_mqtt5_compute_fixed_header_byte1(AWS_MQTT5_PT_SUBSCRIBE, SUBSCRIBE_PACKET_FIXED_HEADER_RESERVED_BITS)); |
| 565 |
ADD_ENCODE_STEP_VLI(encoder, total_remaining_length_u32); |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
ADD_ENCODE_STEP_U16(encoder, (uint16_t)subscription_view->packet_id); |
| 573 |
ADD_ENCODE_STEP_VLI(encoder, subscribe_property_length_u32); |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
if (subscription_view->subscription_identifier != 0) { |
| 581 |
ADD_ENCODE_STEP_U8(encoder, AWS_MQTT5_PROPERTY_TYPE_SUBSCRIPTION_IDENTIFIER); |
| 582 |
ADD_ENCODE_STEP_VLI(encoder, *subscription_view->subscription_identifier); |
| 583 |
} |
| 584 |
|
| 585 |
aws_mqtt5_add_user_property_encoding_steps( |
| 586 |
encoder, subscription_view->user_properties, subscription_view->user_property_count); |
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
aws_mqtt5_add_subscribe_topic_filter_encoding_steps( |
| 601 |
encoder, subscription_view->subscriptions, subscription_view->subscription_count); |
| 602 |
|
| 603 |
return AWS_OP_SUCCESS; |
| 604 |
} |
| 605 |
|
| 606 |
static int s_compute_unsubscribe_variable_length_fields( |
| 607 |
const struct aws_mqtt5_packet_unsubscribe_view *unsubscribe_view, |
| 608 |
size_t *total_remaining_length, |
| 609 |
size_t *unsubscribe_properties_length) { |
| 610 |
|
| 611 |
size_t unsubscribe_variable_header_property_length = aws_mqtt5_compute_user_property_encode_length( |
| 612 |
unsubscribe_view->user_properties, unsubscribe_view->user_property_count); |
| 613 |
|
| 614 |
*unsubscribe_properties_length = unsubscribe_variable_header_property_length; |
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
size_t variable_header_length = 0; |
| 622 |
if (aws_mqtt5_get_variable_length_encode_size( |
| 623 |
unsubscribe_variable_header_property_length, &variable_header_length)) { |
| 624 |
return AWS_OP_ERR; |
| 625 |
} |
| 626 |
variable_header_length += 2 + unsubscribe_variable_header_property_length; |
| 627 |
|
| 628 |
size_t payload_length = 0; |
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
for (size_t i = 0; i < unsubscribe_view->topic_filter_count; ++i) { |
| 637 |
const struct aws_byte_cursor topic_filter = unsubscribe_view->topic_filters[i]; |
| 638 |
payload_length += topic_filter.len; |
| 639 |
} |
| 640 |
|
| 641 |
payload_length += (2 * unsubscribe_view->topic_filter_count); |
| 642 |
|
| 643 |
*total_remaining_length = variable_header_length + payload_length; |
| 644 |
|
| 645 |
return AWS_OP_SUCCESS; |
| 646 |
} |
| 647 |
|
| 648 |
static int s_aws_mqtt5_encoder_begin_unsubscribe(struct aws_mqtt5_encoder *encoder, const void *view) { |
| 649 |
|
| 650 |
const struct aws_mqtt5_packet_unsubscribe_view *unsubscribe_view = view; |
| 651 |
|
| 652 |
size_t total_remaining_length = 0; |
| 653 |
size_t unsubscribe_properties_length = 0; |
| 654 |
|
| 655 |
if (s_compute_unsubscribe_variable_length_fields( |
| 656 |
unsubscribe_view, &total_remaining_length, &unsubscribe_properties_length)) { |
| 657 |
int error_code = aws_last_error(); |
| 658 |
AWS_LOGF_ERROR( |
| 659 |
AWS_LS_MQTT5_GENERAL, |
| 660 |
"(%p) mqtt5 client encoder - failed to compute variable length values for UNSUBSCRIBE packet with error " |
| 661 |
"%d(%s)", |
| 662 |
(void *)encoder->config.client, |
| 663 |
error_code, |
| 664 |
aws_error_debug_str(error_code)); |
| 665 |
return AWS_OP_ERR; |
| 666 |
} |
| 667 |
|
| 668 |
AWS_LOGF_DEBUG( |
| 669 |
AWS_LS_MQTT5_GENERAL, |
| 670 |
"(%p) mqtt5 client encoder - setting up encode for a UNSUBSCRIBE packet with remaining length %zu", |
| 671 |
(void *)encoder->config.client, |
| 672 |
total_remaining_length); |
| 673 |
|
| 674 |
uint32_t total_remaining_length_u32 = (uint32_t)total_remaining_length; |
| 675 |
uint32_t unsubscribe_property_length_u32 = (uint32_t)unsubscribe_properties_length; |
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
ADD_ENCODE_STEP_U8( |
| 685 |
encoder, |
| 686 |
aws_mqtt5_compute_fixed_header_byte1(AWS_MQTT5_PT_UNSUBSCRIBE, UNSUBSCRIBE_PACKET_FIXED_HEADER_RESERVED_BITS)); |
| 687 |
ADD_ENCODE_STEP_VLI(encoder, total_remaining_length_u32); |
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
ADD_ENCODE_STEP_U16(encoder, (uint16_t)unsubscribe_view->packet_id); |
| 695 |
ADD_ENCODE_STEP_VLI(encoder, unsubscribe_property_length_u32); |
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
aws_mqtt5_add_user_property_encoding_steps( |
| 701 |
encoder, unsubscribe_view->user_properties, unsubscribe_view->user_property_count); |
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
aws_mqtt5_add_unsubscribe_topic_filter_encoding_steps( |
| 711 |
encoder, unsubscribe_view->topic_filters, unsubscribe_view->topic_filter_count); |
| 712 |
|
| 713 |
return AWS_OP_SUCCESS; |
| 714 |
} |
| 715 |
|
| 716 |
static int s_compute_publish_variable_length_fields( |
| 717 |
const struct aws_mqtt5_packet_publish_view *publish_view, |
| 718 |
size_t *total_remaining_length, |
| 719 |
size_t *publish_properties_length) { |
| 720 |
|
| 721 |
size_t publish_property_section_length = |
| 722 |
aws_mqtt5_compute_user_property_encode_length(publish_view->user_properties, publish_view->user_property_count); |
| 723 |
|
| 724 |
ADD_OPTIONAL_U8_PROPERTY_LENGTH(publish_view->payload_format, publish_property_section_length); |
| 725 |
ADD_OPTIONAL_U32_PROPERTY_LENGTH(publish_view->message_expiry_interval_seconds, publish_property_section_length); |
| 726 |
ADD_OPTIONAL_U16_PROPERTY_LENGTH(publish_view->topic_alias, publish_property_section_length); |
| 727 |
ADD_OPTIONAL_CURSOR_PROPERTY_LENGTH(publish_view->response_topic, publish_property_section_length); |
| 728 |
ADD_OPTIONAL_CURSOR_PROPERTY_LENGTH(publish_view->correlation_data, publish_property_section_length); |
| 729 |
ADD_OPTIONAL_CURSOR_PROPERTY_LENGTH(publish_view->content_type, publish_property_section_length); |
| 730 |
|
| 731 |
for (size_t i = 0; i < publish_view->subscription_identifier_count; ++i) { |
| 732 |
size_t encoding_size = 0; |
| 733 |
if (aws_mqtt5_get_variable_length_encode_size(publish_view->subscription_identifiers[i], &encoding_size)) { |
| 734 |
return AWS_OP_ERR; |
| 735 |
} |
| 736 |
publish_property_section_length += 1 + encoding_size; |
| 737 |
} |
| 738 |
|
| 739 |
*publish_properties_length = (uint32_t)publish_property_section_length; |
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
size_t remaining_length = 0; |
| 752 |
|
| 753 |
|
| 754 |
if (aws_mqtt5_get_variable_length_encode_size(publish_property_section_length, &remaining_length)) { |
| 755 |
return AWS_OP_ERR; |
| 756 |
} |
| 757 |
|
| 758 |
|
| 759 |
remaining_length += 2 + publish_view->topic.len; |
| 760 |
|
| 761 |
|
| 762 |
if (publish_view->packet_id != 0) { |
| 763 |
remaining_length += 2; |
| 764 |
} |
| 765 |
|
| 766 |
|
| 767 |
remaining_length += publish_property_section_length; |
| 768 |
|
| 769 |
|
| 770 |
remaining_length += publish_view->payload.len; |
| 771 |
|
| 772 |
*total_remaining_length = remaining_length; |
| 773 |
|
| 774 |
return AWS_OP_SUCCESS; |
| 775 |
} |
| 776 |
|
| 777 |
static int s_aws_mqtt5_encoder_begin_publish(struct aws_mqtt5_encoder *encoder, const void *view) { |
| 778 |
|
| 779 |
|
| 780 |
struct aws_mqtt5_packet_publish_view local_publish_view = *((const struct aws_mqtt5_packet_publish_view *)view); |
| 781 |
|
| 782 |
uint16_t outbound_topic_alias = 0; |
| 783 |
struct aws_byte_cursor outbound_topic; |
| 784 |
|
| 785 |
if (encoder->topic_alias_resolver != NULL) { |
| 786 |
AWS_ZERO_STRUCT(outbound_topic); |
| 787 |
if (aws_mqtt5_outbound_topic_alias_resolver_resolve_outbound_publish( |
| 788 |
encoder->topic_alias_resolver, &local_publish_view, &outbound_topic_alias, &outbound_topic)) { |
| 789 |
int error_code = aws_last_error(); |
| 790 |
AWS_LOGF_ERROR( |
| 791 |
AWS_LS_MQTT5_GENERAL, |
| 792 |
"(%p) mqtt5 client encoder - failed to perform outbound topic alias resolution on PUBLISH packet with " |
| 793 |
"error " |
| 794 |
"%d(%s)", |
| 795 |
(void *)encoder->config.client, |
| 796 |
error_code, |
| 797 |
aws_error_debug_str(error_code)); |
| 798 |
return AWS_OP_ERR; |
| 799 |
} |
| 800 |
|
| 801 |
local_publish_view.topic = outbound_topic; |
| 802 |
if (outbound_topic_alias != 0) { |
| 803 |
AWS_LOGF_DEBUG( |
| 804 |
AWS_LS_MQTT5_GENERAL, |
| 805 |
"(%p) mqtt5 client encoder - PUBLISH packet using topic alias value %" PRIu16, |
| 806 |
(void *)encoder->config.client, |
| 807 |
outbound_topic_alias); |
| 808 |
if (outbound_topic.len == 0) { |
| 809 |
AWS_LOGF_DEBUG( |
| 810 |
AWS_LS_MQTT5_GENERAL, |
| 811 |
"(%p) mqtt5 client encoder - PUBLISH packet dropping topic field for previously established alias", |
| 812 |
(void *)encoder->config.client); |
| 813 |
} |
| 814 |
local_publish_view.topic_alias = &outbound_topic_alias; |
| 815 |
} else { |
| 816 |
AWS_FATAL_ASSERT(local_publish_view.topic.len > 0); |
| 817 |
AWS_LOGF_DEBUG( |
| 818 |
AWS_LS_MQTT5_GENERAL, |
| 819 |
"(%p) mqtt5 client encoder - PUBLISH packet not using a topic alias", |
| 820 |
(void *)encoder->config.client); |
| 821 |
local_publish_view.topic_alias = NULL; |
| 822 |
} |
| 823 |
} |
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
const struct aws_mqtt5_packet_publish_view *publish_view = &local_publish_view; |
| 832 |
|
| 833 |
size_t total_remaining_length = 0; |
| 834 |
size_t publish_properties_length = 0; |
| 835 |
|
| 836 |
if (s_compute_publish_variable_length_fields(publish_view, &total_remaining_length, &publish_properties_length)) { |
| 837 |
int error_code = aws_last_error(); |
| 838 |
AWS_LOGF_ERROR( |
| 839 |
AWS_LS_MQTT5_GENERAL, |
| 840 |
"(%p) mqtt5 client encoder - failed to compute variable length values for PUBLISH packet with error " |
| 841 |
"%d(%s)", |
| 842 |
(void *)encoder->config.client, |
| 843 |
error_code, |
| 844 |
aws_error_debug_str(error_code)); |
| 845 |
return AWS_OP_ERR; |
| 846 |
} |
| 847 |
|
| 848 |
AWS_LOGF_DEBUG( |
| 849 |
AWS_LS_MQTT5_GENERAL, |
| 850 |
"(%p) mqtt5 client encoder - setting up encode for a PUBLISH packet with remaining length %zu", |
| 851 |
(void *)encoder->config.client, |
| 852 |
total_remaining_length); |
| 853 |
|
| 854 |
uint32_t total_remaining_length_u32 = (uint32_t)total_remaining_length; |
| 855 |
uint32_t publish_property_length_u32 = (uint32_t)publish_properties_length; |
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
uint8_t flags = 0; |
| 868 |
|
| 869 |
if (publish_view->duplicate) { |
| 870 |
flags |= 1 << 3; |
| 871 |
} |
| 872 |
|
| 873 |
flags |= ((uint8_t)publish_view->qos) << 1; |
| 874 |
|
| 875 |
if (publish_view->retain) { |
| 876 |
flags |= 1; |
| 877 |
} |
| 878 |
|
| 879 |
ADD_ENCODE_STEP_U8(encoder, aws_mqtt5_compute_fixed_header_byte1(AWS_MQTT5_PT_PUBLISH, flags)); |
| 880 |
|
| 881 |
ADD_ENCODE_STEP_VLI(encoder, total_remaining_length_u32); |
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
ADD_ENCODE_STEP_LENGTH_PREFIXED_CURSOR(encoder, publish_view->topic); |
| 892 |
if (publish_view->qos != AWS_MQTT5_QOS_AT_MOST_ONCE) { |
| 893 |
ADD_ENCODE_STEP_U16(encoder, (uint16_t)publish_view->packet_id); |
| 894 |
} |
| 895 |
ADD_ENCODE_STEP_VLI(encoder, publish_property_length_u32); |
| 896 |
|
| 897 |
ADD_ENCODE_STEP_OPTIONAL_U8_PROPERTY( |
| 898 |
encoder, AWS_MQTT5_PROPERTY_TYPE_PAYLOAD_FORMAT_INDICATOR, publish_view->payload_format); |
| 899 |
ADD_ENCODE_STEP_OPTIONAL_U32_PROPERTY( |
| 900 |
encoder, AWS_MQTT5_PROPERTY_TYPE_MESSAGE_EXPIRY_INTERVAL, publish_view->message_expiry_interval_seconds); |
| 901 |
ADD_ENCODE_STEP_OPTIONAL_U16_PROPERTY(encoder, AWS_MQTT5_PROPERTY_TYPE_TOPIC_ALIAS, publish_view->topic_alias); |
| 902 |
ADD_ENCODE_STEP_OPTIONAL_CURSOR_PROPERTY( |
| 903 |
encoder, AWS_MQTT5_PROPERTY_TYPE_RESPONSE_TOPIC, publish_view->response_topic); |
| 904 |
ADD_ENCODE_STEP_OPTIONAL_CURSOR_PROPERTY( |
| 905 |
encoder, AWS_MQTT5_PROPERTY_TYPE_CORRELATION_DATA, publish_view->correlation_data); |
| 906 |
|
| 907 |
for (size_t i = 0; i < publish_view->subscription_identifier_count; ++i) { |
| 908 |
ADD_ENCODE_STEP_OPTIONAL_VLI_PROPERTY( |
| 909 |
encoder, AWS_MQTT5_PROPERTY_TYPE_SUBSCRIPTION_IDENTIFIER, &publish_view->subscription_identifiers[i]); |
| 910 |
} |
| 911 |
|
| 912 |
ADD_ENCODE_STEP_OPTIONAL_CURSOR_PROPERTY(encoder, AWS_MQTT5_PROPERTY_TYPE_CONTENT_TYPE, publish_view->content_type); |
| 913 |
|
| 914 |
aws_mqtt5_add_user_property_encoding_steps( |
| 915 |
encoder, publish_view->user_properties, publish_view->user_property_count); |
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
if (publish_view->payload.len > 0) { |
| 922 |
ADD_ENCODE_STEP_CURSOR(encoder, publish_view->payload); |
| 923 |
} |
| 924 |
|
| 925 |
return AWS_OP_SUCCESS; |
| 926 |
} |
| 927 |
|
| 928 |
static int s_compute_puback_variable_length_fields( |
| 929 |
const struct aws_mqtt5_packet_puback_view *puback_view, |
| 930 |
size_t *total_remaining_length, |
| 931 |
size_t *puback_properties_length) { |
| 932 |
|
| 933 |
size_t local_property_length = |
| 934 |
aws_mqtt5_compute_user_property_encode_length(puback_view->user_properties, puback_view->user_property_count); |
| 935 |
|
| 936 |
ADD_OPTIONAL_CURSOR_PROPERTY_LENGTH(puback_view->reason_string, local_property_length); |
| 937 |
|
| 938 |
*puback_properties_length = (uint32_t)local_property_length; |
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
if (local_property_length == 0) { |
| 952 |
if (puback_view->reason_code == AWS_MQTT5_PARC_SUCCESS) { |
| 953 |
*total_remaining_length = 2; |
| 954 |
} else { |
| 955 |
*total_remaining_length = 3; |
| 956 |
} |
| 957 |
return AWS_OP_SUCCESS; |
| 958 |
} |
| 959 |
|
| 960 |
size_t variable_property_length_size = 0; |
| 961 |
if (aws_mqtt5_get_variable_length_encode_size(local_property_length, &variable_property_length_size)) { |
| 962 |
return AWS_OP_ERR; |
| 963 |
} |
| 964 |
|
| 965 |
*total_remaining_length = variable_property_length_size + 3 + local_property_length; |
| 966 |
|
| 967 |
return AWS_OP_SUCCESS; |
| 968 |
} |
| 969 |
|
| 970 |
static int s_aws_mqtt5_encoder_begin_puback(struct aws_mqtt5_encoder *encoder, const void *view) { |
| 971 |
const struct aws_mqtt5_packet_puback_view *puback_view = view; |
| 972 |
|
| 973 |
size_t total_remaining_length = 0; |
| 974 |
size_t puback_properties_length = 0; |
| 975 |
|
| 976 |
if (s_compute_puback_variable_length_fields(puback_view, &total_remaining_length, &puback_properties_length)) { |
| 977 |
int error_code = aws_last_error(); |
| 978 |
AWS_LOGF_ERROR( |
| 979 |
AWS_LS_MQTT5_GENERAL, |
| 980 |
"(%p) mqtt5 client encoder - failed to compute variable length values for PUBACK packet with error " |
| 981 |
"%d(%s)", |
| 982 |
(void *)encoder->config.client, |
| 983 |
error_code, |
| 984 |
aws_error_debug_str(error_code)); |
| 985 |
return AWS_OP_ERR; |
| 986 |
} |
| 987 |
|
| 988 |
AWS_LOGF_DEBUG( |
| 989 |
AWS_LS_MQTT5_GENERAL, |
| 990 |
"(%p) mqtt5 client encoder - setting up encode for a PUBACK packet with remaining length %zu", |
| 991 |
(void *)encoder->config.client, |
| 992 |
total_remaining_length); |
| 993 |
|
| 994 |
uint32_t total_remaining_length_u32 = (uint32_t)total_remaining_length; |
| 995 |
uint32_t puback_property_length_u32 = (uint32_t)puback_properties_length; |
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
ADD_ENCODE_STEP_U8(encoder, aws_mqtt5_compute_fixed_header_byte1(AWS_MQTT5_PT_PUBACK, 0)); |
| 1006 |
ADD_ENCODE_STEP_VLI(encoder, total_remaining_length_u32); |
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
ADD_ENCODE_STEP_U16(encoder, (uint16_t)puback_view->packet_id); |
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
if (total_remaining_length == 2) { |
| 1021 |
return AWS_OP_SUCCESS; |
| 1022 |
} |
| 1023 |
|
| 1024 |
ADD_ENCODE_STEP_U8(encoder, puback_view->reason_code); |
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
if (total_remaining_length < 4) { |
| 1031 |
return AWS_OP_SUCCESS; |
| 1032 |
} |
| 1033 |
|
| 1034 |
ADD_ENCODE_STEP_VLI(encoder, puback_property_length_u32); |
| 1035 |
ADD_ENCODE_STEP_OPTIONAL_CURSOR_PROPERTY( |
| 1036 |
encoder, AWS_MQTT5_PROPERTY_TYPE_REASON_STRING, puback_view->reason_string); |
| 1037 |
aws_mqtt5_add_user_property_encoding_steps(encoder, puback_view->user_properties, puback_view->user_property_count); |
| 1038 |
|
| 1039 |
return AWS_OP_SUCCESS; |
| 1040 |
} |
| 1041 |
|
| 1042 |
static enum aws_mqtt5_encoding_result s_execute_encode_step( |
| 1043 |
struct aws_mqtt5_encoder *encoder, |
| 1044 |
struct aws_mqtt5_encoding_step *step, |
| 1045 |
struct aws_byte_buf *buffer) { |
| 1046 |
size_t buffer_room = buffer->capacity - buffer->len; |
| 1047 |
|
| 1048 |
switch (step->type) { |
| 1049 |
case AWS_MQTT5_EST_U8: |
| 1050 |
if (buffer_room < 1) { |
| 1051 |
return AWS_MQTT5_ER_OUT_OF_ROOM; |
| 1052 |
} |
| 1053 |
|
| 1054 |
aws_byte_buf_write_u8(buffer, step->value.value_u8); |
| 1055 |
|
| 1056 |
return AWS_MQTT5_ER_FINISHED; |
| 1057 |
|
| 1058 |
case AWS_MQTT5_EST_U16: |
| 1059 |
if (buffer_room < 2) { |
| 1060 |
return AWS_MQTT5_ER_OUT_OF_ROOM; |
| 1061 |
} |
| 1062 |
|
| 1063 |
aws_byte_buf_write_be16(buffer, step->value.value_u16); |
| 1064 |
|
| 1065 |
return AWS_MQTT5_ER_FINISHED; |
| 1066 |
|
| 1067 |
case AWS_MQTT5_EST_U32: |
| 1068 |
if (buffer_room < 4) { |
| 1069 |
return AWS_MQTT5_ER_OUT_OF_ROOM; |
| 1070 |
} |
| 1071 |
|
| 1072 |
aws_byte_buf_write_be32(buffer, step->value.value_u32); |
| 1073 |
|
| 1074 |
return AWS_MQTT5_ER_FINISHED; |
| 1075 |
|
| 1076 |
case AWS_MQTT5_EST_VLI: |
| 1077 |
|
| 1078 |
if (buffer_room < 4) { |
| 1079 |
return AWS_MQTT5_ER_OUT_OF_ROOM; |
| 1080 |
} |
| 1081 |
|
| 1082 |
|
| 1083 |
aws_mqtt5_encode_variable_length_integer(buffer, step->value.value_u32); |
| 1084 |
|
| 1085 |
return AWS_MQTT5_ER_FINISHED; |
| 1086 |
|
| 1087 |
case AWS_MQTT5_EST_CURSOR: |
| 1088 |
if (buffer_room < 1) { |
| 1089 |
return AWS_MQTT5_ER_OUT_OF_ROOM; |
| 1090 |
} |
| 1091 |
|
| 1092 |
aws_byte_buf_write_to_capacity(buffer, &step->value.value_cursor); |
| 1093 |
|
| 1094 |
return (step->value.value_cursor.len == 0) ? AWS_MQTT5_ER_FINISHED : AWS_MQTT5_ER_OUT_OF_ROOM; |
| 1095 |
|
| 1096 |
case AWS_MQTT5_EST_STREAM: |
| 1097 |
while (buffer->len < buffer->capacity) { |
| 1098 |
if (aws_input_stream_read(step->value.value_stream, buffer)) { |
| 1099 |
int error_code = aws_last_error(); |
| 1100 |
AWS_LOGF_ERROR( |
| 1101 |
AWS_LS_MQTT5_CLIENT, |
| 1102 |
"id=%p: failed to read from stream with error %d(%s)", |
| 1103 |
(void *)encoder->config.client, |
| 1104 |
error_code, |
| 1105 |
aws_error_debug_str(error_code)); |
| 1106 |
return AWS_MQTT5_ER_ERROR; |
| 1107 |
} |
| 1108 |
|
| 1109 |
struct aws_stream_status status; |
| 1110 |
if (aws_input_stream_get_status(step->value.value_stream, &status)) { |
| 1111 |
int error_code = aws_last_error(); |
| 1112 |
AWS_LOGF_ERROR( |
| 1113 |
AWS_LS_MQTT5_CLIENT, |
| 1114 |
"id=%p: failed to query stream status with error %d(%s)", |
| 1115 |
(void *)encoder->config.client, |
| 1116 |
error_code, |
| 1117 |
aws_error_debug_str(error_code)); |
| 1118 |
return AWS_MQTT5_ER_ERROR; |
| 1119 |
} |
| 1120 |
|
| 1121 |
if (status.is_end_of_stream) { |
| 1122 |
return AWS_MQTT5_ER_FINISHED; |
| 1123 |
} |
| 1124 |
} |
| 1125 |
|
| 1126 |
if (buffer->len == buffer->capacity) { |
| 1127 |
return AWS_MQTT5_ER_OUT_OF_ROOM; |
| 1128 |
} |
| 1129 |
|
| 1130 |
|
| 1131 |
} |
| 1132 |
|
| 1133 |
|
| 1134 |
AWS_LOGF_ERROR(AWS_LS_MQTT5_CLIENT, "id=%p: encoder reached an unreachable state", (void *)encoder->config.client); |
| 1135 |
aws_raise_error(AWS_ERROR_INVALID_STATE); |
| 1136 |
return AWS_MQTT5_ER_ERROR; |
| 1137 |
} |
| 1138 |
|
| 1139 |
enum aws_mqtt5_encoding_result aws_mqtt5_encoder_encode_to_buffer( |
| 1140 |
struct aws_mqtt5_encoder *encoder, |
| 1141 |
struct aws_byte_buf *buffer) { |
| 1142 |
|
| 1143 |
enum aws_mqtt5_encoding_result result = AWS_MQTT5_ER_FINISHED; |
| 1144 |
size_t step_count = aws_array_list_length(&encoder->encoding_steps); |
| 1145 |
while (result == AWS_MQTT5_ER_FINISHED && encoder->current_encoding_step_index < step_count) { |
| 1146 |
struct aws_mqtt5_encoding_step *step = NULL; |
| 1147 |
aws_array_list_get_at_ptr(&encoder->encoding_steps, (void **)&step, encoder->current_encoding_step_index); |
| 1148 |
|
| 1149 |
result = s_execute_encode_step(encoder, step, buffer); |
| 1150 |
if (result == AWS_MQTT5_ER_FINISHED) { |
| 1151 |
encoder->current_encoding_step_index++; |
| 1152 |
} |
| 1153 |
} |
| 1154 |
|
| 1155 |
if (result == AWS_MQTT5_ER_FINISHED) { |
| 1156 |
AWS_LOGF_DEBUG( |
| 1157 |
AWS_LS_MQTT5_CLIENT, "id=%p: finished encoding current operation", (void *)encoder->config.client); |
| 1158 |
aws_mqtt5_encoder_reset(encoder); |
| 1159 |
} |
| 1160 |
|
| 1161 |
return result; |
| 1162 |
} |
| 1163 |
|
| 1164 |
static struct aws_mqtt5_encoder_function_table s_aws_mqtt5_encoder_default_function_table = { |
| 1165 |
.encoders_by_packet_type = |
| 1166 |
{ |
| 1167 |
NULL, |
| 1168 |
&s_aws_mqtt5_encoder_begin_connect, |
| 1169 |
NULL, |
| 1170 |
&s_aws_mqtt5_encoder_begin_publish, |
| 1171 |
&s_aws_mqtt5_encoder_begin_puback, |
| 1172 |
NULL, |
| 1173 |
NULL, |
| 1174 |
NULL, |
| 1175 |
&s_aws_mqtt5_encoder_begin_subscribe, |
| 1176 |
NULL, |
| 1177 |
&s_aws_mqtt5_encoder_begin_unsubscribe, |
| 1178 |
NULL, |
| 1179 |
&s_aws_mqtt5_encoder_begin_pingreq, |
| 1180 |
NULL, |
| 1181 |
&s_aws_mqtt5_encoder_begin_disconnect, |
| 1182 |
NULL |
| 1183 |
}, |
| 1184 |
}; |
| 1185 |
|
| 1186 |
const struct aws_mqtt5_encoder_function_table *g_aws_mqtt5_encoder_default_function_table = |
| 1187 |
&s_aws_mqtt5_encoder_default_function_table; |
| 1188 |
|
| 1189 |
int aws_mqtt5_encoder_init( |
| 1190 |
struct aws_mqtt5_encoder *encoder, |
| 1191 |
struct aws_allocator *allocator, |
| 1192 |
struct aws_mqtt5_encoder_options *options) { |
| 1193 |
AWS_ZERO_STRUCT(*encoder); |
| 1194 |
|
| 1195 |
encoder->config = *options; |
| 1196 |
if (encoder->config.encoders == NULL) { |
| 1197 |
encoder->config.encoders = &s_aws_mqtt5_encoder_default_function_table; |
| 1198 |
} |
| 1199 |
|
| 1200 |
if (aws_array_list_init_dynamic( |
| 1201 |
&encoder->encoding_steps, allocator, INITIAL_ENCODING_STEP_COUNT, sizeof(struct aws_mqtt5_encoding_step))) { |
| 1202 |
return AWS_OP_ERR; |
| 1203 |
} |
| 1204 |
|
| 1205 |
return AWS_OP_SUCCESS; |
| 1206 |
} |
| 1207 |
|
| 1208 |
void aws_mqtt5_encoder_clean_up(struct aws_mqtt5_encoder *encoder) { |
| 1209 |
aws_array_list_clean_up(&encoder->encoding_steps); |
| 1210 |
} |
| 1211 |
|
| 1212 |
void aws_mqtt5_encoder_reset(struct aws_mqtt5_encoder *encoder) { |
| 1213 |
aws_array_list_clear(&encoder->encoding_steps); |
| 1214 |
encoder->current_encoding_step_index = 0; |
| 1215 |
} |
| 1216 |
|
| 1217 |
int aws_mqtt5_encoder_append_packet_encoding( |
| 1218 |
struct aws_mqtt5_encoder *encoder, |
| 1219 |
enum aws_mqtt5_packet_type packet_type, |
| 1220 |
const void *packet_view) { |
| 1221 |
aws_mqtt5_encode_begin_packet_type_fn *encoding_fn = encoder->config.encoders->encoders_by_packet_type[packet_type]; |
| 1222 |
if (encoding_fn == NULL) { |
| 1223 |
return aws_raise_error(AWS_ERROR_MQTT5_ENCODE_FAILURE); |
| 1224 |
} |
| 1225 |
|
| 1226 |
return (*encoding_fn)(encoder, packet_view); |
| 1227 |
} |
| 1228 |
|
| 1229 |
static int s_compute_packet_size(size_t total_remaining_length, size_t *packet_size) { |
| 1230 |
|
| 1231 |
size_t encode_size = 0; |
| 1232 |
if (aws_mqtt5_get_variable_length_encode_size(total_remaining_length, &encode_size)) { |
| 1233 |
return AWS_OP_ERR; |
| 1234 |
} |
| 1235 |
|
| 1236 |
size_t prefix = (size_t)1 + encode_size; |
| 1237 |
|
| 1238 |
if (aws_add_size_checked(prefix, total_remaining_length, packet_size)) { |
| 1239 |
return AWS_OP_ERR; |
| 1240 |
} |
| 1241 |
|
| 1242 |
return AWS_OP_SUCCESS; |
| 1243 |
} |
| 1244 |
|
| 1245 |
int aws_mqtt5_packet_view_get_encoded_size( |
| 1246 |
enum aws_mqtt5_packet_type packet_type, |
| 1247 |
const void *packet_view, |
| 1248 |
size_t *packet_size) { |
| 1249 |
size_t total_remaining_length = 0; |
| 1250 |
size_t properties_length = 0; |
| 1251 |
|
| 1252 |
if (packet_type == AWS_MQTT5_PT_PINGREQ) { |
| 1253 |
*packet_size = AWS_MQTT5_PINGREQ_ENCODED_SIZE; |
| 1254 |
return AWS_OP_SUCCESS; |
| 1255 |
} |
| 1256 |
|
| 1257 |
switch (packet_type) { |
| 1258 |
case AWS_MQTT5_PT_PUBLISH: |
| 1259 |
if (s_compute_publish_variable_length_fields(packet_view, &total_remaining_length, &properties_length)) { |
| 1260 |
return AWS_OP_ERR; |
| 1261 |
} |
| 1262 |
break; |
| 1263 |
|
| 1264 |
case AWS_MQTT5_PT_SUBSCRIBE: |
| 1265 |
if (s_compute_subscribe_variable_length_fields(packet_view, &total_remaining_length, &properties_length)) { |
| 1266 |
return AWS_OP_ERR; |
| 1267 |
} |
| 1268 |
break; |
| 1269 |
|
| 1270 |
case AWS_MQTT5_PT_UNSUBSCRIBE: |
| 1271 |
if (s_compute_unsubscribe_variable_length_fields( |
| 1272 |
packet_view, &total_remaining_length, &properties_length)) { |
| 1273 |
return AWS_OP_ERR; |
| 1274 |
} |
| 1275 |
break; |
| 1276 |
|
| 1277 |
case AWS_MQTT5_PT_DISCONNECT: |
| 1278 |
if (s_compute_disconnect_variable_length_fields(packet_view, &total_remaining_length, &properties_length)) { |
| 1279 |
return AWS_OP_ERR; |
| 1280 |
} |
| 1281 |
break; |
| 1282 |
|
| 1283 |
case AWS_MQTT5_PT_PUBACK: |
| 1284 |
if (s_compute_puback_variable_length_fields(packet_view, &total_remaining_length, &properties_length)) { |
| 1285 |
return AWS_OP_ERR; |
| 1286 |
} |
| 1287 |
break; |
| 1288 |
|
| 1289 |
default: |
| 1290 |
return aws_raise_error(AWS_ERROR_MQTT5_ENCODE_SIZE_UNSUPPORTED_PACKET_TYPE); |
| 1291 |
} |
| 1292 |
|
| 1293 |
return s_compute_packet_size(total_remaining_length, packet_size); |
| 1294 |
} |
| 1295 |
|
| 1296 |
void aws_mqtt5_encoder_set_outbound_topic_alias_resolver( |
| 1297 |
struct aws_mqtt5_encoder *encoder, |
| 1298 |
struct aws_mqtt5_outbound_topic_alias_resolver *resolver) { |
| 1299 |
|
| 1300 |
encoder->topic_alias_resolver = resolver; |
| 1301 |
} |