| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 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_PACKET_ENTROPY_MANAGER_H_ |
| 9 #define NET_QUIC_QUIC_PACKET_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 and received packets by a connection to track the cumulative |
| 18 // entropy of both sent and received packets separately. It is used by the |
| 19 // connection to validate an ack frame sent by the peer as a preventive measure |
| 20 // against the optimistic ack attack. Also, called by the framer when it |
| 21 // truncates an ack frame to get the correct entropy value for the ack frame |
| 22 // being serialized. |
| 23 class NET_EXPORT_PRIVATE QuicPacketEntropyManager : |
| 24 public QuicReceivedEntropyHashCalculatorInterface { |
| 25 public: |
| 26 QuicPacketEntropyManager(); |
| 27 virtual ~QuicPacketEntropyManager(); |
| 28 |
| 29 // Record the received entropy hash against |sequence_number|. |
| 30 void RecordReceivedPacketEntropyHash(QuicPacketSequenceNumber sequence_number, |
| 31 QuicPacketEntropyHash entropy_hash); |
| 32 |
| 33 // Record |entropy_hash| for sent packet corresponding to |sequence_number|. |
| 34 void RecordSentPacketEntropyHash(QuicPacketSequenceNumber sequence_number, |
| 35 QuicPacketEntropyHash entropy_hash); |
| 36 |
| 37 // QuicReceivedEntropyHashCalculatorInterface |
| 38 // Called by QuicFramer, when the outgoing ack gets truncated, to recalculate |
| 39 // the received entropy hash for the truncated ack frame. |
| 40 virtual QuicPacketEntropyHash ReceivedEntropyHash( |
| 41 QuicPacketSequenceNumber sequence_number) const OVERRIDE; |
| 42 |
| 43 QuicPacketEntropyHash SentEntropyHash( |
| 44 QuicPacketSequenceNumber sequence_number) const; |
| 45 |
| 46 // Recalculate the received entropy hash since we had some missing packets |
| 47 // which the sender won't retransmit again and has sent us the |entropy_hash| |
| 48 // for packets up to, but not including, |sequence_number|. |
| 49 void RecalculateReceivedEntropyHash( |
| 50 QuicPacketSequenceNumber sequence_number, |
| 51 QuicPacketEntropyHash entropy_hash); |
| 52 |
| 53 // Returns true if |entropy_hash| matches the expected sent entropy hash |
| 54 // up to |sequence_number| removing sequence numbers from |missing_packets|. |
| 55 bool IsValidEntropy(QuicPacketSequenceNumber sequence_number, |
| 56 const SequenceNumberSet& missing_packets, |
| 57 QuicPacketEntropyHash entropy_hash) const; |
| 58 |
| 59 // Removes not required entries from |sent_packets_entropy_| before |
| 60 // |sequence_number|. |
| 61 void ClearSentEntropyBefore(QuicPacketSequenceNumber sequence_number); |
| 62 |
| 63 // Removes not required entries from |received_packets_entropy_| before |
| 64 // |sequence_number|. |
| 65 void ClearReceivedEntropyBefore(QuicPacketSequenceNumber sequence_number); |
| 66 |
| 67 QuicPacketEntropyHash sent_packets_entropy_hash() const { |
| 68 return sent_packets_entropy_hash_; |
| 69 } |
| 70 |
| 71 QuicPacketEntropyHash received_packets_entropy_hash() const { |
| 72 return received_packets_entropy_hash_; |
| 73 } |
| 74 |
| 75 private: |
| 76 typedef linked_hash_map<QuicPacketSequenceNumber, |
| 77 std::pair<QuicPacketEntropyHash, |
| 78 QuicPacketEntropyHash> > SentEntropyMap; |
| 79 typedef std::map<QuicPacketSequenceNumber, |
| 80 QuicPacketEntropyHash> ReceivedEntropyMap; |
| 81 |
| 82 // TODO(satyamshekhar): Can be optimized using an interval set like data |
| 83 // structure. |
| 84 // Set of received sequence numbers that had the received entropy flag set. |
| 85 ReceivedEntropyMap received_packets_entropy_; |
| 86 |
| 87 // Linked hash map from sequence numbers to the sent entropy hash up to the |
| 88 // sequence number in the key. |
| 89 SentEntropyMap sent_packets_entropy_; |
| 90 |
| 91 // Cumulative hash of entropy of all sent packets. |
| 92 QuicPacketEntropyHash sent_packets_entropy_hash_; |
| 93 |
| 94 // Cumulative hash of entropy of all received packets. |
| 95 QuicPacketEntropyHash received_packets_entropy_hash_; |
| 96 |
| 97 QuicPacketSequenceNumber largest_received_sequence_number_; |
| 98 }; |
| 99 |
| 100 } // namespace net |
| 101 |
| 102 #endif // NET_QUIC_QUIC_PACKET_ENTROPY_MANAGER_H_ |
| OLD | NEW |