blob: 6fa6c2e81b0a687c4b26fab039cabb51fbf8d485 [file] [log] [blame]
Elliott Hughes58305772015-04-17 13:57:15 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Elliott Hughese67f1f82015-04-30 17:32:03 -070017#define TRACE_TAG TRACE_ADB
18
Elliott Hughes58305772015-04-17 13:57:15 -070019#include "adb_utils.h"
20
Elliott Hughesa7090b92015-04-17 17:03:59 -070021#include <stdlib.h>
Elliott Hughes58305772015-04-17 13:57:15 -070022#include <sys/stat.h>
23#include <sys/types.h>
24#include <unistd.h>
25
Elliott Hughese67f1f82015-04-30 17:32:03 -070026#include <algorithm>
27
Elliott Hughes3d5f60d2015-07-18 12:21:30 -070028#include <base/logging.h>
Elliott Hughese67f1f82015-04-30 17:32:03 -070029#include <base/stringprintf.h>
Elliott Hughes3d5f60d2015-07-18 12:21:30 -070030#include <base/strings.h>
Elliott Hughes381cfa92015-07-23 17:12:58 -070031#include <cutils/sockets.h>
Elliott Hughese67f1f82015-04-30 17:32:03 -070032
33#include "adb_trace.h"
Elliott Hughes53daee62015-04-19 13:17:01 -070034#include "sysdeps.h"
35
Elliott Hughesd48dbd82015-07-24 11:35:40 -070036#if defined(_WIN32)
37#include <ws2tcpip.h>
38#else
39#include <netdb.h>
40#endif
41
Elliott Hughesa7090b92015-04-17 17:03:59 -070042bool getcwd(std::string* s) {
43 char* cwd = getcwd(nullptr, 0);
44 if (cwd != nullptr) *s = cwd;
45 free(cwd);
46 return (cwd != nullptr);
47}
48
Elliott Hughes58305772015-04-17 13:57:15 -070049bool directory_exists(const std::string& path) {
50 struct stat sb;
51 return lstat(path.c_str(), &sb) != -1 && S_ISDIR(sb.st_mode);
52}
53
Elliott Hughes58305772015-04-17 13:57:15 -070054std::string escape_arg(const std::string& s) {
Elliott Hughes5498ade2015-04-17 20:50:11 -070055 std::string result = s;
Elliott Hughes58305772015-04-17 13:57:15 -070056
Elliott Hughes84b0bf22015-05-15 12:06:00 -070057 // Escape any ' in the string (before we single-quote the whole thing).
58 // The correct way to do this for the shell is to replace ' with '\'' --- that is,
59 // close the existing single-quoted string, escape a single single-quote, and start
60 // a new single-quoted string. Like the C preprocessor, the shell will concatenate
61 // these pieces into one string.
62 for (size_t i = 0; i < s.size(); ++i) {
63 if (s[i] == '\'') {
64 result.insert(i, "'\\'");
65 i += 2;
66 }
Elliott Hughes58305772015-04-17 13:57:15 -070067 }
Elliott Hughes5498ade2015-04-17 20:50:11 -070068
69 // Prefix and suffix the whole string with '.
70 result.insert(result.begin(), '\'');
71 result.push_back('\'');
Elliott Hughes58305772015-04-17 13:57:15 -070072 return result;
73}
Elliott Hughese67f1f82015-04-30 17:32:03 -070074
Elliott Hughes3e7048c2015-07-27 21:21:39 -070075int mkdirs(const std::string& path) {
76 // TODO: rewrite this function and merge it with the *other* mkdirs in adb.
77 std::unique_ptr<char> path_rw(strdup(path.c_str()));
Alex Vallée14216142015-05-06 17:22:25 -040078 int ret;
Elliott Hughes3e7048c2015-07-27 21:21:39 -070079 char* x = path_rw.get() + 1;
Alex Vallée14216142015-05-06 17:22:25 -040080
81 for(;;) {
Elliott Hughes3e7048c2015-07-27 21:21:39 -070082 x = const_cast<char*>(adb_dirstart(x));
Alex Vallée14216142015-05-06 17:22:25 -040083 if(x == 0) return 0;
84 *x = 0;
Elliott Hughes3e7048c2015-07-27 21:21:39 -070085 ret = adb_mkdir(path_rw.get(), 0775);
Alex Vallée14216142015-05-06 17:22:25 -040086 *x = OS_PATH_SEPARATOR;
87 if((ret < 0) && (errno != EEXIST)) {
88 return ret;
89 }
90 x++;
91 }
92 return 0;
93}
94
Elliott Hughese67f1f82015-04-30 17:32:03 -070095void dump_hex(const void* data, size_t byte_count) {
96 byte_count = std::min(byte_count, size_t(16));
97
98 const uint8_t* p = reinterpret_cast<const uint8_t*>(data);
99
100 std::string line;
101 for (size_t i = 0; i < byte_count; ++i) {
102 android::base::StringAppendF(&line, "%02x", p[i]);
103 }
104 line.push_back(' ');
105
106 for (size_t i = 0; i < byte_count; ++i) {
107 int c = p[i];
108 if (c < 32 || c > 127) {
109 c = '.';
110 }
111 line.push_back(c);
112 }
113
114 DR("%s\n", line.c_str());
115}
Elliott Hughes3d5f60d2015-07-18 12:21:30 -0700116
117bool parse_host_and_port(const std::string& address,
118 std::string* canonical_address,
119 std::string* host, int* port,
120 std::string* error) {
121 host->clear();
122
123 bool ipv6 = true;
124 bool saw_port = false;
125 size_t colons = std::count(address.begin(), address.end(), ':');
126 size_t dots = std::count(address.begin(), address.end(), '.');
127 std::string port_str;
128 if (address[0] == '[') {
129 // [::1]:123
130 if (address.rfind("]:") == std::string::npos) {
131 *error = android::base::StringPrintf("bad IPv6 address '%s'", address.c_str());
132 return false;
133 }
134 *host = address.substr(1, (address.find("]:") - 1));
135 port_str = address.substr(address.rfind("]:") + 2);
136 saw_port = true;
137 } else if (dots == 0 && colons >= 2 && colons <= 7) {
138 // ::1
139 *host = address;
140 } else if (colons <= 1) {
141 // 1.2.3.4 or some.accidental.domain.com
142 ipv6 = false;
143 std::vector<std::string> pieces = android::base::Split(address, ":");
144 *host = pieces[0];
145 if (pieces.size() > 1) {
146 port_str = pieces[1];
147 saw_port = true;
148 }
149 }
150
151 if (host->empty()) {
152 *error = android::base::StringPrintf("no host in '%s'", address.c_str());
153 return false;
154 }
155
156 if (saw_port) {
157 if (sscanf(port_str.c_str(), "%d", port) != 1 || *port <= 0 || *port > 65535) {
158 *error = android::base::StringPrintf("bad port number '%s' in '%s'",
159 port_str.c_str(), address.c_str());
160 return false;
161 }
162 }
163
164 *canonical_address = android::base::StringPrintf(ipv6 ? "[%s]:%d" : "%s:%d", host->c_str(), *port);
165 LOG(DEBUG) << "parsed " << address << " as " << *host << " and " << *port
166 << " (" << *canonical_address << ")";
167 return true;
168}
Elliott Hughes381cfa92015-07-23 17:12:58 -0700169
170int network_connect(const std::string& host, int port, int type, int timeout, std::string* error) {
171 int getaddrinfo_error = 0;
172 int fd = socket_network_client_timeout(host.c_str(), port, type, timeout, &getaddrinfo_error);
173 if (fd != -1) {
174 return fd;
175 }
176 if (getaddrinfo_error != 0) {
Elliott Hughesd48dbd82015-07-24 11:35:40 -0700177 // TODO: gai_strerror is not thread safe on Win32.
Elliott Hughes381cfa92015-07-23 17:12:58 -0700178 *error = gai_strerror(getaddrinfo_error);
179 } else {
180 *error = strerror(errno);
181 }
182 return -1;
183}