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 "net/base/linked_hash_map.h" 12 #include "net/quic/quic_framer.h" 13 #include "net/quic/quic_protocol.h" 14 15 namespace net { 16 17 // Records all sent packets by a connection to track the cumulative entropy of 18 // sent packets. It is used by the connection to validate an ack 19 // frame sent by the peer as a preventive measure against the optimistic ack 20 // attack. 21 class NET_EXPORT_PRIVATE QuicSentEntropyManager { 22 public: 23 QuicSentEntropyManager(); 24 virtual ~QuicSentEntropyManager(); 25 26 // Record |entropy_hash| for sent packet corresponding to |sequence_number|. 27 void RecordPacketEntropyHash(QuicPacketSequenceNumber sequence_number, 28 QuicPacketEntropyHash entropy_hash); 29 30 QuicPacketEntropyHash EntropyHash( 31 QuicPacketSequenceNumber sequence_number) const; 32 33 // Returns true if |entropy_hash| matches the expected sent entropy hash 34 // up to |sequence_number| removing sequence numbers from |missing_packets|. 35 bool IsValidEntropy(QuicPacketSequenceNumber sequence_number, 36 const SequenceNumberSet& missing_packets, 37 QuicPacketEntropyHash entropy_hash) const; 38 39 // Removes not required entries from |packets_entropy_| before 40 // |sequence_number|. 41 void ClearEntropyBefore(QuicPacketSequenceNumber sequence_number); 42 43 private: 44 typedef linked_hash_map<QuicPacketSequenceNumber, 45 std::pair<QuicPacketEntropyHash, 46 QuicPacketEntropyHash> > SentEntropyMap; 47 48 // Linked hash map from sequence numbers to the sent entropy hash up to the 49 // sequence number in the key. 50 SentEntropyMap packets_entropy_; 51 52 // Cumulative hash of entropy of all sent packets. 53 QuicPacketEntropyHash packets_entropy_hash_; 54 }; 55 56 } // namespace net 57 58 #endif // NET_QUIC_QUIC_SENT_ENTROPY_MANAGER_H_ 59