OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_QUIC_QUIC_FRAMER_H_ | 5 #ifndef NET_QUIC_QUIC_FRAMER_H_ |
6 #define NET_QUIC_QUIC_FRAMER_H_ | 6 #define NET_QUIC_QUIC_FRAMER_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 virtual ~QuicFramerVisitorInterface() {} | 57 virtual ~QuicFramerVisitorInterface() {} |
58 | 58 |
59 // Called if an error is detected in the QUIC protocol. | 59 // Called if an error is detected in the QUIC protocol. |
60 virtual void OnError(QuicFramer* framer) = 0; | 60 virtual void OnError(QuicFramer* framer) = 0; |
61 | 61 |
62 // Called only when |is_server_| is true and the the framer gets a packet with | 62 // Called only when |is_server_| is true and the the framer gets a packet with |
63 // version flag true and the version on the packet doesn't match | 63 // version flag true and the version on the packet doesn't match |
64 // |quic_version_|. The visitor should return true after it updates the | 64 // |quic_version_|. The visitor should return true after it updates the |
65 // version of the |framer_| to |received_version| or false to stop processing | 65 // version of the |framer_| to |received_version| or false to stop processing |
66 // this packet. | 66 // this packet. |
67 virtual bool OnProtocolVersionMismatch(QuicVersionTag received_version) = 0; | 67 virtual bool OnProtocolVersionMismatch(QuicTag received_version) = 0; |
68 | 68 |
69 // Called when a new packet has been received, before it | 69 // Called when a new packet has been received, before it |
70 // has been validated or processed. | 70 // has been validated or processed. |
71 virtual void OnPacket() = 0; | 71 virtual void OnPacket() = 0; |
72 | 72 |
73 // Called when a public reset packet has been parsed but has not yet | 73 // Called when a public reset packet has been parsed but has not yet |
74 // been validated. | 74 // been validated. |
75 virtual void OnPublicResetPacket( | 75 virtual void OnPublicResetPacket( |
76 const QuicPublicResetPacket& packet) = 0; | 76 const QuicPublicResetPacket& packet) = 0; |
77 | 77 |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 }; | 146 }; |
147 | 147 |
148 // Class for parsing and constructing QUIC packets. It has a | 148 // Class for parsing and constructing QUIC packets. It has a |
149 // QuicFramerVisitorInterface that is called when packets are parsed. | 149 // QuicFramerVisitorInterface that is called when packets are parsed. |
150 // It also has a QuicFecBuilder that is called when packets are constructed | 150 // It also has a QuicFecBuilder that is called when packets are constructed |
151 // in order to generate FEC data for subsequently building FEC packets. | 151 // in order to generate FEC data for subsequently building FEC packets. |
152 class NET_EXPORT_PRIVATE QuicFramer { | 152 class NET_EXPORT_PRIVATE QuicFramer { |
153 public: | 153 public: |
154 // Constructs a new framer that installs a kNULL QuicEncrypter and | 154 // Constructs a new framer that installs a kNULL QuicEncrypter and |
155 // QuicDecrypter for level ENCRYPTION_NONE. | 155 // QuicDecrypter for level ENCRYPTION_NONE. |
156 QuicFramer(QuicVersionTag quic_version, | 156 QuicFramer(QuicTag quic_version, |
157 QuicTime creation_time, | 157 QuicTime creation_time, |
158 bool is_server); | 158 bool is_server); |
159 | 159 |
160 virtual ~QuicFramer(); | 160 virtual ~QuicFramer(); |
161 | 161 |
162 // Returns true if |version| is a supported protocol version. | 162 // Returns true if |version| is a supported protocol version. |
163 bool IsSupportedVersion(QuicVersionTag version); | 163 bool IsSupportedVersion(QuicTag version); |
164 | 164 |
165 // Calculates the largest observed packet to advertise in the case an Ack | 165 // Calculates the largest observed packet to advertise in the case an Ack |
166 // Frame was truncated. last_written in this case is the iterator for the | 166 // Frame was truncated. last_written in this case is the iterator for the |
167 // last missing packet which fit in the outgoing ack. | 167 // last missing packet which fit in the outgoing ack. |
168 static QuicPacketSequenceNumber CalculateLargestObserved( | 168 static QuicPacketSequenceNumber CalculateLargestObserved( |
169 const SequenceNumberSet& missing_packets, | 169 const SequenceNumberSet& missing_packets, |
170 SequenceNumberSet::const_iterator last_written); | 170 SequenceNumberSet::const_iterator last_written); |
171 | 171 |
172 // Set callbacks to be called from the framer. A visitor must be set, or | 172 // Set callbacks to be called from the framer. A visitor must be set, or |
173 // else the framer will likely crash. It is acceptable for the visitor | 173 // else the framer will likely crash. It is acceptable for the visitor |
174 // to do nothing. If this is called multiple times, only the last visitor | 174 // to do nothing. If this is called multiple times, only the last visitor |
175 // will be used. | 175 // will be used. |
176 void set_visitor(QuicFramerVisitorInterface* visitor) { | 176 void set_visitor(QuicFramerVisitorInterface* visitor) { |
177 visitor_ = visitor; | 177 visitor_ = visitor; |
178 } | 178 } |
179 | 179 |
180 // Set a builder to be called from the framer when building FEC protected | 180 // Set a builder to be called from the framer when building FEC protected |
181 // packets. If this is called multiple times, only the last builder | 181 // packets. If this is called multiple times, only the last builder |
182 // will be used. The builder need not be set. | 182 // will be used. The builder need not be set. |
183 void set_fec_builder(QuicFecBuilderInterface* builder) { | 183 void set_fec_builder(QuicFecBuilderInterface* builder) { |
184 fec_builder_ = builder; | 184 fec_builder_ = builder; |
185 } | 185 } |
186 | 186 |
187 QuicVersionTag version() const { | 187 QuicTag version() const { |
188 return quic_version_; | 188 return quic_version_; |
189 } | 189 } |
190 | 190 |
191 void set_version(QuicVersionTag version) { | 191 void set_version(QuicTag version) { |
192 DCHECK(IsSupportedVersion(version)); | 192 DCHECK(IsSupportedVersion(version)); |
193 quic_version_ = version; | 193 quic_version_ = version; |
194 } | 194 } |
195 | 195 |
196 // Set entropy calculator to be called from the framer when it needs the | 196 // Set entropy calculator to be called from the framer when it needs the |
197 // entropy of a truncated ack frame. An entropy calculator must be set or else | 197 // entropy of a truncated ack frame. An entropy calculator must be set or else |
198 // the framer will likely crash. If this is called multiple times, only the | 198 // the framer will likely crash. If this is called multiple times, only the |
199 // last visitor will be used. | 199 // last visitor will be used. |
200 void set_entropy_calculator( | 200 void set_entropy_calculator( |
201 QuicReceivedEntropyHashCalculatorInterface* entropy_calculator) { | 201 QuicReceivedEntropyHashCalculatorInterface* entropy_calculator) { |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 // packet could not be created. | 264 // packet could not be created. |
265 SerializedPacket ConstructFecPacket(const QuicPacketHeader& header, | 265 SerializedPacket ConstructFecPacket(const QuicPacketHeader& header, |
266 const QuicFecData& fec); | 266 const QuicFecData& fec); |
267 | 267 |
268 // Returns a new public reset packet, owned by the caller. | 268 // Returns a new public reset packet, owned by the caller. |
269 static QuicEncryptedPacket* ConstructPublicResetPacket( | 269 static QuicEncryptedPacket* ConstructPublicResetPacket( |
270 const QuicPublicResetPacket& packet); | 270 const QuicPublicResetPacket& packet); |
271 | 271 |
272 QuicEncryptedPacket* ConstructVersionNegotiationPacket( | 272 QuicEncryptedPacket* ConstructVersionNegotiationPacket( |
273 const QuicPacketPublicHeader& header, | 273 const QuicPacketPublicHeader& header, |
274 const QuicVersionTagList& supported_versions); | 274 const QuicTagVector& supported_versions); |
275 | 275 |
276 // SetDecrypter sets the primary decrypter, replacing any that already exists, | 276 // SetDecrypter sets the primary decrypter, replacing any that already exists, |
277 // and takes ownership. If an alternative decrypter is in place then the | 277 // and takes ownership. If an alternative decrypter is in place then the |
278 // function DCHECKs. This is intended for cases where one knows that future | 278 // function DCHECKs. This is intended for cases where one knows that future |
279 // packets will be using the new decrypter and the previous decrypter is not | 279 // packets will be using the new decrypter and the previous decrypter is not |
280 // obsolete. | 280 // obsolete. |
281 void SetDecrypter(QuicDecrypter* decrypter); | 281 void SetDecrypter(QuicDecrypter* decrypter); |
282 | 282 |
283 // SetAlternativeDecrypter sets a decrypter that may be used to decrypt | 283 // SetAlternativeDecrypter sets a decrypter that may be used to decrypt |
284 // future packets and takes ownership of it. If |latch_once_used| is true, | 284 // future packets and takes ownership of it. If |latch_once_used| is true, |
285 // then the first time that the decrypter is successful it will replace the | 285 // then the first time that the decrypter is successful it will replace the |
286 // primary decrypter. Otherwise both decrypters will remain active and the | 286 // primary decrypter. Otherwise both decrypters will remain active and the |
287 // primary decrypter will be the one last used. | 287 // primary decrypter will be the one last used. |
288 void SetAlternativeDecrypter(QuicDecrypter* decrypter, | 288 void SetAlternativeDecrypter(QuicDecrypter* decrypter, |
289 bool latch_once_used); | 289 bool latch_once_used); |
290 | 290 |
291 const QuicDecrypter* decrypter() const; | 291 const QuicDecrypter* decrypter() const; |
292 const QuicDecrypter* alternative_decrypter() const; | 292 const QuicDecrypter* alternative_decrypter() const; |
293 | 293 |
294 // Changes the encrypter used for level |level| to |encrypter|. The function | 294 // Changes the encrypter used for level |level| to |encrypter|. The function |
295 // takes ownership of |encrypter|. | 295 // takes ownership of |encrypter|. |
296 void SetEncrypter(EncryptionLevel level, QuicEncrypter* encrypter); | 296 void SetEncrypter(EncryptionLevel level, QuicEncrypter* encrypter); |
297 const QuicEncrypter* encrypter(EncryptionLevel level) const; | 297 const QuicEncrypter* encrypter(EncryptionLevel level) const; |
298 | 298 |
| 299 // SwapCryptersForTest exchanges the state of the crypters with |other|. To |
| 300 // be used in tests only. |
| 301 void SwapCryptersForTest(QuicFramer* other); |
| 302 |
299 // Returns a new encrypted packet, owned by the caller. | 303 // Returns a new encrypted packet, owned by the caller. |
300 QuicEncryptedPacket* EncryptPacket(EncryptionLevel level, | 304 QuicEncryptedPacket* EncryptPacket(EncryptionLevel level, |
301 QuicPacketSequenceNumber sequence_number, | 305 QuicPacketSequenceNumber sequence_number, |
302 const QuicPacket& packet); | 306 const QuicPacket& packet); |
303 | 307 |
304 // Returns the maximum length of plaintext that can be encrypted | 308 // Returns the maximum length of plaintext that can be encrypted |
305 // to ciphertext no larger than |ciphertext_size|. | 309 // to ciphertext no larger than |ciphertext_size|. |
306 size_t GetMaxPlaintextSize(size_t ciphertext_size); | 310 size_t GetMaxPlaintextSize(size_t ciphertext_size); |
307 | 311 |
308 const std::string& detailed_error() { return detailed_error_; } | 312 const std::string& detailed_error() { return detailed_error_; } |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
389 scoped_ptr<QuicDataReader> reader_; | 393 scoped_ptr<QuicDataReader> reader_; |
390 QuicFramerVisitorInterface* visitor_; | 394 QuicFramerVisitorInterface* visitor_; |
391 QuicFecBuilderInterface* fec_builder_; | 395 QuicFecBuilderInterface* fec_builder_; |
392 QuicReceivedEntropyHashCalculatorInterface* entropy_calculator_; | 396 QuicReceivedEntropyHashCalculatorInterface* entropy_calculator_; |
393 QuicErrorCode error_; | 397 QuicErrorCode error_; |
394 // Updated by ProcessPacketHeader when it succeeds. | 398 // Updated by ProcessPacketHeader when it succeeds. |
395 QuicPacketSequenceNumber last_sequence_number_; | 399 QuicPacketSequenceNumber last_sequence_number_; |
396 // Buffer containing decrypted payload data during parsing. | 400 // Buffer containing decrypted payload data during parsing. |
397 scoped_ptr<QuicData> decrypted_; | 401 scoped_ptr<QuicData> decrypted_; |
398 // Version of the protocol being used. | 402 // Version of the protocol being used. |
399 QuicVersionTag quic_version_; | 403 QuicTag quic_version_; |
400 // Primary decrypter used to decrypt packets during parsing. | 404 // Primary decrypter used to decrypt packets during parsing. |
401 scoped_ptr<QuicDecrypter> decrypter_; | 405 scoped_ptr<QuicDecrypter> decrypter_; |
402 // Alternative decrypter that can also be used to decrypt packets. | 406 // Alternative decrypter that can also be used to decrypt packets. |
403 scoped_ptr<QuicDecrypter> alternative_decrypter_; | 407 scoped_ptr<QuicDecrypter> alternative_decrypter_; |
404 // alternative_decrypter_latch_is true if, when |alternative_decrypter_| | 408 // alternative_decrypter_latch_is true if, when |alternative_decrypter_| |
405 // successfully decrypts a packet, we should install it as the only | 409 // successfully decrypts a packet, we should install it as the only |
406 // decrypter. | 410 // decrypter. |
407 bool alternative_decrypter_latch_; | 411 bool alternative_decrypter_latch_; |
408 // Encrypters used to encrypt packets via EncryptPacket(). | 412 // Encrypters used to encrypt packets via EncryptPacket(). |
409 scoped_ptr<QuicEncrypter> encrypter_[NUM_ENCRYPTION_LEVELS]; | 413 scoped_ptr<QuicEncrypter> encrypter_[NUM_ENCRYPTION_LEVELS]; |
410 // Tracks if the framer is being used by the entity that received the | 414 // Tracks if the framer is being used by the entity that received the |
411 // connection or the entity that initiated it. | 415 // connection or the entity that initiated it. |
412 bool is_server_; | 416 bool is_server_; |
413 // The time this frames was created. Time written to the wire will be | 417 // The time this frames was created. Time written to the wire will be |
414 // written as a delta from this value. | 418 // written as a delta from this value. |
415 QuicTime creation_time_; | 419 QuicTime creation_time_; |
416 | 420 |
417 DISALLOW_COPY_AND_ASSIGN(QuicFramer); | 421 DISALLOW_COPY_AND_ASSIGN(QuicFramer); |
418 }; | 422 }; |
419 | 423 |
420 } // namespace net | 424 } // namespace net |
421 | 425 |
422 #endif // NET_QUIC_QUIC_FRAMER_H_ | 426 #endif // NET_QUIC_QUIC_FRAMER_H_ |
OLD | NEW |