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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « remoting/host/gnubby_socket.h ('k') | remoting/host/gnubby_util.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 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 "remoting/host/gnubby_socket.h"
6
7 #include "base/timer/timer.h"
8 #include "net/socket/stream_listen_socket.h"
9
10 namespace remoting {
11
12 namespace {
13
14 const size_t kRequestSizeBytes = 4;
15 const size_t kMaxRequestLength = 16384;
16 const unsigned int kRequestTimeoutSeconds = 60;
17
18 // SSH Failure Code
19 const char kSshError[] = {0x05};
20
21 } // namespace
22
23 GnubbySocket::GnubbySocket(scoped_ptr<net::StreamListenSocket> socket,
24 const base::Closure& timeout_callback)
25 : socket_(socket.Pass()) {
26 timer_.reset(new base::Timer(false, false));
27 timer_->Start(FROM_HERE,
28 base::TimeDelta::FromSeconds(kRequestTimeoutSeconds),
29 timeout_callback);
30 }
31
32 GnubbySocket::~GnubbySocket() {}
33
34 void GnubbySocket::AddRequestData(const char* data, int data_len) {
35 DCHECK(CalledOnValidThread());
36
37 request_data_.insert(request_data_.end(), data, data + data_len);
38 ResetTimer();
39 }
40
41 void GnubbySocket::GetAndClearRequestData(std::string* data_out) {
42 DCHECK(CalledOnValidThread());
43 DCHECK(IsRequestComplete() && !IsRequestTooLarge());
44
45 // The request size is not part of the data; don't send it.
46 data_out->assign(request_data_.begin() + kRequestSizeBytes,
47 request_data_.end());
48 request_data_.clear();
49 }
50
51 bool GnubbySocket::IsRequestComplete() const {
52 DCHECK(CalledOnValidThread());
53
54 if (request_data_.size() < kRequestSizeBytes)
55 return false;
56 return GetRequestLength() <= request_data_.size();
57 }
58
59 bool GnubbySocket::IsRequestTooLarge() const {
60 DCHECK(CalledOnValidThread());
61
62 if (request_data_.size() < kRequestSizeBytes)
63 return false;
64 return GetRequestLength() > kMaxRequestLength;
65 }
66
67 void GnubbySocket::SendResponse(const std::string& response_data) {
68 DCHECK(CalledOnValidThread());
69
70 socket_->Send(GetResponseLengthAsBytes(response_data));
71 socket_->Send(response_data);
72 ResetTimer();
73 }
74
75 void GnubbySocket::SendSshError() {
76 DCHECK(CalledOnValidThread());
77
78 SendResponse(kSshError);
79 }
80
81 bool GnubbySocket::IsSocket(net::StreamListenSocket* socket) const {
82 return socket == socket_.get();
83 }
84
85 void GnubbySocket::SetTimerForTesting(scoped_ptr<base::Timer> timer) {
86 timer->Start(FROM_HERE, timer_->GetCurrentDelay(), timer_->user_task());
87 timer_ = timer.Pass();
88 }
89
90 size_t GnubbySocket::GetRequestLength() const {
91 DCHECK(request_data_.size() >= kRequestSizeBytes);
92
93 return ((request_data_[0] & 255) << 24) + ((request_data_[1] & 255) << 16) +
94 ((request_data_[2] & 255) << 8) + (request_data_[3] & 255) +
95 kRequestSizeBytes;
96 }
97
98 std::string GnubbySocket::GetResponseLengthAsBytes(
99 const std::string& response) const {
100 std::string response_len;
101 int len = response.size();
102
103 response_len.push_back((len >> 24) & 255);
104 response_len.push_back((len >> 16) & 255);
105 response_len.push_back((len >> 8) & 255);
106 response_len.push_back(len & 255);
107
108 return response_len;
109 }
110
111 void GnubbySocket::ResetTimer() {
112 if (timer_->IsRunning())
113 timer_->Reset();
114 }
115
116 } // namespace remoting
OLDNEW
« 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