Debian Codegraph @

crt/aws-c-mqtt/source/packets.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/mqtt/private/packets.h>
7
8 enum { S_PROTOCOL_LEVEL = 4 };
9 enum { S_BIT_1_FLAGS = 0x2 };
10
11 static struct aws_byte_cursor s_protocol_name = {
12 .ptr = (uint8_t *)"MQTT",
13 .len = 4,
14 };
15
16 static size_t s_sizeof_encoded_buffer(struct aws_byte_cursor *buf) {
17 return sizeof(uint16_t) + buf->len;
18 }
19
20 static int s_encode_buffer(struct aws_byte_buf *buf, const struct aws_byte_cursor cur) {
21
22 AWS_PRECONDITION(buf);
23 AWS_PRECONDITION(aws_byte_cursor_is_valid(&cur));
24
25 /* Make sure the buffer isn't too big */
26 if (cur.len > UINT16_MAX) {
27 return aws_raise_error(AWS_ERROR_MQTT_BUFFER_TOO_BIG);
28 }
29
30 /* Write the length */
31 if (!aws_byte_buf_write_be16(buf, (uint16_t)cur.len)) {
32 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
33 }
34
35 /* Write the data */
36 if (!aws_byte_buf_write(buf, cur.ptr, cur.len)) {
37 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
38 }
39
40 return AWS_OP_SUCCESS;
41 }
42
43 static int s_decode_buffer(struct aws_byte_cursor *cur, struct aws_byte_cursor *buf) {
44
45 AWS_PRECONDITION(cur);
46 AWS_PRECONDITION(buf);
47
48 /* Read the length */
49 uint16_t len;
50 if (!aws_byte_cursor_read_be16(cur, &len)) {
51 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
52 }
53
54 /* Store the data */
55 *buf = aws_byte_cursor_advance(cur, len);
56
57 return AWS_OP_SUCCESS;
58 }
59
60 /*****************************************************************************/
61 /* Ack without payload */
62
63 static void s_ack_init(struct aws_mqtt_packet_ack *packet, enum aws_mqtt_packet_type type, uint16_t packet_identifier) {
64
65 AWS_PRECONDITION(packet);
66
67 AWS_ZERO_STRUCT(*packet);
68
69 packet->fixed_header.packet_type = type;
70 packet->fixed_header.remaining_length = sizeof(uint16_t);
71
72 packet->packet_identifier = packet_identifier;
73 }
74
75 int aws_mqtt_packet_ack_encode(struct aws_byte_buf *buf, const struct aws_mqtt_packet_ack *packet) {
76
77 AWS_PRECONDITION(buf);
78 AWS_PRECONDITION(packet);
79
80 /*************************************************************************/
81 /* Fixed Header */
82
83 if (aws_mqtt_fixed_header_encode(buf, &packet->fixed_header)) {
84 return AWS_OP_ERR;
85 }
86
87 /*************************************************************************/
88 /* Variable Header */
89
90 /* Write packet identifier */
91 if (!aws_byte_buf_write_be16(buf, packet->packet_identifier)) {
92 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
93 }
94
95 return AWS_OP_SUCCESS;
96 }
97
98 int aws_mqtt_packet_ack_decode(struct aws_byte_cursor *cur, struct aws_mqtt_packet_ack *packet) {
99
100 AWS_PRECONDITION(cur);
101 AWS_PRECONDITION(packet);
102
103 /*************************************************************************/
104 /* Fixed Header */
105
106 if (aws_mqtt_fixed_header_decode(cur, &packet->fixed_header)) {
107 return AWS_OP_ERR;
108 }
109
110 /* Validate flags */
111 if (packet->fixed_header.flags != (aws_mqtt_packet_has_flags(&packet->fixed_header) ? S_BIT_1_FLAGS : 0U)) {
112
113 return aws_raise_error(AWS_ERROR_MQTT_INVALID_RESERVED_BITS);
114 }
115
116 /*************************************************************************/
117 /* Variable Header */
118
119 /* Read packet identifier */
120 if (!aws_byte_cursor_read_be16(cur, &packet->packet_identifier)) {
121 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
122 }
123
124 return AWS_OP_SUCCESS;
125 }
126
127 /*****************************************************************************/
128 /* Connect */
129
130 int aws_mqtt_packet_connect_init(
131 struct aws_mqtt_packet_connect *packet,
132 struct aws_byte_cursor client_identifier,
133 bool clean_session,
134 uint16_t keep_alive) {
135
136 AWS_PRECONDITION(packet);
137 AWS_PRECONDITION(client_identifier.len > 0);
138
139 AWS_ZERO_STRUCT(*packet);
140
141 packet->fixed_header.packet_type = AWS_MQTT_PACKET_CONNECT;
142 /* [MQTT-3.1.1] */
143 packet->fixed_header.remaining_length = 10 + s_sizeof_encoded_buffer(&client_identifier);
144
145 packet->client_identifier = client_identifier;
146 packet->clean_session = clean_session;
147 packet->keep_alive_timeout = keep_alive;
148
149 return AWS_OP_SUCCESS;
150 }
151
152 int aws_mqtt_packet_connect_add_credentials(
153 struct aws_mqtt_packet_connect *packet,
154 struct aws_byte_cursor username,
155 struct aws_byte_cursor password) {
156
157 AWS_PRECONDITION(packet);
158 AWS_PRECONDITION(username.len > 0);
159
160 if (!packet->has_username) {
161 /* If not already username, add size of length field */
162 packet->fixed_header.remaining_length += 2;
163 }
164
165 /* Add change in size to remaining_length */
166 packet->fixed_header.remaining_length += username.len - packet->username.len;
167 packet->has_username = true;
168
169 packet->username = username;
170
171 if (password.len > 0) {
172
173 if (!packet->has_password) {
174 /* If not already password, add size of length field */
175 packet->fixed_header.remaining_length += 2;
176 }
177
178 /* Add change in size to remaining_length */
179 packet->fixed_header.remaining_length += password.len - packet->password.len;
180 packet->has_password = true;
181
182 packet->password = password;
183 }
184
185 return AWS_OP_SUCCESS;
186 }
187
188 int aws_mqtt_packet_connect_add_will(
189 struct aws_mqtt_packet_connect *packet,
190 struct aws_byte_cursor topic,
191 enum aws_mqtt_qos qos,
192 bool retain,
193 struct aws_byte_cursor payload) {
194
195 packet->has_will = true;
196 packet->will_topic = topic;
197 packet->will_qos = qos;
198 packet->will_retain = retain;
199 packet->will_message = payload;
200
201 packet->fixed_header.remaining_length += s_sizeof_encoded_buffer(&topic) + s_sizeof_encoded_buffer(&payload);
202
203 return AWS_OP_SUCCESS;
204 }
205
206 int aws_mqtt_packet_connect_encode(struct aws_byte_buf *buf, const struct aws_mqtt_packet_connect *packet) {
207
208 AWS_PRECONDITION(buf);
209 AWS_PRECONDITION(packet);
210
211 /* Do validation */
212 if (packet->has_password && !packet->has_username) {
213
214 return aws_raise_error(AWS_ERROR_MQTT_INVALID_CREDENTIALS);
215 }
216
217 /*************************************************************************/
218 /* Fixed Header */
219
220 if (aws_mqtt_fixed_header_encode(buf, &packet->fixed_header)) {
221 return AWS_OP_ERR;
222 }
223
224 /*************************************************************************/
225 /* Variable Header */
226
227 /* Write protocol name */
228 if (s_encode_buffer(buf, s_protocol_name)) {
229 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
230 }
231
232 /* Write protocol level */
233 if (!aws_byte_buf_write_u8(buf, S_PROTOCOL_LEVEL)) {
234 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
235 }
236
237 /* Write connect flags [MQTT-3.1.2.3] */
238 uint8_t connect_flags = (uint8_t)(packet->clean_session << 1 | packet->has_will << 2 | packet->will_qos << 3 |
239 packet->will_retain << 5 | packet->has_password << 6 | packet->has_username << 7);
240
241 if (!aws_byte_buf_write_u8(buf, connect_flags)) {
242 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
243 }
244
245 /* Write keep alive */
246 if (!aws_byte_buf_write_be16(buf, packet->keep_alive_timeout)) {
247 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
248 }
249
250 /*************************************************************************/
251 /* Payload */
252
253 /* Client identifier is required, write it */
254 if (s_encode_buffer(buf, packet->client_identifier)) {
255 return AWS_OP_ERR;
256 }
257
258 /* Write will */
259 if (packet->has_will) {
260 if (s_encode_buffer(buf, packet->will_topic)) {
261 return AWS_OP_ERR;
262 }
263 if (s_encode_buffer(buf, packet->will_message)) {
264 return AWS_OP_ERR;
265 }
266 }
267
268 /* Write username */
269 if (packet->has_username) {
270 if (s_encode_buffer(buf, packet->username)) {
271 return AWS_OP_ERR;
272 }
273 }
274
275 /* Write password */
276 if (packet->has_password) {
277 if (s_encode_buffer(buf, packet->password)) {
278 return AWS_OP_ERR;
279 }
280 }
281
282 return AWS_OP_SUCCESS;
283 }
284
285 int aws_mqtt_packet_connect_decode(struct aws_byte_cursor *cur, struct aws_mqtt_packet_connect *packet) {
286
287 AWS_PRECONDITION(cur);
288 AWS_PRECONDITION(packet);
289
290 /*************************************************************************/
291 /* Fixed Header */
292
293 if (aws_mqtt_fixed_header_decode(cur, &packet->fixed_header)) {
294 return AWS_OP_ERR;
295 }
296
297 /*************************************************************************/
298 /* Variable Header */
299
300 /* Check protocol name */
301 struct aws_byte_cursor protocol_name = {
302 .ptr = NULL,
303 .len = 0,
304 };
305 if (s_decode_buffer(cur, &protocol_name)) {
306 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
307 }
308 AWS_ASSERT(protocol_name.ptr && protocol_name.len);
309 if (protocol_name.len != s_protocol_name.len) {
310 return aws_raise_error(AWS_ERROR_MQTT_UNSUPPORTED_PROTOCOL_NAME);
311 }
312 if (memcmp(protocol_name.ptr, s_protocol_name.ptr, s_protocol_name.len) != 0) {
313 return aws_raise_error(AWS_ERROR_MQTT_UNSUPPORTED_PROTOCOL_NAME);
314 }
315
316 /* Check protocol level */
317 struct aws_byte_cursor protocol_level = aws_byte_cursor_advance(cur, 1);
318 if (protocol_level.len == 0) {
319 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
320 }
321 if (*protocol_level.ptr != S_PROTOCOL_LEVEL) {
322 return aws_raise_error(AWS_ERROR_MQTT_UNSUPPORTED_PROTOCOL_LEVEL);
323 }
324
325 /* Read connect flags [MQTT-3.1.2.3] */
326 uint8_t connect_flags = 0;
327 if (!aws_byte_cursor_read_u8(cur, &connect_flags)) {
328 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
329 }
330 packet->clean_session = (connect_flags >> 1) & 0x1;
331 packet->has_will = (connect_flags >> 2) & 0x1;
332 packet->will_qos = (connect_flags >> 3) & 0x3;
333 packet->will_retain = (connect_flags >> 5) & 0x1;
334 packet->has_password = (connect_flags >> 6) & 0x1;
335 packet->has_username = (connect_flags >> 7) & 0x1;
336
337 /* Read keep alive */
338 if (!aws_byte_cursor_read_be16(cur, &packet->keep_alive_timeout)) {
339 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
340 }
341
342 /*************************************************************************/
343 /* Payload */
344
345 /* Client identifier is required, Read it */
346 if (s_decode_buffer(cur, &packet->client_identifier)) {
347 return AWS_OP_ERR;
348 }
349
350 /* Read will */
351 if (packet->has_will) {
352 if (s_decode_buffer(cur, &packet->will_topic)) {
353 return AWS_OP_ERR;
354 }
355 if (s_decode_buffer(cur, &packet->will_message)) {
356 return AWS_OP_ERR;
357 }
358 }
359
360 /* Read username */
361 if (packet->has_username) {
362 if (s_decode_buffer(cur, &packet->username)) {
363 return AWS_OP_ERR;
364 }
365 }
366
367 /* Read password */
368 if (packet->has_password) {
369 if (s_decode_buffer(cur, &packet->password)) {
370 return AWS_OP_ERR;
371 }
372 }
373
374 /* Do validation */
375 if (packet->has_password && !packet->has_username) {
376
377 return aws_raise_error(AWS_ERROR_MQTT_INVALID_CREDENTIALS);
378 }
379
380 return AWS_OP_SUCCESS;
381 }
382
383 /*****************************************************************************/
384 /* Connack */
385
386 int aws_mqtt_packet_connack_init(
387 struct aws_mqtt_packet_connack *packet,
388 bool session_present,
389 enum aws_mqtt_connect_return_code return_code) {
390
391 AWS_PRECONDITION(packet);
392
393 AWS_ZERO_STRUCT(*packet);
394
395 packet->fixed_header.packet_type = AWS_MQTT_PACKET_CONNACK;
396 packet->fixed_header.remaining_length = 1 + sizeof(packet->connect_return_code);
397
398 packet->session_present = session_present;
399 packet->connect_return_code = (uint8_t)return_code;
400
401 return AWS_OP_SUCCESS;
402 }
403
404 int aws_mqtt_packet_connack_encode(struct aws_byte_buf *buf, const struct aws_mqtt_packet_connack *packet) {
405
406 AWS_PRECONDITION(buf);
407 AWS_PRECONDITION(packet);
408
409 /*************************************************************************/
410 /* Fixed Header */
411
412 if (aws_mqtt_fixed_header_encode(buf, &packet->fixed_header)) {
413 return AWS_OP_ERR;
414 }
415
416 /*************************************************************************/
417 /* Variable Header */
418
419 /* Read connack flags */
420 uint8_t connack_flags = packet->session_present & 0x1;
421 if (!aws_byte_buf_write_u8(buf, connack_flags)) {
422 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
423 }
424
425 /* Read return code */
426 if (!aws_byte_buf_write_u8(buf, packet->connect_return_code)) {
427 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
428 }
429
430 return AWS_OP_SUCCESS;
431 }
432
433 int aws_mqtt_packet_connack_decode(struct aws_byte_cursor *cur, struct aws_mqtt_packet_connack *packet) {
434
435 AWS_PRECONDITION(cur);
436 AWS_PRECONDITION(packet);
437
438 /*************************************************************************/
439 /* Fixed Header */
440
441 if (aws_mqtt_fixed_header_decode(cur, &packet->fixed_header)) {
442 return AWS_OP_ERR;
443 }
444
445 /*************************************************************************/
446 /* Variable Header */
447
448 /* Read connack flags */
449 uint8_t connack_flags = 0;
450 if (!aws_byte_cursor_read_u8(cur, &connack_flags)) {
451 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
452 }
453 packet->session_present = connack_flags & 0x1;
454
455 /* Read return code */
456 if (!aws_byte_cursor_read_u8(cur, &packet->connect_return_code)) {
457 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
458 }
459
460 return AWS_OP_SUCCESS;
461 }
462
463 /*****************************************************************************/
464 /* Publish */
465
466 int aws_mqtt_packet_publish_init(
467 struct aws_mqtt_packet_publish *packet,
468 bool retain,
469 enum aws_mqtt_qos qos,
470 bool dup,
471 struct aws_byte_cursor topic_name,
472 uint16_t packet_identifier,
473 struct aws_byte_cursor payload) {
474
475 AWS_PRECONDITION(packet);
476 AWS_FATAL_PRECONDITION(topic_name.len > 0); /* [MQTT-4.7.3-1] */
477
478 AWS_ZERO_STRUCT(*packet);
479
480 packet->fixed_header.packet_type = AWS_MQTT_PACKET_PUBLISH;
481 packet->fixed_header.remaining_length = s_sizeof_encoded_buffer(&topic_name) + payload.len;
482
483 if (qos > 0) {
484 packet->fixed_header.remaining_length += sizeof(packet->packet_identifier);
485 }
486
487 /* [MQTT-2.2.2] */
488 uint8_t publish_flags = (uint8_t)((retain & 0x1) | (qos & 0x3) << 1 | (dup & 0x1) << 3);
489 packet->fixed_header.flags = publish_flags;
490
491 packet->topic_name = topic_name;
492 packet->packet_identifier = packet_identifier;
493 packet->payload = payload;
494
495 return AWS_OP_SUCCESS;
496 }
497
498 int aws_mqtt_packet_publish_encode(struct aws_byte_buf *buf, const struct aws_mqtt_packet_publish *packet) {
499
500 if (aws_mqtt_packet_publish_encode_headers(buf, packet)) {
501 return AWS_OP_ERR;
502 }
503
504 /*************************************************************************/
505 /* Payload */
506
507 if (!aws_byte_buf_write(buf, packet->payload.ptr, packet->payload.len)) {
508 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
509 }
510
511 return AWS_OP_SUCCESS;
512 }
513
514 int aws_mqtt_packet_publish_encode_headers(struct aws_byte_buf *buf, const struct aws_mqtt_packet_publish *packet) {
515
516 AWS_PRECONDITION(buf);
517 AWS_PRECONDITION(packet);
518
519 /*************************************************************************/
520 /* Fixed Header */
521
522 if (aws_mqtt_fixed_header_encode(buf, &packet->fixed_header)) {
523 return AWS_OP_ERR;
524 }
525
526 /*************************************************************************/
527 /* Variable Header */
528
529 /* Write topic name */
530 if (s_encode_buffer(buf, packet->topic_name)) {
531 return AWS_OP_ERR;
532 }
533
534 enum aws_mqtt_qos qos = aws_mqtt_packet_publish_get_qos(packet);
535 if (qos > 0) {
536 /* Write packet identifier */
537 if (!aws_byte_buf_write_be16(buf, packet->packet_identifier)) {
538 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
539 }
540 }
541
542 return AWS_OP_SUCCESS;
543 }
544
545 int aws_mqtt_packet_publish_decode(struct aws_byte_cursor *cur, struct aws_mqtt_packet_publish *packet) {
546
547 AWS_PRECONDITION(cur);
548 AWS_PRECONDITION(packet);
549
550 /*************************************************************************/
551 /* Fixed Header */
552
553 if (aws_mqtt_fixed_header_decode(cur, &packet->fixed_header)) {
554 return AWS_OP_ERR;
555 }
556
557 /*************************************************************************/
558 /* Variable Header */
559
560 /* Read topic name */
561 if (s_decode_buffer(cur, &packet->topic_name)) {
562 return AWS_OP_ERR;
563 }
564
565 size_t payload_size = packet->fixed_header.remaining_length - s_sizeof_encoded_buffer(&packet->topic_name);
566
567 /* Read QoS */
568 enum aws_mqtt_qos qos = aws_mqtt_packet_publish_get_qos(packet);
569 if (qos > 2) {
570 return aws_raise_error(AWS_ERROR_MQTT_PROTOCOL_ERROR);
571 }
572
573 /* Read packet identifier */
574 if (qos > 0) {
575 if (!aws_byte_cursor_read_be16(cur, &packet->packet_identifier)) {
576 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
577 }
578 payload_size -= sizeof(packet->packet_identifier);
579 } else {
580 packet->packet_identifier = 0;
581 }
582
583 /*************************************************************************/
584 /* Payload */
585 packet->payload = aws_byte_cursor_advance(cur, payload_size);
586 if (packet->payload.len != payload_size) {
587 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
588 }
589
590 return AWS_OP_SUCCESS;
591 }
592
593 bool aws_mqtt_packet_publish_get_dup(const struct aws_mqtt_packet_publish *packet) {
594 return packet->fixed_header.flags & (1 << 3); /* bit 3 */
595 }
596
597 void aws_mqtt_packet_publish_set_dup(struct aws_mqtt_packet_publish *packet) {
598 packet->fixed_header.flags |= 0x08;
599 }
600
601 enum aws_mqtt_qos aws_mqtt_packet_publish_get_qos(const struct aws_mqtt_packet_publish *packet) {
602 return (packet->fixed_header.flags >> 1) & 0x3; /* bits 2,1 */
603 }
604
605 bool aws_mqtt_packet_publish_get_retain(const struct aws_mqtt_packet_publish *packet) {
606 return packet->fixed_header.flags & 0x1; /* bit 0 */
607 }
608
609 /*****************************************************************************/
610 /* Puback */
611
612 int aws_mqtt_packet_puback_init(struct aws_mqtt_packet_ack *packet, uint16_t packet_identifier) {
613
614 s_ack_init(packet, AWS_MQTT_PACKET_PUBACK, packet_identifier);
615
616 return AWS_OP_SUCCESS;
617 }
618
619 /*****************************************************************************/
620 /* Pubrec */
621
622 int aws_mqtt_packet_pubrec_init(struct aws_mqtt_packet_ack *packet, uint16_t packet_identifier) {
623
624 s_ack_init(packet, AWS_MQTT_PACKET_PUBREC, packet_identifier);
625
626 return AWS_OP_SUCCESS;
627 }
628
629 /*****************************************************************************/
630 /* Pubrel */
631
632 int aws_mqtt_packet_pubrel_init(struct aws_mqtt_packet_ack *packet, uint16_t packet_identifier) {
633
634 s_ack_init(packet, AWS_MQTT_PACKET_PUBREL, packet_identifier);
635 packet->fixed_header.flags = S_BIT_1_FLAGS;
636
637 return AWS_OP_SUCCESS;
638 }
639
640 /*****************************************************************************/
641 /* Pubcomp */
642
643 int aws_mqtt_packet_pubcomp_init(struct aws_mqtt_packet_ack *packet, uint16_t packet_identifier) {
644
645 s_ack_init(packet, AWS_MQTT_PACKET_PUBCOMP, packet_identifier);
646
647 return AWS_OP_SUCCESS;
648 }
649
650 /*****************************************************************************/
651 /* Subscribe */
652
653 int aws_mqtt_packet_subscribe_init(
654 struct aws_mqtt_packet_subscribe *packet,
655 struct aws_allocator *allocator,
656 uint16_t packet_identifier) {
657
658 AWS_PRECONDITION(packet);
659
660 AWS_ZERO_STRUCT(*packet);
661
662 packet->fixed_header.packet_type = AWS_MQTT_PACKET_SUBSCRIBE;
663 packet->fixed_header.flags = S_BIT_1_FLAGS;
664 packet->fixed_header.remaining_length = sizeof(uint16_t);
665
666 packet->packet_identifier = packet_identifier;
667
668 if (aws_array_list_init_dynamic(&packet->topic_filters, allocator, 1, sizeof(struct aws_mqtt_subscription))) {
669 return AWS_OP_ERR;
670 }
671
672 return AWS_OP_SUCCESS;
673 }
674
675 void aws_mqtt_packet_subscribe_clean_up(struct aws_mqtt_packet_subscribe *packet) {
676
677 AWS_PRECONDITION(packet);
678
679 aws_array_list_clean_up(&packet->topic_filters);
680
681 AWS_ZERO_STRUCT(*packet);
682 }
683
684 int aws_mqtt_packet_subscribe_add_topic(
685 struct aws_mqtt_packet_subscribe *packet,
686 struct aws_byte_cursor topic_filter,
687 enum aws_mqtt_qos qos) {
688
689 AWS_PRECONDITION(packet);
690
691 /* Add to the array list */
692 struct aws_mqtt_subscription subscription;
693 subscription.topic_filter = topic_filter;
694 subscription.qos = qos;
695 if (aws_array_list_push_back(&packet->topic_filters, &subscription)) {
696 return AWS_OP_ERR;
697 }
698
699 /* Add to the remaining length */
700 packet->fixed_header.remaining_length += s_sizeof_encoded_buffer(&topic_filter) + 1;
701
702 return AWS_OP_SUCCESS;
703 }
704
705 int aws_mqtt_packet_subscribe_encode(struct aws_byte_buf *buf, const struct aws_mqtt_packet_subscribe *packet) {
706
707 AWS_PRECONDITION(buf);
708 AWS_PRECONDITION(packet);
709
710 /*************************************************************************/
711 /* Fixed Header */
712
713 if (aws_mqtt_fixed_header_encode(buf, &packet->fixed_header)) {
714 return AWS_OP_ERR;
715 }
716
717 /*************************************************************************/
718 /* Variable Header */
719
720 /* Write packet identifier */
721 if (!aws_byte_buf_write_be16(buf, packet->packet_identifier)) {
722 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
723 }
724
725 /* Write topic filters */
726 const size_t num_filters = aws_array_list_length(&packet->topic_filters);
727 for (size_t i = 0; i < num_filters; ++i) {
728
729 struct aws_mqtt_subscription *subscription;
730 if (aws_array_list_get_at_ptr(&packet->topic_filters, (void **)&subscription, i)) {
731
732 return AWS_OP_ERR;
733 }
734 s_encode_buffer(buf, subscription->topic_filter);
735
736 uint8_t eos_byte = subscription->qos & 0x3;
737 if (!aws_byte_buf_write_u8(buf, eos_byte)) {
738 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
739 }
740 }
741
742 return AWS_OP_SUCCESS;
743 }
744
745 int aws_mqtt_packet_subscribe_decode(struct aws_byte_cursor *cur, struct aws_mqtt_packet_subscribe *packet) {
746
747 AWS_PRECONDITION(cur);
748 AWS_PRECONDITION(packet);
749
750 /*************************************************************************/
751 /* Fixed Header */
752
753 if (aws_mqtt_fixed_header_decode(cur, &packet->fixed_header)) {
754 return AWS_OP_ERR;
755 }
756
757 if (packet->fixed_header.remaining_length < sizeof(uint16_t)) {
758 return aws_raise_error(AWS_ERROR_MQTT_INVALID_REMAINING_LENGTH);
759 }
760
761 /*************************************************************************/
762 /* Variable Header */
763
764 /* Read packet identifier */
765 if (!aws_byte_cursor_read_be16(cur, &packet->packet_identifier)) {
766 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
767 }
768
769 /* Read topic filters */
770 size_t remaining_length = packet->fixed_header.remaining_length - sizeof(uint16_t);
771 while (remaining_length) {
772
773 struct aws_mqtt_subscription subscription = {
774 .topic_filter = {.ptr = NULL, .len = 0},
775 .qos = 0,
776 };
777 if (s_decode_buffer(cur, &subscription.topic_filter)) {
778 return AWS_OP_ERR;
779 }
780
781 uint8_t eos_byte = 0;
782 if (!aws_byte_cursor_read_u8(cur, &eos_byte)) {
783 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
784 }
785 if ((eos_byte >> 2) != 0) {
786 return aws_raise_error(AWS_ERROR_MQTT_INVALID_RESERVED_BITS);
787 }
788 if (eos_byte == 0x3) {
789 return aws_raise_error(AWS_ERROR_MQTT_INVALID_QOS);
790 }
791 subscription.qos = eos_byte & 0x3;
792
793 aws_array_list_push_back(&packet->topic_filters, &subscription);
794
795 remaining_length -= s_sizeof_encoded_buffer(&subscription.topic_filter) + 1;
796 }
797
798 return AWS_OP_SUCCESS;
799 }
800
801 /*****************************************************************************/
802 /* Suback */
803
804 int aws_mqtt_packet_suback_init(
805 struct aws_mqtt_packet_suback *packet,
806 struct aws_allocator *allocator,
807 uint16_t packet_identifier) {
808
809 AWS_PRECONDITION(packet);
810
811 AWS_ZERO_STRUCT(*packet);
812
813 packet->fixed_header.packet_type = AWS_MQTT_PACKET_SUBACK;
814 packet->fixed_header.remaining_length = sizeof(uint16_t);
815
816 packet->packet_identifier = packet_identifier;
817
818 if (aws_array_list_init_dynamic(&packet->return_codes, allocator, 1, sizeof(uint8_t))) {
819 return AWS_OP_ERR;
820 }
821 return AWS_OP_SUCCESS;
822 }
823
824 void aws_mqtt_packet_suback_clean_up(struct aws_mqtt_packet_suback *packet) {
825
826 AWS_PRECONDITION(packet);
827
828 aws_array_list_clean_up(&packet->return_codes);
829
830 AWS_ZERO_STRUCT(*packet);
831 }
832
833 static bool s_return_code_check(uint8_t return_code) {
834 if (return_code != AWS_MQTT_QOS_FAILURE && return_code != AWS_MQTT_QOS_AT_MOST_ONCE &&
835 return_code != AWS_MQTT_QOS_AT_LEAST_ONCE && return_code != AWS_MQTT_QOS_EXACTLY_ONCE) {
836 return false;
837 }
838 return true;
839 }
840
841 int aws_mqtt_packet_suback_add_return_code(struct aws_mqtt_packet_suback *packet, uint8_t return_code) {
842
843 AWS_PRECONDITION(packet);
844 if (!(s_return_code_check(return_code))) {
845 return aws_raise_error(AWS_ERROR_MQTT_PROTOCOL_ERROR);
846 }
847
848 /* Add to the array list */
849 if (aws_array_list_push_back(&packet->return_codes, &return_code)) {
850 return AWS_OP_ERR;
851 }
852
853 /* Add to the remaining length, each return code takes one byte */
854 packet->fixed_header.remaining_length += 1;
855
856 return AWS_OP_SUCCESS;
857 }
858
859 int aws_mqtt_packet_suback_encode(struct aws_byte_buf *buf, const struct aws_mqtt_packet_suback *packet) {
860
861 AWS_PRECONDITION(buf);
862 AWS_PRECONDITION(packet);
863
864 /*************************************************************************/
865 /* Fixed Header */
866
867 if (aws_mqtt_fixed_header_encode(buf, &packet->fixed_header)) {
868 return AWS_OP_ERR;
869 }
870
871 /*************************************************************************/
872 /* Variable Header */
873
874 /* Write packet identifier */
875 if (!aws_byte_buf_write_be16(buf, packet->packet_identifier)) {
876 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
877 }
878
879 /*************************************************************************/
880 /* Payload */
881
882 /* Write topic filters */
883 const size_t num_filters = aws_array_list_length(&packet->return_codes);
884 for (size_t i = 0; i < num_filters; ++i) {
885
886 uint8_t return_code = 0;
887 if (aws_array_list_get_at(&packet->return_codes, (void *)&return_code, i)) {
888 return AWS_OP_ERR;
889 }
890 if (!aws_byte_buf_write_u8(buf, return_code)) {
891 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
892 }
893 }
894 return AWS_OP_SUCCESS;
895 }
896
897 int aws_mqtt_packet_suback_decode(struct aws_byte_cursor *cur, struct aws_mqtt_packet_suback *packet) {
898
899 AWS_PRECONDITION(cur);
900 AWS_PRECONDITION(packet);
901
902 /*************************************************************************/
903 /* Fixed Header */
904
905 if (aws_mqtt_fixed_header_decode(cur, &packet->fixed_header)) {
906 return AWS_OP_ERR;
907 }
908
909 /* Validate flags */
910 if (packet->fixed_header.flags != (aws_mqtt_packet_has_flags(&packet->fixed_header) ? S_BIT_1_FLAGS : 0U)) {
911
912 return aws_raise_error(AWS_ERROR_MQTT_INVALID_RESERVED_BITS);
913 }
914
915 /*************************************************************************/
916 /* Variable Header */
917
918 /* Read packet identifier */
919 if (!aws_byte_cursor_read_be16(cur, &packet->packet_identifier)) {
920 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
921 }
922
923 /*************************************************************************/
924 /* Payload */
925
926 /* Read return codes */
927 size_t remaining_length = packet->fixed_header.remaining_length - sizeof(uint16_t);
928 while (remaining_length) {
929
930 uint8_t return_code = 0;
931 if (!aws_byte_cursor_read_u8(cur, &return_code)) {
932 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
933 }
934 if (!(s_return_code_check(return_code))) {
935 return aws_raise_error(AWS_ERROR_MQTT_PROTOCOL_ERROR);
936 }
937
938 aws_array_list_push_back(&packet->return_codes, &return_code);
939
940 remaining_length -= 1;
941 }
942
943 return AWS_OP_SUCCESS;
944 }
945
946 /*****************************************************************************/
947 /* Unsubscribe */
948
949 int aws_mqtt_packet_unsubscribe_init(
950 struct aws_mqtt_packet_unsubscribe *packet,
951 struct aws_allocator *allocator,
952 uint16_t packet_identifier) {
953
954 AWS_PRECONDITION(packet);
955 AWS_PRECONDITION(allocator);
956
957 AWS_ZERO_STRUCT(*packet);
958
959 packet->fixed_header.packet_type = AWS_MQTT_PACKET_UNSUBSCRIBE;
960 packet->fixed_header.flags = S_BIT_1_FLAGS;
961 packet->fixed_header.remaining_length = sizeof(uint16_t);
962
963 packet->packet_identifier = packet_identifier;
964
965 if (aws_array_list_init_dynamic(&packet->topic_filters, allocator, 1, sizeof(struct aws_byte_cursor))) {
966 return AWS_OP_ERR;
967 }
968
969 return AWS_OP_SUCCESS;
970 }
971
972 void aws_mqtt_packet_unsubscribe_clean_up(struct aws_mqtt_packet_unsubscribe *packet) {
973
974 AWS_PRECONDITION(packet);
975
976 aws_array_list_clean_up(&packet->topic_filters);
977
978 AWS_ZERO_STRUCT(*packet);
979 }
980
981 int aws_mqtt_packet_unsubscribe_add_topic(
982 struct aws_mqtt_packet_unsubscribe *packet,
983 struct aws_byte_cursor topic_filter) {
984
985 AWS_PRECONDITION(packet);
986
987 /* Add to the array list */
988 if (aws_array_list_push_back(&packet->topic_filters, &topic_filter)) {
989 return AWS_OP_ERR;
990 }
991
992 /* Add to the remaining length */
993 packet->fixed_header.remaining_length += s_sizeof_encoded_buffer(&topic_filter);
994
995 return AWS_OP_SUCCESS;
996 }
997
998 int aws_mqtt_packet_unsubscribe_encode(struct aws_byte_buf *buf, const struct aws_mqtt_packet_unsubscribe *packet) {
999
1000 AWS_PRECONDITION(buf);
1001 AWS_PRECONDITION(packet);
1002
1003 /*************************************************************************/
1004 /* Fixed Header */
1005
1006 if (aws_mqtt_fixed_header_encode(buf, &packet->fixed_header)) {
1007 return AWS_OP_ERR;
1008 }
1009
1010 /*************************************************************************/
1011 /* Variable Header */
1012
1013 /* Write packet identifier */
1014 if (!aws_byte_buf_write_be16(buf, packet->packet_identifier)) {
1015 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
1016 }
1017
1018 /* Write topic filters */
1019 const size_t num_filters = aws_array_list_length(&packet->topic_filters);
1020 for (size_t i = 0; i < num_filters; ++i) {
1021
1022 struct aws_byte_cursor topic_filter = {.ptr = NULL, .len = 0};
1023 if (aws_array_list_get_at(&packet->topic_filters, (void *)&topic_filter, i)) {
1024
1025 return AWS_OP_ERR;
1026 }
1027 s_encode_buffer(buf, topic_filter);
1028 }
1029
1030 return AWS_OP_SUCCESS;
1031 }
1032
1033 int aws_mqtt_packet_unsubscribe_decode(struct aws_byte_cursor *cur, struct aws_mqtt_packet_unsubscribe *packet) {
1034
1035 AWS_PRECONDITION(cur);
1036 AWS_PRECONDITION(packet);
1037
1038 /*************************************************************************/
1039 /* Fixed Header */
1040
1041 if (aws_mqtt_fixed_header_decode(cur, &packet->fixed_header)) {
1042 return AWS_OP_ERR;
1043 }
1044
1045 if (packet->fixed_header.remaining_length < sizeof(uint16_t)) {
1046 return aws_raise_error(AWS_ERROR_MQTT_INVALID_REMAINING_LENGTH);
1047 }
1048
1049 /*************************************************************************/
1050 /* Variable Header */
1051
1052 /* Read packet identifier */
1053 if (!aws_byte_cursor_read_be16(cur, &packet->packet_identifier)) {
1054 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
1055 }
1056
1057 /* Read topic filters */
1058 size_t remaining_length = packet->fixed_header.remaining_length - sizeof(uint16_t);
1059 while (remaining_length) {
1060
1061 struct aws_byte_cursor topic_filter;
1062 AWS_ZERO_STRUCT(topic_filter);
1063 if (s_decode_buffer(cur, &topic_filter)) {
1064 return AWS_OP_ERR;
1065 }
1066
1067 aws_array_list_push_back(&packet->topic_filters, &topic_filter);
1068
1069 remaining_length -= s_sizeof_encoded_buffer(&topic_filter);
1070 }
1071
1072 return AWS_OP_SUCCESS;
1073 }
1074
1075 /*****************************************************************************/
1076 /* Unsuback */
1077
1078 int aws_mqtt_packet_unsuback_init(struct aws_mqtt_packet_ack *packet, uint16_t packet_identifier) {
1079
1080 s_ack_init(packet, AWS_MQTT_PACKET_UNSUBACK, packet_identifier);
1081
1082 return AWS_OP_SUCCESS;
1083 }
1084
1085 /*****************************************************************************/
1086 /* Ping request/response */
1087
1088 static void s_connection_init(struct aws_mqtt_packet_connection *packet, enum aws_mqtt_packet_type type) {
1089
1090 AWS_PRECONDITION(packet);
1091
1092 AWS_ZERO_STRUCT(*packet);
1093 packet->fixed_header.packet_type = type;
1094 }
1095
1096 int aws_mqtt_packet_pingreq_init(struct aws_mqtt_packet_connection *packet) {
1097
1098 s_connection_init(packet, AWS_MQTT_PACKET_PINGREQ);
1099
1100 return AWS_OP_SUCCESS;
1101 }
1102
1103 int aws_mqtt_packet_pingresp_init(struct aws_mqtt_packet_connection *packet) {
1104
1105 s_connection_init(packet, AWS_MQTT_PACKET_PINGRESP);
1106
1107 return AWS_OP_SUCCESS;
1108 }
1109
1110 int aws_mqtt_packet_disconnect_init(struct aws_mqtt_packet_connection *packet) {
1111
1112 s_connection_init(packet, AWS_MQTT_PACKET_DISCONNECT);
1113
1114 return AWS_OP_SUCCESS;
1115 }
1116
1117 int aws_mqtt_packet_connection_encode(struct aws_byte_buf *buf, const struct aws_mqtt_packet_connection *packet) {
1118
1119 AWS_PRECONDITION(buf);
1120 AWS_PRECONDITION(packet);
1121
1122 /*************************************************************************/
1123 /* Fixed Header */
1124
1125 if (aws_mqtt_fixed_header_encode(buf, &packet->fixed_header)) {
1126 return AWS_OP_ERR;
1127 }
1128
1129 return AWS_OP_SUCCESS;
1130 }
1131
1132 int aws_mqtt_packet_connection_decode(struct aws_byte_cursor *cur, struct aws_mqtt_packet_connection *packet) {
1133
1134 AWS_PRECONDITION(cur);
1135 AWS_PRECONDITION(packet);
1136
1137 /*************************************************************************/
1138 /* Fixed Header */
1139
1140 if (aws_mqtt_fixed_header_decode(cur, &packet->fixed_header)) {
1141 return AWS_OP_ERR;
1142 }
1143
1144 return AWS_OP_SUCCESS;
1145 }