OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/quic/quic_packet_creator.h" | 5 #include "net/quic/quic_packet_creator.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 const uint64_t delta = max(current_delta, max_packets_in_flight); | 152 const uint64_t delta = max(current_delta, max_packets_in_flight); |
153 next_packet_number_length_ = | 153 next_packet_number_length_ = |
154 QuicFramer::GetMinSequenceNumberLength(delta * 4); | 154 QuicFramer::GetMinSequenceNumberLength(delta * 4); |
155 } | 155 } |
156 | 156 |
157 bool QuicPacketCreator::ConsumeData(QuicStreamId id, | 157 bool QuicPacketCreator::ConsumeData(QuicStreamId id, |
158 QuicIOVector iov, | 158 QuicIOVector iov, |
159 size_t iov_offset, | 159 size_t iov_offset, |
160 QuicStreamOffset offset, | 160 QuicStreamOffset offset, |
161 bool fin, | 161 bool fin, |
162 bool needs_padding, | 162 bool needs_full_padding, |
163 QuicFrame* frame) { | 163 QuicFrame* frame) { |
164 if (!HasRoomForStreamFrame(id, offset)) { | 164 if (!HasRoomForStreamFrame(id, offset)) { |
165 return false; | 165 return false; |
166 } | 166 } |
167 CreateStreamFrame(id, iov, iov_offset, offset, fin, frame); | 167 CreateStreamFrame(id, iov, iov_offset, offset, fin, frame); |
168 if (!AddFrame(*frame, /*save_retransmittable_frames=*/true)) { | 168 if (!AddFrame(*frame, /*save_retransmittable_frames=*/true)) { |
169 // Fails if we try to write unencrypted stream data. | 169 // Fails if we try to write unencrypted stream data. |
170 delete frame->stream_frame; | 170 delete frame->stream_frame; |
171 return false; | 171 return false; |
172 } | 172 } |
173 if (needs_padding) { | 173 if (needs_full_padding) { |
174 packet_.needs_padding = true; | 174 packet_.num_padding_bytes = -1; |
175 } | 175 } |
176 return true; | 176 return true; |
177 } | 177 } |
178 | 178 |
179 bool QuicPacketCreator::HasRoomForStreamFrame(QuicStreamId id, | 179 bool QuicPacketCreator::HasRoomForStreamFrame(QuicStreamId id, |
180 QuicStreamOffset offset) { | 180 QuicStreamOffset offset) { |
181 return BytesFree() > QuicFramer::GetMinStreamFrameSize(id, offset, true); | 181 return BytesFree() > QuicFramer::GetMinStreamFrameSize(id, offset, true); |
182 } | 182 } |
183 | 183 |
184 // static | 184 // static |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 copy_len = min(length, iov.iov[iovnum].iov_len); | 286 copy_len = min(length, iov.iov[iovnum].iov_len); |
287 } | 287 } |
288 QUIC_BUG_IF(length > 0) << "Failed to copy entire length to buffer."; | 288 QUIC_BUG_IF(length > 0) << "Failed to copy entire length to buffer."; |
289 } | 289 } |
290 | 290 |
291 void QuicPacketCreator::ReserializeAllFrames( | 291 void QuicPacketCreator::ReserializeAllFrames( |
292 const PendingRetransmission& retransmission, | 292 const PendingRetransmission& retransmission, |
293 char* buffer, | 293 char* buffer, |
294 size_t buffer_len) { | 294 size_t buffer_len) { |
295 DCHECK(queued_frames_.empty()); | 295 DCHECK(queued_frames_.empty()); |
296 DCHECK(!packet_.needs_padding); | 296 DCHECK_EQ(0, packet_.num_padding_bytes); |
297 QUIC_BUG_IF(retransmission.retransmittable_frames.empty()) | 297 QUIC_BUG_IF(retransmission.retransmittable_frames.empty()) |
298 << "Attempt to serialize empty packet"; | 298 << "Attempt to serialize empty packet"; |
299 const QuicPacketNumberLength saved_length = packet_.packet_number_length; | 299 const QuicPacketNumberLength saved_length = packet_.packet_number_length; |
300 const QuicPacketNumberLength saved_next_length = next_packet_number_length_; | 300 const QuicPacketNumberLength saved_next_length = next_packet_number_length_; |
301 const EncryptionLevel default_encryption_level = packet_.encryption_level; | 301 const EncryptionLevel default_encryption_level = packet_.encryption_level; |
302 | 302 |
303 // Temporarily set the packet number length and change the encryption level. | 303 // Temporarily set the packet number length and change the encryption level. |
304 packet_.packet_number_length = retransmission.packet_number_length; | 304 packet_.packet_number_length = retransmission.packet_number_length; |
305 next_packet_number_length_ = retransmission.packet_number_length; | 305 next_packet_number_length_ = retransmission.packet_number_length; |
306 packet_.needs_padding = retransmission.needs_padding; | 306 packet_.num_padding_bytes = retransmission.num_padding_bytes; |
307 // Only preserve the original encryption level if it's a handshake packet or | 307 // Only preserve the original encryption level if it's a handshake packet or |
308 // if we haven't gone forward secure. | 308 // if we haven't gone forward secure. |
309 if (retransmission.has_crypto_handshake || | 309 if (retransmission.has_crypto_handshake || |
310 packet_.encryption_level != ENCRYPTION_FORWARD_SECURE) { | 310 packet_.encryption_level != ENCRYPTION_FORWARD_SECURE) { |
311 packet_.encryption_level = retransmission.encryption_level; | 311 packet_.encryption_level = retransmission.encryption_level; |
312 } | 312 } |
313 | 313 |
314 // Serialize the packet and restore packet number length state. | 314 // Serialize the packet and restore packet number length state. |
315 for (const QuicFrame& frame : retransmission.retransmittable_frames) { | 315 for (const QuicFrame& frame : retransmission.retransmittable_frames) { |
316 bool success = AddFrame(frame, false); | 316 bool success = AddFrame(frame, false); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 // constructed, so here we have a good opportunity to actually change it. | 354 // constructed, so here we have a good opportunity to actually change it. |
355 if (CanSetMaxPacketLength()) { | 355 if (CanSetMaxPacketLength()) { |
356 SetMaxPacketLength(max_packet_length_); | 356 SetMaxPacketLength(max_packet_length_); |
357 } | 357 } |
358 } | 358 } |
359 | 359 |
360 void QuicPacketCreator::ClearPacket() { | 360 void QuicPacketCreator::ClearPacket() { |
361 packet_.has_ack = false; | 361 packet_.has_ack = false; |
362 packet_.has_stop_waiting = false; | 362 packet_.has_stop_waiting = false; |
363 packet_.has_crypto_handshake = NOT_HANDSHAKE; | 363 packet_.has_crypto_handshake = NOT_HANDSHAKE; |
364 packet_.needs_padding = false; | 364 packet_.num_padding_bytes = 0; |
365 packet_.original_packet_number = 0; | 365 packet_.original_packet_number = 0; |
366 packet_.transmission_type = NOT_RETRANSMISSION; | 366 packet_.transmission_type = NOT_RETRANSMISSION; |
367 packet_.encrypted_buffer = nullptr; | 367 packet_.encrypted_buffer = nullptr; |
368 packet_.encrypted_length = 0; | 368 packet_.encrypted_length = 0; |
369 DCHECK(packet_.retransmittable_frames.empty()); | 369 DCHECK(packet_.retransmittable_frames.empty()); |
370 packet_.listeners.clear(); | 370 packet_.listeners.clear(); |
371 } | 371 } |
372 | 372 |
373 bool QuicPacketCreator::HasPendingFrames() const { | 373 bool QuicPacketCreator::HasPendingFrames() const { |
374 return !queued_frames_.empty(); | 374 return !queued_frames_.empty(); |
(...skipping 28 matching lines...) Expand all Loading... |
403 IncludeNonceInPublicHeader(), packet_.packet_number_length); | 403 IncludeNonceInPublicHeader(), packet_.packet_number_length); |
404 return packet_size_; | 404 return packet_size_; |
405 } | 405 } |
406 | 406 |
407 bool QuicPacketCreator::AddSavedFrame(const QuicFrame& frame) { | 407 bool QuicPacketCreator::AddSavedFrame(const QuicFrame& frame) { |
408 return AddFrame(frame, /*save_retransmittable_frames=*/true); | 408 return AddFrame(frame, /*save_retransmittable_frames=*/true); |
409 } | 409 } |
410 | 410 |
411 bool QuicPacketCreator::AddPaddedSavedFrame(const QuicFrame& frame) { | 411 bool QuicPacketCreator::AddPaddedSavedFrame(const QuicFrame& frame) { |
412 if (AddFrame(frame, /*save_retransmittable_frames=*/true)) { | 412 if (AddFrame(frame, /*save_retransmittable_frames=*/true)) { |
413 packet_.needs_padding = true; | 413 packet_.num_padding_bytes = -1; |
414 return true; | 414 return true; |
415 } | 415 } |
416 return false; | 416 return false; |
417 } | 417 } |
418 | 418 |
419 void QuicPacketCreator::AddAckListener(QuicAckListenerInterface* listener, | 419 void QuicPacketCreator::AddAckListener(QuicAckListenerInterface* listener, |
420 QuicPacketLength length) { | 420 QuicPacketLength length) { |
421 DCHECK(!queued_frames_.empty()); | 421 DCHECK(!queued_frames_.empty()); |
422 packet_.listeners.emplace_back(listener, length); | 422 packet_.listeners.emplace_back(listener, length); |
423 } | 423 } |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
565 packet_.has_stop_waiting = true; | 565 packet_.has_stop_waiting = true; |
566 } | 566 } |
567 if (debug_delegate_ != nullptr) { | 567 if (debug_delegate_ != nullptr) { |
568 debug_delegate_->OnFrameAddedToPacket(frame); | 568 debug_delegate_->OnFrameAddedToPacket(frame); |
569 } | 569 } |
570 | 570 |
571 return true; | 571 return true; |
572 } | 572 } |
573 | 573 |
574 void QuicPacketCreator::MaybeAddPadding() { | 574 void QuicPacketCreator::MaybeAddPadding() { |
575 if (!packet_.needs_padding) { | 575 if (packet_.num_padding_bytes == 0) { |
576 return; | 576 return; |
577 } | 577 } |
578 | 578 |
579 if (BytesFree() == 0) { | 579 if (BytesFree() == 0) { |
580 // Don't pad full packets. | 580 // Don't pad full packets. |
581 return; | 581 return; |
582 } | 582 } |
583 | 583 |
584 bool success = AddFrame(QuicFrame(QuicPaddingFrame()), false); | 584 bool success = |
| 585 AddFrame(QuicFrame(QuicPaddingFrame(packet_.num_padding_bytes)), false); |
585 DCHECK(success); | 586 DCHECK(success); |
586 } | 587 } |
587 | 588 |
588 void QuicPacketCreator::SetCurrentPath( | 589 void QuicPacketCreator::SetCurrentPath( |
589 QuicPathId path_id, | 590 QuicPathId path_id, |
590 QuicPacketNumber least_packet_awaited_by_peer, | 591 QuicPacketNumber least_packet_awaited_by_peer, |
591 QuicPacketCount max_packets_in_flight) { | 592 QuicPacketCount max_packets_in_flight) { |
592 if (packet_.path_id == path_id) { | 593 if (packet_.path_id == path_id) { |
593 return; | 594 return; |
594 } | 595 } |
(...skipping 15 matching lines...) Expand all Loading... |
610 // Switching path needs to update packet number length. | 611 // Switching path needs to update packet number length. |
611 UpdatePacketNumberLength(least_packet_awaited_by_peer, max_packets_in_flight); | 612 UpdatePacketNumberLength(least_packet_awaited_by_peer, max_packets_in_flight); |
612 } | 613 } |
613 | 614 |
614 bool QuicPacketCreator::IncludeNonceInPublicHeader() { | 615 bool QuicPacketCreator::IncludeNonceInPublicHeader() { |
615 return have_diversification_nonce_ && | 616 return have_diversification_nonce_ && |
616 packet_.encryption_level == ENCRYPTION_INITIAL; | 617 packet_.encryption_level == ENCRYPTION_INITIAL; |
617 } | 618 } |
618 | 619 |
619 } // namespace net | 620 } // namespace net |
OLD | NEW |