blob: de026a760815679044046c030ab990a6d55cc632 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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
3// found in the LICENSE file.
4
5#ifndef QUICHE_QUIC_CORE_QUIC_FRAMER_H_
6#define QUICHE_QUIC_CORE_QUIC_FRAMER_H_
7
8#include <cstddef>
9#include <cstdint>
10#include <memory>
vasilvv872e7a32019-03-12 16:42:44 -070011#include <string>
QUICHE teama6ef0a62019-03-07 20:34:33 -050012
QUICHE teama6ef0a62019-03-07 20:34:33 -050013#include "net/third_party/quiche/src/quic/core/crypto/quic_decrypter.h"
14#include "net/third_party/quiche/src/quic/core/crypto/quic_encrypter.h"
15#include "net/third_party/quiche/src/quic/core/crypto/quic_random.h"
dschinazi244f6dc2019-05-06 15:45:16 -070016#include "net/third_party/quiche/src/quic/core/quic_connection_id.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050017#include "net/third_party/quiche/src/quic/core/quic_packets.h"
dschinazi4b5a68a2019-08-15 15:45:36 -070018#include "net/third_party/quiche/src/quic/core/quic_types.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050019#include "net/third_party/quiche/src/quic/platform/api/quic_export.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050020#include "net/third_party/quiche/src/quic/platform/api/quic_string_piece.h"
21
22namespace quic {
23
24namespace test {
25class QuicFramerPeer;
26} // namespace test
27
28class QuicDataReader;
29class QuicDataWriter;
30class QuicFramer;
31class QuicStreamFrameDataProducer;
32
33// Number of bytes reserved for the frame type preceding each frame.
34const size_t kQuicFrameTypeSize = 1;
35// Number of bytes reserved for error code.
36const size_t kQuicErrorCodeSize = 4;
37// Number of bytes reserved to denote the length of error details field.
38const size_t kQuicErrorDetailsLengthSize = 2;
39
40// Maximum number of bytes reserved for stream id.
41const size_t kQuicMaxStreamIdSize = 4;
42// Maximum number of bytes reserved for byte offset in stream frame.
43const size_t kQuicMaxStreamOffsetSize = 8;
44// Number of bytes reserved to store payload length in stream frame.
45const size_t kQuicStreamPayloadLengthSize = 2;
46// Number of bytes to reserve for IQ Error codes (for the Connection Close,
47// Application Close, and Reset Stream frames).
48const size_t kQuicIetfQuicErrorCodeSize = 2;
49// Minimum size of the IETF QUIC Error Phrase's length field
50const size_t kIetfQuicMinErrorPhraseLengthSize = 1;
51
52// Size in bytes reserved for the delta time of the largest observed
53// packet number in ack frames.
54const size_t kQuicDeltaTimeLargestObservedSize = 2;
55// Size in bytes reserved for the number of received packets with timestamps.
56const size_t kQuicNumTimestampsSize = 1;
57// Size in bytes reserved for the number of missing packets in ack frames.
58const size_t kNumberOfNackRangesSize = 1;
59// Size in bytes reserved for the number of ack blocks in ack frames.
60const size_t kNumberOfAckBlocksSize = 1;
61// Maximum number of missing packet ranges that can fit within an ack frame.
62const size_t kMaxNackRanges = (1 << (kNumberOfNackRangesSize * 8)) - 1;
63// Maximum number of ack blocks that can fit within an ack frame.
64const size_t kMaxAckBlocks = (1 << (kNumberOfAckBlocksSize * 8)) - 1;
65
66// This class receives callbacks from the framer when packets
67// are processed.
68class QUIC_EXPORT_PRIVATE QuicFramerVisitorInterface {
69 public:
70 virtual ~QuicFramerVisitorInterface() {}
71
72 // Called if an error is detected in the QUIC protocol.
73 virtual void OnError(QuicFramer* framer) = 0;
74
75 // Called only when |perspective_| is IS_SERVER and the framer gets a
76 // packet with version flag true and the version on the packet doesn't match
77 // |quic_version_|. The visitor should return true after it updates the
78 // version of the |framer_| to |received_version| or false to stop processing
79 // this packet.
fayang8aba1ff2019-06-21 12:00:54 -070080 virtual bool OnProtocolVersionMismatch(
81 ParsedQuicVersion received_version) = 0;
QUICHE teama6ef0a62019-03-07 20:34:33 -050082
83 // Called when a new packet has been received, before it
84 // has been validated or processed.
85 virtual void OnPacket() = 0;
86
87 // Called when a public reset packet has been parsed but has not yet
88 // been validated.
89 virtual void OnPublicResetPacket(const QuicPublicResetPacket& packet) = 0;
90
91 // Called only when |perspective_| is IS_CLIENT and a version negotiation
92 // packet has been parsed.
93 virtual void OnVersionNegotiationPacket(
94 const QuicVersionNegotiationPacket& packet) = 0;
95
dschinazi244f6dc2019-05-06 15:45:16 -070096 // Called only when |perspective_| is IS_CLIENT and a retry packet has been
97 // parsed.
98 virtual void OnRetryPacket(QuicConnectionId original_connection_id,
99 QuicConnectionId new_connection_id,
100 QuicStringPiece retry_token) = 0;
101
QUICHE teama6ef0a62019-03-07 20:34:33 -0500102 // Called when all fields except packet number has been parsed, but has not
103 // been authenticated. If it returns false, framing for this packet will
104 // cease.
105 virtual bool OnUnauthenticatedPublicHeader(
106 const QuicPacketHeader& header) = 0;
107
108 // Called when the unauthenticated portion of the header has been parsed.
109 // If OnUnauthenticatedHeader returns false, framing for this packet will
110 // cease.
111 virtual bool OnUnauthenticatedHeader(const QuicPacketHeader& header) = 0;
112
113 // Called when a packet has been decrypted. |level| is the encryption level
114 // of the packet.
115 virtual void OnDecryptedPacket(EncryptionLevel level) = 0;
116
117 // Called when the complete header of a packet had been parsed.
118 // If OnPacketHeader returns false, framing for this packet will cease.
119 virtual bool OnPacketHeader(const QuicPacketHeader& header) = 0;
120
121 // Called when the packet being processed contains multiple IETF QUIC packets,
122 // which is due to there being more data after what is covered by the length
123 // field. |packet| contains the remaining data which can be processed.
124 // Note that this is called when the framer parses the length field, before
125 // it attempts to decrypt the first payload. It is the visitor's
126 // responsibility to buffer the packet and call ProcessPacket on it
127 // after the framer is done parsing the current payload. |packet| does not
128 // own its internal buffer, the visitor should make a copy of it.
129 virtual void OnCoalescedPacket(const QuicEncryptedPacket& packet) = 0;
130
dschinazi4b5a68a2019-08-15 15:45:36 -0700131 // Called when the packet being processed failed to decrypt.
132 // |has_decryption_key| indicates whether the framer knew which decryption
133 // key to use for this packet and already had a suitable key.
134 virtual void OnUndecryptablePacket(const QuicEncryptedPacket& packet,
135 EncryptionLevel decryption_level,
136 bool has_decryption_key) = 0;
137
QUICHE teama6ef0a62019-03-07 20:34:33 -0500138 // Called when a StreamFrame has been parsed.
139 virtual bool OnStreamFrame(const QuicStreamFrame& frame) = 0;
140
141 // Called when a CRYPTO frame has been parsed.
142 virtual bool OnCryptoFrame(const QuicCryptoFrame& frame) = 0;
143
144 // Called when largest acked of an AckFrame has been parsed.
145 virtual bool OnAckFrameStart(QuicPacketNumber largest_acked,
146 QuicTime::Delta ack_delay_time) = 0;
147
148 // Called when ack range [start, end) of an AckFrame has been parsed.
149 virtual bool OnAckRange(QuicPacketNumber start, QuicPacketNumber end) = 0;
150
151 // Called when a timestamp in the AckFrame has been parsed.
152 virtual bool OnAckTimestamp(QuicPacketNumber packet_number,
153 QuicTime timestamp) = 0;
154
155 // Called after the last ack range in an AckFrame has been parsed.
156 // |start| is the starting value of the last ack range.
157 virtual bool OnAckFrameEnd(QuicPacketNumber start) = 0;
158
159 // Called when a StopWaitingFrame has been parsed.
160 virtual bool OnStopWaitingFrame(const QuicStopWaitingFrame& frame) = 0;
161
162 // Called when a QuicPaddingFrame has been parsed.
163 virtual bool OnPaddingFrame(const QuicPaddingFrame& frame) = 0;
164
165 // Called when a PingFrame has been parsed.
166 virtual bool OnPingFrame(const QuicPingFrame& frame) = 0;
167
168 // Called when a RstStreamFrame has been parsed.
169 virtual bool OnRstStreamFrame(const QuicRstStreamFrame& frame) = 0;
170
fkastenholz04bd4f32019-04-16 12:24:38 -0700171 // Called when a ConnectionCloseFrame, of any type, has been parsed.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500172 virtual bool OnConnectionCloseFrame(
173 const QuicConnectionCloseFrame& frame) = 0;
174
QUICHE teama6ef0a62019-03-07 20:34:33 -0500175 // Called when a StopSendingFrame has been parsed.
176 virtual bool OnStopSendingFrame(const QuicStopSendingFrame& frame) = 0;
177
178 // Called when a PathChallengeFrame has been parsed.
179 virtual bool OnPathChallengeFrame(const QuicPathChallengeFrame& frame) = 0;
180
181 // Called when a PathResponseFrame has been parsed.
182 virtual bool OnPathResponseFrame(const QuicPathResponseFrame& frame) = 0;
183
184 // Called when a GoAwayFrame has been parsed.
185 virtual bool OnGoAwayFrame(const QuicGoAwayFrame& frame) = 0;
186
187 // Called when a WindowUpdateFrame has been parsed.
188 virtual bool OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame) = 0;
189
190 // Called when a BlockedFrame has been parsed.
191 virtual bool OnBlockedFrame(const QuicBlockedFrame& frame) = 0;
192
193 // Called when a NewConnectionIdFrame has been parsed.
194 virtual bool OnNewConnectionIdFrame(
195 const QuicNewConnectionIdFrame& frame) = 0;
196
197 // Called when a RetireConnectionIdFrame has been parsed.
198 virtual bool OnRetireConnectionIdFrame(
199 const QuicRetireConnectionIdFrame& frame) = 0;
200
201 // Called when a NewTokenFrame has been parsed.
202 virtual bool OnNewTokenFrame(const QuicNewTokenFrame& frame) = 0;
203
204 // Called when a message frame has been parsed.
205 virtual bool OnMessageFrame(const QuicMessageFrame& frame) = 0;
206
207 // Called when a packet has been completely processed.
208 virtual void OnPacketComplete() = 0;
209
210 // Called to check whether |token| is a valid stateless reset token.
211 virtual bool IsValidStatelessResetToken(QuicUint128 token) const = 0;
212
213 // Called when an IETF stateless reset packet has been parsed and validated
214 // with the stateless reset token.
215 virtual void OnAuthenticatedIetfStatelessResetPacket(
216 const QuicIetfStatelessResetPacket& packet) = 0;
217
fkastenholz3c4eabf2019-04-22 07:49:59 -0700218 // Called when an IETF MaxStreams frame has been parsed.
219 virtual bool OnMaxStreamsFrame(const QuicMaxStreamsFrame& frame) = 0;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500220
fkastenholz3c4eabf2019-04-22 07:49:59 -0700221 // Called when an IETF StreamsBlocked frame has been parsed.
222 virtual bool OnStreamsBlockedFrame(const QuicStreamsBlockedFrame& frame) = 0;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500223};
224
225// Class for parsing and constructing QUIC packets. It has a
226// QuicFramerVisitorInterface that is called when packets are parsed.
227class QUIC_EXPORT_PRIVATE QuicFramer {
228 public:
229 // Constructs a new framer that installs a kNULL QuicEncrypter and
QUICHE team6987b4a2019-03-15 16:23:04 -0700230 // QuicDecrypter for level ENCRYPTION_INITIAL. |supported_versions| specifies
231 // the list of supported QUIC versions. |quic_version_| is set to the maximum
QUICHE teama6ef0a62019-03-07 20:34:33 -0500232 // version in |supported_versions|.
233 QuicFramer(const ParsedQuicVersionVector& supported_versions,
234 QuicTime creation_time,
235 Perspective perspective,
dschinazi8ff74822019-05-28 16:37:20 -0700236 uint8_t expected_server_connection_id_length);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500237 QuicFramer(const QuicFramer&) = delete;
238 QuicFramer& operator=(const QuicFramer&) = delete;
239
240 virtual ~QuicFramer();
241
242 // Returns true if |version| is a supported transport version.
243 bool IsSupportedTransportVersion(const QuicTransportVersion version) const;
244
245 // Returns true if |version| is a supported protocol version.
246 bool IsSupportedVersion(const ParsedQuicVersion version) const;
247
248 // Set callbacks to be called from the framer. A visitor must be set, or
249 // else the framer will likely crash. It is acceptable for the visitor
250 // to do nothing. If this is called multiple times, only the last visitor
251 // will be used.
252 void set_visitor(QuicFramerVisitorInterface* visitor) { visitor_ = visitor; }
253
254 const ParsedQuicVersionVector& supported_versions() const {
255 return supported_versions_;
256 }
257
258 QuicTransportVersion transport_version() const {
259 return version_.transport_version;
260 }
261
262 ParsedQuicVersion version() const { return version_; }
263
264 void set_version(const ParsedQuicVersion version);
265
266 // Does not DCHECK for supported version. Used by tests to set unsupported
267 // version to trigger version negotiation.
268 void set_version_for_tests(const ParsedQuicVersion version) {
269 version_ = version;
270 }
271
272 QuicErrorCode error() const { return error_; }
273
274 // Allows enabling or disabling of timestamp processing and serialization.
275 void set_process_timestamps(bool process_timestamps) {
276 process_timestamps_ = process_timestamps;
277 }
278
279 // Pass a UDP packet into the framer for parsing.
dschinazi244f6dc2019-05-06 15:45:16 -0700280 // Return true if the packet was processed successfully. |packet| must be a
QUICHE teama6ef0a62019-03-07 20:34:33 -0500281 // single, complete UDP packet (not a frame of a packet). This packet
282 // might be null padded past the end of the payload, which will be correctly
283 // ignored.
284 bool ProcessPacket(const QuicEncryptedPacket& packet);
285
286 // Largest size in bytes of all stream frame fields without the payload.
287 static size_t GetMinStreamFrameSize(QuicTransportVersion version,
288 QuicStreamId stream_id,
289 QuicStreamOffset offset,
290 bool last_frame_in_packet,
291 QuicPacketLength data_length);
292 // Returns the overhead of framing a CRYPTO frame with the specific offset and
293 // data length provided, but not counting the size of the data payload.
294 static size_t GetMinCryptoFrameSize(QuicStreamOffset offset,
295 QuicPacketLength data_length);
296 static size_t GetMessageFrameSize(QuicTransportVersion version,
297 bool last_frame_in_packet,
298 QuicByteCount length);
299 // Size in bytes of all ack frame fields without the missing packets or ack
300 // blocks.
301 static size_t GetMinAckFrameSize(
302 QuicTransportVersion version,
303 QuicPacketNumberLength largest_observed_length);
304 // Size in bytes of a stop waiting frame.
305 static size_t GetStopWaitingFrameSize(
306 QuicTransportVersion version,
307 QuicPacketNumberLength packet_number_length);
308 // Size in bytes of all reset stream frame fields.
309 static size_t GetRstStreamFrameSize(QuicTransportVersion version,
310 const QuicRstStreamFrame& frame);
fkastenholza037b8b2019-05-07 06:00:05 -0700311 // Size in bytes of all connection close frame fields, including the error
312 // details.
313 static size_t GetConnectionCloseFrameSize(
QUICHE teama6ef0a62019-03-07 20:34:33 -0500314 QuicTransportVersion version,
315 const QuicConnectionCloseFrame& frame);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500316 // Size in bytes of all GoAway frame fields without the reason phrase.
317 static size_t GetMinGoAwayFrameSize();
318 // Size in bytes of all WindowUpdate frame fields.
319 // For version 99, determines whether a MAX DATA or MAX STREAM DATA frame will
320 // be generated and calculates the appropriate size.
321 static size_t GetWindowUpdateFrameSize(QuicTransportVersion version,
322 const QuicWindowUpdateFrame& frame);
323 // Size in bytes of all MaxStreams frame fields.
324 static size_t GetMaxStreamsFrameSize(QuicTransportVersion version,
fkastenholz3c4eabf2019-04-22 07:49:59 -0700325 const QuicMaxStreamsFrame& frame);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500326 // Size in bytes of all StreamsBlocked frame fields.
327 static size_t GetStreamsBlockedFrameSize(
328 QuicTransportVersion version,
fkastenholz3c4eabf2019-04-22 07:49:59 -0700329 const QuicStreamsBlockedFrame& frame);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500330 // Size in bytes of all Blocked frame fields.
331 static size_t GetBlockedFrameSize(QuicTransportVersion version,
332 const QuicBlockedFrame& frame);
333 // Size in bytes of PathChallenge frame.
334 static size_t GetPathChallengeFrameSize(const QuicPathChallengeFrame& frame);
335 // Size in bytes of PathResponse frame.
336 static size_t GetPathResponseFrameSize(const QuicPathResponseFrame& frame);
337 // Size in bytes required to serialize the stream id.
338 static size_t GetStreamIdSize(QuicStreamId stream_id);
339 // Size in bytes required to serialize the stream offset.
340 static size_t GetStreamOffsetSize(QuicTransportVersion version,
341 QuicStreamOffset offset);
342 // Size in bytes for a serialized new connection id frame
343 static size_t GetNewConnectionIdFrameSize(
344 const QuicNewConnectionIdFrame& frame);
345
346 // Size in bytes for a serialized retire connection id frame
347 static size_t GetRetireConnectionIdFrameSize(
348 const QuicRetireConnectionIdFrame& frame);
349
350 // Size in bytes for a serialized new token frame
351 static size_t GetNewTokenFrameSize(const QuicNewTokenFrame& frame);
352
353 // Size in bytes required for a serialized stop sending frame.
354 static size_t GetStopSendingFrameSize(const QuicStopSendingFrame& frame);
355
356 // Size in bytes required for a serialized retransmittable control |frame|.
357 static size_t GetRetransmittableControlFrameSize(QuicTransportVersion version,
358 const QuicFrame& frame);
359
360 // Returns the number of bytes added to the packet for the specified frame,
361 // and 0 if the frame doesn't fit. Includes the header size for the first
362 // frame.
363 size_t GetSerializedFrameLength(const QuicFrame& frame,
364 size_t free_bytes,
365 bool first_frame_in_packet,
366 bool last_frame_in_packet,
367 QuicPacketNumberLength packet_number_length);
368
369 // Returns the associated data from the encrypted packet |encrypted| as a
370 // stringpiece.
371 static QuicStringPiece GetAssociatedDataFromEncryptedPacket(
372 QuicTransportVersion version,
373 const QuicEncryptedPacket& encrypted,
374 QuicConnectionIdLength destination_connection_id_length,
375 QuicConnectionIdLength source_connection_id_length,
376 bool includes_version,
377 bool includes_diversification_nonce,
378 QuicPacketNumberLength packet_number_length,
379 QuicVariableLengthIntegerLength retry_token_length_length,
380 uint64_t retry_token_length,
381 QuicVariableLengthIntegerLength length_length);
382
fayangccbab732019-05-13 10:11:25 -0700383 // Lightweight parsing of |packet| and populates |format|, |version_flag|,
dschinazib42a8c52019-05-30 09:45:01 -0700384 // |version_label|, |destination_connection_id|, |source_connection_id| and
385 // |detailed_error|. Please note, |expected_destination_connection_id_length|
386 // is only used to determine IETF short header packet's destination
387 // connection ID length.
fayangccbab732019-05-13 10:11:25 -0700388 static QuicErrorCode ProcessPacketDispatcher(
389 const QuicEncryptedPacket& packet,
dschinazib42a8c52019-05-30 09:45:01 -0700390 uint8_t expected_destination_connection_id_length,
fayangccbab732019-05-13 10:11:25 -0700391 PacketHeaderFormat* format,
392 bool* version_flag,
393 QuicVersionLabel* version_label,
fayangccbab732019-05-13 10:11:25 -0700394 QuicConnectionId* destination_connection_id,
dschinazib42a8c52019-05-30 09:45:01 -0700395 QuicConnectionId* source_connection_id,
fayangccbab732019-05-13 10:11:25 -0700396 std::string* detailed_error);
397
dschinazi48ac9192019-07-31 00:07:26 -0700398 // Parses the unencryoted fields in a QUIC header using |reader| as input,
399 // stores the result in the other parameters.
400 // |expected_destination_connection_id_length| is only used for short headers.
401 static QuicErrorCode ParsePublicHeader(
402 QuicDataReader* reader,
403 uint8_t expected_destination_connection_id_length,
404 bool ietf_format,
405 uint8_t* first_byte,
406 PacketHeaderFormat* format,
407 bool* version_present,
408 bool* has_length_prefix,
409 QuicVersionLabel* version_label,
410 ParsedQuicVersion* parsed_version,
411 QuicConnectionId* destination_connection_id,
412 QuicConnectionId* source_connection_id,
413 QuicLongHeaderType* long_packet_type,
414 QuicVariableLengthIntegerLength* retry_token_length_length,
415 QuicStringPiece* retry_token,
416 std::string* detailed_error);
417
418 // Parses the unencryoted fields in |packet| and stores them in the other
419 // parameters. This can only be called on the server.
420 // |expected_destination_connection_id_length| is only used for short headers.
421 static QuicErrorCode ParsePublicHeaderDispatcher(
422 const QuicEncryptedPacket& packet,
423 uint8_t expected_destination_connection_id_length,
424 PacketHeaderFormat* format,
425 bool* version_present,
426 bool* has_length_prefix,
427 QuicVersionLabel* version_label,
428 ParsedQuicVersion* parsed_version,
429 QuicConnectionId* destination_connection_id,
430 QuicConnectionId* source_connection_id,
431 bool* retry_token_present,
432 QuicStringPiece* retry_token,
433 std::string* detailed_error);
434
QUICHE teama6ef0a62019-03-07 20:34:33 -0500435 // Serializes a packet containing |frames| into |buffer|.
436 // Returns the length of the packet, which must not be longer than
437 // |packet_length|. Returns 0 if it fails to serialize.
438 size_t BuildDataPacket(const QuicPacketHeader& header,
439 const QuicFrames& frames,
440 char* buffer,
441 size_t packet_length,
442 EncryptionLevel level);
443
444 // Serializes a probing packet, which is a padded PING packet. Returns the
445 // length of the packet. Returns 0 if it fails to serialize.
446 size_t BuildConnectivityProbingPacket(const QuicPacketHeader& header,
447 char* buffer,
448 size_t packet_length,
449 EncryptionLevel level);
450
QUICHE teama6ef0a62019-03-07 20:34:33 -0500451 // Serialize a probing packet that uses IETF QUIC's PATH CHALLENGE frame. Also
452 // fills the packet with padding.
453 size_t BuildPaddedPathChallengePacket(const QuicPacketHeader& header,
454 char* buffer,
455 size_t packet_length,
456 QuicPathFrameBuffer* payload,
457 QuicRandom* randomizer,
458 EncryptionLevel level);
459
460 // Serialize a probing response packet that uses IETF QUIC's PATH RESPONSE
461 // frame. Also fills the packet with padding if |is_padded| is
462 // true. |payloads| is always emptied, even if the packet can not be
463 // successfully built.
464 size_t BuildPathResponsePacket(const QuicPacketHeader& header,
465 char* buffer,
466 size_t packet_length,
467 const QuicDeque<QuicPathFrameBuffer>& payloads,
468 const bool is_padded,
469 EncryptionLevel level);
470
471 // Returns a new public reset packet.
472 static std::unique_ptr<QuicEncryptedPacket> BuildPublicResetPacket(
473 const QuicPublicResetPacket& packet);
474
475 // Returns a new IETF stateless reset packet.
476 static std::unique_ptr<QuicEncryptedPacket> BuildIetfStatelessResetPacket(
477 QuicConnectionId connection_id,
478 QuicUint128 stateless_reset_token);
479
480 // Returns a new version negotiation packet.
481 static std::unique_ptr<QuicEncryptedPacket> BuildVersionNegotiationPacket(
dschinazib417d602019-05-29 13:08:45 -0700482 QuicConnectionId server_connection_id,
483 QuicConnectionId client_connection_id,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500484 bool ietf_quic,
dschinazi48ac9192019-07-31 00:07:26 -0700485 bool use_length_prefix,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500486 const ParsedQuicVersionVector& versions);
487
488 // Returns a new IETF version negotiation packet.
489 static std::unique_ptr<QuicEncryptedPacket> BuildIetfVersionNegotiationPacket(
dschinazi48ac9192019-07-31 00:07:26 -0700490 bool use_length_prefix,
dschinazib417d602019-05-29 13:08:45 -0700491 QuicConnectionId server_connection_id,
492 QuicConnectionId client_connection_id,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500493 const ParsedQuicVersionVector& versions);
494
495 // If header.version_flag is set, the version in the
496 // packet will be set -- but it will be set from version_ not
497 // header.versions.
498 bool AppendPacketHeader(const QuicPacketHeader& header,
499 QuicDataWriter* writer,
500 size_t* length_field_offset);
501 bool AppendIetfHeaderTypeByte(const QuicPacketHeader& header,
502 QuicDataWriter* writer);
503 bool AppendIetfPacketHeader(const QuicPacketHeader& header,
504 QuicDataWriter* writer,
505 size_t* length_field_offset);
506 bool WriteIetfLongHeaderLength(const QuicPacketHeader& header,
507 QuicDataWriter* writer,
508 size_t length_field_offset,
509 EncryptionLevel level);
510 bool AppendTypeByte(const QuicFrame& frame,
511 bool last_frame_in_packet,
512 QuicDataWriter* writer);
513 bool AppendIetfTypeByte(const QuicFrame& frame,
514 bool last_frame_in_packet,
515 QuicDataWriter* writer);
516 size_t AppendIetfFrames(const QuicFrames& frames, QuicDataWriter* writer);
517 bool AppendStreamFrame(const QuicStreamFrame& frame,
518 bool last_frame_in_packet,
519 QuicDataWriter* writer);
520 bool AppendCryptoFrame(const QuicCryptoFrame& frame, QuicDataWriter* writer);
521
522 // SetDecrypter sets the primary decrypter, replacing any that already exists.
523 // If an alternative decrypter is in place then the function DCHECKs. This is
524 // intended for cases where one knows that future packets will be using the
525 // new decrypter and the previous decrypter is now obsolete. |level| indicates
526 // the encryption level of the new decrypter.
527 void SetDecrypter(EncryptionLevel level,
528 std::unique_ptr<QuicDecrypter> decrypter);
529
530 // SetAlternativeDecrypter sets a decrypter that may be used to decrypt
531 // future packets. |level| indicates the encryption level of the decrypter. If
532 // |latch_once_used| is true, then the first time that the decrypter is
533 // successful it will replace the primary decrypter. Otherwise both
534 // decrypters will remain active and the primary decrypter will be the one
535 // last used.
536 void SetAlternativeDecrypter(EncryptionLevel level,
537 std::unique_ptr<QuicDecrypter> decrypter,
538 bool latch_once_used);
539
zhongyi546cc452019-04-12 15:27:49 -0700540 void InstallDecrypter(EncryptionLevel level,
541 std::unique_ptr<QuicDecrypter> decrypter);
542 void RemoveDecrypter(EncryptionLevel level);
543
544 const QuicDecrypter* GetDecrypter(EncryptionLevel level) const;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500545 const QuicDecrypter* decrypter() const;
546 const QuicDecrypter* alternative_decrypter() const;
547
548 // Changes the encrypter used for level |level| to |encrypter|.
549 void SetEncrypter(EncryptionLevel level,
550 std::unique_ptr<QuicEncrypter> encrypter);
551
nharper4a5a76c2019-09-13 13:44:37 -0700552 // Sets the encrypter and decrypter for the ENCRYPTION_INITIAL level.
553 void SetInitialObfuscators(QuicConnectionId connection_id);
554
QUICHE teama6ef0a62019-03-07 20:34:33 -0500555 // Encrypts a payload in |buffer|. |ad_len| is the length of the associated
556 // data. |total_len| is the length of the associated data plus plaintext.
557 // |buffer_len| is the full length of the allocated buffer.
558 size_t EncryptInPlace(EncryptionLevel level,
559 QuicPacketNumber packet_number,
560 size_t ad_len,
561 size_t total_len,
562 size_t buffer_len,
563 char* buffer);
564
565 // Returns the length of the data encrypted into |buffer| if |buffer_len| is
566 // long enough, and otherwise 0.
567 size_t EncryptPayload(EncryptionLevel level,
568 QuicPacketNumber packet_number,
569 const QuicPacket& packet,
570 char* buffer,
571 size_t buffer_len);
572
573 // Returns the length of the ciphertext that would be generated by encrypting
574 // to plaintext of size |plaintext_size| at the given level.
575 size_t GetCiphertextSize(EncryptionLevel level, size_t plaintext_size) const;
576
577 // Returns the maximum length of plaintext that can be encrypted
578 // to ciphertext no larger than |ciphertext_size|.
579 size_t GetMaxPlaintextSize(size_t ciphertext_size);
580
vasilvvc48c8712019-03-11 13:38:16 -0700581 const std::string& detailed_error() { return detailed_error_; }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500582
583 // The minimum packet number length required to represent |packet_number|.
584 static QuicPacketNumberLength GetMinPacketNumberLength(
585 QuicTransportVersion version,
586 QuicPacketNumber packet_number);
587
588 void SetSupportedVersions(const ParsedQuicVersionVector& versions) {
589 supported_versions_ = versions;
590 version_ = versions[0];
591 }
592
593 // Tell framer to infer packet header type from version_.
594 void InferPacketHeaderTypeFromVersion();
595
QUICHE teama6ef0a62019-03-07 20:34:33 -0500596 // Returns true if |header| is considered as an stateless reset packet.
597 bool IsIetfStatelessResetPacket(const QuicPacketHeader& header) const;
598
599 // Returns true if encrypter of |level| is available.
600 bool HasEncrypterOfEncryptionLevel(EncryptionLevel level) const;
601
602 void set_validate_flags(bool value) { validate_flags_ = value; }
603
604 Perspective perspective() const { return perspective_; }
605
QUICHE teama6ef0a62019-03-07 20:34:33 -0500606 void set_data_producer(QuicStreamFrameDataProducer* data_producer) {
607 data_producer_ = data_producer;
608 }
609
QUICHE teama6ef0a62019-03-07 20:34:33 -0500610 QuicTime creation_time() const { return creation_time_; }
611
612 QuicPacketNumber first_sending_packet_number() const {
613 return first_sending_packet_number_;
614 }
615
fkastenholza3660102019-08-28 05:19:24 -0700616 uint64_t current_received_frame_type() const {
617 return current_received_frame_type_;
618 }
619
dschinazi8ff74822019-05-28 16:37:20 -0700620 // The connection ID length the framer expects on incoming IETF short headers
621 // on the server.
622 uint8_t GetExpectedServerConnectionIdLength() {
623 return expected_server_connection_id_length_;
QUICHE team8e2e4532019-03-14 14:37:56 -0700624 }
625
dschinazi346b7ce2019-06-05 01:38:18 -0700626 // Change the expected destination connection ID length for short headers on
627 // the client.
628 void SetExpectedClientConnectionIdLength(
629 uint8_t expected_client_connection_id_length) {
630 expected_client_connection_id_length_ =
631 expected_client_connection_id_length;
632 }
633
QUICHE team10b22a12019-03-21 15:31:42 -0700634 void EnableMultiplePacketNumberSpacesSupport();
635
dschinazide0f6dc2019-05-15 16:10:11 -0700636 // Writes an array of bytes that, if sent as a UDP datagram, will trigger
637 // IETF QUIC Version Negotiation on servers. The bytes will be written to
638 // |packet_bytes|, which must point to |packet_length| bytes of memory.
639 // |packet_length| must be in the range [1200, 65535].
640 // |destination_connection_id_bytes| will be sent as the destination
641 // connection ID, and must point to |destination_connection_id_length| bytes
dschinazi19dc2b52019-07-17 19:54:43 -0700642 // of memory. |destination_connection_id_length| must be in the range [8,18].
643 // When targeting Google servers, it is recommended to use a
dschinazide0f6dc2019-05-15 16:10:11 -0700644 // |destination_connection_id_length| of 8.
645 static bool WriteClientVersionNegotiationProbePacket(
646 char* packet_bytes,
647 QuicByteCount packet_length,
648 const char* destination_connection_id_bytes,
649 uint8_t destination_connection_id_length);
650
651 // Parses a packet which a QUIC server sent in response to a packet sent by
652 // WriteClientVersionNegotiationProbePacket. |packet_bytes| must point to
653 // |packet_length| bytes in memory which represent the response.
654 // |packet_length| must be greater or equal to 6. This method will fill in
655 // |source_connection_id_bytes| which must point to at least 18 bytes in
656 // memory. |source_connection_id_length_out| will contain the length of the
657 // received source connection ID, which on success will match the contents of
658 // the destination connection ID passed in to
659 // WriteClientVersionNegotiationProbePacket. In the case of a failure,
660 // |detailed_error| will be filled in with an explanation of what failed.
661 static bool ParseServerVersionNegotiationProbeResponse(
662 const char* packet_bytes,
663 QuicByteCount packet_length,
664 char* source_connection_id_bytes,
665 uint8_t* source_connection_id_length_out,
666 std::string* detailed_error);
667
fkastenholz4dc4ba32019-07-30 09:55:25 -0700668 void set_local_ack_delay_exponent(uint32_t exponent) {
669 local_ack_delay_exponent_ = exponent;
670 }
671 uint32_t local_ack_delay_exponent() const {
672 return local_ack_delay_exponent_;
673 }
674
675 void set_peer_ack_delay_exponent(uint32_t exponent) {
676 peer_ack_delay_exponent_ = exponent;
677 }
678 uint32_t peer_ack_delay_exponent() const { return peer_ack_delay_exponent_; }
679
QUICHE teama6ef0a62019-03-07 20:34:33 -0500680 private:
681 friend class test::QuicFramerPeer;
682
683 typedef std::map<QuicPacketNumber, uint8_t> NackRangeMap;
684
685 struct AckFrameInfo {
686 AckFrameInfo();
687 AckFrameInfo(const AckFrameInfo& other);
688 ~AckFrameInfo();
689
690 // The maximum ack block length.
691 QuicPacketCount max_block_length;
692 // Length of first ack block.
693 QuicPacketCount first_block_length;
694 // Number of ACK blocks needed for the ACK frame.
695 size_t num_ack_blocks;
696 };
697
nharper55fa6132019-05-07 19:37:21 -0700698 // Applies header protection to an IETF QUIC packet header in |buffer| using
699 // the encrypter for level |level|. The buffer has |buffer_len| bytes of data,
700 // with the first protected packet bytes starting at |ad_len|.
701 bool ApplyHeaderProtection(EncryptionLevel level,
702 char* buffer,
703 size_t buffer_len,
704 size_t ad_len);
705
706 // Removes header protection from an IETF QUIC packet header.
707 //
708 // The packet number from the header is read from |reader|, where the packet
709 // number is the next contents in |reader|. |reader| is only advanced by the
710 // length of the packet number, but it is also used to peek the sample needed
711 // for removing header protection.
712 //
713 // Properties needed for removing header protection are read from |header|.
714 // The packet number length and type byte are written to |header|.
715 //
716 // The packet number, after removing header protection and decoding it, is
717 // written to |full_packet_number|. Finally, the header, with header
718 // protection removed, is written to |associated_data| to be used in packet
719 // decryption. |packet| is used in computing the asociated data.
720 bool RemoveHeaderProtection(QuicDataReader* reader,
721 const QuicEncryptedPacket& packet,
722 QuicPacketHeader* header,
723 uint64_t* full_packet_number,
724 std::vector<char>* associated_data);
725
QUICHE teama6ef0a62019-03-07 20:34:33 -0500726 bool ProcessDataPacket(QuicDataReader* reader,
727 QuicPacketHeader* header,
728 const QuicEncryptedPacket& packet,
729 char* decrypted_buffer,
730 size_t buffer_length);
731
732 bool ProcessIetfDataPacket(QuicDataReader* encrypted_reader,
733 QuicPacketHeader* header,
734 const QuicEncryptedPacket& packet,
735 char* decrypted_buffer,
736 size_t buffer_length);
737
738 bool ProcessPublicResetPacket(QuicDataReader* reader,
739 const QuicPacketHeader& header);
740
741 bool ProcessVersionNegotiationPacket(QuicDataReader* reader,
742 const QuicPacketHeader& header);
743
dschinazi244f6dc2019-05-06 15:45:16 -0700744 bool ProcessRetryPacket(QuicDataReader* reader,
745 const QuicPacketHeader& header);
746
QUICHE teama6ef0a62019-03-07 20:34:33 -0500747 void MaybeProcessCoalescedPacket(const QuicDataReader& encrypted_reader,
748 uint64_t remaining_bytes_length,
749 const QuicPacketHeader& header);
750
751 bool MaybeProcessIetfLength(QuicDataReader* encrypted_reader,
752 QuicPacketHeader* header);
753
754 bool ProcessPublicHeader(QuicDataReader* reader,
755 bool packet_has_ietf_packet_header,
756 QuicPacketHeader* header);
757
758 // Processes the unauthenticated portion of the header into |header| from
759 // the current QuicDataReader. Returns true on success, false on failure.
760 bool ProcessUnauthenticatedHeader(QuicDataReader* encrypted_reader,
761 QuicPacketHeader* header);
762
fayang40315542019-05-09 09:19:09 -0700763 // Processes the version label in the packet header.
764 static bool ProcessVersionLabel(QuicDataReader* reader,
765 QuicVersionLabel* version_label);
766
767 // Validates and updates |destination_connection_id_length| and
dschinazi334f0232019-05-29 16:08:53 -0700768 // |source_connection_id_length|. When
769 // |should_update_expected_server_connection_id_length| is true, length
770 // validation is disabled and |expected_server_connection_id_length| is set
771 // to the appropriate length.
772 // TODO(b/133873272) refactor this method.
fayangccbab732019-05-13 10:11:25 -0700773 static bool ProcessAndValidateIetfConnectionIdLength(
774 QuicDataReader* reader,
fayang40315542019-05-09 09:19:09 -0700775 ParsedQuicVersion version,
dschinazi334f0232019-05-29 16:08:53 -0700776 Perspective perspective,
dschinazi8ff74822019-05-28 16:37:20 -0700777 bool should_update_expected_server_connection_id_length,
778 uint8_t* expected_server_connection_id_length,
fayang40315542019-05-09 09:19:09 -0700779 uint8_t* destination_connection_id_length,
fayangccbab732019-05-13 10:11:25 -0700780 uint8_t* source_connection_id_length,
781 std::string* detailed_error);
fayang40315542019-05-09 09:19:09 -0700782
QUICHE teama6ef0a62019-03-07 20:34:33 -0500783 bool ProcessIetfHeaderTypeByte(QuicDataReader* reader,
784 QuicPacketHeader* header);
785 bool ProcessIetfPacketHeader(QuicDataReader* reader,
786 QuicPacketHeader* header);
787
788 // First processes possibly truncated packet number. Calculates the full
789 // packet number from the truncated one and the last seen packet number, and
790 // stores it to |packet_number|.
791 bool ProcessAndCalculatePacketNumber(
792 QuicDataReader* reader,
793 QuicPacketNumberLength packet_number_length,
794 QuicPacketNumber base_packet_number,
795 uint64_t* packet_number);
796 bool ProcessFrameData(QuicDataReader* reader, const QuicPacketHeader& header);
797 bool ProcessIetfFrameData(QuicDataReader* reader,
798 const QuicPacketHeader& header);
799 bool ProcessStreamFrame(QuicDataReader* reader,
800 uint8_t frame_type,
801 QuicStreamFrame* frame);
802 bool ProcessAckFrame(QuicDataReader* reader, uint8_t frame_type);
803 bool ProcessTimestampsInAckFrame(uint8_t num_received_packets,
804 QuicPacketNumber largest_acked,
805 QuicDataReader* reader);
806 bool ProcessIetfAckFrame(QuicDataReader* reader,
807 uint64_t frame_type,
808 QuicAckFrame* ack_frame);
809 bool ProcessStopWaitingFrame(QuicDataReader* reader,
810 const QuicPacketHeader& header,
811 QuicStopWaitingFrame* stop_waiting);
812 bool ProcessRstStreamFrame(QuicDataReader* reader, QuicRstStreamFrame* frame);
813 bool ProcessConnectionCloseFrame(QuicDataReader* reader,
814 QuicConnectionCloseFrame* frame);
815 bool ProcessGoAwayFrame(QuicDataReader* reader, QuicGoAwayFrame* frame);
816 bool ProcessWindowUpdateFrame(QuicDataReader* reader,
817 QuicWindowUpdateFrame* frame);
818 bool ProcessBlockedFrame(QuicDataReader* reader, QuicBlockedFrame* frame);
819 void ProcessPaddingFrame(QuicDataReader* reader, QuicPaddingFrame* frame);
820 bool ProcessMessageFrame(QuicDataReader* reader,
821 bool no_message_length,
822 QuicMessageFrame* frame);
823
824 bool DecryptPayload(QuicStringPiece encrypted,
825 QuicStringPiece associated_data,
826 const QuicPacketHeader& header,
827 char* decrypted_buffer,
828 size_t buffer_length,
QUICHE team10b22a12019-03-21 15:31:42 -0700829 size_t* decrypted_length,
830 EncryptionLevel* decrypted_level);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500831
832 // Returns the full packet number from the truncated
833 // wire format version and the last seen packet number.
834 uint64_t CalculatePacketNumberFromWire(
835 QuicPacketNumberLength packet_number_length,
836 QuicPacketNumber base_packet_number,
837 uint64_t packet_number) const;
838
839 // Returns the QuicTime::Delta corresponding to the time from when the framer
840 // was created.
841 const QuicTime::Delta CalculateTimestampFromWire(uint32_t time_delta_us);
842
843 // Computes the wire size in bytes of time stamps in |ack|.
844 size_t GetAckFrameTimeStampSize(const QuicAckFrame& ack);
845
846 // Computes the wire size in bytes of the |ack| frame.
847 size_t GetAckFrameSize(const QuicAckFrame& ack,
848 QuicPacketNumberLength packet_number_length);
849 // Computes the wire-size, in bytes, of the |frame| ack frame, for IETF Quic.
850 size_t GetIetfAckFrameSize(const QuicAckFrame& frame);
851
852 // Computes the wire size in bytes of the |ack| frame.
853 size_t GetAckFrameSize(const QuicAckFrame& ack);
854
855 // Computes the wire size in bytes of the payload of |frame|.
856 size_t ComputeFrameLength(const QuicFrame& frame,
857 bool last_frame_in_packet,
858 QuicPacketNumberLength packet_number_length);
859
860 static bool AppendPacketNumber(QuicPacketNumberLength packet_number_length,
861 QuicPacketNumber packet_number,
862 QuicDataWriter* writer);
863 static bool AppendStreamId(size_t stream_id_length,
864 QuicStreamId stream_id,
865 QuicDataWriter* writer);
866 static bool AppendStreamOffset(size_t offset_length,
867 QuicStreamOffset offset,
868 QuicDataWriter* writer);
869
870 // Appends a single ACK block to |writer| and returns true if the block was
871 // successfully appended.
872 static bool AppendAckBlock(uint8_t gap,
873 QuicPacketNumberLength length_length,
874 uint64_t length,
875 QuicDataWriter* writer);
876
877 static uint8_t GetPacketNumberFlags(
878 QuicPacketNumberLength packet_number_length);
879
880 static AckFrameInfo GetAckFrameInfo(const QuicAckFrame& frame);
881
dschinazi48ac9192019-07-31 00:07:26 -0700882 static QuicErrorCode ParsePublicHeaderGoogleQuic(
883 QuicDataReader* reader,
884 uint8_t* first_byte,
885 PacketHeaderFormat* format,
886 bool* version_present,
887 QuicVersionLabel* version_label,
dschinazi243eabc2019-08-05 16:15:29 -0700888 ParsedQuicVersion* parsed_version,
dschinazi48ac9192019-07-31 00:07:26 -0700889 QuicConnectionId* destination_connection_id,
890 std::string* detailed_error);
891
dschinazib953d022019-08-01 18:05:58 -0700892 bool ValidateReceivedConnectionIds(const QuicPacketHeader& header);
893
QUICHE teama6ef0a62019-03-07 20:34:33 -0500894 // The Append* methods attempt to write the provided header or frame using the
895 // |writer|, and return true if successful.
896
897 bool AppendAckFrameAndTypeByte(const QuicAckFrame& frame,
898 QuicDataWriter* builder);
899 bool AppendTimestampsToAckFrame(const QuicAckFrame& frame,
900 QuicDataWriter* writer);
901
902 // Append IETF format ACK frame.
903 //
904 // AppendIetfAckFrameAndTypeByte adds the IETF type byte and the body
905 // of the frame.
906 bool AppendIetfAckFrameAndTypeByte(const QuicAckFrame& frame,
907 QuicDataWriter* writer);
908
909 // Used by AppendIetfAckFrameAndTypeByte to figure out how many ack
910 // blocks can be included.
911 int CalculateIetfAckBlockCount(const QuicAckFrame& frame,
912 QuicDataWriter* writer,
913 size_t available_space);
914 bool AppendStopWaitingFrame(const QuicPacketHeader& header,
915 const QuicStopWaitingFrame& frame,
916 QuicDataWriter* builder);
917 bool AppendRstStreamFrame(const QuicRstStreamFrame& frame,
918 QuicDataWriter* builder);
919 bool AppendConnectionCloseFrame(const QuicConnectionCloseFrame& frame,
920 QuicDataWriter* builder);
921 bool AppendGoAwayFrame(const QuicGoAwayFrame& frame, QuicDataWriter* writer);
922 bool AppendWindowUpdateFrame(const QuicWindowUpdateFrame& frame,
923 QuicDataWriter* writer);
924 bool AppendBlockedFrame(const QuicBlockedFrame& frame,
925 QuicDataWriter* writer);
926 bool AppendPaddingFrame(const QuicPaddingFrame& frame,
927 QuicDataWriter* writer);
928 bool AppendMessageFrameAndTypeByte(const QuicMessageFrame& frame,
929 bool last_frame_in_packet,
930 QuicDataWriter* writer);
931
932 // IETF frame processing methods.
933 bool ProcessIetfStreamFrame(QuicDataReader* reader,
934 uint8_t frame_type,
935 QuicStreamFrame* frame);
936 bool ProcessIetfConnectionCloseFrame(QuicDataReader* reader,
fkastenholze9d71a82019-04-09 05:12:13 -0700937 QuicConnectionCloseType type,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500938 QuicConnectionCloseFrame* frame);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500939 bool ProcessPathChallengeFrame(QuicDataReader* reader,
940 QuicPathChallengeFrame* frame);
941 bool ProcessPathResponseFrame(QuicDataReader* reader,
942 QuicPathResponseFrame* frame);
943 bool ProcessIetfResetStreamFrame(QuicDataReader* reader,
944 QuicRstStreamFrame* frame);
945 bool ProcessStopSendingFrame(QuicDataReader* reader,
946 QuicStopSendingFrame* stop_sending_frame);
947 bool ProcessCryptoFrame(QuicDataReader* reader, QuicCryptoFrame* frame);
948
949 // IETF frame appending methods. All methods append the type byte as well.
950 bool AppendIetfStreamFrame(const QuicStreamFrame& frame,
951 bool last_frame_in_packet,
952 QuicDataWriter* writer);
953 bool AppendIetfConnectionCloseFrame(const QuicConnectionCloseFrame& frame,
954 QuicDataWriter* writer);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500955 bool AppendPathChallengeFrame(const QuicPathChallengeFrame& frame,
956 QuicDataWriter* writer);
957 bool AppendPathResponseFrame(const QuicPathResponseFrame& frame,
958 QuicDataWriter* writer);
959 bool AppendIetfResetStreamFrame(const QuicRstStreamFrame& frame,
960 QuicDataWriter* writer);
961 bool AppendStopSendingFrame(const QuicStopSendingFrame& stop_sending_frame,
962 QuicDataWriter* writer);
963
964 // Append/consume IETF-Format MAX_DATA and MAX_STREAM_DATA frames
965 bool AppendMaxDataFrame(const QuicWindowUpdateFrame& frame,
966 QuicDataWriter* writer);
967 bool AppendMaxStreamDataFrame(const QuicWindowUpdateFrame& frame,
968 QuicDataWriter* writer);
969 bool ProcessMaxDataFrame(QuicDataReader* reader,
970 QuicWindowUpdateFrame* frame);
971 bool ProcessMaxStreamDataFrame(QuicDataReader* reader,
972 QuicWindowUpdateFrame* frame);
973
fkastenholz3c4eabf2019-04-22 07:49:59 -0700974 bool AppendMaxStreamsFrame(const QuicMaxStreamsFrame& frame,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500975 QuicDataWriter* writer);
976 bool ProcessMaxStreamsFrame(QuicDataReader* reader,
fkastenholz3c4eabf2019-04-22 07:49:59 -0700977 QuicMaxStreamsFrame* frame,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500978 uint64_t frame_type);
979
980 bool AppendIetfBlockedFrame(const QuicBlockedFrame& frame,
981 QuicDataWriter* writer);
982 bool ProcessIetfBlockedFrame(QuicDataReader* reader, QuicBlockedFrame* frame);
983
984 bool AppendStreamBlockedFrame(const QuicBlockedFrame& frame,
985 QuicDataWriter* writer);
986 bool ProcessStreamBlockedFrame(QuicDataReader* reader,
987 QuicBlockedFrame* frame);
988
fkastenholz3c4eabf2019-04-22 07:49:59 -0700989 bool AppendStreamsBlockedFrame(const QuicStreamsBlockedFrame& frame,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500990 QuicDataWriter* writer);
991 bool ProcessStreamsBlockedFrame(QuicDataReader* reader,
fkastenholz3c4eabf2019-04-22 07:49:59 -0700992 QuicStreamsBlockedFrame* frame,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500993 uint64_t frame_type);
994
995 bool AppendNewConnectionIdFrame(const QuicNewConnectionIdFrame& frame,
996 QuicDataWriter* writer);
997 bool ProcessNewConnectionIdFrame(QuicDataReader* reader,
998 QuicNewConnectionIdFrame* frame);
999 bool AppendRetireConnectionIdFrame(const QuicRetireConnectionIdFrame& frame,
1000 QuicDataWriter* writer);
1001 bool ProcessRetireConnectionIdFrame(QuicDataReader* reader,
1002 QuicRetireConnectionIdFrame* frame);
1003
1004 bool AppendNewTokenFrame(const QuicNewTokenFrame& frame,
1005 QuicDataWriter* writer);
1006 bool ProcessNewTokenFrame(QuicDataReader* reader, QuicNewTokenFrame* frame);
1007
1008 bool RaiseError(QuicErrorCode error);
1009
1010 // Returns true if |header| indicates a version negotiation packet.
1011 bool IsVersionNegotiation(const QuicPacketHeader& header,
1012 bool packet_has_ietf_packet_header) const;
1013
1014 // Calculates and returns type byte of stream frame.
1015 uint8_t GetStreamFrameTypeByte(const QuicStreamFrame& frame,
1016 bool last_frame_in_packet) const;
1017 uint8_t GetIetfStreamFrameTypeByte(const QuicStreamFrame& frame,
1018 bool last_frame_in_packet) const;
1019
1020 void set_error(QuicErrorCode error) { error_ = error; }
1021
1022 void set_detailed_error(const char* error) { detailed_error_ = error; }
dschinazi48ac9192019-07-31 00:07:26 -07001023 void set_detailed_error(std::string error) { detailed_error_ = error; }
QUICHE teama6ef0a62019-03-07 20:34:33 -05001024
vasilvvc48c8712019-03-11 13:38:16 -07001025 std::string detailed_error_;
QUICHE teama6ef0a62019-03-07 20:34:33 -05001026 QuicFramerVisitorInterface* visitor_;
1027 QuicErrorCode error_;
1028 // Updated by ProcessPacketHeader when it succeeds decrypting a larger packet.
1029 QuicPacketNumber largest_packet_number_;
QUICHE team10b22a12019-03-21 15:31:42 -07001030 // Largest successfully decrypted packet number per packet number space. Only
1031 // used when supports_multiple_packet_number_spaces_ is true.
1032 QuicPacketNumber largest_decrypted_packet_numbers_[NUM_PACKET_NUMBER_SPACES];
dschinazi7b9278c2019-05-20 07:36:21 -07001033 // Last server connection ID seen on the wire.
1034 QuicConnectionId last_serialized_server_connection_id_;
dschinazi346b7ce2019-06-05 01:38:18 -07001035 // Last client connection ID seen on the wire.
1036 QuicConnectionId last_serialized_client_connection_id_;
QUICHE teama6ef0a62019-03-07 20:34:33 -05001037 // Version of the protocol being used.
1038 ParsedQuicVersion version_;
1039 // This vector contains QUIC versions which we currently support.
1040 // This should be ordered such that the highest supported version is the first
1041 // element, with subsequent elements in descending order (versions can be
1042 // skipped as necessary).
1043 ParsedQuicVersionVector supported_versions_;
QUICHE team76086e42019-03-25 15:12:29 -07001044 // Decrypters used to decrypt packets during parsing.
1045 std::unique_ptr<QuicDecrypter> decrypter_[NUM_ENCRYPTION_LEVELS];
1046 // The encryption level of the primary decrypter to use in |decrypter_|.
QUICHE teama6ef0a62019-03-07 20:34:33 -05001047 EncryptionLevel decrypter_level_;
QUICHE team76086e42019-03-25 15:12:29 -07001048 // The encryption level of the alternative decrypter to use in |decrypter_|.
1049 // When set to NUM_ENCRYPTION_LEVELS, indicates that there is no alternative
QUICHE teama6ef0a62019-03-07 20:34:33 -05001050 // decrypter.
QUICHE team76086e42019-03-25 15:12:29 -07001051 EncryptionLevel alternative_decrypter_level_;
1052 // |alternative_decrypter_latch_| is true if, when the decrypter at
1053 // |alternative_decrypter_level_| successfully decrypts a packet, we should
1054 // install it as the only decrypter.
QUICHE teama6ef0a62019-03-07 20:34:33 -05001055 bool alternative_decrypter_latch_;
1056 // Encrypters used to encrypt packets via EncryptPayload().
1057 std::unique_ptr<QuicEncrypter> encrypter_[NUM_ENCRYPTION_LEVELS];
1058 // Tracks if the framer is being used by the entity that received the
1059 // connection or the entity that initiated it.
1060 Perspective perspective_;
1061 // If false, skip validation that the public flags are set to legal values.
1062 bool validate_flags_;
1063 // The diversification nonce from the last received packet.
1064 DiversificationNonce last_nonce_;
1065 // If true, send and process timestamps in the ACK frame.
1066 bool process_timestamps_;
1067 // The creation time of the connection, used to calculate timestamps.
1068 QuicTime creation_time_;
1069 // The last timestamp received if process_timestamps_ is true.
1070 QuicTime::Delta last_timestamp_;
1071
1072 // If this is a framer of a connection, this is the packet number of first
1073 // sending packet. If this is a framer of a framer of dispatcher, this is the
1074 // packet number of sent packets (for those which have packet number).
1075 const QuicPacketNumber first_sending_packet_number_;
1076
1077 // If not null, framer asks data_producer_ to write stream frame data. Not
1078 // owned. TODO(fayang): Consider add data producer to framer's constructor.
1079 QuicStreamFrameDataProducer* data_producer_;
1080
1081 // If true, framer infers packet header type (IETF/GQUIC) from version_.
1082 // Otherwise, framer infers packet header type from first byte of a received
1083 // packet.
1084 bool infer_packet_header_type_from_version_;
1085
1086 // IETF short headers contain a destination connection ID but do not
dschinazi346b7ce2019-06-05 01:38:18 -07001087 // encode its length. These variables contains the length we expect to read.
1088 // This is also used to validate the long header destination connection ID
1089 // lengths in older versions of QUIC.
dschinazi8ff74822019-05-28 16:37:20 -07001090 uint8_t expected_server_connection_id_length_;
dschinazi346b7ce2019-06-05 01:38:18 -07001091 uint8_t expected_client_connection_id_length_;
QUICHE team4d9d6292019-03-11 14:25:33 -07001092
QUICHE team10b22a12019-03-21 15:31:42 -07001093 // Indicates whether this framer supports multiple packet number spaces.
1094 bool supports_multiple_packet_number_spaces_;
nharper55fa6132019-05-07 19:37:21 -07001095
1096 // The length in bytes of the last packet number written to an IETF-framed
1097 // packet.
1098 size_t last_written_packet_number_length_;
fkastenholz4dc4ba32019-07-30 09:55:25 -07001099
1100 // The amount to shift the ack timestamp in ACK frames. The default is 3.
1101 // Local_ is the amount this node shifts timestamps in ACK frames it
1102 // generates. it is sent to the peer in a transport parameter negotiation.
1103 // Peer_ is the amount the peer shifts timestamps when it sends ACK frames to
1104 // this node. This node "unshifts" by this amount. The value is received from
1105 // the peer in the transport parameter negotiation. IETF QUIC only.
1106 uint32_t peer_ack_delay_exponent_;
1107 uint32_t local_ack_delay_exponent_;
fkastenholza3660102019-08-28 05:19:24 -07001108
1109 // The type of received IETF frame currently being processed. 0 when not
1110 // processing a frame or when processing Google QUIC frames. Used to populate
1111 // the Transport Connection Close when there is an error during frame
1112 // processing.
1113 uint64_t current_received_frame_type_;
QUICHE teama6ef0a62019-03-07 20:34:33 -05001114};
1115
fkastenholzb4dade72019-08-05 06:54:20 -07001116// Look for and parse the error code from the "<quic_error_code>:" text that
1117// may be present at the start of the CONNECTION_CLOSE error details string.
1118// This text, inserted by the peer if it's using Google's QUIC implementation,
fkastenholz488a4622019-08-26 06:24:46 -07001119// contains additional error information that narrows down the exact error. The
1120// extracted error code and (possibly updated) error_details string are returned
1121// in |*frame|. If an error code is not found in the error details then the
1122// extracted_error_code is set to QuicErrorCode::QUIC_IETF_GQUIC_ERROR_MISSING.
1123// If there is an error code in the string then it is removed from the string.
1124QUIC_EXPORT_PRIVATE void MaybeExtractQuicErrorCode(
1125 QuicConnectionCloseFrame* frame);
fkastenholzb4dade72019-08-05 06:54:20 -07001126
QUICHE teama6ef0a62019-03-07 20:34:33 -05001127} // namespace quic
1128
1129#endif // QUICHE_QUIC_CORE_QUIC_FRAMER_H_