-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathadb_client_socket.cc
652 lines (584 loc) · 22.7 KB
/
adb_client_socket.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "chrome/test/chromedriver/net/adb_client_socket.h"
#include <stddef.h>
#include <memory>
#include <utility>
#include "base/compiler_specific.h"
#include "base/containers/span.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "net/base/address_list.h"
#include "net/base/completion_repeating_callback.h"
#include "net/base/ip_address.h"
#include "net/base/net_errors.h"
#include "net/log/net_log_source.h"
#include "net/socket/tcp_client_socket.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "third_party/blink/public/public_buildflags.h"
namespace {
const int kBufferSize = 16 * 1024;
const int kBufferGrowthRate = 16 * 1024;
const size_t kAdbDataChunkSize = 32 * 1024;
const char kOkayResponse[] = "OKAY";
const char kFailResponse[] = "FAIL";
const char kHostTransportCommand[] = "host:transport:%s";
const char kLocalAbstractCommand[] = "localabstract:%s";
const char kSyncCommand[] = "sync:";
const char kSendCommand[] = "SEND";
const char kDataCommand[] = "DATA";
const char kDoneCommand[] = "DONE";
const int kAdbFailure = 1;
const int kAdbSuccess = 0;
typedef base::RepeatingCallback<void(int, const std::string&)> CommandCallback;
typedef base::RepeatingCallback<void(int, net::StreamSocket*)> SocketCallback;
typedef base::RepeatingCallback<void(const std::string&)> ParserCallback;
std::string EncodeMessage(const std::string& message) {
size_t length = message.length();
CHECK_LE(length, 0xffffu);
std::string result;
result.reserve(4);
base::AppendHexEncodedByte(reinterpret_cast<const uint8_t*>(&length)[1],
result);
base::AppendHexEncodedByte(reinterpret_cast<const uint8_t*>(&length)[0],
result);
return result + message;
}
class AdbTransportSocket : public AdbClientSocket {
public:
AdbTransportSocket(int port,
const std::string& serial,
const std::string& socket_name,
const SocketCallback& callback)
: AdbClientSocket(port),
serial_(serial),
socket_name_(socket_name),
callback_(callback) {
Connect(base::BindOnce(&AdbTransportSocket::OnConnected,
base::Unretained(this)));
}
private:
~AdbTransportSocket() = default;
void OnConnected(int result) {
if (!CheckNetResultOrDie(result))
return;
SendCommand(base::StringPrintf(kHostTransportCommand, serial_.c_str()),
false, true,
base::BindRepeating(&AdbTransportSocket::SendLocalAbstract,
base::Unretained(this)));
}
void SendLocalAbstract(int result, const std::string& response) {
if (!CheckNetResultOrDie(result))
return;
SendCommand(base::StringPrintf(kLocalAbstractCommand, socket_name_.c_str()),
false, true,
base::BindRepeating(&AdbTransportSocket::OnSocketAvailable,
base::Unretained(this)));
}
void OnSocketAvailable(int result, const std::string& response) {
if (!CheckNetResultOrDie(result))
return;
callback_.Run(net::OK, socket_.release());
delete this;
}
bool CheckNetResultOrDie(int result) {
if (result >= 0)
return true;
callback_.Run(result, NULL);
delete this;
return false;
}
std::string serial_;
std::string socket_name_;
SocketCallback callback_;
};
class HttpOverAdbSocket {
public:
HttpOverAdbSocket(int port,
const std::string& serial,
const std::string& socket_name,
const std::string& request,
const CommandCallback& callback)
: request_(request),
command_callback_(callback),
body_pos_(0) {
Connect(port, serial, socket_name);
}
HttpOverAdbSocket(int port,
const std::string& serial,
const std::string& socket_name,
const std::string& request,
const SocketCallback& callback)
: request_(request),
socket_callback_(callback),
body_pos_(0) {
Connect(port, serial, socket_name);
}
private:
~HttpOverAdbSocket() = default;
void Connect(int port,
const std::string& serial,
const std::string& socket_name) {
AdbClientSocket::TransportQuery(
port, serial, socket_name,
base::BindRepeating(&HttpOverAdbSocket::OnSocketAvailable,
base::Unretained(this)));
}
void OnSocketAvailable(int result,
net::StreamSocket* socket) {
if (!CheckNetResultOrDie(result))
return;
socket_.reset(socket);
scoped_refptr<net::StringIOBuffer> request_buffer =
base::MakeRefCounted<net::StringIOBuffer>(request_);
result = socket_->Write(request_buffer.get(), request_buffer->size(),
base::BindOnce(&HttpOverAdbSocket::ReadResponse,
base::Unretained(this)),
TRAFFIC_ANNOTATION_FOR_TESTS);
if (result != net::ERR_IO_PENDING)
ReadResponse(result);
}
void ReadResponse(int result) {
if (!CheckNetResultOrDie(result))
return;
auto response_buffer =
base::MakeRefCounted<net::IOBufferWithSize>(kBufferSize);
result = socket_->Read(
response_buffer.get(), kBufferSize,
base::BindOnce(&HttpOverAdbSocket::OnResponseData,
base::Unretained(this), response_buffer, -1));
if (result != net::ERR_IO_PENDING)
OnResponseData(response_buffer, -1, result);
}
void OnResponseData(scoped_refptr<net::IOBuffer> response_buffer,
int bytes_total,
int result) {
if (!CheckNetResultOrDie(result))
return;
if (result == 0) {
CheckNetResultOrDie(net::ERR_CONNECTION_CLOSED);
return;
}
response_ += std::string(response_buffer->data(), result);
int expected_length = 0;
if (bytes_total < 0) {
size_t content_pos = response_.find("Content-Length:");
if (content_pos != std::string::npos) {
size_t endline_pos = response_.find("\n", content_pos);
if (endline_pos != std::string::npos) {
std::string len = response_.substr(content_pos + 15,
endline_pos - content_pos - 15);
base::TrimWhitespaceASCII(len, base::TRIM_ALL, &len);
if (!base::StringToInt(len, &expected_length)) {
CheckNetResultOrDie(net::ERR_FAILED);
return;
}
}
}
body_pos_ = response_.find("\r\n\r\n");
if (body_pos_ != std::string::npos) {
body_pos_ += 4;
bytes_total = body_pos_ + expected_length;
}
}
if (bytes_total == static_cast<int>(response_.length())) {
if (!command_callback_.is_null())
command_callback_.Run(body_pos_, response_);
else
socket_callback_.Run(net::OK, socket_.release());
delete this;
return;
}
result = socket_->Read(
response_buffer.get(), kBufferSize,
base::BindOnce(&HttpOverAdbSocket::OnResponseData,
base::Unretained(this), response_buffer, bytes_total));
if (result != net::ERR_IO_PENDING)
OnResponseData(response_buffer, bytes_total, result);
}
bool CheckNetResultOrDie(int result) {
if (result >= 0)
return true;
if (!command_callback_.is_null())
command_callback_.Run(result, std::string());
else
socket_callback_.Run(result, NULL);
delete this;
return false;
}
std::unique_ptr<net::StreamSocket> socket_;
std::string request_;
std::string response_;
CommandCallback command_callback_;
SocketCallback socket_callback_;
size_t body_pos_;
};
class AdbQuerySocket : AdbClientSocket {
public:
AdbQuerySocket(int port,
const std::string& query,
const CommandCallback& callback)
: AdbClientSocket(port),
current_query_(0),
callback_(callback) {
queries_ = base::SplitString(
query, "|", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
if (queries_.empty()) {
CheckNetResultOrDie(net::ERR_INVALID_ARGUMENT);
return;
}
Connect(
base::BindOnce(&AdbQuerySocket::SendNextQuery, base::Unretained(this)));
}
private:
~AdbQuerySocket() = default;
void SendNextQuery(int result) {
if (!CheckNetResultOrDie(result))
return;
std::string query = queries_[current_query_];
if (query.length() > 0xFFFF) {
CheckNetResultOrDie(net::ERR_MSG_TOO_BIG);
return;
}
bool has_output = current_query_ == queries_.size() - 1;
// The |shell| command is a special case because it is the only command that
// doesn't include a length at the beginning of the data stream.
bool has_length =
!base::StartsWith(query, "shell:", base::CompareCase::SENSITIVE);
SendCommand(query, has_output, has_length,
base::BindRepeating(&AdbQuerySocket::OnResponse,
base::Unretained(this)));
}
void OnResponse(int result, const std::string& response) {
if (++current_query_ < queries_.size()) {
SendNextQuery(net::OK);
} else {
callback_.Run(result, response);
delete this;
}
}
bool CheckNetResultOrDie(int result) {
if (result >= 0)
return true;
callback_.Run(result, std::string());
delete this;
return false;
}
std::vector<std::string> queries_;
size_t current_query_;
CommandCallback callback_;
};
// Implement the ADB protocol to send a file to the device.
// The protocol consists of the following steps:
// * Send "host:transport" command with device serial
// * Send "sync:" command to initialize file transfer
// * Send "SEND" command with name and mode of the file
// * Send "DATA" command one or more times for the file content
// * Send "DONE" command to indicate end of file transfer
// The first two commands use normal ADB command format implemented by
// AdbClientSocket::SendCommand. The remaining commands use a special
// format implemented by AdbSendFileSocket::SendPayload.
class AdbSendFileSocket : AdbClientSocket {
public:
AdbSendFileSocket(int port,
const std::string& serial,
const std::string& filename,
const std::string& content,
const CommandCallback& callback)
: AdbClientSocket(port),
serial_(serial),
filename_(filename),
content_(content),
current_offset_(0),
callback_(callback) {
Connect(base::BindOnce(&AdbSendFileSocket::SendTransport,
base::Unretained(this)));
}
private:
~AdbSendFileSocket() = default;
void SendTransport(int result) {
if (!CheckNetResultOrDie(result))
return;
SendCommand(base::StringPrintf(kHostTransportCommand, serial_.c_str()),
false, true,
base::BindRepeating(&AdbSendFileSocket::SendSync,
base::Unretained(this)));
}
void SendSync(int result, const std::string& response) {
if (!CheckNetResultOrDie(result))
return;
SendCommand(kSyncCommand, false, true,
base::BindRepeating(&AdbSendFileSocket::SendSend,
base::Unretained(this)));
}
void SendSend(int result, const std::string& response) {
if (!CheckNetResultOrDie(result))
return;
// File mode. The following value is equivalent to S_IRUSR | S_IWUSR.
// Can't use the symbolic names since they are not available on Windows.
int mode = 0600;
std::string payload = base::StringPrintf("%s,%d", filename_.c_str(), mode);
SendPayload(kSendCommand, payload.length(), payload.c_str(),
payload.length(),
base::BindOnce(&AdbSendFileSocket::SendContent,
base::Unretained(this)));
}
void SendContent(int result) {
if (!CheckNetResultOrDie(result))
return;
if (current_offset_ >= content_.length()) {
SendDone();
return;
}
size_t offset = current_offset_;
size_t length = std::min(content_.length() - offset, kAdbDataChunkSize);
current_offset_ += length;
SendPayload(kDataCommand, length, content_.c_str() + offset, length,
base::BindOnce(&AdbSendFileSocket::SendContent,
base::Unretained(this)));
}
void SendDone() {
int data = time(nullptr);
SendPayload(kDoneCommand, data, nullptr, 0,
base::BindOnce(&AdbSendFileSocket::ReadFinalResponse,
base::Unretained(this)));
}
void ReadFinalResponse(int result) {
ReadResponse(callback_, false, false, result);
}
// Send a special payload command ("SEND", "DATA", or "DONE").
// Each command consists of a command line, followed by a 4-byte integer
// sent in raw little-endian format, followed by an optional payload.
void SendPayload(const char* command,
int data,
const char* payload,
size_t payload_length,
net::CompletionOnceCallback callback) {
std::string buffer(command);
for (int i = 0; i < 4; i++) {
buffer.append(1, static_cast<char>(data & 0xff));
data >>= 8;
}
if (payload_length > 0)
buffer.append(payload, payload_length);
scoped_refptr<net::StringIOBuffer> request_buffer =
base::MakeRefCounted<net::StringIOBuffer>(std::move(buffer));
auto split_callback = base::SplitOnceCallback(std::move(callback));
int result = socket_->Write(request_buffer.get(), request_buffer->size(),
std::move(split_callback.first),
TRAFFIC_ANNOTATION_FOR_TESTS);
if (result != net::ERR_IO_PENDING)
std::move(split_callback.second).Run(result);
}
bool CheckNetResultOrDie(int result) {
if (result >= 0)
return true;
callback_.Run(result, std::string());
delete this;
return false;
}
std::string serial_;
std::string filename_;
std::string content_;
size_t current_offset_;
CommandCallback callback_;
};
} // namespace
// static
void AdbClientSocket::AdbQuery(int port,
const std::string& query,
const CommandCallback& callback) {
new AdbQuerySocket(port, query, callback);
}
// static
void AdbClientSocket::SendFile(int port,
const std::string& serial,
const std::string& filename,
const std::string& content,
const CommandCallback& callback) {
new AdbSendFileSocket(port, serial, filename, content, callback);
}
// static
void AdbClientSocket::TransportQuery(int port,
const std::string& serial,
const std::string& socket_name,
const SocketCallback& callback) {
new AdbTransportSocket(port, serial, socket_name, callback);
}
// static
void AdbClientSocket::HttpQuery(int port,
const std::string& serial,
const std::string& socket_name,
const std::string& request_path,
const CommandCallback& callback) {
new HttpOverAdbSocket(port, serial, socket_name, request_path,
callback);
}
// static
void AdbClientSocket::HttpQuery(int port,
const std::string& serial,
const std::string& socket_name,
const std::string& request_path,
const SocketCallback& callback) {
new HttpOverAdbSocket(port, serial, socket_name, request_path,
callback);
}
AdbClientSocket::AdbClientSocket(int port) : port_(port) {}
AdbClientSocket::~AdbClientSocket() = default;
void AdbClientSocket::Connect(net::CompletionOnceCallback callback) {
// In a IPv4/IPv6 dual stack environment, getaddrinfo for localhost could
// only return IPv6 address while current adb (1.0.36) will always listen
// on IPv4. So just try IPv4 first, then fall back to IPv6.
net::IPAddressList list = {net::IPAddress::IPv4Localhost(),
net::IPAddress::IPv6Localhost()};
std::vector<std::string> aliases({"localhost"});
net::AddressList ip_list =
net::AddressList::CreateFromIPAddressList(list, std::move(aliases));
net::AddressList address_list = net::AddressList::CopyWithPort(
ip_list, port_);
socket_ = std::make_unique<net::TCPClientSocket>(
address_list, nullptr, nullptr, nullptr, net::NetLogSource());
auto split_callback = base::SplitOnceCallback(std::move(callback));
int result = socket_->Connect(std::move(split_callback.first));
if (result != net::ERR_IO_PENDING)
std::move(split_callback.second).Run(result);
}
void AdbClientSocket::SendCommand(const std::string& command,
bool has_output,
bool has_length,
const CommandCallback& response_callback) {
scoped_refptr<net::StringIOBuffer> request_buffer =
base::MakeRefCounted<net::StringIOBuffer>(EncodeMessage(command));
int result = socket_->Write(
request_buffer.get(), request_buffer->size(),
base::BindOnce(&AdbClientSocket::ReadResponse, base::Unretained(this),
response_callback, has_output, has_length),
TRAFFIC_ANNOTATION_FOR_TESTS);
if (result != net::ERR_IO_PENDING)
ReadResponse(response_callback, has_output, has_length, result);
}
void AdbClientSocket::ReadResponse(const CommandCallback& response_callback,
bool has_output,
bool has_length,
int result) {
if (result < 0) {
response_callback.Run(result, "IO error");
return;
}
scoped_refptr<net::GrowableIOBuffer> socket_buffer =
base::MakeRefCounted<net::GrowableIOBuffer>();
socket_buffer->SetCapacity(kBufferSize);
if (has_output) {
const ParserCallback& parse_output_callback = base::BindRepeating(
&AdbClientSocket::ParseOutput, has_length, response_callback);
int socket_result = socket_->Read(
socket_buffer.get(), kBufferSize,
base::BindOnce(&AdbClientSocket::ReadUntilEOF, base::Unretained(this),
parse_output_callback, response_callback,
socket_buffer));
if (socket_result != net::ERR_IO_PENDING) {
ReadUntilEOF(parse_output_callback, response_callback, socket_buffer,
socket_result);
}
} else {
int socket_result =
socket_->Read(socket_buffer.get(), kBufferSize,
base::BindOnce(&AdbClientSocket::ReadStatusOutput,
response_callback, socket_buffer));
if (socket_result != net::ERR_IO_PENDING) {
ReadStatusOutput(response_callback, socket_buffer, socket_result);
}
}
}
void AdbClientSocket::ReadStatusOutput(
const CommandCallback& response_callback,
scoped_refptr<net::IOBuffer> socket_buffer,
int socket_result) {
// Sometimes adb-server responds with the 8 char string "OKAY\0\0\0\0" instead
// of the expected "OKAY".
if (socket_result >= 4 &&
std::string(socket_buffer->data(), 4) == kOkayResponse) {
response_callback.Run(kAdbSuccess, kOkayResponse);
return;
}
response_callback.Run(kAdbFailure, kFailResponse);
}
void AdbClientSocket::ReadUntilEOF(
const ParserCallback& parse_output_callback,
const CommandCallback& response_callback,
scoped_refptr<net::GrowableIOBuffer> socket_buffer,
int socket_result) {
if (socket_result < 0) {
VLOG(3) << "IO error";
response_callback.Run(socket_result, "IO error");
return;
} else if (socket_result > 0) {
// We read in data. Let's try to read in more.
socket_buffer->set_offset(socket_buffer->offset() + socket_result);
if (socket_buffer->RemainingCapacity() == 0) {
socket_buffer->SetCapacity(socket_buffer->capacity() + kBufferGrowthRate);
}
int new_socket_result = socket_->Read(
socket_buffer.get(), socket_buffer->RemainingCapacity(),
base::BindOnce(&AdbClientSocket::ReadUntilEOF, base::Unretained(this),
parse_output_callback, response_callback,
socket_buffer));
if (new_socket_result != net::ERR_IO_PENDING) {
ReadUntilEOF(parse_output_callback, response_callback, socket_buffer,
new_socket_result);
}
} else if (socket_result == 0) {
// We hit EOF. The socket is closed on the other side.
parse_output_callback.Run(
std::string(base::as_string_view(socket_buffer->span_before_offset())));
}
}
void AdbClientSocket::ParseOutput(bool has_length,
const CommandCallback& response_callback,
const std::string& adb_output) {
int result = kAdbFailure;
// Expected data format is
// "OKAY<payload_length><payload>" if has_length
// or "OKAY<payload>" if not has_length.
// or "OKAY" if there is no output (regardless of has_length).
// FAIL is returned instead of OKAY if there was an error.
std::string output(adb_output);
if (output.substr(0, 4) == kOkayResponse) {
output = output.substr(4);
result = kAdbSuccess;
}
if (output.substr(0, 4) == kFailResponse) {
output = output.substr(4);
result = kAdbFailure;
}
if (output.substr(0, 4) == kOkayResponse) {
VLOG(3) << "ADB server responded with \"OKAYOKAY\" instead of \"OKAY\".";
output = output.substr(4);
// Note: It's unclear whether we should set result = kAdbSuccess here or
// not. I've never seen "FAILOKAY" in the wild.
}
// Note that has_length=true implies that the payload length will be prepended
// to the payload in the case that there is a payload. If there is no payload,
// then there may not be a payload length.
if (has_length && output.size() != 0) {
if (output.size() >= 4) {
// Just skip the hex string length. It is unnecessary since
// EOF is sent after the message is complete.
output = output.substr(4);
} else {
VLOG(3) << "Error: ADB server responded without the expected hexstring"
<< " length";
result = kAdbFailure;
}
}
response_callback.Run(result, output);
}