Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / net / quic / quic_sent_entropy_manager.h
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.
7
8 #ifndef NET_QUIC_QUIC_SENT_ENTROPY_MANAGER_H_
9 #define NET_QUIC_QUIC_SENT_ENTROPY_MANAGER_H_
10
11 #include <deque>
12
13 #include "net/base/linked_hash_map.h"
14 #include "net/quic/quic_framer.h"
15 #include "net/quic/quic_protocol.h"
16
17 namespace net {
18
19 namespace test {
20 class QuicConnectionPeer;
21 }  // namespace test
22
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();
31
32   // Record |entropy_hash| for sent packet corresponding to |sequence_number|.
33   void RecordPacketEntropyHash(QuicPacketSequenceNumber sequence_number,
34                                QuicPacketEntropyHash entropy_hash);
35
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);
40
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);
47
48   // Removes unnecessary entries before |sequence_number|.
49   void ClearEntropyBefore(QuicPacketSequenceNumber sequence_number);
50
51  private:
52   friend class test::QuicConnectionPeer;
53
54   typedef std::deque<QuicPacketEntropyHash> SentEntropyMap;
55
56   struct CumulativeEntropy {
57     CumulativeEntropy() : sequence_number(0), entropy(0) {}
58
59     QuicPacketSequenceNumber sequence_number;
60     QuicPacketEntropyHash entropy;
61   };
62
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;
69
70   // Update the cumulative entropy to |sequence_number|.
71   void UpdateCumulativeEntropy(QuicPacketSequenceNumber sequence_number,
72                                CumulativeEntropy* cumulative) const;
73
74   // Maps sequence numbers to the sent entropy hash for the sequence number.
75   SentEntropyMap packets_entropy_;
76   QuicPacketSequenceNumber map_offset_;
77
78   // Cache the cumulative entropy for IsValidEntropy.
79   CumulativeEntropy last_valid_entropy_;
80
81   // Cache the cumulative entropy for the sequence number used by EntropyHash.
82   CumulativeEntropy last_cumulative_entropy_;
83
84   DISALLOW_COPY_AND_ASSIGN(QuicSentEntropyManager);
85 };
86
87 }  // namespace net
88
89 #endif  // NET_QUIC_QUIC_SENT_ENTROPY_MANAGER_H_