| 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/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 quic_version_ = supported_versions_[0]; | 152 quic_version_ = supported_versions_[0]; |
| 153 decrypter_.reset(QuicDecrypter::Create(kNULL)); | 153 decrypter_.reset(QuicDecrypter::Create(kNULL)); |
| 154 encrypter_[ENCRYPTION_NONE].reset(QuicEncrypter::Create(kNULL)); | 154 encrypter_[ENCRYPTION_NONE].reset(QuicEncrypter::Create(kNULL)); |
| 155 } | 155 } |
| 156 | 156 |
| 157 QuicFramer::~QuicFramer() {} | 157 QuicFramer::~QuicFramer() {} |
| 158 | 158 |
| 159 // static | 159 // static |
| 160 size_t QuicFramer::GetMinStreamFrameSize(QuicStreamId stream_id, | 160 size_t QuicFramer::GetMinStreamFrameSize(QuicStreamId stream_id, |
| 161 QuicStreamOffset offset, | 161 QuicStreamOffset offset, |
| 162 bool last_frame_in_packet, | 162 bool last_frame_in_packet) { |
| 163 InFecGroup is_in_fec_group) { | |
| 164 bool no_stream_frame_length = | |
| 165 last_frame_in_packet && is_in_fec_group == NOT_IN_FEC_GROUP; | |
| 166 return kQuicFrameTypeSize + GetStreamIdSize(stream_id) + | 163 return kQuicFrameTypeSize + GetStreamIdSize(stream_id) + |
| 167 GetStreamOffsetSize(offset) + | 164 GetStreamOffsetSize(offset) + |
| 168 (no_stream_frame_length ? 0 : kQuicStreamPayloadLengthSize); | 165 (last_frame_in_packet ? 0 : kQuicStreamPayloadLengthSize); |
| 169 } | 166 } |
| 170 | 167 |
| 171 // static | 168 // static |
| 172 size_t QuicFramer::GetMinAckFrameSize( | 169 size_t QuicFramer::GetMinAckFrameSize( |
| 173 QuicPacketNumberLength largest_observed_length) { | 170 QuicPacketNumberLength largest_observed_length) { |
| 174 return kQuicFrameTypeSize + kQuicEntropyHashSize + largest_observed_length + | 171 return kQuicFrameTypeSize + kQuicEntropyHashSize + largest_observed_length + |
| 175 kQuicDeltaTimeLargestObservedSize; | 172 kQuicDeltaTimeLargestObservedSize; |
| 176 } | 173 } |
| 177 | 174 |
| 178 // static | 175 // static |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 } | 260 } |
| 264 } | 261 } |
| 265 return false; | 262 return false; |
| 266 } | 263 } |
| 267 | 264 |
| 268 size_t QuicFramer::GetSerializedFrameLength( | 265 size_t QuicFramer::GetSerializedFrameLength( |
| 269 const QuicFrame& frame, | 266 const QuicFrame& frame, |
| 270 size_t free_bytes, | 267 size_t free_bytes, |
| 271 bool first_frame, | 268 bool first_frame, |
| 272 bool last_frame, | 269 bool last_frame, |
| 273 InFecGroup is_in_fec_group, | |
| 274 QuicPacketNumberLength packet_number_length) { | 270 QuicPacketNumberLength packet_number_length) { |
| 275 // Prevent a rare crash reported in b/19458523. | 271 // Prevent a rare crash reported in b/19458523. |
| 276 if ((frame.type == STREAM_FRAME || frame.type == ACK_FRAME) && | 272 if ((frame.type == STREAM_FRAME || frame.type == ACK_FRAME) && |
| 277 frame.stream_frame == nullptr) { | 273 frame.stream_frame == nullptr) { |
| 278 QUIC_BUG << "Cannot compute the length of a null frame. " | 274 QUIC_BUG << "Cannot compute the length of a null frame. " |
| 279 << "type:" << frame.type << "free_bytes:" << free_bytes | 275 << "type:" << frame.type << "free_bytes:" << free_bytes |
| 280 << " first_frame:" << first_frame << " last_frame:" << last_frame | 276 << " first_frame:" << first_frame << " last_frame:" << last_frame |
| 281 << " is_in_fec:" << is_in_fec_group | |
| 282 << " seq num length:" << packet_number_length; | 277 << " seq num length:" << packet_number_length; |
| 283 set_error(QUIC_INTERNAL_ERROR); | 278 set_error(QUIC_INTERNAL_ERROR); |
| 284 visitor_->OnError(this); | 279 visitor_->OnError(this); |
| 285 return 0; | 280 return 0; |
| 286 } | 281 } |
| 287 if (frame.type == PADDING_FRAME) { | 282 if (frame.type == PADDING_FRAME) { |
| 288 // PADDING implies end of packet. | 283 // PADDING implies end of packet. |
| 289 return free_bytes; | 284 return free_bytes; |
| 290 } | 285 } |
| 291 size_t frame_len = ComputeFrameLength(frame, last_frame, is_in_fec_group, | 286 size_t frame_len = |
| 292 packet_number_length); | 287 ComputeFrameLength(frame, last_frame, packet_number_length); |
| 293 if (frame_len <= free_bytes) { | 288 if (frame_len <= free_bytes) { |
| 294 // Frame fits within packet. Note that acks may be truncated. | 289 // Frame fits within packet. Note that acks may be truncated. |
| 295 return frame_len; | 290 return frame_len; |
| 296 } | 291 } |
| 297 // Only truncate the first frame in a packet, so if subsequent ones go | 292 // Only truncate the first frame in a packet, so if subsequent ones go |
| 298 // over, stop including more frames. | 293 // over, stop including more frames. |
| 299 if (!first_frame) { | 294 if (!first_frame) { |
| 300 return 0; | 295 return 0; |
| 301 } | 296 } |
| 302 bool can_truncate = | 297 bool can_truncate = |
| (...skipping 26 matching lines...) Expand all Loading... |
| 329 size_t packet_length) { | 324 size_t packet_length) { |
| 330 QuicDataWriter writer(packet_length, buffer); | 325 QuicDataWriter writer(packet_length, buffer); |
| 331 if (!AppendPacketHeader(header, &writer)) { | 326 if (!AppendPacketHeader(header, &writer)) { |
| 332 QUIC_BUG << "AppendPacketHeader failed"; | 327 QUIC_BUG << "AppendPacketHeader failed"; |
| 333 return 0; | 328 return 0; |
| 334 } | 329 } |
| 335 | 330 |
| 336 size_t i = 0; | 331 size_t i = 0; |
| 337 for (const QuicFrame& frame : frames) { | 332 for (const QuicFrame& frame : frames) { |
| 338 // Determine if we should write stream frame length in header. | 333 // Determine if we should write stream frame length in header. |
| 339 const bool no_stream_frame_length = | 334 const bool no_stream_frame_length = i == frames.size() - 1; |
| 340 (header.is_in_fec_group == NOT_IN_FEC_GROUP) && | |
| 341 (i == frames.size() - 1); | |
| 342 if (!AppendTypeByte(frame, no_stream_frame_length, &writer)) { | 335 if (!AppendTypeByte(frame, no_stream_frame_length, &writer)) { |
| 343 QUIC_BUG << "AppendTypeByte failed"; | 336 QUIC_BUG << "AppendTypeByte failed"; |
| 344 return 0; | 337 return 0; |
| 345 } | 338 } |
| 346 | 339 |
| 347 switch (frame.type) { | 340 switch (frame.type) { |
| 348 case PADDING_FRAME: | 341 case PADDING_FRAME: |
| 349 writer.WritePadding(); | 342 writer.WritePadding(); |
| 350 break; | 343 break; |
| 351 case STREAM_FRAME: | 344 case STREAM_FRAME: |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 RaiseError(QUIC_INVALID_FRAME_DATA); | 407 RaiseError(QUIC_INVALID_FRAME_DATA); |
| 415 QUIC_BUG << "QUIC_INVALID_FRAME_DATA"; | 408 QUIC_BUG << "QUIC_INVALID_FRAME_DATA"; |
| 416 return 0; | 409 return 0; |
| 417 } | 410 } |
| 418 ++i; | 411 ++i; |
| 419 } | 412 } |
| 420 | 413 |
| 421 return writer.length(); | 414 return writer.length(); |
| 422 } | 415 } |
| 423 | 416 |
| 424 QuicPacket* QuicFramer::BuildFecPacket(const QuicPacketHeader& header, | |
| 425 StringPiece redundancy) { | |
| 426 DCHECK_EQ(IN_FEC_GROUP, header.is_in_fec_group); | |
| 427 DCHECK_NE(0u, header.fec_group); | |
| 428 size_t len = GetPacketHeaderSize(header); | |
| 429 len += redundancy.length(); | |
| 430 | |
| 431 scoped_ptr<char[]> buffer(new char[len]); | |
| 432 QuicDataWriter writer(len, buffer.get()); | |
| 433 if (!AppendPacketHeader(header, &writer)) { | |
| 434 QUIC_BUG << "AppendPacketHeader failed"; | |
| 435 return nullptr; | |
| 436 } | |
| 437 | |
| 438 if (!writer.WriteBytes(redundancy.data(), redundancy.length())) { | |
| 439 QUIC_BUG << "Failed to add FEC"; | |
| 440 return nullptr; | |
| 441 } | |
| 442 | |
| 443 return new QuicPacket( | |
| 444 buffer.release(), len, true, header.public_header.connection_id_length, | |
| 445 header.public_header.version_flag, header.public_header.multipath_flag, | |
| 446 header.public_header.packet_number_length); | |
| 447 } | |
| 448 | |
| 449 // static | 417 // static |
| 450 QuicEncryptedPacket* QuicFramer::BuildPublicResetPacket( | 418 QuicEncryptedPacket* QuicFramer::BuildPublicResetPacket( |
| 451 const QuicPublicResetPacket& packet) { | 419 const QuicPublicResetPacket& packet) { |
| 452 DCHECK(packet.public_header.reset_flag); | 420 DCHECK(packet.public_header.reset_flag); |
| 453 | 421 |
| 454 CryptoHandshakeMessage reset; | 422 CryptoHandshakeMessage reset; |
| 455 reset.set_tag(kPRST); | 423 reset.set_tag(kPRST); |
| 456 reset.SetValue(kRNON, packet.nonce_proof); | 424 reset.SetValue(kRNON, packet.nonce_proof); |
| 457 reset.SetValue(kRSEQ, packet.rejected_packet_number); | 425 reset.SetValue(kRSEQ, packet.rejected_packet_number); |
| 458 if (!packet.client_address.address().empty()) { | 426 if (!packet.client_address.address().empty()) { |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 612 // The visitor suppresses further processing of the packet. | 580 // The visitor suppresses further processing of the packet. |
| 613 return true; | 581 return true; |
| 614 } | 582 } |
| 615 | 583 |
| 616 if (packet.length() > kMaxPacketSize) { | 584 if (packet.length() > kMaxPacketSize) { |
| 617 // If the packet has gotten this far, it should not be too large. | 585 // If the packet has gotten this far, it should not be too large. |
| 618 QUIC_BUG << "Packet too large:" << packet.length(); | 586 QUIC_BUG << "Packet too large:" << packet.length(); |
| 619 return RaiseError(QUIC_PACKET_TOO_LARGE); | 587 return RaiseError(QUIC_PACKET_TOO_LARGE); |
| 620 } | 588 } |
| 621 | 589 |
| 590 DCHECK(!header.fec_flag); |
| 622 // Handle the payload. | 591 // Handle the payload. |
| 623 if (!header.fec_flag) { | 592 if (!ProcessFrameData(&reader, header)) { |
| 624 if (header.is_in_fec_group == IN_FEC_GROUP) { | 593 DCHECK_NE(QUIC_NO_ERROR, error_); // ProcessFrameData sets the error. |
| 625 StringPiece payload = reader.PeekRemainingPayload(); | 594 DLOG(WARNING) << "Unable to process frame data."; |
| 626 visitor_->OnFecProtectedPayload(payload); | 595 return false; |
| 627 } | |
| 628 if (!ProcessFrameData(&reader, header)) { | |
| 629 DCHECK_NE(QUIC_NO_ERROR, error_); // ProcessFrameData sets the error. | |
| 630 DLOG(WARNING) << "Unable to process frame data."; | |
| 631 return false; | |
| 632 } | |
| 633 } else { | |
| 634 visitor_->OnFecData(reader.ReadRemainingPayload()); | |
| 635 } | 596 } |
| 636 | 597 |
| 637 visitor_->OnPacketComplete(); | 598 visitor_->OnPacketComplete(); |
| 638 return true; | 599 return true; |
| 639 } | 600 } |
| 640 | 601 |
| 641 bool QuicFramer::ProcessPublicResetPacket( | 602 bool QuicFramer::ProcessPublicResetPacket( |
| 642 QuicDataReader* reader, | 603 QuicDataReader* reader, |
| 643 const QuicPacketPublicHeader& public_header) { | 604 const QuicPacketPublicHeader& public_header) { |
| 644 QuicPublicResetPacket packet(public_header); | 605 QuicPublicResetPacket packet(public_header); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 672 if (address_coder.Decode(address.data(), address.length())) { | 633 if (address_coder.Decode(address.data(), address.length())) { |
| 673 packet.client_address = | 634 packet.client_address = |
| 674 IPEndPoint(address_coder.ip(), address_coder.port()); | 635 IPEndPoint(address_coder.ip(), address_coder.port()); |
| 675 } | 636 } |
| 676 } | 637 } |
| 677 | 638 |
| 678 visitor_->OnPublicResetPacket(packet); | 639 visitor_->OnPublicResetPacket(packet); |
| 679 return true; | 640 return true; |
| 680 } | 641 } |
| 681 | 642 |
| 682 bool QuicFramer::ProcessRevivedPacket(QuicPacketHeader* header, | |
| 683 StringPiece payload) { | |
| 684 visitor_->OnRevivedPacket(); | |
| 685 | |
| 686 header->entropy_hash = GetPacketEntropyHash(*header); | |
| 687 | |
| 688 if (!visitor_->OnPacketHeader(*header)) { | |
| 689 return true; | |
| 690 } | |
| 691 | |
| 692 if (payload.length() > kMaxPacketSize) { | |
| 693 set_detailed_error("Revived packet too large."); | |
| 694 return RaiseError(QUIC_PACKET_TOO_LARGE); | |
| 695 } | |
| 696 | |
| 697 QuicDataReader reader(payload.data(), payload.length()); | |
| 698 if (!ProcessFrameData(&reader, *header)) { | |
| 699 DCHECK_NE(QUIC_NO_ERROR, error_); // ProcessFrameData sets the error. | |
| 700 DLOG(WARNING) << "Unable to process frame data."; | |
| 701 return false; | |
| 702 } | |
| 703 | |
| 704 visitor_->OnPacketComplete(); | |
| 705 return true; | |
| 706 } | |
| 707 | |
| 708 bool QuicFramer::AppendPacketHeader(const QuicPacketHeader& header, | 643 bool QuicFramer::AppendPacketHeader(const QuicPacketHeader& header, |
| 709 QuicDataWriter* writer) { | 644 QuicDataWriter* writer) { |
| 710 DVLOG(1) << "Appending header: " << header; | 645 DVLOG(1) << "Appending header: " << header; |
| 711 DCHECK(header.fec_group > 0 || header.is_in_fec_group == NOT_IN_FEC_GROUP); | |
| 712 uint8_t public_flags = 0; | 646 uint8_t public_flags = 0; |
| 713 if (header.public_header.reset_flag) { | 647 if (header.public_header.reset_flag) { |
| 714 public_flags |= PACKET_PUBLIC_FLAGS_RST; | 648 public_flags |= PACKET_PUBLIC_FLAGS_RST; |
| 715 } | 649 } |
| 716 if (header.public_header.version_flag) { | 650 if (header.public_header.version_flag) { |
| 717 public_flags |= PACKET_PUBLIC_FLAGS_VERSION; | 651 public_flags |= PACKET_PUBLIC_FLAGS_VERSION; |
| 718 } | 652 } |
| 719 if (header.public_header.multipath_flag) { | 653 if (header.public_header.multipath_flag) { |
| 720 public_flags |= PACKET_PUBLIC_FLAGS_MULTIPATH; | 654 public_flags |= PACKET_PUBLIC_FLAGS_MULTIPATH; |
| 721 } | 655 } |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 778 | 712 |
| 779 if (!AppendPacketSequenceNumber(header.public_header.packet_number_length, | 713 if (!AppendPacketSequenceNumber(header.public_header.packet_number_length, |
| 780 header.packet_number, writer)) { | 714 header.packet_number, writer)) { |
| 781 return false; | 715 return false; |
| 782 } | 716 } |
| 783 | 717 |
| 784 uint8_t private_flags = 0; | 718 uint8_t private_flags = 0; |
| 785 if (header.entropy_flag) { | 719 if (header.entropy_flag) { |
| 786 private_flags |= PACKET_PRIVATE_FLAGS_ENTROPY; | 720 private_flags |= PACKET_PRIVATE_FLAGS_ENTROPY; |
| 787 } | 721 } |
| 788 if (header.is_in_fec_group == IN_FEC_GROUP) { | |
| 789 private_flags |= PACKET_PRIVATE_FLAGS_FEC_GROUP; | |
| 790 } | |
| 791 if (header.fec_flag) { | |
| 792 private_flags |= PACKET_PRIVATE_FLAGS_FEC; | |
| 793 } | |
| 794 if (!writer->WriteUInt8(private_flags)) { | 722 if (!writer->WriteUInt8(private_flags)) { |
| 795 return false; | 723 return false; |
| 796 } | 724 } |
| 797 | 725 |
| 798 // The FEC group number is the packet number of the first fec | |
| 799 // protected packet, or 0 if this packet is not protected. | |
| 800 if (header.is_in_fec_group == IN_FEC_GROUP) { | |
| 801 DCHECK_LE(header.fec_group, header.packet_number); | |
| 802 DCHECK_LT(header.packet_number - header.fec_group, 255u); | |
| 803 // Offset from the current packet number to the first fec | |
| 804 // protected packet. | |
| 805 uint8_t first_fec_protected_packet_offset = | |
| 806 static_cast<uint8_t>(header.packet_number - header.fec_group); | |
| 807 if (!writer->WriteBytes(&first_fec_protected_packet_offset, 1)) { | |
| 808 return false; | |
| 809 } | |
| 810 } | |
| 811 | |
| 812 return true; | 726 return true; |
| 813 } | 727 } |
| 814 | 728 |
| 815 const QuicTime::Delta QuicFramer::CalculateTimestampFromWire( | 729 const QuicTime::Delta QuicFramer::CalculateTimestampFromWire( |
| 816 uint32_t time_delta_us) { | 730 uint32_t time_delta_us) { |
| 817 // The new time_delta might have wrapped to the next epoch, or it | 731 // The new time_delta might have wrapped to the next epoch, or it |
| 818 // might have reverse wrapped to the previous epoch, or it might | 732 // might have reverse wrapped to the previous epoch, or it might |
| 819 // remain in the same epoch. Select the time closest to the previous | 733 // remain in the same epoch. Select the time closest to the previous |
| 820 // time. | 734 // time. |
| 821 // | 735 // |
| (...skipping 1020 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1842 ack_size += 3 * (ack.received_packet_times.size() - 1); | 1756 ack_size += 3 * (ack.received_packet_times.size() - 1); |
| 1843 } | 1757 } |
| 1844 } | 1758 } |
| 1845 | 1759 |
| 1846 return ack_size; | 1760 return ack_size; |
| 1847 } | 1761 } |
| 1848 | 1762 |
| 1849 size_t QuicFramer::ComputeFrameLength( | 1763 size_t QuicFramer::ComputeFrameLength( |
| 1850 const QuicFrame& frame, | 1764 const QuicFrame& frame, |
| 1851 bool last_frame_in_packet, | 1765 bool last_frame_in_packet, |
| 1852 InFecGroup is_in_fec_group, | |
| 1853 QuicPacketNumberLength packet_number_length) { | 1766 QuicPacketNumberLength packet_number_length) { |
| 1854 switch (frame.type) { | 1767 switch (frame.type) { |
| 1855 case STREAM_FRAME: | 1768 case STREAM_FRAME: |
| 1856 return GetMinStreamFrameSize(frame.stream_frame->stream_id, | 1769 return GetMinStreamFrameSize(frame.stream_frame->stream_id, |
| 1857 frame.stream_frame->offset, | 1770 frame.stream_frame->offset, |
| 1858 last_frame_in_packet, is_in_fec_group) + | 1771 last_frame_in_packet) + |
| 1859 frame.stream_frame->frame_length; | 1772 frame.stream_frame->frame_length; |
| 1860 case ACK_FRAME: { | 1773 case ACK_FRAME: { |
| 1861 return GetAckFrameSize(*frame.ack_frame, packet_number_length); | 1774 return GetAckFrameSize(*frame.ack_frame, packet_number_length); |
| 1862 } | 1775 } |
| 1863 case STOP_WAITING_FRAME: | 1776 case STOP_WAITING_FRAME: |
| 1864 return GetStopWaitingFrameSize(packet_number_length); | 1777 return GetStopWaitingFrameSize(packet_number_length); |
| 1865 case MTU_DISCOVERY_FRAME: | 1778 case MTU_DISCOVERY_FRAME: |
| 1866 // MTU discovery frames are serialized as ping frames. | 1779 // MTU discovery frames are serialized as ping frames. |
| 1867 case PING_FRAME: | 1780 case PING_FRAME: |
| 1868 // Ping has no payload. | 1781 // Ping has no payload. |
| (...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2308 | 2221 |
| 2309 bool QuicFramer::RaiseError(QuicErrorCode error) { | 2222 bool QuicFramer::RaiseError(QuicErrorCode error) { |
| 2310 DVLOG(1) << "Error: " << QuicUtils::ErrorToString(error) | 2223 DVLOG(1) << "Error: " << QuicUtils::ErrorToString(error) |
| 2311 << " detail: " << detailed_error_; | 2224 << " detail: " << detailed_error_; |
| 2312 set_error(error); | 2225 set_error(error); |
| 2313 visitor_->OnError(this); | 2226 visitor_->OnError(this); |
| 2314 return false; | 2227 return false; |
| 2315 } | 2228 } |
| 2316 | 2229 |
| 2317 } // namespace net | 2230 } // namespace net |
| OLD | NEW |