Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 1 | /* |
| 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 Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 17 | #define TRACE_TAG TRACE_ADB |
| 18 | |
Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 19 | #include "adb_utils.h" |
| 20 | |
Elliott Hughes | 381cfa9 | 2015-07-23 17:12:58 -0700 | [diff] [blame^] | 21 | #include <netdb.h> |
Elliott Hughes | a7090b9 | 2015-04-17 17:03:59 -0700 | [diff] [blame] | 22 | #include <stdlib.h> |
Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <unistd.h> |
| 26 | |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 27 | #include <algorithm> |
| 28 | |
Elliott Hughes | 3d5f60d | 2015-07-18 12:21:30 -0700 | [diff] [blame] | 29 | #include <base/logging.h> |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 30 | #include <base/stringprintf.h> |
Elliott Hughes | 3d5f60d | 2015-07-18 12:21:30 -0700 | [diff] [blame] | 31 | #include <base/strings.h> |
Elliott Hughes | 381cfa9 | 2015-07-23 17:12:58 -0700 | [diff] [blame^] | 32 | #include <cutils/sockets.h> |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 33 | |
| 34 | #include "adb_trace.h" |
Elliott Hughes | 53daee6 | 2015-04-19 13:17:01 -0700 | [diff] [blame] | 35 | #include "sysdeps.h" |
| 36 | |
Elliott Hughes | a7090b9 | 2015-04-17 17:03:59 -0700 | [diff] [blame] | 37 | bool getcwd(std::string* s) { |
| 38 | char* cwd = getcwd(nullptr, 0); |
| 39 | if (cwd != nullptr) *s = cwd; |
| 40 | free(cwd); |
| 41 | return (cwd != nullptr); |
| 42 | } |
| 43 | |
Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 44 | bool directory_exists(const std::string& path) { |
| 45 | struct stat sb; |
| 46 | return lstat(path.c_str(), &sb) != -1 && S_ISDIR(sb.st_mode); |
| 47 | } |
| 48 | |
Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 49 | std::string escape_arg(const std::string& s) { |
Elliott Hughes | 5498ade | 2015-04-17 20:50:11 -0700 | [diff] [blame] | 50 | std::string result = s; |
Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 51 | |
Elliott Hughes | 84b0bf2 | 2015-05-15 12:06:00 -0700 | [diff] [blame] | 52 | // Escape any ' in the string (before we single-quote the whole thing). |
| 53 | // The correct way to do this for the shell is to replace ' with '\'' --- that is, |
| 54 | // close the existing single-quoted string, escape a single single-quote, and start |
| 55 | // a new single-quoted string. Like the C preprocessor, the shell will concatenate |
| 56 | // these pieces into one string. |
| 57 | for (size_t i = 0; i < s.size(); ++i) { |
| 58 | if (s[i] == '\'') { |
| 59 | result.insert(i, "'\\'"); |
| 60 | i += 2; |
| 61 | } |
Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 62 | } |
Elliott Hughes | 5498ade | 2015-04-17 20:50:11 -0700 | [diff] [blame] | 63 | |
| 64 | // Prefix and suffix the whole string with '. |
| 65 | result.insert(result.begin(), '\''); |
| 66 | result.push_back('\''); |
Elliott Hughes | 5830577 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 67 | return result; |
| 68 | } |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 69 | |
Alex Vallée | 1421614 | 2015-05-06 17:22:25 -0400 | [diff] [blame] | 70 | int mkdirs(const char *path) |
| 71 | { |
| 72 | int ret; |
| 73 | char *x = (char *)path + 1; |
| 74 | |
| 75 | for(;;) { |
| 76 | x = adb_dirstart(x); |
| 77 | if(x == 0) return 0; |
| 78 | *x = 0; |
| 79 | ret = adb_mkdir(path, 0775); |
| 80 | *x = OS_PATH_SEPARATOR; |
| 81 | if((ret < 0) && (errno != EEXIST)) { |
| 82 | return ret; |
| 83 | } |
| 84 | x++; |
| 85 | } |
| 86 | return 0; |
| 87 | } |
| 88 | |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 89 | void dump_hex(const void* data, size_t byte_count) { |
| 90 | byte_count = std::min(byte_count, size_t(16)); |
| 91 | |
| 92 | const uint8_t* p = reinterpret_cast<const uint8_t*>(data); |
| 93 | |
| 94 | std::string line; |
| 95 | for (size_t i = 0; i < byte_count; ++i) { |
| 96 | android::base::StringAppendF(&line, "%02x", p[i]); |
| 97 | } |
| 98 | line.push_back(' '); |
| 99 | |
| 100 | for (size_t i = 0; i < byte_count; ++i) { |
| 101 | int c = p[i]; |
| 102 | if (c < 32 || c > 127) { |
| 103 | c = '.'; |
| 104 | } |
| 105 | line.push_back(c); |
| 106 | } |
| 107 | |
| 108 | DR("%s\n", line.c_str()); |
| 109 | } |
Elliott Hughes | 3d5f60d | 2015-07-18 12:21:30 -0700 | [diff] [blame] | 110 | |
| 111 | bool parse_host_and_port(const std::string& address, |
| 112 | std::string* canonical_address, |
| 113 | std::string* host, int* port, |
| 114 | std::string* error) { |
| 115 | host->clear(); |
| 116 | |
| 117 | bool ipv6 = true; |
| 118 | bool saw_port = false; |
| 119 | size_t colons = std::count(address.begin(), address.end(), ':'); |
| 120 | size_t dots = std::count(address.begin(), address.end(), '.'); |
| 121 | std::string port_str; |
| 122 | if (address[0] == '[') { |
| 123 | // [::1]:123 |
| 124 | if (address.rfind("]:") == std::string::npos) { |
| 125 | *error = android::base::StringPrintf("bad IPv6 address '%s'", address.c_str()); |
| 126 | return false; |
| 127 | } |
| 128 | *host = address.substr(1, (address.find("]:") - 1)); |
| 129 | port_str = address.substr(address.rfind("]:") + 2); |
| 130 | saw_port = true; |
| 131 | } else if (dots == 0 && colons >= 2 && colons <= 7) { |
| 132 | // ::1 |
| 133 | *host = address; |
| 134 | } else if (colons <= 1) { |
| 135 | // 1.2.3.4 or some.accidental.domain.com |
| 136 | ipv6 = false; |
| 137 | std::vector<std::string> pieces = android::base::Split(address, ":"); |
| 138 | *host = pieces[0]; |
| 139 | if (pieces.size() > 1) { |
| 140 | port_str = pieces[1]; |
| 141 | saw_port = true; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | if (host->empty()) { |
| 146 | *error = android::base::StringPrintf("no host in '%s'", address.c_str()); |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | if (saw_port) { |
| 151 | if (sscanf(port_str.c_str(), "%d", port) != 1 || *port <= 0 || *port > 65535) { |
| 152 | *error = android::base::StringPrintf("bad port number '%s' in '%s'", |
| 153 | port_str.c_str(), address.c_str()); |
| 154 | return false; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | *canonical_address = android::base::StringPrintf(ipv6 ? "[%s]:%d" : "%s:%d", host->c_str(), *port); |
| 159 | LOG(DEBUG) << "parsed " << address << " as " << *host << " and " << *port |
| 160 | << " (" << *canonical_address << ")"; |
| 161 | return true; |
| 162 | } |
Elliott Hughes | 381cfa9 | 2015-07-23 17:12:58 -0700 | [diff] [blame^] | 163 | |
| 164 | int network_connect(const std::string& host, int port, int type, int timeout, std::string* error) { |
| 165 | int getaddrinfo_error = 0; |
| 166 | int fd = socket_network_client_timeout(host.c_str(), port, type, timeout, &getaddrinfo_error); |
| 167 | if (fd != -1) { |
| 168 | return fd; |
| 169 | } |
| 170 | if (getaddrinfo_error != 0) { |
| 171 | // TODO: not thread safe on Win32. |
| 172 | *error = gai_strerror(getaddrinfo_error); |
| 173 | } else { |
| 174 | *error = strerror(errno); |
| 175 | } |
| 176 | return -1; |
| 177 | } |