1 // Copyright (c) 2012 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 "tools/android/common/adb_connection.h"
6 
7 #include <arpa/inet.h>
8 #include <errno.h>
9 #include <stddef.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <sys/socket.h>
14 #include <sys/types.h>
15 #include <unistd.h>
16 
17 #include "base/logging.h"
18 #include "base/posix/eintr_wrapper.h"
19 #include "base/stl_util.h"
20 #include "tools/android/common/net.h"
21 
22 namespace tools {
23 namespace {
24 
CloseSocket(int fd)25 void CloseSocket(int fd) {
26   if (fd >= 0) {
27     int old_errno = errno;
28     close(fd);
29     errno = old_errno;
30   }
31 }
32 
33 }  // namespace
34 
ConnectAdbHostSocket(const char * forward_to)35 int ConnectAdbHostSocket(const char* forward_to) {
36   // ADB port forward request format: HHHHtcp:port:address.
37   // HHHH is the hexidecimal length of the "tcp:port:address" part.
38   const size_t kBufferMaxLength = 30;
39   const size_t kLengthOfLength = 4;
40 
41   const char kAddressPrefix[] = { 't', 'c', 'p', ':' };
42   size_t address_length = base::size(kAddressPrefix) + strlen(forward_to);
43   if (address_length > kBufferMaxLength - kLengthOfLength) {
44     LOG(ERROR) << "Forward to address is too long: " << forward_to;
45     return -1;
46   }
47 
48   char request[kBufferMaxLength];
49   memcpy(request + kLengthOfLength, kAddressPrefix, base::size(kAddressPrefix));
50   memcpy(request + kLengthOfLength + base::size(kAddressPrefix), forward_to,
51          strlen(forward_to));
52 
53   char length_buffer[kLengthOfLength + 1];
54   snprintf(length_buffer, base::size(length_buffer), "%04X",
55            static_cast<int>(address_length));
56   memcpy(request, length_buffer, kLengthOfLength);
57 
58   int host_socket = socket(AF_INET, SOCK_STREAM, 0);
59   if (host_socket < 0) {
60     LOG(ERROR) << "Failed to create adb socket: " << strerror(errno);
61     return -1;
62   }
63 
64   DisableNagle(host_socket);
65 
66   const int kAdbPort = 5037;
67   sockaddr_in addr;
68   memset(&addr, 0, sizeof(addr));
69   addr.sin_family = AF_INET;
70   addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
71   addr.sin_port = htons(kAdbPort);
72   if (HANDLE_EINTR(connect(host_socket, reinterpret_cast<sockaddr*>(&addr),
73                            sizeof(addr))) < 0) {
74     LOG(ERROR) << "Failed to connect adb socket: " << strerror(errno);
75     CloseSocket(host_socket);
76     return -1;
77   }
78 
79   size_t bytes_remaining = address_length + kLengthOfLength;
80   size_t bytes_sent = 0;
81   while (bytes_remaining > 0) {
82     int ret = HANDLE_EINTR(send(host_socket, request + bytes_sent,
83                                 bytes_remaining, 0));
84     if (ret < 0) {
85       LOG(ERROR) << "Failed to send request: " << strerror(errno);
86       CloseSocket(host_socket);
87       return -1;
88     }
89 
90     bytes_sent += ret;
91     bytes_remaining -= ret;
92   }
93 
94   const int kAdbStatusLength = 4;
95   char response[kBufferMaxLength];
96   int response_length = HANDLE_EINTR(recv(host_socket, response,
97                                           kBufferMaxLength, 0));
98   if (response_length < kAdbStatusLength ||
99       strncmp("OKAY", response, kAdbStatusLength) != 0) {
100     LOG(ERROR) << "Bad response from ADB: length: " << response_length
101                << " data: " << DumpBinary(response, response_length);
102     CloseSocket(host_socket);
103     return -1;
104   }
105 
106   return host_socket;
107 }
108 
109 }  // namespace tools
110