OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_QUIC_QUIC_PROTOCOL_H_ | 5 #ifndef NET_QUIC_CORE_QUIC_FRAMES_H_ |
6 #define NET_QUIC_QUIC_PROTOCOL_H_ | 6 #define NET_QUIC_CORE_QUIC_FRAMES_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <cstdint> |
9 #include <stdint.h> | |
10 | |
11 #include <array> | |
12 #include <limits> | 9 #include <limits> |
13 #include <list> | |
14 #include <map> | |
15 #include <memory> | 10 #include <memory> |
16 #include <ostream> | |
17 #include <set> | |
18 #include <string> | 11 #include <string> |
19 #include <utility> | |
20 #include <vector> | 12 #include <vector> |
21 | 13 |
22 #include "base/logging.h" | 14 #include "base/logging.h" |
23 #include "base/macros.h" | 15 #include "base/macros.h" |
24 #include "base/memory/ref_counted.h" | |
25 #include "base/strings/string_piece.h" | 16 #include "base/strings/string_piece.h" |
26 #include "net/base/int128.h" | |
27 #include "net/base/iovec.h" | |
28 #include "net/base/net_export.h" | |
29 #include "net/quic/core/interval_set.h" | 17 #include "net/quic/core/interval_set.h" |
30 #include "net/quic/core/quic_bandwidth.h" | |
31 #include "net/quic/core/quic_buffer_allocator.h" | 18 #include "net/quic/core/quic_buffer_allocator.h" |
32 #include "net/quic/core/quic_constants.h" | 19 #include "net/quic/core/quic_constants.h" |
33 #include "net/quic/core/quic_error_codes.h" | 20 #include "net/quic/core/quic_error_codes.h" |
34 #include "net/quic/core/quic_time.h" | 21 #include "net/quic/core/quic_time.h" |
35 #include "net/quic/core/quic_types.h" | 22 #include "net/quic/core/quic_types.h" |
36 #include "net/quic/core/quic_versions.h" | 23 #include "net/quic/core/quic_versions.h" |
37 #include "net/quic/platform/api/quic_socket_address.h" | |
38 | 24 |
39 namespace net { | 25 namespace net { |
40 | 26 |
41 class QuicPacket; | |
42 struct QuicPacketHeader; | |
43 | |
44 // Size in bytes of the data packet header. | |
45 NET_EXPORT_PRIVATE size_t GetPacketHeaderSize(QuicVersion version, | |
46 const QuicPacketHeader& header); | |
47 | |
48 NET_EXPORT_PRIVATE size_t | |
49 GetPacketHeaderSize(QuicVersion version, | |
50 QuicConnectionIdLength connection_id_length, | |
51 bool include_version, | |
52 bool include_path_id, | |
53 bool include_diversification_nonce, | |
54 QuicPacketNumberLength packet_number_length); | |
55 | |
56 // Index of the first byte in a QUIC packet of encrypted data. | |
57 NET_EXPORT_PRIVATE size_t | |
58 GetStartOfEncryptedData(QuicVersion version, const QuicPacketHeader& header); | |
59 | |
60 NET_EXPORT_PRIVATE size_t | |
61 GetStartOfEncryptedData(QuicVersion version, | |
62 QuicConnectionIdLength connection_id_length, | |
63 bool include_version, | |
64 bool include_path_id, | |
65 bool include_diversification_nonce, | |
66 QuicPacketNumberLength packet_number_length); | |
67 | |
68 struct NET_EXPORT_PRIVATE QuicPacketPublicHeader { | |
69 QuicPacketPublicHeader(); | |
70 explicit QuicPacketPublicHeader(const QuicPacketPublicHeader& other); | |
71 ~QuicPacketPublicHeader(); | |
72 | |
73 // Universal header. All QuicPacket headers will have a connection_id and | |
74 // public flags. | |
75 QuicConnectionId connection_id; | |
76 QuicConnectionIdLength connection_id_length; | |
77 bool multipath_flag; | |
78 bool reset_flag; | |
79 bool version_flag; | |
80 QuicPacketNumberLength packet_number_length; | |
81 QuicVersionVector versions; | |
82 // nonce contains an optional, 32-byte nonce value. If not included in the | |
83 // packet, |nonce| will be empty. | |
84 DiversificationNonce* nonce; | |
85 }; | |
86 | |
87 // Header for Data packets. | |
88 struct NET_EXPORT_PRIVATE QuicPacketHeader { | |
89 QuicPacketHeader(); | |
90 explicit QuicPacketHeader(const QuicPacketPublicHeader& header); | |
91 QuicPacketHeader(const QuicPacketHeader& other); | |
92 | |
93 NET_EXPORT_PRIVATE friend std::ostream& operator<<(std::ostream& os, | |
94 const QuicPacketHeader& s); | |
95 | |
96 QuicPacketPublicHeader public_header; | |
97 QuicPacketNumber packet_number; | |
98 QuicPathId path_id; | |
99 }; | |
100 | |
101 struct NET_EXPORT_PRIVATE QuicPublicResetPacket { | |
102 QuicPublicResetPacket(); | |
103 explicit QuicPublicResetPacket(const QuicPacketPublicHeader& header); | |
104 | |
105 QuicPacketPublicHeader public_header; | |
106 QuicPublicResetNonceProof nonce_proof; | |
107 // TODO(fayang): remove rejected_packet_number when deprecating | |
108 // FLAGS_quic_remove_packet_number_from_public_reset. | |
109 QuicPacketNumber rejected_packet_number; | |
110 QuicSocketAddress client_address; | |
111 }; | |
112 | |
113 typedef QuicPacketPublicHeader QuicVersionNegotiationPacket; | |
114 | |
115 // A padding frame contains no payload. | 27 // A padding frame contains no payload. |
116 struct NET_EXPORT_PRIVATE QuicPaddingFrame { | 28 struct NET_EXPORT_PRIVATE QuicPaddingFrame { |
117 QuicPaddingFrame() : num_padding_bytes(-1) {} | 29 QuicPaddingFrame() : num_padding_bytes(-1) {} |
118 explicit QuicPaddingFrame(int num_padding_bytes) | 30 explicit QuicPaddingFrame(int num_padding_bytes) |
119 : num_padding_bytes(num_padding_bytes) {} | 31 : num_padding_bytes(num_padding_bytes) {} |
120 | 32 |
121 NET_EXPORT_PRIVATE friend std::ostream& operator<<(std::ostream& os, | 33 NET_EXPORT_PRIVATE friend std::ostream& operator<<(std::ostream& os, |
122 const QuicPaddingFrame& s); | 34 const QuicPaddingFrame& s); |
123 | 35 |
124 // -1: full padding to the end of a max-sized packet | 36 // -1: full padding to the end of a max-sized packet |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 // in a sliding window fashion, where smaller old packet numbers are removed and | 126 // in a sliding window fashion, where smaller old packet numbers are removed and |
215 // larger new packet numbers are added, with the occasional random access. | 127 // larger new packet numbers are added, with the occasional random access. |
216 class NET_EXPORT_PRIVATE PacketNumberQueue { | 128 class NET_EXPORT_PRIVATE PacketNumberQueue { |
217 public: | 129 public: |
218 using const_iterator = IntervalSet<QuicPacketNumber>::const_iterator; | 130 using const_iterator = IntervalSet<QuicPacketNumber>::const_iterator; |
219 using const_reverse_iterator = | 131 using const_reverse_iterator = |
220 IntervalSet<QuicPacketNumber>::const_reverse_iterator; | 132 IntervalSet<QuicPacketNumber>::const_reverse_iterator; |
221 | 133 |
222 PacketNumberQueue(); | 134 PacketNumberQueue(); |
223 PacketNumberQueue(const PacketNumberQueue& other); | 135 PacketNumberQueue(const PacketNumberQueue& other); |
224 // TODO(rtenneti): on windows RValue reference gives errors. | 136 PacketNumberQueue(PacketNumberQueue&& other); |
225 // PacketNumberQueue(PacketNumberQueue&& other); | |
226 ~PacketNumberQueue(); | 137 ~PacketNumberQueue(); |
227 | 138 |
228 PacketNumberQueue& operator=(const PacketNumberQueue& other); | 139 PacketNumberQueue& operator=(const PacketNumberQueue& other); |
229 // PacketNumberQueue& operator=(PacketNumberQueue&& other); | 140 PacketNumberQueue& operator=(PacketNumberQueue&& other); |
230 | 141 |
231 // Adds |packet_number| to the set of packets in the queue. | 142 // Adds |packet_number| to the set of packets in the queue. |
232 void Add(QuicPacketNumber packet_number); | 143 void Add(QuicPacketNumber packet_number); |
233 | 144 |
234 // Adds packets between [lower, higher) to the set of packets in the queue. It | 145 // Adds packets between [lower, higher) to the set of packets in the queue. It |
235 // is undefined behavior to call this with |higher| < |lower|. | 146 // is undefined behavior to call this with |higher| < |lower|. |
236 void Add(QuicPacketNumber lower, QuicPacketNumber higher); | 147 void Add(QuicPacketNumber lower, QuicPacketNumber higher); |
237 | 148 |
238 // Removes |packet_number| from the set of packets in the queue. | 149 // Removes |packet_number| from the set of packets in the queue. |
239 void Remove(QuicPacketNumber packet_number); | 150 void Remove(QuicPacketNumber packet_number); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 // Set of packets. | 223 // Set of packets. |
313 PacketNumberQueue packets; | 224 PacketNumberQueue packets; |
314 | 225 |
315 // Path which this ack belongs to. | 226 // Path which this ack belongs to. |
316 QuicPathId path_id; | 227 QuicPathId path_id; |
317 }; | 228 }; |
318 | 229 |
319 // True if the packet number is greater than largest_observed or is listed | 230 // True if the packet number is greater than largest_observed or is listed |
320 // as missing. | 231 // as missing. |
321 // Always returns false for packet numbers less than least_unacked. | 232 // Always returns false for packet numbers less than least_unacked. |
322 bool NET_EXPORT_PRIVATE | 233 NET_EXPORT_PRIVATE bool IsAwaitingPacket( |
323 IsAwaitingPacket(const QuicAckFrame& ack_frame, | 234 const QuicAckFrame& ack_frame, |
324 QuicPacketNumber packet_number, | 235 QuicPacketNumber packet_number, |
325 QuicPacketNumber peer_least_packet_awaiting_ack); | 236 QuicPacketNumber peer_least_packet_awaiting_ack); |
326 | 237 |
327 struct NET_EXPORT_PRIVATE QuicRstStreamFrame { | 238 struct NET_EXPORT_PRIVATE QuicRstStreamFrame { |
328 QuicRstStreamFrame(); | 239 QuicRstStreamFrame(); |
329 QuicRstStreamFrame(QuicStreamId stream_id, | 240 QuicRstStreamFrame(QuicStreamId stream_id, |
330 QuicRstStreamErrorCode error_code, | 241 QuicRstStreamErrorCode error_code, |
331 QuicStreamOffset bytes_written); | 242 QuicStreamOffset bytes_written); |
332 | 243 |
333 NET_EXPORT_PRIVATE friend std::ostream& operator<<( | 244 NET_EXPORT_PRIVATE friend std::ostream& operator<<( |
334 std::ostream& os, | 245 std::ostream& os, |
335 const QuicRstStreamFrame& r); | 246 const QuicRstStreamFrame& r); |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
468 | 379 |
469 typedef std::vector<QuicFrame> QuicFrames; | 380 typedef std::vector<QuicFrame> QuicFrames; |
470 | 381 |
471 // Deletes all the sub-frames contained in |frames|. | 382 // Deletes all the sub-frames contained in |frames|. |
472 NET_EXPORT_PRIVATE void DeleteFrames(QuicFrames* frames); | 383 NET_EXPORT_PRIVATE void DeleteFrames(QuicFrames* frames); |
473 | 384 |
474 // Deletes all the QuicStreamFrames for the specified |stream_id|. | 385 // Deletes all the QuicStreamFrames for the specified |stream_id|. |
475 NET_EXPORT_PRIVATE void RemoveFramesForStream(QuicFrames* frames, | 386 NET_EXPORT_PRIVATE void RemoveFramesForStream(QuicFrames* frames, |
476 QuicStreamId stream_id); | 387 QuicStreamId stream_id); |
477 | 388 |
478 class NET_EXPORT_PRIVATE QuicData { | |
479 public: | |
480 QuicData(const char* buffer, size_t length); | |
481 QuicData(const char* buffer, size_t length, bool owns_buffer); | |
482 virtual ~QuicData(); | |
483 | |
484 base::StringPiece AsStringPiece() const { | |
485 return base::StringPiece(data(), length()); | |
486 } | |
487 | |
488 const char* data() const { return buffer_; } | |
489 size_t length() const { return length_; } | |
490 bool owns_buffer() const { return owns_buffer_; } | |
491 | |
492 private: | |
493 const char* buffer_; | |
494 size_t length_; | |
495 bool owns_buffer_; | |
496 | |
497 DISALLOW_COPY_AND_ASSIGN(QuicData); | |
498 }; | |
499 | |
500 class NET_EXPORT_PRIVATE QuicPacket : public QuicData { | |
501 public: | |
502 // TODO(fayang): 4 fields from public header are passed in as arguments. | |
503 // Consider to add a convenience method which directly accepts the entire | |
504 // public header. | |
505 QuicPacket(char* buffer, | |
506 size_t length, | |
507 bool owns_buffer, | |
508 QuicConnectionIdLength connection_id_length, | |
509 bool includes_version, | |
510 bool includes_path_id, | |
511 bool includes_diversification_nonce, | |
512 QuicPacketNumberLength packet_number_length); | |
513 | |
514 base::StringPiece AssociatedData(QuicVersion version) const; | |
515 base::StringPiece Plaintext(QuicVersion version) const; | |
516 | |
517 char* mutable_data() { return buffer_; } | |
518 | |
519 private: | |
520 char* buffer_; | |
521 const QuicConnectionIdLength connection_id_length_; | |
522 const bool includes_version_; | |
523 const bool includes_path_id_; | |
524 const bool includes_diversification_nonce_; | |
525 const QuicPacketNumberLength packet_number_length_; | |
526 | |
527 DISALLOW_COPY_AND_ASSIGN(QuicPacket); | |
528 }; | |
529 | |
530 class NET_EXPORT_PRIVATE QuicEncryptedPacket : public QuicData { | |
531 public: | |
532 QuicEncryptedPacket(const char* buffer, size_t length); | |
533 QuicEncryptedPacket(const char* buffer, size_t length, bool owns_buffer); | |
534 | |
535 // Clones the packet into a new packet which owns the buffer. | |
536 std::unique_ptr<QuicEncryptedPacket> Clone() const; | |
537 | |
538 // By default, gtest prints the raw bytes of an object. The bool data | |
539 // member (in the base class QuicData) causes this object to have padding | |
540 // bytes, which causes the default gtest object printer to read | |
541 // uninitialize memory. So we need to teach gtest how to print this object. | |
542 NET_EXPORT_PRIVATE friend std::ostream& operator<<( | |
543 std::ostream& os, | |
544 const QuicEncryptedPacket& s); | |
545 | |
546 private: | |
547 DISALLOW_COPY_AND_ASSIGN(QuicEncryptedPacket); | |
548 }; | |
549 | |
550 // A received encrypted QUIC packet, with a recorded time of receipt. | |
551 class NET_EXPORT_PRIVATE QuicReceivedPacket : public QuicEncryptedPacket { | |
552 public: | |
553 QuicReceivedPacket(const char* buffer, size_t length, QuicTime receipt_time); | |
554 QuicReceivedPacket(const char* buffer, | |
555 size_t length, | |
556 QuicTime receipt_time, | |
557 bool owns_buffer); | |
558 QuicReceivedPacket(const char* buffer, | |
559 size_t length, | |
560 QuicTime receipt_time, | |
561 bool owns_buffer, | |
562 int ttl, | |
563 bool ttl_valid); | |
564 | |
565 // Clones the packet into a new packet which owns the buffer. | |
566 std::unique_ptr<QuicReceivedPacket> Clone() const; | |
567 | |
568 // Returns the time at which the packet was received. | |
569 QuicTime receipt_time() const { return receipt_time_; } | |
570 | |
571 // This is the TTL of the packet, assuming ttl_vaild_ is true. | |
572 int ttl() const { return ttl_; } | |
573 | |
574 // By default, gtest prints the raw bytes of an object. The bool data | |
575 // member (in the base class QuicData) causes this object to have padding | |
576 // bytes, which causes the default gtest object printer to read | |
577 // uninitialize memory. So we need to teach gtest how to print this object. | |
578 NET_EXPORT_PRIVATE friend std::ostream& operator<<( | |
579 std::ostream& os, | |
580 const QuicReceivedPacket& s); | |
581 | |
582 private: | |
583 const QuicTime receipt_time_; | |
584 int ttl_; | |
585 | |
586 DISALLOW_COPY_AND_ASSIGN(QuicReceivedPacket); | |
587 }; | |
588 | |
589 // Pure virtual class to listen for packet acknowledgements. | |
590 class NET_EXPORT_PRIVATE QuicAckListenerInterface | |
591 : public base::RefCounted<QuicAckListenerInterface> { | |
592 public: | |
593 QuicAckListenerInterface() {} | |
594 | |
595 // Called when a packet is acked. Called once per packet. | |
596 // |acked_bytes| is the number of data bytes acked. | |
597 virtual void OnPacketAcked(int acked_bytes, | |
598 QuicTime::Delta ack_delay_time) = 0; | |
599 | |
600 // Called when a packet is retransmitted. Called once per packet. | |
601 // |retransmitted_bytes| is the number of data bytes retransmitted. | |
602 virtual void OnPacketRetransmitted(int retransmitted_bytes) = 0; | |
603 | |
604 protected: | |
605 friend class base::RefCounted<QuicAckListenerInterface>; | |
606 | |
607 // Delegates are ref counted. | |
608 virtual ~QuicAckListenerInterface() {} | |
609 }; | |
610 | |
611 struct NET_EXPORT_PRIVATE AckListenerWrapper { | |
612 AckListenerWrapper(QuicAckListenerInterface* listener, | |
613 QuicPacketLength data_length); | |
614 AckListenerWrapper(const AckListenerWrapper& other); | |
615 ~AckListenerWrapper(); | |
616 | |
617 scoped_refptr<QuicAckListenerInterface> ack_listener; | |
618 QuicPacketLength length; | |
619 }; | |
620 | |
621 struct NET_EXPORT_PRIVATE SerializedPacket { | |
622 SerializedPacket(QuicPathId path_id, | |
623 QuicPacketNumber packet_number, | |
624 QuicPacketNumberLength packet_number_length, | |
625 const char* encrypted_buffer, | |
626 QuicPacketLength encrypted_length, | |
627 bool has_ack, | |
628 bool has_stop_waiting); | |
629 SerializedPacket(const SerializedPacket& other); | |
630 ~SerializedPacket(); | |
631 | |
632 // Not owned. | |
633 const char* encrypted_buffer; | |
634 QuicPacketLength encrypted_length; | |
635 QuicFrames retransmittable_frames; | |
636 IsHandshake has_crypto_handshake; | |
637 // -1: full padding to the end of a max-sized packet | |
638 // 0: no padding | |
639 // otherwise: only pad up to num_padding_bytes bytes | |
640 int16_t num_padding_bytes; | |
641 QuicPathId path_id; | |
642 QuicPacketNumber packet_number; | |
643 QuicPacketNumberLength packet_number_length; | |
644 EncryptionLevel encryption_level; | |
645 bool has_ack; | |
646 bool has_stop_waiting; | |
647 TransmissionType transmission_type; | |
648 QuicPathId original_path_id; | |
649 QuicPacketNumber original_packet_number; | |
650 | |
651 // Optional notifiers which will be informed when this packet has been ACKed. | |
652 std::list<AckListenerWrapper> listeners; | |
653 }; | |
654 | |
655 // Deletes and clears all the frames and the packet from serialized packet. | |
656 NET_EXPORT_PRIVATE void ClearSerializedPacket( | |
657 SerializedPacket* serialized_packet); | |
658 | |
659 // Allocates a new char[] of size |packet.encrypted_length| and copies in | |
660 // |packet.encrypted_buffer|. | |
661 NET_EXPORT_PRIVATE char* CopyBuffer(const SerializedPacket& packet); | |
662 | |
663 struct NET_EXPORT_PRIVATE TransmissionInfo { | |
664 // Used by STL when assigning into a map. | |
665 TransmissionInfo(); | |
666 | |
667 // Constructs a Transmission with a new all_transmissions set | |
668 // containing |packet_number|. | |
669 TransmissionInfo(EncryptionLevel level, | |
670 QuicPacketNumberLength packet_number_length, | |
671 TransmissionType transmission_type, | |
672 QuicTime sent_time, | |
673 QuicPacketLength bytes_sent, | |
674 bool has_crypto_handshake, | |
675 int num_padding_bytes); | |
676 | |
677 TransmissionInfo(const TransmissionInfo& other); | |
678 | |
679 ~TransmissionInfo(); | |
680 | |
681 QuicFrames retransmittable_frames; | |
682 EncryptionLevel encryption_level; | |
683 QuicPacketNumberLength packet_number_length; | |
684 QuicPacketLength bytes_sent; | |
685 QuicTime sent_time; | |
686 // Reason why this packet was transmitted. | |
687 TransmissionType transmission_type; | |
688 // In flight packets have not been abandoned or lost. | |
689 bool in_flight; | |
690 // True if the packet can never be acked, so it can be removed. Occurs when | |
691 // a packet is never sent, after it is acknowledged once, or if it's a crypto | |
692 // packet we never expect to receive an ack for. | |
693 bool is_unackable; | |
694 // True if the packet contains stream data from the crypto stream. | |
695 bool has_crypto_handshake; | |
696 // Non-zero if the packet needs padding if it's retransmitted. | |
697 int16_t num_padding_bytes; | |
698 // Stores the packet number of the next retransmission of this packet. | |
699 // Zero if the packet has not been retransmitted. | |
700 QuicPacketNumber retransmission; | |
701 // Non-empty if there is a std::listener for this packet. | |
702 std::list<AckListenerWrapper> ack_listeners; | |
703 }; | |
704 | |
705 // Struct to store the pending retransmission information. | |
706 struct PendingRetransmission { | |
707 PendingRetransmission(QuicPathId path_id, | |
708 QuicPacketNumber packet_number, | |
709 TransmissionType transmission_type, | |
710 const QuicFrames& retransmittable_frames, | |
711 bool has_crypto_handshake, | |
712 int num_padding_bytes, | |
713 EncryptionLevel encryption_level, | |
714 QuicPacketNumberLength packet_number_length) | |
715 : packet_number(packet_number), | |
716 retransmittable_frames(retransmittable_frames), | |
717 transmission_type(transmission_type), | |
718 path_id(path_id), | |
719 has_crypto_handshake(has_crypto_handshake), | |
720 num_padding_bytes(num_padding_bytes), | |
721 encryption_level(encryption_level), | |
722 packet_number_length(packet_number_length) {} | |
723 | |
724 QuicPacketNumber packet_number; | |
725 const QuicFrames& retransmittable_frames; | |
726 TransmissionType transmission_type; | |
727 QuicPathId path_id; | |
728 bool has_crypto_handshake; | |
729 int num_padding_bytes; | |
730 EncryptionLevel encryption_level; | |
731 QuicPacketNumberLength packet_number_length; | |
732 }; | |
733 | |
734 } // namespace net | 389 } // namespace net |
735 | 390 |
736 #endif // NET_QUIC_QUIC_PROTOCOL_H_ | 391 #endif // NET_QUIC_CORE_QUIC_FRAMES_H_ |
OLD | NEW |