Chromium Code Reviews| 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..4c5f8bb1a7691c834979b0f24bfe45b81bcd967f |
| --- /dev/null |
| +++ b/remoting/host/gnubby_socket.cc |
| @@ -0,0 +1,102 @@ |
| +// 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" |
| + |
| +namespace remoting { |
| + |
| +namespace { |
| + |
| +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()); |
| + |
|
Sergey Ulanov
2014/03/21 02:09:37
Add DCHECK(IsRequestComplete() && !IsRequestTooLar
psj
2014/03/21 21:30:45
Done.
|
| + // The first four bytes are not part of the request and need to be stripped. |
| + data_out->assign(request_data_.data() + 4, request_data_.size() - 4); |
|
Sergey Ulanov
2014/03/21 02:09:37
define a const for 4, e.g. kRequestSizeBytes
Sergey Ulanov
2014/03/21 02:09:37
You can replace the last parameter with request_da
psj
2014/03/21 21:30:45
Done.
psj
2014/03/21 21:30:45
Done.
|
| + request_data_.clear(); |
| +} |
| + |
| +bool GnubbySocket::IsRequestComplete() const { |
| + DCHECK(CalledOnValidThread()); |
| + |
| + if (request_data_.size() < 4) |
|
Sergey Ulanov
2014/03/21 02:09:37
define a const for 4, e.g. kRequestSizeBytes
psj
2014/03/21 21:30:45
Done.
|
| + return false; |
| + return GetRequestLength() <= request_data_.size(); |
| +} |
| + |
| +bool GnubbySocket::IsRequestTooLarge() const { |
| + DCHECK(CalledOnValidThread()); |
| + |
| + if (request_data_.size() < 4) |
| + 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, sizeof(kSshError) / sizeof(*kSshError)); |
|
Sergey Ulanov
2014/03/21 02:09:37
Use arraysize() from base/macros.h
psj
2014/03/21 21:30:45
Done.
|
| +} |
| + |
| +bool GnubbySocket::IsSocket(net::StreamListenSocket* socket) const { |
| + return socket == socket_.get(); |
| +} |
| + |
| +void GnubbySocket::SetTimerForTesting(base::Timer* timer) { |
| + timer->Start(FROM_HERE, timer_->GetCurrentDelay(), timer_->user_task()); |
| + timer_.reset(timer); |
| +} |
| + |
| +size_t GnubbySocket::GetRequestLength() const { |
| + return ((request_data_[0] & 255) << 24) + ((request_data_[1] & 255) << 16) + |
|
Sergey Ulanov
2014/03/21 02:09:37
DCHECK that there are at least 4 bytes in |request
psj
2014/03/21 21:30:45
Done.
|
| + ((request_data_[2] & 255) << 8) + (request_data_[3] & 255) + 4; |
| +} |
| + |
| +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(); } |
|
Sergey Ulanov
2014/03/21 02:09:37
move the body to a separate line
psj
2014/03/21 21:30:45
Done. clang_format put it on one line...
|
| + |
| +} // namespace remoting |