- add sources.
[platform/framework/web/crosswalk.git] / src / net / quic / quic_connection.cc
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 #include "net/quic/quic_connection.h"
6
7 #include <string.h>
8 #include <sys/types.h>
9 #include <algorithm>
10 #include <iterator>
11 #include <limits>
12 #include <memory>
13 #include <set>
14 #include <utility>
15
16 #include "base/logging.h"
17 #include "base/stl_util.h"
18 #include "net/quic/crypto/quic_decrypter.h"
19 #include "net/quic/crypto/quic_encrypter.h"
20 #include "net/quic/iovector.h"
21 #include "net/quic/quic_bandwidth.h"
22 #include "net/quic/quic_config.h"
23 #include "net/quic/quic_utils.h"
24
25 using base::hash_map;
26 using base::hash_set;
27 using base::StringPiece;
28 using std::list;
29 using std::make_pair;
30 using std::min;
31 using std::max;
32 using std::numeric_limits;
33 using std::vector;
34 using std::set;
35 using std::string;
36
37 int FLAGS_fake_packet_loss_percentage = 0;
38
39 // If true, then QUIC connections will bundle acks with any outgoing packet when
40 // an ack is being delayed. This is an optimization to reduce ack latency and
41 // packet count of pure ack packets.
42 bool FLAGS_bundle_ack_with_outgoing_packet = false;
43
44 namespace net {
45
46 class QuicDecrypter;
47 class QuicEncrypter;
48
49 namespace {
50
51 // The largest gap in packets we'll accept without closing the connection.
52 // This will likely have to be tuned.
53 const QuicPacketSequenceNumber kMaxPacketGap = 5000;
54
55 // We want to make sure if we get a nack packet which triggers several
56 // retransmissions, we don't queue up too many packets.  10 is TCP's default
57 // initial congestion window(RFC 6928).
58 const size_t kMaxRetransmissionsPerAck = kDefaultInitialWindow;
59
60 // TCP retransmits after 3 nacks.
61 // TODO(ianswett): Change to match TCP's rule of retransmitting once an ack
62 // at least 3 sequence numbers larger arrives.
63 const size_t kNumberOfNacksBeforeRetransmission = 3;
64
65 // Limit the number of FEC groups to two.  If we get enough out of order packets
66 // that this becomes limiting, we can revisit.
67 const size_t kMaxFecGroups = 2;
68
69 // Limit the number of undecryptable packets we buffer in
70 // expectation of the CHLO/SHLO arriving.
71 const size_t kMaxUndecryptablePackets = 10;
72
73 bool Near(QuicPacketSequenceNumber a, QuicPacketSequenceNumber b) {
74   QuicPacketSequenceNumber delta = (a > b) ? a - b : b - a;
75   return delta <= kMaxPacketGap;
76 }
77
78
79 // An alarm that is scheduled to send an ack if a timeout occurs.
80 class AckAlarm : public QuicAlarm::Delegate {
81  public:
82   explicit AckAlarm(QuicConnection* connection)
83       : connection_(connection) {
84   }
85
86   virtual QuicTime OnAlarm() OVERRIDE {
87     connection_->SendAck();
88     return QuicTime::Zero();
89   }
90
91  private:
92   QuicConnection* connection_;
93 };
94
95 // This alarm will be scheduled any time a data-bearing packet is sent out.
96 // When the alarm goes off, the connection checks to see if the oldest packets
97 // have been acked, and retransmit them if they have not.
98 class RetransmissionAlarm : public QuicAlarm::Delegate {
99  public:
100   explicit RetransmissionAlarm(QuicConnection* connection)
101       : connection_(connection) {
102   }
103
104   virtual QuicTime OnAlarm() OVERRIDE {
105     connection_->OnRetransmissionTimeout();
106     return QuicTime::Zero();
107   }
108
109  private:
110   QuicConnection* connection_;
111 };
112
113 // This alarm will be scheduled any time a FEC-bearing packet is sent out.
114 // When the alarm goes off, the connection checks to see if the oldest packets
115 // have been acked, and removes them from the congestion window if not.
116 class AbandonFecAlarm : public QuicAlarm::Delegate {
117  public:
118   explicit AbandonFecAlarm(QuicConnection* connection)
119       : connection_(connection) {
120   }
121
122   virtual QuicTime OnAlarm() OVERRIDE {
123     return connection_->OnAbandonFecTimeout();
124   }
125
126  private:
127   QuicConnection* connection_;
128 };
129
130 // An alarm that is scheduled when the sent scheduler requires a
131 // a delay before sending packets and fires when the packet may be sent.
132 class SendAlarm : public QuicAlarm::Delegate {
133  public:
134   explicit SendAlarm(QuicConnection* connection)
135       : connection_(connection) {
136   }
137
138   virtual QuicTime OnAlarm() OVERRIDE {
139     connection_->WriteIfNotBlocked();
140     // Never reschedule the alarm, since OnCanWrite does that.
141     return QuicTime::Zero();
142   }
143
144  private:
145   QuicConnection* connection_;
146 };
147
148 class TimeoutAlarm : public QuicAlarm::Delegate {
149  public:
150   explicit TimeoutAlarm(QuicConnection* connection)
151       : connection_(connection) {
152   }
153
154   virtual QuicTime OnAlarm() OVERRIDE {
155     connection_->CheckForTimeout();
156     // Never reschedule the alarm, since CheckForTimeout does that.
157     return QuicTime::Zero();
158   }
159
160  private:
161   QuicConnection* connection_;
162 };
163
164 // Indicates if any of the frames are intended to be sent with FORCE.
165 // Returns true when one of the frames is a CONNECTION_CLOSE_FRAME.
166 net::QuicConnection::Force HasForcedFrames(
167     const RetransmittableFrames* retransmittable_frames) {
168   if (!retransmittable_frames) {
169     return net::QuicConnection::NO_FORCE;
170   }
171   for (size_t i = 0; i < retransmittable_frames->frames().size(); ++i) {
172     if (retransmittable_frames->frames()[i].type == CONNECTION_CLOSE_FRAME) {
173       return net::QuicConnection::FORCE;
174     }
175   }
176   return net::QuicConnection::NO_FORCE;
177 }
178
179 net::IsHandshake HasCryptoHandshake(
180     const RetransmittableFrames* retransmittable_frames) {
181   if (!retransmittable_frames) {
182     return net::NOT_HANDSHAKE;
183   }
184   for (size_t i = 0; i < retransmittable_frames->frames().size(); ++i) {
185     if (retransmittable_frames->frames()[i].type == STREAM_FRAME &&
186         retransmittable_frames->frames()[i].stream_frame->stream_id ==
187             kCryptoStreamId) {
188       return net::IS_HANDSHAKE;
189     }
190   }
191   return net::NOT_HANDSHAKE;
192 }
193
194 }  // namespace
195
196 #define ENDPOINT (is_server_ ? "Server: " : " Client: ")
197
198 QuicConnection::QuicConnection(QuicGuid guid,
199                                IPEndPoint address,
200                                QuicConnectionHelperInterface* helper,
201                                QuicPacketWriter* writer,
202                                bool is_server,
203                                const QuicVersionVector& supported_versions)
204     : framer_(supported_versions,
205               helper->GetClock()->ApproximateNow(),
206               is_server),
207       helper_(helper),
208       writer_(writer),
209       encryption_level_(ENCRYPTION_NONE),
210       clock_(helper->GetClock()),
211       random_generator_(helper->GetRandomGenerator()),
212       guid_(guid),
213       peer_address_(address),
214       largest_seen_packet_with_ack_(0),
215       pending_version_negotiation_packet_(false),
216       write_blocked_(false),
217       ack_alarm_(helper->CreateAlarm(new AckAlarm(this))),
218       retransmission_alarm_(helper->CreateAlarm(new RetransmissionAlarm(this))),
219       abandon_fec_alarm_(helper->CreateAlarm(new AbandonFecAlarm(this))),
220       send_alarm_(helper->CreateAlarm(new SendAlarm(this))),
221       resume_writes_alarm_(helper->CreateAlarm(new SendAlarm(this))),
222       timeout_alarm_(helper->CreateAlarm(new TimeoutAlarm(this))),
223       debug_visitor_(NULL),
224       packet_creator_(guid_, &framer_, random_generator_, is_server),
225       packet_generator_(this, NULL, &packet_creator_),
226       idle_network_timeout_(
227           QuicTime::Delta::FromSeconds(kDefaultInitialTimeoutSecs)),
228       overall_connection_timeout_(QuicTime::Delta::Infinite()),
229       creation_time_(clock_->ApproximateNow()),
230       time_of_last_received_packet_(clock_->ApproximateNow()),
231       time_of_last_sent_packet_(clock_->ApproximateNow()),
232       sequence_number_of_last_inorder_packet_(0),
233       congestion_manager_(clock_, kTCP),
234       sent_packet_manager_(is_server, this),
235       version_negotiation_state_(START_NEGOTIATION),
236       consecutive_rto_count_(0),
237       is_server_(is_server),
238       connected_(true),
239       received_truncated_ack_(false),
240       address_migrating_(false) {
241   DLOG(INFO) << ENDPOINT << "Created connection with guid: " << guid;
242   timeout_alarm_->Set(clock_->ApproximateNow().Add(idle_network_timeout_));
243   framer_.set_visitor(this);
244   framer_.set_received_entropy_calculator(&received_packet_manager_);
245 }
246
247 QuicConnection::~QuicConnection() {
248   STLDeleteElements(&undecryptable_packets_);
249   STLDeleteValues(&group_map_);
250   for (QueuedPacketList::iterator it = queued_packets_.begin();
251        it != queued_packets_.end(); ++it) {
252     delete it->packet;
253   }
254 }
255
256 void QuicConnection::SetFromConfig(const QuicConfig& config) {
257   DCHECK_LT(0u, config.server_max_packet_size());
258   DCHECK_LT(0u, config.server_initial_congestion_window());
259   SetIdleNetworkTimeout(config.idle_connection_state_lifetime());
260   // Set the max packet length only when QUIC_VERSION_12 or later is supported,
261   // with explicitly truncated acks.
262   if (version() > QUIC_VERSION_11) {
263     options()->max_packet_length = config.server_max_packet_size();
264   }
265   congestion_manager_.SetFromConfig(config, is_server_);
266   // TODO(satyamshekhar): Set congestion control and ICSL also.
267 }
268
269 bool QuicConnection::SelectMutualVersion(
270     const QuicVersionVector& available_versions) {
271   // Try to find the highest mutual version by iterating over supported
272   // versions, starting with the highest, and breaking out of the loop once we
273   // find a matching version in the provided available_versions vector.
274   const QuicVersionVector& supported_versions = framer_.supported_versions();
275   for (size_t i = 0; i < supported_versions.size(); ++i) {
276     const QuicVersion& version = supported_versions[i];
277     if (std::find(available_versions.begin(), available_versions.end(),
278                   version) != available_versions.end()) {
279       framer_.set_version(version);
280       return true;
281     }
282   }
283
284   return false;
285 }
286
287 void QuicConnection::OnError(QuicFramer* framer) {
288   // Packets that we cannot decrypt are dropped.
289   // TODO(rch): add stats to measure this.
290   if (!connected_ || framer->error() == QUIC_DECRYPTION_FAILURE) {
291     return;
292   }
293   SendConnectionClose(framer->error());
294 }
295
296 void QuicConnection::OnPacket() {
297   DCHECK(last_stream_frames_.empty() &&
298          last_goaway_frames_.empty() &&
299          last_rst_frames_.empty() &&
300          last_ack_frames_.empty() &&
301          last_congestion_frames_.empty());
302 }
303
304 void QuicConnection::OnPublicResetPacket(
305     const QuicPublicResetPacket& packet) {
306   if (debug_visitor_) {
307     debug_visitor_->OnPublicResetPacket(packet);
308   }
309   CloseConnection(QUIC_PUBLIC_RESET, true);
310 }
311
312 bool QuicConnection::OnProtocolVersionMismatch(QuicVersion received_version) {
313   DLOG(INFO) << ENDPOINT << "Received packet with mismatched version "
314              << received_version;
315   // TODO(satyamshekhar): Implement no server state in this mode.
316   if (!is_server_) {
317     LOG(DFATAL) << ENDPOINT << "Framer called OnProtocolVersionMismatch. "
318                 << "Closing connection.";
319     CloseConnection(QUIC_INTERNAL_ERROR, false);
320     return false;
321   }
322   DCHECK_NE(version(), received_version);
323
324   if (debug_visitor_) {
325     debug_visitor_->OnProtocolVersionMismatch(received_version);
326   }
327
328   switch (version_negotiation_state_) {
329     case START_NEGOTIATION:
330       if (!framer_.IsSupportedVersion(received_version)) {
331         SendVersionNegotiationPacket();
332         version_negotiation_state_ = NEGOTIATION_IN_PROGRESS;
333         return false;
334       }
335       break;
336
337     case NEGOTIATION_IN_PROGRESS:
338       if (!framer_.IsSupportedVersion(received_version)) {
339         SendVersionNegotiationPacket();
340         return false;
341       }
342       break;
343
344     case NEGOTIATED_VERSION:
345       // Might be old packets that were sent by the client before the version
346       // was negotiated. Drop these.
347       return false;
348
349     default:
350       DCHECK(false);
351   }
352
353   version_negotiation_state_ = NEGOTIATED_VERSION;
354   visitor_->OnSuccessfulVersionNegotiation(received_version);
355   DLOG(INFO) << ENDPOINT << "version negotiated " << received_version;
356
357   // Store the new version.
358   framer_.set_version(received_version);
359
360   // TODO(satyamshekhar): Store the sequence number of this packet and close the
361   // connection if we ever received a packet with incorrect version and whose
362   // sequence number is greater.
363   return true;
364 }
365
366 // Handles version negotiation for client connection.
367 void QuicConnection::OnVersionNegotiationPacket(
368     const QuicVersionNegotiationPacket& packet) {
369   if (is_server_) {
370     LOG(DFATAL) << ENDPOINT << "Framer parsed VersionNegotiationPacket."
371                 << " Closing connection.";
372     CloseConnection(QUIC_INTERNAL_ERROR, false);
373     return;
374   }
375   if (debug_visitor_) {
376     debug_visitor_->OnVersionNegotiationPacket(packet);
377   }
378
379   if (version_negotiation_state_ != START_NEGOTIATION) {
380     // Possibly a duplicate version negotiation packet.
381     return;
382   }
383
384   if (std::find(packet.versions.begin(),
385                 packet.versions.end(), version()) !=
386       packet.versions.end()) {
387     DLOG(WARNING) << ENDPOINT << "The server already supports our version. "
388                   << "It should have accepted our connection.";
389     // Just drop the connection.
390     CloseConnection(QUIC_INVALID_VERSION_NEGOTIATION_PACKET, false);
391     return;
392   }
393
394   if (!SelectMutualVersion(packet.versions)) {
395     SendConnectionCloseWithDetails(QUIC_INVALID_VERSION,
396                                    "no common version found");
397     return;
398   }
399
400   DLOG(INFO) << ENDPOINT << "negotiating version " << version();
401   version_negotiation_state_ = NEGOTIATION_IN_PROGRESS;
402   RetransmitUnackedPackets(ALL_PACKETS);
403 }
404
405 void QuicConnection::OnRevivedPacket() {
406 }
407
408 bool QuicConnection::OnPacketHeader(const QuicPacketHeader& header) {
409   if (debug_visitor_) {
410     debug_visitor_->OnPacketHeader(header);
411   }
412
413   if (!ProcessValidatedPacket()) {
414     return false;
415   }
416
417   // Will be decrement below if we fall through to return true;
418   ++stats_.packets_dropped;
419
420   if (header.public_header.guid != guid_) {
421     DLOG(INFO) << ENDPOINT << "Ignoring packet from unexpected GUID: "
422                << header.public_header.guid << " instead of " << guid_;
423     return false;
424   }
425
426   if (!Near(header.packet_sequence_number,
427             last_header_.packet_sequence_number)) {
428     DLOG(INFO) << ENDPOINT << "Packet " << header.packet_sequence_number
429                << " out of bounds.  Discarding";
430     SendConnectionCloseWithDetails(QUIC_INVALID_PACKET_HEADER,
431                                    "Packet sequence number out of bounds");
432     return false;
433   }
434
435   // If this packet has already been seen, or that the sender
436   // has told us will not be retransmitted, then stop processing the packet.
437   if (!received_packet_manager_.IsAwaitingPacket(
438           header.packet_sequence_number)) {
439     return false;
440   }
441
442   if (version_negotiation_state_ != NEGOTIATED_VERSION) {
443     if (is_server_) {
444       if (!header.public_header.version_flag) {
445         DLOG(WARNING) << ENDPOINT << "Got packet without version flag before "
446                       << "version negotiated.";
447         // Packets should have the version flag till version negotiation is
448         // done.
449         CloseConnection(QUIC_INVALID_VERSION, false);
450         return false;
451       } else {
452         DCHECK_EQ(1u, header.public_header.versions.size());
453         DCHECK_EQ(header.public_header.versions[0], version());
454         version_negotiation_state_ = NEGOTIATED_VERSION;
455         visitor_->OnSuccessfulVersionNegotiation(version());
456       }
457     } else {
458       DCHECK(!header.public_header.version_flag);
459       // If the client gets a packet without the version flag from the server
460       // it should stop sending version since the version negotiation is done.
461       packet_creator_.StopSendingVersion();
462       version_negotiation_state_ = NEGOTIATED_VERSION;
463       visitor_->OnSuccessfulVersionNegotiation(version());
464     }
465   }
466
467   DCHECK_EQ(NEGOTIATED_VERSION, version_negotiation_state_);
468
469   --stats_.packets_dropped;
470   DVLOG(1) << ENDPOINT << "Received packet header: " << header;
471   last_header_ = header;
472   DCHECK(connected_);
473   return true;
474 }
475
476 void QuicConnection::OnFecProtectedPayload(StringPiece payload) {
477   DCHECK_EQ(IN_FEC_GROUP, last_header_.is_in_fec_group);
478   DCHECK_NE(0u, last_header_.fec_group);
479   QuicFecGroup* group = GetFecGroup();
480   if (group != NULL) {
481     group->Update(last_header_, payload);
482   }
483 }
484
485 bool QuicConnection::OnStreamFrame(const QuicStreamFrame& frame) {
486   DCHECK(connected_);
487   if (debug_visitor_) {
488     debug_visitor_->OnStreamFrame(frame);
489   }
490   last_stream_frames_.push_back(frame);
491   return true;
492 }
493
494 bool QuicConnection::OnAckFrame(const QuicAckFrame& incoming_ack) {
495   DCHECK(connected_);
496   if (debug_visitor_) {
497     debug_visitor_->OnAckFrame(incoming_ack);
498   }
499   DVLOG(1) << ENDPOINT << "OnAckFrame: " << incoming_ack;
500
501   if (last_header_.packet_sequence_number <= largest_seen_packet_with_ack_) {
502     DLOG(INFO) << ENDPOINT << "Received an old ack frame: ignoring";
503     return true;
504   }
505
506   if (!ValidateAckFrame(incoming_ack)) {
507     SendConnectionClose(QUIC_INVALID_ACK_DATA);
508     return false;
509   }
510
511   last_ack_frames_.push_back(incoming_ack);
512   return connected_;
513 }
514
515 void QuicConnection::ProcessAckFrame(const QuicAckFrame& incoming_ack) {
516   // Latch current least unacked sequence number. This allows us to reset the
517   // retransmission and FEC abandonment timers conditionally below.
518   QuicPacketSequenceNumber least_unacked_sent_before =
519       sent_packet_manager_.GetLeastUnackedSentPacket();
520
521   largest_seen_packet_with_ack_ = last_header_.packet_sequence_number;
522
523   received_truncated_ack_ = version() <= QUIC_VERSION_11 ?
524       incoming_ack.received_info.missing_packets.size() >=
525           QuicFramer::GetMaxUnackedPackets(last_header_) :
526       incoming_ack.received_info.is_truncated;
527
528   received_packet_manager_.UpdatePacketInformationReceivedByPeer(incoming_ack);
529   received_packet_manager_.UpdatePacketInformationSentByPeer(incoming_ack);
530   // Possibly close any FecGroups which are now irrelevant.
531   CloseFecGroupsBefore(incoming_ack.sent_info.least_unacked + 1);
532
533   sent_entropy_manager_.ClearEntropyBefore(
534       received_packet_manager_.least_packet_awaited_by_peer() - 1);
535
536   retransmitted_nacked_packet_count_ = 0;
537   sent_packet_manager_.OnIncomingAck(incoming_ack.received_info,
538                                      received_truncated_ack_);
539
540   // Get the updated least unacked sequence number.
541   QuicPacketSequenceNumber least_unacked_sent_after =
542       sent_packet_manager_.GetLeastUnackedSentPacket();
543
544   // Used to set RTO and FEC alarms.
545   QuicTime::Delta retransmission_delay =
546       congestion_manager_.GetRetransmissionDelay(
547           sent_packet_manager_.GetNumUnackedPackets(), 0);
548
549   // If there are outstanding packets, and the least unacked sequence number
550   // has increased after processing this latest AckFrame, then reschedule the
551   // retransmission timer.
552   if (sent_packet_manager_.HasUnackedPackets() &&
553       least_unacked_sent_before < least_unacked_sent_after) {
554     if (retransmission_alarm_->IsSet()) {
555       retransmission_alarm_->Cancel();
556     }
557     retransmission_alarm_->Set(
558         clock_->ApproximateNow().Add(retransmission_delay));
559     consecutive_rto_count_ = 0;
560   } else if (!sent_packet_manager_.HasUnackedPackets()) {
561     retransmission_alarm_->Cancel();
562   }
563
564   // If there are outstanding FEC packets, and the least unacked sequence number
565   // has increased after processing this latest AckFrame, then reschedule the
566   // abandon FEC timer.
567   abandon_fec_alarm_->Cancel();
568   if (sent_packet_manager_.HasUnackedFecPackets() &&
569       least_unacked_sent_before < least_unacked_sent_after) {
570     abandon_fec_alarm_->Set(clock_->ApproximateNow().Add(retransmission_delay));
571   }
572
573   congestion_manager_.OnIncomingAckFrame(incoming_ack,
574                                          time_of_last_received_packet_);
575 }
576
577 bool QuicConnection::OnCongestionFeedbackFrame(
578     const QuicCongestionFeedbackFrame& feedback) {
579   DCHECK(connected_);
580   if (debug_visitor_) {
581     debug_visitor_->OnCongestionFeedbackFrame(feedback);
582   }
583   last_congestion_frames_.push_back(feedback);
584   return connected_;
585 }
586
587 bool QuicConnection::ValidateAckFrame(const QuicAckFrame& incoming_ack) {
588   if (incoming_ack.received_info.largest_observed >
589       packet_creator_.sequence_number()) {
590     DLOG(ERROR) << ENDPOINT << "Peer's observed unsent packet:"
591                 << incoming_ack.received_info.largest_observed << " vs "
592                 << packet_creator_.sequence_number();
593     // We got an error for data we have not sent.  Error out.
594     return false;
595   }
596
597   if (incoming_ack.received_info.largest_observed <
598           received_packet_manager_.peer_largest_observed_packet()) {
599     DLOG(ERROR) << ENDPOINT << "Peer's largest_observed packet decreased:"
600                 << incoming_ack.received_info.largest_observed << " vs "
601                 << received_packet_manager_.peer_largest_observed_packet();
602     // A new ack has a diminished largest_observed value.  Error out.
603     // If this was an old packet, we wouldn't even have checked.
604     return false;
605   }
606
607   // We can't have too many unacked packets, or our ack frames go over
608   // kMaxPacketSize.
609   if (version() <= QUIC_VERSION_11) {
610     DCHECK_LE(incoming_ack.received_info.missing_packets.size(),
611               QuicFramer::GetMaxUnackedPackets(last_header_));
612   }
613
614   if (incoming_ack.sent_info.least_unacked <
615       received_packet_manager_.peer_least_packet_awaiting_ack()) {
616     DLOG(ERROR) << ENDPOINT << "Peer's sent low least_unacked: "
617                 << incoming_ack.sent_info.least_unacked << " vs "
618                 << received_packet_manager_.peer_least_packet_awaiting_ack();
619     // We never process old ack frames, so this number should only increase.
620     return false;
621   }
622
623   if (incoming_ack.sent_info.least_unacked >
624       last_header_.packet_sequence_number) {
625     DLOG(ERROR) << ENDPOINT << "Peer sent least_unacked:"
626                 << incoming_ack.sent_info.least_unacked
627                 << " greater than the enclosing packet sequence number:"
628                 << last_header_.packet_sequence_number;
629     return false;
630   }
631
632   if (!incoming_ack.received_info.missing_packets.empty() &&
633       *incoming_ack.received_info.missing_packets.rbegin() >
634       incoming_ack.received_info.largest_observed) {
635     DLOG(ERROR) << ENDPOINT << "Peer sent missing packet: "
636                 << *incoming_ack.received_info.missing_packets.rbegin()
637                 << " which is greater than largest observed: "
638                 << incoming_ack.received_info.largest_observed;
639     return false;
640   }
641
642   if (!incoming_ack.received_info.missing_packets.empty() &&
643       *incoming_ack.received_info.missing_packets.begin() <
644       received_packet_manager_.least_packet_awaited_by_peer()) {
645     DLOG(ERROR) << ENDPOINT << "Peer sent missing packet: "
646                 << *incoming_ack.received_info.missing_packets.begin()
647                 << " which is smaller than least_packet_awaited_by_peer_: "
648                 << received_packet_manager_.least_packet_awaited_by_peer();
649     return false;
650   }
651
652   if (!sent_entropy_manager_.IsValidEntropy(
653           incoming_ack.received_info.largest_observed,
654           incoming_ack.received_info.missing_packets,
655           incoming_ack.received_info.entropy_hash)) {
656     DLOG(ERROR) << ENDPOINT << "Peer sent invalid entropy.";
657     return false;
658   }
659
660   return true;
661 }
662
663 void QuicConnection::OnFecData(const QuicFecData& fec) {
664   DCHECK_EQ(IN_FEC_GROUP, last_header_.is_in_fec_group);
665   DCHECK_NE(0u, last_header_.fec_group);
666   QuicFecGroup* group = GetFecGroup();
667   if (group != NULL) {
668     group->UpdateFec(last_header_.packet_sequence_number,
669                      last_header_.entropy_flag, fec);
670   }
671 }
672
673 bool QuicConnection::OnRstStreamFrame(const QuicRstStreamFrame& frame) {
674   DCHECK(connected_);
675   if (debug_visitor_) {
676     debug_visitor_->OnRstStreamFrame(frame);
677   }
678   DLOG(INFO) << ENDPOINT << "Stream reset with error "
679              << QuicUtils::StreamErrorToString(frame.error_code);
680   last_rst_frames_.push_back(frame);
681   return connected_;
682 }
683
684 bool QuicConnection::OnConnectionCloseFrame(
685     const QuicConnectionCloseFrame& frame) {
686   DCHECK(connected_);
687   if (debug_visitor_) {
688     debug_visitor_->OnConnectionCloseFrame(frame);
689   }
690   DLOG(INFO) << ENDPOINT << "Connection " << guid() << " closed with error "
691              << QuicUtils::ErrorToString(frame.error_code)
692              << " " << frame.error_details;
693   last_close_frames_.push_back(frame);
694   return connected_;
695 }
696
697 bool QuicConnection::OnGoAwayFrame(const QuicGoAwayFrame& frame) {
698   DCHECK(connected_);
699   DLOG(INFO) << ENDPOINT << "Go away received with error "
700              << QuicUtils::ErrorToString(frame.error_code)
701              << " and reason:" << frame.reason_phrase;
702   last_goaway_frames_.push_back(frame);
703   return connected_;
704 }
705
706 void QuicConnection::OnPacketComplete() {
707   // Don't do anything if this packet closed the connection.
708   if (!connected_) {
709     ClearLastFrames();
710     return;
711   }
712
713   DLOG(INFO) << ENDPOINT << (last_packet_revived_ ? "Revived" : "Got")
714              << " packet " << last_header_.packet_sequence_number
715              << " with " << last_ack_frames_.size() << " acks, "
716              << last_congestion_frames_.size() << " congestions, "
717              << last_goaway_frames_.size() << " goaways, "
718              << last_rst_frames_.size() << " rsts, "
719              << last_stream_frames_.size()
720              << " stream frames for " << last_header_.public_header.guid;
721   if (!last_packet_revived_) {
722     congestion_manager_.RecordIncomingPacket(
723         last_size_, last_header_.packet_sequence_number,
724         time_of_last_received_packet_, last_packet_revived_);
725   }
726
727   // Must called before ack processing, because processing acks removes entries
728   // from unacket_packets_, increasing the least_unacked.
729   const bool last_packet_should_instigate_ack = ShouldLastPacketInstigateAck();
730
731   // If we are missing any packets from the peer, then we want to ack
732   // immediately.  We need to check both before and after we process the
733   // current packet because we want to ack immediately when we discover
734   // a missing packet AND when we receive the last missing packet.
735   bool send_ack_immediately =
736       received_packet_manager_.GetNumMissingPackets() != 0;
737
738   // Ensure the visitor can process the stream frames before recording and
739   // processing the rest of the packet.
740   if (last_stream_frames_.empty() ||
741       visitor_->OnStreamFrames(last_stream_frames_)) {
742     received_packet_manager_.RecordPacketReceived(
743         last_header_, time_of_last_received_packet_);
744     for (size_t i = 0; i < last_stream_frames_.size(); ++i) {
745       stats_.stream_bytes_received += last_stream_frames_[i].data.length();
746     }
747   }
748
749   // Process stream resets, then acks, then congestion feedback.
750   for (size_t i = 0; i < last_goaway_frames_.size(); ++i) {
751     visitor_->OnGoAway(last_goaway_frames_[i]);
752   }
753   for (size_t i = 0; i < last_rst_frames_.size(); ++i) {
754     visitor_->OnRstStream(last_rst_frames_[i]);
755   }
756   for (size_t i = 0; i < last_ack_frames_.size(); ++i) {
757     ProcessAckFrame(last_ack_frames_[i]);
758   }
759   for (size_t i = 0; i < last_congestion_frames_.size(); ++i) {
760     congestion_manager_.OnIncomingQuicCongestionFeedbackFrame(
761         last_congestion_frames_[i], time_of_last_received_packet_);
762   }
763   if (!last_close_frames_.empty()) {
764     CloseConnection(last_close_frames_[0].error_code, true);
765     DCHECK(!connected_);
766   }
767
768   if (received_packet_manager_.GetNumMissingPackets() != 0) {
769     send_ack_immediately = true;
770   }
771
772   MaybeSendInResponseToPacket(send_ack_immediately,
773                               last_packet_should_instigate_ack);
774
775   ClearLastFrames();
776 }
777
778 void QuicConnection::ClearLastFrames() {
779   last_stream_frames_.clear();
780   last_goaway_frames_.clear();
781   last_rst_frames_.clear();
782   last_ack_frames_.clear();
783   last_congestion_frames_.clear();
784 }
785
786 QuicAckFrame* QuicConnection::CreateAckFrame() {
787   QuicAckFrame* outgoing_ack = new QuicAckFrame();
788   received_packet_manager_.UpdateReceivedPacketInfo(
789       &(outgoing_ack->received_info), clock_->ApproximateNow());
790   UpdateSentPacketInfo(&(outgoing_ack->sent_info));
791   DVLOG(1) << ENDPOINT << "Creating ack frame: " << *outgoing_ack;
792   return outgoing_ack;
793 }
794
795 QuicCongestionFeedbackFrame* QuicConnection::CreateFeedbackFrame() {
796   return new QuicCongestionFeedbackFrame(outgoing_congestion_feedback_);
797 }
798
799 bool QuicConnection::ShouldLastPacketInstigateAck() {
800   if (!last_stream_frames_.empty() ||
801       !last_goaway_frames_.empty() ||
802       !last_rst_frames_.empty()) {
803     return true;
804   }
805
806   // If the peer is still waiting for a packet that we are no
807   // longer planning to send, we should send an ack to raise
808   // the high water mark.
809   if (!last_ack_frames_.empty() &&
810       !last_ack_frames_.back().received_info.missing_packets.empty()) {
811     return sent_packet_manager_.GetLeastUnackedSentPacket() >
812         *last_ack_frames_.back().received_info.missing_packets.begin();
813   }
814   return false;
815 }
816
817 void QuicConnection::MaybeSendInResponseToPacket(
818     bool send_ack_immediately,
819     bool last_packet_should_instigate_ack) {
820   // |include_ack| is false since we decide about ack bundling below.
821   ScopedPacketBundler bundler(this, false);
822
823   if (last_packet_should_instigate_ack) {
824     // In general, we ack every second packet.  When we don't ack the first
825     // packet, we set the delayed ack alarm.  Thus, if the ack alarm is set
826     // then we know this is the second packet, and we should send an ack.
827     if (send_ack_immediately || ack_alarm_->IsSet()) {
828       SendAck();
829       DCHECK(!ack_alarm_->IsSet());
830     } else {
831       ack_alarm_->Set(clock_->ApproximateNow().Add(
832           congestion_manager_.DelayedAckTime()));
833       DVLOG(1) << "Ack timer set; next packet or timer will trigger ACK.";
834     }
835   }
836
837   if (!last_ack_frames_.empty()) {
838     // Now the we have received an ack, we might be able to send packets which
839     // are queued locally, or drain streams which are blocked.
840     QuicTime::Delta delay = congestion_manager_.TimeUntilSend(
841         time_of_last_received_packet_, NOT_RETRANSMISSION,
842         HAS_RETRANSMITTABLE_DATA, NOT_HANDSHAKE);
843     if (delay.IsZero()) {
844       send_alarm_->Cancel();
845       WriteIfNotBlocked();
846     } else if (!delay.IsInfinite()) {
847       send_alarm_->Cancel();
848       send_alarm_->Set(time_of_last_received_packet_.Add(delay));
849     }
850   }
851 }
852
853 void QuicConnection::SendVersionNegotiationPacket() {
854   scoped_ptr<QuicEncryptedPacket> version_packet(
855       packet_creator_.SerializeVersionNegotiationPacket(
856           framer_.supported_versions()));
857   // TODO(satyamshekhar): implement zero server state negotiation.
858   WriteResult result =
859       writer_->WritePacket(version_packet->data(), version_packet->length(),
860                            self_address().address(), peer_address(), this);
861   if (result.status == WRITE_STATUS_BLOCKED) {
862     write_blocked_ = true;
863   }
864   if (result.status == WRITE_STATUS_OK ||
865       (result.status == WRITE_STATUS_BLOCKED &&
866        writer_->IsWriteBlockedDataBuffered())) {
867     pending_version_negotiation_packet_ = false;
868     return;
869   }
870   if (result.status == WRITE_STATUS_ERROR) {
871     // We can't send an error as the socket is presumably borked.
872     CloseConnection(QUIC_PACKET_WRITE_ERROR, false);
873   }
874   pending_version_negotiation_packet_ = true;
875 }
876
877 QuicConsumedData QuicConnection::SendStreamDataInner(
878     QuicStreamId id,
879     const IOVector& data,
880     QuicStreamOffset offset,
881     bool fin,
882     QuicAckNotifier* notifier) {
883   // TODO(ianswett): Further improve sending by passing the iovec down
884   // instead of batching into multiple stream frames in a single packet.
885
886   // Opportunistically bundle an ack with this outgoing packet.
887   ScopedPacketBundler ack_bundler(this, true);
888   size_t bytes_written = 0;
889   bool fin_consumed = false;
890
891   for (size_t i = 0; i < data.Size(); ++i) {
892     bool send_fin = fin && (i == data.Size() - 1);
893     if (!send_fin && data.iovec()[i].iov_len == 0) {
894       LOG(DFATAL) << "Attempt to send empty stream frame";
895     }
896
897     StringPiece data_piece(static_cast<char*>(data.iovec()[i].iov_base),
898                            data.iovec()[i].iov_len);
899     int currentOffset = offset + bytes_written;
900     QuicConsumedData consumed_data =
901         packet_generator_.ConsumeData(id,
902                                       data_piece,
903                                       currentOffset,
904                                       send_fin,
905                                       notifier);
906
907     DCHECK_LE(consumed_data.bytes_consumed, numeric_limits<uint32>::max());
908     bytes_written += consumed_data.bytes_consumed;
909     fin_consumed = consumed_data.fin_consumed;
910     // If no bytes were consumed, bail now, because the stream can not write
911     // more data.
912     if (consumed_data.bytes_consumed < data.iovec()[i].iov_len) {
913       break;
914     }
915   }
916   // Handle the 0 byte write properly.
917   if (data.Empty()) {
918     DCHECK(fin);
919     QuicConsumedData consumed_data = packet_generator_.ConsumeData(
920         id, StringPiece(), offset, fin, NULL);
921     fin_consumed = consumed_data.fin_consumed;
922   }
923
924   stats_.stream_bytes_sent += bytes_written;
925   return QuicConsumedData(bytes_written, fin_consumed);
926 }
927
928 QuicConsumedData QuicConnection::SendStreamData(QuicStreamId id,
929                                                 const IOVector& data,
930                                                 QuicStreamOffset offset,
931                                                 bool fin) {
932   return SendStreamDataInner(id, data, offset, fin, NULL);
933 }
934
935 QuicConsumedData QuicConnection::SendStreamDataAndNotifyWhenAcked(
936     QuicStreamId id,
937     const IOVector& data,
938     QuicStreamOffset offset,
939     bool fin,
940     QuicAckNotifier::DelegateInterface* delegate) {
941   if (!fin && data.Empty()) {
942     LOG(DFATAL) << "Attempt to send empty stream frame";
943   }
944
945   // This notifier will be owned by the AckNotifierManager (or deleted below if
946   // no data was consumed).
947   QuicAckNotifier* notifier = new QuicAckNotifier(delegate);
948   QuicConsumedData consumed_data =
949       SendStreamDataInner(id, data, offset, fin, notifier);
950
951   if (consumed_data.bytes_consumed == 0) {
952     // No data was consumed, delete the notifier.
953     delete notifier;
954   }
955
956   return consumed_data;
957 }
958
959 void QuicConnection::SendRstStream(QuicStreamId id,
960                                    QuicRstStreamErrorCode error) {
961   LOG(INFO) << "Sending RST_STREAM: " << id << " code: " << error;
962   // Opportunistically bundle an ack with this outgoing packet.
963   ScopedPacketBundler ack_bundler(this, true);
964   packet_generator_.AddControlFrame(
965       QuicFrame(new QuicRstStreamFrame(id, error)));
966 }
967
968 const QuicConnectionStats& QuicConnection::GetStats() {
969   // Update rtt and estimated bandwidth.
970   stats_.rtt = congestion_manager_.SmoothedRtt().ToMicroseconds();
971   stats_.estimated_bandwidth =
972       congestion_manager_.BandwidthEstimate().ToBytesPerSecond();
973   return stats_;
974 }
975
976 void QuicConnection::ProcessUdpPacket(const IPEndPoint& self_address,
977                                       const IPEndPoint& peer_address,
978                                       const QuicEncryptedPacket& packet) {
979   if (!connected_) {
980     return;
981   }
982   if (debug_visitor_) {
983     debug_visitor_->OnPacketReceived(self_address, peer_address, packet);
984   }
985   last_packet_revived_ = false;
986   last_size_ = packet.length();
987
988   address_migrating_ = false;
989
990   if (peer_address_.address().empty()) {
991     peer_address_ = peer_address;
992   }
993   if (self_address_.address().empty()) {
994     self_address_ = self_address;
995   }
996
997   if (!(peer_address == peer_address_ && self_address == self_address_)) {
998     address_migrating_ = true;
999   }
1000
1001   stats_.bytes_received += packet.length();
1002   ++stats_.packets_received;
1003
1004   if (!framer_.ProcessPacket(packet)) {
1005     // If we are unable to decrypt this packet, it might be
1006     // because the CHLO or SHLO packet was lost.
1007     if (encryption_level_ != ENCRYPTION_FORWARD_SECURE &&
1008         framer_.error() == QUIC_DECRYPTION_FAILURE &&
1009         undecryptable_packets_.size() < kMaxUndecryptablePackets) {
1010       QueueUndecryptablePacket(packet);
1011     }
1012     DVLOG(1) << ENDPOINT << "Unable to process packet.  Last packet processed: "
1013              << last_header_.packet_sequence_number;
1014     return;
1015   }
1016   MaybeProcessUndecryptablePackets();
1017   MaybeProcessRevivedPacket();
1018 }
1019
1020 bool QuicConnection::OnCanWrite() {
1021   write_blocked_ = false;
1022   return DoWrite();
1023 }
1024
1025 bool QuicConnection::WriteIfNotBlocked() {
1026   if (write_blocked_) {
1027     return false;
1028   }
1029   return DoWrite();
1030 }
1031
1032 bool QuicConnection::DoWrite() {
1033   DCHECK(!write_blocked_);
1034   WriteQueuedPackets();
1035
1036   WritePendingRetransmissions();
1037
1038   IsHandshake pending_handshake = visitor_->HasPendingHandshake() ?
1039       IS_HANDSHAKE : NOT_HANDSHAKE;
1040   // Sending queued packets may have caused the socket to become write blocked,
1041   // or the congestion manager to prohibit sending.  If we've sent everything
1042   // we had queued and we're still not blocked, let the visitor know it can
1043   // write more.
1044   if (CanWrite(NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA,
1045                pending_handshake)) {
1046     // Set |include_ack| to false in bundler; ack inclusion happens elsewhere.
1047     scoped_ptr<ScopedPacketBundler> bundler(
1048         new ScopedPacketBundler(this, false));
1049     bool all_bytes_written = visitor_->OnCanWrite();
1050     bundler.reset();
1051     // After the visitor writes, it may have caused the socket to become write
1052     // blocked or the congestion manager to prohibit sending, so check again.
1053     pending_handshake = visitor_->HasPendingHandshake() ? IS_HANDSHAKE
1054                                                         : NOT_HANDSHAKE;
1055     if (!all_bytes_written && !resume_writes_alarm_->IsSet() &&
1056         CanWrite(NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA,
1057                  pending_handshake)) {
1058       // We're not write blocked, but some stream didn't write out all of its
1059       // bytes.  Register for 'immediate' resumption so we'll keep writing after
1060       // other quic connections have had a chance to use the socket.
1061       resume_writes_alarm_->Set(clock_->ApproximateNow());
1062     }
1063   }
1064
1065   return !write_blocked_;
1066 }
1067
1068 bool QuicConnection::ProcessValidatedPacket() {
1069   if (address_migrating_) {
1070     SendConnectionCloseWithDetails(
1071         QUIC_ERROR_MIGRATING_ADDRESS,
1072         "Address migration is not yet a supported feature");
1073     return false;
1074   }
1075   time_of_last_received_packet_ = clock_->Now();
1076   DVLOG(1) << ENDPOINT << "time of last received packet: "
1077            << time_of_last_received_packet_.ToDebuggingValue();
1078   return true;
1079 }
1080
1081 bool QuicConnection::WriteQueuedPackets() {
1082   DCHECK(!write_blocked_);
1083
1084   if (pending_version_negotiation_packet_) {
1085     SendVersionNegotiationPacket();
1086   }
1087
1088   QueuedPacketList::iterator packet_iterator = queued_packets_.begin();
1089   while (!write_blocked_ && packet_iterator != queued_packets_.end()) {
1090     if (WritePacket(packet_iterator->encryption_level,
1091                     packet_iterator->sequence_number,
1092                     packet_iterator->packet,
1093                     packet_iterator->transmission_type,
1094                     packet_iterator->retransmittable,
1095                     packet_iterator->handshake,
1096                     packet_iterator->forced)) {
1097       packet_iterator = queued_packets_.erase(packet_iterator);
1098     } else {
1099       // Continue, because some queued packets may still be writable.
1100       // This can happen if a retransmit send fail.
1101       ++packet_iterator;
1102     }
1103   }
1104
1105   return !write_blocked_;
1106 }
1107
1108 void QuicConnection::WritePendingRetransmissions() {
1109   // Keep writing as long as there's a pending retransmission which can be
1110   // written.
1111   while (sent_packet_manager_.HasPendingRetransmissions()) {
1112     const QuicSentPacketManager::PendingRetransmission pending =
1113         sent_packet_manager_.NextPendingRetransmission();
1114     if (HasForcedFrames(&pending.retransmittable_frames) == NO_FORCE &&
1115         !CanWrite(pending.transmission_type, HAS_RETRANSMITTABLE_DATA,
1116                   HasCryptoHandshake(&pending.retransmittable_frames))) {
1117       break;
1118     }
1119
1120     // Re-packetize the frames with a new sequence number for retransmission.
1121     // Retransmitted data packets do not use FEC, even when it's enabled.
1122     // Retransmitted packets use the same sequence number length as the
1123     // original.
1124     // Flush the packet creator before making a new packet.
1125     // TODO(ianswett): Implement ReserializeAllFrames as a separate path that
1126     // does not require the creator to be flushed.
1127     Flush();
1128     SerializedPacket serialized_packet = packet_creator_.ReserializeAllFrames(
1129         pending.retransmittable_frames.frames(),
1130         pending.sequence_number_length);
1131
1132     DLOG(INFO) << ENDPOINT << "Retransmitting " << pending.sequence_number
1133                << " as " << serialized_packet.sequence_number;
1134     if (debug_visitor_) {
1135       debug_visitor_->OnPacketRetransmitted(
1136           pending.sequence_number, serialized_packet.sequence_number);
1137     }
1138     sent_packet_manager_.OnRetransmittedPacket(
1139         pending.sequence_number, serialized_packet.sequence_number);
1140
1141     SendOrQueuePacket(pending.retransmittable_frames.encryption_level(),
1142                       serialized_packet.sequence_number,
1143                       serialized_packet.packet,
1144                       serialized_packet.entropy_hash,
1145                       pending.transmission_type,
1146                       HAS_RETRANSMITTABLE_DATA,
1147                       HasCryptoHandshake(
1148                           serialized_packet.retransmittable_frames),
1149                       HasForcedFrames(
1150                           serialized_packet.retransmittable_frames));
1151   }
1152 }
1153
1154 void QuicConnection::RetransmitUnackedPackets(
1155     RetransmissionType retransmission_type) {
1156   SequenceNumberSet unacked_packets =
1157       sent_packet_manager_.GetUnackedPackets();
1158   if (unacked_packets.empty()) {
1159     return;
1160   }
1161
1162   for (SequenceNumberSet::const_iterator unacked_it = unacked_packets.begin();
1163        unacked_it != unacked_packets.end(); ++unacked_it) {
1164     if (!sent_packet_manager_.HasRetransmittableFrames(*unacked_it)) {
1165       continue;
1166     }
1167     const RetransmittableFrames& frames =
1168         sent_packet_manager_.GetRetransmittableFrames(*unacked_it);
1169     if (retransmission_type == ALL_PACKETS ||
1170         frames.encryption_level() == ENCRYPTION_INITIAL) {
1171       // TODO(satyamshekhar): Think about congestion control here.
1172       // Specifically, about the retransmission count of packets being sent
1173       // proactively to achieve 0 (minimal) RTT.
1174       RetransmitPacket(*unacked_it, NACK_RETRANSMISSION);
1175     }
1176   }
1177 }
1178
1179 void QuicConnection::RetransmitPacket(
1180     QuicPacketSequenceNumber sequence_number,
1181     TransmissionType transmission_type) {
1182   DCHECK(sent_packet_manager_.IsUnacked(sequence_number));
1183
1184   // TODO(pwestin): Need to fix potential issue with FEC and a 1 packet
1185   // congestion window see b/8331807 for details.
1186   congestion_manager_.OnPacketAbandoned(sequence_number);
1187
1188   // If we have received an ACK for an old version of this packet, then
1189   // we should not retransmit the data.
1190   if (!sent_packet_manager_.MarkForRetransmission(sequence_number,
1191                                                   transmission_type)) {
1192     sent_packet_manager_.DiscardUnackedPacket(sequence_number);
1193     return;
1194   }
1195
1196   WriteIfNotBlocked();
1197 }
1198
1199 bool QuicConnection::ShouldGeneratePacket(
1200     TransmissionType transmission_type,
1201     HasRetransmittableData retransmittable,
1202     IsHandshake handshake) {
1203   // We should serialize handshake packets immediately to ensure that they
1204   // end up sent at the right encryption level.
1205   if (handshake == IS_HANDSHAKE) {
1206     return true;
1207   }
1208
1209   return CanWrite(transmission_type, retransmittable, handshake);
1210 }
1211
1212 bool QuicConnection::CanWrite(TransmissionType transmission_type,
1213                               HasRetransmittableData retransmittable,
1214                               IsHandshake handshake) {
1215   // This check assumes that if the send alarm is set, it applies equally to all
1216   // types of transmissions.
1217   if (write_blocked_ || send_alarm_->IsSet()) {
1218     return false;
1219   }
1220
1221   QuicTime now = clock_->Now();
1222   QuicTime::Delta delay = congestion_manager_.TimeUntilSend(
1223       now, transmission_type, retransmittable, handshake);
1224   if (delay.IsInfinite()) {
1225     return false;
1226   }
1227
1228   // If the scheduler requires a delay, then we can not send this packet now.
1229   if (!delay.IsZero()) {
1230     send_alarm_->Cancel();
1231     send_alarm_->Set(now.Add(delay));
1232     return false;
1233   }
1234   return true;
1235 }
1236
1237 void QuicConnection::SetupRetransmission(
1238     QuicPacketSequenceNumber sequence_number,
1239     EncryptionLevel level) {
1240   if (!sent_packet_manager_.HasRetransmittableFrames(sequence_number)) {
1241     DVLOG(1) << ENDPOINT << "Will not retransmit packet " << sequence_number;
1242     return;
1243   }
1244
1245   // Do not set the retransmission alarm if we're already handling one, since
1246   // it will be reset when OnRetransmissionTimeout completes.
1247   if (retransmission_alarm_->IsSet()) {
1248     return;
1249   }
1250
1251   QuicTime::Delta retransmission_delay =
1252       congestion_manager_.GetRetransmissionDelay(
1253           sent_packet_manager_.GetNumUnackedPackets(), consecutive_rto_count_);
1254   retransmission_alarm_->Set(
1255       clock_->ApproximateNow().Add(retransmission_delay));
1256 }
1257
1258 void QuicConnection::SetupAbandonFecTimer(
1259     QuicPacketSequenceNumber sequence_number) {
1260   if (abandon_fec_alarm_->IsSet()) {
1261     return;
1262   }
1263   QuicTime::Delta retransmission_delay =
1264       congestion_manager_.GetRetransmissionDelay(
1265           sent_packet_manager_.GetNumUnackedPackets(), consecutive_rto_count_);
1266   abandon_fec_alarm_->Set(clock_->ApproximateNow().Add(retransmission_delay));
1267 }
1268
1269 bool QuicConnection::WritePacket(EncryptionLevel level,
1270                                  QuicPacketSequenceNumber sequence_number,
1271                                  QuicPacket* packet,
1272                                  TransmissionType transmission_type,
1273                                  HasRetransmittableData retransmittable,
1274                                  IsHandshake handshake,
1275                                  Force forced) {
1276   if (ShouldDiscardPacket(level, sequence_number, retransmittable)) {
1277     delete packet;
1278     return true;
1279   }
1280
1281   // If we're write blocked, we know we can't write.
1282   if (write_blocked_) {
1283     return false;
1284   }
1285
1286   // If we are not forced and we can't write, then simply return false;
1287   if (forced == NO_FORCE &&
1288       !CanWrite(transmission_type, retransmittable, handshake)) {
1289     return false;
1290   }
1291
1292   // Some encryption algorithms require the packet sequence numbers not be
1293   // repeated.
1294   DCHECK_LE(sequence_number_of_last_inorder_packet_, sequence_number);
1295   // Only increase this when packets have not been queued.  Once they're queued
1296   // due to a write block, there is the chance of sending forced and other
1297   // higher priority packets out of order.
1298   if (queued_packets_.empty()) {
1299     sequence_number_of_last_inorder_packet_ = sequence_number;
1300   }
1301
1302   scoped_ptr<QuicEncryptedPacket> encrypted(
1303       framer_.EncryptPacket(level, sequence_number, *packet));
1304   if (encrypted.get() == NULL) {
1305     LOG(DFATAL) << ENDPOINT << "Failed to encrypt packet number "
1306                 << sequence_number;
1307     CloseConnection(QUIC_ENCRYPTION_FAILURE, false);
1308     return false;
1309   }
1310
1311   if (encrypted->length() > options()->max_packet_length) {
1312     LOG(DFATAL) << ENDPOINT
1313                 << "Writing an encrypted packet larger than max_packet_length:"
1314                 << options()->max_packet_length;
1315   }
1316   DLOG(INFO) << ENDPOINT << "Sending packet number " << sequence_number
1317              << " : " << (packet->is_fec_packet() ? "FEC " :
1318                  (retransmittable == HAS_RETRANSMITTABLE_DATA
1319                       ? "data bearing " : " ack only "))
1320              << ", encryption level: "
1321              << QuicUtils::EncryptionLevelToString(level)
1322              << ", length:" << packet->length() << ", encrypted length:"
1323              << encrypted->length();
1324   DVLOG(2) << ENDPOINT << "packet(" << sequence_number << "): " << std::endl
1325            << QuicUtils::StringToHexASCIIDump(packet->AsStringPiece());
1326
1327   DCHECK(encrypted->length() <= kMaxPacketSize)
1328       << "Packet " << sequence_number << " will not be read; too large: "
1329       << packet->length() << " " << encrypted->length() << " "
1330       << " forced: " << (forced == FORCE ? "yes" : "no");
1331
1332   DCHECK(pending_write_.get() == NULL);
1333   pending_write_.reset(new PendingWrite(sequence_number, transmission_type,
1334                                         retransmittable, level,
1335                                         packet->is_fec_packet(),
1336                                         packet->length()));
1337
1338   WriteResult result =
1339       writer_->WritePacket(encrypted->data(), encrypted->length(),
1340                            self_address().address(), peer_address(), this);
1341   if (debug_visitor_) {
1342     // Pass the write result to the visitor.
1343     debug_visitor_->OnPacketSent(sequence_number, level, *encrypted, result);
1344   }
1345   if (result.status == WRITE_STATUS_BLOCKED) {
1346     // TODO(satyashekhar): It might be more efficient (fewer system calls), if
1347     // all connections share this variable i.e this becomes a part of
1348     // PacketWriterInterface.
1349     write_blocked_ = true;
1350     // If the socket buffers the the data, then the packet should not
1351     // be queued and sent again, which would result in an unnecessary
1352     // duplicate packet being sent.  The helper must call OnPacketSent
1353     // when the packet is actually sent.
1354     if (writer_->IsWriteBlockedDataBuffered()) {
1355       delete packet;
1356       return true;
1357     }
1358     pending_write_.reset();
1359     return false;
1360   }
1361
1362   if (OnPacketSent(result)) {
1363     delete packet;
1364     return true;
1365   }
1366   return false;
1367 }
1368
1369 bool QuicConnection::ShouldDiscardPacket(
1370     EncryptionLevel level,
1371     QuicPacketSequenceNumber sequence_number,
1372     HasRetransmittableData retransmittable) {
1373   if (!connected_) {
1374     DLOG(INFO) << ENDPOINT
1375                << "Not sending packet as connection is disconnected.";
1376     return true;
1377   }
1378
1379   if (encryption_level_ == ENCRYPTION_FORWARD_SECURE &&
1380       level == ENCRYPTION_NONE) {
1381     // Drop packets that are NULL encrypted since the peer won't accept them
1382     // anymore.
1383     DLOG(INFO) << ENDPOINT << "Dropping packet: " << sequence_number
1384                << " since the packet is NULL encrypted.";
1385     sent_packet_manager_.DiscardUnackedPacket(sequence_number);
1386     return true;
1387   }
1388
1389   if (retransmittable == HAS_RETRANSMITTABLE_DATA) {
1390     if (!sent_packet_manager_.IsUnacked(sequence_number)) {
1391       // This is a crazy edge case, but if we retransmit a packet,
1392       // (but have to queue it for some reason) then receive an ack
1393       // for the previous transmission (but not the retransmission)
1394       // then receive a truncated ACK which causes us to raise the
1395       // high water mark, all before we're able to send the packet
1396       // then we can simply drop it.
1397       DLOG(INFO) << ENDPOINT << "Dropping packet: " << sequence_number
1398                  << " since it has already been acked.";
1399       return true;
1400     }
1401
1402     if (sent_packet_manager_.IsPreviousTransmission(sequence_number)) {
1403       // If somehow we have already retransmitted this packet *before*
1404       // we actually send it for the first time (I think this is probably
1405       // impossible in the real world), then don't bother sending it.
1406       // We don't want to call DiscardUnackedPacket because in this case
1407       // the peer has not yet ACK'd the data.  We need the subsequent
1408       // retransmission to be sent.
1409       DLOG(INFO) << ENDPOINT << "Dropping packet: " << sequence_number
1410           << " since it has already been retransmitted.";
1411       return true;
1412     }
1413
1414     if (!sent_packet_manager_.HasRetransmittableFrames(sequence_number)) {
1415       DLOG(INFO) << ENDPOINT << "Dropping packet: " << sequence_number
1416                  << " since a previous transmission has been acked.";
1417       sent_packet_manager_.DiscardUnackedPacket(sequence_number);
1418       return true;
1419     }
1420   }
1421
1422   return false;
1423 }
1424
1425 bool QuicConnection::OnPacketSent(WriteResult result) {
1426   DCHECK_NE(WRITE_STATUS_BLOCKED, result.status);
1427   if (pending_write_.get() == NULL) {
1428     LOG(DFATAL) << "OnPacketSent called without a pending write.";
1429     return false;
1430   }
1431
1432   QuicPacketSequenceNumber sequence_number = pending_write_->sequence_number;
1433   TransmissionType transmission_type  = pending_write_->transmission_type;
1434   HasRetransmittableData retransmittable = pending_write_->retransmittable;
1435   EncryptionLevel level = pending_write_->level;
1436   bool is_fec_packet = pending_write_->is_fec_packet;
1437   size_t length = pending_write_->length;
1438   pending_write_.reset();
1439
1440   if (result.status == WRITE_STATUS_ERROR) {
1441     DLOG(INFO) << "Write failed with error code: " << result.error_code;
1442     // We can't send an error as the socket is presumably borked.
1443     CloseConnection(QUIC_PACKET_WRITE_ERROR, false);
1444     return false;
1445   }
1446
1447   QuicTime now = clock_->Now();
1448   if (transmission_type == NOT_RETRANSMISSION) {
1449     time_of_last_sent_packet_ = now;
1450   }
1451   DVLOG(1) << ENDPOINT << "time of last sent packet: "
1452            << now.ToDebuggingValue();
1453
1454   // Set the retransmit alarm only when we have sent the packet to the client
1455   // and not when it goes to the pending queue, otherwise we will end up adding
1456   // an entry to retransmission_timeout_ every time we attempt a write.
1457   if (retransmittable == HAS_RETRANSMITTABLE_DATA) {
1458     SetupRetransmission(sequence_number, level);
1459   } else if (is_fec_packet) {
1460     SetupAbandonFecTimer(sequence_number);
1461   }
1462
1463   // TODO(ianswett): Change the sequence number length and other packet creator
1464   // options by a more explicit API than setting a struct value directly.
1465   packet_creator_.UpdateSequenceNumberLength(
1466       received_packet_manager_.least_packet_awaited_by_peer(),
1467       congestion_manager_.BandwidthEstimate().ToBytesPerPeriod(
1468           congestion_manager_.SmoothedRtt()));
1469
1470   congestion_manager_.OnPacketSent(sequence_number, now, length,
1471                                    transmission_type, retransmittable);
1472
1473   stats_.bytes_sent += result.bytes_written;
1474   ++stats_.packets_sent;
1475
1476   if (transmission_type == NACK_RETRANSMISSION ||
1477       transmission_type == RTO_RETRANSMISSION) {
1478     stats_.bytes_retransmitted += result.bytes_written;
1479     ++stats_.packets_retransmitted;
1480   }
1481
1482   return true;
1483 }
1484
1485 bool QuicConnection::OnSerializedPacket(
1486     const SerializedPacket& serialized_packet) {
1487   if (serialized_packet.retransmittable_frames) {
1488     serialized_packet.retransmittable_frames->
1489         set_encryption_level(encryption_level_);
1490   }
1491   sent_packet_manager_.OnSerializedPacket(serialized_packet,
1492                                           clock_->ApproximateNow());
1493   // The TransmissionType is NOT_RETRANSMISSION because all retransmissions
1494   // serialize packets and invoke SendOrQueuePacket directly.
1495   return SendOrQueuePacket(encryption_level_,
1496                            serialized_packet.sequence_number,
1497                            serialized_packet.packet,
1498                            serialized_packet.entropy_hash,
1499                            NOT_RETRANSMISSION,
1500                            serialized_packet.retransmittable_frames != NULL ?
1501                                HAS_RETRANSMITTABLE_DATA :
1502                                NO_RETRANSMITTABLE_DATA,
1503                            HasCryptoHandshake(
1504                                serialized_packet.retransmittable_frames),
1505                            HasForcedFrames(
1506                                serialized_packet.retransmittable_frames));
1507 }
1508
1509 QuicPacketSequenceNumber QuicConnection::GetNextPacketSequenceNumber() {
1510   return packet_creator_.sequence_number() + 1;
1511 }
1512
1513 void QuicConnection::OnPacketNacked(QuicPacketSequenceNumber sequence_number,
1514                                     size_t nack_count) {
1515   if (nack_count >= kNumberOfNacksBeforeRetransmission &&
1516       retransmitted_nacked_packet_count_ < kMaxRetransmissionsPerAck) {
1517     ++retransmitted_nacked_packet_count_;
1518     RetransmitPacket(sequence_number, NACK_RETRANSMISSION);
1519   }
1520 }
1521
1522 bool QuicConnection::SendOrQueuePacket(EncryptionLevel level,
1523                                        QuicPacketSequenceNumber sequence_number,
1524                                        QuicPacket* packet,
1525                                        QuicPacketEntropyHash entropy_hash,
1526                                        TransmissionType transmission_type,
1527                                        HasRetransmittableData retransmittable,
1528                                        IsHandshake handshake,
1529                                        Force forced) {
1530   sent_entropy_manager_.RecordPacketEntropyHash(sequence_number, entropy_hash);
1531   if (!WritePacket(level, sequence_number, packet,
1532                    transmission_type, retransmittable, handshake, forced)) {
1533     queued_packets_.push_back(QueuedPacket(sequence_number, packet, level,
1534                                            transmission_type, retransmittable,
1535                                            handshake, forced));
1536     return false;
1537   }
1538   return true;
1539 }
1540
1541 void QuicConnection::UpdateSentPacketInfo(SentPacketInfo* sent_info) {
1542   sent_info->least_unacked = sent_packet_manager_.GetLeastUnackedSentPacket();
1543   sent_info->entropy_hash = sent_entropy_manager_.EntropyHash(
1544       sent_info->least_unacked - 1);
1545 }
1546
1547 void QuicConnection::SendAck() {
1548   ack_alarm_->Cancel();
1549   // TODO(rch): delay this until the CreateFeedbackFrame
1550   // method is invoked.  This requires changes SetShouldSendAck
1551   // to be a no-arg method, and re-jiggering its implementation.
1552   bool send_feedback = false;
1553   if (congestion_manager_.GenerateCongestionFeedback(
1554           &outgoing_congestion_feedback_)) {
1555     DVLOG(1) << ENDPOINT << "Sending feedback: "
1556              << outgoing_congestion_feedback_;
1557     send_feedback = true;
1558   }
1559
1560   packet_generator_.SetShouldSendAck(send_feedback);
1561 }
1562
1563 void QuicConnection::OnRetransmissionTimeout() {
1564   if (!sent_packet_manager_.HasUnackedPackets()) {
1565     return;
1566   }
1567
1568   // TODO(ianswett): When an RTO fires, but the connection has not been
1569   // established as forward secure, re-send the client hello first.
1570   ++stats_.rto_count;
1571   ++consecutive_rto_count_;
1572
1573   // Attempt to send all the unacked packets when the RTO fires, let the
1574   // congestion manager decide how many to send immediately and the remaining
1575   // packets will be queued for future sending.
1576   SequenceNumberSet unacked_packets =
1577       sent_packet_manager_.GetUnackedPackets();
1578   DLOG(INFO) << "OnRetransmissionTimeout() fired with "
1579              << unacked_packets.size() << " unacked packets.";
1580
1581   // Abandon all unacked packets to ensure the congestion window
1582   // opens up before we attempt to retransmit the packet.
1583   for (SequenceNumberSet::const_iterator it = unacked_packets.begin();
1584        it != unacked_packets.end(); ++it) {
1585     congestion_manager_.OnPacketAbandoned(*it);
1586   }
1587
1588   // Retransmit any packet with retransmittable frames.
1589   for (SequenceNumberSet::const_iterator it = unacked_packets.begin();
1590        it != unacked_packets.end(); ++it) {
1591     if (sent_packet_manager_.IsUnacked(*it) &&
1592         sent_packet_manager_.HasRetransmittableFrames(*it)) {
1593       RetransmitPacket(*it, RTO_RETRANSMISSION);
1594     }
1595   }
1596
1597   // If the data from all unacked packets had been acked, then no packets will
1598   // have been retransmitted.  If we had been congestion blocked then we might
1599   // have data ready to send, so we should attempt to send it now.  If we don't
1600   // send now, and we never receive an ack from the peer, we may hang.
1601   if (!retransmission_alarm_->IsSet()) {
1602     WriteIfNotBlocked();
1603   }
1604 }
1605
1606 QuicTime QuicConnection::OnAbandonFecTimeout() {
1607   // Abandon all the FEC packets older than the current RTO, then reschedule
1608   // the alarm if there are more pending fec packets.
1609   QuicTime::Delta retransmission_delay =
1610       congestion_manager_.GetRetransmissionDelay(
1611           sent_packet_manager_.GetNumUnackedPackets(), consecutive_rto_count_);
1612   QuicTime max_send_time =
1613       clock_->ApproximateNow().Subtract(retransmission_delay);
1614   bool abandoned_packet = false;
1615   while (sent_packet_manager_.HasUnackedFecPackets()) {
1616     QuicPacketSequenceNumber oldest_unacked_fec =
1617         sent_packet_manager_.GetLeastUnackedFecPacket();
1618     QuicTime fec_sent_time =
1619         sent_packet_manager_.GetFecSentTime(oldest_unacked_fec);
1620     if (fec_sent_time > max_send_time) {
1621       return fec_sent_time.Add(retransmission_delay);
1622     }
1623     abandoned_packet = true;
1624     sent_packet_manager_.DiscardFecPacket(oldest_unacked_fec);
1625     congestion_manager_.OnPacketAbandoned(oldest_unacked_fec);
1626   }
1627   if (abandoned_packet) {
1628     // If a packet was abandoned, then the congestion window may have
1629     // opened up, so attempt to write.
1630     WriteIfNotBlocked();
1631   }
1632   return QuicTime::Zero();
1633 }
1634
1635 void QuicConnection::SetEncrypter(EncryptionLevel level,
1636                                   QuicEncrypter* encrypter) {
1637   framer_.SetEncrypter(level, encrypter);
1638 }
1639
1640 const QuicEncrypter* QuicConnection::encrypter(EncryptionLevel level) const {
1641   return framer_.encrypter(level);
1642 }
1643
1644 void QuicConnection::SetDefaultEncryptionLevel(
1645     EncryptionLevel level) {
1646   encryption_level_ = level;
1647 }
1648
1649 void QuicConnection::SetDecrypter(QuicDecrypter* decrypter) {
1650   framer_.SetDecrypter(decrypter);
1651 }
1652
1653 void QuicConnection::SetAlternativeDecrypter(QuicDecrypter* decrypter,
1654                                              bool latch_once_used) {
1655   framer_.SetAlternativeDecrypter(decrypter, latch_once_used);
1656 }
1657
1658 const QuicDecrypter* QuicConnection::decrypter() const {
1659   return framer_.decrypter();
1660 }
1661
1662 const QuicDecrypter* QuicConnection::alternative_decrypter() const {
1663   return framer_.alternative_decrypter();
1664 }
1665
1666 void QuicConnection::QueueUndecryptablePacket(
1667     const QuicEncryptedPacket& packet) {
1668   DVLOG(1) << ENDPOINT << "Queueing undecryptable packet.";
1669   char* data = new char[packet.length()];
1670   memcpy(data, packet.data(), packet.length());
1671   undecryptable_packets_.push_back(
1672       new QuicEncryptedPacket(data, packet.length(), true));
1673 }
1674
1675 void QuicConnection::MaybeProcessUndecryptablePackets() {
1676   if (undecryptable_packets_.empty() ||
1677       encryption_level_ == ENCRYPTION_NONE) {
1678     return;
1679   }
1680
1681   while (connected_ && !undecryptable_packets_.empty()) {
1682     DVLOG(1) << ENDPOINT << "Attempting to process undecryptable packet";
1683     QuicEncryptedPacket* packet = undecryptable_packets_.front();
1684     if (!framer_.ProcessPacket(*packet) &&
1685         framer_.error() == QUIC_DECRYPTION_FAILURE) {
1686       DVLOG(1) << ENDPOINT << "Unable to process undecryptable packet...";
1687       break;
1688     }
1689     DVLOG(1) << ENDPOINT << "Processed undecryptable packet!";
1690     delete packet;
1691     undecryptable_packets_.pop_front();
1692   }
1693
1694   // Once forward secure encryption is in use, there will be no
1695   // new keys installed and hence any undecryptable packets will
1696   // never be able to be decrypted.
1697   if (encryption_level_ == ENCRYPTION_FORWARD_SECURE) {
1698     STLDeleteElements(&undecryptable_packets_);
1699   }
1700 }
1701
1702 void QuicConnection::MaybeProcessRevivedPacket() {
1703   QuicFecGroup* group = GetFecGroup();
1704   if (!connected_ || group == NULL || !group->CanRevive()) {
1705     return;
1706   }
1707   QuicPacketHeader revived_header;
1708   char revived_payload[kMaxPacketSize];
1709   size_t len = group->Revive(&revived_header, revived_payload, kMaxPacketSize);
1710   revived_header.public_header.guid = guid_;
1711   revived_header.public_header.version_flag = false;
1712   revived_header.public_header.reset_flag = false;
1713   revived_header.fec_flag = false;
1714   revived_header.is_in_fec_group = NOT_IN_FEC_GROUP;
1715   revived_header.fec_group = 0;
1716   group_map_.erase(last_header_.fec_group);
1717   delete group;
1718
1719   last_packet_revived_ = true;
1720   if (debug_visitor_) {
1721     debug_visitor_->OnRevivedPacket(revived_header,
1722                                     StringPiece(revived_payload, len));
1723   }
1724
1725   ++stats_.packets_revived;
1726   framer_.ProcessRevivedPacket(&revived_header,
1727                                StringPiece(revived_payload, len));
1728 }
1729
1730 QuicFecGroup* QuicConnection::GetFecGroup() {
1731   QuicFecGroupNumber fec_group_num = last_header_.fec_group;
1732   if (fec_group_num == 0) {
1733     return NULL;
1734   }
1735   if (group_map_.count(fec_group_num) == 0) {
1736     if (group_map_.size() >= kMaxFecGroups) {  // Too many groups
1737       if (fec_group_num < group_map_.begin()->first) {
1738         // The group being requested is a group we've seen before and deleted.
1739         // Don't recreate it.
1740         return NULL;
1741       }
1742       // Clear the lowest group number.
1743       delete group_map_.begin()->second;
1744       group_map_.erase(group_map_.begin());
1745     }
1746     group_map_[fec_group_num] = new QuicFecGroup();
1747   }
1748   return group_map_[fec_group_num];
1749 }
1750
1751 void QuicConnection::SendConnectionClose(QuicErrorCode error) {
1752   SendConnectionCloseWithDetails(error, string());
1753 }
1754
1755 void QuicConnection::SendConnectionCloseWithDetails(QuicErrorCode error,
1756                                                     const string& details) {
1757   if (!write_blocked_) {
1758     SendConnectionClosePacket(error, details);
1759   }
1760   CloseConnection(error, false);
1761 }
1762
1763 void QuicConnection::SendConnectionClosePacket(QuicErrorCode error,
1764                                                const string& details) {
1765   DLOG(INFO) << ENDPOINT << "Force closing " << guid() << " with error "
1766              << QuicUtils::ErrorToString(error) << " (" << error << ") "
1767              << details;
1768   ScopedPacketBundler ack_bundler(this, version() > QUIC_VERSION_11);
1769   QuicConnectionCloseFrame* frame = new QuicConnectionCloseFrame();
1770   frame->error_code = error;
1771   frame->error_details = details;
1772   UpdateSentPacketInfo(&frame->ack_frame.sent_info);
1773   received_packet_manager_.UpdateReceivedPacketInfo(
1774       &frame->ack_frame.received_info, clock_->ApproximateNow());
1775   packet_generator_.AddControlFrame(QuicFrame(frame));
1776   Flush();
1777 }
1778
1779 void QuicConnection::CloseConnection(QuicErrorCode error, bool from_peer) {
1780   DCHECK(connected_);
1781   connected_ = false;
1782   visitor_->OnConnectionClosed(error, from_peer);
1783   // Cancel the alarms so they don't trigger any action now that the
1784   // connection is closed.
1785   ack_alarm_->Cancel();
1786   resume_writes_alarm_->Cancel();
1787   retransmission_alarm_->Cancel();
1788   send_alarm_->Cancel();
1789   timeout_alarm_->Cancel();
1790 }
1791
1792 void QuicConnection::SendGoAway(QuicErrorCode error,
1793                                 QuicStreamId last_good_stream_id,
1794                                 const string& reason) {
1795   DLOG(INFO) << ENDPOINT << "Going away with error "
1796              << QuicUtils::ErrorToString(error)
1797              << " (" << error << ")";
1798
1799   // Opportunistically bundle an ack with this outgoing packet.
1800   ScopedPacketBundler ack_bundler(this, true);
1801   packet_generator_.AddControlFrame(
1802       QuicFrame(new QuicGoAwayFrame(error, last_good_stream_id, reason)));
1803 }
1804
1805 void QuicConnection::CloseFecGroupsBefore(
1806     QuicPacketSequenceNumber sequence_number) {
1807   FecGroupMap::iterator it = group_map_.begin();
1808   while (it != group_map_.end()) {
1809     // If this is the current group or the group doesn't protect this packet
1810     // we can ignore it.
1811     if (last_header_.fec_group == it->first ||
1812         !it->second->ProtectsPacketsBefore(sequence_number)) {
1813       ++it;
1814       continue;
1815     }
1816     QuicFecGroup* fec_group = it->second;
1817     DCHECK(!fec_group->CanRevive());
1818     FecGroupMap::iterator next = it;
1819     ++next;
1820     group_map_.erase(it);
1821     delete fec_group;
1822     it = next;
1823   }
1824 }
1825
1826 void QuicConnection::Flush() {
1827   packet_generator_.FlushAllQueuedFrames();
1828 }
1829
1830 bool QuicConnection::HasQueuedData() const {
1831   return pending_version_negotiation_packet_ ||
1832       !queued_packets_.empty() || packet_generator_.HasQueuedFrames();
1833 }
1834
1835 void QuicConnection::SetIdleNetworkTimeout(QuicTime::Delta timeout) {
1836   if (timeout < idle_network_timeout_) {
1837     idle_network_timeout_ = timeout;
1838     CheckForTimeout();
1839   } else {
1840      idle_network_timeout_ = timeout;
1841   }
1842 }
1843
1844 void QuicConnection::SetOverallConnectionTimeout(QuicTime::Delta timeout) {
1845   if (timeout < overall_connection_timeout_) {
1846     overall_connection_timeout_ = timeout;
1847     CheckForTimeout();
1848   } else {
1849     overall_connection_timeout_ = timeout;
1850   }
1851 }
1852
1853 bool QuicConnection::CheckForTimeout() {
1854   QuicTime now = clock_->ApproximateNow();
1855   QuicTime time_of_last_packet = std::max(time_of_last_received_packet_,
1856                                           time_of_last_sent_packet_);
1857
1858   // |delta| can be < 0 as |now| is approximate time but |time_of_last_packet|
1859   // is accurate time. However, this should not change the behavior of
1860   // timeout handling.
1861   QuicTime::Delta delta = now.Subtract(time_of_last_packet);
1862   DVLOG(1) << ENDPOINT << "last packet "
1863            << time_of_last_packet.ToDebuggingValue()
1864            << " now:" << now.ToDebuggingValue()
1865            << " delta:" << delta.ToMicroseconds()
1866            << " network_timeout: " << idle_network_timeout_.ToMicroseconds();
1867   if (delta >= idle_network_timeout_) {
1868     DVLOG(1) << ENDPOINT << "Connection timedout due to no network activity.";
1869     SendConnectionClose(QUIC_CONNECTION_TIMED_OUT);
1870     return true;
1871   }
1872
1873   // Next timeout delta.
1874   QuicTime::Delta timeout = idle_network_timeout_.Subtract(delta);
1875
1876   if (!overall_connection_timeout_.IsInfinite()) {
1877     QuicTime::Delta connected_time = now.Subtract(creation_time_);
1878     DVLOG(1) << ENDPOINT << "connection time: "
1879              << connected_time.ToMilliseconds() << " overall timeout: "
1880              << overall_connection_timeout_.ToMilliseconds();
1881     if (connected_time >= overall_connection_timeout_) {
1882       DVLOG(1) << ENDPOINT <<
1883           "Connection timedout due to overall connection timeout.";
1884       SendConnectionClose(QUIC_CONNECTION_TIMED_OUT);
1885       return true;
1886     }
1887
1888     // Take the min timeout.
1889     QuicTime::Delta connection_timeout =
1890         overall_connection_timeout_.Subtract(connected_time);
1891     if (connection_timeout < timeout) {
1892       timeout = connection_timeout;
1893     }
1894   }
1895
1896   timeout_alarm_->Cancel();
1897   timeout_alarm_->Set(clock_->ApproximateNow().Add(timeout));
1898   return false;
1899 }
1900
1901 QuicConnection::ScopedPacketBundler::ScopedPacketBundler(
1902     QuicConnection* connection,
1903     bool include_ack)
1904     : connection_(connection),
1905       already_in_batch_mode_(connection->packet_generator_.InBatchMode()) {
1906   // Move generator into batch mode. If caller wants us to include an ack,
1907   // check the delayed-ack timer to see if there's ack info to be sent.
1908   if (!already_in_batch_mode_) {
1909     DVLOG(1) << "Entering Batch Mode.";
1910     connection_->packet_generator_.StartBatchOperations();
1911   }
1912   if (FLAGS_bundle_ack_with_outgoing_packet &&
1913       include_ack && connection_->ack_alarm_->IsSet()) {
1914     DVLOG(1) << "Bundling ack with outgoing packet.";
1915     connection_->SendAck();
1916   }
1917 }
1918
1919 QuicConnection::ScopedPacketBundler::~ScopedPacketBundler() {
1920   // If we changed the generator's batch state, restore original batch state.
1921   if (!already_in_batch_mode_) {
1922     DVLOG(1) << "Leaving Batch Mode.";
1923     connection_->packet_generator_.FinishBatchOperations();
1924   }
1925   DCHECK_EQ(already_in_batch_mode_,
1926             connection_->packet_generator_.InBatchMode());
1927 }
1928
1929 }  // namespace net