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 // The entity that handles framing writes for a Quic client or server. | |
6 // Each QuicSession will have a connection associated with it. | |
7 // | |
8 // On the server side, the Dispatcher handles the raw reads, and hands off | |
9 // packets via ProcessUdpPacket for framing and processing. | |
10 // | |
11 // On the client side, the Connection handles the raw reads, as well as the | |
12 // processing. | |
13 // | |
14 // Note: this class is not thread-safe. | |
15 | |
16 #ifndef NET_QUIC_QUIC_CONNECTION_H_ | |
17 #define NET_QUIC_QUIC_CONNECTION_H_ | |
18 | |
19 #include <stddef.h> | |
20 #include <deque> | |
21 #include <list> | |
22 #include <map> | |
23 #include <queue> | |
24 #include <string> | |
25 #include <vector> | |
26 | |
27 #include "base/basictypes.h" | |
28 #include "base/logging.h" | |
29 #include "net/base/iovec.h" | |
30 #include "net/base/ip_endpoint.h" | |
31 #include "net/quic/iovector.h" | |
32 #include "net/quic/quic_ack_notifier.h" | |
33 #include "net/quic/quic_ack_notifier_manager.h" | |
34 #include "net/quic/quic_alarm.h" | |
35 #include "net/quic/quic_blocked_writer_interface.h" | |
36 #include "net/quic/quic_connection_stats.h" | |
37 #include "net/quic/quic_packet_creator.h" | |
38 #include "net/quic/quic_packet_generator.h" | |
39 #include "net/quic/quic_packet_writer.h" | |
40 #include "net/quic/quic_protocol.h" | |
41 #include "net/quic/quic_received_packet_manager.h" | |
42 #include "net/quic/quic_sent_entropy_manager.h" | |
43 #include "net/quic/quic_sent_packet_manager.h" | |
44 #include "net/quic/quic_time.h" | |
45 #include "net/quic/quic_types.h" | |
46 | |
47 namespace net { | |
48 | |
49 class QuicClock; | |
50 class QuicConfig; | |
51 class QuicConnection; | |
52 class QuicDecrypter; | |
53 class QuicEncrypter; | |
54 class QuicFecGroup; | |
55 class QuicRandom; | |
56 | |
57 namespace test { | |
58 class PacketSavingConnection; | |
59 class QuicConnectionPeer; | |
60 } // namespace test | |
61 | |
62 // Class that receives callbacks from the connection when frames are received | |
63 // and when other interesting events happen. | |
64 class NET_EXPORT_PRIVATE QuicConnectionVisitorInterface { | |
65 public: | |
66 virtual ~QuicConnectionVisitorInterface() {} | |
67 | |
68 // A simple visitor interface for dealing with data frames. | |
69 virtual void OnStreamFrames(const std::vector<QuicStreamFrame>& frames) = 0; | |
70 | |
71 // The session should process all WINDOW_UPDATE frames, adjusting both stream | |
72 // and connection level flow control windows. | |
73 virtual void OnWindowUpdateFrames( | |
74 const std::vector<QuicWindowUpdateFrame>& frames) = 0; | |
75 | |
76 // BLOCKED frames tell us that the peer believes it is flow control blocked on | |
77 // a specified stream. If the session at this end disagrees, something has | |
78 // gone wrong with our flow control accounting. | |
79 virtual void OnBlockedFrames(const std::vector<QuicBlockedFrame>& frames) = 0; | |
80 | |
81 // Called when the stream is reset by the peer. | |
82 virtual void OnRstStream(const QuicRstStreamFrame& frame) = 0; | |
83 | |
84 // Called when the connection is going away according to the peer. | |
85 virtual void OnGoAway(const QuicGoAwayFrame& frame) = 0; | |
86 | |
87 // Called when the connection is closed either locally by the framer, or | |
88 // remotely by the peer. | |
89 virtual void OnConnectionClosed(QuicErrorCode error, bool from_peer) = 0; | |
90 | |
91 // Called when the connection failed to write because the socket was blocked. | |
92 virtual void OnWriteBlocked() = 0; | |
93 | |
94 // Called once a specific QUIC version is agreed by both endpoints. | |
95 virtual void OnSuccessfulVersionNegotiation(const QuicVersion& version) = 0; | |
96 | |
97 // Called when a blocked socket becomes writable. | |
98 virtual void OnCanWrite() = 0; | |
99 | |
100 // Called when the connection experiences a change in congestion window. | |
101 virtual void OnCongestionWindowChange(QuicTime now) = 0; | |
102 | |
103 // Called to ask if the visitor wants to schedule write resumption as it both | |
104 // has pending data to write, and is able to write (e.g. based on flow control | |
105 // limits). | |
106 // Writes may be pending because they were write-blocked, congestion-throttled | |
107 // or yielded to other connections. | |
108 virtual bool WillingAndAbleToWrite() const = 0; | |
109 | |
110 // Called to ask if any handshake messages are pending in this visitor. | |
111 virtual bool HasPendingHandshake() const = 0; | |
112 | |
113 // Called to ask if any streams are open in this visitor, excluding the | |
114 // reserved crypto and headers stream. | |
115 virtual bool HasOpenDataStreams() const = 0; | |
116 }; | |
117 | |
118 // Interface which gets callbacks from the QuicConnection at interesting | |
119 // points. Implementations must not mutate the state of the connection | |
120 // as a result of these callbacks. | |
121 class NET_EXPORT_PRIVATE QuicConnectionDebugVisitor | |
122 : public QuicPacketGenerator::DebugDelegate, | |
123 public QuicSentPacketManager::DebugDelegate { | |
124 public: | |
125 ~QuicConnectionDebugVisitor() override {} | |
126 | |
127 // Called when a packet has been sent. | |
128 virtual void OnPacketSent(const SerializedPacket& serialized_packet, | |
129 QuicPacketSequenceNumber original_sequence_number, | |
130 EncryptionLevel level, | |
131 TransmissionType transmission_type, | |
132 const QuicEncryptedPacket& packet, | |
133 QuicTime sent_time) {} | |
134 | |
135 // Called when a packet has been received, but before it is | |
136 // validated or parsed. | |
137 virtual void OnPacketReceived(const IPEndPoint& self_address, | |
138 const IPEndPoint& peer_address, | |
139 const QuicEncryptedPacket& packet) {} | |
140 | |
141 // Called when a packet is received with a connection id that does not | |
142 // match the ID of this connection. | |
143 virtual void OnIncorrectConnectionId( | |
144 QuicConnectionId connection_id) {} | |
145 | |
146 // Called when an undecryptable packet has been received. | |
147 virtual void OnUndecryptablePacket() {} | |
148 | |
149 // Called when a duplicate packet has been received. | |
150 virtual void OnDuplicatePacket(QuicPacketSequenceNumber sequence_number) {} | |
151 | |
152 // Called when the protocol version on the received packet doensn't match | |
153 // current protocol version of the connection. | |
154 virtual void OnProtocolVersionMismatch(QuicVersion version) {} | |
155 | |
156 // Called when the complete header of a packet has been parsed. | |
157 virtual void OnPacketHeader(const QuicPacketHeader& header) {} | |
158 | |
159 // Called when a StreamFrame has been parsed. | |
160 virtual void OnStreamFrame(const QuicStreamFrame& frame) {} | |
161 | |
162 // Called when a AckFrame has been parsed. | |
163 virtual void OnAckFrame(const QuicAckFrame& frame) {} | |
164 | |
165 // Called when a StopWaitingFrame has been parsed. | |
166 virtual void OnStopWaitingFrame(const QuicStopWaitingFrame& frame) {} | |
167 | |
168 // Called when a Ping has been parsed. | |
169 virtual void OnPingFrame(const QuicPingFrame& frame) {} | |
170 | |
171 // Called when a GoAway has been parsed. | |
172 virtual void OnGoAwayFrame(const QuicGoAwayFrame& frame) {} | |
173 | |
174 // Called when a RstStreamFrame has been parsed. | |
175 virtual void OnRstStreamFrame(const QuicRstStreamFrame& frame) {} | |
176 | |
177 // Called when a ConnectionCloseFrame has been parsed. | |
178 virtual void OnConnectionCloseFrame( | |
179 const QuicConnectionCloseFrame& frame) {} | |
180 | |
181 // Called when a WindowUpdate has been parsed. | |
182 virtual void OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame) {} | |
183 | |
184 // Called when a BlockedFrame has been parsed. | |
185 virtual void OnBlockedFrame(const QuicBlockedFrame& frame) {} | |
186 | |
187 // Called when a public reset packet has been received. | |
188 virtual void OnPublicResetPacket(const QuicPublicResetPacket& packet) {} | |
189 | |
190 // Called when a version negotiation packet has been received. | |
191 virtual void OnVersionNegotiationPacket( | |
192 const QuicVersionNegotiationPacket& packet) {} | |
193 | |
194 // Called after a packet has been successfully parsed which results | |
195 // in the revival of a packet via FEC. | |
196 virtual void OnRevivedPacket(const QuicPacketHeader& revived_header, | |
197 base::StringPiece payload) {} | |
198 | |
199 // Called when the connection is closed. | |
200 virtual void OnConnectionClosed(QuicErrorCode error, bool from_peer) {} | |
201 | |
202 // Called when the version negotiation is successful. | |
203 virtual void OnSuccessfulVersionNegotiation(const QuicVersion& version) {} | |
204 }; | |
205 | |
206 class NET_EXPORT_PRIVATE QuicConnectionHelperInterface { | |
207 public: | |
208 virtual ~QuicConnectionHelperInterface() {} | |
209 | |
210 // Returns a QuicClock to be used for all time related functions. | |
211 virtual const QuicClock* GetClock() const = 0; | |
212 | |
213 // Returns a QuicRandom to be used for all random number related functions. | |
214 virtual QuicRandom* GetRandomGenerator() = 0; | |
215 | |
216 // Creates a new platform-specific alarm which will be configured to | |
217 // notify |delegate| when the alarm fires. Caller takes ownership | |
218 // of the new alarm, which will not yet be "set" to fire. | |
219 virtual QuicAlarm* CreateAlarm(QuicAlarm::Delegate* delegate) = 0; | |
220 }; | |
221 | |
222 class NET_EXPORT_PRIVATE QuicConnection | |
223 : public QuicFramerVisitorInterface, | |
224 public QuicBlockedWriterInterface, | |
225 public QuicPacketGenerator::DelegateInterface, | |
226 public QuicSentPacketManager::NetworkChangeVisitor { | |
227 public: | |
228 enum AckBundling { | |
229 NO_ACK = 0, | |
230 SEND_ACK = 1, | |
231 BUNDLE_PENDING_ACK = 2, | |
232 }; | |
233 | |
234 class PacketWriterFactory { | |
235 public: | |
236 virtual ~PacketWriterFactory() {} | |
237 | |
238 virtual QuicPacketWriter* Create(QuicConnection* connection) const = 0; | |
239 }; | |
240 | |
241 // Constructs a new QuicConnection for |connection_id| and |address|. Invokes | |
242 // writer_factory->Create() to get a writer; |owns_writer| specifies whether | |
243 // the connection takes ownership of the returned writer. |helper| must | |
244 // outlive this connection. | |
245 QuicConnection(QuicConnectionId connection_id, | |
246 IPEndPoint address, | |
247 QuicConnectionHelperInterface* helper, | |
248 const PacketWriterFactory& writer_factory, | |
249 bool owns_writer, | |
250 bool is_server, | |
251 bool is_secure, | |
252 const QuicVersionVector& supported_versions); | |
253 ~QuicConnection() override; | |
254 | |
255 // Sets connection parameters from the supplied |config|. | |
256 void SetFromConfig(const QuicConfig& config); | |
257 | |
258 // Called by the Session when the client has provided CachedNetworkParameters. | |
259 // Returns true if this changes the initial connection state. | |
260 virtual bool ResumeConnectionState( | |
261 const CachedNetworkParameters& cached_network_params); | |
262 | |
263 // Sets the number of active streams on the connection for congestion control. | |
264 void SetNumOpenStreams(size_t num_streams); | |
265 | |
266 // Send the data in |data| to the peer in as few packets as possible. | |
267 // Returns a pair with the number of bytes consumed from data, and a boolean | |
268 // indicating if the fin bit was consumed. This does not indicate the data | |
269 // has been sent on the wire: it may have been turned into a packet and queued | |
270 // if the socket was unexpectedly blocked. |fec_protection| indicates if | |
271 // data is to be FEC protected. Note that data that is sent immediately | |
272 // following MUST_FEC_PROTECT data may get protected by falling within the | |
273 // same FEC group. | |
274 // If |delegate| is provided, then it will be informed once ACKs have been | |
275 // received for all the packets written in this call. | |
276 // The |delegate| is not owned by the QuicConnection and must outlive it. | |
277 QuicConsumedData SendStreamData(QuicStreamId id, | |
278 const IOVector& data, | |
279 QuicStreamOffset offset, | |
280 bool fin, | |
281 FecProtection fec_protection, | |
282 QuicAckNotifier::DelegateInterface* delegate); | |
283 | |
284 // Send a RST_STREAM frame to the peer. | |
285 virtual void SendRstStream(QuicStreamId id, | |
286 QuicRstStreamErrorCode error, | |
287 QuicStreamOffset bytes_written); | |
288 | |
289 // Send a BLOCKED frame to the peer. | |
290 virtual void SendBlocked(QuicStreamId id); | |
291 | |
292 // Send a WINDOW_UPDATE frame to the peer. | |
293 virtual void SendWindowUpdate(QuicStreamId id, | |
294 QuicStreamOffset byte_offset); | |
295 | |
296 // Sends the connection close packet without affecting the state of the | |
297 // connection. This should only be called if the session is actively being | |
298 // destroyed: otherwise call SendConnectionCloseWithDetails instead. | |
299 virtual void SendConnectionClosePacket(QuicErrorCode error, | |
300 const std::string& details); | |
301 | |
302 // Sends a connection close frame to the peer, and closes the connection by | |
303 // calling CloseConnection(notifying the visitor as it does so). | |
304 virtual void SendConnectionClose(QuicErrorCode error); | |
305 virtual void SendConnectionCloseWithDetails(QuicErrorCode error, | |
306 const std::string& details); | |
307 // Notifies the visitor of the close and marks the connection as disconnected. | |
308 void CloseConnection(QuicErrorCode error, bool from_peer) override; | |
309 virtual void SendGoAway(QuicErrorCode error, | |
310 QuicStreamId last_good_stream_id, | |
311 const std::string& reason); | |
312 | |
313 // Returns statistics tracked for this connection. | |
314 const QuicConnectionStats& GetStats(); | |
315 | |
316 // Processes an incoming UDP packet (consisting of a QuicEncryptedPacket) from | |
317 // the peer. If processing this packet permits a packet to be revived from | |
318 // its FEC group that packet will be revived and processed. | |
319 virtual void ProcessUdpPacket(const IPEndPoint& self_address, | |
320 const IPEndPoint& peer_address, | |
321 const QuicEncryptedPacket& packet); | |
322 | |
323 // QuicBlockedWriterInterface | |
324 // Called when the underlying connection becomes writable to allow queued | |
325 // writes to happen. | |
326 void OnCanWrite() override; | |
327 | |
328 // Called when an error occurs while attempting to write a packet to the | |
329 // network. | |
330 void OnWriteError(int error_code); | |
331 | |
332 // If the socket is not blocked, writes queued packets. | |
333 void WriteIfNotBlocked(); | |
334 | |
335 // The version of the protocol this connection is using. | |
336 QuicVersion version() const { return framer_.version(); } | |
337 | |
338 // The versions of the protocol that this connection supports. | |
339 const QuicVersionVector& supported_versions() const { | |
340 return framer_.supported_versions(); | |
341 } | |
342 | |
343 // From QuicFramerVisitorInterface | |
344 void OnError(QuicFramer* framer) override; | |
345 bool OnProtocolVersionMismatch(QuicVersion received_version) override; | |
346 void OnPacket() override; | |
347 void OnPublicResetPacket(const QuicPublicResetPacket& packet) override; | |
348 void OnVersionNegotiationPacket( | |
349 const QuicVersionNegotiationPacket& packet) override; | |
350 void OnRevivedPacket() override; | |
351 bool OnUnauthenticatedPublicHeader( | |
352 const QuicPacketPublicHeader& header) override; | |
353 bool OnUnauthenticatedHeader(const QuicPacketHeader& header) override; | |
354 void OnDecryptedPacket(EncryptionLevel level) override; | |
355 bool OnPacketHeader(const QuicPacketHeader& header) override; | |
356 void OnFecProtectedPayload(base::StringPiece payload) override; | |
357 bool OnStreamFrame(const QuicStreamFrame& frame) override; | |
358 bool OnAckFrame(const QuicAckFrame& frame) override; | |
359 bool OnStopWaitingFrame(const QuicStopWaitingFrame& frame) override; | |
360 bool OnPingFrame(const QuicPingFrame& frame) override; | |
361 bool OnRstStreamFrame(const QuicRstStreamFrame& frame) override; | |
362 bool OnConnectionCloseFrame(const QuicConnectionCloseFrame& frame) override; | |
363 bool OnGoAwayFrame(const QuicGoAwayFrame& frame) override; | |
364 bool OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame) override; | |
365 bool OnBlockedFrame(const QuicBlockedFrame& frame) override; | |
366 void OnFecData(const QuicFecData& fec) override; | |
367 void OnPacketComplete() override; | |
368 | |
369 // QuicPacketGenerator::DelegateInterface | |
370 bool ShouldGeneratePacket(TransmissionType transmission_type, | |
371 HasRetransmittableData retransmittable, | |
372 IsHandshake handshake) override; | |
373 void PopulateAckFrame(QuicAckFrame* ack) override; | |
374 void PopulateStopWaitingFrame(QuicStopWaitingFrame* stop_waiting) override; | |
375 void OnSerializedPacket(const SerializedPacket& packet) override; | |
376 | |
377 // QuicSentPacketManager::NetworkChangeVisitor | |
378 void OnCongestionWindowChange() override; | |
379 void OnRttChange() override; | |
380 | |
381 // Called by the crypto stream when the handshake completes. In the server's | |
382 // case this is when the SHLO has been ACKed. Clients call this on receipt of | |
383 // the SHLO. | |
384 void OnHandshakeComplete(); | |
385 | |
386 // Accessors | |
387 void set_visitor(QuicConnectionVisitorInterface* visitor) { | |
388 visitor_ = visitor; | |
389 } | |
390 // This method takes ownership of |debug_visitor|. | |
391 void set_debug_visitor(QuicConnectionDebugVisitor* debug_visitor) { | |
392 debug_visitor_.reset(debug_visitor); | |
393 packet_generator_.set_debug_delegate(debug_visitor); | |
394 sent_packet_manager_.set_debug_delegate(debug_visitor); | |
395 } | |
396 const IPEndPoint& self_address() const { return self_address_; } | |
397 const IPEndPoint& peer_address() const { return peer_address_; } | |
398 QuicConnectionId connection_id() const { return connection_id_; } | |
399 const QuicClock* clock() const { return clock_; } | |
400 QuicRandom* random_generator() const { return random_generator_; } | |
401 QuicByteCount max_packet_length() const; | |
402 void set_max_packet_length(QuicByteCount length); | |
403 | |
404 bool connected() const { return connected_; } | |
405 | |
406 // Must only be called on client connections. | |
407 const QuicVersionVector& server_supported_versions() const { | |
408 DCHECK(!is_server_); | |
409 return server_supported_versions_; | |
410 } | |
411 | |
412 size_t NumFecGroups() const { return group_map_.size(); } | |
413 | |
414 // Testing only. | |
415 size_t NumQueuedPackets() const { return queued_packets_.size(); } | |
416 | |
417 QuicEncryptedPacket* ReleaseConnectionClosePacket() { | |
418 return connection_close_packet_.release(); | |
419 } | |
420 | |
421 // Returns true if the underlying UDP socket is writable, there is | |
422 // no queued data and the connection is not congestion-control | |
423 // blocked. | |
424 bool CanWriteStreamData(); | |
425 | |
426 // Returns true if the connection has queued packets or frames. | |
427 bool HasQueuedData() const; | |
428 | |
429 // Sets the overall and idle state connection timeouts. | |
430 void SetNetworkTimeouts(QuicTime::Delta overall_timeout, | |
431 QuicTime::Delta idle_timeout); | |
432 | |
433 // If the connection has timed out, this will close the connection. | |
434 // Otherwise, it will reschedule the timeout alarm. | |
435 void CheckForTimeout(); | |
436 | |
437 // Sends a ping, and resets the ping alarm. | |
438 void SendPing(); | |
439 | |
440 // Sets up a packet with an QuicAckFrame and sends it out. | |
441 void SendAck(); | |
442 | |
443 // Called when an RTO fires. Resets the retransmission alarm if there are | |
444 // remaining unacked packets. | |
445 void OnRetransmissionTimeout(); | |
446 | |
447 // Called when a data packet is sent. Starts an alarm if the data sent in | |
448 // |sequence_number| was FEC protected. | |
449 void MaybeSetFecAlarm(QuicPacketSequenceNumber sequence_number); | |
450 | |
451 // Retransmits all unacked packets with retransmittable frames if | |
452 // |retransmission_type| is ALL_UNACKED_PACKETS, otherwise retransmits only | |
453 // initially encrypted packets. Used when the negotiated protocol version is | |
454 // different from what was initially assumed and when the initial encryption | |
455 // changes. | |
456 void RetransmitUnackedPackets(TransmissionType retransmission_type); | |
457 | |
458 // Calls |sent_packet_manager_|'s NeuterUnencryptedPackets. Used when the | |
459 // connection becomes forward secure and hasn't received acks for all packets. | |
460 void NeuterUnencryptedPackets(); | |
461 | |
462 // Changes the encrypter used for level |level| to |encrypter|. The function | |
463 // takes ownership of |encrypter|. | |
464 void SetEncrypter(EncryptionLevel level, QuicEncrypter* encrypter); | |
465 const QuicEncrypter* encrypter(EncryptionLevel level) const; | |
466 | |
467 // SetDefaultEncryptionLevel sets the encryption level that will be applied | |
468 // to new packets. | |
469 void SetDefaultEncryptionLevel(EncryptionLevel level); | |
470 | |
471 // SetDecrypter sets the primary decrypter, replacing any that already exists, | |
472 // and takes ownership. If an alternative decrypter is in place then the | |
473 // function DCHECKs. This is intended for cases where one knows that future | |
474 // packets will be using the new decrypter and the previous decrypter is now | |
475 // obsolete. |level| indicates the encryption level of the new decrypter. | |
476 void SetDecrypter(QuicDecrypter* decrypter, EncryptionLevel level); | |
477 | |
478 // SetAlternativeDecrypter sets a decrypter that may be used to decrypt | |
479 // future packets and takes ownership of it. |level| indicates the encryption | |
480 // level of the decrypter. If |latch_once_used| is true, then the first time | |
481 // that the decrypter is successful it will replace the primary decrypter. | |
482 // Otherwise both decrypters will remain active and the primary decrypter | |
483 // will be the one last used. | |
484 void SetAlternativeDecrypter(QuicDecrypter* decrypter, | |
485 EncryptionLevel level, | |
486 bool latch_once_used); | |
487 | |
488 const QuicDecrypter* decrypter() const; | |
489 const QuicDecrypter* alternative_decrypter() const; | |
490 | |
491 bool is_server() const { return is_server_; } | |
492 | |
493 // Allow easy overriding of truncated connection IDs. | |
494 void set_can_truncate_connection_ids(bool can) { | |
495 can_truncate_connection_ids_ = can; | |
496 } | |
497 | |
498 // Returns the underlying sent packet manager. | |
499 const QuicSentPacketManager& sent_packet_manager() const { | |
500 return sent_packet_manager_; | |
501 } | |
502 | |
503 bool CanWrite(HasRetransmittableData retransmittable); | |
504 | |
505 // Stores current batch state for connection, puts the connection | |
506 // into batch mode, and destruction restores the stored batch state. | |
507 // While the bundler is in scope, any generated frames are bundled | |
508 // as densely as possible into packets. In addition, this bundler | |
509 // can be configured to ensure that an ACK frame is included in the | |
510 // first packet created, if there's new ack information to be sent. | |
511 class ScopedPacketBundler { | |
512 public: | |
513 // In addition to all outgoing frames being bundled when the | |
514 // bundler is in scope, setting |include_ack| to true ensures that | |
515 // an ACK frame is opportunistically bundled with the first | |
516 // outgoing packet. | |
517 ScopedPacketBundler(QuicConnection* connection, AckBundling send_ack); | |
518 ~ScopedPacketBundler(); | |
519 | |
520 private: | |
521 QuicConnection* connection_; | |
522 bool already_in_batch_mode_; | |
523 }; | |
524 | |
525 QuicPacketSequenceNumber sequence_number_of_last_sent_packet() const { | |
526 return sequence_number_of_last_sent_packet_; | |
527 } | |
528 const QuicPacketWriter* writer() const { return writer_; } | |
529 | |
530 bool is_secure() const { return is_secure_; } | |
531 | |
532 protected: | |
533 // Packets which have not been written to the wire. | |
534 // Owns the QuicPacket* packet. | |
535 struct QueuedPacket { | |
536 QueuedPacket(SerializedPacket packet, | |
537 EncryptionLevel level); | |
538 QueuedPacket(SerializedPacket packet, | |
539 EncryptionLevel level, | |
540 TransmissionType transmission_type, | |
541 QuicPacketSequenceNumber original_sequence_number); | |
542 | |
543 SerializedPacket serialized_packet; | |
544 const EncryptionLevel encryption_level; | |
545 TransmissionType transmission_type; | |
546 // The packet's original sequence number if it is a retransmission. | |
547 // Otherwise it must be 0. | |
548 QuicPacketSequenceNumber original_sequence_number; | |
549 }; | |
550 | |
551 // Do any work which logically would be done in OnPacket but can not be | |
552 // safely done until the packet is validated. Returns true if the packet | |
553 // can be handled, false otherwise. | |
554 virtual bool ProcessValidatedPacket(); | |
555 | |
556 // Send a packet to the peer, and takes ownership of the packet if the packet | |
557 // cannot be written immediately. | |
558 virtual void SendOrQueuePacket(QueuedPacket packet); | |
559 | |
560 QuicConnectionHelperInterface* helper() { return helper_; } | |
561 | |
562 // Selects and updates the version of the protocol being used by selecting a | |
563 // version from |available_versions| which is also supported. Returns true if | |
564 // such a version exists, false otherwise. | |
565 bool SelectMutualVersion(const QuicVersionVector& available_versions); | |
566 | |
567 QuicPacketWriter* writer() { return writer_; } | |
568 | |
569 bool peer_port_changed() const { return peer_port_changed_; } | |
570 | |
571 private: | |
572 friend class test::QuicConnectionPeer; | |
573 friend class test::PacketSavingConnection; | |
574 | |
575 typedef std::list<QueuedPacket> QueuedPacketList; | |
576 typedef std::map<QuicFecGroupNumber, QuicFecGroup*> FecGroupMap; | |
577 | |
578 // Writes the given packet to socket, encrypted with packet's | |
579 // encryption_level. Returns true on successful write, and false if the writer | |
580 // was blocked and the write needs to be tried again. Notifies the | |
581 // SentPacketManager when the write is successful and sets | |
582 // retransmittable frames to nullptr. | |
583 // Saves the connection close packet for later transmission, even if the | |
584 // writer is write blocked. | |
585 bool WritePacket(QueuedPacket* packet); | |
586 | |
587 // Does the main work of WritePacket, but does not delete the packet or | |
588 // retransmittable frames upon success. | |
589 bool WritePacketInner(QueuedPacket* packet); | |
590 | |
591 // Make sure an ack we got from our peer is sane. | |
592 bool ValidateAckFrame(const QuicAckFrame& incoming_ack); | |
593 | |
594 // Make sure a stop waiting we got from our peer is sane. | |
595 bool ValidateStopWaitingFrame(const QuicStopWaitingFrame& stop_waiting); | |
596 | |
597 // Sends a version negotiation packet to the peer. | |
598 void SendVersionNegotiationPacket(); | |
599 | |
600 // Clears any accumulated frames from the last received packet. | |
601 void ClearLastFrames(); | |
602 | |
603 // Closes the connection if the sent or received packet manager are tracking | |
604 // too many outstanding packets. | |
605 void MaybeCloseIfTooManyOutstandingPackets(); | |
606 | |
607 // Writes as many queued packets as possible. The connection must not be | |
608 // blocked when this is called. | |
609 void WriteQueuedPackets(); | |
610 | |
611 // Writes as many pending retransmissions as possible. | |
612 void WritePendingRetransmissions(); | |
613 | |
614 // Returns true if the packet should be discarded and not sent. | |
615 bool ShouldDiscardPacket(const QueuedPacket& packet); | |
616 | |
617 // Queues |packet| in the hopes that it can be decrypted in the | |
618 // future, when a new key is installed. | |
619 void QueueUndecryptablePacket(const QuicEncryptedPacket& packet); | |
620 | |
621 // Attempts to process any queued undecryptable packets. | |
622 void MaybeProcessUndecryptablePackets(); | |
623 | |
624 // If a packet can be revived from the current FEC group, then | |
625 // revive and process the packet. | |
626 void MaybeProcessRevivedPacket(); | |
627 | |
628 void ProcessAckFrame(const QuicAckFrame& incoming_ack); | |
629 | |
630 void ProcessStopWaitingFrame(const QuicStopWaitingFrame& stop_waiting); | |
631 | |
632 // Queues an ack or sets the ack alarm when an incoming packet arrives that | |
633 // should be acked. | |
634 void MaybeQueueAck(); | |
635 | |
636 // Checks if the last packet should instigate an ack. | |
637 bool ShouldLastPacketInstigateAck() const; | |
638 | |
639 // Checks if the peer is waiting for packets that have been given up on, and | |
640 // therefore an ack frame should be sent with a larger least_unacked. | |
641 void UpdateStopWaitingCount(); | |
642 | |
643 // Sends any packets which are a response to the last packet, including both | |
644 // acks and pending writes if an ack opened the congestion window. | |
645 void MaybeSendInResponseToPacket(); | |
646 | |
647 // Gets the least unacked sequence number, which is the next sequence number | |
648 // to be sent if there are no outstanding packets. | |
649 QuicPacketSequenceNumber GetLeastUnacked() const; | |
650 | |
651 // Get the FEC group associate with the last processed packet or nullptr, if | |
652 // the group has already been deleted. | |
653 QuicFecGroup* GetFecGroup(); | |
654 | |
655 // Closes any FEC groups protecting packets before |sequence_number|. | |
656 void CloseFecGroupsBefore(QuicPacketSequenceNumber sequence_number); | |
657 | |
658 // Sets the timeout alarm to the appropriate value, if any. | |
659 void SetTimeoutAlarm(); | |
660 | |
661 // Sets the ping alarm to the appropriate value, if any. | |
662 void SetPingAlarm(); | |
663 | |
664 // On arrival of a new packet, checks to see if the socket addresses have | |
665 // changed since the last packet we saw on this connection. | |
666 void CheckForAddressMigration(const IPEndPoint& self_address, | |
667 const IPEndPoint& peer_address); | |
668 | |
669 HasRetransmittableData IsRetransmittable(const QueuedPacket& packet); | |
670 bool IsConnectionClose(const QueuedPacket& packet); | |
671 | |
672 QuicFramer framer_; | |
673 QuicConnectionHelperInterface* helper_; // Not owned. | |
674 QuicPacketWriter* writer_; // Owned or not depending on |owns_writer_|. | |
675 bool owns_writer_; | |
676 // Encryption level for new packets. Should only be changed via | |
677 // SetDefaultEncryptionLevel(). | |
678 EncryptionLevel encryption_level_; | |
679 bool has_forward_secure_encrypter_; | |
680 // The sequence number of the first packet which will be encrypted with the | |
681 // foward-secure encrypter, even if the peer has not started sending | |
682 // forward-secure packets. | |
683 QuicPacketSequenceNumber first_required_forward_secure_packet_; | |
684 const QuicClock* clock_; | |
685 QuicRandom* random_generator_; | |
686 | |
687 const QuicConnectionId connection_id_; | |
688 // Address on the last successfully processed packet received from the | |
689 // client. | |
690 IPEndPoint self_address_; | |
691 IPEndPoint peer_address_; | |
692 // Used to store latest peer port to possibly migrate to later. | |
693 uint16 migrating_peer_port_; | |
694 | |
695 // True if the last packet has gotten far enough in the framer to be | |
696 // decrypted. | |
697 bool last_packet_decrypted_; | |
698 bool last_packet_revived_; // True if the last packet was revived from FEC. | |
699 QuicByteCount last_size_; // Size of the last received packet. | |
700 EncryptionLevel last_decrypted_packet_level_; | |
701 QuicPacketHeader last_header_; | |
702 std::vector<QuicStreamFrame> last_stream_frames_; | |
703 std::vector<QuicAckFrame> last_ack_frames_; | |
704 std::vector<QuicStopWaitingFrame> last_stop_waiting_frames_; | |
705 std::vector<QuicRstStreamFrame> last_rst_frames_; | |
706 std::vector<QuicGoAwayFrame> last_goaway_frames_; | |
707 std::vector<QuicWindowUpdateFrame> last_window_update_frames_; | |
708 std::vector<QuicBlockedFrame> last_blocked_frames_; | |
709 std::vector<QuicPingFrame> last_ping_frames_; | |
710 std::vector<QuicConnectionCloseFrame> last_close_frames_; | |
711 | |
712 // Track some peer state so we can do less bookkeeping | |
713 // Largest sequence sent by the peer which had an ack frame (latest ack info). | |
714 QuicPacketSequenceNumber largest_seen_packet_with_ack_; | |
715 | |
716 // Largest sequence number sent by the peer which had a stop waiting frame. | |
717 QuicPacketSequenceNumber largest_seen_packet_with_stop_waiting_; | |
718 | |
719 // Collection of packets which were received before encryption was | |
720 // established, but which could not be decrypted. We buffer these on | |
721 // the assumption that they could not be processed because they were | |
722 // sent with the INITIAL encryption and the CHLO message was lost. | |
723 std::deque<QuicEncryptedPacket*> undecryptable_packets_; | |
724 | |
725 // Maximum number of undecryptable packets the connection will store. | |
726 size_t max_undecryptable_packets_; | |
727 | |
728 // When the version negotiation packet could not be sent because the socket | |
729 // was not writable, this is set to true. | |
730 bool pending_version_negotiation_packet_; | |
731 | |
732 // When packets could not be sent because the socket was not writable, | |
733 // they are added to this list. All corresponding frames are in | |
734 // unacked_packets_ if they are to be retransmitted. | |
735 QueuedPacketList queued_packets_; | |
736 | |
737 // Contains the connection close packet if the connection has been closed. | |
738 scoped_ptr<QuicEncryptedPacket> connection_close_packet_; | |
739 | |
740 // When true, the connection does not send a close packet on timeout. | |
741 bool silent_close_enabled_; | |
742 | |
743 FecGroupMap group_map_; | |
744 | |
745 QuicReceivedPacketManager received_packet_manager_; | |
746 QuicSentEntropyManager sent_entropy_manager_; | |
747 | |
748 // Indicates whether an ack should be sent the next time we try to write. | |
749 bool ack_queued_; | |
750 // Indicates how many consecutive packets have arrived without sending an ack. | |
751 QuicPacketCount num_packets_received_since_last_ack_sent_; | |
752 // Indicates how many consecutive times an ack has arrived which indicates | |
753 // the peer needs to stop waiting for some packets. | |
754 int stop_waiting_count_; | |
755 | |
756 // An alarm that fires when an ACK should be sent to the peer. | |
757 scoped_ptr<QuicAlarm> ack_alarm_; | |
758 // An alarm that fires when a packet needs to be retransmitted. | |
759 scoped_ptr<QuicAlarm> retransmission_alarm_; | |
760 // An alarm that is scheduled when the sent scheduler requires a | |
761 // a delay before sending packets and fires when the packet may be sent. | |
762 scoped_ptr<QuicAlarm> send_alarm_; | |
763 // An alarm that is scheduled when the connection can still write and there | |
764 // may be more data to send. | |
765 scoped_ptr<QuicAlarm> resume_writes_alarm_; | |
766 // An alarm that fires when the connection may have timed out. | |
767 scoped_ptr<QuicAlarm> timeout_alarm_; | |
768 // An alarm that fires when a ping should be sent. | |
769 scoped_ptr<QuicAlarm> ping_alarm_; | |
770 | |
771 QuicConnectionVisitorInterface* visitor_; | |
772 scoped_ptr<QuicConnectionDebugVisitor> debug_visitor_; | |
773 QuicPacketGenerator packet_generator_; | |
774 | |
775 // An alarm that fires when an FEC packet should be sent. | |
776 scoped_ptr<QuicAlarm> fec_alarm_; | |
777 | |
778 // Network idle time before we kill of this connection. | |
779 QuicTime::Delta idle_network_timeout_; | |
780 // Overall connection timeout. | |
781 QuicTime::Delta overall_connection_timeout_; | |
782 | |
783 // Statistics for this session. | |
784 QuicConnectionStats stats_; | |
785 | |
786 // The time that we got a packet for this connection. | |
787 // This is used for timeouts, and does not indicate the packet was processed. | |
788 QuicTime time_of_last_received_packet_; | |
789 | |
790 // The last time this connection began sending a new (non-retransmitted) | |
791 // packet. | |
792 QuicTime time_of_last_sent_new_packet_; | |
793 | |
794 // Sequence number of the last sent packet. Packets are guaranteed to be sent | |
795 // in sequence number order. | |
796 QuicPacketSequenceNumber sequence_number_of_last_sent_packet_; | |
797 | |
798 // Sent packet manager which tracks the status of packets sent by this | |
799 // connection and contains the send and receive algorithms to determine when | |
800 // to send packets. | |
801 QuicSentPacketManager sent_packet_manager_; | |
802 | |
803 // The state of connection in version negotiation finite state machine. | |
804 QuicVersionNegotiationState version_negotiation_state_; | |
805 | |
806 // Tracks if the connection was created by the server. | |
807 bool is_server_; | |
808 | |
809 // True by default. False if we've received or sent an explicit connection | |
810 // close. | |
811 bool connected_; | |
812 | |
813 // Set to true if the UDP packet headers have a new IP address for the peer. | |
814 // If true, do not perform connection migration. | |
815 bool peer_ip_changed_; | |
816 | |
817 // Set to true if the UDP packet headers have a new port for the peer. | |
818 // If true, and the IP has not changed, then we can migrate the connection. | |
819 bool peer_port_changed_; | |
820 | |
821 // Set to true if the UDP packet headers are addressed to a different IP. | |
822 // We do not support connection migration when the self IP changed. | |
823 bool self_ip_changed_; | |
824 | |
825 // Set to true if the UDP packet headers are addressed to a different port. | |
826 // We do not support connection migration when the self port changed. | |
827 bool self_port_changed_; | |
828 | |
829 // Set to false if the connection should not send truncated connection IDs to | |
830 // the peer, even if the peer supports it. | |
831 bool can_truncate_connection_ids_; | |
832 | |
833 // If non-empty this contains the set of versions received in a | |
834 // version negotiation packet. | |
835 QuicVersionVector server_supported_versions_; | |
836 | |
837 // True if this is a secure QUIC connection. | |
838 bool is_secure_; | |
839 | |
840 DISALLOW_COPY_AND_ASSIGN(QuicConnection); | |
841 }; | |
842 | |
843 } // namespace net | |
844 | |
845 #endif // NET_QUIC_QUIC_CONNECTION_H_ | |
OLD | NEW |