blob: ab0e8d7289f49bfbd2857edfb09deb096d54e1cb [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"
18#include "net/third_party/quiche/src/quic/platform/api/quic_endian.h"
19#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.
80 virtual bool OnProtocolVersionMismatch(ParsedQuicVersion received_version,
81 PacketHeaderFormat form) = 0;
82
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
131 // Called when a StreamFrame has been parsed.
132 virtual bool OnStreamFrame(const QuicStreamFrame& frame) = 0;
133
134 // Called when a CRYPTO frame has been parsed.
135 virtual bool OnCryptoFrame(const QuicCryptoFrame& frame) = 0;
136
137 // Called when largest acked of an AckFrame has been parsed.
138 virtual bool OnAckFrameStart(QuicPacketNumber largest_acked,
139 QuicTime::Delta ack_delay_time) = 0;
140
141 // Called when ack range [start, end) of an AckFrame has been parsed.
142 virtual bool OnAckRange(QuicPacketNumber start, QuicPacketNumber end) = 0;
143
144 // Called when a timestamp in the AckFrame has been parsed.
145 virtual bool OnAckTimestamp(QuicPacketNumber packet_number,
146 QuicTime timestamp) = 0;
147
148 // Called after the last ack range in an AckFrame has been parsed.
149 // |start| is the starting value of the last ack range.
150 virtual bool OnAckFrameEnd(QuicPacketNumber start) = 0;
151
152 // Called when a StopWaitingFrame has been parsed.
153 virtual bool OnStopWaitingFrame(const QuicStopWaitingFrame& frame) = 0;
154
155 // Called when a QuicPaddingFrame has been parsed.
156 virtual bool OnPaddingFrame(const QuicPaddingFrame& frame) = 0;
157
158 // Called when a PingFrame has been parsed.
159 virtual bool OnPingFrame(const QuicPingFrame& frame) = 0;
160
161 // Called when a RstStreamFrame has been parsed.
162 virtual bool OnRstStreamFrame(const QuicRstStreamFrame& frame) = 0;
163
fkastenholz04bd4f32019-04-16 12:24:38 -0700164 // Called when a ConnectionCloseFrame, of any type, has been parsed.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500165 virtual bool OnConnectionCloseFrame(
166 const QuicConnectionCloseFrame& frame) = 0;
167
QUICHE teama6ef0a62019-03-07 20:34:33 -0500168 // Called when a StopSendingFrame has been parsed.
169 virtual bool OnStopSendingFrame(const QuicStopSendingFrame& frame) = 0;
170
171 // Called when a PathChallengeFrame has been parsed.
172 virtual bool OnPathChallengeFrame(const QuicPathChallengeFrame& frame) = 0;
173
174 // Called when a PathResponseFrame has been parsed.
175 virtual bool OnPathResponseFrame(const QuicPathResponseFrame& frame) = 0;
176
177 // Called when a GoAwayFrame has been parsed.
178 virtual bool OnGoAwayFrame(const QuicGoAwayFrame& frame) = 0;
179
180 // Called when a WindowUpdateFrame has been parsed.
181 virtual bool OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame) = 0;
182
183 // Called when a BlockedFrame has been parsed.
184 virtual bool OnBlockedFrame(const QuicBlockedFrame& frame) = 0;
185
186 // Called when a NewConnectionIdFrame has been parsed.
187 virtual bool OnNewConnectionIdFrame(
188 const QuicNewConnectionIdFrame& frame) = 0;
189
190 // Called when a RetireConnectionIdFrame has been parsed.
191 virtual bool OnRetireConnectionIdFrame(
192 const QuicRetireConnectionIdFrame& frame) = 0;
193
194 // Called when a NewTokenFrame has been parsed.
195 virtual bool OnNewTokenFrame(const QuicNewTokenFrame& frame) = 0;
196
197 // Called when a message frame has been parsed.
198 virtual bool OnMessageFrame(const QuicMessageFrame& frame) = 0;
199
200 // Called when a packet has been completely processed.
201 virtual void OnPacketComplete() = 0;
202
203 // Called to check whether |token| is a valid stateless reset token.
204 virtual bool IsValidStatelessResetToken(QuicUint128 token) const = 0;
205
206 // Called when an IETF stateless reset packet has been parsed and validated
207 // with the stateless reset token.
208 virtual void OnAuthenticatedIetfStatelessResetPacket(
209 const QuicIetfStatelessResetPacket& packet) = 0;
210
fkastenholz3c4eabf2019-04-22 07:49:59 -0700211 // Called when an IETF MaxStreams frame has been parsed.
212 virtual bool OnMaxStreamsFrame(const QuicMaxStreamsFrame& frame) = 0;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500213
fkastenholz3c4eabf2019-04-22 07:49:59 -0700214 // Called when an IETF StreamsBlocked frame has been parsed.
215 virtual bool OnStreamsBlockedFrame(const QuicStreamsBlockedFrame& frame) = 0;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500216};
217
218// Class for parsing and constructing QUIC packets. It has a
219// QuicFramerVisitorInterface that is called when packets are parsed.
220class QUIC_EXPORT_PRIVATE QuicFramer {
221 public:
222 // Constructs a new framer that installs a kNULL QuicEncrypter and
QUICHE team6987b4a2019-03-15 16:23:04 -0700223 // QuicDecrypter for level ENCRYPTION_INITIAL. |supported_versions| specifies
224 // the list of supported QUIC versions. |quic_version_| is set to the maximum
QUICHE teama6ef0a62019-03-07 20:34:33 -0500225 // version in |supported_versions|.
226 QuicFramer(const ParsedQuicVersionVector& supported_versions,
227 QuicTime creation_time,
228 Perspective perspective,
229 uint8_t expected_connection_id_length);
230 QuicFramer(const QuicFramer&) = delete;
231 QuicFramer& operator=(const QuicFramer&) = delete;
232
233 virtual ~QuicFramer();
234
235 // Returns true if |version| is a supported transport version.
236 bool IsSupportedTransportVersion(const QuicTransportVersion version) const;
237
238 // Returns true if |version| is a supported protocol version.
239 bool IsSupportedVersion(const ParsedQuicVersion version) const;
240
241 // Set callbacks to be called from the framer. A visitor must be set, or
242 // else the framer will likely crash. It is acceptable for the visitor
243 // to do nothing. If this is called multiple times, only the last visitor
244 // will be used.
245 void set_visitor(QuicFramerVisitorInterface* visitor) { visitor_ = visitor; }
246
247 const ParsedQuicVersionVector& supported_versions() const {
248 return supported_versions_;
249 }
250
251 QuicTransportVersion transport_version() const {
252 return version_.transport_version;
253 }
254
255 ParsedQuicVersion version() const { return version_; }
256
257 void set_version(const ParsedQuicVersion version);
258
259 // Does not DCHECK for supported version. Used by tests to set unsupported
260 // version to trigger version negotiation.
261 void set_version_for_tests(const ParsedQuicVersion version) {
262 version_ = version;
263 }
264
265 QuicErrorCode error() const { return error_; }
266
267 // Allows enabling or disabling of timestamp processing and serialization.
268 void set_process_timestamps(bool process_timestamps) {
269 process_timestamps_ = process_timestamps;
270 }
271
272 // Pass a UDP packet into the framer for parsing.
dschinazi244f6dc2019-05-06 15:45:16 -0700273 // Return true if the packet was processed successfully. |packet| must be a
QUICHE teama6ef0a62019-03-07 20:34:33 -0500274 // single, complete UDP packet (not a frame of a packet). This packet
275 // might be null padded past the end of the payload, which will be correctly
276 // ignored.
277 bool ProcessPacket(const QuicEncryptedPacket& packet);
278
279 // Largest size in bytes of all stream frame fields without the payload.
280 static size_t GetMinStreamFrameSize(QuicTransportVersion version,
281 QuicStreamId stream_id,
282 QuicStreamOffset offset,
283 bool last_frame_in_packet,
284 QuicPacketLength data_length);
285 // Returns the overhead of framing a CRYPTO frame with the specific offset and
286 // data length provided, but not counting the size of the data payload.
287 static size_t GetMinCryptoFrameSize(QuicStreamOffset offset,
288 QuicPacketLength data_length);
289 static size_t GetMessageFrameSize(QuicTransportVersion version,
290 bool last_frame_in_packet,
291 QuicByteCount length);
292 // Size in bytes of all ack frame fields without the missing packets or ack
293 // blocks.
294 static size_t GetMinAckFrameSize(
295 QuicTransportVersion version,
296 QuicPacketNumberLength largest_observed_length);
297 // Size in bytes of a stop waiting frame.
298 static size_t GetStopWaitingFrameSize(
299 QuicTransportVersion version,
300 QuicPacketNumberLength packet_number_length);
301 // Size in bytes of all reset stream frame fields.
302 static size_t GetRstStreamFrameSize(QuicTransportVersion version,
303 const QuicRstStreamFrame& frame);
fkastenholza037b8b2019-05-07 06:00:05 -0700304 // Size in bytes of all connection close frame fields, including the error
305 // details.
306 static size_t GetConnectionCloseFrameSize(
QUICHE teama6ef0a62019-03-07 20:34:33 -0500307 QuicTransportVersion version,
308 const QuicConnectionCloseFrame& frame);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500309 // Size in bytes of all GoAway frame fields without the reason phrase.
310 static size_t GetMinGoAwayFrameSize();
311 // Size in bytes of all WindowUpdate frame fields.
312 // For version 99, determines whether a MAX DATA or MAX STREAM DATA frame will
313 // be generated and calculates the appropriate size.
314 static size_t GetWindowUpdateFrameSize(QuicTransportVersion version,
315 const QuicWindowUpdateFrame& frame);
316 // Size in bytes of all MaxStreams frame fields.
317 static size_t GetMaxStreamsFrameSize(QuicTransportVersion version,
fkastenholz3c4eabf2019-04-22 07:49:59 -0700318 const QuicMaxStreamsFrame& frame);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500319 // Size in bytes of all StreamsBlocked frame fields.
320 static size_t GetStreamsBlockedFrameSize(
321 QuicTransportVersion version,
fkastenholz3c4eabf2019-04-22 07:49:59 -0700322 const QuicStreamsBlockedFrame& frame);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500323 // Size in bytes of all Blocked frame fields.
324 static size_t GetBlockedFrameSize(QuicTransportVersion version,
325 const QuicBlockedFrame& frame);
326 // Size in bytes of PathChallenge frame.
327 static size_t GetPathChallengeFrameSize(const QuicPathChallengeFrame& frame);
328 // Size in bytes of PathResponse frame.
329 static size_t GetPathResponseFrameSize(const QuicPathResponseFrame& frame);
330 // Size in bytes required to serialize the stream id.
331 static size_t GetStreamIdSize(QuicStreamId stream_id);
332 // Size in bytes required to serialize the stream offset.
333 static size_t GetStreamOffsetSize(QuicTransportVersion version,
334 QuicStreamOffset offset);
335 // Size in bytes for a serialized new connection id frame
336 static size_t GetNewConnectionIdFrameSize(
337 const QuicNewConnectionIdFrame& frame);
338
339 // Size in bytes for a serialized retire connection id frame
340 static size_t GetRetireConnectionIdFrameSize(
341 const QuicRetireConnectionIdFrame& frame);
342
343 // Size in bytes for a serialized new token frame
344 static size_t GetNewTokenFrameSize(const QuicNewTokenFrame& frame);
345
346 // Size in bytes required for a serialized stop sending frame.
347 static size_t GetStopSendingFrameSize(const QuicStopSendingFrame& frame);
348
349 // Size in bytes required for a serialized retransmittable control |frame|.
350 static size_t GetRetransmittableControlFrameSize(QuicTransportVersion version,
351 const QuicFrame& frame);
352
353 // Returns the number of bytes added to the packet for the specified frame,
354 // and 0 if the frame doesn't fit. Includes the header size for the first
355 // frame.
356 size_t GetSerializedFrameLength(const QuicFrame& frame,
357 size_t free_bytes,
358 bool first_frame_in_packet,
359 bool last_frame_in_packet,
360 QuicPacketNumberLength packet_number_length);
361
362 // Returns the associated data from the encrypted packet |encrypted| as a
363 // stringpiece.
364 static QuicStringPiece GetAssociatedDataFromEncryptedPacket(
365 QuicTransportVersion version,
366 const QuicEncryptedPacket& encrypted,
367 QuicConnectionIdLength destination_connection_id_length,
368 QuicConnectionIdLength source_connection_id_length,
369 bool includes_version,
370 bool includes_diversification_nonce,
371 QuicPacketNumberLength packet_number_length,
372 QuicVariableLengthIntegerLength retry_token_length_length,
373 uint64_t retry_token_length,
374 QuicVariableLengthIntegerLength length_length);
375
fayangccbab732019-05-13 10:11:25 -0700376 // Lightweight parsing of |packet| and populates |format|, |version_flag|,
377 // |version_label|, |destination_connection_id_length|,
378 // |destination_connection_id| and |detailed_error|. Please note,
379 // |expected_connection_id_length| is only used to determine IETF short header
380 // packet's destination connection ID length.
381 static QuicErrorCode ProcessPacketDispatcher(
382 const QuicEncryptedPacket& packet,
383 uint8_t expected_connection_id_length,
384 PacketHeaderFormat* format,
385 bool* version_flag,
386 QuicVersionLabel* version_label,
387 uint8_t* destination_connection_id_length,
388 QuicConnectionId* destination_connection_id,
389 std::string* detailed_error);
390
QUICHE teama6ef0a62019-03-07 20:34:33 -0500391 // Serializes a packet containing |frames| into |buffer|.
392 // Returns the length of the packet, which must not be longer than
393 // |packet_length|. Returns 0 if it fails to serialize.
394 size_t BuildDataPacket(const QuicPacketHeader& header,
395 const QuicFrames& frames,
396 char* buffer,
397 size_t packet_length,
398 EncryptionLevel level);
399
400 // Serializes a probing packet, which is a padded PING packet. Returns the
401 // length of the packet. Returns 0 if it fails to serialize.
402 size_t BuildConnectivityProbingPacket(const QuicPacketHeader& header,
403 char* buffer,
404 size_t packet_length,
405 EncryptionLevel level);
406
QUICHE teama6ef0a62019-03-07 20:34:33 -0500407 // Serialize a probing packet that uses IETF QUIC's PATH CHALLENGE frame. Also
408 // fills the packet with padding.
409 size_t BuildPaddedPathChallengePacket(const QuicPacketHeader& header,
410 char* buffer,
411 size_t packet_length,
412 QuicPathFrameBuffer* payload,
413 QuicRandom* randomizer,
414 EncryptionLevel level);
415
416 // Serialize a probing response packet that uses IETF QUIC's PATH RESPONSE
417 // frame. Also fills the packet with padding if |is_padded| is
418 // true. |payloads| is always emptied, even if the packet can not be
419 // successfully built.
420 size_t BuildPathResponsePacket(const QuicPacketHeader& header,
421 char* buffer,
422 size_t packet_length,
423 const QuicDeque<QuicPathFrameBuffer>& payloads,
424 const bool is_padded,
425 EncryptionLevel level);
426
427 // Returns a new public reset packet.
428 static std::unique_ptr<QuicEncryptedPacket> BuildPublicResetPacket(
429 const QuicPublicResetPacket& packet);
430
431 // Returns a new IETF stateless reset packet.
432 static std::unique_ptr<QuicEncryptedPacket> BuildIetfStatelessResetPacket(
433 QuicConnectionId connection_id,
434 QuicUint128 stateless_reset_token);
435
436 // Returns a new version negotiation packet.
437 static std::unique_ptr<QuicEncryptedPacket> BuildVersionNegotiationPacket(
438 QuicConnectionId connection_id,
439 bool ietf_quic,
440 const ParsedQuicVersionVector& versions);
441
442 // Returns a new IETF version negotiation packet.
443 static std::unique_ptr<QuicEncryptedPacket> BuildIetfVersionNegotiationPacket(
444 QuicConnectionId connection_id,
445 const ParsedQuicVersionVector& versions);
446
447 // If header.version_flag is set, the version in the
448 // packet will be set -- but it will be set from version_ not
449 // header.versions.
450 bool AppendPacketHeader(const QuicPacketHeader& header,
451 QuicDataWriter* writer,
452 size_t* length_field_offset);
453 bool AppendIetfHeaderTypeByte(const QuicPacketHeader& header,
454 QuicDataWriter* writer);
455 bool AppendIetfPacketHeader(const QuicPacketHeader& header,
456 QuicDataWriter* writer,
457 size_t* length_field_offset);
458 bool WriteIetfLongHeaderLength(const QuicPacketHeader& header,
459 QuicDataWriter* writer,
460 size_t length_field_offset,
461 EncryptionLevel level);
462 bool AppendTypeByte(const QuicFrame& frame,
463 bool last_frame_in_packet,
464 QuicDataWriter* writer);
465 bool AppendIetfTypeByte(const QuicFrame& frame,
466 bool last_frame_in_packet,
467 QuicDataWriter* writer);
468 size_t AppendIetfFrames(const QuicFrames& frames, QuicDataWriter* writer);
469 bool AppendStreamFrame(const QuicStreamFrame& frame,
470 bool last_frame_in_packet,
471 QuicDataWriter* writer);
472 bool AppendCryptoFrame(const QuicCryptoFrame& frame, QuicDataWriter* writer);
473
474 // SetDecrypter sets the primary decrypter, replacing any that already exists.
475 // If an alternative decrypter is in place then the function DCHECKs. This is
476 // intended for cases where one knows that future packets will be using the
477 // new decrypter and the previous decrypter is now obsolete. |level| indicates
478 // the encryption level of the new decrypter.
479 void SetDecrypter(EncryptionLevel level,
480 std::unique_ptr<QuicDecrypter> decrypter);
481
482 // SetAlternativeDecrypter sets a decrypter that may be used to decrypt
483 // future packets. |level| indicates the encryption level of the decrypter. If
484 // |latch_once_used| is true, then the first time that the decrypter is
485 // successful it will replace the primary decrypter. Otherwise both
486 // decrypters will remain active and the primary decrypter will be the one
487 // last used.
488 void SetAlternativeDecrypter(EncryptionLevel level,
489 std::unique_ptr<QuicDecrypter> decrypter,
490 bool latch_once_used);
491
zhongyi546cc452019-04-12 15:27:49 -0700492 void InstallDecrypter(EncryptionLevel level,
493 std::unique_ptr<QuicDecrypter> decrypter);
494 void RemoveDecrypter(EncryptionLevel level);
495
496 const QuicDecrypter* GetDecrypter(EncryptionLevel level) const;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500497 const QuicDecrypter* decrypter() const;
498 const QuicDecrypter* alternative_decrypter() const;
499
500 // Changes the encrypter used for level |level| to |encrypter|.
501 void SetEncrypter(EncryptionLevel level,
502 std::unique_ptr<QuicEncrypter> encrypter);
503
504 // Encrypts a payload in |buffer|. |ad_len| is the length of the associated
505 // data. |total_len| is the length of the associated data plus plaintext.
506 // |buffer_len| is the full length of the allocated buffer.
507 size_t EncryptInPlace(EncryptionLevel level,
508 QuicPacketNumber packet_number,
509 size_t ad_len,
510 size_t total_len,
511 size_t buffer_len,
512 char* buffer);
513
514 // Returns the length of the data encrypted into |buffer| if |buffer_len| is
515 // long enough, and otherwise 0.
516 size_t EncryptPayload(EncryptionLevel level,
517 QuicPacketNumber packet_number,
518 const QuicPacket& packet,
519 char* buffer,
520 size_t buffer_len);
521
522 // Returns the length of the ciphertext that would be generated by encrypting
523 // to plaintext of size |plaintext_size| at the given level.
524 size_t GetCiphertextSize(EncryptionLevel level, size_t plaintext_size) const;
525
526 // Returns the maximum length of plaintext that can be encrypted
527 // to ciphertext no larger than |ciphertext_size|.
528 size_t GetMaxPlaintextSize(size_t ciphertext_size);
529
vasilvvc48c8712019-03-11 13:38:16 -0700530 const std::string& detailed_error() { return detailed_error_; }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500531
532 // The minimum packet number length required to represent |packet_number|.
533 static QuicPacketNumberLength GetMinPacketNumberLength(
534 QuicTransportVersion version,
535 QuicPacketNumber packet_number);
536
537 void SetSupportedVersions(const ParsedQuicVersionVector& versions) {
538 supported_versions_ = versions;
539 version_ = versions[0];
540 }
541
542 // Tell framer to infer packet header type from version_.
543 void InferPacketHeaderTypeFromVersion();
544
QUICHE teama6ef0a62019-03-07 20:34:33 -0500545 // Returns true if |header| is considered as an stateless reset packet.
546 bool IsIetfStatelessResetPacket(const QuicPacketHeader& header) const;
547
548 // Returns true if encrypter of |level| is available.
549 bool HasEncrypterOfEncryptionLevel(EncryptionLevel level) const;
550
551 void set_validate_flags(bool value) { validate_flags_ = value; }
552
553 Perspective perspective() const { return perspective_; }
554
555 QuicVersionLabel last_version_label() const { return last_version_label_; }
556
557 void set_data_producer(QuicStreamFrameDataProducer* data_producer) {
558 data_producer_ = data_producer;
559 }
560
561 // Returns true if we are doing IETF-formatted packets.
562 // In the future this could encompass a wide variety of
563 // versions. Doing the test by name ("ietf format") rather
564 // than version number localizes the version/ietf-ness binding
565 // to this method.
566 bool is_ietf_format() {
567 return version_.transport_version == QUIC_VERSION_99;
568 }
569
570 QuicTime creation_time() const { return creation_time_; }
571
572 QuicPacketNumber first_sending_packet_number() const {
573 return first_sending_packet_number_;
574 }
575
QUICHE team4d9d6292019-03-11 14:25:33 -0700576 // If true, QuicFramer will change its expected connection ID length
577 // to the received destination connection ID length of all IETF long headers.
578 void SetShouldUpdateExpectedConnectionIdLength(
579 bool should_update_expected_connection_id_length) {
580 should_update_expected_connection_id_length_ =
581 should_update_expected_connection_id_length;
582 }
583
QUICHE team8e2e4532019-03-14 14:37:56 -0700584 // The connection ID length the framer expects on incoming IETF short headers.
585 uint8_t GetExpectedConnectionIdLength() {
586 return expected_connection_id_length_;
587 }
588
QUICHE team10b22a12019-03-21 15:31:42 -0700589 void EnableMultiplePacketNumberSpacesSupport();
590
dschinazide0f6dc2019-05-15 16:10:11 -0700591 // Writes an array of bytes that, if sent as a UDP datagram, will trigger
592 // IETF QUIC Version Negotiation on servers. The bytes will be written to
593 // |packet_bytes|, which must point to |packet_length| bytes of memory.
594 // |packet_length| must be in the range [1200, 65535].
595 // |destination_connection_id_bytes| will be sent as the destination
596 // connection ID, and must point to |destination_connection_id_length| bytes
597 // of memory. |destination_connection_id_length| must be either 0 or in the
598 // range [4,18]. When targeting Google servers, it is recommended to use a
599 // |destination_connection_id_length| of 8.
600 static bool WriteClientVersionNegotiationProbePacket(
601 char* packet_bytes,
602 QuicByteCount packet_length,
603 const char* destination_connection_id_bytes,
604 uint8_t destination_connection_id_length);
605
606 // Parses a packet which a QUIC server sent in response to a packet sent by
607 // WriteClientVersionNegotiationProbePacket. |packet_bytes| must point to
608 // |packet_length| bytes in memory which represent the response.
609 // |packet_length| must be greater or equal to 6. This method will fill in
610 // |source_connection_id_bytes| which must point to at least 18 bytes in
611 // memory. |source_connection_id_length_out| will contain the length of the
612 // received source connection ID, which on success will match the contents of
613 // the destination connection ID passed in to
614 // WriteClientVersionNegotiationProbePacket. In the case of a failure,
615 // |detailed_error| will be filled in with an explanation of what failed.
616 static bool ParseServerVersionNegotiationProbeResponse(
617 const char* packet_bytes,
618 QuicByteCount packet_length,
619 char* source_connection_id_bytes,
620 uint8_t* source_connection_id_length_out,
621 std::string* detailed_error);
622
QUICHE teama6ef0a62019-03-07 20:34:33 -0500623 private:
624 friend class test::QuicFramerPeer;
625
626 typedef std::map<QuicPacketNumber, uint8_t> NackRangeMap;
627
628 struct AckFrameInfo {
629 AckFrameInfo();
630 AckFrameInfo(const AckFrameInfo& other);
631 ~AckFrameInfo();
632
633 // The maximum ack block length.
634 QuicPacketCount max_block_length;
635 // Length of first ack block.
636 QuicPacketCount first_block_length;
637 // Number of ACK blocks needed for the ACK frame.
638 size_t num_ack_blocks;
639 };
640
nharper55fa6132019-05-07 19:37:21 -0700641 // Applies header protection to an IETF QUIC packet header in |buffer| using
642 // the encrypter for level |level|. The buffer has |buffer_len| bytes of data,
643 // with the first protected packet bytes starting at |ad_len|.
644 bool ApplyHeaderProtection(EncryptionLevel level,
645 char* buffer,
646 size_t buffer_len,
647 size_t ad_len);
648
649 // Removes header protection from an IETF QUIC packet header.
650 //
651 // The packet number from the header is read from |reader|, where the packet
652 // number is the next contents in |reader|. |reader| is only advanced by the
653 // length of the packet number, but it is also used to peek the sample needed
654 // for removing header protection.
655 //
656 // Properties needed for removing header protection are read from |header|.
657 // The packet number length and type byte are written to |header|.
658 //
659 // The packet number, after removing header protection and decoding it, is
660 // written to |full_packet_number|. Finally, the header, with header
661 // protection removed, is written to |associated_data| to be used in packet
662 // decryption. |packet| is used in computing the asociated data.
663 bool RemoveHeaderProtection(QuicDataReader* reader,
664 const QuicEncryptedPacket& packet,
665 QuicPacketHeader* header,
666 uint64_t* full_packet_number,
667 std::vector<char>* associated_data);
668
QUICHE teama6ef0a62019-03-07 20:34:33 -0500669 bool ProcessDataPacket(QuicDataReader* reader,
670 QuicPacketHeader* header,
671 const QuicEncryptedPacket& packet,
672 char* decrypted_buffer,
673 size_t buffer_length);
674
675 bool ProcessIetfDataPacket(QuicDataReader* encrypted_reader,
676 QuicPacketHeader* header,
677 const QuicEncryptedPacket& packet,
678 char* decrypted_buffer,
679 size_t buffer_length);
680
681 bool ProcessPublicResetPacket(QuicDataReader* reader,
682 const QuicPacketHeader& header);
683
684 bool ProcessVersionNegotiationPacket(QuicDataReader* reader,
685 const QuicPacketHeader& header);
686
dschinazi244f6dc2019-05-06 15:45:16 -0700687 bool ProcessRetryPacket(QuicDataReader* reader,
688 const QuicPacketHeader& header);
689
QUICHE teama6ef0a62019-03-07 20:34:33 -0500690 bool MaybeProcessIetfInitialRetryToken(QuicDataReader* encrypted_reader,
691 QuicPacketHeader* header);
692
693 void MaybeProcessCoalescedPacket(const QuicDataReader& encrypted_reader,
694 uint64_t remaining_bytes_length,
695 const QuicPacketHeader& header);
696
697 bool MaybeProcessIetfLength(QuicDataReader* encrypted_reader,
698 QuicPacketHeader* header);
699
700 bool ProcessPublicHeader(QuicDataReader* reader,
701 bool packet_has_ietf_packet_header,
702 QuicPacketHeader* header);
703
704 // Processes the unauthenticated portion of the header into |header| from
705 // the current QuicDataReader. Returns true on success, false on failure.
706 bool ProcessUnauthenticatedHeader(QuicDataReader* encrypted_reader,
707 QuicPacketHeader* header);
708
fayang40315542019-05-09 09:19:09 -0700709 // Processes the version label in the packet header.
710 static bool ProcessVersionLabel(QuicDataReader* reader,
711 QuicVersionLabel* version_label);
712
713 // Validates and updates |destination_connection_id_length| and
714 // |source_connection_id_length|.
fayangccbab732019-05-13 10:11:25 -0700715 static bool ProcessAndValidateIetfConnectionIdLength(
716 QuicDataReader* reader,
fayang40315542019-05-09 09:19:09 -0700717 ParsedQuicVersion version,
718 bool should_update_expected_connection_id_length,
719 uint8_t* expected_connection_id_length,
720 uint8_t* destination_connection_id_length,
fayangccbab732019-05-13 10:11:25 -0700721 uint8_t* source_connection_id_length,
722 std::string* detailed_error);
fayang40315542019-05-09 09:19:09 -0700723
QUICHE teama6ef0a62019-03-07 20:34:33 -0500724 bool ProcessIetfHeaderTypeByte(QuicDataReader* reader,
725 QuicPacketHeader* header);
726 bool ProcessIetfPacketHeader(QuicDataReader* reader,
727 QuicPacketHeader* header);
728
729 // First processes possibly truncated packet number. Calculates the full
730 // packet number from the truncated one and the last seen packet number, and
731 // stores it to |packet_number|.
732 bool ProcessAndCalculatePacketNumber(
733 QuicDataReader* reader,
734 QuicPacketNumberLength packet_number_length,
735 QuicPacketNumber base_packet_number,
736 uint64_t* packet_number);
737 bool ProcessFrameData(QuicDataReader* reader, const QuicPacketHeader& header);
738 bool ProcessIetfFrameData(QuicDataReader* reader,
739 const QuicPacketHeader& header);
740 bool ProcessStreamFrame(QuicDataReader* reader,
741 uint8_t frame_type,
742 QuicStreamFrame* frame);
743 bool ProcessAckFrame(QuicDataReader* reader, uint8_t frame_type);
744 bool ProcessTimestampsInAckFrame(uint8_t num_received_packets,
745 QuicPacketNumber largest_acked,
746 QuicDataReader* reader);
747 bool ProcessIetfAckFrame(QuicDataReader* reader,
748 uint64_t frame_type,
749 QuicAckFrame* ack_frame);
750 bool ProcessStopWaitingFrame(QuicDataReader* reader,
751 const QuicPacketHeader& header,
752 QuicStopWaitingFrame* stop_waiting);
753 bool ProcessRstStreamFrame(QuicDataReader* reader, QuicRstStreamFrame* frame);
754 bool ProcessConnectionCloseFrame(QuicDataReader* reader,
755 QuicConnectionCloseFrame* frame);
756 bool ProcessGoAwayFrame(QuicDataReader* reader, QuicGoAwayFrame* frame);
757 bool ProcessWindowUpdateFrame(QuicDataReader* reader,
758 QuicWindowUpdateFrame* frame);
759 bool ProcessBlockedFrame(QuicDataReader* reader, QuicBlockedFrame* frame);
760 void ProcessPaddingFrame(QuicDataReader* reader, QuicPaddingFrame* frame);
761 bool ProcessMessageFrame(QuicDataReader* reader,
762 bool no_message_length,
763 QuicMessageFrame* frame);
764
765 bool DecryptPayload(QuicStringPiece encrypted,
766 QuicStringPiece associated_data,
767 const QuicPacketHeader& header,
768 char* decrypted_buffer,
769 size_t buffer_length,
QUICHE team10b22a12019-03-21 15:31:42 -0700770 size_t* decrypted_length,
771 EncryptionLevel* decrypted_level);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500772
773 // Returns the full packet number from the truncated
774 // wire format version and the last seen packet number.
775 uint64_t CalculatePacketNumberFromWire(
776 QuicPacketNumberLength packet_number_length,
777 QuicPacketNumber base_packet_number,
778 uint64_t packet_number) const;
779
780 // Returns the QuicTime::Delta corresponding to the time from when the framer
781 // was created.
782 const QuicTime::Delta CalculateTimestampFromWire(uint32_t time_delta_us);
783
784 // Computes the wire size in bytes of time stamps in |ack|.
785 size_t GetAckFrameTimeStampSize(const QuicAckFrame& ack);
786
787 // Computes the wire size in bytes of the |ack| frame.
788 size_t GetAckFrameSize(const QuicAckFrame& ack,
789 QuicPacketNumberLength packet_number_length);
790 // Computes the wire-size, in bytes, of the |frame| ack frame, for IETF Quic.
791 size_t GetIetfAckFrameSize(const QuicAckFrame& frame);
792
793 // Computes the wire size in bytes of the |ack| frame.
794 size_t GetAckFrameSize(const QuicAckFrame& ack);
795
796 // Computes the wire size in bytes of the payload of |frame|.
797 size_t ComputeFrameLength(const QuicFrame& frame,
798 bool last_frame_in_packet,
799 QuicPacketNumberLength packet_number_length);
800
801 static bool AppendPacketNumber(QuicPacketNumberLength packet_number_length,
802 QuicPacketNumber packet_number,
803 QuicDataWriter* writer);
804 static bool AppendStreamId(size_t stream_id_length,
805 QuicStreamId stream_id,
806 QuicDataWriter* writer);
807 static bool AppendStreamOffset(size_t offset_length,
808 QuicStreamOffset offset,
809 QuicDataWriter* writer);
810
811 // Appends a single ACK block to |writer| and returns true if the block was
812 // successfully appended.
813 static bool AppendAckBlock(uint8_t gap,
814 QuicPacketNumberLength length_length,
815 uint64_t length,
816 QuicDataWriter* writer);
817
818 static uint8_t GetPacketNumberFlags(
819 QuicPacketNumberLength packet_number_length);
820
821 static AckFrameInfo GetAckFrameInfo(const QuicAckFrame& frame);
822
QUICHE teama6ef0a62019-03-07 20:34:33 -0500823 // The Append* methods attempt to write the provided header or frame using the
824 // |writer|, and return true if successful.
825
826 bool AppendAckFrameAndTypeByte(const QuicAckFrame& frame,
827 QuicDataWriter* builder);
828 bool AppendTimestampsToAckFrame(const QuicAckFrame& frame,
829 QuicDataWriter* writer);
830
831 // Append IETF format ACK frame.
832 //
833 // AppendIetfAckFrameAndTypeByte adds the IETF type byte and the body
834 // of the frame.
835 bool AppendIetfAckFrameAndTypeByte(const QuicAckFrame& frame,
836 QuicDataWriter* writer);
837
838 // Used by AppendIetfAckFrameAndTypeByte to figure out how many ack
839 // blocks can be included.
840 int CalculateIetfAckBlockCount(const QuicAckFrame& frame,
841 QuicDataWriter* writer,
842 size_t available_space);
843 bool AppendStopWaitingFrame(const QuicPacketHeader& header,
844 const QuicStopWaitingFrame& frame,
845 QuicDataWriter* builder);
846 bool AppendRstStreamFrame(const QuicRstStreamFrame& frame,
847 QuicDataWriter* builder);
848 bool AppendConnectionCloseFrame(const QuicConnectionCloseFrame& frame,
849 QuicDataWriter* builder);
850 bool AppendGoAwayFrame(const QuicGoAwayFrame& frame, QuicDataWriter* writer);
851 bool AppendWindowUpdateFrame(const QuicWindowUpdateFrame& frame,
852 QuicDataWriter* writer);
853 bool AppendBlockedFrame(const QuicBlockedFrame& frame,
854 QuicDataWriter* writer);
855 bool AppendPaddingFrame(const QuicPaddingFrame& frame,
856 QuicDataWriter* writer);
857 bool AppendMessageFrameAndTypeByte(const QuicMessageFrame& frame,
858 bool last_frame_in_packet,
859 QuicDataWriter* writer);
860
861 // IETF frame processing methods.
862 bool ProcessIetfStreamFrame(QuicDataReader* reader,
863 uint8_t frame_type,
864 QuicStreamFrame* frame);
865 bool ProcessIetfConnectionCloseFrame(QuicDataReader* reader,
fkastenholze9d71a82019-04-09 05:12:13 -0700866 QuicConnectionCloseType type,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500867 QuicConnectionCloseFrame* frame);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500868 bool ProcessPathChallengeFrame(QuicDataReader* reader,
869 QuicPathChallengeFrame* frame);
870 bool ProcessPathResponseFrame(QuicDataReader* reader,
871 QuicPathResponseFrame* frame);
872 bool ProcessIetfResetStreamFrame(QuicDataReader* reader,
873 QuicRstStreamFrame* frame);
874 bool ProcessStopSendingFrame(QuicDataReader* reader,
875 QuicStopSendingFrame* stop_sending_frame);
876 bool ProcessCryptoFrame(QuicDataReader* reader, QuicCryptoFrame* frame);
877
878 // IETF frame appending methods. All methods append the type byte as well.
879 bool AppendIetfStreamFrame(const QuicStreamFrame& frame,
880 bool last_frame_in_packet,
881 QuicDataWriter* writer);
882 bool AppendIetfConnectionCloseFrame(const QuicConnectionCloseFrame& frame,
883 QuicDataWriter* writer);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500884 bool AppendPathChallengeFrame(const QuicPathChallengeFrame& frame,
885 QuicDataWriter* writer);
886 bool AppendPathResponseFrame(const QuicPathResponseFrame& frame,
887 QuicDataWriter* writer);
888 bool AppendIetfResetStreamFrame(const QuicRstStreamFrame& frame,
889 QuicDataWriter* writer);
890 bool AppendStopSendingFrame(const QuicStopSendingFrame& stop_sending_frame,
891 QuicDataWriter* writer);
892
893 // Append/consume IETF-Format MAX_DATA and MAX_STREAM_DATA frames
894 bool AppendMaxDataFrame(const QuicWindowUpdateFrame& frame,
895 QuicDataWriter* writer);
896 bool AppendMaxStreamDataFrame(const QuicWindowUpdateFrame& frame,
897 QuicDataWriter* writer);
898 bool ProcessMaxDataFrame(QuicDataReader* reader,
899 QuicWindowUpdateFrame* frame);
900 bool ProcessMaxStreamDataFrame(QuicDataReader* reader,
901 QuicWindowUpdateFrame* frame);
902
fkastenholz3c4eabf2019-04-22 07:49:59 -0700903 bool AppendMaxStreamsFrame(const QuicMaxStreamsFrame& frame,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500904 QuicDataWriter* writer);
905 bool ProcessMaxStreamsFrame(QuicDataReader* reader,
fkastenholz3c4eabf2019-04-22 07:49:59 -0700906 QuicMaxStreamsFrame* frame,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500907 uint64_t frame_type);
908
909 bool AppendIetfBlockedFrame(const QuicBlockedFrame& frame,
910 QuicDataWriter* writer);
911 bool ProcessIetfBlockedFrame(QuicDataReader* reader, QuicBlockedFrame* frame);
912
913 bool AppendStreamBlockedFrame(const QuicBlockedFrame& frame,
914 QuicDataWriter* writer);
915 bool ProcessStreamBlockedFrame(QuicDataReader* reader,
916 QuicBlockedFrame* frame);
917
fkastenholz3c4eabf2019-04-22 07:49:59 -0700918 bool AppendStreamsBlockedFrame(const QuicStreamsBlockedFrame& frame,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500919 QuicDataWriter* writer);
920 bool ProcessStreamsBlockedFrame(QuicDataReader* reader,
fkastenholz3c4eabf2019-04-22 07:49:59 -0700921 QuicStreamsBlockedFrame* frame,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500922 uint64_t frame_type);
923
924 bool AppendNewConnectionIdFrame(const QuicNewConnectionIdFrame& frame,
925 QuicDataWriter* writer);
926 bool ProcessNewConnectionIdFrame(QuicDataReader* reader,
927 QuicNewConnectionIdFrame* frame);
928 bool AppendRetireConnectionIdFrame(const QuicRetireConnectionIdFrame& frame,
929 QuicDataWriter* writer);
930 bool ProcessRetireConnectionIdFrame(QuicDataReader* reader,
931 QuicRetireConnectionIdFrame* frame);
932
933 bool AppendNewTokenFrame(const QuicNewTokenFrame& frame,
934 QuicDataWriter* writer);
935 bool ProcessNewTokenFrame(QuicDataReader* reader, QuicNewTokenFrame* frame);
936
937 bool RaiseError(QuicErrorCode error);
938
939 // Returns true if |header| indicates a version negotiation packet.
940 bool IsVersionNegotiation(const QuicPacketHeader& header,
941 bool packet_has_ietf_packet_header) const;
942
943 // Calculates and returns type byte of stream frame.
944 uint8_t GetStreamFrameTypeByte(const QuicStreamFrame& frame,
945 bool last_frame_in_packet) const;
946 uint8_t GetIetfStreamFrameTypeByte(const QuicStreamFrame& frame,
947 bool last_frame_in_packet) const;
948
949 void set_error(QuicErrorCode error) { error_ = error; }
950
951 void set_detailed_error(const char* error) { detailed_error_ = error; }
952
vasilvvc48c8712019-03-11 13:38:16 -0700953 std::string detailed_error_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500954 QuicFramerVisitorInterface* visitor_;
955 QuicErrorCode error_;
956 // Updated by ProcessPacketHeader when it succeeds decrypting a larger packet.
957 QuicPacketNumber largest_packet_number_;
QUICHE team10b22a12019-03-21 15:31:42 -0700958 // Largest successfully decrypted packet number per packet number space. Only
959 // used when supports_multiple_packet_number_spaces_ is true.
960 QuicPacketNumber largest_decrypted_packet_numbers_[NUM_PACKET_NUMBER_SPACES];
QUICHE teama6ef0a62019-03-07 20:34:33 -0500961 // Updated by WritePacketHeader.
962 QuicConnectionId last_serialized_connection_id_;
963 // The last QUIC version label received.
fayangccbab732019-05-13 10:11:25 -0700964 // TODO(fayang): Remove this when deprecating
965 // quic_no_framer_object_in_dispatcher.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500966 QuicVersionLabel last_version_label_;
967 // Version of the protocol being used.
968 ParsedQuicVersion version_;
969 // This vector contains QUIC versions which we currently support.
970 // This should be ordered such that the highest supported version is the first
971 // element, with subsequent elements in descending order (versions can be
972 // skipped as necessary).
973 ParsedQuicVersionVector supported_versions_;
QUICHE team76086e42019-03-25 15:12:29 -0700974 // Decrypters used to decrypt packets during parsing.
975 std::unique_ptr<QuicDecrypter> decrypter_[NUM_ENCRYPTION_LEVELS];
976 // The encryption level of the primary decrypter to use in |decrypter_|.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500977 EncryptionLevel decrypter_level_;
QUICHE team76086e42019-03-25 15:12:29 -0700978 // The encryption level of the alternative decrypter to use in |decrypter_|.
979 // When set to NUM_ENCRYPTION_LEVELS, indicates that there is no alternative
QUICHE teama6ef0a62019-03-07 20:34:33 -0500980 // decrypter.
QUICHE team76086e42019-03-25 15:12:29 -0700981 EncryptionLevel alternative_decrypter_level_;
982 // |alternative_decrypter_latch_| is true if, when the decrypter at
983 // |alternative_decrypter_level_| successfully decrypts a packet, we should
984 // install it as the only decrypter.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500985 bool alternative_decrypter_latch_;
986 // Encrypters used to encrypt packets via EncryptPayload().
987 std::unique_ptr<QuicEncrypter> encrypter_[NUM_ENCRYPTION_LEVELS];
988 // Tracks if the framer is being used by the entity that received the
989 // connection or the entity that initiated it.
990 Perspective perspective_;
991 // If false, skip validation that the public flags are set to legal values.
992 bool validate_flags_;
993 // The diversification nonce from the last received packet.
994 DiversificationNonce last_nonce_;
995 // If true, send and process timestamps in the ACK frame.
996 bool process_timestamps_;
997 // The creation time of the connection, used to calculate timestamps.
998 QuicTime creation_time_;
999 // The last timestamp received if process_timestamps_ is true.
1000 QuicTime::Delta last_timestamp_;
1001
1002 // If this is a framer of a connection, this is the packet number of first
1003 // sending packet. If this is a framer of a framer of dispatcher, this is the
1004 // packet number of sent packets (for those which have packet number).
1005 const QuicPacketNumber first_sending_packet_number_;
1006
1007 // If not null, framer asks data_producer_ to write stream frame data. Not
1008 // owned. TODO(fayang): Consider add data producer to framer's constructor.
1009 QuicStreamFrameDataProducer* data_producer_;
1010
1011 // If true, framer infers packet header type (IETF/GQUIC) from version_.
1012 // Otherwise, framer infers packet header type from first byte of a received
1013 // packet.
1014 bool infer_packet_header_type_from_version_;
1015
1016 // IETF short headers contain a destination connection ID but do not
1017 // encode its length. This variable contains the length we expect to read.
1018 // This is also used to validate the long header connection ID lengths in
1019 // older versions of QUIC.
fayangccbab732019-05-13 10:11:25 -07001020 // TODO(fayang): Remove this when deprecating
1021 // quic_no_framer_object_in_dispatcher.
QUICHE team4d9d6292019-03-11 14:25:33 -07001022 uint8_t expected_connection_id_length_;
1023
1024 // When this is true, QuicFramer will change expected_connection_id_length_
1025 // to the received destination connection ID length of all IETF long headers.
fayangccbab732019-05-13 10:11:25 -07001026 // TODO(fayang): Remove this when deprecating
1027 // quic_no_framer_object_in_dispatcher.
QUICHE team4d9d6292019-03-11 14:25:33 -07001028 bool should_update_expected_connection_id_length_;
QUICHE team10b22a12019-03-21 15:31:42 -07001029
1030 // Indicates whether this framer supports multiple packet number spaces.
1031 bool supports_multiple_packet_number_spaces_;
nharper55fa6132019-05-07 19:37:21 -07001032
1033 // The length in bytes of the last packet number written to an IETF-framed
1034 // packet.
1035 size_t last_written_packet_number_length_;
QUICHE teama6ef0a62019-03-07 20:34:33 -05001036};
1037
1038} // namespace quic
1039
1040#endif // QUICHE_QUIC_CORE_QUIC_FRAMER_H_