1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
|
/*
Copyright (c) 2018-2020 Roger Light <roger@atchoo.org>
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License 2.0
and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at
https://www.eclipse.org/legal/epl-2.0/
and the Eclipse Distribution License is available at
http://www.eclipse.org/org/documents/edl-v10.php.
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
Contributors:
Roger Light - initial implementation and documentation.
*/
#include "config.h"
#include <errno.h>
#include <string.h>
#ifndef WIN32
# include <strings.h>
#endif
#include "logging_mosq.h"
#include "memory_mosq.h"
#include "mqtt_protocol.h"
#include "packet_mosq.h"
#include "property_mosq.h"
static int property__read(struct mosquitto__packet *packet, uint32_t *len, mosquitto_property *property)
{
int rc;
uint32_t property_identifier;
uint8_t byte;
uint8_t byte_count;
uint16_t uint16;
uint32_t uint32;
uint32_t varint;
char *str1, *str2;
uint16_t slen1, slen2;
if(!property) return MOSQ_ERR_INVAL;
rc = packet__read_varint(packet, &property_identifier, NULL);
if(rc){
return rc;
}
*len -= 1;
memset(property, 0, sizeof(mosquitto_property));
property->identifier = (int32_t)property_identifier;
switch(property_identifier){
case MQTT_PROP_PAYLOAD_FORMAT_INDICATOR:
case MQTT_PROP_REQUEST_PROBLEM_INFORMATION:
case MQTT_PROP_REQUEST_RESPONSE_INFORMATION:
case MQTT_PROP_MAXIMUM_QOS:
case MQTT_PROP_RETAIN_AVAILABLE:
case MQTT_PROP_WILDCARD_SUB_AVAILABLE:
case MQTT_PROP_SUBSCRIPTION_ID_AVAILABLE:
case MQTT_PROP_SHARED_SUB_AVAILABLE:
rc = packet__read_byte(packet, &byte);
if(rc) return rc;
*len -= 1; /* byte */
property->value.i8 = byte;
break;
case MQTT_PROP_SERVER_KEEP_ALIVE:
case MQTT_PROP_RECEIVE_MAXIMUM:
case MQTT_PROP_TOPIC_ALIAS_MAXIMUM:
case MQTT_PROP_TOPIC_ALIAS:
rc = packet__read_uint16(packet, &uint16);
if(rc) return rc;
*len -= 2; /* uint16 */
property->value.i16 = uint16;
break;
case MQTT_PROP_MESSAGE_EXPIRY_INTERVAL:
case MQTT_PROP_SESSION_EXPIRY_INTERVAL:
case MQTT_PROP_WILL_DELAY_INTERVAL:
case MQTT_PROP_MAXIMUM_PACKET_SIZE:
rc = packet__read_uint32(packet, &uint32);
if(rc) return rc;
*len -= 4; /* uint32 */
property->value.i32 = uint32;
break;
case MQTT_PROP_SUBSCRIPTION_IDENTIFIER:
rc = packet__read_varint(packet, &varint, &byte_count);
if(rc) return rc;
*len -= byte_count;
property->value.varint = varint;
break;
case MQTT_PROP_CONTENT_TYPE:
case MQTT_PROP_RESPONSE_TOPIC:
case MQTT_PROP_ASSIGNED_CLIENT_IDENTIFIER:
case MQTT_PROP_AUTHENTICATION_METHOD:
case MQTT_PROP_RESPONSE_INFORMATION:
case MQTT_PROP_SERVER_REFERENCE:
case MQTT_PROP_REASON_STRING:
rc = packet__read_string(packet, &str1, &slen1);
if(rc) return rc;
*len = (*len) - 2 - slen1; /* uint16, string len */
property->value.s.v = str1;
property->value.s.len = slen1;
break;
case MQTT_PROP_AUTHENTICATION_DATA:
case MQTT_PROP_CORRELATION_DATA:
rc = packet__read_binary(packet, (uint8_t **)&str1, &slen1);
if(rc) return rc;
*len = (*len) - 2 - slen1; /* uint16, binary len */
property->value.bin.v = str1;
property->value.bin.len = slen1;
break;
case MQTT_PROP_USER_PROPERTY:
rc = packet__read_string(packet, &str1, &slen1);
if(rc) return rc;
*len = (*len) - 2 - slen1; /* uint16, string len */
rc = packet__read_string(packet, &str2, &slen2);
if(rc){
mosquitto__free(str1);
return rc;
}
*len = (*len) - 2 - slen2; /* uint16, string len */
property->name.v = str1;
property->name.len = slen1;
property->value.s.v = str2;
property->value.s.len = slen2;
break;
default:
#ifdef WITH_BROKER
log__printf(NULL, MOSQ_LOG_DEBUG, "Unsupported property type: %d", property_identifier);
#endif
return MOSQ_ERR_MALFORMED_PACKET;
}
return MOSQ_ERR_SUCCESS;
}
int property__read_all(int command, struct mosquitto__packet *packet, mosquitto_property **properties)
{
int rc;
uint32_t proplen;
mosquitto_property *p, *tail = NULL;
rc = packet__read_varint(packet, &proplen, NULL);
if(rc) return rc;
*properties = NULL;
/* The order of properties must be preserved for some types, so keep the
* same order for all */
while(proplen > 0){
p = mosquitto__calloc(1, sizeof(mosquitto_property));
if(!p){
mosquitto_property_free_all(properties);
return MOSQ_ERR_NOMEM;
}
rc = property__read(packet, &proplen, p);
if(rc){
mosquitto__free(p);
mosquitto_property_free_all(properties);
return rc;
}
if(!(*properties)){
*properties = p;
}else{
tail->next = p;
}
tail = p;
}
rc = mosquitto_property_check_all(command, *properties);
if(rc){
mosquitto_property_free_all(properties);
return rc;
}
return MOSQ_ERR_SUCCESS;
}
void property__free(mosquitto_property **property)
{
if(!property || !(*property)) return;
switch((*property)->identifier){
case MQTT_PROP_CONTENT_TYPE:
case MQTT_PROP_RESPONSE_TOPIC:
case MQTT_PROP_ASSIGNED_CLIENT_IDENTIFIER:
case MQTT_PROP_AUTHENTICATION_METHOD:
case MQTT_PROP_RESPONSE_INFORMATION:
case MQTT_PROP_SERVER_REFERENCE:
case MQTT_PROP_REASON_STRING:
mosquitto__free((*property)->value.s.v);
break;
case MQTT_PROP_AUTHENTICATION_DATA:
case MQTT_PROP_CORRELATION_DATA:
mosquitto__free((*property)->value.bin.v);
break;
case MQTT_PROP_USER_PROPERTY:
mosquitto__free((*property)->name.v);
mosquitto__free((*property)->value.s.v);
break;
case MQTT_PROP_PAYLOAD_FORMAT_INDICATOR:
case MQTT_PROP_MESSAGE_EXPIRY_INTERVAL:
case MQTT_PROP_SUBSCRIPTION_IDENTIFIER:
case MQTT_PROP_SESSION_EXPIRY_INTERVAL:
case MQTT_PROP_SERVER_KEEP_ALIVE:
case MQTT_PROP_REQUEST_PROBLEM_INFORMATION:
case MQTT_PROP_WILL_DELAY_INTERVAL:
case MQTT_PROP_REQUEST_RESPONSE_INFORMATION:
case MQTT_PROP_RECEIVE_MAXIMUM:
case MQTT_PROP_TOPIC_ALIAS_MAXIMUM:
case MQTT_PROP_TOPIC_ALIAS:
case MQTT_PROP_MAXIMUM_QOS:
case MQTT_PROP_RETAIN_AVAILABLE:
case MQTT_PROP_MAXIMUM_PACKET_SIZE:
case MQTT_PROP_WILDCARD_SUB_AVAILABLE:
case MQTT_PROP_SUBSCRIPTION_ID_AVAILABLE:
case MQTT_PROP_SHARED_SUB_AVAILABLE:
/* Nothing to free */
break;
}
mosquitto__free(*property);
*property = NULL;
}
void mosquitto_property_free_all(mosquitto_property **property)
{
mosquitto_property *p, *next;
if(!property) return;
p = *property;
while(p){
next = p->next;
property__free(&p);
p = next;
}
*property = NULL;
}
unsigned int property__get_length(const mosquitto_property *property)
{
if(!property) return 0;
switch(property->identifier){
/* Byte */
case MQTT_PROP_PAYLOAD_FORMAT_INDICATOR:
case MQTT_PROP_REQUEST_PROBLEM_INFORMATION:
case MQTT_PROP_REQUEST_RESPONSE_INFORMATION:
case MQTT_PROP_MAXIMUM_QOS:
case MQTT_PROP_RETAIN_AVAILABLE:
case MQTT_PROP_WILDCARD_SUB_AVAILABLE:
case MQTT_PROP_SUBSCRIPTION_ID_AVAILABLE:
case MQTT_PROP_SHARED_SUB_AVAILABLE:
return 2; /* 1 (identifier) + 1 byte */
/* uint16 */
case MQTT_PROP_SERVER_KEEP_ALIVE:
case MQTT_PROP_RECEIVE_MAXIMUM:
case MQTT_PROP_TOPIC_ALIAS_MAXIMUM:
case MQTT_PROP_TOPIC_ALIAS:
return 3; /* 1 (identifier) + 2 bytes */
/* uint32 */
case MQTT_PROP_MESSAGE_EXPIRY_INTERVAL:
case MQTT_PROP_WILL_DELAY_INTERVAL:
case MQTT_PROP_MAXIMUM_PACKET_SIZE:
case MQTT_PROP_SESSION_EXPIRY_INTERVAL:
return 5; /* 1 (identifier) + 4 bytes */
/* varint */
case MQTT_PROP_SUBSCRIPTION_IDENTIFIER:
if(property->value.varint < 128){
return 2;
}else if(property->value.varint < 16384){
return 3;
}else if(property->value.varint < 2097152){
return 4;
}else if(property->value.varint < 268435456){
return 5;
}else{
return 0;
}
/* binary */
case MQTT_PROP_CORRELATION_DATA:
case MQTT_PROP_AUTHENTICATION_DATA:
return 3U + property->value.bin.len; /* 1 + 2 bytes (len) + X bytes (payload) */
/* string */
case MQTT_PROP_CONTENT_TYPE:
case MQTT_PROP_RESPONSE_TOPIC:
case MQTT_PROP_ASSIGNED_CLIENT_IDENTIFIER:
case MQTT_PROP_AUTHENTICATION_METHOD:
case MQTT_PROP_RESPONSE_INFORMATION:
case MQTT_PROP_SERVER_REFERENCE:
case MQTT_PROP_REASON_STRING:
return 3U + property->value.s.len; /* 1 + 2 bytes (len) + X bytes (string) */
/* string pair */
case MQTT_PROP_USER_PROPERTY:
return 5U + property->value.s.len + property->name.len; /* 1 + 2*(2 bytes (len) + X bytes (string))*/
default:
return 0;
}
return 0;
}
unsigned int property__get_length_all(const mosquitto_property *property)
{
const mosquitto_property *p;
unsigned int len = 0;
p = property;
while(p){
len += property__get_length(p);
p = p->next;
}
return len;
}
/* Return the number of bytes we need to add on to the remaining length when
* encoding these properties. */
unsigned int property__get_remaining_length(const mosquitto_property *props)
{
unsigned int proplen, varbytes;
proplen = property__get_length_all(props);
varbytes = packet__varint_bytes(proplen);
return proplen + varbytes;
}
static int property__write(struct mosquitto__packet *packet, const mosquitto_property *property)
{
int rc;
rc = packet__write_varint(packet, (uint32_t)property->identifier);
if(rc) return rc;
switch(property->identifier){
case MQTT_PROP_PAYLOAD_FORMAT_INDICATOR:
case MQTT_PROP_REQUEST_PROBLEM_INFORMATION:
case MQTT_PROP_REQUEST_RESPONSE_INFORMATION:
case MQTT_PROP_MAXIMUM_QOS:
case MQTT_PROP_RETAIN_AVAILABLE:
case MQTT_PROP_WILDCARD_SUB_AVAILABLE:
case MQTT_PROP_SUBSCRIPTION_ID_AVAILABLE:
case MQTT_PROP_SHARED_SUB_AVAILABLE:
packet__write_byte(packet, property->value.i8);
break;
case MQTT_PROP_SERVER_KEEP_ALIVE:
case MQTT_PROP_RECEIVE_MAXIMUM:
case MQTT_PROP_TOPIC_ALIAS_MAXIMUM:
case MQTT_PROP_TOPIC_ALIAS:
packet__write_uint16(packet, property->value.i16);
break;
case MQTT_PROP_MESSAGE_EXPIRY_INTERVAL:
case MQTT_PROP_SESSION_EXPIRY_INTERVAL:
case MQTT_PROP_WILL_DELAY_INTERVAL:
case MQTT_PROP_MAXIMUM_PACKET_SIZE:
packet__write_uint32(packet, property->value.i32);
break;
case MQTT_PROP_SUBSCRIPTION_IDENTIFIER:
return packet__write_varint(packet, property->value.varint);
case MQTT_PROP_CONTENT_TYPE:
case MQTT_PROP_RESPONSE_TOPIC:
case MQTT_PROP_ASSIGNED_CLIENT_IDENTIFIER:
case MQTT_PROP_AUTHENTICATION_METHOD:
case MQTT_PROP_RESPONSE_INFORMATION:
case MQTT_PROP_SERVER_REFERENCE:
case MQTT_PROP_REASON_STRING:
packet__write_string(packet, property->value.s.v, property->value.s.len);
break;
case MQTT_PROP_AUTHENTICATION_DATA:
case MQTT_PROP_CORRELATION_DATA:
packet__write_uint16(packet, property->value.bin.len);
packet__write_bytes(packet, property->value.bin.v, property->value.bin.len);
break;
case MQTT_PROP_USER_PROPERTY:
packet__write_string(packet, property->name.v, property->name.len);
packet__write_string(packet, property->value.s.v, property->value.s.len);
break;
default:
#ifdef WITH_BROKER
log__printf(NULL, MOSQ_LOG_DEBUG, "Unsupported property type: %d", property->identifier);
#endif
return MOSQ_ERR_INVAL;
}
return MOSQ_ERR_SUCCESS;
}
int property__write_all(struct mosquitto__packet *packet, const mosquitto_property *properties, bool write_len)
{
int rc;
const mosquitto_property *p;
if(write_len){
rc = packet__write_varint(packet, property__get_length_all(properties));
if(rc) return rc;
}
p = properties;
while(p){
rc = property__write(packet, p);
if(rc) return rc;
p = p->next;
}
return MOSQ_ERR_SUCCESS;
}
int mosquitto_property_check_command(int command, int identifier)
{
switch(identifier){
case MQTT_PROP_PAYLOAD_FORMAT_INDICATOR:
case MQTT_PROP_MESSAGE_EXPIRY_INTERVAL:
case MQTT_PROP_CONTENT_TYPE:
case MQTT_PROP_RESPONSE_TOPIC:
case MQTT_PROP_CORRELATION_DATA:
if(command != CMD_PUBLISH && command != CMD_WILL){
return MOSQ_ERR_PROTOCOL;
}
break;
case MQTT_PROP_SUBSCRIPTION_IDENTIFIER:
if(command != CMD_PUBLISH && command != CMD_SUBSCRIBE){
return MOSQ_ERR_PROTOCOL;
}
break;
case MQTT_PROP_SESSION_EXPIRY_INTERVAL:
if(command != CMD_CONNECT && command != CMD_CONNACK && command != CMD_DISCONNECT){
return MOSQ_ERR_PROTOCOL;
}
break;
case MQTT_PROP_AUTHENTICATION_METHOD:
case MQTT_PROP_AUTHENTICATION_DATA:
if(command != CMD_CONNECT && command != CMD_CONNACK && command != CMD_AUTH){
return MOSQ_ERR_PROTOCOL;
}
break;
case MQTT_PROP_ASSIGNED_CLIENT_IDENTIFIER:
case MQTT_PROP_SERVER_KEEP_ALIVE:
case MQTT_PROP_RESPONSE_INFORMATION:
case MQTT_PROP_MAXIMUM_QOS:
case MQTT_PROP_RETAIN_AVAILABLE:
case MQTT_PROP_WILDCARD_SUB_AVAILABLE:
case MQTT_PROP_SUBSCRIPTION_ID_AVAILABLE:
case MQTT_PROP_SHARED_SUB_AVAILABLE:
if(command != CMD_CONNACK){
return MOSQ_ERR_PROTOCOL;
}
break;
case MQTT_PROP_WILL_DELAY_INTERVAL:
if(command != CMD_WILL){
return MOSQ_ERR_PROTOCOL;
}
break;
case MQTT_PROP_REQUEST_PROBLEM_INFORMATION:
case MQTT_PROP_REQUEST_RESPONSE_INFORMATION:
if(command != CMD_CONNECT){
return MOSQ_ERR_PROTOCOL;
}
break;
case MQTT_PROP_SERVER_REFERENCE:
if(command != CMD_CONNACK && command != CMD_DISCONNECT){
return MOSQ_ERR_PROTOCOL;
}
break;
case MQTT_PROP_REASON_STRING:
if(command == CMD_CONNECT || command == CMD_PUBLISH || command == CMD_SUBSCRIBE || command == CMD_UNSUBSCRIBE){
return MOSQ_ERR_PROTOCOL;
}
break;
case MQTT_PROP_RECEIVE_MAXIMUM:
case MQTT_PROP_TOPIC_ALIAS_MAXIMUM:
case MQTT_PROP_MAXIMUM_PACKET_SIZE:
if(command != CMD_CONNECT && command != CMD_CONNACK){
return MOSQ_ERR_PROTOCOL;
}
break;
case MQTT_PROP_TOPIC_ALIAS:
if(command != CMD_PUBLISH){
return MOSQ_ERR_PROTOCOL;
}
break;
case MQTT_PROP_USER_PROPERTY:
break;
default:
return MOSQ_ERR_PROTOCOL;
}
return MOSQ_ERR_SUCCESS;
}
const char *mosquitto_property_identifier_to_string(int identifier)
{
switch(identifier){
case MQTT_PROP_PAYLOAD_FORMAT_INDICATOR:
return "payload-format-indicator";
case MQTT_PROP_MESSAGE_EXPIRY_INTERVAL:
return "message-expiry-interval";
case MQTT_PROP_CONTENT_TYPE:
return "content-type";
case MQTT_PROP_RESPONSE_TOPIC:
return "response-topic";
case MQTT_PROP_CORRELATION_DATA:
return "correlation-data";
case MQTT_PROP_SUBSCRIPTION_IDENTIFIER:
return "subscription-identifier";
case MQTT_PROP_SESSION_EXPIRY_INTERVAL:
return "session-expiry-interval";
case MQTT_PROP_ASSIGNED_CLIENT_IDENTIFIER:
return "assigned-client-identifier";
case MQTT_PROP_SERVER_KEEP_ALIVE:
return "server-keep-alive";
case MQTT_PROP_AUTHENTICATION_METHOD:
return "authentication-method";
case MQTT_PROP_AUTHENTICATION_DATA:
return "authentication-data";
case MQTT_PROP_REQUEST_PROBLEM_INFORMATION:
return "request-problem-information";
case MQTT_PROP_WILL_DELAY_INTERVAL:
return "will-delay-interval";
case MQTT_PROP_REQUEST_RESPONSE_INFORMATION:
return "request-response-information";
case MQTT_PROP_RESPONSE_INFORMATION:
return "response-information";
case MQTT_PROP_SERVER_REFERENCE:
return "server-reference";
case MQTT_PROP_REASON_STRING:
return "reason-string";
case MQTT_PROP_RECEIVE_MAXIMUM:
return "receive-maximum";
case MQTT_PROP_TOPIC_ALIAS_MAXIMUM:
return "topic-alias-maximum";
case MQTT_PROP_TOPIC_ALIAS:
return "topic-alias";
case MQTT_PROP_MAXIMUM_QOS:
return "maximum-qos";
case MQTT_PROP_RETAIN_AVAILABLE:
return "retain-available";
case MQTT_PROP_USER_PROPERTY:
return "user-property";
case MQTT_PROP_MAXIMUM_PACKET_SIZE:
return "maximum-packet-size";
case MQTT_PROP_WILDCARD_SUB_AVAILABLE:
return "wildcard-subscription-available";
case MQTT_PROP_SUBSCRIPTION_ID_AVAILABLE:
return "subscription-identifier-available";
case MQTT_PROP_SHARED_SUB_AVAILABLE:
return "shared-subscription-available";
default:
return NULL;
}
}
int mosquitto_string_to_property_info(const char *propname, int *identifier, int *type)
{
if(!propname) return MOSQ_ERR_INVAL;
if(!strcasecmp(propname, "payload-format-indicator")){
*identifier = MQTT_PROP_PAYLOAD_FORMAT_INDICATOR;
*type = MQTT_PROP_TYPE_BYTE;
}else if(!strcasecmp(propname, "message-expiry-interval")){
*identifier = MQTT_PROP_MESSAGE_EXPIRY_INTERVAL;
*type = MQTT_PROP_TYPE_INT32;
}else if(!strcasecmp(propname, "content-type")){
*identifier = MQTT_PROP_CONTENT_TYPE;
*type = MQTT_PROP_TYPE_STRING;
}else if(!strcasecmp(propname, "response-topic")){
*identifier = MQTT_PROP_RESPONSE_TOPIC;
*type = MQTT_PROP_TYPE_STRING;
}else if(!strcasecmp(propname, "correlation-data")){
*identifier = MQTT_PROP_CORRELATION_DATA;
*type = MQTT_PROP_TYPE_BINARY;
}else if(!strcasecmp(propname, "subscription-identifier")){
*identifier = MQTT_PROP_SUBSCRIPTION_IDENTIFIER;
*type = MQTT_PROP_TYPE_VARINT;
}else if(!strcasecmp(propname, "session-expiry-interval")){
*identifier = MQTT_PROP_SESSION_EXPIRY_INTERVAL;
*type = MQTT_PROP_TYPE_INT32;
}else if(!strcasecmp(propname, "assigned-client-identifier")){
*identifier = MQTT_PROP_ASSIGNED_CLIENT_IDENTIFIER;
*type = MQTT_PROP_TYPE_STRING;
}else if(!strcasecmp(propname, "server-keep-alive")){
*identifier = MQTT_PROP_SERVER_KEEP_ALIVE;
*type = MQTT_PROP_TYPE_INT16;
}else if(!strcasecmp(propname, "authentication-method")){
*identifier = MQTT_PROP_AUTHENTICATION_METHOD;
*type = MQTT_PROP_TYPE_STRING;
}else if(!strcasecmp(propname, "authentication-data")){
*identifier = MQTT_PROP_AUTHENTICATION_DATA;
*type = MQTT_PROP_TYPE_BINARY;
}else if(!strcasecmp(propname, "request-problem-information")){
*identifier = MQTT_PROP_REQUEST_PROBLEM_INFORMATION;
*type = MQTT_PROP_TYPE_BYTE;
}else if(!strcasecmp(propname, "will-delay-interval")){
*identifier = MQTT_PROP_WILL_DELAY_INTERVAL;
*type = MQTT_PROP_TYPE_INT32;
}else if(!strcasecmp(propname, "request-response-information")){
*identifier = MQTT_PROP_REQUEST_RESPONSE_INFORMATION;
*type = MQTT_PROP_TYPE_BYTE;
}else if(!strcasecmp(propname, "response-information")){
*identifier = MQTT_PROP_RESPONSE_INFORMATION;
*type = MQTT_PROP_TYPE_STRING;
}else if(!strcasecmp(propname, "server-reference")){
*identifier = MQTT_PROP_SERVER_REFERENCE;
*type = MQTT_PROP_TYPE_STRING;
}else if(!strcasecmp(propname, "reason-string")){
*identifier = MQTT_PROP_REASON_STRING;
*type = MQTT_PROP_TYPE_STRING;
}else if(!strcasecmp(propname, "receive-maximum")){
*identifier = MQTT_PROP_RECEIVE_MAXIMUM;
*type = MQTT_PROP_TYPE_INT16;
}else if(!strcasecmp(propname, "topic-alias-maximum")){
*identifier = MQTT_PROP_TOPIC_ALIAS_MAXIMUM;
*type = MQTT_PROP_TYPE_INT16;
}else if(!strcasecmp(propname, "topic-alias")){
*identifier = MQTT_PROP_TOPIC_ALIAS;
*type = MQTT_PROP_TYPE_INT16;
}else if(!strcasecmp(propname, "maximum-qos")){
*identifier = MQTT_PROP_MAXIMUM_QOS;
*type = MQTT_PROP_TYPE_BYTE;
}else if(!strcasecmp(propname, "retain-available")){
*identifier = MQTT_PROP_RETAIN_AVAILABLE;
*type = MQTT_PROP_TYPE_BYTE;
}else if(!strcasecmp(propname, "user-property")){
*identifier = MQTT_PROP_USER_PROPERTY;
*type = MQTT_PROP_TYPE_STRING_PAIR;
}else if(!strcasecmp(propname, "maximum-packet-size")){
*identifier = MQTT_PROP_MAXIMUM_PACKET_SIZE;
*type = MQTT_PROP_TYPE_INT32;
}else if(!strcasecmp(propname, "wildcard-subscription-available")){
*identifier = MQTT_PROP_WILDCARD_SUB_AVAILABLE;
*type = MQTT_PROP_TYPE_BYTE;
}else if(!strcasecmp(propname, "subscription-identifier-available")){
*identifier = MQTT_PROP_SUBSCRIPTION_ID_AVAILABLE;
*type = MQTT_PROP_TYPE_BYTE;
}else if(!strcasecmp(propname, "shared-subscription-available")){
*identifier = MQTT_PROP_SHARED_SUB_AVAILABLE;
*type = MQTT_PROP_TYPE_BYTE;
}else{
return MOSQ_ERR_INVAL;
}
return MOSQ_ERR_SUCCESS;
}
static void property__add(mosquitto_property **proplist, struct mqtt5__property *prop)
{
mosquitto_property *p;
if(!(*proplist)){
*proplist = prop;
}
p = *proplist;
while(p->next){
p = p->next;
}
p->next = prop;
prop->next = NULL;
}
int mosquitto_property_add_byte(mosquitto_property **proplist, int identifier, uint8_t value)
{
mosquitto_property *prop;
if(!proplist) return MOSQ_ERR_INVAL;
if(identifier != MQTT_PROP_PAYLOAD_FORMAT_INDICATOR
&& identifier != MQTT_PROP_REQUEST_PROBLEM_INFORMATION
&& identifier != MQTT_PROP_REQUEST_RESPONSE_INFORMATION
&& identifier != MQTT_PROP_MAXIMUM_QOS
&& identifier != MQTT_PROP_RETAIN_AVAILABLE
&& identifier != MQTT_PROP_WILDCARD_SUB_AVAILABLE
&& identifier != MQTT_PROP_SUBSCRIPTION_ID_AVAILABLE
&& identifier != MQTT_PROP_SHARED_SUB_AVAILABLE){
return MOSQ_ERR_INVAL;
}
prop = mosquitto__calloc(1, sizeof(mosquitto_property));
if(!prop) return MOSQ_ERR_NOMEM;
prop->client_generated = true;
prop->identifier = identifier;
prop->value.i8 = value;
property__add(proplist, prop);
return MOSQ_ERR_SUCCESS;
}
int mosquitto_property_add_int16(mosquitto_property **proplist, int identifier, uint16_t value)
{
mosquitto_property *prop;
if(!proplist) return MOSQ_ERR_INVAL;
if(identifier != MQTT_PROP_SERVER_KEEP_ALIVE
&& identifier != MQTT_PROP_RECEIVE_MAXIMUM
&& identifier != MQTT_PROP_TOPIC_ALIAS_MAXIMUM
&& identifier != MQTT_PROP_TOPIC_ALIAS){
return MOSQ_ERR_INVAL;
}
prop = mosquitto__calloc(1, sizeof(mosquitto_property));
if(!prop) return MOSQ_ERR_NOMEM;
prop->client_generated = true;
prop->identifier = identifier;
prop->value.i16 = value;
property__add(proplist, prop);
return MOSQ_ERR_SUCCESS;
}
int mosquitto_property_add_int32(mosquitto_property **proplist, int identifier, uint32_t value)
{
mosquitto_property *prop;
if(!proplist) return MOSQ_ERR_INVAL;
if(identifier != MQTT_PROP_MESSAGE_EXPIRY_INTERVAL
&& identifier != MQTT_PROP_SESSION_EXPIRY_INTERVAL
&& identifier != MQTT_PROP_WILL_DELAY_INTERVAL
&& identifier != MQTT_PROP_MAXIMUM_PACKET_SIZE){
return MOSQ_ERR_INVAL;
}
prop = mosquitto__calloc(1, sizeof(mosquitto_property));
if(!prop) return MOSQ_ERR_NOMEM;
prop->client_generated = true;
prop->identifier = identifier;
prop->value.i32 = value;
property__add(proplist, prop);
return MOSQ_ERR_SUCCESS;
}
int mosquitto_property_add_varint(mosquitto_property **proplist, int identifier, uint32_t value)
{
mosquitto_property *prop;
if(!proplist || value > 268435455) return MOSQ_ERR_INVAL;
if(identifier != MQTT_PROP_SUBSCRIPTION_IDENTIFIER) return MOSQ_ERR_INVAL;
prop = mosquitto__calloc(1, sizeof(mosquitto_property));
if(!prop) return MOSQ_ERR_NOMEM;
prop->client_generated = true;
prop->identifier = identifier;
prop->value.varint = value;
property__add(proplist, prop);
return MOSQ_ERR_SUCCESS;
}
int mosquitto_property_add_binary(mosquitto_property **proplist, int identifier, const void *value, uint16_t len)
{
mosquitto_property *prop;
if(!proplist) return MOSQ_ERR_INVAL;
if(identifier != MQTT_PROP_CORRELATION_DATA
&& identifier != MQTT_PROP_AUTHENTICATION_DATA){
return MOSQ_ERR_INVAL;
}
prop = mosquitto__calloc(1, sizeof(mosquitto_property));
if(!prop) return MOSQ_ERR_NOMEM;
prop->client_generated = true;
prop->identifier = identifier;
if(len){
prop->value.bin.v = mosquitto__malloc(len);
if(!prop->value.bin.v){
mosquitto__free(prop);
return MOSQ_ERR_NOMEM;
}
memcpy(prop->value.bin.v, value, len);
prop->value.bin.len = len;
}
property__add(proplist, prop);
return MOSQ_ERR_SUCCESS;
}
int mosquitto_property_add_string(mosquitto_property **proplist, int identifier, const char *value)
{
mosquitto_property *prop;
size_t slen = 0;
if(!proplist) return MOSQ_ERR_INVAL;
if(value){
slen = strlen(value);
if(mosquitto_validate_utf8(value, (int)slen)) return MOSQ_ERR_MALFORMED_UTF8;
}
if(identifier != MQTT_PROP_CONTENT_TYPE
&& identifier != MQTT_PROP_RESPONSE_TOPIC
&& identifier != MQTT_PROP_ASSIGNED_CLIENT_IDENTIFIER
&& identifier != MQTT_PROP_AUTHENTICATION_METHOD
&& identifier != MQTT_PROP_RESPONSE_INFORMATION
&& identifier != MQTT_PROP_SERVER_REFERENCE
&& identifier != MQTT_PROP_REASON_STRING){
return MOSQ_ERR_INVAL;
}
prop = mosquitto__calloc(1, sizeof(mosquitto_property));
if(!prop) return MOSQ_ERR_NOMEM;
prop->client_generated = true;
prop->identifier = identifier;
if(value && slen > 0){
prop->value.s.v = mosquitto__strdup(value);
if(!prop->value.s.v){
mosquitto__free(prop);
return MOSQ_ERR_NOMEM;
}
prop->value.s.len = (uint16_t)slen;
}
property__add(proplist, prop);
return MOSQ_ERR_SUCCESS;
}
int mosquitto_property_add_string_pair(mosquitto_property **proplist, int identifier, const char *name, const char *value)
{
mosquitto_property *prop;
size_t slen_name = 0, slen_value = 0;
if(!proplist) return MOSQ_ERR_INVAL;
if(identifier != MQTT_PROP_USER_PROPERTY) return MOSQ_ERR_INVAL;
if(name){
slen_name = strlen(name);
if(mosquitto_validate_utf8(name, (int)slen_name)) return MOSQ_ERR_MALFORMED_UTF8;
}
if(value){
if(mosquitto_validate_utf8(value, (int)slen_value)) return MOSQ_ERR_MALFORMED_UTF8;
}
prop = mosquitto__calloc(1, sizeof(mosquitto_property));
if(!prop) return MOSQ_ERR_NOMEM;
prop->client_generated = true;
prop->identifier = identifier;
if(name){
prop->name.v = mosquitto__strdup(name);
if(!prop->name.v){
mosquitto__free(prop);
return MOSQ_ERR_NOMEM;
}
prop->name.len = (uint16_t)strlen(name);
}
if(value){
prop->value.s.v = mosquitto__strdup(value);
if(!prop->value.s.v){
mosquitto__free(prop->name.v);
mosquitto__free(prop);
return MOSQ_ERR_NOMEM;
}
prop->value.s.len = (uint16_t)strlen(value);
}
property__add(proplist, prop);
return MOSQ_ERR_SUCCESS;
}
int mosquitto_property_check_all(int command, const mosquitto_property *properties)
{
const mosquitto_property *p, *tail;
int rc;
p = properties;
while(p){
/* Validity checks */
if(p->identifier == MQTT_PROP_REQUEST_PROBLEM_INFORMATION
|| p->identifier == MQTT_PROP_PAYLOAD_FORMAT_INDICATOR
|| p->identifier == MQTT_PROP_REQUEST_RESPONSE_INFORMATION
|| p->identifier == MQTT_PROP_MAXIMUM_QOS
|| p->identifier == MQTT_PROP_RETAIN_AVAILABLE
|| p->identifier == MQTT_PROP_WILDCARD_SUB_AVAILABLE
|| p->identifier == MQTT_PROP_SUBSCRIPTION_ID_AVAILABLE
|| p->identifier == MQTT_PROP_SHARED_SUB_AVAILABLE){
if(p->value.i8 > 1){
return MOSQ_ERR_PROTOCOL;
}
}else if(p->identifier == MQTT_PROP_MAXIMUM_PACKET_SIZE){
if( p->value.i32 == 0){
return MOSQ_ERR_PROTOCOL;
}
}else if(p->identifier == MQTT_PROP_RECEIVE_MAXIMUM
|| p->identifier == MQTT_PROP_TOPIC_ALIAS){
if(p->value.i16 == 0){
return MOSQ_ERR_PROTOCOL;
}
}else if(p->identifier == MQTT_PROP_RESPONSE_TOPIC){
if(mosquitto_pub_topic_check(p->value.s.v) != MOSQ_ERR_SUCCESS){
return MOSQ_ERR_PROTOCOL;
}
}
/* Check for properties on incorrect commands */
rc = mosquitto_property_check_command(command, p->identifier);
if(rc) return rc;
/* Check for duplicates */
if(p->identifier != MQTT_PROP_USER_PROPERTY){
tail = p->next;
while(tail){
if(p->identifier == tail->identifier){
return MOSQ_ERR_DUPLICATE_PROPERTY;
}
tail = tail->next;
}
}
p = p->next;
}
return MOSQ_ERR_SUCCESS;
}
static const mosquitto_property *property__get_property(const mosquitto_property *proplist, int identifier, bool skip_first)
{
const mosquitto_property *p;
bool is_first = true;
p = proplist;
while(p){
if(p->identifier == identifier){
if(!is_first || !skip_first){
return p;
}
is_first = false;
}
p = p->next;
}
return NULL;
}
int mosquitto_property_identifier(const mosquitto_property *property)
{
if(property == NULL) return 0;
return property->identifier;
}
const mosquitto_property *mosquitto_property_next(const mosquitto_property *proplist)
{
if(proplist == NULL) return NULL;
return proplist->next;
}
const mosquitto_property *mosquitto_property_read_byte(const mosquitto_property *proplist, int identifier, uint8_t *value, bool skip_first)
{
const mosquitto_property *p;
if(!proplist) return NULL;
p = property__get_property(proplist, identifier, skip_first);
if(!p) return NULL;
if(p->identifier != MQTT_PROP_PAYLOAD_FORMAT_INDICATOR
&& p->identifier != MQTT_PROP_REQUEST_PROBLEM_INFORMATION
&& p->identifier != MQTT_PROP_REQUEST_RESPONSE_INFORMATION
&& p->identifier != MQTT_PROP_MAXIMUM_QOS
&& p->identifier != MQTT_PROP_RETAIN_AVAILABLE
&& p->identifier != MQTT_PROP_WILDCARD_SUB_AVAILABLE
&& p->identifier != MQTT_PROP_SUBSCRIPTION_ID_AVAILABLE
&& p->identifier != MQTT_PROP_SHARED_SUB_AVAILABLE){
return NULL;
}
if(value) *value = p->value.i8;
return p;
}
const mosquitto_property *mosquitto_property_read_int16(const mosquitto_property *proplist, int identifier, uint16_t *value, bool skip_first)
{
const mosquitto_property *p;
if(!proplist) return NULL;
p = property__get_property(proplist, identifier, skip_first);
if(!p) return NULL;
if(p->identifier != MQTT_PROP_SERVER_KEEP_ALIVE
&& p->identifier != MQTT_PROP_RECEIVE_MAXIMUM
&& p->identifier != MQTT_PROP_TOPIC_ALIAS_MAXIMUM
&& p->identifier != MQTT_PROP_TOPIC_ALIAS){
return NULL;
}
if(value) *value = p->value.i16;
return p;
}
const mosquitto_property *mosquitto_property_read_int32(const mosquitto_property *proplist, int identifier, uint32_t *value, bool skip_first)
{
const mosquitto_property *p;
if(!proplist) return NULL;
p = property__get_property(proplist, identifier, skip_first);
if(!p) return NULL;
if(p->identifier != MQTT_PROP_MESSAGE_EXPIRY_INTERVAL
&& p->identifier != MQTT_PROP_SESSION_EXPIRY_INTERVAL
&& p->identifier != MQTT_PROP_WILL_DELAY_INTERVAL
&& p->identifier != MQTT_PROP_MAXIMUM_PACKET_SIZE){
return NULL;
}
if(value) *value = p->value.i32;
return p;
}
const mosquitto_property *mosquitto_property_read_varint(const mosquitto_property *proplist, int identifier, uint32_t *value, bool skip_first)
{
const mosquitto_property *p;
if(!proplist) return NULL;
p = property__get_property(proplist, identifier, skip_first);
if(!p) return NULL;
if(p->identifier != MQTT_PROP_SUBSCRIPTION_IDENTIFIER){
return NULL;
}
if(value) *value = p->value.varint;
return p;
}
const mosquitto_property *mosquitto_property_read_binary(const mosquitto_property *proplist, int identifier, void **value, uint16_t *len, bool skip_first)
{
const mosquitto_property *p;
if(!proplist || (value && !len) || (!value && len)) return NULL;
if(value) *value = NULL;
p = property__get_property(proplist, identifier, skip_first);
if(!p) return NULL;
if(p->identifier != MQTT_PROP_CORRELATION_DATA
&& p->identifier != MQTT_PROP_AUTHENTICATION_DATA){
return NULL;
}
if(value){
*len = p->value.bin.len;
*value = mosquitto__calloc(1, *len + 1U);
if(!(*value)) return NULL;
memcpy(*value, p->value.bin.v, *len);
}
return p;
}
const mosquitto_property *mosquitto_property_read_string(const mosquitto_property *proplist, int identifier, char **value, bool skip_first)
{
const mosquitto_property *p;
if(!proplist) return NULL;
p = property__get_property(proplist, identifier, skip_first);
if(!p) return NULL;
if(p->identifier != MQTT_PROP_CONTENT_TYPE
&& p->identifier != MQTT_PROP_RESPONSE_TOPIC
&& p->identifier != MQTT_PROP_ASSIGNED_CLIENT_IDENTIFIER
&& p->identifier != MQTT_PROP_AUTHENTICATION_METHOD
&& p->identifier != MQTT_PROP_RESPONSE_INFORMATION
&& p->identifier != MQTT_PROP_SERVER_REFERENCE
&& p->identifier != MQTT_PROP_REASON_STRING){
return NULL;
}
if(value){
*value = mosquitto__calloc(1, (size_t)p->value.s.len+1);
if(!(*value)) return NULL;
memcpy(*value, p->value.s.v, p->value.s.len);
}
return p;
}
const mosquitto_property *mosquitto_property_read_string_pair(const mosquitto_property *proplist, int identifier, char **name, char **value, bool skip_first)
{
const mosquitto_property *p;
if(!proplist) return NULL;
if(name) *name = NULL;
if(value) *value = NULL;
p = property__get_property(proplist, identifier, skip_first);
if(!p) return NULL;
if(p->identifier != MQTT_PROP_USER_PROPERTY) return NULL;
if(name){
*name = mosquitto__calloc(1, (size_t)p->name.len+1);
if(!(*name)) return NULL;
memcpy(*name, p->name.v, p->name.len);
}
if(value){
*value = mosquitto__calloc(1, (size_t)p->value.s.len+1);
if(!(*value)){
if(name){
mosquitto__free(*name);
*name = NULL;
}
return NULL;
}
memcpy(*value, p->value.s.v, p->value.s.len);
}
return p;
}
int mosquitto_property_copy_all(mosquitto_property **dest, const mosquitto_property *src)
{
mosquitto_property *pnew, *plast = NULL;
if(!src) return MOSQ_ERR_SUCCESS;
if(!dest) return MOSQ_ERR_INVAL;
*dest = NULL;
while(src){
pnew = mosquitto__calloc(1, sizeof(mosquitto_property));
if(!pnew){
mosquitto_property_free_all(dest);
return MOSQ_ERR_NOMEM;
}
if(plast){
plast->next = pnew;
}else{
*dest = pnew;
}
plast = pnew;
pnew->client_generated = src->client_generated;
pnew->identifier = src->identifier;
switch(pnew->identifier){
case MQTT_PROP_PAYLOAD_FORMAT_INDICATOR:
case MQTT_PROP_REQUEST_PROBLEM_INFORMATION:
case MQTT_PROP_REQUEST_RESPONSE_INFORMATION:
case MQTT_PROP_MAXIMUM_QOS:
case MQTT_PROP_RETAIN_AVAILABLE:
case MQTT_PROP_WILDCARD_SUB_AVAILABLE:
case MQTT_PROP_SUBSCRIPTION_ID_AVAILABLE:
case MQTT_PROP_SHARED_SUB_AVAILABLE:
pnew->value.i8 = src->value.i8;
break;
case MQTT_PROP_SERVER_KEEP_ALIVE:
case MQTT_PROP_RECEIVE_MAXIMUM:
case MQTT_PROP_TOPIC_ALIAS_MAXIMUM:
case MQTT_PROP_TOPIC_ALIAS:
pnew->value.i16 = src->value.i16;
break;
case MQTT_PROP_MESSAGE_EXPIRY_INTERVAL:
case MQTT_PROP_SESSION_EXPIRY_INTERVAL:
case MQTT_PROP_WILL_DELAY_INTERVAL:
case MQTT_PROP_MAXIMUM_PACKET_SIZE:
pnew->value.i32 = src->value.i32;
break;
case MQTT_PROP_SUBSCRIPTION_IDENTIFIER:
pnew->value.varint = src->value.varint;
break;
case MQTT_PROP_CONTENT_TYPE:
case MQTT_PROP_RESPONSE_TOPIC:
case MQTT_PROP_ASSIGNED_CLIENT_IDENTIFIER:
case MQTT_PROP_AUTHENTICATION_METHOD:
case MQTT_PROP_RESPONSE_INFORMATION:
case MQTT_PROP_SERVER_REFERENCE:
case MQTT_PROP_REASON_STRING:
pnew->value.s.len = src->value.s.len;
pnew->value.s.v = src->value.s.v ? mosquitto__strdup(src->value.s.v) : (char*)mosquitto__calloc(1,1);
if(!pnew->value.s.v){
mosquitto_property_free_all(dest);
return MOSQ_ERR_NOMEM;
}
break;
case MQTT_PROP_AUTHENTICATION_DATA:
case MQTT_PROP_CORRELATION_DATA:
pnew->value.bin.len = src->value.bin.len;
pnew->value.bin.v = mosquitto__malloc(pnew->value.bin.len);
if(!pnew->value.bin.v){
mosquitto_property_free_all(dest);
return MOSQ_ERR_NOMEM;
}
memcpy(pnew->value.bin.v, src->value.bin.v, pnew->value.bin.len);
break;
case MQTT_PROP_USER_PROPERTY:
pnew->value.s.len = src->value.s.len;
pnew->value.s.v = src->value.s.v ? mosquitto__strdup(src->value.s.v) : (char*)mosquitto__calloc(1,1);
if(!pnew->value.s.v){
mosquitto_property_free_all(dest);
return MOSQ_ERR_NOMEM;
}
pnew->name.len = src->name.len;
pnew->name.v = src->name.v ? mosquitto__strdup(src->name.v) : (char*)mosquitto__calloc(1,1);
if(!pnew->name.v){
mosquitto_property_free_all(dest);
return MOSQ_ERR_NOMEM;
}
break;
default:
mosquitto_property_free_all(dest);
return MOSQ_ERR_INVAL;
}
src = src->next;
}
return MOSQ_ERR_SUCCESS;
}
|