| 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 QuicPacketSequenceNumber LargestReceivedSequenceNumber() const; | |
| 47 | |
| 48 // Recalculate the received entropy hash and clears old packet entropies, | |
| 49 // now that the sender sent us the |entropy_hash| for packets up to, | |
| 50 // but not including, |peer_least_unacked|. | |
| 51 void RecalculateReceivedEntropyHash( | |
| 52 QuicPacketSequenceNumber peer_least_unacked, | |
| 53 QuicPacketEntropyHash entropy_hash); | |
| 54 | |
| 55 // Returns true if |entropy_hash| matches the expected sent entropy hash | |
| 56 // up to |sequence_number| removing sequence numbers from |missing_packets|. | |
| 57 bool IsValidEntropy(QuicPacketSequenceNumber sequence_number, | |
| 58 const SequenceNumberSet& missing_packets, | |
| 59 QuicPacketEntropyHash entropy_hash) const; | |
| 60 | |
| 61 // Removes not required entries from |sent_packets_entropy_| before | |
| 62 // |sequence_number|. | |
| 63 void ClearSentEntropyBefore(QuicPacketSequenceNumber sequence_number); | |
| 64 | |
| 65 QuicPacketEntropyHash sent_packets_entropy_hash() const { | |
| 66 return sent_packets_entropy_hash_; | |
| 67 } | |
| 68 | |
| 69 QuicPacketEntropyHash received_packets_entropy_hash() const { | |
| 70 return received_packets_entropy_hash_; | |
| 71 } | |
| 72 | |
| 73 private: | |
| 74 typedef linked_hash_map<QuicPacketSequenceNumber, | |
| 75 std::pair<QuicPacketEntropyHash, | |
| 76 QuicPacketEntropyHash> > SentEntropyMap; | |
| 77 typedef std::map<QuicPacketSequenceNumber, | |
| 78 QuicPacketEntropyHash> ReceivedEntropyMap; | |
| 79 | |
| 80 // Linked hash map from sequence numbers to the sent entropy hash up to the | |
| 81 // sequence number in the key. | |
| 82 SentEntropyMap sent_packets_entropy_; | |
| 83 | |
| 84 // Cumulative hash of entropy of all sent packets. | |
| 85 QuicPacketEntropyHash sent_packets_entropy_hash_; | |
| 86 | |
| 87 // TODO(satyamshekhar): Can be optimized using an interval set like data | |
| 88 // structure. | |
| 89 // Map of received sequence numbers to their corresponding entropy. | |
| 90 // Every received packet has an entry, and packets without the entropy bit set | |
| 91 // have an entropy value of 0. | |
| 92 // TODO(ianswett): When the entropy flag is off, the entropy should not be 0. | |
| 93 ReceivedEntropyMap received_packets_entropy_; | |
| 94 | |
| 95 // Cumulative hash of entropy of all received packets. | |
| 96 QuicPacketEntropyHash received_packets_entropy_hash_; | |
| 97 | |
| 98 // The largest sequence number cleared by RecalculateReceivedEntropyHash. | |
| 99 // Received entropy cannot be calculated for numbers less than it. | |
| 100 QuicPacketSequenceNumber largest_received_sequence_number_; | |
| 101 }; | |
| 102 | |
| 103 } // namespace net | |
| 104 | |
| 105 #endif // NET_QUIC_QUIC_PACKET_ENTROPY_MANAGER_H_ | |
| OLD | NEW |