blob: 755ded59eded870843a3f9f1d8238c3280e5490b [file] [log] [blame]
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001/*
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 Hughes872d4ec2011-10-21 17:07:15 -070016
Elliott Hughes07ed66b2012-12-12 18:34:25 -080017#include <arpa/inet.h>
Elliott Hughes872d4ec2011-10-21 17:07:15 -070018#include <errno.h>
Elliott Hughes07ed66b2012-12-12 18:34:25 -080019#include <netdb.h>
Elliott Hughes872d4ec2011-10-21 17:07:15 -070020#include <netinet/in.h>
21#include <netinet/tcp.h>
Elliott Hughes07ed66b2012-12-12 18:34:25 -080022#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 Hughese222ee02012-12-13 14:41:43 -080030#include "base/stringprintf.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080031#include "jdwp/jdwp_priv.h"
Elliott Hughes872d4ec2011-10-21 17:07:15 -070032
33#define kBasePort 8000
34#define kMaxPort 8040
35
Elliott Hughes872d4ec2011-10-21 17:07:15 -070036namespace art {
37
38namespace JDWP {
39
Elliott Hughes872d4ec2011-10-21 17:07:15 -070040/*
41 * JDWP network state.
42 *
43 * We only talk to one debugger at a time.
44 */
45struct JdwpNetState : public JdwpNetStateBase {
Elliott Hughes74847412012-06-20 18:10:21 -070046 uint16_t listenPort;
47 int listenSock; /* listen for connection from debugger */
48 int wakePipe[2]; /* break out of select */
Elliott Hughes872d4ec2011-10-21 17:07:15 -070049
Elliott Hughes74847412012-06-20 18:10:21 -070050 in_addr remoteAddr;
51 uint16_t remotePort;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070052
Elliott Hughes74847412012-06-20 18:10:21 -070053 JdwpNetState() {
54 listenPort = 0;
55 listenSock = -1;
56 wakePipe[0] = -1;
57 wakePipe[1] = -1;
Elliott Hughes74847412012-06-20 18:10:21 -070058 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -070059};
60
Elliott Hughes68a5e3c2013-04-17 17:13:35 -070061static void socketStateShutdown(JdwpNetState* state);
62static void socketStateFree(JdwpNetState* state);
63
64static JdwpNetState* GetNetState(JdwpState* state) {
65 return reinterpret_cast<JdwpNetState*>(state->netState);
66}
67
Elliott Hughes6d8dd472012-01-17 18:27:41 -080068static JdwpNetState* netStartup(uint16_t port, bool probe);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070069
70/*
71 * Set up some stuff for transport=dt_socket.
72 */
Elliott Hughes376a7a02011-10-24 18:35:55 -070073static bool prepareSocket(JdwpState* state, const JdwpOptions* options) {
Elliott Hughes6d8dd472012-01-17 18:27:41 -080074 uint16_t port = options->port;
Elliott Hughes872d4ec2011-10-21 17:07:15 -070075
Elliott Hughes376a7a02011-10-24 18:35:55 -070076 if (options->server) {
77 if (options->port != 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070078 /* try only the specified port */
Elliott Hughes3d30d9b2011-12-07 17:35:48 -080079 state->netState = netStartup(port, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070080 } else {
81 /* scan through a range of ports, binding to the first available */
82 for (port = kBasePort; port <= kMaxPort; port++) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -080083 state->netState = netStartup(port, true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070084 if (state->netState != NULL) {
85 break;
86 }
87 }
88 }
89 if (state->netState == NULL) {
Elliott Hughes376a7a02011-10-24 18:35:55 -070090 LOG(ERROR) << "JDWP net startup failed (req port=" << options->port << ")";
Elliott Hughes872d4ec2011-10-21 17:07:15 -070091 return false;
92 }
93 } else {
Elliott Hughes6d8dd472012-01-17 18:27:41 -080094 state->netState = netStartup(0, false);
Elliott Hughes872d4ec2011-10-21 17:07:15 -070095 }
96
Elliott Hughes376a7a02011-10-24 18:35:55 -070097 if (options->suspend) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -070098 LOG(INFO) << "JDWP will wait for debugger on port " << port;
99 } else {
Elliott Hughes376a7a02011-10-24 18:35:55 -0700100 LOG(INFO) << "JDWP will " << (options->server ? "listen" : "connect") << " on port " << port;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700101 }
102
103 return true;
104}
105
106/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700107 * Initialize JDWP stuff.
108 *
Elliott Hughes6d8dd472012-01-17 18:27:41 -0800109 * 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 Hughes872d4ec2011-10-21 17:07:15 -0700111 * 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 Hughes6d8dd472012-01-17 18:27:41 -0800118static JdwpNetState* netStartup(uint16_t port, bool probe) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700119 JdwpNetState* netState = new JdwpNetState;
Elliott Hughes6d8dd472012-01-17 18:27:41 -0800120 if (port == 0) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700121 return netState;
122 }
123
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700124 netState->listenSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
125 if (netState->listenSock < 0) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800126 PLOG(probe ? ERROR : FATAL) << "Socket create failed";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700127 goto fail;
128 }
129
130 /* allow immediate re-use */
Elliott Hughes6d8dd472012-01-17 18:27:41 -0800131 {
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 Hughes872d4ec2011-10-21 17:07:15 -0700137 }
138
139 union {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700140 sockaddr_in addrInet;
141 sockaddr addrPlain;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700142 } 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 Hughes3d30d9b2011-12-07 17:35:48 -0800148 PLOG(probe ? ERROR : FATAL) << "Attempt to bind to port " << port << " failed";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700149 goto fail;
150 }
151
152 netState->listenPort = port;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700153
154 if (listen(netState->listenSock, 5) != 0) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800155 PLOG(probe ? ERROR : FATAL) << "Listen failed";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700156 goto fail;
157 }
158
159 return netState;
160
Elliott Hughesa21039c2012-06-21 12:09:25 -0700161 fail:
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700162 socketStateShutdown(netState);
163 socketStateFree(netState);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700164 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 Hughes68a5e3c2013-04-17 17:13:35 -0700178static void socketStateShutdown(JdwpNetState* netState) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700179 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 Hughes4dd9b4d2011-12-12 18:29:24 -0800201 VLOG(jdwp) << "+++ writing to wakePipe";
Elliott Hughes068193c2013-04-15 16:05:28 -0700202 TEMP_FAILURE_RETRY(write(netState->wakePipe[1], "", 1));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700203 }
204}
205
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700206static void netShutdown(JdwpState* state) {
207 socketStateShutdown(GetNetState(state));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700208}
209
210/*
211 * Free JDWP state.
212 *
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700213 * Call this after shutting the network down with socketStateShutdown().
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700214 */
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700215static void socketStateFree(JdwpNetState* netState) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700216 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 Hughes68a5e3c2013-04-17 17:13:35 -0700234static void netFree(JdwpState* state) {
235 socketStateFree(GetNetState(state));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700236}
237
238/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700239 * 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 Hughes74847412012-06-20 18:10:21 -0700243static 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 Hughes872d4ec2011-10-21 17:07:15 -0700248}
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 Hughes74847412012-06-20 18:10:21 -0700255static bool acceptConnection(JdwpState* state) {
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700256 JdwpNetState* netState = GetNetState(state);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700257 union {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700258 sockaddr_in addrInet;
259 sockaddr addrPlain;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700260 } 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 Hughes4dd9b4d2011-12-12 18:29:24 -0800277 if (VLOG_IS_ON(jdwp)) {
278 PLOG(ERROR) << "accept failed";
279 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700280 } 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 Hughes4dd9b4d2011-12-12 18:29:24 -0800289 VLOG(jdwp) << "+++ accepted connection from " << inet_ntoa(netState->remoteAddr) << ":" << netState->remotePort;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700290
291 netState->clientSock = sock;
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700292 netState->SetAwaitingHandshake(true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700293 netState->inputCount = 0;
294
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800295 VLOG(jdwp) << "Setting TCP_NODELAY on accepted socket";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700296 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 Hughesa21039c2012-06-21 12:09:25 -0700309static bool establishConnection(JdwpState* state, const JdwpOptions* options) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700310 union {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700311 sockaddr_in addrInet;
312 sockaddr addrPlain;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700313 } addr;
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700314 hostent* pEntry;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700315
316 CHECK(state != NULL && state->netState != NULL);
Elliott Hughesa21039c2012-06-21 12:09:25 -0700317 CHECK(!options->server);
318 CHECK(!options->host.empty());
319 CHECK_NE(options->port, 0);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700320
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 Hughes7b9d9962012-04-20 18:48:18 -0700327 hostent he;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700328 char auxBuf[128];
329 int error;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700330 int cc = gethostbyname_r(options->host.c_str(), &he, auxBuf, sizeof(auxBuf), &pEntry, &error);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700331 if (cc != 0) {
Elliott Hughesa21039c2012-06-21 12:09:25 -0700332 LOG(WARNING) << "gethostbyname_r('" << options->host << "') failed: " << hstrerror(error);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700333 return false;
334 }
335#else
336 h_errno = 0;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700337 pEntry = gethostbyname(options->host.c_str());
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700338 if (pEntry == NULL) {
Elliott Hughesa21039c2012-06-21 12:09:25 -0700339 PLOG(WARNING) << "gethostbyname('" << options->host << "') failed";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700340 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 Hughesa21039c2012-06-21 12:09:25 -0700348 addr.addrInet.sin_port = htons(options->port);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700349
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 Hughes68a5e3c2013-04-17 17:13:35 -0700355 JdwpNetState* netState = GetNetState(state);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700356 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 Hughesa21039c2012-06-21 12:09:25 -0700372 LOG(INFO) << "Connection established to " << options->host << " (" << inet_ntoa(addr.addrInet.sin_addr) << ":" << ntohs(addr.addrInet.sin_port) << ")";
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700373 netState->SetAwaitingHandshake(true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700374 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 Hughes872d4ec2011-10-21 17:07:15 -0700387 * 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 */
401static bool processIncoming(JdwpState* state) {
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700402 JdwpNetState* netState = GetNetState(state);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700403 int readCount;
404
405 CHECK_GE(netState->clientSock, 0);
406
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700407 if (!netState->HaveFullPacket()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700408 /* 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 Hughes4dd9b4d2011-12-12 18:29:24 -0800425 VLOG(jdwp) << "+++ all fds are closed";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700426 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 Hughes7b9d9962012-04-20 18:48:18 -0700479 sockaddr_in addrInet;
480 sockaddr addrPlain;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700481 } 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 Hughes68a5e3c2013-04-17 17:13:35 -0700502 VLOG(jdwp) << "+++ peer disconnected";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700503 goto fail;
504 } else {
505 break;
506 }
507 }
508 }
509
510 netState->inputCount += readCount;
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700511 if (!netState->HaveFullPacket()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700512 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 Hughes68a5e3c2013-04-17 17:13:35 -0700524 if (netState->IsAwaitingHandshake()) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700525 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 Hughes068193c2013-04-15 16:05:28 -0700533 cc = TEMP_FAILURE_RETRY(write(netState->clientSock, netState->inputBuffer, kMagicHandshakeLen));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700534 if (cc != kMagicHandshakeLen) {
535 PLOG(ERROR) << "Failed writing handshake bytes (" << cc << " of " << kMagicHandshakeLen << ")";
536 goto fail;
537 }
538
Elliott Hughescb693062013-02-21 09:48:08 -0800539 netState->ConsumeBytes(kMagicHandshakeLen);
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700540 netState->SetAwaitingHandshake(false);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800541 VLOG(jdwp) << "+++ handshake complete";
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700542 return true;
543 }
544
545 /*
546 * Handle this packet.
547 */
Elliott Hughescb693062013-02-21 09:48:08 -0800548 return state->HandlePacket();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700549
Elliott Hughesa21039c2012-06-21 12:09:25 -0700550 fail:
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700551 netState->Close();
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700552 return false;
553}
554
555/*
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700556 * 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 */
562static const JdwpTransport socketTransport = {
563 prepareSocket,
564 acceptConnection,
565 establishConnection,
Elliott Hughes68a5e3c2013-04-17 17:13:35 -0700566 netShutdown,
567 netFree,
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700568 processIncoming,
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700569};
570
571/*
572 * Return our set.
573 */
574const JdwpTransport* SocketTransport() {
575 return &socketTransport;
576}
577
578} // namespace JDWP
579
580} // namespace art