Roll WebRTC 9745:9761, Libjingle 9742:9761
[chromium-blink-merge.git] / net / quic / quic_sent_entropy_manager.h
blob9173d9518f390e46a7bc79e430a33635eecd2e3e
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 |sequence_number|.
33 void RecordPacketEntropyHash(QuicPacketSequenceNumber sequence_number,
34 QuicPacketEntropyHash entropy_hash);
36 // Retrieves the cumulative entropy up to |sequence_number|.
37 // Must always be called with a monotonically increasing |sequence_number|.
38 QuicPacketEntropyHash GetCumulativeEntropy(
39 QuicPacketSequenceNumber sequence_number);
41 // Returns true if |entropy_hash| matches the expected sent entropy hash
42 // up to |largest_observed| removing sequence numbers from |missing_packets|.
43 // Must always be called with a monotonically increasing |largest_observed|.
44 bool IsValidEntropy(QuicPacketSequenceNumber largest_observed,
45 const SequenceNumberSet& missing_packets,
46 QuicPacketEntropyHash entropy_hash);
48 // Removes unnecessary entries before |sequence_number|.
49 void ClearEntropyBefore(QuicPacketSequenceNumber sequence_number);
51 private:
52 friend class test::QuicConnectionPeer;
54 typedef std::deque<QuicPacketEntropyHash> SentEntropyMap;
56 struct CumulativeEntropy {
57 CumulativeEntropy() : sequence_number(0), entropy(0) {}
59 QuicPacketSequenceNumber sequence_number;
60 QuicPacketEntropyHash entropy;
63 // Convenience methods to get the largest and smallest packets with entropies.
64 QuicPacketSequenceNumber GetLargestPacketWithEntropy() const;
65 QuicPacketSequenceNumber GetSmallestPacketWithEntropy() const;
66 // Convenience method to get the entropy hash for |sequence_number|.
67 QuicPacketEntropyHash GetPacketEntropy(
68 QuicPacketSequenceNumber sequence_number) const;
70 // Update the cumulative entropy to |sequence_number|.
71 void UpdateCumulativeEntropy(QuicPacketSequenceNumber sequence_number,
72 CumulativeEntropy* cumulative) const;
74 // Maps sequence numbers to the sent entropy hash for the sequence number.
75 SentEntropyMap packets_entropy_;
76 QuicPacketSequenceNumber map_offset_;
78 // Cache the cumulative entropy for IsValidEntropy.
79 CumulativeEntropy last_valid_entropy_;
81 // Cache the cumulative entropy for the sequence number used by EntropyHash.
82 CumulativeEntropy last_cumulative_entropy_;
84 DISALLOW_COPY_AND_ASSIGN(QuicSentEntropyManager);
87 } // namespace net
89 #endif // NET_QUIC_QUIC_SENT_ENTROPY_MANAGER_H_