OLD | NEW |
---|---|
(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 namespace remoting { | |
8 | |
9 namespace { | |
10 | |
11 const size_t kMaxRequestLength = 16384; | |
12 const unsigned int kRequestTimeoutSeconds = 60; | |
13 | |
14 // Length-prefixed SSH Failure Code | |
15 const char kSshError[] = {0x00, 0x00, 0x00, 0x01, 0x05}; | |
16 | |
17 } // namespace | |
18 | |
19 GnubbySocket::GnubbySocket(scoped_ptr<net::StreamListenSocket> socket, | |
20 const base::Closure& timeout_callback) | |
21 : socket_(socket.Pass()) { | |
22 timer_.reset(new base::Timer(false, false)); | |
23 timer_->Start(FROM_HERE, | |
24 base::TimeDelta::FromSeconds(kRequestTimeoutSeconds), | |
25 timeout_callback); | |
26 } | |
27 | |
28 void GnubbySocket::AddRequestData(const char* data, int data_len) { | |
29 DCHECK(CalledOnValidThread()); | |
30 | |
31 request_data_.insert(request_data_.end(), data, data + data_len); | |
32 ResetTimer(); | |
33 } | |
34 | |
35 void GnubbySocket::GetAndClearRequestData(std::string* data_out) { | |
36 DCHECK(CalledOnValidThread()); | |
37 | |
Sergey Ulanov
2014/03/21 02:09:37
Add DCHECK(IsRequestComplete() && !IsRequestTooLar
psj
2014/03/21 21:30:45
Done.
| |
38 // The first four bytes are not part of the request and need to be stripped. | |
39 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.
| |
40 request_data_.clear(); | |
41 } | |
42 | |
43 bool GnubbySocket::IsRequestComplete() const { | |
44 DCHECK(CalledOnValidThread()); | |
45 | |
46 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.
| |
47 return false; | |
48 return GetRequestLength() <= request_data_.size(); | |
49 } | |
50 | |
51 bool GnubbySocket::IsRequestTooLarge() const { | |
52 DCHECK(CalledOnValidThread()); | |
53 | |
54 if (request_data_.size() < 4) | |
55 return false; | |
56 return GetRequestLength() > kMaxRequestLength; | |
57 } | |
58 | |
59 void GnubbySocket::SendResponse(const std::string& response_data) { | |
60 DCHECK(CalledOnValidThread()); | |
61 | |
62 socket_->Send(GetResponseLengthAsBytes(response_data)); | |
63 socket_->Send(response_data); | |
64 ResetTimer(); | |
65 } | |
66 | |
67 void GnubbySocket::SendSshError() { | |
68 DCHECK(CalledOnValidThread()); | |
69 | |
70 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.
| |
71 } | |
72 | |
73 bool GnubbySocket::IsSocket(net::StreamListenSocket* socket) const { | |
74 return socket == socket_.get(); | |
75 } | |
76 | |
77 void GnubbySocket::SetTimerForTesting(base::Timer* timer) { | |
78 timer->Start(FROM_HERE, timer_->GetCurrentDelay(), timer_->user_task()); | |
79 timer_.reset(timer); | |
80 } | |
81 | |
82 size_t GnubbySocket::GetRequestLength() const { | |
83 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.
| |
84 ((request_data_[2] & 255) << 8) + (request_data_[3] & 255) + 4; | |
85 } | |
86 | |
87 std::string GnubbySocket::GetResponseLengthAsBytes( | |
88 const std::string& response) const { | |
89 std::string response_len; | |
90 int len = response.size(); | |
91 | |
92 response_len.push_back((len >> 24) & 255); | |
93 response_len.push_back((len >> 16) & 255); | |
94 response_len.push_back((len >> 8) & 255); | |
95 response_len.push_back(len & 255); | |
96 | |
97 return response_len; | |
98 } | |
99 | |
100 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...
| |
101 | |
102 } // namespace remoting | |
OLD | NEW |