OLD | NEW |
| (Empty) |
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 "base/macros.h" | |
14 #include "net/base/linked_hash_map.h" | |
15 #include "net/base/net_export.h" | |
16 #include "net/quic/core/quic_framer.h" | |
17 #include "net/quic/core/quic_protocol.h" | |
18 | |
19 namespace net { | |
20 | |
21 namespace test { | |
22 class QuicConnectionPeer; | |
23 } // namespace test | |
24 | |
25 // Records all sent packets by a connection to track the cumulative entropy of | |
26 // sent packets. It is used by the connection to validate an ack | |
27 // frame sent by the peer as a preventive measure against the optimistic ack | |
28 // attack. | |
29 class NET_EXPORT_PRIVATE QuicSentEntropyManager { | |
30 public: | |
31 QuicSentEntropyManager(); | |
32 virtual ~QuicSentEntropyManager(); | |
33 | |
34 // Record |entropy_hash| for sent packet corresponding to |packet_number|. | |
35 void RecordPacketEntropyHash(QuicPacketNumber packet_number, | |
36 QuicPacketEntropyHash entropy_hash); | |
37 | |
38 // Retrieves the cumulative entropy up to |packet_number|. | |
39 // Must always be called with a monotonically increasing |packet_number|. | |
40 QuicPacketEntropyHash GetCumulativeEntropy(QuicPacketNumber packet_number); | |
41 | |
42 // Returns true if |entropy_hash| matches the expected sent entropy hash | |
43 // up to |largest_observed| removing packet numbers from |missing_packets|. | |
44 // Must always be called with a monotonically increasing |largest_observed|. | |
45 bool IsValidEntropy(QuicPacketNumber largest_observed, | |
46 const PacketNumberQueue& missing_packets, | |
47 QuicPacketEntropyHash entropy_hash); | |
48 | |
49 // Removes unnecessary entries before |packet_number|. | |
50 void ClearEntropyBefore(QuicPacketNumber packet_number); | |
51 | |
52 private: | |
53 friend class test::QuicConnectionPeer; | |
54 | |
55 typedef std::deque<QuicPacketEntropyHash> SentEntropyMap; | |
56 | |
57 struct CumulativeEntropy { | |
58 CumulativeEntropy() : packet_number(0), entropy(0) {} | |
59 | |
60 QuicPacketNumber packet_number; | |
61 QuicPacketEntropyHash entropy; | |
62 }; | |
63 | |
64 // Convenience methods to get the largest and smallest packets with entropies. | |
65 QuicPacketNumber GetLargestPacketWithEntropy() const; | |
66 QuicPacketNumber GetSmallestPacketWithEntropy() const; | |
67 // Convenience method to get the entropy hash for |packet_number|. | |
68 QuicPacketEntropyHash GetPacketEntropy(QuicPacketNumber packet_number) const; | |
69 | |
70 // Update the cumulative entropy to |packet_number|. | |
71 void UpdateCumulativeEntropy(QuicPacketNumber packet_number, | |
72 CumulativeEntropy* cumulative) const; | |
73 | |
74 // Maps packet numbers to the sent entropy hash for the packet number. | |
75 SentEntropyMap packets_entropy_; | |
76 QuicPacketNumber map_offset_; | |
77 | |
78 // Cache the cumulative entropy for IsValidEntropy. | |
79 CumulativeEntropy last_valid_entropy_; | |
80 | |
81 // Cache the cumulative entropy for the packet 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_ | |
OLD | NEW |