From 33968b78167e8071c315f0719326fb5c654e9261 Mon Sep 17 00:00:00 2001 From: Artem Nosach Date: Wed, 5 Aug 2015 17:52:21 +0300 Subject: Check MTU size according to new rules - Use 1488 bytes MTU for protocol versions 1 and 2 - Use specified in ini file MTU size for protocol versions 3 and higher Implements: APPLINK-14656 --- .../protocol_handler/src/protocol_handler_impl.cc | 37 ++++++++++++++-------- .../protocol_handler/src/protocol_packet.cc | 20 ++++++++---- 2 files changed, 38 insertions(+), 19 deletions(-) (limited to 'src/components/protocol_handler') diff --git a/src/components/protocol_handler/src/protocol_handler_impl.cc b/src/components/protocol_handler/src/protocol_handler_impl.cc index a5ed1adc95..2c7bad263d 100644 --- a/src/components/protocol_handler/src/protocol_handler_impl.cc +++ b/src/components/protocol_handler/src/protocol_handler_impl.cc @@ -37,6 +37,7 @@ #include "connection_handler/connection_handler_impl.h" #include "config_profile/profile.h" #include "utils/byte_order.h" +#include "protocol/common.h" #ifdef ENABLE_SECURITY #include "security_manager/ssl_context.h" @@ -358,29 +359,39 @@ void ProtocolHandlerImpl::SendMessageToMobileApp(const RawMessagePtr message, metric_observer_->StartMessageProcess(message_id, start_time); } #endif // TIME_TESTER - - const uint32_t header_size = (PROTOCOL_VERSION_1 == message->protocol_version()) - ? PROTOCOL_HEADER_V1_SIZE : PROTOCOL_HEADER_V2_SIZE; - uint32_t max_frame_size = MAXIMUM_FRAME_DATA_SIZE - header_size; + const size_t max_frame_size = + profile::Profile::instance()->maximum_payload_size(); + size_t frame_size = DEFAULT_FRAME_DATA_SIZE; + switch (message->protocol_version()) { + case PROTOCOL_VERSION_1: + case PROTOCOL_VERSION_2: + break; + case PROTOCOL_VERSION_3: + case PROTOCOL_VERSION_4: + frame_size = max_frame_size > DEFAULT_FRAME_DATA_SIZE ? + max_frame_size : DEFAULT_FRAME_DATA_SIZE; + break; + default: + break; + } #ifdef ENABLE_SECURITY const security_manager::SSLContext *ssl_context = session_observer_-> GetSSLContext(message->connection_key(), message->service_type()); if (ssl_context && ssl_context->IsInitCompleted()) { - const size_t max_block_size = ssl_context->get_max_block_size(max_frame_size); + const size_t max_block_size = ssl_context->get_max_block_size(frame_size); DCHECK(max_block_size > 0); if (max_block_size > 0) { - max_frame_size = max_block_size; - LOG4CXX_DEBUG(logger_, "Security set new optimal packet size " << max_frame_size); + frame_size = max_block_size; + LOG4CXX_DEBUG(logger_, "Security set new optimal packet size " << frame_size); } else { LOG4CXX_ERROR(logger_, "Security could not return max block size, use the origin one"); } } - LOG4CXX_DEBUG(logger_, "Optimal packet size is " << max_frame_size); + LOG4CXX_DEBUG(logger_, "Optimal packet size is " << frame_size); #endif // ENABLE_SECURITY - DCHECK(MAXIMUM_FRAME_DATA_SIZE > max_frame_size); - + DCHECK(DEFAULT_FRAME_DATA_SIZE <= frame_size); - if (message->data_size() <= max_frame_size) { + if (message->data_size() <= frame_size) { RESULT_CODE result = SendSingleFrameMessage(connection_handle, sessionID, message->protocol_version(), message->service_type(), @@ -394,14 +405,14 @@ void ProtocolHandlerImpl::SendMessageToMobileApp(const RawMessagePtr message, } else { LOG4CXX_DEBUG( logger_, - "Message will be sent in multiple frames; max frame size is " << max_frame_size); + "Message will be sent in multiple frames; max frame size is " << frame_size); RESULT_CODE result = SendMultiFrameMessage(connection_handle, sessionID, message->protocol_version(), message->service_type(), message->data_size(), message->data(), - max_frame_size, final_message); + frame_size, final_message); if (result != RESULT_OK) { LOG4CXX_ERROR(logger_, "ProtocolHandler failed to send multiframe messages."); diff --git a/src/components/protocol_handler/src/protocol_packet.cc b/src/components/protocol_handler/src/protocol_packet.cc index bdaabe3d35..e22d10e723 100644 --- a/src/components/protocol_handler/src/protocol_packet.cc +++ b/src/components/protocol_handler/src/protocol_packet.cc @@ -36,6 +36,7 @@ #include #include +#include "protocol/common.h" #include "protocol_handler/protocol_packet.h" #include "utils/macro.h" #include "utils/byte_order.h" @@ -118,7 +119,8 @@ void ProtocolPacket::ProtocolHeader::deserialize( } ProtocolPacket::ProtocolHeaderValidator::ProtocolHeaderValidator() - : max_payload_size_(std::numeric_limits::max()) {} + : max_payload_size_(std::numeric_limits::max()) { +} void ProtocolPacket::ProtocolHeaderValidator::set_max_payload_size( const size_t max_payload_size) { @@ -129,13 +131,20 @@ size_t ProtocolPacket::ProtocolHeaderValidator::max_payload_size() const { return max_payload_size_; } -RESULT_CODE ProtocolPacket::ProtocolHeaderValidator::validate(const ProtocolHeader& header) const { - // Protocol version shall be from 1 to 4 +RESULT_CODE ProtocolPacket::ProtocolHeaderValidator::validate( + const ProtocolHeader& header) const { + // expected payload size will be calculated depending + // on used protocol version + size_t payload_size = DEFAULT_FRAME_DATA_SIZE; + // Protocol version shall be from 1 to 3 switch (header.version) { case PROTOCOL_VERSION_1: case PROTOCOL_VERSION_2: + break; case PROTOCOL_VERSION_3: case PROTOCOL_VERSION_4: + payload_size = max_payload_size_ > DEFAULT_FRAME_DATA_SIZE ? + max_payload_size_ : DEFAULT_FRAME_DATA_SIZE; break; default: return RESULT_FAIL; @@ -185,8 +194,8 @@ RESULT_CODE ProtocolPacket::ProtocolHeaderValidator::validate(const ProtocolHead } // For Control frames Data Size value shall be less than MTU header // For Single and Consecutive Data Size value shall be greater than 0x00 - // and shall be less than N (this value will be defined in .ini file) - if (header.dataSize > max_payload_size_) { + // and shall be less than payload size + if (header.dataSize > payload_size) { return RESULT_FAIL; } switch (header.frameType) { @@ -209,7 +218,6 @@ RESULT_CODE ProtocolPacket::ProtocolHeaderValidator::validate(const ProtocolHead return RESULT_OK; } - ProtocolPacket::ProtocolPacket() : payload_size_(0), connection_id_(0) { } -- cgit v1.2.1