Erik Kline | 871e63d | 2016-01-22 09:07:44 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
| 17 | |
| 18 | #include <errno.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <netdb.h> |
| 21 | #include <netinet/in.h> |
| 22 | #include <sys/socket.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <sys/uio.h> |
| 25 | #include <iostream> |
| 26 | #include <string> |
| 27 | |
| 28 | #include <android/multinetwork.h> |
| 29 | #include <android-base/stringprintf.h> |
| 30 | #include "common.h" |
| 31 | |
| 32 | |
| 33 | |
| 34 | struct Parameters { |
| 35 | Parameters() : ss({}), port("80"), path("/") {} |
| 36 | |
| 37 | struct sockaddr_storage ss; |
| 38 | std::string host; |
| 39 | std::string hostname; |
| 40 | std::string port; |
| 41 | std::string path; |
| 42 | }; |
| 43 | |
| 44 | |
| 45 | bool parseUrl(const struct Arguments& args, struct Parameters* parameters) { |
| 46 | if (parameters == nullptr) { return false; } |
| 47 | |
| 48 | static const char HTTP_PREFIX[] = "http://"; |
| 49 | if (strncmp(args.arg1, HTTP_PREFIX, strlen(HTTP_PREFIX)) != 0) { |
| 50 | std::cerr << "Only " << HTTP_PREFIX << " URLs supported." << std::endl; |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | parameters->host = std::string(args.arg1).substr(strlen(HTTP_PREFIX)); |
| 55 | const auto first_slash = parameters->host.find_first_of("/"); |
| 56 | if (first_slash != std::string::npos) { |
| 57 | parameters->path = parameters->host.substr(first_slash); |
| 58 | parameters->host.erase(first_slash); |
| 59 | } |
| 60 | |
| 61 | if (parameters->host.size() == 0) { |
| 62 | std::cerr << "Host portion cannot be empty." << std::endl; |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | if (parameters->host[0] == '[') { |
| 67 | const auto closing_bracket = parameters->host.find_first_of("]"); |
| 68 | if (closing_bracket == std::string::npos) { |
| 69 | std::cerr << "Missing closing bracket." << std::endl; |
| 70 | return false; |
| 71 | } |
| 72 | parameters->hostname = parameters->host.substr(1, closing_bracket - 1); |
| 73 | |
| 74 | const auto colon_port = closing_bracket + 1; |
| 75 | if (colon_port < parameters->host.size()) { |
| 76 | if (parameters->host[colon_port] != ':') { |
| 77 | std::cerr << "Malformed port portion." << std::endl; |
| 78 | return false; |
| 79 | } |
| 80 | parameters->port = parameters->host.substr(closing_bracket + 2); |
| 81 | } |
| 82 | } else { |
| 83 | const auto first_colon = parameters->host.find_first_of(":"); |
| 84 | if (first_colon != std::string::npos) { |
| 85 | parameters->port = parameters->host.substr(first_colon + 1); |
| 86 | parameters->hostname = parameters->host.substr(0, first_colon); |
| 87 | } else { |
| 88 | parameters->hostname = parameters->host; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // TODO: find the request portion to send (before '#...'). |
| 93 | |
| 94 | std::cerr << "Resolving hostname=" << parameters->hostname |
| 95 | << ", port=" << parameters->port |
| 96 | << std::endl; |
| 97 | |
| 98 | struct addrinfo hints = { |
| 99 | .ai_family = args.family, |
| 100 | .ai_socktype = SOCK_STREAM, |
| 101 | }; |
| 102 | struct addrinfo *result = nullptr; |
| 103 | |
| 104 | int rval = -1; |
| 105 | switch (args.api_mode) { |
| 106 | case ApiMode::EXPLICIT: |
| 107 | rval = android_getaddrinfofornetwork(args.nethandle, |
| 108 | parameters->hostname.c_str(), |
| 109 | parameters->port.c_str(), |
| 110 | &hints, &result); |
| 111 | break; |
| 112 | case ApiMode::PROCESS: |
| 113 | rval = getaddrinfo(parameters->hostname.c_str(), |
| 114 | parameters->port.c_str(), |
| 115 | &hints, &result); |
| 116 | break; |
| 117 | default: |
| 118 | // Unreachable. |
| 119 | std::cerr << "Unknown api mode." << std::endl; |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | if (rval != 0) { |
| 124 | std::cerr << "DNS resolution failure; gaierror=" << rval |
| 125 | << " [" << gai_strerror(rval) << "]" |
| 126 | << std::endl; |
| 127 | return rval; |
| 128 | } |
| 129 | |
| 130 | memcpy(&(parameters->ss), result[0].ai_addr, result[0].ai_addrlen); |
| 131 | std::cerr << "Connecting to: " |
| 132 | << inetSockaddrToString(result[0].ai_addr) |
| 133 | << std::endl; |
| 134 | |
| 135 | freeaddrinfo(result); |
| 136 | return true; |
| 137 | } |
| 138 | |
| 139 | |
| 140 | int makeTcpSocket(sa_family_t address_family, net_handle_t nethandle) { |
| 141 | int fd = socket(address_family, SOCK_STREAM, IPPROTO_TCP); |
| 142 | if (fd < 0) { |
| 143 | std::cerr << "failed to create TCP socket" << std::endl; |
| 144 | return -1; |
| 145 | } |
| 146 | |
| 147 | // Don't let reads or writes block indefinitely. We cannot control |
| 148 | // connect() timeouts without nonblocking sockets and select/poll/epoll. |
| 149 | const struct timeval timeo = { 5, 0 }; // 5 seconds |
| 150 | setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo)); |
| 151 | setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeo, sizeof(timeo)); |
| 152 | |
| 153 | if (nethandle != NETWORK_UNSPECIFIED) { |
| 154 | if (android_setsocknetwork(nethandle, fd) != 0) { |
| 155 | int errnum = errno; |
| 156 | std::cerr << "android_setsocknetwork() failed;" |
| 157 | << " errno: " << errnum << " [" << strerror(errnum) << "]" |
| 158 | << std::endl; |
| 159 | close(fd); |
| 160 | return -1; |
| 161 | } |
| 162 | } |
| 163 | return fd; |
| 164 | } |
| 165 | |
| 166 | |
| 167 | int doHttpQuery(int fd, const struct Parameters& parameters) { |
| 168 | int rval = -1; |
| 169 | if (connect(fd, |
| 170 | reinterpret_cast<const struct sockaddr *>(&(parameters.ss)), |
| 171 | (parameters.ss.ss_family == AF_INET6) |
| 172 | ? sizeof(struct sockaddr_in6) |
| 173 | : sizeof(struct sockaddr_in)) != 0) { |
| 174 | int errnum = errno; |
| 175 | std::cerr << "Failed to connect; errno=" << errnum |
| 176 | << " [" << strerror(errnum) << "]" |
| 177 | << std::endl; |
| 178 | return -1; |
| 179 | } |
| 180 | |
| 181 | const std::string request(android::base::StringPrintf( |
| 182 | "GET %s HTTP/1.1\r\n" |
| 183 | "Host: %s\r\n" |
| 184 | "Accept: */*\r\n" |
| 185 | "Connection: close\r\n" |
| 186 | "User-Agent: httpurl/0.0\r\n" |
| 187 | "\r\n", |
| 188 | parameters.path.c_str(), parameters.host.c_str())); |
| 189 | const ssize_t sent = write(fd, request.c_str(), request.size()); |
| 190 | if (sent != static_cast<ssize_t>(request.size())) { |
| 191 | std::cerr << "Sent only " << sent << "/" << request.size() << " bytes" |
| 192 | << std::endl; |
| 193 | return -1; |
| 194 | } |
| 195 | |
| 196 | char buf[4*1024]; |
| 197 | do { |
| 198 | rval = recv(fd, buf, sizeof(buf), 0); |
| 199 | |
| 200 | if (rval < 0) { |
| 201 | const int saved_errno = errno; |
| 202 | std::cerr << "Failed to recv; errno=" << saved_errno |
| 203 | << " [" << strerror(saved_errno) << "]" |
| 204 | << std::endl; |
| 205 | } else if (rval > 0) { |
| 206 | std::cout.write(buf, rval); |
| 207 | std::cout.flush(); |
| 208 | } |
| 209 | } while (rval > 0); |
| 210 | std::cout << std::endl; |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | |
| 216 | int main(int argc, const char* argv[]) { |
| 217 | int rval = -1; |
| 218 | |
| 219 | struct Arguments args; |
| 220 | if (!args.parseArguments(argc, argv)) { return rval; } |
| 221 | |
| 222 | if (args.api_mode == ApiMode::PROCESS) { |
| 223 | rval = android_setprocnetwork(args.nethandle); |
| 224 | if (rval != 0) { |
| 225 | int errnum = errno; |
| 226 | std::cerr << "android_setprocnetwork(" << args.nethandle << ") failed;" |
| 227 | << " errno: " << errnum << " [" << strerror(errnum) << "]" |
| 228 | << std::endl; |
| 229 | return rval; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | struct Parameters parameters; |
| 234 | if (!parseUrl(args, ¶meters)) { return -1; } |
| 235 | |
| 236 | // TODO: Fall back from IPv6 to IPv4 if ss.ss_family is AF_UNSPEC. |
| 237 | // This will involve changes to parseUrl() as well. |
| 238 | struct FdAutoCloser closer = makeTcpSocket( |
| 239 | parameters.ss.ss_family, |
| 240 | (args.api_mode == ApiMode::EXPLICIT) ? args.nethandle |
| 241 | : NETWORK_UNSPECIFIED); |
| 242 | if (closer.fd < 0) { return closer.fd; } |
| 243 | |
| 244 | return doHttpQuery(closer.fd, parameters); |
| 245 | } |