Changeset 399 for azure_iot_hub/trunk/azure_iothub/umqtt/src/mqtt_codec.c
- Timestamp:
- Jun 5, 2019, 2:23:55 PM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
azure_iot_hub/trunk/azure_iothub/umqtt/src/mqtt_codec.c
r389 r399 38 38 #define UNSUBSCRIBE_FIXED_HEADER_FLAG 0x2 39 39 40 #define MAX_SEND_SIZE 0xFFFFFF7F 40 #define MAX_SEND_SIZE 0xFFFFFF7F // 268435455 41 42 // This captures the maximum packet size for 3 digits. 43 // If it's above this value then we bail out of the loop 44 #define MAX_3_DIGIT_PACKET_SIZE 2097152 41 45 42 46 #define CODEC_STATE_VALUES \ … … 409 413 size_t willTopicLen = 0; 410 414 size_t spaceLen = 0; 415 size_t currLen = 0; 416 size_t totalLen = 0; 411 417 412 418 if (mqttOptions->clientId != NULL) … … 436 442 } 437 443 438 size_tcurrLen = BUFFER_length(ctrlPacket);439 size_ttotalLen = clientLen + usernameLen + passwordLen + willMessageLen + willTopicLen + spaceLen;444 currLen = BUFFER_length(ctrlPacket); 445 totalLen = clientLen + usernameLen + passwordLen + willMessageLen + willTopicLen + spaceLen; 440 446 441 447 // Validate the Username & Password … … 507 513 { 508 514 (void)STRING_sprintf(connect_payload_trace, " | PWD: XXXX"); 515 //(void)STRING_sprintf(connect_payload_trace, " | PWD: %s", mqttOptions->password); 509 516 } 510 517 } … … 554 561 multiplier *= NEXT_128_CHUNK; 555 562 556 if (multiplier > 128 * 128 * 128)563 if (multiplier > MAX_3_DIGIT_PACKET_SIZE) 557 564 { 558 565 result = MU_FAILURE; … … 561 568 } while ((encodeByte & NEXT_128_CHUNK) != 0); 562 569 563 codecData->codecState = CODEC_STATE_VAR_HEADER; 564 565 // Reset remainLen Index 566 codecData->remainLenIndex = 0; 567 memset(codecData->storeRemainLen, 0, 4 * sizeof(uint8_t)); 568 569 if (totalLen > 0) 570 { 571 codecData->bufferOffset = 0; 572 codecData->headerData = BUFFER_new(); 573 if (codecData->headerData == NULL) 574 { 575 /* Codes_SRS_MQTT_CODEC_07_035: [ If any error is encountered then the packet state will be marked as error and mqtt_codec_bytesReceived shall return a non-zero value. ] */ 576 LogError("Failed BUFFER_new"); 577 result = MU_FAILURE; 578 } 579 else 580 { 581 if (BUFFER_pre_build(codecData->headerData, totalLen) != 0) 570 if (result != 0 || totalLen > MAX_SEND_SIZE) 571 { 572 LogError("Receive buffer too large for MQTT packet"); 573 result = MU_FAILURE; 574 } 575 else 576 { 577 codecData->codecState = CODEC_STATE_VAR_HEADER; 578 579 // Reset remainLen Index 580 codecData->remainLenIndex = 0; 581 memset(codecData->storeRemainLen, 0, 4 * sizeof(uint8_t)); 582 583 if (totalLen > 0) 584 { 585 codecData->bufferOffset = 0; 586 codecData->headerData = BUFFER_new(); 587 if (codecData->headerData == NULL) 582 588 { 583 589 /* Codes_SRS_MQTT_CODEC_07_035: [ If any error is encountered then the packet state will be marked as error and mqtt_codec_bytesReceived shall return a non-zero value. ] */ 584 LogError("Failed BUFFER_ pre_build");590 LogError("Failed BUFFER_new"); 585 591 result = MU_FAILURE; 586 592 } 587 593 else 594 { 595 if (BUFFER_pre_build(codecData->headerData, totalLen) != 0) 596 { 597 /* Codes_SRS_MQTT_CODEC_07_035: [ If any error is encountered then the packet state will be marked as error and mqtt_codec_bytesReceived shall return a non-zero value. ] */ 598 LogError("Failed BUFFER_pre_build"); 599 result = MU_FAILURE; 600 } 601 602 } 588 603 } 589 604 } … … 611 626 } 612 627 628 static void clear_codec_data(MQTTCODEC_INSTANCE* codec_data) 629 { 630 // Clear the code instance data 631 codec_data->currPacket = UNKNOWN_TYPE; 632 codec_data->codecState = CODEC_STATE_FIXED_HEADER; 633 codec_data->headerFlags = 0; 634 codec_data->bufferOffset = 0; 635 codec_data->headerData = NULL; 636 memset(codec_data->storeRemainLen, 0, 4 * sizeof(uint8_t)); 637 codec_data->remainLenIndex = 0; 638 } 639 640 void mqtt_codec_reset(MQTTCODEC_HANDLE handle) 641 { 642 if (handle != NULL) 643 { 644 clear_codec_data(handle); 645 } 646 } 647 613 648 MQTTCODEC_HANDLE mqtt_codec_create(ON_PACKET_COMPLETE_CALLBACK packetComplete, void* callbackCtx) 614 649 { … … 619 654 { 620 655 /* Codes_SRS_MQTT_CODEC_07_002: [On success mqtt_codec_create shall return a MQTTCODEC_HANDLE value.] */ 621 result->currPacket = UNKNOWN_TYPE; 622 result->codecState = CODEC_STATE_FIXED_HEADER; 623 result->headerFlags = 0; 624 result->bufferOffset = 0; 656 clear_codec_data(result); 625 657 result->packetComplete = packetComplete; 626 658 result->callContext = callbackCtx; 627 result->headerData = NULL;628 memset(result->storeRemainLen, 0, 4 * sizeof(uint8_t));629 result->remainLenIndex = 0;630 659 } 631 660 return result; … … 1086 1115 result = MU_FAILURE; 1087 1116 } 1088 if (codec_Data->currPacket == PINGRESP_TYPE)1117 else if (codec_Data->currPacket == PINGRESP_TYPE) 1089 1118 { 1090 1119 /* Codes_SRS_MQTT_CODEC_07_034: [Upon a constructing a complete MQTT packet mqtt_codec_bytesReceived shall call the ON_PACKET_COMPLETE_CALLBACK function.] */
Note:
See TracChangeset
for help on using the changeset viewer.