src/share/transport/socket/socketTransport.c

Print this page




 117     jvalue dontcare;
 118     int err;
 119 
 120     dontcare.i = 0;  /* keep compiler happy */
 121 
 122     err = dbgsysSetSocketOption(fd, SO_REUSEADDR, JNI_TRUE, dontcare);
 123     if (err < 0) {
 124         RETURN_IO_ERROR("setsockopt SO_REUSEADDR failed");
 125     }
 126 
 127     err = dbgsysSetSocketOption(fd, TCP_NODELAY, JNI_TRUE, dontcare);
 128     if (err < 0) {
 129         RETURN_IO_ERROR("setsockopt TCPNODELAY failed");
 130     }
 131 
 132     return JDWPTRANSPORT_ERROR_NONE;
 133 }
 134 
 135 static jdwpTransportError
 136 handshake(int fd, jlong timeout) {
 137     char *hello = "JDWP-Handshake";
 138     char b[16];
 139     int rv, received, i;
 140 
 141     if (timeout > 0) {
 142         dbgsysConfigureBlocking(fd, JNI_FALSE);
 143     }

 144     received = 0;
 145     while (received < (int)strlen(hello)) {
 146         int n;
 147         char *buf;
 148         if (timeout > 0) {
 149             rv = dbgsysPoll(fd, JNI_TRUE, JNI_FALSE, (long)timeout);
 150             if (rv <= 0) {
 151                 setLastError(0, "timeout during handshake");
 152                 return JDWPTRANSPORT_ERROR_IO_ERROR;
 153             }
 154         }
 155         buf = b;
 156         buf += received;
 157         n = dbgsysRecv(fd, buf, (int)strlen(hello)-received, 0);
 158         if (n == 0) {
 159             setLastError(0, "handshake failed - connection prematurally closed");
 160             return JDWPTRANSPORT_ERROR_IO_ERROR;
 161         }
 162         if (n < 0) {
 163             RETURN_IO_ERROR("recv failed during handshake");
 164         }
 165         received += n;
 166     }
 167     if (timeout > 0) {
 168         dbgsysConfigureBlocking(fd, JNI_TRUE);
 169     }
 170     for (i=0; i<(int)strlen(hello); i++) {
 171         if (b[i] != hello[i]) {
 172             char msg[64];
 173             strcpy(msg, "handshake failed - received >");
 174             strncat(msg, b, strlen(hello));
 175             strcat(msg, "< - excepted >");
 176             strcat(msg, hello);
 177             strcat(msg, "<");
 178             setLastError(0, msg);
 179             return JDWPTRANSPORT_ERROR_IO_ERROR;
 180         }
 181     }
 182 
 183     if (dbgsysSend(fd, hello, (int)strlen(hello), 0) != (int)strlen(hello)) {
 184         RETURN_IO_ERROR("send failed during handshake");
 185     }
 186     return JDWPTRANSPORT_ERROR_NONE;
 187 }
 188 
 189 static jdwpTransportError
 190 parseAddress(const char *address, struct sockaddr_in *sa, uint32_t defaultHost) {
 191     char *colon;
 192 
 193     memset((void *)sa,0,sizeof(struct sockaddr_in));
 194     sa->sin_family = AF_INET;
 195 
 196     /* check for host:port or port */
 197     colon = strchr(address, ':');
 198     if (colon == NULL) {
 199         u_short port = (u_short)atoi(address);
 200         sa->sin_port = dbgsysHostToNetworkShort(port);
 201         sa->sin_addr.s_addr = dbgsysHostToNetworkLong(defaultHost);
 202     } else {
 203         char *buf;




 117     jvalue dontcare;
 118     int err;
 119 
 120     dontcare.i = 0;  /* keep compiler happy */
 121 
 122     err = dbgsysSetSocketOption(fd, SO_REUSEADDR, JNI_TRUE, dontcare);
 123     if (err < 0) {
 124         RETURN_IO_ERROR("setsockopt SO_REUSEADDR failed");
 125     }
 126 
 127     err = dbgsysSetSocketOption(fd, TCP_NODELAY, JNI_TRUE, dontcare);
 128     if (err < 0) {
 129         RETURN_IO_ERROR("setsockopt TCPNODELAY failed");
 130     }
 131 
 132     return JDWPTRANSPORT_ERROR_NONE;
 133 }
 134 
 135 static jdwpTransportError
 136 handshake(int fd, jlong timeout) {
 137     const char *hello = "JDWP-Handshake";
 138     char b[16];
 139     int rv, helloLen, received;
 140 
 141     if (timeout > 0) {
 142         dbgsysConfigureBlocking(fd, JNI_FALSE);
 143     }
 144     helloLen = (int)strlen(hello);
 145     received = 0;
 146     while (received < helloLen) {
 147         int n;
 148         char *buf;
 149         if (timeout > 0) {
 150             rv = dbgsysPoll(fd, JNI_TRUE, JNI_FALSE, (long)timeout);
 151             if (rv <= 0) {
 152                 setLastError(0, "timeout during handshake");
 153                 return JDWPTRANSPORT_ERROR_IO_ERROR;
 154             }
 155         }
 156         buf = b;
 157         buf += received;
 158         n = dbgsysRecv(fd, buf, helloLen-received, 0);
 159         if (n == 0) {
 160             setLastError(0, "handshake failed - connection prematurally closed");
 161             return JDWPTRANSPORT_ERROR_IO_ERROR;
 162         }
 163         if (n < 0) {
 164             RETURN_IO_ERROR("recv failed during handshake");
 165         }
 166         received += n;
 167     }
 168     if (timeout > 0) {
 169         dbgsysConfigureBlocking(fd, JNI_TRUE);
 170     }
 171     if (strncmp(b, hello, received) != 0) {
 172         char msg[80+2*16];
 173         b[received] = '\0';
 174         /*
 175          * We should really use snprintf here but it's not available on Windows. 
 176          * We can't use jio_snprintf without linking the transport against the VM.
 177          */
 178         sprintf(msg, "handshake failed - received >%s< - expected >%s<", b, hello);
 179         setLastError(0, msg);
 180         return JDWPTRANSPORT_ERROR_IO_ERROR;
 181     }

 182 
 183     if (dbgsysSend(fd, (char*)hello, helloLen, 0) != helloLen) {
 184         RETURN_IO_ERROR("send failed during handshake");
 185     }
 186     return JDWPTRANSPORT_ERROR_NONE;
 187 }
 188 
 189 static jdwpTransportError
 190 parseAddress(const char *address, struct sockaddr_in *sa, uint32_t defaultHost) {
 191     char *colon;
 192 
 193     memset((void *)sa,0,sizeof(struct sockaddr_in));
 194     sa->sin_family = AF_INET;
 195 
 196     /* check for host:port or port */
 197     colon = strchr(address, ':');
 198     if (colon == NULL) {
 199         u_short port = (u_short)atoi(address);
 200         sa->sin_port = dbgsysHostToNetworkShort(port);
 201         sa->sin_addr.s_addr = dbgsysHostToNetworkLong(defaultHost);
 202     } else {
 203         char *buf;