Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(164)

Side by Side Diff: chrome/browser/extensions/api/cast_channel/cast_socket.cc

Issue 408633002: Fix error handling for message parse errors that occur during connection setup. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Allow read event loop to close socket on error. Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "chrome/browser/extensions/api/cast_channel/cast_socket.h" 5 #include "chrome/browser/extensions/api/cast_channel/cast_socket.h"
6 6
7 #include <stdlib.h> 7 #include <stdlib.h>
8 #include <string.h> 8 #include <string.h>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 return false; 151 return false;
152 bool result = net::X509Certificate::GetDEREncoded( 152 bool result = net::X509Certificate::GetDEREncoded(
153 ssl_info.cert->os_cert_handle(), cert); 153 ssl_info.cert->os_cert_handle(), cert);
154 if (result) 154 if (result)
155 VLOG_WITH_CONNECTION(1) << "Successfully extracted peer certificate: " 155 VLOG_WITH_CONNECTION(1) << "Successfully extracted peer certificate: "
156 << *cert; 156 << *cert;
157 return result; 157 return result;
158 } 158 }
159 159
160 bool CastSocket::VerifyChallengeReply() { 160 bool CastSocket::VerifyChallengeReply() {
161 return AuthenticateChallengeReply(*challenge_reply_.get(), peer_cert_); 161 return AuthenticateChallengeReply(*challenge_reply_, peer_cert_);
mark a. foltz 2014/07/21 17:38:52 Thank you for fixing this, in my email reply I for
Kevin M 2014/07/21 20:48:58 Acknowledged.
162 } 162 }
163 163
164 void CastSocket::Connect(const net::CompletionCallback& callback) { 164 void CastSocket::Connect(const net::CompletionCallback& callback) {
165 DCHECK(CalledOnValidThread()); 165 DCHECK(CalledOnValidThread());
166 VLOG_WITH_CONNECTION(1) << "Connect readyState = " << ready_state_; 166 VLOG_WITH_CONNECTION(1) << "Connect readyState = " << ready_state_;
167 if (ready_state_ != READY_STATE_NONE) { 167 if (ready_state_ != READY_STATE_NONE) {
168 callback.Run(net::ERR_CONNECTION_FAILED); 168 callback.Run(net::ERR_CONNECTION_FAILED);
169 return; 169 return;
170 } 170 }
171 ready_state_ = READY_STATE_CONNECTING; 171 ready_state_ = READY_STATE_CONNECTING;
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after
544 CloseWithError(error_state_); 544 CloseWithError(error_state_);
545 } 545 }
546 546
547 int CastSocket::DoRead() { 547 int CastSocket::DoRead() {
548 read_state_ = READ_STATE_READ_COMPLETE; 548 read_state_ = READ_STATE_READ_COMPLETE;
549 // Figure out whether to read header or body, and the remaining bytes. 549 // Figure out whether to read header or body, and the remaining bytes.
550 uint32 num_bytes_to_read = 0; 550 uint32 num_bytes_to_read = 0;
551 if (header_read_buffer_->RemainingCapacity() > 0) { 551 if (header_read_buffer_->RemainingCapacity() > 0) {
552 current_read_buffer_ = header_read_buffer_; 552 current_read_buffer_ = header_read_buffer_;
553 num_bytes_to_read = header_read_buffer_->RemainingCapacity(); 553 num_bytes_to_read = header_read_buffer_->RemainingCapacity();
554 DCHECK_LE(num_bytes_to_read, MessageHeader::header_size()); 554 DCHECK_LE(num_bytes_to_read, MessageHeader::header_size());
mark a. foltz 2014/07/21 17:38:52 We should audit the use of DCHECKs in the read and
Kevin M 2014/07/21 20:48:58 Done here and elsewhere in places that deal with s
Wez 2014/07/22 22:33:09 Rather than [D]CHECK(), we could do the following:
Kevin M 2014/07/23 18:57:33 Done.
555 } else { 555 } else {
556 DCHECK_GT(current_message_size_, 0U); 556 DCHECK_GT(current_message_size_, 0U);
557 num_bytes_to_read = current_message_size_ - body_read_buffer_->offset(); 557 num_bytes_to_read = current_message_size_ - body_read_buffer_->offset();
558 current_read_buffer_ = body_read_buffer_; 558 current_read_buffer_ = body_read_buffer_;
559 DCHECK_LE(num_bytes_to_read, MessageHeader::max_message_size()); 559 DCHECK_LE(num_bytes_to_read, MessageHeader::max_message_size());
560 } 560 }
561 DCHECK_GT(num_bytes_to_read, 0U); 561 DCHECK_GT(num_bytes_to_read, 0U);
562 562
563 // Read up to num_bytes_to_read into |current_read_buffer_|. 563 // Read up to num_bytes_to_read into |current_read_buffer_|.
564 return socket_->Read( 564 return socket_->Read(
(...skipping 19 matching lines...) Expand all
584 current_read_buffer_->capacity()); 584 current_read_buffer_->capacity());
585 current_read_buffer_->set_offset(current_read_buffer_->offset() + result); 585 current_read_buffer_->set_offset(current_read_buffer_->offset() + result);
586 read_state_ = READ_STATE_READ; 586 read_state_ = READ_STATE_READ;
587 587
588 if (current_read_buffer_.get() == header_read_buffer_.get() && 588 if (current_read_buffer_.get() == header_read_buffer_.get() &&
589 current_read_buffer_->RemainingCapacity() == 0) { 589 current_read_buffer_->RemainingCapacity() == 0) {
590 // A full header is read, process the contents. 590 // A full header is read, process the contents.
591 if (!ProcessHeader()) { 591 if (!ProcessHeader()) {
592 error_state_ = cast_channel::CHANNEL_ERROR_INVALID_MESSAGE; 592 error_state_ = cast_channel::CHANNEL_ERROR_INVALID_MESSAGE;
593 read_state_ = READ_STATE_ERROR; 593 read_state_ = READ_STATE_ERROR;
594 return net::ERR_FAILED;
Wez 2014/07/22 22:33:09 IIUC this change will mean we get read error notif
Kevin M 2014/07/23 18:57:33 This was removed.
594 } 595 }
595 } else if (current_read_buffer_.get() == body_read_buffer_.get() && 596 } else if (current_read_buffer_.get() == body_read_buffer_.get() &&
596 static_cast<uint32>(current_read_buffer_->offset()) == 597 static_cast<uint32>(current_read_buffer_->offset()) ==
597 current_message_size_) { 598 current_message_size_) {
598 // Full body is read, process the contents. 599 // Full body is read, process the contents.
599 if (ProcessBody()) { 600 if (ProcessBody()) {
600 read_state_ = READ_STATE_DO_CALLBACK; 601 read_state_ = READ_STATE_DO_CALLBACK;
601 } else { 602 } else {
602 error_state_ = cast_channel::CHANNEL_ERROR_INVALID_MESSAGE; 603 error_state_ = cast_channel::CHANNEL_ERROR_INVALID_MESSAGE;
603 read_state_ = READ_STATE_ERROR; 604 read_state_ = READ_STATE_ERROR;
605 return net::ERR_FAILED;
604 } 606 }
605 } 607 }
606 608
607 return net::OK; 609 return net::OK;
608 } 610 }
609 611
610 int CastSocket::DoReadCallback() { 612 int CastSocket::DoReadCallback() {
611 read_state_ = READ_STATE_READ; 613 read_state_ = READ_STATE_READ;
612 const CastMessage& message = *(current_message_.get()); 614 const CastMessage& message = *current_message_;
613 if (IsAuthMessage(message)) { 615 if (IsAuthMessage(message)) {
614 // An auth message is received, check that connect flow is running. 616 // An auth message is received, check that connect flow is running.
615 if (ready_state_ == READY_STATE_CONNECTING) { 617 if (ready_state_ == READY_STATE_CONNECTING) {
616 challenge_reply_.reset(new CastMessage(message)); 618 challenge_reply_.reset(new CastMessage(message));
617 PostTaskToStartConnectLoop(net::OK); 619 PostTaskToStartConnectLoop(net::OK);
618 } else { 620 } else {
619 read_state_ = READ_STATE_ERROR; 621 read_state_ = READ_STATE_ERROR;
mark a. foltz 2014/07/21 17:38:52 This is another place we need to abort the read lo
Kevin M 2014/07/21 20:48:57 Already done - setting read_state_ to READ_STATE_E
620 } 622 }
621 } else if (delegate_) { 623 } else if (delegate_) {
622 MessageInfo message_info; 624 MessageInfo message_info;
623 if (CastMessageToMessageInfo(message, &message_info)) 625 if (CastMessageToMessageInfo(message, &message_info))
624 delegate_->OnMessage(this, message_info); 626 delegate_->OnMessage(this, message_info);
625 else 627 else
626 read_state_ = READ_STATE_ERROR; 628 read_state_ = READ_STATE_ERROR;
mark a. foltz 2014/07/21 17:38:52 Ditto
Kevin M 2014/07/21 20:48:57 Acknowledged.
627 } 629 }
628 current_message_->Clear(); 630 current_message_->Clear();
629 return net::OK; 631 return net::OK;
630 } 632 }
631 633
632 int CastSocket::DoReadError(int result) { 634 int CastSocket::DoReadError(int result) {
635 VLOG_WITH_CONNECTION(1) << "DoReadError: " << result;
633 DCHECK_LE(result, 0); 636 DCHECK_LE(result, 0);
637 read_state_ = READ_STATE_NONE;
634 // If inside connection flow, then get back to connect loop. 638 // If inside connection flow, then get back to connect loop.
mark a. foltz 2014/07/21 17:38:52 I don't fully understand this piece of logic. A re
Kevin M 2014/07/21 20:48:58 Changed the flow to call the completion callback d
mark a. foltz 2014/07/21 21:23:00 I'm not 100% comfortable changing the control flow
Kevin M 2014/07/21 23:50:31 Done. OK, back to the previous method.
635 if (ready_state_ == READY_STATE_CONNECTING) { 639 if (ready_state_ == READY_STATE_CONNECTING) {
636 PostTaskToStartConnectLoop(result); 640 PostTaskToStartConnectLoop(result);
637 // does not try to report error also.
Wez 2014/07/22 22:33:09 This comment implies that if we return something o
638 return net::OK;
639 } 641 }
642 // Allow the read loop error handler to close the connection.
640 return net::ERR_FAILED; 643 return net::ERR_FAILED;
641 } 644 }
642 645
643 bool CastSocket::ProcessHeader() { 646 bool CastSocket::ProcessHeader() {
644 DCHECK_EQ(static_cast<uint32>(header_read_buffer_->offset()), 647 DCHECK_EQ(static_cast<uint32>(header_read_buffer_->offset()),
645 MessageHeader::header_size()); 648 MessageHeader::header_size());
646 MessageHeader header; 649 MessageHeader header;
647 MessageHeader::ReadFromIOBuffer(header_read_buffer_.get(), &header); 650 MessageHeader::ReadFromIOBuffer(header_read_buffer_.get(), &header);
648 if (header.message_size > MessageHeader::max_message_size()) 651 if (header.message_size > MessageHeader::max_message_size())
649 return false; 652 return false;
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
751 return true; 754 return true;
752 } 755 }
753 756
754 CastSocket::WriteRequest::~WriteRequest() { } 757 CastSocket::WriteRequest::~WriteRequest() { }
755 758
756 } // namespace cast_channel 759 } // namespace cast_channel
757 } // namespace api 760 } // namespace api
758 } // namespace extensions 761 } // namespace extensions
759 762
760 #undef VLOG_WITH_CONNECTION 763 #undef VLOG_WITH_CONNECTION
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698