jdk/src/jdk.jdwp.agent/share/native/libdt_socket/socketTransport.c
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 24514 jdk/src/share/transport/socket/socketTransport.c@2440b44952d7
child 36134 63514f38c74c
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 22605
diff changeset
     2
 * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3723
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3723
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3723
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3723
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3723
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <errno.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include <ctype.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include "jdwpTransport.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include "sysSocket.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
24503
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
    34
#ifdef _WIN32
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
    35
 #include <winsock2.h>
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
    36
 #include <ws2tcpip.h>
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
    37
#endif
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
    38
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * The Socket Transport Library.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * This module is an implementation of the Java Debug Wire Protocol Transport
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Service Provider Interface - see src/share/javavm/export/jdwpTransport.h.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
static int serverSocketFD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
static int socketFD = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
static jdwpTransportCallback *callback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
static JavaVM *jvm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
static int tlsIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
static jboolean initialized;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
static struct jdwpTransportNativeInterface_ interface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
static jdwpTransportEnv single_env = (jdwpTransportEnv)&interface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
#define RETURN_ERROR(err, msg) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        if (1==1) { \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            setLastError(err, msg); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            return err; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
#define RETURN_IO_ERROR(msg)    RETURN_ERROR(JDWPTRANSPORT_ERROR_IO_ERROR, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
#define RETURN_RECV_ERROR(n) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if (n == 0) { \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            RETURN_ERROR(JDWPTRANSPORT_ERROR_IO_ERROR, "premature EOF"); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        } else { \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            RETURN_IO_ERROR("recv error"); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
#define HEADER_SIZE     11
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
#define MAX_DATA_SIZE 1000
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    73
static jint recv_fully(int, char *, int);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    74
static jint send_fully(int, char *, int);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    75
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * Record the last error for this thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
setLastError(jdwpTransportError err, char *newmsg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    char buf[255];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    char *msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /* get any I/O first in case any system calls override errno */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    if (err == JDWPTRANSPORT_ERROR_IO_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        dbgsysGetLastIOError(buf, sizeof(buf));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    msg = (char *)dbgsysTlsGet(tlsIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    if (msg != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        (*callback->free)(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    if (err == JDWPTRANSPORT_ERROR_IO_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        char *join_str = ": ";
896
5c02031316bf 6725543: Compiler warnings in serviceability native code
ohair
parents: 2
diff changeset
    96
        int msg_len = (int)strlen(newmsg) + (int)strlen(join_str) +
5c02031316bf 6725543: Compiler warnings in serviceability native code
ohair
parents: 2
diff changeset
    97
                      (int)strlen(buf) + 3;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        msg = (*callback->alloc)(msg_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (msg != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            strcpy(msg, newmsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            strcat(msg, join_str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            strcat(msg, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    } else {
896
5c02031316bf 6725543: Compiler warnings in serviceability native code
ohair
parents: 2
diff changeset
   105
        msg = (*callback->alloc)((int)strlen(newmsg)+1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if (msg != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            strcpy(msg, newmsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    dbgsysTlsPut(tlsIndex, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * Return the last error for this thread (may be NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
static char*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
getLastError() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    return (char *)dbgsysTlsGet(tlsIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
static jdwpTransportError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
setOptions(int fd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    jvalue dontcare;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    int err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    dontcare.i = 0;  /* keep compiler happy */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    err = dbgsysSetSocketOption(fd, SO_REUSEADDR, JNI_TRUE, dontcare);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    if (err < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        RETURN_IO_ERROR("setsockopt SO_REUSEADDR failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    err = dbgsysSetSocketOption(fd, TCP_NODELAY, JNI_TRUE, dontcare);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    if (err < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        RETURN_IO_ERROR("setsockopt TCPNODELAY failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
static jdwpTransportError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
handshake(int fd, jlong timeout) {
3723
a27278866f80 6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents: 1247
diff changeset
   145
    const char *hello = "JDWP-Handshake";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    char b[16];
3723
a27278866f80 6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents: 1247
diff changeset
   147
    int rv, helloLen, received;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    if (timeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        dbgsysConfigureBlocking(fd, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
3723
a27278866f80 6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents: 1247
diff changeset
   152
    helloLen = (int)strlen(hello);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    received = 0;
3723
a27278866f80 6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents: 1247
diff changeset
   154
    while (received < helloLen) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        char *buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (timeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            rv = dbgsysPoll(fd, JNI_TRUE, JNI_FALSE, (long)timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            if (rv <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                setLastError(0, "timeout during handshake");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                return JDWPTRANSPORT_ERROR_IO_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        buf = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        buf += received;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   166
        n = recv_fully(fd, buf, helloLen-received);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (n == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            setLastError(0, "handshake failed - connection prematurally closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            return JDWPTRANSPORT_ERROR_IO_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (n < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            RETURN_IO_ERROR("recv failed during handshake");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        received += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    if (timeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        dbgsysConfigureBlocking(fd, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
3723
a27278866f80 6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents: 1247
diff changeset
   179
    if (strncmp(b, hello, received) != 0) {
a27278866f80 6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents: 1247
diff changeset
   180
        char msg[80+2*16];
a27278866f80 6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents: 1247
diff changeset
   181
        b[received] = '\0';
a27278866f80 6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents: 1247
diff changeset
   182
        /*
a27278866f80 6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents: 1247
diff changeset
   183
         * We should really use snprintf here but it's not available on Windows.
a27278866f80 6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents: 1247
diff changeset
   184
         * We can't use jio_snprintf without linking the transport against the VM.
a27278866f80 6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents: 1247
diff changeset
   185
         */
a27278866f80 6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents: 1247
diff changeset
   186
        sprintf(msg, "handshake failed - received >%s< - expected >%s<", b, hello);
a27278866f80 6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents: 1247
diff changeset
   187
        setLastError(0, msg);
a27278866f80 6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents: 1247
diff changeset
   188
        return JDWPTRANSPORT_ERROR_IO_ERROR;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   191
    if (send_fully(fd, (char*)hello, helloLen) != helloLen) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        RETURN_IO_ERROR("send failed during handshake");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
24503
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   197
static uint32_t
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   198
getLocalHostAddress() {
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   199
    // Simple routine to guess localhost address.
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   200
    // it looks up "localhost" and returns 127.0.0.1 if lookup
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   201
    // fails.
24514
2440b44952d7 8043716: JDI test com/sun/jdi/ProcessAttachTest.sh and other 3 jdi tests failed in nightly
dsamersoff
parents: 24503
diff changeset
   202
    struct addrinfo hints, *res = NULL;
24503
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   203
    int err;
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   204
24514
2440b44952d7 8043716: JDI test com/sun/jdi/ProcessAttachTest.sh and other 3 jdi tests failed in nightly
dsamersoff
parents: 24503
diff changeset
   205
    // Use portable way to initialize the structure
2440b44952d7 8043716: JDI test com/sun/jdi/ProcessAttachTest.sh and other 3 jdi tests failed in nightly
dsamersoff
parents: 24503
diff changeset
   206
    memset((void *)&hints, 0, sizeof(hints));
24503
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   207
    hints.ai_family = AF_INET;
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   208
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   209
    err = getaddrinfo("localhost", NULL, &hints, &res);
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   210
    if (err < 0 || res == NULL) {
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   211
        return dbgsysHostToNetworkLong(INADDR_LOOPBACK);
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   212
    }
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   213
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   214
    // getaddrinfo might return more than one address
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   215
    // but we are using first one only
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   216
    return ((struct sockaddr_in *)(res->ai_addr))->sin_addr.s_addr;
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   217
}
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   218
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   219
static int
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   220
getPortNumber(const char *s_port) {
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   221
    u_long n;
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   222
    char *eptr;
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   223
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   224
    if (*s_port == 0) {
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   225
        // bad address - colon with no port number in parameters
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   226
        return -1;
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   227
    }
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   228
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   229
    n = strtoul(s_port, &eptr, 10);
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   230
    if (eptr != s_port + strlen(s_port)) {
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   231
        // incomplete conversion - port number contains non-digit
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   232
        return -1;
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   233
    }
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   234
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   235
    if (n > (u_short) -1) {
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   236
        // check that value supplied by user is less than
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   237
        // maximum possible u_short value (65535) and
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   238
        // will not be truncated later.
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   239
        return -1;
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   240
    }
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   241
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   242
    return n;
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   243
}
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   244
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
static jdwpTransportError
24503
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   246
parseAddress(const char *address, struct sockaddr_in *sa) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    char *colon;
24503
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   248
    int port;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    memset((void *)sa,0,sizeof(struct sockaddr_in));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    sa->sin_family = AF_INET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    /* check for host:port or port */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    colon = strchr(address, ':');
24503
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   255
    port = getPortNumber((colon == NULL) ? address : colon +1);
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   256
    if (port < 0) {
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   257
        RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, "invalid port number specified");
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   258
    }
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   259
    sa->sin_port = dbgsysHostToNetworkShort((u_short)port);
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   260
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    if (colon == NULL) {
24503
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   262
        // bind to localhost only if no address specified
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   263
        sa->sin_addr.s_addr = getLocalHostAddress();
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   264
    } else if (strncmp(address,"localhost:",10) == 0) {
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   265
        // optimize for common case
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   266
        sa->sin_addr.s_addr = getLocalHostAddress();
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   267
    } else if (*address == '*' && *(address+1) == ':') {
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   268
        // we are explicitly asked to bind server to all available IP addresses
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   269
        // has no meaning for client.
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   270
        sa->sin_addr.s_addr = dbgsysHostToNetworkLong(INADDR_ANY);
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   271
     } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        char *buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        char *hostname;
896
5c02031316bf 6725543: Compiler warnings in serviceability native code
ohair
parents: 2
diff changeset
   274
        uint32_t addr;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
896
5c02031316bf 6725543: Compiler warnings in serviceability native code
ohair
parents: 2
diff changeset
   276
        buf = (*callback->alloc)((int)strlen(address)+1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        if (buf == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            RETURN_ERROR(JDWPTRANSPORT_ERROR_OUT_OF_MEMORY, "out of memory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        strcpy(buf, address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        buf[colon - address] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        hostname = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
         * First see if the host is a literal IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
         * If not then try to resolve it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        addr = dbgsysInetAddr(hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        if (addr == 0xffffffff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            struct hostent *hp = dbgsysGetHostByName(hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            if (hp == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                /* don't use RETURN_IO_ERROR as unknown host is normal */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                setLastError(0, "gethostbyname: unknown host");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                (*callback->free)(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                return JDWPTRANSPORT_ERROR_IO_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            /* lookup was successful */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            memcpy(&(sa->sin_addr), hp->h_addr_list[0], hp->h_length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            sa->sin_addr.s_addr = addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        (*callback->free)(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
socketTransport_getCapabilities(jdwpTransportEnv* env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        JDWPTransportCapabilities* capabilitiesPtr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    JDWPTransportCapabilities result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    memset(&result, 0, sizeof(result));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    result.can_timeout_attach = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    result.can_timeout_accept = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    result.can_timeout_handshake = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    *capabilitiesPtr = result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
socketTransport_startListening(jdwpTransportEnv* env, const char* address,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                               char** actualAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    struct sockaddr_in sa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    int err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    memset((void *)&sa,0,sizeof(struct sockaddr_in));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    sa.sin_family = AF_INET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    /* no address provided */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    if ((address == NULL) || (address[0] == '\0')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        address = "0";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
24503
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   343
    err = parseAddress(address, &sa);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    if (err != JDWPTRANSPORT_ERROR_NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        return err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    serverSocketFD = dbgsysSocket(AF_INET, SOCK_STREAM, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    if (serverSocketFD < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        RETURN_IO_ERROR("socket creation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    err = setOptions(serverSocketFD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    if (err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        return err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    err = dbgsysBind(serverSocketFD, (struct sockaddr *)&sa, sizeof(sa));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    if (err < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        RETURN_IO_ERROR("bind failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    err = dbgsysListen(serverSocketFD, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    if (err < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        RETURN_IO_ERROR("listen failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        char buf[20];
15274
a77cdd5ea6e3 8005120: Compiler warnings in socket transport native code
jzavgren
parents: 14342
diff changeset
   370
        socklen_t len = sizeof(sa);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        jint portNum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        err = dbgsysGetSocketName(serverSocketFD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                               (struct sockaddr *)&sa, &len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        portNum = dbgsysNetworkToHostShort(sa.sin_port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        sprintf(buf, "%d", portNum);
896
5c02031316bf 6725543: Compiler warnings in serviceability native code
ohair
parents: 2
diff changeset
   376
        *actualAddress = (*callback->alloc)((int)strlen(buf) + 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        if (*actualAddress == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            RETURN_ERROR(JDWPTRANSPORT_ERROR_OUT_OF_MEMORY, "out of memory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            strcpy(*actualAddress, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
socketTransport_accept(jdwpTransportEnv* env, jlong acceptTimeout, jlong handshakeTimeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
{
15274
a77cdd5ea6e3 8005120: Compiler warnings in socket transport native code
jzavgren
parents: 14342
diff changeset
   390
    socklen_t socketLen;
a77cdd5ea6e3 8005120: Compiler warnings in socket transport native code
jzavgren
parents: 14342
diff changeset
   391
    int err;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    struct sockaddr_in socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    jlong startTime = (jlong)0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * Use a default handshake timeout if not specified - this avoids an indefinite
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * hang in cases where something other than a debugger connects to our port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    if (handshakeTimeout == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        handshakeTimeout = 2000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
         * If there is an accept timeout then we put the socket in non-blocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
         * mode and poll for a connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        if (acceptTimeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            int rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            dbgsysConfigureBlocking(serverSocketFD, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            startTime = dbgsysCurrentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            rv = dbgsysPoll(serverSocketFD, JNI_TRUE, JNI_FALSE, (long)acceptTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            if (rv <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                /* set the last error here as could be overridden by configureBlocking */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                if (rv == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                    setLastError(JDWPTRANSPORT_ERROR_IO_ERROR, "poll failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                /* restore blocking state */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                dbgsysConfigureBlocking(serverSocketFD, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                if (rv == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                    RETURN_ERROR(JDWPTRANSPORT_ERROR_TIMEOUT, "timed out waiting for connection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    return JDWPTRANSPORT_ERROR_IO_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
         * Accept the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        memset((void *)&socket,0,sizeof(struct sockaddr_in));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        socketLen = sizeof(socket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        socketFD = dbgsysAccept(serverSocketFD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                                (struct sockaddr *)&socket,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                                &socketLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        /* set the last error here as could be overridden by configureBlocking */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        if (socketFD < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            setLastError(JDWPTRANSPORT_ERROR_IO_ERROR, "accept failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
         * Restore the blocking state - note that the accepted socket may be in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
         * blocking or non-blocking mode (platform dependent). However as there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
         * is a handshake timeout set then it will go into non-blocking mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
         * anyway for the handshake.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        if (acceptTimeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            dbgsysConfigureBlocking(serverSocketFD, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        if (socketFD < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            return JDWPTRANSPORT_ERROR_IO_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        /* handshake with the debugger */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        err = handshake(socketFD, handshakeTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
         * If the handshake fails then close the connection. If there if an accept
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
         * timeout then we must adjust the timeout for the next poll.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        if (err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            fprintf(stderr, "Debugger failed to attach: %s\n", getLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            dbgsysSocketClose(socketFD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            socketFD = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            if (acceptTimeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                long endTime = dbgsysCurrentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                acceptTimeout -= (endTime - startTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                if (acceptTimeout <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                    setLastError(JDWPTRANSPORT_ERROR_IO_ERROR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                        "timeout waiting for debugger to connect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                    return JDWPTRANSPORT_ERROR_IO_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    } while (socketFD < 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
socketTransport_stopListening(jdwpTransportEnv *env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    if (serverSocketFD < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_STATE, "connection not open");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    if (dbgsysSocketClose(serverSocketFD) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        RETURN_IO_ERROR("close failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    serverSocketFD = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
socketTransport_attach(jdwpTransportEnv* env, const char* addressString, jlong attachTimeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                       jlong handshakeTimeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    struct sockaddr_in sa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    int err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    if (addressString == NULL || addressString[0] == '\0') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, "address is missing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
24503
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   503
    err = parseAddress(addressString, &sa);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    if (err != JDWPTRANSPORT_ERROR_NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        return err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    socketFD = dbgsysSocket(AF_INET, SOCK_STREAM, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    if (socketFD < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        RETURN_IO_ERROR("unable to create socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    err = setOptions(socketFD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    if (err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        return err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * To do a timed connect we make the socket non-blocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * and poll with a timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    if (attachTimeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        dbgsysConfigureBlocking(socketFD, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    err = dbgsysConnect(socketFD, (struct sockaddr *)&sa, sizeof(sa));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    if (err == DBG_EINPROGRESS && attachTimeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        err = dbgsysFinishConnect(socketFD, (long)attachTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        if (err == DBG_ETIMEOUT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            dbgsysConfigureBlocking(socketFD, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            RETURN_ERROR(JDWPTRANSPORT_ERROR_TIMEOUT, "connect timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    if (err < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        RETURN_IO_ERROR("connect failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    if (attachTimeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        dbgsysConfigureBlocking(socketFD, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    err = handshake(socketFD, handshakeTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    if (err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        dbgsysSocketClose(socketFD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        socketFD = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        return err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
static jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
socketTransport_isOpen(jdwpTransportEnv* env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    if (socketFD >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
socketTransport_close(jdwpTransportEnv* env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    int fd = socketFD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    socketFD = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    if (fd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    }
22605
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 15274
diff changeset
   572
#ifdef _AIX
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 15274
diff changeset
   573
    /*
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 15274
diff changeset
   574
      AIX needs a workaround for I/O cancellation, see:
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 15274
diff changeset
   575
      http://publib.boulder.ibm.com/infocenter/pseries/v5r3/index.jsp?topic=/com.ibm.aix.basetechref/doc/basetrf1/close.htm
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 15274
diff changeset
   576
      ...
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 15274
diff changeset
   577
      The close subroutine is blocked until all subroutines which use the file
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 15274
diff changeset
   578
      descriptor return to usr space. For example, when a thread is calling close
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 15274
diff changeset
   579
      and another thread is calling select with the same file descriptor, the
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 15274
diff changeset
   580
      close subroutine does not return until the select call returns.
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 15274
diff changeset
   581
      ...
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 15274
diff changeset
   582
    */
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 15274
diff changeset
   583
    shutdown(fd, 2);
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 15274
diff changeset
   584
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    if (dbgsysSocketClose(fd) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
         * close failed - it's pointless to restore socketFD here because
15274
a77cdd5ea6e3 8005120: Compiler warnings in socket transport native code
jzavgren
parents: 14342
diff changeset
   588
         * any subsequent close will likely fail as well.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        RETURN_IO_ERROR("close failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
socketTransport_writePacket(jdwpTransportEnv* env, const jdwpPacket *packet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    jint len, data_len, id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * room for header and up to MAX_DATA_SIZE data bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    char header[HEADER_SIZE + MAX_DATA_SIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    jbyte *data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    /* packet can't be null */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    if (packet == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, "packet is NULL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    len = packet->type.cmd.len;         /* includes header */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    data_len = len - HEADER_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    /* bad packet */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    if (data_len < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, "invalid length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    /* prepare the header for transmission */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    len = (jint)dbgsysHostToNetworkLong(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    id = (jint)dbgsysHostToNetworkLong(packet->type.cmd.id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    memcpy(header + 0, &len, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    memcpy(header + 4, &id, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    header[8] = packet->type.cmd.flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    if (packet->type.cmd.flags & JDWPTRANSPORT_FLAGS_REPLY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        jshort errorCode =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            dbgsysHostToNetworkShort(packet->type.reply.errorCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        memcpy(header + 9, &errorCode, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        header[9] = packet->type.cmd.cmdSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        header[10] = packet->type.cmd.cmd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    data = packet->type.cmd.data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    /* Do one send for short packets, two for longer ones */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    if (data_len <= MAX_DATA_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        memcpy(header + HEADER_SIZE, data, data_len);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   638
        if (send_fully(socketFD, (char *)&header, HEADER_SIZE + data_len) !=
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            HEADER_SIZE + data_len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            RETURN_IO_ERROR("send failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        memcpy(header + HEADER_SIZE, data, MAX_DATA_SIZE);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   644
        if (send_fully(socketFD, (char *)&header, HEADER_SIZE + MAX_DATA_SIZE) !=
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            HEADER_SIZE + MAX_DATA_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            RETURN_IO_ERROR("send failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        /* Send the remaining data bytes right out of the data area. */
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   649
        if (send_fully(socketFD, (char *)data + MAX_DATA_SIZE,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   650
                       data_len - MAX_DATA_SIZE) != data_len - MAX_DATA_SIZE) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            RETURN_IO_ERROR("send failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
static jint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
recv_fully(int f, char *buf, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    int nbytes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    while (nbytes < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        int res = dbgsysRecv(f, buf + nbytes, len - nbytes, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        if (res < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        } else if (res == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            break; /* eof, return nbytes which is less than len */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        nbytes += res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    return nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   674
jint
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   675
send_fully(int f, char *buf, int len)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   676
{
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   677
    int nbytes = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   678
    while (nbytes < len) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   679
        int res = dbgsysSend(f, buf + nbytes, len - nbytes, 0);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   680
        if (res < 0) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   681
            return res;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   682
        } else if (res == 0) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   683
            break; /* eof, return nbytes which is less than len */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   684
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   685
        nbytes += res;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   686
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   687
    return nbytes;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   688
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   689
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
socketTransport_readPacket(jdwpTransportEnv* env, jdwpPacket* packet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    jint length, data_len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    jint n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    /* packet can't be null */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    if (packet == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, "packet is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    /* read the length field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    n = recv_fully(socketFD, (char *)&length, sizeof(jint));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    /* check for EOF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    if (n == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        packet->type.cmd.len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    if (n != sizeof(jint)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        RETURN_RECV_ERROR(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    length = (jint)dbgsysNetworkToHostLong(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    packet->type.cmd.len = length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    n = recv_fully(socketFD,(char *)&(packet->type.cmd.id),sizeof(jint));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    if (n < (int)sizeof(jint)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        RETURN_RECV_ERROR(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    packet->type.cmd.id = (jint)dbgsysNetworkToHostLong(packet->type.cmd.id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    n = recv_fully(socketFD,(char *)&(packet->type.cmd.flags),sizeof(jbyte));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    if (n < (int)sizeof(jbyte)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        RETURN_RECV_ERROR(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    if (packet->type.cmd.flags & JDWPTRANSPORT_FLAGS_REPLY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        n = recv_fully(socketFD,(char *)&(packet->type.reply.errorCode),sizeof(jbyte));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        if (n < (int)sizeof(jshort)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            RETURN_RECV_ERROR(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        /* FIXME - should the error be converted to host order?? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        n = recv_fully(socketFD,(char *)&(packet->type.cmd.cmdSet),sizeof(jbyte));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        if (n < (int)sizeof(jbyte)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            RETURN_RECV_ERROR(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        n = recv_fully(socketFD,(char *)&(packet->type.cmd.cmd),sizeof(jbyte));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        if (n < (int)sizeof(jbyte)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            RETURN_RECV_ERROR(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    data_len = length - ((sizeof(jint) * 2) + (sizeof(jbyte) * 3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    if (data_len < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        setLastError(0, "Badly formed packet received - invalid length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        return JDWPTRANSPORT_ERROR_IO_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    } else if (data_len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        packet->type.cmd.data = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        packet->type.cmd.data= (*callback->alloc)(data_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        if (packet->type.cmd.data == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            RETURN_ERROR(JDWPTRANSPORT_ERROR_OUT_OF_MEMORY, "out of memory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        n = recv_fully(socketFD,(char *)packet->type.cmd.data, data_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        if (n < data_len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            (*callback->free)(packet->type.cmd.data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            RETURN_RECV_ERROR(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
socketTransport_getLastError(jdwpTransportEnv* env, char** msgP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    char *msg = (char *)dbgsysTlsGet(tlsIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    if (msg == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        return JDWPTRANSPORT_ERROR_MSG_NOT_AVAILABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    }
896
5c02031316bf 6725543: Compiler warnings in serviceability native code
ohair
parents: 2
diff changeset
   779
    *msgP = (*callback->alloc)((int)strlen(msg)+1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    if (*msgP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        return JDWPTRANSPORT_ERROR_OUT_OF_MEMORY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    strcpy(*msgP, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
jdwpTransport_OnLoad(JavaVM *vm, jdwpTransportCallback* cbTablePtr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                     jint version, jdwpTransportEnv** result)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    if (version != JDWPTRANSPORT_VERSION_1_0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        return JNI_EVERSION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    if (initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
         * This library doesn't support multiple environments (yet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        return JNI_EEXIST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
    initialized = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
    jvm = vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    callback = cbTablePtr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    /* initialize interface table */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
    interface.GetCapabilities = &socketTransport_getCapabilities;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    interface.Attach = &socketTransport_attach;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    interface.StartListening = &socketTransport_startListening;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    interface.StopListening = &socketTransport_stopListening;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    interface.Accept = &socketTransport_accept;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
    interface.IsOpen = &socketTransport_isOpen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    interface.Close = &socketTransport_close;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    interface.ReadPacket = &socketTransport_readPacket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    interface.WritePacket = &socketTransport_writePacket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    interface.GetLastError = &socketTransport_getLastError;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    *result = &single_env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    /* initialized TLS */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    tlsIndex = dbgsysTlsAlloc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
    return JNI_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
}