summaryrefslogtreecommitdiffstats
path: root/net/quic/quic_sent_entropy_manager.h
blob: 9ea900562ea0cbabfbab8a5e76c211f43e61f69f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Manages the packet entropy calculation for both sent and received packets
// for a connection.

#ifndef NET_QUIC_QUIC_SENT_ENTROPY_MANAGER_H_
#define NET_QUIC_QUIC_SENT_ENTROPY_MANAGER_H_

#include <deque>

#include "base/macros.h"
#include "net/base/linked_hash_map.h"
#include "net/quic/quic_framer.h"
#include "net/quic/quic_protocol.h"

namespace net {

namespace test {
class QuicConnectionPeer;
}  // namespace test

// Records all sent packets by a connection to track the cumulative entropy of
// sent packets.  It is used by the connection to validate an ack
// frame sent by the peer as a preventive measure against the optimistic ack
// attack.
class NET_EXPORT_PRIVATE QuicSentEntropyManager {
 public:
  QuicSentEntropyManager();
  virtual ~QuicSentEntropyManager();

  // Record |entropy_hash| for sent packet corresponding to |packet_number|.
  void RecordPacketEntropyHash(QuicPacketNumber packet_number,
                               QuicPacketEntropyHash entropy_hash);

  // Retrieves the cumulative entropy up to |packet_number|.
  // Must always be called with a monotonically increasing |packet_number|.
  QuicPacketEntropyHash GetCumulativeEntropy(QuicPacketNumber packet_number);

  // Returns true if |entropy_hash| matches the expected sent entropy hash
  // up to |largest_observed| removing packet numbers from |missing_packets|.
  // Must always be called with a monotonically increasing |largest_observed|.
  bool IsValidEntropy(QuicPacketNumber largest_observed,
                      const PacketNumberQueue& missing_packets,
                      QuicPacketEntropyHash entropy_hash);

  // Removes unnecessary entries before |packet_number|.
  void ClearEntropyBefore(QuicPacketNumber packet_number);

 private:
  friend class test::QuicConnectionPeer;

  typedef std::deque<QuicPacketEntropyHash> SentEntropyMap;

  struct CumulativeEntropy {
    CumulativeEntropy() : packet_number(0), entropy(0) {}

    QuicPacketNumber packet_number;
    QuicPacketEntropyHash entropy;
  };

  // Convenience methods to get the largest and smallest packets with entropies.
  QuicPacketNumber GetLargestPacketWithEntropy() const;
  QuicPacketNumber GetSmallestPacketWithEntropy() const;
  // Convenience method to get the entropy hash for |packet_number|.
  QuicPacketEntropyHash GetPacketEntropy(QuicPacketNumber packet_number) const;

  // Update the cumulative entropy to |packet_number|.
  void UpdateCumulativeEntropy(QuicPacketNumber packet_number,
                               CumulativeEntropy* cumulative) const;

  // Maps packet numbers to the sent entropy hash for the packet number.
  SentEntropyMap packets_entropy_;
  QuicPacketNumber map_offset_;

  // Cache the cumulative entropy for IsValidEntropy.
  CumulativeEntropy last_valid_entropy_;

  // Cache the cumulative entropy for the packet number used by EntropyHash.
  CumulativeEntropy last_cumulative_entropy_;

  DISALLOW_COPY_AND_ASSIGN(QuicSentEntropyManager);
};

}  // namespace net

#endif  // NET_QUIC_QUIC_SENT_ENTROPY_MANAGER_H_