OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/quic/quic_framer.h" | 5 #include "net/quic/quic_framer.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 } | 297 } |
298 bool can_truncate = | 298 bool can_truncate = |
299 frame.type == ACK_FRAME && | 299 frame.type == ACK_FRAME && |
300 free_bytes >= GetMinAckFrameSize(PACKET_6BYTE_PACKET_NUMBER); | 300 free_bytes >= GetMinAckFrameSize(PACKET_6BYTE_PACKET_NUMBER); |
301 if (can_truncate) { | 301 if (can_truncate) { |
302 // Truncate the frame so the packet will not exceed kMaxPacketSize. | 302 // Truncate the frame so the packet will not exceed kMaxPacketSize. |
303 // Note that we may not use every byte of the writer in this case. | 303 // Note that we may not use every byte of the writer in this case. |
304 DVLOG(1) << "Truncating large frame, free bytes: " << free_bytes; | 304 DVLOG(1) << "Truncating large frame, free bytes: " << free_bytes; |
305 return free_bytes; | 305 return free_bytes; |
306 } | 306 } |
307 if (!FLAGS_quic_allow_oversized_packets_for_test) { | 307 return 0; |
308 return 0; | |
309 } | |
310 LOG(DFATAL) << "Packet size too small to fit frame."; | |
311 return frame_len; | |
312 } | 308 } |
313 | 309 |
314 QuicFramer::AckFrameInfo::AckFrameInfo() : max_delta(0) {} | 310 QuicFramer::AckFrameInfo::AckFrameInfo() : max_delta(0) {} |
315 | 311 |
316 QuicFramer::AckFrameInfo::~AckFrameInfo() {} | 312 QuicFramer::AckFrameInfo::~AckFrameInfo() {} |
317 | 313 |
318 // static | 314 // static |
319 QuicPacketEntropyHash QuicFramer::GetPacketEntropyHash( | 315 QuicPacketEntropyHash QuicFramer::GetPacketEntropyHash( |
320 const QuicPacketHeader& header) { | 316 const QuicPacketHeader& header) { |
321 return header.entropy_flag << (header.packet_number % 8); | 317 return header.entropy_flag << (header.packet_number % 8); |
(...skipping 1034 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1356 } | 1352 } |
1357 ack_frame->missing_packets.Add(last_packet_number - range_length, | 1353 ack_frame->missing_packets.Add(last_packet_number - range_length, |
1358 last_packet_number + 1); | 1354 last_packet_number + 1); |
1359 // Subtract an extra 1 to ensure ranges are represented efficiently and | 1355 // Subtract an extra 1 to ensure ranges are represented efficiently and |
1360 // can't overlap by 1 packet number. This allows a missing_delta of 0 | 1356 // can't overlap by 1 packet number. This allows a missing_delta of 0 |
1361 // to represent an adjacent nack range. | 1357 // to represent an adjacent nack range. |
1362 last_packet_number -= (range_length + 1); | 1358 last_packet_number -= (range_length + 1); |
1363 } | 1359 } |
1364 | 1360 |
1365 // Parse the revived packets list. | 1361 // Parse the revived packets list. |
| 1362 // TODO(ianswett): Change the ack frame so it only expresses one revived. |
1366 uint8 num_revived_packets; | 1363 uint8 num_revived_packets; |
1367 if (!reader->ReadBytes(&num_revived_packets, 1)) { | 1364 if (!reader->ReadBytes(&num_revived_packets, 1)) { |
1368 set_detailed_error("Unable to read num revived packets."); | 1365 set_detailed_error("Unable to read num revived packets."); |
1369 return false; | 1366 return false; |
1370 } | 1367 } |
1371 | 1368 |
1372 for (size_t i = 0; i < num_revived_packets; ++i) { | 1369 for (size_t i = 0; i < num_revived_packets; ++i) { |
1373 QuicPacketNumber revived_packet = 0; | 1370 QuicPacketNumber revived_packet = 0; |
1374 if (!reader->ReadBytes(&revived_packet, | 1371 if (!reader->ReadBytes(&revived_packet, |
1375 largest_observed_packet_number_length)) { | 1372 largest_observed_packet_number_length)) { |
1376 set_detailed_error("Unable to read revived packet."); | 1373 set_detailed_error("Unable to read revived packet."); |
1377 return false; | 1374 return false; |
1378 } | 1375 } |
1379 | 1376 ack_frame->latest_revived_packet = revived_packet; |
1380 ack_frame->revived_packets.insert(revived_packet); | |
1381 } | 1377 } |
1382 | 1378 |
1383 return true; | 1379 return true; |
1384 } | 1380 } |
1385 | 1381 |
1386 bool QuicFramer::ProcessTimestampsInAckFrame(QuicDataReader* reader, | 1382 bool QuicFramer::ProcessTimestampsInAckFrame(QuicDataReader* reader, |
1387 QuicAckFrame* ack_frame) { | 1383 QuicAckFrame* ack_frame) { |
1388 if (ack_frame->is_truncated) { | 1384 if (ack_frame->is_truncated) { |
1389 return true; | 1385 return true; |
1390 } | 1386 } |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1607 return alternative_decrypter_.get(); | 1603 return alternative_decrypter_.get(); |
1608 } | 1604 } |
1609 | 1605 |
1610 void QuicFramer::SetEncrypter(EncryptionLevel level, | 1606 void QuicFramer::SetEncrypter(EncryptionLevel level, |
1611 QuicEncrypter* encrypter) { | 1607 QuicEncrypter* encrypter) { |
1612 DCHECK_GE(level, 0); | 1608 DCHECK_GE(level, 0); |
1613 DCHECK_LT(level, NUM_ENCRYPTION_LEVELS); | 1609 DCHECK_LT(level, NUM_ENCRYPTION_LEVELS); |
1614 encrypter_[level].reset(encrypter); | 1610 encrypter_[level].reset(encrypter); |
1615 } | 1611 } |
1616 | 1612 |
1617 QuicEncryptedPacket* QuicFramer::EncryptPayload(EncryptionLevel level, | 1613 size_t QuicFramer::EncryptPayload(EncryptionLevel level, |
1618 QuicPacketNumber packet_number, | 1614 QuicPacketNumber packet_number, |
1619 const QuicPacket& packet, | 1615 const QuicPacket& packet, |
1620 char* buffer, | 1616 char* buffer, |
1621 size_t buffer_len) { | 1617 size_t buffer_len) { |
1622 DCHECK(encrypter_[level].get() != nullptr); | 1618 DCHECK(encrypter_[level].get() != nullptr); |
1623 | 1619 |
1624 const size_t encrypted_len = | |
1625 encrypter_[level]->GetCiphertextSize(packet.Plaintext().length()); | |
1626 StringPiece header_data = packet.BeforePlaintext(); | 1620 StringPiece header_data = packet.BeforePlaintext(); |
1627 const size_t total_len = header_data.length() + encrypted_len; | |
1628 | |
1629 char* encryption_buffer = buffer; | |
1630 // Allocate a large enough buffer for the header and the encrypted data. | |
1631 const bool is_new_buffer = total_len > buffer_len; | |
1632 if (is_new_buffer) { | |
1633 if (!FLAGS_quic_allow_oversized_packets_for_test) { | |
1634 LOG(DFATAL) << "Buffer of length:" << buffer_len | |
1635 << " is not large enough to encrypt length " << total_len; | |
1636 return nullptr; | |
1637 } | |
1638 encryption_buffer = new char[total_len]; | |
1639 } | |
1640 // Copy in the header, because the encrypter only populates the encrypted | 1621 // Copy in the header, because the encrypter only populates the encrypted |
1641 // plaintext content. | 1622 // plaintext content. |
1642 memcpy(encryption_buffer, header_data.data(), header_data.length()); | 1623 const size_t header_len = header_data.length(); |
| 1624 memcpy(buffer, header_data.data(), header_len); |
1643 // Encrypt the plaintext into the buffer. | 1625 // Encrypt the plaintext into the buffer. |
1644 size_t output_length = 0; | 1626 size_t output_length = 0; |
1645 if (!encrypter_[level]->EncryptPacket( | 1627 if (!encrypter_[level]->EncryptPacket( |
1646 packet_number, packet.AssociatedData(), packet.Plaintext(), | 1628 packet_number, packet.AssociatedData(), packet.Plaintext(), |
1647 encryption_buffer + header_data.length(), &output_length, | 1629 buffer + header_len, &output_length, buffer_len - header_len)) { |
1648 encrypted_len)) { | |
1649 RaiseError(QUIC_ENCRYPTION_FAILURE); | 1630 RaiseError(QUIC_ENCRYPTION_FAILURE); |
1650 return nullptr; | 1631 return 0; |
1651 } | 1632 } |
1652 | 1633 |
1653 return new QuicEncryptedPacket( | 1634 return header_len + output_length; |
1654 encryption_buffer, header_data.length() + output_length, is_new_buffer); | |
1655 } | 1635 } |
1656 | 1636 |
1657 size_t QuicFramer::GetMaxPlaintextSize(size_t ciphertext_size) { | 1637 size_t QuicFramer::GetMaxPlaintextSize(size_t ciphertext_size) { |
1658 // In order to keep the code simple, we don't have the current encryption | 1638 // In order to keep the code simple, we don't have the current encryption |
1659 // level to hand. Both the NullEncrypter and AES-GCM have a tag length of 12. | 1639 // level to hand. Both the NullEncrypter and AES-GCM have a tag length of 12. |
1660 size_t min_plaintext_size = ciphertext_size; | 1640 size_t min_plaintext_size = ciphertext_size; |
1661 | 1641 |
1662 for (int i = ENCRYPTION_NONE; i < NUM_ENCRYPTION_LEVELS; i++) { | 1642 for (int i = ENCRYPTION_NONE; i < NUM_ENCRYPTION_LEVELS; i++) { |
1663 if (encrypter_[i].get() != nullptr) { | 1643 if (encrypter_[i].get() != nullptr) { |
1664 size_t size = encrypter_[i]->GetMaxPlaintextSize(ciphertext_size); | 1644 size_t size = encrypter_[i]->GetMaxPlaintextSize(ciphertext_size); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1726 QuicPacketNumberLength largest_observed_length = | 1706 QuicPacketNumberLength largest_observed_length = |
1727 GetMinSequenceNumberLength(ack.largest_observed); | 1707 GetMinSequenceNumberLength(ack.largest_observed); |
1728 QuicPacketNumberLength missing_packet_number_length = | 1708 QuicPacketNumberLength missing_packet_number_length = |
1729 GetMinSequenceNumberLength(ack_info.max_delta); | 1709 GetMinSequenceNumberLength(ack_info.max_delta); |
1730 | 1710 |
1731 size_t ack_size = GetMinAckFrameSize(largest_observed_length); | 1711 size_t ack_size = GetMinAckFrameSize(largest_observed_length); |
1732 if (!ack_info.nack_ranges.empty()) { | 1712 if (!ack_info.nack_ranges.empty()) { |
1733 ack_size += kNumberOfNackRangesSize + kNumberOfRevivedPacketsSize; | 1713 ack_size += kNumberOfNackRangesSize + kNumberOfRevivedPacketsSize; |
1734 ack_size += min(ack_info.nack_ranges.size(), kMaxNackRanges) * | 1714 ack_size += min(ack_info.nack_ranges.size(), kMaxNackRanges) * |
1735 (missing_packet_number_length + PACKET_1BYTE_PACKET_NUMBER); | 1715 (missing_packet_number_length + PACKET_1BYTE_PACKET_NUMBER); |
1736 ack_size += min(ack.revived_packets.size(), | 1716 if (ack.latest_revived_packet != 0) { |
1737 kMaxRevivedPackets) * largest_observed_length; | 1717 ack_size += largest_observed_length; |
| 1718 } |
1738 } | 1719 } |
1739 | 1720 |
1740 // In version 23, if the ack will be truncated due to too many nack ranges, | 1721 // In version 23, if the ack will be truncated due to too many nack ranges, |
1741 // then do not include the number of timestamps (1 byte). | 1722 // then do not include the number of timestamps (1 byte). |
1742 if (ack_info.nack_ranges.size() <= kMaxNackRanges) { | 1723 if (ack_info.nack_ranges.size() <= kMaxNackRanges) { |
1743 // 1 byte for the number of timestamps. | 1724 // 1 byte for the number of timestamps. |
1744 ack_size += 1; | 1725 ack_size += 1; |
1745 if (ack.received_packet_times.size() > 0) { | 1726 if (ack.received_packet_times.size() > 0) { |
1746 // 1 byte for packet number, 4 bytes for timestamp for the first | 1727 // 1 byte for packet number, 4 bytes for timestamp for the first |
1747 // packet. | 1728 // packet. |
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2019 return false; | 2000 return false; |
2020 } | 2001 } |
2021 // Subtract 1 so a missing_delta of 0 means an adjacent range. | 2002 // Subtract 1 so a missing_delta of 0 means an adjacent range. |
2022 last_sequence_written = ack_iter->first - 1; | 2003 last_sequence_written = ack_iter->first - 1; |
2023 ++num_ranges_written; | 2004 ++num_ranges_written; |
2024 } | 2005 } |
2025 DCHECK_EQ(num_missing_ranges, num_ranges_written); | 2006 DCHECK_EQ(num_missing_ranges, num_ranges_written); |
2026 | 2007 |
2027 // Append revived packets. | 2008 // Append revived packets. |
2028 // If not all the revived packets fit, only mention the ones that do. | 2009 // If not all the revived packets fit, only mention the ones that do. |
2029 uint8 num_revived_packets = | 2010 uint8 num_revived_packets = frame.latest_revived_packet == 0 ? 0 : 1; |
2030 static_cast<uint8>(min(frame.revived_packets.size(), kMaxRevivedPackets)); | 2011 if (((writer->capacity() - writer->length()) / largest_observed_length) == |
2031 num_revived_packets = static_cast<uint8>(min( | 2012 0) { |
2032 static_cast<size_t>(num_revived_packets), | 2013 num_revived_packets = 0; |
2033 (writer->capacity() - writer->length()) / largest_observed_length)); | 2014 } |
2034 if (!writer->WriteBytes(&num_revived_packets, 1)) { | 2015 if (!writer->WriteBytes(&num_revived_packets, 1)) { |
2035 return false; | 2016 return false; |
2036 } | 2017 } |
2037 | 2018 |
2038 PacketNumberSet::const_iterator iter = frame.revived_packets.begin(); | 2019 if (num_revived_packets > 0) { |
2039 for (int i = 0; i < num_revived_packets; ++i, ++iter) { | 2020 LOG_IF(DFATAL, |
2040 LOG_IF(DFATAL, !frame.missing_packets.Contains(*iter)); | 2021 !frame.missing_packets.Contains(frame.latest_revived_packet)); |
2041 if (!AppendPacketSequenceNumber(largest_observed_length, | 2022 if (!AppendPacketSequenceNumber(largest_observed_length, |
2042 *iter, writer)) { | 2023 frame.latest_revived_packet, writer)) { |
2043 return false; | 2024 return false; |
2044 } | 2025 } |
2045 } | 2026 } |
2046 | 2027 |
2047 return true; | 2028 return true; |
2048 } | 2029 } |
2049 | 2030 |
2050 bool QuicFramer::AppendTimestampToAckFrame(const QuicAckFrame& frame, | 2031 bool QuicFramer::AppendTimestampToAckFrame(const QuicAckFrame& frame, |
2051 QuicDataWriter* writer) { | 2032 QuicDataWriter* writer) { |
2052 DCHECK_GE(numeric_limits<uint8>::max(), frame.received_packet_times.size()); | 2033 DCHECK_GE(numeric_limits<uint8>::max(), frame.received_packet_times.size()); |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2212 | 2193 |
2213 bool QuicFramer::RaiseError(QuicErrorCode error) { | 2194 bool QuicFramer::RaiseError(QuicErrorCode error) { |
2214 DVLOG(1) << "Error: " << QuicUtils::ErrorToString(error) | 2195 DVLOG(1) << "Error: " << QuicUtils::ErrorToString(error) |
2215 << " detail: " << detailed_error_; | 2196 << " detail: " << detailed_error_; |
2216 set_error(error); | 2197 set_error(error); |
2217 visitor_->OnError(this); | 2198 visitor_->OnError(this); |
2218 return false; | 2199 return false; |
2219 } | 2200 } |
2220 | 2201 |
2221 } // namespace net | 2202 } // namespace net |
OLD | NEW |