Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | */ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 16 | |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 17 | #include <arpa/inet.h> |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 18 | #include <errno.h> |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 19 | #include <netdb.h> |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 20 | #include <netinet/in.h> |
| 21 | #include <netinet/tcp.h> |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/socket.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <unistd.h> |
| 28 | |
| 29 | #include "base/logging.h" |
Elliott Hughes | e222ee0 | 2012-12-13 14:41:43 -0800 | [diff] [blame] | 30 | #include "base/stringprintf.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 31 | #include "jdwp/jdwp_priv.h" |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 32 | |
| 33 | #define kBasePort 8000 |
| 34 | #define kMaxPort 8040 |
| 35 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 36 | namespace art { |
| 37 | |
| 38 | namespace JDWP { |
| 39 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 40 | /* |
| 41 | * JDWP network state. |
| 42 | * |
| 43 | * We only talk to one debugger at a time. |
| 44 | */ |
| 45 | struct JdwpNetState : public JdwpNetStateBase { |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 46 | uint16_t listenPort; |
| 47 | int listenSock; /* listen for connection from debugger */ |
| 48 | int wakePipe[2]; /* break out of select */ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 49 | |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 50 | in_addr remoteAddr; |
| 51 | uint16_t remotePort; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 52 | |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 53 | JdwpNetState() { |
| 54 | listenPort = 0; |
| 55 | listenSock = -1; |
| 56 | wakePipe[0] = -1; |
| 57 | wakePipe[1] = -1; |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 58 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame^] | 61 | static void socketStateShutdown(JdwpNetState* state); |
| 62 | static void socketStateFree(JdwpNetState* state); |
| 63 | |
| 64 | static JdwpNetState* GetNetState(JdwpState* state) { |
| 65 | return reinterpret_cast<JdwpNetState*>(state->netState); |
| 66 | } |
| 67 | |
Elliott Hughes | 6d8dd47 | 2012-01-17 18:27:41 -0800 | [diff] [blame] | 68 | static JdwpNetState* netStartup(uint16_t port, bool probe); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 69 | |
| 70 | /* |
| 71 | * Set up some stuff for transport=dt_socket. |
| 72 | */ |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 73 | static bool prepareSocket(JdwpState* state, const JdwpOptions* options) { |
Elliott Hughes | 6d8dd47 | 2012-01-17 18:27:41 -0800 | [diff] [blame] | 74 | uint16_t port = options->port; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 75 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 76 | if (options->server) { |
| 77 | if (options->port != 0) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 78 | /* try only the specified port */ |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 79 | state->netState = netStartup(port, false); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 80 | } else { |
| 81 | /* scan through a range of ports, binding to the first available */ |
| 82 | for (port = kBasePort; port <= kMaxPort; port++) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 83 | state->netState = netStartup(port, true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 84 | if (state->netState != NULL) { |
| 85 | break; |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | if (state->netState == NULL) { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 90 | LOG(ERROR) << "JDWP net startup failed (req port=" << options->port << ")"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 91 | return false; |
| 92 | } |
| 93 | } else { |
Elliott Hughes | 6d8dd47 | 2012-01-17 18:27:41 -0800 | [diff] [blame] | 94 | state->netState = netStartup(0, false); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 97 | if (options->suspend) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 98 | LOG(INFO) << "JDWP will wait for debugger on port " << port; |
| 99 | } else { |
Elliott Hughes | 376a7a0 | 2011-10-24 18:35:55 -0700 | [diff] [blame] | 100 | LOG(INFO) << "JDWP will " << (options->server ? "listen" : "connect") << " on port " << port; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | /* |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 107 | * Initialize JDWP stuff. |
| 108 | * |
Elliott Hughes | 6d8dd47 | 2012-01-17 18:27:41 -0800 | [diff] [blame] | 109 | * Allocates a new state structure. If "port" is non-zero, this also |
| 110 | * tries to bind to a listen port. If "port" is zero, we assume |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 111 | * we're preparing for an outbound connection, and return without binding |
| 112 | * to anything. |
| 113 | * |
| 114 | * This may be called several times if we're probing for a port. |
| 115 | * |
| 116 | * Returns 0 on success. |
| 117 | */ |
Elliott Hughes | 6d8dd47 | 2012-01-17 18:27:41 -0800 | [diff] [blame] | 118 | static JdwpNetState* netStartup(uint16_t port, bool probe) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 119 | JdwpNetState* netState = new JdwpNetState; |
Elliott Hughes | 6d8dd47 | 2012-01-17 18:27:41 -0800 | [diff] [blame] | 120 | if (port == 0) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 121 | return netState; |
| 122 | } |
| 123 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 124 | netState->listenSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 125 | if (netState->listenSock < 0) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 126 | PLOG(probe ? ERROR : FATAL) << "Socket create failed"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 127 | goto fail; |
| 128 | } |
| 129 | |
| 130 | /* allow immediate re-use */ |
Elliott Hughes | 6d8dd47 | 2012-01-17 18:27:41 -0800 | [diff] [blame] | 131 | { |
| 132 | int one = 1; |
| 133 | if (setsockopt(netState->listenSock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0) { |
| 134 | PLOG(probe ? ERROR : FATAL) << "setsockopt(SO_REUSEADDR) failed"; |
| 135 | goto fail; |
| 136 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | union { |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 140 | sockaddr_in addrInet; |
| 141 | sockaddr addrPlain; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 142 | } addr; |
| 143 | addr.addrInet.sin_family = AF_INET; |
| 144 | addr.addrInet.sin_port = htons(port); |
| 145 | inet_aton("127.0.0.1", &addr.addrInet.sin_addr); |
| 146 | |
| 147 | if (bind(netState->listenSock, &addr.addrPlain, sizeof(addr)) != 0) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 148 | PLOG(probe ? ERROR : FATAL) << "Attempt to bind to port " << port << " failed"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 149 | goto fail; |
| 150 | } |
| 151 | |
| 152 | netState->listenPort = port; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 153 | |
| 154 | if (listen(netState->listenSock, 5) != 0) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 155 | PLOG(probe ? ERROR : FATAL) << "Listen failed"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 156 | goto fail; |
| 157 | } |
| 158 | |
| 159 | return netState; |
| 160 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 161 | fail: |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame^] | 162 | socketStateShutdown(netState); |
| 163 | socketStateFree(netState); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 164 | return NULL; |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * Shut down JDWP listener. Don't free state. |
| 169 | * |
| 170 | * Note that "netState" may be partially initialized if "startup" failed. |
| 171 | * |
| 172 | * This may be called from a non-JDWP thread as part of shutting the |
| 173 | * JDWP thread down. |
| 174 | * |
| 175 | * (This is currently called several times during startup as we probe |
| 176 | * for an open port.) |
| 177 | */ |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame^] | 178 | static void socketStateShutdown(JdwpNetState* netState) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 179 | if (netState == NULL) { |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | int listenSock = netState->listenSock; |
| 184 | int clientSock = netState->clientSock; |
| 185 | |
| 186 | /* clear these out so it doesn't wake up and try to reuse them */ |
| 187 | netState->listenSock = netState->clientSock = -1; |
| 188 | |
| 189 | /* "shutdown" dislodges blocking read() and accept() calls */ |
| 190 | if (listenSock >= 0) { |
| 191 | shutdown(listenSock, SHUT_RDWR); |
| 192 | close(listenSock); |
| 193 | } |
| 194 | if (clientSock >= 0) { |
| 195 | shutdown(clientSock, SHUT_RDWR); |
| 196 | close(clientSock); |
| 197 | } |
| 198 | |
| 199 | /* if we might be sitting in select, kick us loose */ |
| 200 | if (netState->wakePipe[1] >= 0) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 201 | VLOG(jdwp) << "+++ writing to wakePipe"; |
Elliott Hughes | 068193c | 2013-04-15 16:05:28 -0700 | [diff] [blame] | 202 | TEMP_FAILURE_RETRY(write(netState->wakePipe[1], "", 1)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame^] | 206 | static void netShutdown(JdwpState* state) { |
| 207 | socketStateShutdown(GetNetState(state)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /* |
| 211 | * Free JDWP state. |
| 212 | * |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame^] | 213 | * Call this after shutting the network down with socketStateShutdown(). |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 214 | */ |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame^] | 215 | static void socketStateFree(JdwpNetState* netState) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 216 | if (netState == NULL) { |
| 217 | return; |
| 218 | } |
| 219 | CHECK_EQ(netState->listenSock, -1); |
| 220 | CHECK_EQ(netState->clientSock, -1); |
| 221 | |
| 222 | if (netState->wakePipe[0] >= 0) { |
| 223 | close(netState->wakePipe[0]); |
| 224 | netState->wakePipe[0] = -1; |
| 225 | } |
| 226 | if (netState->wakePipe[1] >= 0) { |
| 227 | close(netState->wakePipe[1]); |
| 228 | netState->wakePipe[1] = -1; |
| 229 | } |
| 230 | |
| 231 | delete netState; |
| 232 | } |
| 233 | |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame^] | 234 | static void netFree(JdwpState* state) { |
| 235 | socketStateFree(GetNetState(state)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | /* |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 239 | * Disable the TCP Nagle algorithm, which delays transmission of outbound |
| 240 | * packets until the previous transmissions have been acked. JDWP does a |
| 241 | * lot of back-and-forth with small packets, so this may help. |
| 242 | */ |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 243 | static int setNoDelay(int fd) { |
| 244 | int on = 1; |
| 245 | int cc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)); |
| 246 | CHECK_EQ(cc, 0); |
| 247 | return cc; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | /* |
| 251 | * Accept a connection. This will block waiting for somebody to show up. |
| 252 | * If that's not desirable, use checkConnection() to make sure something |
| 253 | * is pending. |
| 254 | */ |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 255 | static bool acceptConnection(JdwpState* state) { |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame^] | 256 | JdwpNetState* netState = GetNetState(state); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 257 | union { |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 258 | sockaddr_in addrInet; |
| 259 | sockaddr addrPlain; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 260 | } addr; |
| 261 | socklen_t addrlen; |
| 262 | int sock; |
| 263 | |
| 264 | if (netState->listenSock < 0) { |
| 265 | return false; /* you're not listening! */ |
| 266 | } |
| 267 | |
| 268 | CHECK_LT(netState->clientSock, 0); /* must not already be talking */ |
| 269 | |
| 270 | addrlen = sizeof(addr); |
| 271 | do { |
| 272 | sock = accept(netState->listenSock, &addr.addrPlain, &addrlen); |
| 273 | if (sock < 0 && errno != EINTR) { |
| 274 | // When we call shutdown() on the socket, accept() returns with |
| 275 | // EINVAL. Don't gripe about it. |
| 276 | if (errno == EINVAL) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 277 | if (VLOG_IS_ON(jdwp)) { |
| 278 | PLOG(ERROR) << "accept failed"; |
| 279 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 280 | } else { |
| 281 | PLOG(ERROR) << "accept failed"; |
| 282 | return false; |
| 283 | } |
| 284 | } |
| 285 | } while (sock < 0); |
| 286 | |
| 287 | netState->remoteAddr = addr.addrInet.sin_addr; |
| 288 | netState->remotePort = ntohs(addr.addrInet.sin_port); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 289 | VLOG(jdwp) << "+++ accepted connection from " << inet_ntoa(netState->remoteAddr) << ":" << netState->remotePort; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 290 | |
| 291 | netState->clientSock = sock; |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame^] | 292 | netState->SetAwaitingHandshake(true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 293 | netState->inputCount = 0; |
| 294 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 295 | VLOG(jdwp) << "Setting TCP_NODELAY on accepted socket"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 296 | setNoDelay(netState->clientSock); |
| 297 | |
| 298 | if (pipe(netState->wakePipe) < 0) { |
| 299 | PLOG(ERROR) << "pipe failed"; |
| 300 | return false; |
| 301 | } |
| 302 | |
| 303 | return true; |
| 304 | } |
| 305 | |
| 306 | /* |
| 307 | * Create a connection to a waiting debugger. |
| 308 | */ |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 309 | static bool establishConnection(JdwpState* state, const JdwpOptions* options) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 310 | union { |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 311 | sockaddr_in addrInet; |
| 312 | sockaddr addrPlain; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 313 | } addr; |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 314 | hostent* pEntry; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 315 | |
| 316 | CHECK(state != NULL && state->netState != NULL); |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 317 | CHECK(!options->server); |
| 318 | CHECK(!options->host.empty()); |
| 319 | CHECK_NE(options->port, 0); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 320 | |
| 321 | /* |
| 322 | * Start by resolving the host name. |
| 323 | */ |
| 324 | //#undef HAVE_GETHOSTBYNAME_R |
| 325 | //#warning "forcing non-R" |
| 326 | #ifdef HAVE_GETHOSTBYNAME_R |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 327 | hostent he; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 328 | char auxBuf[128]; |
| 329 | int error; |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 330 | int cc = gethostbyname_r(options->host.c_str(), &he, auxBuf, sizeof(auxBuf), &pEntry, &error); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 331 | if (cc != 0) { |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 332 | LOG(WARNING) << "gethostbyname_r('" << options->host << "') failed: " << hstrerror(error); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 333 | return false; |
| 334 | } |
| 335 | #else |
| 336 | h_errno = 0; |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 337 | pEntry = gethostbyname(options->host.c_str()); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 338 | if (pEntry == NULL) { |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 339 | PLOG(WARNING) << "gethostbyname('" << options->host << "') failed"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 340 | return false; |
| 341 | } |
| 342 | #endif |
| 343 | |
| 344 | /* copy it out ASAP to minimize risk of multithreaded annoyances */ |
| 345 | memcpy(&addr.addrInet.sin_addr, pEntry->h_addr, pEntry->h_length); |
| 346 | addr.addrInet.sin_family = pEntry->h_addrtype; |
| 347 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 348 | addr.addrInet.sin_port = htons(options->port); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 349 | |
| 350 | LOG(INFO) << "Connecting out to " << inet_ntoa(addr.addrInet.sin_addr) << ":" << ntohs(addr.addrInet.sin_port); |
| 351 | |
| 352 | /* |
| 353 | * Create a socket. |
| 354 | */ |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame^] | 355 | JdwpNetState* netState = GetNetState(state); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 356 | netState->clientSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 357 | if (netState->clientSock < 0) { |
| 358 | PLOG(ERROR) << "Unable to create socket"; |
| 359 | return false; |
| 360 | } |
| 361 | |
| 362 | /* |
| 363 | * Try to connect. |
| 364 | */ |
| 365 | if (connect(netState->clientSock, &addr.addrPlain, sizeof(addr)) != 0) { |
| 366 | PLOG(ERROR) << "Unable to connect to " << inet_ntoa(addr.addrInet.sin_addr) << ":" << ntohs(addr.addrInet.sin_port); |
| 367 | close(netState->clientSock); |
| 368 | netState->clientSock = -1; |
| 369 | return false; |
| 370 | } |
| 371 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 372 | LOG(INFO) << "Connection established to " << options->host << " (" << inet_ntoa(addr.addrInet.sin_addr) << ":" << ntohs(addr.addrInet.sin_port) << ")"; |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame^] | 373 | netState->SetAwaitingHandshake(true); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 374 | netState->inputCount = 0; |
| 375 | |
| 376 | setNoDelay(netState->clientSock); |
| 377 | |
| 378 | if (pipe(netState->wakePipe) < 0) { |
| 379 | PLOG(ERROR) << "pipe failed"; |
| 380 | return false; |
| 381 | } |
| 382 | |
| 383 | return true; |
| 384 | } |
| 385 | |
| 386 | /* |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 387 | * Process incoming data. If no data is available, this will block until |
| 388 | * some arrives. |
| 389 | * |
| 390 | * If we get a full packet, handle it. |
| 391 | * |
| 392 | * To take some of the mystery out of life, we want to reject incoming |
| 393 | * connections if we already have a debugger attached. If we don't, the |
| 394 | * debugger will just mysteriously hang until it times out. We could just |
| 395 | * close the listen socket, but there's a good chance we won't be able to |
| 396 | * bind to the same port again, which would confuse utilities. |
| 397 | * |
| 398 | * Returns "false" on error (indicating that the connection has been severed), |
| 399 | * "true" if things are still okay. |
| 400 | */ |
| 401 | static bool processIncoming(JdwpState* state) { |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame^] | 402 | JdwpNetState* netState = GetNetState(state); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 403 | int readCount; |
| 404 | |
| 405 | CHECK_GE(netState->clientSock, 0); |
| 406 | |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame^] | 407 | if (!netState->HaveFullPacket()) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 408 | /* read some more, looping until we have data */ |
| 409 | errno = 0; |
| 410 | while (1) { |
| 411 | int selCount; |
| 412 | fd_set readfds; |
| 413 | int maxfd; |
| 414 | int fd; |
| 415 | |
| 416 | maxfd = netState->listenSock; |
| 417 | if (netState->clientSock > maxfd) { |
| 418 | maxfd = netState->clientSock; |
| 419 | } |
| 420 | if (netState->wakePipe[0] > maxfd) { |
| 421 | maxfd = netState->wakePipe[0]; |
| 422 | } |
| 423 | |
| 424 | if (maxfd < 0) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 425 | VLOG(jdwp) << "+++ all fds are closed"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 426 | return false; |
| 427 | } |
| 428 | |
| 429 | FD_ZERO(&readfds); |
| 430 | |
| 431 | /* configure fds; note these may get zapped by another thread */ |
| 432 | fd = netState->listenSock; |
| 433 | if (fd >= 0) { |
| 434 | FD_SET(fd, &readfds); |
| 435 | } |
| 436 | fd = netState->clientSock; |
| 437 | if (fd >= 0) { |
| 438 | FD_SET(fd, &readfds); |
| 439 | } |
| 440 | fd = netState->wakePipe[0]; |
| 441 | if (fd >= 0) { |
| 442 | FD_SET(fd, &readfds); |
| 443 | } else { |
| 444 | LOG(INFO) << "NOTE: entering select w/o wakepipe"; |
| 445 | } |
| 446 | |
| 447 | /* |
| 448 | * Select blocks until it sees activity on the file descriptors. |
| 449 | * Closing the local file descriptor does not count as activity, |
| 450 | * so we can't rely on that to wake us up (it works for read() |
| 451 | * and accept(), but not select()). |
| 452 | * |
| 453 | * We can do one of three things: (1) send a signal and catch |
| 454 | * EINTR, (2) open an additional fd ("wakePipe") and write to |
| 455 | * it when it's time to exit, or (3) time out periodically and |
| 456 | * re-issue the select. We're currently using #2, as it's more |
| 457 | * reliable than #1 and generally better than #3. Wastes two fds. |
| 458 | */ |
| 459 | selCount = select(maxfd+1, &readfds, NULL, NULL, NULL); |
| 460 | if (selCount < 0) { |
| 461 | if (errno == EINTR) { |
| 462 | continue; |
| 463 | } |
| 464 | PLOG(ERROR) << "select failed"; |
| 465 | goto fail; |
| 466 | } |
| 467 | |
| 468 | if (netState->wakePipe[0] >= 0 && FD_ISSET(netState->wakePipe[0], &readfds)) { |
| 469 | if (netState->listenSock >= 0) { |
| 470 | LOG(ERROR) << "Exit wake set, but not exiting?"; |
| 471 | } else { |
| 472 | LOG(DEBUG) << "Got wake-up signal, bailing out of select"; |
| 473 | } |
| 474 | goto fail; |
| 475 | } |
| 476 | if (netState->listenSock >= 0 && FD_ISSET(netState->listenSock, &readfds)) { |
| 477 | LOG(INFO) << "Ignoring second debugger -- accepting and dropping"; |
| 478 | union { |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 479 | sockaddr_in addrInet; |
| 480 | sockaddr addrPlain; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 481 | } addr; |
| 482 | socklen_t addrlen; |
| 483 | int tmpSock; |
| 484 | tmpSock = accept(netState->listenSock, &addr.addrPlain, &addrlen); |
| 485 | if (tmpSock < 0) { |
| 486 | LOG(INFO) << "Weird -- accept failed"; |
| 487 | } else { |
| 488 | close(tmpSock); |
| 489 | } |
| 490 | } |
| 491 | if (netState->clientSock >= 0 && FD_ISSET(netState->clientSock, &readfds)) { |
| 492 | readCount = read(netState->clientSock, netState->inputBuffer + netState->inputCount, sizeof(netState->inputBuffer) - netState->inputCount); |
| 493 | if (readCount < 0) { |
| 494 | /* read failed */ |
| 495 | if (errno != EINTR) { |
| 496 | goto fail; |
| 497 | } |
| 498 | LOG(DEBUG) << "+++ EINTR hit"; |
| 499 | return true; |
| 500 | } else if (readCount == 0) { |
| 501 | /* EOF hit -- far end went away */ |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame^] | 502 | VLOG(jdwp) << "+++ peer disconnected"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 503 | goto fail; |
| 504 | } else { |
| 505 | break; |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | netState->inputCount += readCount; |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame^] | 511 | if (!netState->HaveFullPacket()) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 512 | return true; /* still not there yet */ |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | /* |
| 517 | * Special-case the initial handshake. For some bizarre reason we're |
| 518 | * expected to emulate bad tty settings by echoing the request back |
| 519 | * exactly as it was sent. Note the handshake is always initiated by |
| 520 | * the debugger, no matter who connects to whom. |
| 521 | * |
| 522 | * Other than this one case, the protocol [claims to be] stateless. |
| 523 | */ |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame^] | 524 | if (netState->IsAwaitingHandshake()) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 525 | int cc; |
| 526 | |
| 527 | if (memcmp(netState->inputBuffer, kMagicHandshake, kMagicHandshakeLen) != 0) { |
| 528 | LOG(ERROR) << StringPrintf("ERROR: bad handshake '%.14s'", netState->inputBuffer); |
| 529 | goto fail; |
| 530 | } |
| 531 | |
| 532 | errno = 0; |
Elliott Hughes | 068193c | 2013-04-15 16:05:28 -0700 | [diff] [blame] | 533 | cc = TEMP_FAILURE_RETRY(write(netState->clientSock, netState->inputBuffer, kMagicHandshakeLen)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 534 | if (cc != kMagicHandshakeLen) { |
| 535 | PLOG(ERROR) << "Failed writing handshake bytes (" << cc << " of " << kMagicHandshakeLen << ")"; |
| 536 | goto fail; |
| 537 | } |
| 538 | |
Elliott Hughes | cb69306 | 2013-02-21 09:48:08 -0800 | [diff] [blame] | 539 | netState->ConsumeBytes(kMagicHandshakeLen); |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame^] | 540 | netState->SetAwaitingHandshake(false); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 541 | VLOG(jdwp) << "+++ handshake complete"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 542 | return true; |
| 543 | } |
| 544 | |
| 545 | /* |
| 546 | * Handle this packet. |
| 547 | */ |
Elliott Hughes | cb69306 | 2013-02-21 09:48:08 -0800 | [diff] [blame] | 548 | return state->HandlePacket(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 549 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 550 | fail: |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame^] | 551 | netState->Close(); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 552 | return false; |
| 553 | } |
| 554 | |
| 555 | /* |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 556 | * Our functions. |
| 557 | * |
| 558 | * We can't generally share the implementations with other transports, |
| 559 | * even if they're also socket-based, because our JdwpNetState will be |
| 560 | * different from theirs. |
| 561 | */ |
| 562 | static const JdwpTransport socketTransport = { |
| 563 | prepareSocket, |
| 564 | acceptConnection, |
| 565 | establishConnection, |
Elliott Hughes | 68a5e3c | 2013-04-17 17:13:35 -0700 | [diff] [blame^] | 566 | netShutdown, |
| 567 | netFree, |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 568 | processIncoming, |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 569 | }; |
| 570 | |
| 571 | /* |
| 572 | * Return our set. |
| 573 | */ |
| 574 | const JdwpTransport* SocketTransport() { |
| 575 | return &socketTransport; |
| 576 | } |
| 577 | |
| 578 | } // namespace JDWP |
| 579 | |
| 580 | } // namespace art |