Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / net / quic / quic_sent_entropy_manager.h
blobfd1de76752ad34a7174b5e5839b0198d45244e57
1 // Copyright 2013 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 // Manages the packet entropy calculation for both sent and received packets
6 // for a connection.
8 #ifndef NET_QUIC_QUIC_SENT_ENTROPY_MANAGER_H_
9 #define NET_QUIC_QUIC_SENT_ENTROPY_MANAGER_H_
11 #include <deque>
13 #include "net/base/linked_hash_map.h"
14 #include "net/quic/quic_framer.h"
15 #include "net/quic/quic_protocol.h"
17 namespace net {
19 namespace test {
20 class QuicConnectionPeer;
21 } // namespace test
23 // Records all sent packets by a connection to track the cumulative entropy of
24 // sent packets. It is used by the connection to validate an ack
25 // frame sent by the peer as a preventive measure against the optimistic ack
26 // attack.
27 class NET_EXPORT_PRIVATE QuicSentEntropyManager {
28 public:
29 QuicSentEntropyManager();
30 virtual ~QuicSentEntropyManager();
32 // Record |entropy_hash| for sent packet corresponding to |packet_number|.
33 void RecordPacketEntropyHash(QuicPacketNumber packet_number,
34 QuicPacketEntropyHash entropy_hash);
36 // Retrieves the cumulative entropy up to |packet_number|.
37 // Must always be called with a monotonically increasing |packet_number|.
38 QuicPacketEntropyHash GetCumulativeEntropy(QuicPacketNumber packet_number);
40 // Returns true if |entropy_hash| matches the expected sent entropy hash
41 // up to |largest_observed| removing packet numbers from |missing_packets|.
42 // Must always be called with a monotonically increasing |largest_observed|.
43 bool IsValidEntropy(QuicPacketNumber largest_observed,
44 const PacketNumberSet& missing_packets,
45 QuicPacketEntropyHash entropy_hash);
47 // Removes unnecessary entries before |packet_number|.
48 void ClearEntropyBefore(QuicPacketNumber packet_number);
50 private:
51 friend class test::QuicConnectionPeer;
53 typedef std::deque<QuicPacketEntropyHash> SentEntropyMap;
55 struct CumulativeEntropy {
56 CumulativeEntropy() : packet_number(0), entropy(0) {}
58 QuicPacketNumber packet_number;
59 QuicPacketEntropyHash entropy;
62 // Convenience methods to get the largest and smallest packets with entropies.
63 QuicPacketNumber GetLargestPacketWithEntropy() const;
64 QuicPacketNumber GetSmallestPacketWithEntropy() const;
65 // Convenience method to get the entropy hash for |packet_number|.
66 QuicPacketEntropyHash GetPacketEntropy(QuicPacketNumber packet_number) const;
68 // Update the cumulative entropy to |packet_number|.
69 void UpdateCumulativeEntropy(QuicPacketNumber packet_number,
70 CumulativeEntropy* cumulative) const;
72 // Maps packet numbers to the sent entropy hash for the packet number.
73 SentEntropyMap packets_entropy_;
74 QuicPacketNumber map_offset_;
76 // Cache the cumulative entropy for IsValidEntropy.
77 CumulativeEntropy last_valid_entropy_;
79 // Cache the cumulative entropy for the packet number used by EntropyHash.
80 CumulativeEntropy last_cumulative_entropy_;
82 DISALLOW_COPY_AND_ASSIGN(QuicSentEntropyManager);
85 } // namespace net
87 #endif // NET_QUIC_QUIC_SENT_ENTROPY_MANAGER_H_