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 "base/containers/hash_tables.h" | 7 #include "base/containers/hash_tables.h" |
8 #include "net/quic/crypto/quic_decrypter.h" | 8 #include "net/quic/crypto/quic_decrypter.h" |
9 #include "net/quic/crypto/quic_encrypter.h" | 9 #include "net/quic/crypto/quic_encrypter.h" |
10 #include "net/quic/quic_data_reader.h" | 10 #include "net/quic/quic_data_reader.h" |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 encrypter_[ENCRYPTION_NONE].reset(QuicEncrypter::Create(kNULL)); | 96 encrypter_[ENCRYPTION_NONE].reset(QuicEncrypter::Create(kNULL)); |
97 } | 97 } |
98 | 98 |
99 QuicFramer::~QuicFramer() {} | 99 QuicFramer::~QuicFramer() {} |
100 | 100 |
101 // static | 101 // static |
102 size_t QuicFramer::GetMinStreamFrameSize(QuicVersion version, | 102 size_t QuicFramer::GetMinStreamFrameSize(QuicVersion version, |
103 QuicStreamId stream_id, | 103 QuicStreamId stream_id, |
104 QuicStreamOffset offset, | 104 QuicStreamOffset offset, |
105 bool last_frame_in_packet) { | 105 bool last_frame_in_packet) { |
106 if (version == QUIC_VERSION_6) { | |
107 return kQuicFrameTypeSize + kQuicMaxStreamIdSize + | |
108 kQuicStreamFinSize + kQuicMaxStreamOffsetSize + | |
109 kQuicStreamPayloadLengthSize; | |
110 } | |
111 return kQuicFrameTypeSize + GetStreamIdSize(stream_id) + | 106 return kQuicFrameTypeSize + GetStreamIdSize(stream_id) + |
112 GetStreamOffsetSize(offset) + | 107 GetStreamOffsetSize(offset) + |
113 (last_frame_in_packet ? 0 : kQuicStreamPayloadLengthSize); | 108 (last_frame_in_packet ? 0 : kQuicStreamPayloadLengthSize); |
114 } | 109 } |
115 | 110 |
116 // static | 111 // static |
117 size_t QuicFramer::GetMinAckFrameSize() { | 112 size_t QuicFramer::GetMinAckFrameSize() { |
118 return kQuicFrameTypeSize + kQuicEntropyHashSize + | 113 return kQuicFrameTypeSize + kQuicEntropyHashSize + |
119 PACKET_6BYTE_SEQUENCE_NUMBER + kQuicEntropyHashSize + | 114 PACKET_6BYTE_SEQUENCE_NUMBER + kQuicEntropyHashSize + |
120 PACKET_6BYTE_SEQUENCE_NUMBER + kQuicDeltaTimeLargestObservedSize + | 115 PACKET_6BYTE_SEQUENCE_NUMBER + kQuicDeltaTimeLargestObservedSize + |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 const bool last_frame_in_packet = i == (frames.size() - 1); | 271 const bool last_frame_in_packet = i == (frames.size() - 1); |
277 if (!AppendTypeByte(frame, last_frame_in_packet, &writer)) { | 272 if (!AppendTypeByte(frame, last_frame_in_packet, &writer)) { |
278 return kNoPacket; | 273 return kNoPacket; |
279 } | 274 } |
280 | 275 |
281 switch (frame.type) { | 276 switch (frame.type) { |
282 case PADDING_FRAME: | 277 case PADDING_FRAME: |
283 writer.WritePadding(); | 278 writer.WritePadding(); |
284 break; | 279 break; |
285 case STREAM_FRAME: | 280 case STREAM_FRAME: |
286 if (quic_version_ == QUIC_VERSION_6) { | 281 if (!AppendStreamFramePayload( |
287 if (!AppendV6StreamFramePayload(*frame.stream_frame, &writer)) { | 282 *frame.stream_frame, last_frame_in_packet, &writer)) { |
288 return kNoPacket; | 283 return kNoPacket; |
289 } | |
290 } else { | |
291 if (!AppendStreamFramePayload( | |
292 *frame.stream_frame, last_frame_in_packet, &writer)) { | |
293 return kNoPacket; | |
294 } | |
295 } | 284 } |
296 break; | 285 break; |
297 case ACK_FRAME: | 286 case ACK_FRAME: |
298 if (!AppendAckFramePayload(*frame.ack_frame, &writer)) { | 287 if (!AppendAckFramePayload(*frame.ack_frame, &writer)) { |
299 return kNoPacket; | 288 return kNoPacket; |
300 } | 289 } |
301 break; | 290 break; |
302 case CONGESTION_FEEDBACK_FRAME: | 291 case CONGESTION_FEEDBACK_FRAME: |
303 if (!AppendQuicCongestionFeedbackFramePayload( | 292 if (!AppendQuicCongestionFeedbackFramePayload( |
304 *frame.congestion_feedback_frame, &writer)) { | 293 *frame.congestion_feedback_frame, &writer)) { |
(...skipping 586 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
891 set_detailed_error("Unable to read frame type."); | 880 set_detailed_error("Unable to read frame type."); |
892 return RaiseError(QUIC_INVALID_FRAME_DATA); | 881 return RaiseError(QUIC_INVALID_FRAME_DATA); |
893 } | 882 } |
894 while (!reader_->IsDoneReading()) { | 883 while (!reader_->IsDoneReading()) { |
895 uint8 frame_type; | 884 uint8 frame_type; |
896 if (!reader_->ReadBytes(&frame_type, 1)) { | 885 if (!reader_->ReadBytes(&frame_type, 1)) { |
897 set_detailed_error("Unable to read frame type."); | 886 set_detailed_error("Unable to read frame type."); |
898 return RaiseError(QUIC_INVALID_FRAME_DATA); | 887 return RaiseError(QUIC_INVALID_FRAME_DATA); |
899 } | 888 } |
900 | 889 |
901 if (quic_version_ >= QUIC_VERSION_7) { | 890 if ((frame_type & kQuicFrameType0BitMask) == 0) { |
902 if ((frame_type & kQuicFrameType0BitMask) == 0) { | 891 QuicStreamFrame frame; |
903 QuicStreamFrame frame; | 892 if (!ProcessStreamFrame(frame_type, &frame)) { |
904 if (!ProcessStreamFrame(frame_type, &frame)) { | 893 return RaiseError(QUIC_INVALID_FRAME_DATA); |
905 return RaiseError(QUIC_INVALID_FRAME_DATA); | |
906 } | |
907 if (!visitor_->OnStreamFrame(frame)) { | |
908 DLOG(INFO) << "Visitor asked to stop further processing."; | |
909 // Returning true since there was no parsing error. | |
910 return true; | |
911 } | |
912 continue; | |
913 } | 894 } |
914 | 895 if (!visitor_->OnStreamFrame(frame)) { |
915 frame_type >>= 1; | 896 DLOG(INFO) << "Visitor asked to stop further processing."; |
916 if ((frame_type & kQuicFrameType0BitMask) == 0) { | 897 // Returning true since there was no parsing error. |
917 QuicAckFrame frame; | 898 return true; |
918 if (!ProcessAckFrame(&frame)) { | |
919 return RaiseError(QUIC_INVALID_FRAME_DATA); | |
920 } | |
921 if (!visitor_->OnAckFrame(frame)) { | |
922 DLOG(INFO) << "Visitor asked to stop further processing."; | |
923 // Returning true since there was no parsing error. | |
924 return true; | |
925 } | |
926 continue; | |
927 } | 899 } |
928 | 900 continue; |
929 frame_type >>= 1; | |
930 if ((frame_type & kQuicFrameType0BitMask) == 0) { | |
931 QuicCongestionFeedbackFrame frame; | |
932 if (!ProcessQuicCongestionFeedbackFrame(&frame)) { | |
933 return RaiseError(QUIC_INVALID_FRAME_DATA); | |
934 } | |
935 if (!visitor_->OnCongestionFeedbackFrame(frame)) { | |
936 DLOG(INFO) << "Visitor asked to stop further processing."; | |
937 // Returning true since there was no parsing error. | |
938 return true; | |
939 } | |
940 continue; | |
941 } | |
942 | |
943 frame_type >>= 1; | |
944 } | 901 } |
945 | 902 |
| 903 frame_type >>= 1; |
| 904 if ((frame_type & kQuicFrameType0BitMask) == 0) { |
| 905 QuicAckFrame frame; |
| 906 if (!ProcessAckFrame(&frame)) { |
| 907 return RaiseError(QUIC_INVALID_FRAME_DATA); |
| 908 } |
| 909 if (!visitor_->OnAckFrame(frame)) { |
| 910 DLOG(INFO) << "Visitor asked to stop further processing."; |
| 911 // Returning true since there was no parsing error. |
| 912 return true; |
| 913 } |
| 914 continue; |
| 915 } |
| 916 |
| 917 frame_type >>= 1; |
| 918 if ((frame_type & kQuicFrameType0BitMask) == 0) { |
| 919 QuicCongestionFeedbackFrame frame; |
| 920 if (!ProcessQuicCongestionFeedbackFrame(&frame)) { |
| 921 return RaiseError(QUIC_INVALID_FRAME_DATA); |
| 922 } |
| 923 if (!visitor_->OnCongestionFeedbackFrame(frame)) { |
| 924 DLOG(INFO) << "Visitor asked to stop further processing."; |
| 925 // Returning true since there was no parsing error. |
| 926 return true; |
| 927 } |
| 928 continue; |
| 929 } |
| 930 |
| 931 frame_type >>= 1; |
| 932 |
946 switch (frame_type) { | 933 switch (frame_type) { |
| 934 // STREAM_FRAME, ACK_FRAME, and CONGESTION_FEEDBACK_FRAME are handled |
| 935 // above. |
947 case PADDING_FRAME: | 936 case PADDING_FRAME: |
948 // We're done with the packet | 937 // We're done with the packet |
949 return true; | 938 return true; |
950 // STREAM_FRAME, ACK_FRAME, and CONGESTION_FEEDBACK handled above for | |
951 // QUIC_VERSION_7 and later. | |
952 case STREAM_FRAME: { | |
953 QuicStreamFrame frame; | |
954 if (!ProcessV6StreamFrame(&frame)) { | |
955 return RaiseError(QUIC_INVALID_FRAME_DATA); | |
956 } | |
957 if (!visitor_->OnStreamFrame(frame)) { | |
958 DLOG(INFO) << "Visitor asked to stop further processing."; | |
959 // Returning true since there was no parsing error. | |
960 return true; | |
961 } | |
962 continue; | |
963 } | |
964 | |
965 case ACK_FRAME: { | |
966 QuicAckFrame frame; | |
967 if (!ProcessAckFrame(&frame)) { | |
968 return RaiseError(QUIC_INVALID_FRAME_DATA); | |
969 } | |
970 if (!visitor_->OnAckFrame(frame)) { | |
971 DLOG(INFO) << "Visitor asked to stop further processing."; | |
972 // Returning true since there was no parsing error. | |
973 // TODO(ianswett): Consider continuing to process frames, since there | |
974 // was not a parsing error. | |
975 return true; | |
976 } | |
977 continue; | |
978 } | |
979 | |
980 case CONGESTION_FEEDBACK_FRAME: { | |
981 QuicCongestionFeedbackFrame frame; | |
982 if (!ProcessQuicCongestionFeedbackFrame(&frame)) { | |
983 return RaiseError(QUIC_INVALID_FRAME_DATA); | |
984 } | |
985 if (!visitor_->OnCongestionFeedbackFrame(frame)) { | |
986 DLOG(INFO) << "Visitor asked to stop further processing."; | |
987 // Returning true since there was no parsing error. | |
988 return true; | |
989 } | |
990 continue; | |
991 } | |
992 | 939 |
993 case RST_STREAM_FRAME: { | 940 case RST_STREAM_FRAME: { |
994 QuicRstStreamFrame frame; | 941 QuicRstStreamFrame frame; |
995 if (!ProcessRstStreamFrame(&frame)) { | 942 if (!ProcessRstStreamFrame(&frame)) { |
996 return RaiseError(QUIC_INVALID_RST_STREAM_DATA); | 943 return RaiseError(QUIC_INVALID_RST_STREAM_DATA); |
997 } | 944 } |
998 if (!visitor_->OnRstStreamFrame(frame)) { | 945 if (!visitor_->OnRstStreamFrame(frame)) { |
999 DLOG(INFO) << "Visitor asked to stop further processing."; | 946 DLOG(INFO) << "Visitor asked to stop further processing."; |
1000 // Returning true since there was no parsing error. | 947 // Returning true since there was no parsing error. |
1001 return true; | 948 return true; |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1086 } else { | 1033 } else { |
1087 if (!reader_->ReadStringPiece(&frame->data, reader_->BytesRemaining())) { | 1034 if (!reader_->ReadStringPiece(&frame->data, reader_->BytesRemaining())) { |
1088 set_detailed_error("Unable to read frame data."); | 1035 set_detailed_error("Unable to read frame data."); |
1089 return false; | 1036 return false; |
1090 } | 1037 } |
1091 } | 1038 } |
1092 | 1039 |
1093 return true; | 1040 return true; |
1094 } | 1041 } |
1095 | 1042 |
1096 bool QuicFramer::ProcessV6StreamFrame(QuicStreamFrame* frame) { | |
1097 if (!reader_->ReadUInt32(&frame->stream_id)) { | |
1098 set_detailed_error("Unable to read stream_id."); | |
1099 return false; | |
1100 } | |
1101 | |
1102 uint8 fin; | |
1103 if (!reader_->ReadBytes(&fin, 1)) { | |
1104 set_detailed_error("Unable to read fin."); | |
1105 return false; | |
1106 } | |
1107 if (fin > 1) { | |
1108 set_detailed_error("Invalid fin value."); | |
1109 return false; | |
1110 } | |
1111 frame->fin = (fin == 1); | |
1112 | |
1113 if (!reader_->ReadUInt64(&frame->offset)) { | |
1114 set_detailed_error("Unable to read offset."); | |
1115 return false; | |
1116 } | |
1117 | |
1118 if (!reader_->ReadStringPiece16(&frame->data)) { | |
1119 set_detailed_error("Unable to read frame data."); | |
1120 return false; | |
1121 } | |
1122 | |
1123 return true; | |
1124 } | |
1125 | |
1126 bool QuicFramer::ProcessAckFrame(QuicAckFrame* frame) { | 1043 bool QuicFramer::ProcessAckFrame(QuicAckFrame* frame) { |
1127 if (!ProcessSentInfo(&frame->sent_info)) { | 1044 if (!ProcessSentInfo(&frame->sent_info)) { |
1128 return false; | 1045 return false; |
1129 } | 1046 } |
1130 if (!ProcessReceivedInfo(&frame->received_info)) { | 1047 if (!ProcessReceivedInfo(&frame->received_info)) { |
1131 return false; | 1048 return false; |
1132 } | 1049 } |
1133 return true; | 1050 return true; |
1134 } | 1051 } |
1135 | 1052 |
(...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1595 } | 1512 } |
1596 | 1513 |
1597 // Not reachable, but some Chrome compilers can't figure that out. *sigh* | 1514 // Not reachable, but some Chrome compilers can't figure that out. *sigh* |
1598 DCHECK(false); | 1515 DCHECK(false); |
1599 return 0; | 1516 return 0; |
1600 } | 1517 } |
1601 | 1518 |
1602 bool QuicFramer::AppendTypeByte(const QuicFrame& frame, | 1519 bool QuicFramer::AppendTypeByte(const QuicFrame& frame, |
1603 bool last_frame_in_packet, | 1520 bool last_frame_in_packet, |
1604 QuicDataWriter* writer) { | 1521 QuicDataWriter* writer) { |
1605 if (quic_version_ == QUIC_VERSION_6) { | |
1606 return writer->WriteUInt8(frame.type); | |
1607 } | |
1608 | |
1609 uint8 type_byte = 0; | 1522 uint8 type_byte = 0; |
1610 switch (frame.type) { | 1523 switch (frame.type) { |
1611 case STREAM_FRAME: { | 1524 case STREAM_FRAME: { |
1612 if (frame.stream_frame == NULL) { | 1525 if (frame.stream_frame == NULL) { |
1613 LOG(DFATAL) << "Failed to append STREAM frame with no stream_frame."; | 1526 LOG(DFATAL) << "Failed to append STREAM frame with no stream_frame."; |
1614 } | 1527 } |
1615 // Fin bit. | 1528 // Fin bit. |
1616 type_byte |= frame.stream_frame->fin ? kQuicStreamFinMask : 0; | 1529 type_byte |= frame.stream_frame->fin ? kQuicStreamFinMask : 0; |
1617 | 1530 |
1618 // Data Length bit. | 1531 // Data Length bit. |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1699 if (!writer->WriteUInt16(frame.data.size())) { | 1612 if (!writer->WriteUInt16(frame.data.size())) { |
1700 return false; | 1613 return false; |
1701 } | 1614 } |
1702 } | 1615 } |
1703 if (!writer->WriteBytes(frame.data.data(), frame.data.size())) { | 1616 if (!writer->WriteBytes(frame.data.data(), frame.data.size())) { |
1704 return false; | 1617 return false; |
1705 } | 1618 } |
1706 return true; | 1619 return true; |
1707 } | 1620 } |
1708 | 1621 |
1709 bool QuicFramer::AppendV6StreamFramePayload( | |
1710 const QuicStreamFrame& frame, | |
1711 QuicDataWriter* writer) { | |
1712 if (!writer->WriteUInt32(frame.stream_id)) { | |
1713 return false; | |
1714 } | |
1715 if (!writer->WriteUInt8(frame.fin)) { | |
1716 return false; | |
1717 } | |
1718 if (!writer->WriteUInt64(frame.offset)) { | |
1719 return false; | |
1720 } | |
1721 if (!writer->WriteUInt16(frame.data.size())) { | |
1722 return false; | |
1723 } | |
1724 if (!writer->WriteBytes(frame.data.data(), frame.data.size())) { | |
1725 return false; | |
1726 } | |
1727 return true; | |
1728 } | |
1729 | |
1730 QuicPacketSequenceNumber QuicFramer::CalculateLargestObserved( | 1622 QuicPacketSequenceNumber QuicFramer::CalculateLargestObserved( |
1731 const SequenceNumberSet& missing_packets, | 1623 const SequenceNumberSet& missing_packets, |
1732 SequenceNumberSet::const_iterator largest_written) { | 1624 SequenceNumberSet::const_iterator largest_written) { |
1733 SequenceNumberSet::const_iterator it = largest_written; | 1625 SequenceNumberSet::const_iterator it = largest_written; |
1734 QuicPacketSequenceNumber previous_missing = *it; | 1626 QuicPacketSequenceNumber previous_missing = *it; |
1735 ++it; | 1627 ++it; |
1736 | 1628 |
1737 // See if the next thing is a gap in the missing packets: if it's a | 1629 // See if the next thing is a gap in the missing packets: if it's a |
1738 // non-missing packet we can return it. | 1630 // non-missing packet we can return it. |
1739 if (it != missing_packets.end() && previous_missing + 1 != *it) { | 1631 if (it != missing_packets.end() && previous_missing + 1 != *it) { |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1958 | 1850 |
1959 bool QuicFramer::RaiseError(QuicErrorCode error) { | 1851 bool QuicFramer::RaiseError(QuicErrorCode error) { |
1960 DLOG(INFO) << detailed_error_; | 1852 DLOG(INFO) << detailed_error_; |
1961 set_error(error); | 1853 set_error(error); |
1962 visitor_->OnError(this); | 1854 visitor_->OnError(this); |
1963 reader_.reset(NULL); | 1855 reader_.reset(NULL); |
1964 return false; | 1856 return false; |
1965 } | 1857 } |
1966 | 1858 |
1967 } // namespace net | 1859 } // namespace net |
OLD | NEW |