int handshake() { int bytes; unsigned long buf[64]; // Receive DRPC hello request bytes = recv_fully(debugger, (char *) buf, 256, 0); if (bytes == 0 || bytes < 0) return -1; if (buf[0] != DRPC_SIGNATURE) { logmsg("unexpected signature %08X\n"); return -1; } // Send DRPC hello response startmark = DRPC_STARTMARK; memset(buf, 0, 128); buf[0] = DRPC_SIGNATURE; buf[1] = 2; buf[6] = startmark; buf[10] = 1; send(debugger, (char *) buf, 128, 0); return 0; }
static void * recv_id(void) { void *result; jint nbytes = recv_fully(hprof_fd, (char *)&result, sizeof(void *)); if (nbytes == 0) { result = (void *)-1; } return result; }
static unsigned int recv_u4(void) { unsigned int i; jint nbytes = recv_fully(hprof_fd, (char *)&i, sizeof(unsigned int)); if (nbytes == 0) { i = (unsigned int)-1; } return ntohl(i); }
static unsigned short recv_u2(void) { unsigned short s; jint nbytes = recv_fully(hprof_fd, (char *)&s, sizeof(unsigned short)); if (nbytes == 0) { s = (unsigned short)-1; } return ntohs(s); }
static unsigned char recv_u1(void) { unsigned char c; jint nbytes = recv_fully(hprof_fd, (char *)&c, sizeof(unsigned char)); if (nbytes == 0) { c = HPROF_CMD_EOF; } return c; }
static jdwpTransportError handshake(int fd, jlong timeout) { const char *hello = "JDWP-Handshake"; char b[16]; int rv, helloLen, received; if (timeout > 0) { dbgsysConfigureBlocking(fd, JNI_FALSE); } helloLen = (int)strlen(hello); received = 0; while (received < helloLen) { int n; char *buf; if (timeout > 0) { rv = dbgsysPoll(fd, JNI_TRUE, JNI_FALSE, (long)timeout); if (rv <= 0) { setLastError(0, "timeout during handshake"); return JDWPTRANSPORT_ERROR_IO_ERROR; } } buf = b; buf += received; n = recv_fully(fd, buf, helloLen-received); if (n == 0) { setLastError(0, "handshake failed - connection prematurally closed"); return JDWPTRANSPORT_ERROR_IO_ERROR; } if (n < 0) { RETURN_IO_ERROR("recv failed during handshake"); } received += n; } if (timeout > 0) { dbgsysConfigureBlocking(fd, JNI_TRUE); } if (strncmp(b, hello, received) != 0) { char msg[80+2*16]; b[received] = '\0'; /* * We should really use snprintf here but it's not available on Windows. * We can't use jio_snprintf without linking the transport against the VM. */ sprintf(msg, "handshake failed - received >%s< - expected >%s<", b, hello); setLastError(0, msg); return JDWPTRANSPORT_ERROR_IO_ERROR; } if (send_fully(fd, (char*)hello, helloLen) != helloLen) { RETURN_IO_ERROR("send failed during handshake"); } return JDWPTRANSPORT_ERROR_NONE; }
static jdwpTransportError JNICALL socketTransport_readPacket(jdwpTransportEnv* env, jdwpPacket* packet) { jint length, data_len; jint n; /* packet can't be null */ if (packet == NULL) { RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, "packet is null"); } /* read the length field */ n = recv_fully(socketFD, (char *)&length, sizeof(jint)); /* check for EOF */ if (n == 0) { packet->type.cmd.len = 0; return JDWPTRANSPORT_ERROR_NONE; } if (n != sizeof(jint)) { RETURN_RECV_ERROR(n); } length = (jint)dbgsysNetworkToHostLong(length); packet->type.cmd.len = length; n = recv_fully(socketFD,(char *)&(packet->type.cmd.id),sizeof(jint)); if (n < (int)sizeof(jint)) { RETURN_RECV_ERROR(n); } packet->type.cmd.id = (jint)dbgsysNetworkToHostLong(packet->type.cmd.id); n = recv_fully(socketFD,(char *)&(packet->type.cmd.flags),sizeof(jbyte)); if (n < (int)sizeof(jbyte)) { RETURN_RECV_ERROR(n); } if (packet->type.cmd.flags & JDWPTRANSPORT_FLAGS_REPLY) { n = recv_fully(socketFD,(char *)&(packet->type.reply.errorCode),sizeof(jbyte)); if (n < (int)sizeof(jshort)) { RETURN_RECV_ERROR(n); } /* FIXME - should the error be converted to host order?? */ } else { n = recv_fully(socketFD,(char *)&(packet->type.cmd.cmdSet),sizeof(jbyte)); if (n < (int)sizeof(jbyte)) { RETURN_RECV_ERROR(n); } n = recv_fully(socketFD,(char *)&(packet->type.cmd.cmd),sizeof(jbyte)); if (n < (int)sizeof(jbyte)) { RETURN_RECV_ERROR(n); } } data_len = length - ((sizeof(jint) * 2) + (sizeof(jbyte) * 3)); if (data_len < 0) { setLastError(0, "Badly formed packet received - invalid length"); return JDWPTRANSPORT_ERROR_IO_ERROR; } else if (data_len == 0) { packet->type.cmd.data = NULL; } else { packet->type.cmd.data= (*callback->alloc)(data_len); if (packet->type.cmd.data == NULL) { RETURN_ERROR(JDWPTRANSPORT_ERROR_OUT_OF_MEMORY, "out of memory"); } n = recv_fully(socketFD,(char *)packet->type.cmd.data, data_len); if (n < data_len) { (*callback->free)(packet->type.cmd.data); RETURN_RECV_ERROR(n); } } return JDWPTRANSPORT_ERROR_NONE; }
void handle_session() { struct drpc_packet pkt; int bytes; char *buf = NULL; unsigned int buflen = 0; char fn[256]; printf("starting session\n"); sprintf(fn, "dbgspy%8d.log", GetTickCount()); //logfile = fopen(fn, "w"); first_event = 1; if (handshake() < 0) { closesocket(debugger); return; } while (1) { // Receive header from debugger bytes = recv_fully(debugger, (char *) &pkt, sizeof(struct drpc_packet), 0); if (bytes == 0 || bytes < 0) { closesocket(debugger); break; } // Allocate data if (pkt.reqlen > 0 && pkt.reqlen > buflen) { buf = realloc(buf, pkt.reqlen); buflen = pkt.reqlen; } if (pkt.rsplen > 0 && pkt.rsplen > buflen) { buf = realloc(buf, pkt.rsplen); buflen = pkt.rsplen; } // Receive request data if (pkt.reqlen > 0) recv_fully(debugger, buf, pkt.reqlen, 0); // Handle command handle_command(&pkt, buf); // Send response to debugger send(debugger, (char *) &pkt, sizeof(struct drpc_packet), 0); if (pkt.rsplen > 0) send(debugger, buf, pkt.rsplen, 0); } printf("terminating session\n"); if (logfile) fclose(logfile); logfile = NULL; if (buf) free(buf); if (session) { dbg_close_session(session, 0); session = NULL; } }