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

Unified Diff: remoting/host/gnubby_socket.cc

Issue 205493005: Do minimal processing of gnubby data. Add request timeouts and send error (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address review comments Created 6 years, 9 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « remoting/host/gnubby_socket.h ('k') | remoting/host/gnubby_util.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: remoting/host/gnubby_socket.cc
diff --git a/remoting/host/gnubby_socket.cc b/remoting/host/gnubby_socket.cc
new file mode 100644
index 0000000000000000000000000000000000000000..0337b61de01f156c2e73a2cfabf724ce3bdf5266
--- /dev/null
+++ b/remoting/host/gnubby_socket.cc
@@ -0,0 +1,111 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "remoting/host/gnubby_socket.h"
+
+#include "base/macros.h"
+
+namespace remoting {
+
+namespace {
+
+const size_t kRequestSizeBytes = 4;
+const size_t kMaxRequestLength = 16384;
+const unsigned int kRequestTimeoutSeconds = 60;
+
+// Length-prefixed SSH Failure Code
+const char kSshError[] = {0x00, 0x00, 0x00, 0x01, 0x05};
+
+} // namespace
+
+GnubbySocket::GnubbySocket(scoped_ptr<net::StreamListenSocket> socket,
+ const base::Closure& timeout_callback)
+ : socket_(socket.Pass()) {
+ timer_.reset(new base::Timer(false, false));
+ timer_->Start(FROM_HERE,
+ base::TimeDelta::FromSeconds(kRequestTimeoutSeconds),
+ timeout_callback);
+}
+
+void GnubbySocket::AddRequestData(const char* data, int data_len) {
+ DCHECK(CalledOnValidThread());
+
+ request_data_.insert(request_data_.end(), data, data + data_len);
+ ResetTimer();
+}
+
+void GnubbySocket::GetAndClearRequestData(std::string* data_out) {
+ DCHECK(CalledOnValidThread());
+ DCHECK(IsRequestComplete() && !IsRequestTooLarge());
+
+ // The request size is not part of the data; don't send it.
+ data_out->assign(request_data_.begin() + kRequestSizeBytes,
+ request_data_.end());
+ request_data_.clear();
+}
+
+bool GnubbySocket::IsRequestComplete() const {
+ DCHECK(CalledOnValidThread());
+
+ if (request_data_.size() < kRequestSizeBytes)
+ return false;
+ return GetRequestLength() <= request_data_.size();
+}
+
+bool GnubbySocket::IsRequestTooLarge() const {
+ DCHECK(CalledOnValidThread());
+
+ if (request_data_.size() < kRequestSizeBytes)
+ return false;
+ return GetRequestLength() > kMaxRequestLength;
+}
+
+void GnubbySocket::SendResponse(const std::string& response_data) {
+ DCHECK(CalledOnValidThread());
+
+ socket_->Send(GetResponseLengthAsBytes(response_data));
+ socket_->Send(response_data);
+ ResetTimer();
+}
+
+void GnubbySocket::SendSshError() {
+ DCHECK(CalledOnValidThread());
+
+ socket_->Send(kSshError, arraysize(kSshError));
Sergey Ulanov 2014/03/21 22:53:16 Can this invoke SendResponse?
psj 2014/03/21 23:24:17 Done.
+}
+
+bool GnubbySocket::IsSocket(net::StreamListenSocket* socket) const {
+ return socket == socket_.get();
+}
+
+void GnubbySocket::SetTimerForTesting(scoped_ptr<base::Timer> timer) {
+ timer->Start(FROM_HERE, timer_->GetCurrentDelay(), timer_->user_task());
+ timer_ = timer.Pass();
+}
+
+size_t GnubbySocket::GetRequestLength() const {
+ DCHECK(request_data_.size() >= kRequestSizeBytes);
+
+ return ((request_data_[0] & 255) << 24) + ((request_data_[1] & 255) << 16) +
+ ((request_data_[2] & 255) << 8) + (request_data_[3] & 255) + 4;
Sergey Ulanov 2014/03/21 22:53:16 +kRequestSizeBytes at the end
psj 2014/03/21 23:24:17 Arggggg, I was sure I had gotten all of the 4's...
+}
+
+std::string GnubbySocket::GetResponseLengthAsBytes(
+ const std::string& response) const {
+ std::string response_len;
+ int len = response.size();
+
+ response_len.push_back((len >> 24) & 255);
+ response_len.push_back((len >> 16) & 255);
+ response_len.push_back((len >> 8) & 255);
+ response_len.push_back(len & 255);
+
+ return response_len;
+}
+
+void GnubbySocket::ResetTimer() {
+ timer_->Reset();
+}
+
+} // namespace remoting
« no previous file with comments | « remoting/host/gnubby_socket.h ('k') | remoting/host/gnubby_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698