Ignore:
Timestamp:
Jun 5, 2019, 2:23:55 PM (5 years ago)
Author:
coas-nagasima
Message:

更新

File:
1 edited

Legend:

Unmodified
Added
Removed
  • azure_iot_hub/trunk/azure_iothub/umqtt/src/mqtt_codec.c

    r389 r399  
    3838#define UNSUBSCRIBE_FIXED_HEADER_FLAG       0x2
    3939
    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
    4145
    4246#define CODEC_STATE_VALUES      \
     
    409413        size_t willTopicLen = 0;
    410414        size_t spaceLen = 0;
     415        size_t currLen = 0;
     416        size_t totalLen = 0;
    411417
    412418        if (mqttOptions->clientId != NULL)
     
    436442        }
    437443
    438         size_t currLen = BUFFER_length(ctrlPacket);
    439         size_t totalLen = clientLen + usernameLen + passwordLen + willMessageLen + willTopicLen + spaceLen;
     444        currLen = BUFFER_length(ctrlPacket);
     445        totalLen = clientLen + usernameLen + passwordLen + willMessageLen + willTopicLen + spaceLen;
    440446
    441447        // Validate the Username & Password
     
    507513                    {
    508514                        (void)STRING_sprintf(connect_payload_trace, " | PWD: XXXX");
     515                        //(void)STRING_sprintf(connect_payload_trace, " | PWD: %s", mqttOptions->password);
    509516                    }
    510517                }
     
    554561                multiplier *= NEXT_128_CHUNK;
    555562
    556                 if (multiplier > 128 * 128 * 128)
     563                if (multiplier > MAX_3_DIGIT_PACKET_SIZE)
    557564                {
    558565                    result = MU_FAILURE;
     
    561568            } while ((encodeByte & NEXT_128_CHUNK) != 0);
    562569
    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)
    582588                    {
    583589                        /* 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");
    585591                        result = MU_FAILURE;
    586592                    }
    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                    }
    588603                }
    589604            }
     
    611626}
    612627
     628static 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
     640void mqtt_codec_reset(MQTTCODEC_HANDLE handle)
     641{
     642    if (handle != NULL)
     643    {
     644        clear_codec_data(handle);
     645    }
     646}
     647
    613648MQTTCODEC_HANDLE mqtt_codec_create(ON_PACKET_COMPLETE_CALLBACK packetComplete, void* callbackCtx)
    614649{
     
    619654    {
    620655        /* 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);
    625657        result->packetComplete = packetComplete;
    626658        result->callContext = callbackCtx;
    627         result->headerData = NULL;
    628         memset(result->storeRemainLen, 0, 4 * sizeof(uint8_t));
    629         result->remainLenIndex = 0;
    630659    }
    631660    return result;
     
    10861115                        result = MU_FAILURE;
    10871116                    }
    1088                     if (codec_Data->currPacket == PINGRESP_TYPE)
     1117                    else if (codec_Data->currPacket == PINGRESP_TYPE)
    10891118                    {
    10901119                        /* 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.