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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 | 49 |
50 // This class receives callbacks from the framer when packets | 50 // This class receives callbacks from the framer when packets |
51 // are processed. | 51 // are processed. |
52 class NET_EXPORT_PRIVATE QuicFramerVisitorInterface { | 52 class NET_EXPORT_PRIVATE QuicFramerVisitorInterface { |
53 public: | 53 public: |
54 virtual ~QuicFramerVisitorInterface() {} | 54 virtual ~QuicFramerVisitorInterface() {} |
55 | 55 |
56 // Called if an error is detected in the QUIC protocol. | 56 // Called if an error is detected in the QUIC protocol. |
57 virtual void OnError(QuicFramer* framer) = 0; | 57 virtual void OnError(QuicFramer* framer) = 0; |
58 | 58 |
| 59 // Called only when |is_server_| is true and the the framer gets a packet with |
| 60 // version flag true and the version on the packet doesn't match |
| 61 // |quic_version_|. The visitor should return true after it updates the |
| 62 // version of the |framer_| to |received_version| or false to stop processing |
| 63 // this packet. |
| 64 virtual bool OnProtocolVersionMismatch(QuicVersionTag received_version) = 0; |
| 65 |
59 // Called when a new packet has been received, before it | 66 // Called when a new packet has been received, before it |
60 // has been validated or processed. | 67 // has been validated or processed. |
61 virtual void OnPacket() = 0; | 68 virtual void OnPacket() = 0; |
62 | 69 |
63 // Called when a public reset packet has been parsed but has not yet | 70 // Called when a public reset packet has been parsed but has not yet |
64 // been validated. | 71 // been validated. |
65 virtual void OnPublicResetPacket( | 72 virtual void OnPublicResetPacket( |
66 const QuicPublicResetPacket& packet) = 0; | 73 const QuicPublicResetPacket& packet) = 0; |
67 | 74 |
| 75 // Called only when |is_server_| is false and a version negotiation packet has |
| 76 // been parsed. |
| 77 virtual void OnVersionNegotiationPacket( |
| 78 const QuicVersionNegotiationPacket& packet) = 0; |
| 79 |
68 // Called when a lost packet has been recovered via FEC, | 80 // Called when a lost packet has been recovered via FEC, |
69 // before it has been processed. | 81 // before it has been processed. |
70 virtual void OnRevivedPacket() = 0; | 82 virtual void OnRevivedPacket() = 0; |
71 | 83 |
72 // Called when the header of a packet had been parsed. | 84 // Called when the complete header of a packet had been parsed. |
73 // If OnPacketHeader returns false, parsing for this packet will cease. | 85 // If OnPacketHeader returns false, framing for this packet will cease. |
74 virtual bool OnPacketHeader(const QuicPacketHeader& header) = 0; | 86 virtual bool OnPacketHeader(const QuicPacketHeader& header) = 0; |
75 | 87 |
76 // Called when a data packet is parsed that is part of an FEC group. | 88 // Called when a data packet is parsed that is part of an FEC group. |
77 // |payload| is the non-encrypted FEC protected payload of the packet. | 89 // |payload| is the non-encrypted FEC protected payload of the packet. |
78 virtual void OnFecProtectedPayload(base::StringPiece payload) = 0; | 90 virtual void OnFecProtectedPayload(base::StringPiece payload) = 0; |
79 | 91 |
80 // Called when a StreamFrame has been parsed. | 92 // Called when a StreamFrame has been parsed. |
81 virtual void OnStreamFrame(const QuicStreamFrame& frame) = 0; | 93 virtual void OnStreamFrame(const QuicStreamFrame& frame) = 0; |
82 | 94 |
83 // Called when a AckFrame has been parsed. | 95 // Called when a AckFrame has been parsed. |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 | 143 |
132 // Class for parsing and constructing QUIC packets. It has a | 144 // Class for parsing and constructing QUIC packets. It has a |
133 // QuicFramerVisitorInterface that is called when packets are parsed. | 145 // QuicFramerVisitorInterface that is called when packets are parsed. |
134 // It also has a QuicFecBuilder that is called when packets are constructed | 146 // It also has a QuicFecBuilder that is called when packets are constructed |
135 // in order to generate FEC data for subsequently building FEC packets. | 147 // in order to generate FEC data for subsequently building FEC packets. |
136 class NET_EXPORT_PRIVATE QuicFramer { | 148 class NET_EXPORT_PRIVATE QuicFramer { |
137 public: | 149 public: |
138 // Constructs a new framer that will own |decrypter| and |encrypter|. | 150 // Constructs a new framer that will own |decrypter| and |encrypter|. |
139 QuicFramer(QuicVersionTag quic_version, | 151 QuicFramer(QuicVersionTag quic_version, |
140 QuicDecrypter* decrypter, | 152 QuicDecrypter* decrypter, |
141 QuicEncrypter* encrypter); | 153 QuicEncrypter* encrypter, |
| 154 bool is_server); |
142 | 155 |
143 virtual ~QuicFramer(); | 156 virtual ~QuicFramer(); |
144 | 157 |
| 158 // Returns true if |version| is a supported protocol version. |
| 159 bool IsSupportedVersion(QuicVersionTag version); |
| 160 |
145 // Calculates the largest observed packet to advertise in the case an Ack | 161 // Calculates the largest observed packet to advertise in the case an Ack |
146 // Frame was truncated. last_written in this case is the iterator for the | 162 // Frame was truncated. last_written in this case is the iterator for the |
147 // last missing packet which fit in the outgoing ack. | 163 // last missing packet which fit in the outgoing ack. |
148 static QuicPacketSequenceNumber CalculateLargestObserved( | 164 static QuicPacketSequenceNumber CalculateLargestObserved( |
149 const SequenceNumberSet& missing_packets, | 165 const SequenceNumberSet& missing_packets, |
150 SequenceNumberSet::const_iterator last_written); | 166 SequenceNumberSet::const_iterator last_written); |
151 | 167 |
152 // Set callbacks to be called from the framer. A visitor must be set, or | 168 // Set callbacks to be called from the framer. A visitor must be set, or |
153 // else the framer will likely crash. It is acceptable for the visitor | 169 // else the framer will likely crash. It is acceptable for the visitor |
154 // to do nothing. If this is called multiple times, only the last visitor | 170 // to do nothing. If this is called multiple times, only the last visitor |
155 // will be used. | 171 // will be used. |
156 void set_visitor(QuicFramerVisitorInterface* visitor) { | 172 void set_visitor(QuicFramerVisitorInterface* visitor) { |
157 visitor_ = visitor; | 173 visitor_ = visitor; |
158 } | 174 } |
159 | 175 |
160 // Set a builder to be called from the framer when building FEC protected | 176 // Set a builder to be called from the framer when building FEC protected |
161 // packets. If this is called multiple times, only the last builder | 177 // packets. If this is called multiple times, only the last builder |
162 // will be used. The builder need not be set. | 178 // will be used. The builder need not be set. |
163 void set_fec_builder(QuicFecBuilderInterface* builder) { | 179 void set_fec_builder(QuicFecBuilderInterface* builder) { |
164 fec_builder_ = builder; | 180 fec_builder_ = builder; |
165 } | 181 } |
166 | 182 |
167 QuicVersionTag version() const { | 183 QuicVersionTag version() const { |
168 return quic_version_; | 184 return quic_version_; |
169 } | 185 } |
170 | 186 |
| 187 void set_version(QuicVersionTag version) { |
| 188 DCHECK(IsSupportedVersion(version)); |
| 189 quic_version_ = version; |
| 190 } |
| 191 |
171 // Set entropy calculator to be called from the framer when it needs the | 192 // Set entropy calculator to be called from the framer when it needs the |
172 // entropy of a truncated ack frame. An entropy calculator must be set or else | 193 // entropy of a truncated ack frame. An entropy calculator must be set or else |
173 // the framer will likely crash. If this is called multiple times, only the | 194 // the framer will likely crash. If this is called multiple times, only the |
174 // last visitor will be used. | 195 // last visitor will be used. |
175 void set_entropy_calculator( | 196 void set_entropy_calculator( |
176 QuicReceivedEntropyHashCalculatorInterface* entropy_calculator) { | 197 QuicReceivedEntropyHashCalculatorInterface* entropy_calculator) { |
177 entropy_calculator_ = entropy_calculator; | 198 entropy_calculator_ = entropy_calculator; |
178 } | 199 } |
179 | 200 |
180 QuicErrorCode error() const { | 201 QuicErrorCode error() const { |
(...skipping 18 matching lines...) Expand all Loading... |
199 static size_t GetMinStreamFrameSize(); | 220 static size_t GetMinStreamFrameSize(); |
200 // Size in bytes of all ack frame fields without the missing packets. | 221 // Size in bytes of all ack frame fields without the missing packets. |
201 static size_t GetMinAckFrameSize(); | 222 static size_t GetMinAckFrameSize(); |
202 // Size in bytes of all reset stream frame without the error details. | 223 // Size in bytes of all reset stream frame without the error details. |
203 static size_t GetMinRstStreamFrameSize(); | 224 static size_t GetMinRstStreamFrameSize(); |
204 // Size in bytes of all connection close frame fields without the error | 225 // Size in bytes of all connection close frame fields without the error |
205 // details and the missing packets from the enclosed ack frame. | 226 // details and the missing packets from the enclosed ack frame. |
206 static size_t GetMinConnectionCloseFrameSize(); | 227 static size_t GetMinConnectionCloseFrameSize(); |
207 // Size in bytes of all GoAway frame fields without the reason phrase. | 228 // Size in bytes of all GoAway frame fields without the reason phrase. |
208 static size_t GetMinGoAwayFrameSize(); | 229 static size_t GetMinGoAwayFrameSize(); |
| 230 // Size in bytes required for a serialized version negotiation packet |
| 231 size_t GetVersionNegotiationPacketSize(size_t number_versions); |
209 | 232 |
210 // Returns the number of bytes added to the packet for the specified frame, | 233 // Returns the number of bytes added to the packet for the specified frame, |
211 // and 0 if the frame doesn't fit. Includes the header size for the first | 234 // and 0 if the frame doesn't fit. Includes the header size for the first |
212 // frame. | 235 // frame. |
213 size_t GetSerializedFrameLength( | 236 size_t GetSerializedFrameLength( |
214 const QuicFrame& frame, size_t free_bytes, bool first_frame); | 237 const QuicFrame& frame, size_t free_bytes, bool first_frame); |
215 | 238 |
216 // Returns the associated data from the encrypted packet |encrypted| as a | 239 // Returns the associated data from the encrypted packet |encrypted| as a |
217 // stringpiece. | 240 // stringpiece. |
218 static base::StringPiece GetAssociatedDataFromEncryptedPacket( | 241 static base::StringPiece GetAssociatedDataFromEncryptedPacket( |
(...skipping 16 matching lines...) Expand all Loading... |
235 // Returns a SerializedPacket whose |packet| member is owned by the caller, | 258 // Returns a SerializedPacket whose |packet| member is owned by the caller, |
236 // and is populated with the fields in |header| and |fec|, or is NULL if the | 259 // and is populated with the fields in |header| and |fec|, or is NULL if the |
237 // packet could not be created. | 260 // packet could not be created. |
238 SerializedPacket ConstructFecPacket(const QuicPacketHeader& header, | 261 SerializedPacket ConstructFecPacket(const QuicPacketHeader& header, |
239 const QuicFecData& fec); | 262 const QuicFecData& fec); |
240 | 263 |
241 // Returns a new public reset packet, owned by the caller. | 264 // Returns a new public reset packet, owned by the caller. |
242 static QuicEncryptedPacket* ConstructPublicResetPacket( | 265 static QuicEncryptedPacket* ConstructPublicResetPacket( |
243 const QuicPublicResetPacket& packet); | 266 const QuicPublicResetPacket& packet); |
244 | 267 |
| 268 QuicEncryptedPacket* ConstructVersionNegotiationPacket( |
| 269 const QuicPacketPublicHeader& header, |
| 270 const QuicVersionTagList& supported_versions); |
| 271 |
245 // Returns a new encrypted packet, owned by the caller. | 272 // Returns a new encrypted packet, owned by the caller. |
246 QuicEncryptedPacket* EncryptPacket(QuicPacketSequenceNumber sequence_number, | 273 QuicEncryptedPacket* EncryptPacket(QuicPacketSequenceNumber sequence_number, |
247 const QuicPacket& packet); | 274 const QuicPacket& packet); |
248 | 275 |
249 // Returns the maximum length of plaintext that can be encrypted | 276 // Returns the maximum length of plaintext that can be encrypted |
250 // to ciphertext no larger than |ciphertext_size|. | 277 // to ciphertext no larger than |ciphertext_size|. |
251 size_t GetMaxPlaintextSize(size_t ciphertext_size); | 278 size_t GetMaxPlaintextSize(size_t ciphertext_size); |
252 | 279 |
253 const std::string& detailed_error() { return detailed_error_; } | 280 const std::string& detailed_error() { return detailed_error_; } |
254 | 281 |
255 // Read the guid from a packet header. | 282 // Read the guid from a packet header. |
256 // Return true on success, else false. | 283 // Return true on success, else false. |
257 static bool ReadGuidFromPacket(const QuicEncryptedPacket& packet, | 284 static bool ReadGuidFromPacket(const QuicEncryptedPacket& packet, |
258 QuicGuid* guid); | 285 QuicGuid* guid); |
259 | 286 |
260 private: | 287 private: |
261 friend class test::QuicFramerPeer; | 288 friend class test::QuicFramerPeer; |
262 | 289 |
263 QuicPacketEntropyHash GetPacketEntropyHash( | 290 QuicPacketEntropyHash GetPacketEntropyHash( |
264 const QuicPacketHeader& header) const; | 291 const QuicPacketHeader& header) const; |
265 | 292 |
266 bool ProcessDataPacket(const QuicPacketPublicHeader& public_header, | 293 bool ProcessDataPacket(const QuicPacketPublicHeader& public_header, |
267 const QuicEncryptedPacket& packet); | 294 const QuicEncryptedPacket& packet); |
268 | 295 |
269 bool ProcessPublicResetPacket(const QuicPacketPublicHeader& public_header); | 296 bool ProcessPublicResetPacket(const QuicPacketPublicHeader& public_header); |
270 | 297 |
| 298 bool ProcessVersionNegotiationPacket(QuicPacketPublicHeader* public_header); |
| 299 |
271 bool WritePacketHeader(const QuicPacketHeader& header, | 300 bool WritePacketHeader(const QuicPacketHeader& header, |
272 QuicDataWriter* writer); | 301 QuicDataWriter* writer); |
273 | 302 |
274 bool ProcessPublicHeader(QuicPacketPublicHeader* header); | 303 bool ProcessPublicHeader(QuicPacketPublicHeader* header); |
275 | 304 |
276 bool ProcessPacketHeader(QuicPacketHeader* header, | 305 bool ProcessPacketHeader(QuicPacketHeader* header, |
277 const QuicEncryptedPacket& packet); | 306 const QuicEncryptedPacket& packet); |
278 | 307 |
279 bool ProcessPacketSequenceNumber(QuicPacketSequenceNumber* sequence_number); | 308 bool ProcessPacketSequenceNumber(QuicPacketSequenceNumber* sequence_number); |
280 bool ProcessFrameData(); | 309 bool ProcessFrameData(); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 // Updated by ProcessPacketHeader when it succeeds. | 366 // Updated by ProcessPacketHeader when it succeeds. |
338 QuicPacketSequenceNumber last_sequence_number_; | 367 QuicPacketSequenceNumber last_sequence_number_; |
339 // Buffer containing decrypted payload data during parsing. | 368 // Buffer containing decrypted payload data during parsing. |
340 scoped_ptr<QuicData> decrypted_; | 369 scoped_ptr<QuicData> decrypted_; |
341 // Version of the protocol being used. | 370 // Version of the protocol being used. |
342 QuicVersionTag quic_version_; | 371 QuicVersionTag quic_version_; |
343 // Decrypter used to decrypt packets during parsing. | 372 // Decrypter used to decrypt packets during parsing. |
344 scoped_ptr<QuicDecrypter> decrypter_; | 373 scoped_ptr<QuicDecrypter> decrypter_; |
345 // Encrypter used to encrypt packets via EncryptPacket(). | 374 // Encrypter used to encrypt packets via EncryptPacket(). |
346 scoped_ptr<QuicEncrypter> encrypter_; | 375 scoped_ptr<QuicEncrypter> encrypter_; |
| 376 // Tracks if the framer is being used by the entity that received the |
| 377 // connection or the entity that initiated it. |
| 378 bool is_server_; |
347 | 379 |
348 DISALLOW_COPY_AND_ASSIGN(QuicFramer); | 380 DISALLOW_COPY_AND_ASSIGN(QuicFramer); |
349 }; | 381 }; |
350 | 382 |
351 } // namespace net | 383 } // namespace net |
352 | 384 |
353 #endif // NET_QUIC_QUIC_FRAMER_H_ | 385 #endif // NET_QUIC_QUIC_FRAMER_H_ |
OLD | NEW |