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