src/jdk.jdwp.agent/share/native/libdt_socket/socketTransport.c
author amenkov
Thu, 31 Oct 2019 14:23:06 -0700
changeset 58876 1a8d65e71a66
parent 58433 a7c95e2f8814
permissions -rw-r--r--
8224159: JDWP IPv6 scope support Reviewed-by: sspitsyn, cjplummer
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
     2
 * Copyright (c) 1998, 2019, 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
49440
396ea30afbd5 8200178: Remove mapfiles for JDK native libraries
ihse
parents: 48978
diff changeset
    31
#include "jni.h"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include "jdwpTransport.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include "sysSocket.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
24503
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
    35
#ifdef _WIN32
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
    36
 #include <winsock2.h>
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
    37
 #include <ws2tcpip.h>
58876
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
    38
 #include <iphlpapi.h>
47121
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
    39
#else
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
    40
 #include <arpa/inet.h>
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
    41
 #include <sys/socket.h>
58876
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
    42
 #include <net/if.h>
24503
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
    43
#endif
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
    44
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * The Socket Transport Library.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * This module is an implementation of the Java Debug Wire Protocol Transport
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Service Provider Interface - see src/share/javavm/export/jdwpTransport.h.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
    52
static int serverSocketFD = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
static int socketFD = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
static jdwpTransportCallback *callback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
static JavaVM *jvm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
static int tlsIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
static jboolean initialized;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
static struct jdwpTransportNativeInterface_ interface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
static jdwpTransportEnv single_env = (jdwpTransportEnv)&interface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
#define RETURN_ERROR(err, msg) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        if (1==1) { \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            setLastError(err, msg); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            return err; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
#define RETURN_IO_ERROR(msg)    RETURN_ERROR(JDWPTRANSPORT_ERROR_IO_ERROR, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
#define RETURN_RECV_ERROR(n) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        if (n == 0) { \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            RETURN_ERROR(JDWPTRANSPORT_ERROR_IO_ERROR, "premature EOF"); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        } else { \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            RETURN_IO_ERROR("recv error"); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
#define MAX_DATA_SIZE 1000
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    78
static jint recv_fully(int, char *, int);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    79
static jint send_fully(int, char *, int);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    80
47121
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
    81
/* version >= JDWPTRANSPORT_VERSION_1_1 */
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
    82
typedef struct {
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
    83
    /* subnet and mask are stored as IPv6 addresses, IPv4 is stored as mapped IPv6 */
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
    84
    struct in6_addr subnet;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
    85
    struct in6_addr netmask;
47121
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
    86
} AllowedPeerInfo;
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
    87
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
    88
#define STR(x) #x
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
    89
#define MAX_PEER_ENTRIES 32
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
    90
#define MAX_PEERS_STR STR(MAX_PEER_ENTRIES)
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
    91
static AllowedPeerInfo _peers[MAX_PEER_ENTRIES];
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
    92
static int _peers_cnt = 0;
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
    93
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
    94
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
    95
static int allowOnlyIPv4 = 0;                  // reflects "java.net.preferIPv4Stack" sys. property
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
    96
static int preferredAddressFamily = AF_INET;   // "java.net.preferIPv6Addresses"
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
    97
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * Record the last error for this thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
setLastError(jdwpTransportError err, char *newmsg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    char buf[255];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    char *msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /* get any I/O first in case any system calls override errno */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    if (err == JDWPTRANSPORT_ERROR_IO_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        dbgsysGetLastIOError(buf, sizeof(buf));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    msg = (char *)dbgsysTlsGet(tlsIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    if (msg != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        (*callback->free)(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    if (err == JDWPTRANSPORT_ERROR_IO_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        char *join_str = ": ";
896
5c02031316bf 6725543: Compiler warnings in serviceability native code
ohair
parents: 2
diff changeset
   118
        int msg_len = (int)strlen(newmsg) + (int)strlen(join_str) +
5c02031316bf 6725543: Compiler warnings in serviceability native code
ohair
parents: 2
diff changeset
   119
                      (int)strlen(buf) + 3;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        msg = (*callback->alloc)(msg_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (msg != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            strcpy(msg, newmsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            strcat(msg, join_str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            strcat(msg, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    } else {
896
5c02031316bf 6725543: Compiler warnings in serviceability native code
ohair
parents: 2
diff changeset
   127
        msg = (*callback->alloc)((int)strlen(newmsg)+1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (msg != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            strcpy(msg, newmsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    dbgsysTlsPut(tlsIndex, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * Return the last error for this thread (may be NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
static char*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
getLastError() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    return (char *)dbgsysTlsGet(tlsIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
46091
a6f2dc91ba56 8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents: 36134
diff changeset
   144
/* Set options common to client and server sides */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
static jdwpTransportError
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   146
setOptionsCommon(int domain, int fd)
46091
a6f2dc91ba56 8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents: 36134
diff changeset
   147
{
a6f2dc91ba56 8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents: 36134
diff changeset
   148
    jvalue dontcare;
a6f2dc91ba56 8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents: 36134
diff changeset
   149
    int err;
a6f2dc91ba56 8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents: 36134
diff changeset
   150
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   151
    if (domain == AF_INET6) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   152
        int off = 0;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   153
        // make the socket a dual mode socket
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   154
        // this may fail if IPv4 is not supported - it's ok
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   155
        setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&off, sizeof(off));
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   156
    }
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   157
46091
a6f2dc91ba56 8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents: 36134
diff changeset
   158
    dontcare.i = 0;  /* keep compiler happy */
a6f2dc91ba56 8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents: 36134
diff changeset
   159
    err = dbgsysSetSocketOption(fd, TCP_NODELAY, JNI_TRUE, dontcare);
a6f2dc91ba56 8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents: 36134
diff changeset
   160
    if (err < 0) {
a6f2dc91ba56 8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents: 36134
diff changeset
   161
        RETURN_IO_ERROR("setsockopt TCPNODELAY failed");
a6f2dc91ba56 8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents: 36134
diff changeset
   162
    }
a6f2dc91ba56 8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents: 36134
diff changeset
   163
a6f2dc91ba56 8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents: 36134
diff changeset
   164
    return JDWPTRANSPORT_ERROR_NONE;
a6f2dc91ba56 8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents: 36134
diff changeset
   165
}
a6f2dc91ba56 8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents: 36134
diff changeset
   166
a6f2dc91ba56 8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents: 36134
diff changeset
   167
/* Set the SO_REUSEADDR option */
a6f2dc91ba56 8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents: 36134
diff changeset
   168
static jdwpTransportError
a6f2dc91ba56 8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents: 36134
diff changeset
   169
setReuseAddrOption(int fd)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    jvalue dontcare;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    int err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    dontcare.i = 0;  /* keep compiler happy */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    err = dbgsysSetSocketOption(fd, SO_REUSEADDR, JNI_TRUE, dontcare);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    if (err < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        RETURN_IO_ERROR("setsockopt SO_REUSEADDR failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
static jdwpTransportError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
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
   186
    const char *hello = "JDWP-Handshake";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    char b[16];
3723
a27278866f80 6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents: 1247
diff changeset
   188
    int rv, helloLen, received;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    if (timeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        dbgsysConfigureBlocking(fd, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
3723
a27278866f80 6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents: 1247
diff changeset
   193
    helloLen = (int)strlen(hello);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    received = 0;
3723
a27278866f80 6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents: 1247
diff changeset
   195
    while (received < helloLen) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        char *buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (timeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            rv = dbgsysPoll(fd, JNI_TRUE, JNI_FALSE, (long)timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            if (rv <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                setLastError(0, "timeout during handshake");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                return JDWPTRANSPORT_ERROR_IO_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        buf = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        buf += received;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   207
        n = recv_fully(fd, buf, helloLen-received);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        if (n == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            setLastError(0, "handshake failed - connection prematurally closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            return JDWPTRANSPORT_ERROR_IO_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        if (n < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            RETURN_IO_ERROR("recv failed during handshake");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        received += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    if (timeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        dbgsysConfigureBlocking(fd, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
3723
a27278866f80 6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents: 1247
diff changeset
   220
    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
   221
        char msg[80+2*16];
a27278866f80 6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents: 1247
diff changeset
   222
        b[received] = '\0';
a27278866f80 6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents: 1247
diff changeset
   223
        /*
a27278866f80 6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents: 1247
diff changeset
   224
         * 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
   225
         * 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
   226
         */
a27278866f80 6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents: 1247
diff changeset
   227
        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
   228
        setLastError(0, msg);
a27278866f80 6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents: 1247
diff changeset
   229
        return JDWPTRANSPORT_ERROR_IO_ERROR;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   232
    if (send_fully(fd, (char*)hello, helloLen) != helloLen) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        RETURN_IO_ERROR("send failed during handshake");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
24503
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   238
static int
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   239
getPortNumber(const char *s_port) {
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   240
    u_long n;
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   241
    char *eptr;
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   242
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   243
    if (*s_port == 0) {
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   244
        // 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
   245
        return -1;
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   246
    }
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   247
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   248
    n = strtoul(s_port, &eptr, 10);
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   249
    if (eptr != s_port + strlen(s_port)) {
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   250
        // incomplete conversion - port number contains non-digit
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   251
        return -1;
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   252
    }
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   253
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   254
    if (n > (u_short) -1) {
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   255
        // 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
   256
        // maximum possible u_short value (65535) and
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   257
        // will not be truncated later.
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   258
        return -1;
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   259
    }
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   260
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   261
    return n;
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   262
}
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   263
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   264
static unsigned short getPort(struct sockaddr *sa)
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   265
{
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   266
    return dbgsysNetworkToHostShort(sa->sa_family == AF_INET
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   267
                                    ? (((struct sockaddr_in*)sa)->sin_port)
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   268
                                    : (((struct sockaddr_in6*)sa)->sin6_port));
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   269
}
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   270
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   271
/*
58876
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   272
 * Parses scope id.
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   273
 * Scope id is ulong on Windows, uint32 on unix, so returns long which can be cast to uint32.
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   274
 * On error sets last error and returns -1.
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   275
 */
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   276
static long parseScopeId(const char *str) {
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   277
    // try to handle scope as interface name
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   278
    unsigned long scopeId = if_nametoindex(str);
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   279
    if (scopeId == 0) {
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   280
        // try to parse integer value
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   281
        char *end;
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   282
        scopeId = strtoul(str, &end, 10);
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   283
        if (*end != '\0') {
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   284
            setLastError(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, "failed to parse scope");
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   285
            return -1;
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   286
        }
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   287
    }
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   288
    // ensure parsed value is in uint32 range
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   289
    if (scopeId > 0xFFFFFFFF) {
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   290
        setLastError(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, "scope is out of range");
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   291
        return -1;
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   292
    }
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   293
    return (long)scopeId;
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   294
}
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   295
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   296
/*
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   297
 * Wrapper for dbgsysGetAddrInfo (getaddrinfo).
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   298
 * Handles enclosing square brackets and scopes.
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   299
 */
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   300
static jdwpTransportError
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   301
getAddrInfo(const char *hostname, size_t hostnameLen,
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   302
            const char *service,
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   303
            const struct addrinfo *hints,
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   304
            struct addrinfo **result)
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   305
{
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   306
    int err = 0;
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   307
    char *buffer = NULL;
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   308
    long scopeId = 0;
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   309
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   310
    if (hostname != NULL) {
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   311
        char *scope = NULL;
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   312
        // skip surrounding
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   313
        if (hostnameLen > 2 && hostname[0] == '[' && hostname[hostnameLen - 1] == ']') {
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   314
            hostname++;
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   315
            hostnameLen -= 2;
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   316
        }
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   317
        buffer = (*callback->alloc)((int)hostnameLen + 1);
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   318
        if (buffer == NULL) {
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   319
            RETURN_ERROR(JDWPTRANSPORT_ERROR_OUT_OF_MEMORY, "out of memory");
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   320
        }
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   321
        memcpy(buffer, hostname, hostnameLen);
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   322
        buffer[hostnameLen] = '\0';
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   323
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   324
        scope = strchr(buffer, '%');
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   325
        if (scope != NULL) {
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   326
            // drop scope from the address
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   327
            *scope = '\0';
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   328
            // and parse the value
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   329
            scopeId = parseScopeId(scope + 1);
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   330
            if (scopeId < 0) {
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   331
                (*callback->free)(buffer);
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   332
                return JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT;
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   333
            }
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   334
        }
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   335
    }
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   336
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   337
    err = dbgsysGetAddrInfo(buffer, service, hints, result);
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   338
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   339
    if (buffer != NULL) {
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   340
        (*callback->free)(buffer);
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   341
    }
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   342
    if (err != 0) {
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   343
        setLastError(err, "getaddrinfo: failed to parse address");
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   344
        return JDWPTRANSPORT_ERROR_IO_ERROR;
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   345
    }
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   346
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   347
    if (scopeId > 0) {
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   348
        if ((*result)->ai_family != AF_INET6) {
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   349
            RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, "IPv4 address cannot contain scope");
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   350
        }
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   351
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   352
        ((struct sockaddr_in6 *)((*result)->ai_addr))->sin6_scope_id = (uint32_t)scopeId;
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   353
    }
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   354
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   355
    return JDWPTRANSPORT_ERROR_NONE;
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   356
}
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   357
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   358
/*
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   359
 * Result must be released with dbgsysFreeAddrInfo.
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   360
 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
static jdwpTransportError
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   362
parseAddress(const char *address, struct addrinfo **result) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   363
    const char *colon;
58876
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   364
    size_t hostnameLen;
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   365
    const char *port;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   366
    struct addrinfo hints;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   368
    *result = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    /* check for host:port or port */
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   371
    colon = strrchr(address, ':');
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   372
    port = (colon == NULL ? address : colon + 1);
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   373
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   374
    /* ensure the port is valid (getaddrinfo allows port to be empty) */
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   375
    if (getPortNumber(port) < 0) {
24503
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   376
        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
   377
    }
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   378
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   379
    memset (&hints, 0, sizeof(hints));
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   380
    hints.ai_family = allowOnlyIPv4 ? AF_INET : AF_UNSPEC;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   381
    hints.ai_socktype = SOCK_STREAM;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   382
    hints.ai_protocol = IPPROTO_TCP;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   383
    hints.ai_flags = AI_NUMERICSERV;    // port must be a number
24503
fe24408289d7 8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents: 23010
diff changeset
   384
58876
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   385
    hostnameLen = (colon == NULL ? 0 : colon - address);
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   386
    if (hostnameLen == 0) {
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   387
        /* no hostname - use localhost address (pass NULL to getaddrinfo) */
58876
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   388
        address = NULL;
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   389
    } else  if (*address == '*' && hostnameLen == 1) {
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   390
        /* *:port - listen on all interfaces
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   391
         * use IPv6 socket (to accept IPv6 and mapped IPv4),
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   392
         * pass hostname == NULL to getaddrinfo.
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   393
         */
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   394
        hints.ai_family = allowOnlyIPv4 ? AF_INET : AF_INET6;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   395
        hints.ai_flags |= AI_PASSIVE | (allowOnlyIPv4 ? 0 : AI_V4MAPPED | AI_ALL);
58876
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   396
        address = NULL;
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   397
    }
48767
0c6ce8fdb50a 8080990: libdt_socket/socket_md.c(202) : warning C4996: 'gethostbyname': Use getaddrinfo() or GetAddrInfoW()
gadams
parents: 48242
diff changeset
   398
58876
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   399
    return getAddrInfo(address, hostnameLen, port, &hints, result);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   402
/*
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   403
 * Input is sockaddr just because all clients have it.
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   404
 */
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   405
static void convertIPv4ToIPv6(const struct sockaddr *addr4, struct in6_addr *addr6) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   406
    // Implement in a platform-independent way.
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   407
    // Spec requires in_addr has s_addr member, in6_addr has s6_addr[16] member.
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   408
    struct in_addr *a4 = &(((struct sockaddr_in*)addr4)->sin_addr);
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   409
    memset(addr6, 0, sizeof(*addr6));   // for safety
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   410
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   411
    // Mapped address contains 80 zero bits, then 16 "1" bits, then IPv4 address (4 bytes).
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   412
    addr6->s6_addr[10] = addr6->s6_addr[11] = 0xFF;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   413
    memcpy(&(addr6->s6_addr[12]), &(a4->s_addr), 4);
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   414
}
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   415
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   416
/*
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   417
 * Parses address (IPv4 or IPv6), fills in result by parsed address.
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   418
 * For IPv4 mapped IPv6 is returned in result, isIPv4 is set.
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   419
 */
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   420
static jdwpTransportError
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   421
parseAllowedAddr(const char *buffer, struct in6_addr *result, int *isIPv4) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   422
    struct addrinfo hints;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   423
    struct addrinfo *addrInfo = NULL;
58876
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   424
    jdwpTransportError err;
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   425
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   426
    /*
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   427
     * To parse both IPv4 and IPv6 need to specify AF_UNSPEC family
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   428
     * (with AF_INET6 IPv4 addresses are not parsed even with AI_V4MAPPED and AI_ALL flags).
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   429
     */
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   430
    memset (&hints, 0, sizeof(hints));
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   431
    hints.ai_family = AF_UNSPEC;            // IPv6 or mapped IPv4
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   432
    hints.ai_socktype = SOCK_STREAM;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   433
    hints.ai_protocol = IPPROTO_TCP;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   434
    hints.ai_flags = AI_NUMERICHOST;        // only numeric addresses, no resolution
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   435
58876
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   436
    err = getAddrInfo(buffer, strlen(buffer), NULL, &hints, &addrInfo);
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   437
58876
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   438
    if (err != JDWPTRANSPORT_ERROR_NONE) {
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   439
        return err;
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   440
    }
47121
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   441
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   442
    if (addrInfo->ai_family == AF_INET6) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   443
        memcpy(result, &(((struct sockaddr_in6 *)(addrInfo->ai_addr))->sin6_addr), sizeof(*result));
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   444
        *isIPv4 = 0;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   445
    } else {    // IPv4 address - convert to mapped IPv6
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   446
        struct in6_addr addr6;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   447
        convertIPv4ToIPv6(addrInfo->ai_addr, &addr6);
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   448
        memcpy(result, &addr6, sizeof(*result));
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   449
        *isIPv4 = 1;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   450
    }
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   451
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   452
    dbgsysFreeAddrInfo(addrInfo);
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   453
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   454
    return JDWPTRANSPORT_ERROR_NONE;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   455
}
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   456
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   457
/*
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   458
 * Parses prefix length from buffer (integer value), fills in result with corresponding net mask.
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   459
 * For IPv4 (isIPv4 is set), maximum prefix length is 32 bit, for IPv6 - 128 bit.
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   460
 */
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   461
static jdwpTransportError
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   462
parseAllowedMask(const char *buffer, int isIPv4, struct in6_addr *result) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   463
    int prefixLen = 0;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   464
    int maxValue = isIPv4 ? 32 : 128;
54925
3205f4c40716 8224028: loop initial declarations introduced by JDK-8184770
aoqi
parents: 54884
diff changeset
   465
    int i;
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   466
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   467
    do {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   468
        if (*buffer < '0' || *buffer > '9') {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   469
            return JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT;
47121
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   470
        }
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   471
        prefixLen = prefixLen * 10 + (*buffer - '0');
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   472
        if (prefixLen > maxValue) {  // avoid overflow
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   473
            return JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT;
47121
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   474
        }
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   475
        buffer++;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   476
    } while (*buffer != '\0');
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   477
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   478
    if (isIPv4) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   479
        // IPv4 are stored as mapped IPv6, prefixLen needs to be converted too
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   480
        prefixLen += 96;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   481
    }
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   482
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   483
    if (prefixLen == 0) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   484
        return JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT;
47121
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   485
    }
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   486
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   487
    // generate mask for prefix length
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   488
    memset(result, 0, sizeof(*result));
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   489
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   490
    // prefixLen <= 128, so we won't go over result's size
54925
3205f4c40716 8224028: loop initial declarations introduced by JDK-8184770
aoqi
parents: 54884
diff changeset
   491
    for (i = 0; prefixLen > 0; i++, prefixLen -= 8) {
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   492
        if (prefixLen >= 8) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   493
            // set the whole byte
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   494
            result->s6_addr[i] = 0xFF;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   495
        } else {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   496
            // set only "prefixLen" bits
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   497
            result->s6_addr[i] = (char)(0xFF << (8 - prefixLen));
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   498
        }
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   499
    }
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   500
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   501
    return JDWPTRANSPORT_ERROR_NONE;
47121
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   502
}
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   503
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   504
/*
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   505
 * Internal implementation of parseAllowedPeers (requires writable buffer).
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   506
 */
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   507
static jdwpTransportError
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   508
parseAllowedPeersInternal(char *buffer) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   509
    char *next;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   510
    int isIPv4 = 0;
47121
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   511
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   512
    do {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   513
        char *mask = NULL;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   514
        char *endOfAddr = strpbrk(buffer, "/+");
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   515
        if (endOfAddr == NULL) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   516
            // this is the last address and there is no prefix length
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   517
            next = NULL;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   518
        } else {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   519
            next = endOfAddr + 1;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   520
            if (*endOfAddr == '/') {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   521
                // mask (prefix length) presents
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   522
                char *endOfMask = strchr(next, '+');
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   523
                mask = next;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   524
                if (endOfMask == NULL) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   525
                    // no more addresses
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   526
                    next = NULL;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   527
                } else {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   528
                    next = endOfMask + 1;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   529
                    *endOfMask = '\0';
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   530
                }
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   531
            }
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   532
            *endOfAddr = '\0';
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   533
        }
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   534
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   535
        // parse subnet address (IPv4 is stored as mapped IPv6)
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   536
        if (parseAllowedAddr(buffer, &(_peers[_peers_cnt].subnet), &isIPv4) != JDWPTRANSPORT_ERROR_NONE) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   537
            _peers_cnt = 0;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   538
            fprintf(stderr, "Error in allow option: '%s'\n", buffer);
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   539
            RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT,
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   540
                         "invalid IP address in allow option");
47121
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   541
        }
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   542
        if (mask != NULL) {
54925
3205f4c40716 8224028: loop initial declarations introduced by JDK-8184770
aoqi
parents: 54884
diff changeset
   543
            size_t i;
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   544
            if (parseAllowedMask(mask, isIPv4, &(_peers[_peers_cnt].netmask)) != JDWPTRANSPORT_ERROR_NONE) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   545
                _peers_cnt = 0;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   546
                fprintf(stderr, "Error in allow option: '%s'\n", mask);
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   547
                RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT,
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   548
                             "invalid netmask in allow option");
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   549
            }
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   550
            // for safety update subnet to satisfy the mask
54925
3205f4c40716 8224028: loop initial declarations introduced by JDK-8184770
aoqi
parents: 54884
diff changeset
   551
            for (i = 0; i < sizeof(_peers[_peers_cnt].subnet); i++) {
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   552
                _peers[_peers_cnt].subnet.s6_addr[i] &= _peers[_peers_cnt].netmask.s6_addr[i];
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   553
            }
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   554
        } else {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   555
            memset(&(_peers[_peers_cnt].netmask), 0xFF, sizeof(_peers[_peers_cnt].netmask));
47121
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   556
        }
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   557
        _peers_cnt++;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   558
        buffer = next;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   559
    } while (next != NULL);
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   560
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   561
    return JDWPTRANSPORT_ERROR_NONE;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   562
}
47121
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   563
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   564
/*
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   565
 * Parses 'allow' argument (fills in list of allowed peers (global _peers variable)).
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   566
 * 'Allow' value consists of tokens separated by '+',
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   567
 * each token contains IP address (IPv4 or IPv6) and optional prefixLength:
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   568
 * '<addr>[/<prefixLength>]'.
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   569
 * Example: '192.168.1.10+192.168.0.0/24'
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   570
 *   - connections are allowed from 192.168.1.10 and subnet 192.168.0.XX.
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   571
 */
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   572
static jdwpTransportError
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   573
parseAllowedPeers(const char *allowed_peers, size_t len) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   574
    // Build a list of allowed peers from char string
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   575
    // of format 192.168.0.10+192.168.0.0/24
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   576
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   577
    // writable copy of the value
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   578
    char *buffer = (*callback->alloc)((int)len + 1);
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   579
    if (buffer == NULL) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   580
        RETURN_ERROR(JDWPTRANSPORT_ERROR_OUT_OF_MEMORY, "out of memory");
47121
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   581
    }
58433
a7c95e2f8814 8231600: Avoid GCC 8 strncpy error in jdk.jdwp.agent
fweimer
parents: 54925
diff changeset
   582
    memcpy(buffer, allowed_peers, len);
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   583
    buffer[len] = '\0';
47121
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   584
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   585
    jdwpTransportError err = parseAllowedPeersInternal(buffer);
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   586
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   587
    (*callback->free)(buffer);
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   588
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   589
    return err;
47121
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   590
}
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   591
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   592
static int
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   593
isAddressInSubnet(const struct in6_addr *address, const struct in6_addr *subnet, const struct in6_addr *mask) {
54925
3205f4c40716 8224028: loop initial declarations introduced by JDK-8184770
aoqi
parents: 54884
diff changeset
   594
    size_t i;
3205f4c40716 8224028: loop initial declarations introduced by JDK-8184770
aoqi
parents: 54884
diff changeset
   595
    for (i = 0; i < sizeof(struct in6_addr); i++) {
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   596
        if ((address->s6_addr[i] & mask->s6_addr[i]) != subnet->s6_addr[i]) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   597
            return 0;
47121
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   598
        }
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   599
    }
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   600
    return 1;
47121
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   601
}
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   602
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   603
static int
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   604
isPeerAllowed(struct sockaddr_storage *peer) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   605
    struct in6_addr tmp;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   606
    struct in6_addr *addr6;
54925
3205f4c40716 8224028: loop initial declarations introduced by JDK-8184770
aoqi
parents: 54884
diff changeset
   607
    int i;
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   608
    // _peers contains IPv6 subnet and mask (IPv4 is converted to mapped IPv6)
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   609
    if (peer->ss_family == AF_INET) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   610
        convertIPv4ToIPv6((struct sockaddr *)peer, &tmp);
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   611
        addr6 = &tmp;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   612
    } else {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   613
        addr6 = &(((struct sockaddr_in6 *)peer)->sin6_addr);
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   614
    }
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   615
54925
3205f4c40716 8224028: loop initial declarations introduced by JDK-8184770
aoqi
parents: 54884
diff changeset
   616
    for (i = 0; i < _peers_cnt; ++i) {
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   617
        if (isAddressInSubnet(addr6, &(_peers[i].subnet), &(_peers[i].netmask))) {
47121
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   618
            return 1;
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   619
        }
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   620
    }
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   621
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   622
    return 0;
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   623
}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
socketTransport_getCapabilities(jdwpTransportEnv* env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        JDWPTransportCapabilities* capabilitiesPtr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    JDWPTransportCapabilities result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    memset(&result, 0, sizeof(result));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    result.can_timeout_attach = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    result.can_timeout_accept = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    result.can_timeout_handshake = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    *capabilitiesPtr = result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   641
/*
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   642
 * Starts listening on the specified addrinfo,
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   643
 * returns listening socket and actual listening port.
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   644
 * If the function fails and returned socket != -1, the socket should be closed.
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   645
 */
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   646
static jdwpTransportError startListening(struct addrinfo *ai, int *socket, char** actualAddress)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    int err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   650
    *socket = dbgsysSocket(ai->ai_family, SOCK_STREAM, IPPROTO_TCP);
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   651
    if (*socket < 0) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   652
        RETURN_IO_ERROR("socket creation failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   655
    err = setOptionsCommon(ai->ai_family, *socket);
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   656
    if (err) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        return err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   660
    if (getPort(ai->ai_addr) != 0) {
46091
a6f2dc91ba56 8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents: 36134
diff changeset
   661
        /*
a6f2dc91ba56 8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents: 36134
diff changeset
   662
         * Only need SO_REUSEADDR if we're using a fixed port. If we
a6f2dc91ba56 8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents: 36134
diff changeset
   663
         * start seeing EADDRINUSE due to collisions in free ports
a6f2dc91ba56 8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents: 36134
diff changeset
   664
         * then we should retry the dbgsysBind() a few times.
a6f2dc91ba56 8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents: 36134
diff changeset
   665
         */
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   666
        err = setReuseAddrOption(*socket);
46091
a6f2dc91ba56 8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents: 36134
diff changeset
   667
        if (err) {
a6f2dc91ba56 8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents: 36134
diff changeset
   668
            return err;
a6f2dc91ba56 8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents: 36134
diff changeset
   669
        }
a6f2dc91ba56 8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents: 36134
diff changeset
   670
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   672
    err = dbgsysBind(*socket, ai->ai_addr, (socklen_t)ai->ai_addrlen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    if (err < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        RETURN_IO_ERROR("bind failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   677
    err = dbgsysListen(*socket, 1); // only 1 debugger can attach
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    if (err < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        RETURN_IO_ERROR("listen failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        char buf[20];
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   684
        struct sockaddr_storage addr;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   685
        socklen_t len = sizeof(addr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        jint portNum;
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   687
        err = dbgsysGetSocketName(*socket, (struct sockaddr *)&addr, &len);
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   688
        if (err != 0) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   689
            RETURN_IO_ERROR("getsockname failed");
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   690
        }
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   691
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   692
        portNum = getPort((struct sockaddr *)&addr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        sprintf(buf, "%d", portNum);
896
5c02031316bf 6725543: Compiler warnings in serviceability native code
ohair
parents: 2
diff changeset
   694
        *actualAddress = (*callback->alloc)((int)strlen(buf) + 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        if (*actualAddress == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            RETURN_ERROR(JDWPTRANSPORT_ERROR_OUT_OF_MEMORY, "out of memory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            strcpy(*actualAddress, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
static jdwpTransportError JNICALL
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   706
socketTransport_startListening(jdwpTransportEnv* env, const char* address,
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   707
                               char** actualAddress)
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   708
{
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   709
    int err;
54925
3205f4c40716 8224028: loop initial declarations introduced by JDK-8184770
aoqi
parents: 54884
diff changeset
   710
    int pass;
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   711
    struct addrinfo *addrInfo = NULL;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   712
    struct addrinfo *listenAddr = NULL;
54925
3205f4c40716 8224028: loop initial declarations introduced by JDK-8184770
aoqi
parents: 54884
diff changeset
   713
    struct addrinfo *ai = NULL;
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   714
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   715
    /* no address provided */
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   716
    if ((address == NULL) || (address[0] == '\0')) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   717
        address = "0";
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   718
    }
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   719
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   720
    err = parseAddress(address, &addrInfo);
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   721
    if (err != JDWPTRANSPORT_ERROR_NONE) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   722
        return err;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   723
    }
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   724
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   725
    /* 1st pass - preferredAddressFamily (by default IPv4), 2nd pass - the rest */
54925
3205f4c40716 8224028: loop initial declarations introduced by JDK-8184770
aoqi
parents: 54884
diff changeset
   726
    for (pass = 0; pass < 2 && listenAddr == NULL; pass++) {
3205f4c40716 8224028: loop initial declarations introduced by JDK-8184770
aoqi
parents: 54884
diff changeset
   727
        for (ai = addrInfo; ai != NULL; ai = ai->ai_next) {
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   728
            if ((pass == 0 && ai->ai_family == preferredAddressFamily) ||
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   729
                (pass == 1 && ai->ai_family != preferredAddressFamily))
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   730
            {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   731
                listenAddr = ai;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   732
                break;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   733
            }
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   734
        }
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   735
    }
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   736
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   737
    if (listenAddr == NULL) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   738
        dbgsysFreeAddrInfo(addrInfo);
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   739
        RETURN_ERROR(JDWPTRANSPORT_ERROR_INTERNAL, "listen failed: wrong address");
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   740
    }
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   741
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   742
    err = startListening(listenAddr, &serverSocketFD, actualAddress);
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   743
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   744
    dbgsysFreeAddrInfo(addrInfo);
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   745
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   746
    if (err != JDWPTRANSPORT_ERROR_NONE) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   747
        if (serverSocketFD >= 0) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   748
            dbgsysSocketClose(serverSocketFD);
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   749
            serverSocketFD = -1;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   750
        }
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   751
        return err;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   752
    }
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   753
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   754
    return JDWPTRANSPORT_ERROR_NONE;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   755
}
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   756
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   757
static jdwpTransportError JNICALL
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
socketTransport_accept(jdwpTransportEnv* env, jlong acceptTimeout, jlong handshakeTimeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
{
47121
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   760
    int err = JDWPTRANSPORT_ERROR_NONE;
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   761
    struct sockaddr_storage clientAddr;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   762
    socklen_t clientAddrLen;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   763
    jlong startTime = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * Use a default handshake timeout if not specified - this avoids an indefinite
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * hang in cases where something other than a debugger connects to our port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    if (handshakeTimeout == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        handshakeTimeout = 2000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
         * If there is an accept timeout then we put the socket in non-blocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
         * mode and poll for a connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        if (acceptTimeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            int rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            dbgsysConfigureBlocking(serverSocketFD, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            startTime = dbgsysCurrentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            rv = dbgsysPoll(serverSocketFD, JNI_TRUE, JNI_FALSE, (long)acceptTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            if (rv <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                /* set the last error here as could be overridden by configureBlocking */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                if (rv == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                    setLastError(JDWPTRANSPORT_ERROR_IO_ERROR, "poll failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                /* restore blocking state */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                dbgsysConfigureBlocking(serverSocketFD, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                if (rv == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                    RETURN_ERROR(JDWPTRANSPORT_ERROR_TIMEOUT, "timed out waiting for connection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                    return JDWPTRANSPORT_ERROR_IO_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
         * Accept the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
         */
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   801
        clientAddrLen = sizeof(clientAddr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        socketFD = dbgsysAccept(serverSocketFD,
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   803
                                (struct sockaddr *)&clientAddr,
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   804
                                &clientAddrLen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        /* set the last error here as could be overridden by configureBlocking */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        if (socketFD < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            setLastError(JDWPTRANSPORT_ERROR_IO_ERROR, "accept failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
         * Restore the blocking state - note that the accepted socket may be in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
         * blocking or non-blocking mode (platform dependent). However as there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
         * is a handshake timeout set then it will go into non-blocking mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
         * anyway for the handshake.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        if (acceptTimeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            dbgsysConfigureBlocking(serverSocketFD, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        if (socketFD < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            return JDWPTRANSPORT_ERROR_IO_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
47121
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   822
        /*
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   823
         * version >= JDWPTRANSPORT_VERSION_1_1:
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   824
         * Verify that peer is allowed to connect.
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   825
         */
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   826
        if (_peers_cnt > 0) {
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   827
            if (!isPeerAllowed(&clientAddr)) {
47121
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   828
                char ebuf[64] = { 0 };
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   829
                char addrStr[INET_ADDRSTRLEN] = { 0 };
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   830
                int err2 = getnameinfo((struct sockaddr *)&clientAddr, clientAddrLen,
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   831
                                       addrStr, sizeof(addrStr), NULL, 0,
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   832
                                       NI_NUMERICHOST);
47121
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   833
                sprintf(ebuf, "ERROR: Peer not allowed to connect: %s\n",
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   834
                        (err2 != 0) ? "<bad address>" : addrStr);
47121
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   835
                dbgsysSocketClose(socketFD);
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   836
                socketFD = -1;
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   837
                err = JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT;
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   838
                setLastError(err, ebuf);
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   839
            }
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   840
        }
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   841
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   842
        if (socketFD > 0) {
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   843
          /* handshake with the debugger */
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   844
          err = handshake(socketFD, handshakeTimeout);
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   845
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
         * If the handshake fails then close the connection. If there if an accept
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
         * timeout then we must adjust the timeout for the next poll.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
         */
47121
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
   851
        if (err != JDWPTRANSPORT_ERROR_NONE) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            fprintf(stderr, "Debugger failed to attach: %s\n", getLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            dbgsysSocketClose(socketFD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            socketFD = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            if (acceptTimeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                long endTime = dbgsysCurrentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                acceptTimeout -= (endTime - startTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                if (acceptTimeout <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                    setLastError(JDWPTRANSPORT_ERROR_IO_ERROR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                        "timeout waiting for debugger to connect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                    return JDWPTRANSPORT_ERROR_IO_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    } while (socketFD < 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
socketTransport_stopListening(jdwpTransportEnv *env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
    if (serverSocketFD < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_STATE, "connection not open");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
    if (dbgsysSocketClose(serverSocketFD) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        RETURN_IO_ERROR("close failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    serverSocketFD = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   883
/*
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   884
 * Tries to connect to the specified addrinfo, returns connected socket.
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   885
 * If the function fails and returned socket != -1, the socket should be closed.
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   886
 */
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   887
static jdwpTransportError connectToAddr(struct addrinfo *ai, jlong timeout, int *socket) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
    int err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   890
    *socket = dbgsysSocket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   891
    if (*socket < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        RETURN_IO_ERROR("unable to create socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   895
    err = setOptionsCommon(ai->ai_family, socketFD);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    if (err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        return err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
    /*
46091
a6f2dc91ba56 8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents: 36134
diff changeset
   901
     * We don't call setReuseAddrOption() for the non-server socket
a6f2dc91ba56 8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents: 36134
diff changeset
   902
     * case. If we start seeing EADDRINUSE due to collisions in free
a6f2dc91ba56 8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents: 36134
diff changeset
   903
     * ports then we should retry the dbgsysConnect() a few times.
a6f2dc91ba56 8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents: 36134
diff changeset
   904
     */
a6f2dc91ba56 8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents: 36134
diff changeset
   905
a6f2dc91ba56 8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents: 36134
diff changeset
   906
    /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * To do a timed connect we make the socket non-blocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * and poll with a timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     */
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   910
    if (timeout > 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        dbgsysConfigureBlocking(socketFD, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   914
    err = dbgsysConnect(socketFD, ai->ai_addr, (socklen_t)ai->ai_addrlen);
58876
1a8d65e71a66 8224159: JDWP IPv6 scope support
amenkov
parents: 58433
diff changeset
   915
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   916
    if (err == DBG_EINPROGRESS && timeout > 0) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   917
        err = dbgsysFinishConnect(socketFD, (long)timeout);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        if (err == DBG_ETIMEOUT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            dbgsysConfigureBlocking(socketFD, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            RETURN_ERROR(JDWPTRANSPORT_ERROR_TIMEOUT, "connect timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   925
    if (err) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        RETURN_IO_ERROR("connect failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   929
    return err;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   930
}
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   931
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   932
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   933
static jdwpTransportError JNICALL
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   934
socketTransport_attach(jdwpTransportEnv* env, const char* addressString, jlong attachTimeout,
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   935
                       jlong handshakeTimeout)
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   936
{
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   937
    int err;
54925
3205f4c40716 8224028: loop initial declarations introduced by JDK-8184770
aoqi
parents: 54884
diff changeset
   938
    int pass;
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   939
    struct addrinfo *addrInfo = NULL;
54925
3205f4c40716 8224028: loop initial declarations introduced by JDK-8184770
aoqi
parents: 54884
diff changeset
   940
    struct addrinfo *ai;
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   941
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   942
    if (addressString == NULL || addressString[0] == '\0') {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   943
        RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, "address is missing");
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   944
    }
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   945
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   946
    err = parseAddress(addressString, &addrInfo);
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   947
    if (err) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   948
        return err;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   949
    }
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   950
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   951
    /* 1st pass - preferredAddressFamily (by default IPv4), 2nd pass - the rest */
54925
3205f4c40716 8224028: loop initial declarations introduced by JDK-8184770
aoqi
parents: 54884
diff changeset
   952
    for (pass = 0; pass < 2 && socketFD < 0; pass++) {
3205f4c40716 8224028: loop initial declarations introduced by JDK-8184770
aoqi
parents: 54884
diff changeset
   953
        for (ai = addrInfo; ai != NULL; ai = ai->ai_next) {
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   954
            if ((pass == 0 && ai->ai_family == preferredAddressFamily) ||
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   955
                (pass == 1 && ai->ai_family != preferredAddressFamily))
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   956
            {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   957
                err = connectToAddr(ai, attachTimeout, &socketFD);
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   958
                if (err == JDWPTRANSPORT_ERROR_NONE) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   959
                    break;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   960
                }
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   961
                if (socketFD >= 0) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   962
                    dbgsysSocketClose(socketFD);
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   963
                    socketFD = -1;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   964
                }
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   965
            }
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   966
        }
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   967
    }
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   968
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   969
    freeaddrinfo(addrInfo);
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   970
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   971
    /* err from the last connectToAddr() call */
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   972
    if (err != 0) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   973
        return err;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   974
    }
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
   975
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
    if (attachTimeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        dbgsysConfigureBlocking(socketFD, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
    err = handshake(socketFD, handshakeTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
    if (err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        dbgsysSocketClose(socketFD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        socketFD = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        return err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
static jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
socketTransport_isOpen(jdwpTransportEnv* env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
    if (socketFD >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
socketTransport_close(jdwpTransportEnv* env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
    int fd = socketFD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
    socketFD = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
    if (fd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    }
22605
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 15274
diff changeset
  1008
#ifdef _AIX
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 15274
diff changeset
  1009
    /*
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 15274
diff changeset
  1010
      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
  1011
      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
  1012
      ...
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 15274
diff changeset
  1013
      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
  1014
      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
  1015
      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
  1016
      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
  1017
      ...
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 15274
diff changeset
  1018
    */
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 15274
diff changeset
  1019
    shutdown(fd, 2);
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 15274
diff changeset
  1020
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
    if (dbgsysSocketClose(fd) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
         * 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
  1024
         * any subsequent close will likely fail as well.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        RETURN_IO_ERROR("close failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
socketTransport_writePacket(jdwpTransportEnv* env, const jdwpPacket *packet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
    jint len, data_len, id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     * room for header and up to MAX_DATA_SIZE data bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     */
48242
61e60548c0cf 8193258: Better usage of JDWP HEADER SIZE
clanger
parents: 47216
diff changeset
  1038
    char header[JDWP_HEADER_SIZE + MAX_DATA_SIZE];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
    jbyte *data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
    /* packet can't be null */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
    if (packet == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, "packet is NULL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
    len = packet->type.cmd.len;         /* includes header */
48242
61e60548c0cf 8193258: Better usage of JDWP HEADER SIZE
clanger
parents: 47216
diff changeset
  1047
    data_len = len - JDWP_HEADER_SIZE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    /* bad packet */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
    if (data_len < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, "invalid length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
    /* prepare the header for transmission */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
    len = (jint)dbgsysHostToNetworkLong(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
    id = (jint)dbgsysHostToNetworkLong(packet->type.cmd.id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
    memcpy(header + 0, &len, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
    memcpy(header + 4, &id, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
    header[8] = packet->type.cmd.flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
    if (packet->type.cmd.flags & JDWPTRANSPORT_FLAGS_REPLY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        jshort errorCode =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
            dbgsysHostToNetworkShort(packet->type.reply.errorCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        memcpy(header + 9, &errorCode, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        header[9] = packet->type.cmd.cmdSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        header[10] = packet->type.cmd.cmd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    data = packet->type.cmd.data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
    /* Do one send for short packets, two for longer ones */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
    if (data_len <= MAX_DATA_SIZE) {
48242
61e60548c0cf 8193258: Better usage of JDWP HEADER SIZE
clanger
parents: 47216
diff changeset
  1073
        memcpy(header + JDWP_HEADER_SIZE, data, data_len);
61e60548c0cf 8193258: Better usage of JDWP HEADER SIZE
clanger
parents: 47216
diff changeset
  1074
        if (send_fully(socketFD, (char *)&header, JDWP_HEADER_SIZE + data_len) !=
61e60548c0cf 8193258: Better usage of JDWP HEADER SIZE
clanger
parents: 47216
diff changeset
  1075
            JDWP_HEADER_SIZE + data_len) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
            RETURN_IO_ERROR("send failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
    } else {
48242
61e60548c0cf 8193258: Better usage of JDWP HEADER SIZE
clanger
parents: 47216
diff changeset
  1079
        memcpy(header + JDWP_HEADER_SIZE, data, MAX_DATA_SIZE);
61e60548c0cf 8193258: Better usage of JDWP HEADER SIZE
clanger
parents: 47216
diff changeset
  1080
        if (send_fully(socketFD, (char *)&header, JDWP_HEADER_SIZE + MAX_DATA_SIZE) !=
61e60548c0cf 8193258: Better usage of JDWP HEADER SIZE
clanger
parents: 47216
diff changeset
  1081
            JDWP_HEADER_SIZE + MAX_DATA_SIZE) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
            RETURN_IO_ERROR("send failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        /* 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
  1085
        if (send_fully(socketFD, (char *)data + MAX_DATA_SIZE,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
  1086
                       data_len - MAX_DATA_SIZE) != data_len - MAX_DATA_SIZE) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
            RETURN_IO_ERROR("send failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
static jint
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
recv_fully(int f, char *buf, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
    int nbytes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
    while (nbytes < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        int res = dbgsysRecv(f, buf + nbytes, len - nbytes, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        if (res < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        } else if (res == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            break; /* eof, return nbytes which is less than len */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        nbytes += res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
    return nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
  1110
jint
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
  1111
send_fully(int f, char *buf, int len)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
  1112
{
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
  1113
    int nbytes = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
  1114
    while (nbytes < len) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
  1115
        int res = dbgsysSend(f, buf + nbytes, len - nbytes, 0);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
  1116
        if (res < 0) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
  1117
            return res;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
  1118
        } else if (res == 0) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
  1119
            break; /* eof, return nbytes which is less than len */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
  1120
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
  1121
        nbytes += res;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
  1122
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
  1123
    return nbytes;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
  1124
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
  1125
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
socketTransport_readPacket(jdwpTransportEnv* env, jdwpPacket* packet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
    jint length, data_len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
    jint n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    /* packet can't be null */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
    if (packet == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, "packet is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
    /* read the length field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
    n = recv_fully(socketFD, (char *)&length, sizeof(jint));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
    /* check for EOF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
    if (n == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        packet->type.cmd.len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
    if (n != sizeof(jint)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        RETURN_RECV_ERROR(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
    length = (jint)dbgsysNetworkToHostLong(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
    packet->type.cmd.len = length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
47121
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1152
    n = recv_fully(socketFD,(char *)&(packet->type.cmd.id), sizeof(jint));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
    if (n < (int)sizeof(jint)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        RETURN_RECV_ERROR(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
    packet->type.cmd.id = (jint)dbgsysNetworkToHostLong(packet->type.cmd.id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
47121
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1159
    n = recv_fully(socketFD,(char *)&(packet->type.cmd.flags), sizeof(jbyte));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
    if (n < (int)sizeof(jbyte)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        RETURN_RECV_ERROR(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
    if (packet->type.cmd.flags & JDWPTRANSPORT_FLAGS_REPLY) {
47121
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1165
        n = recv_fully(socketFD,(char *)&(packet->type.reply.errorCode), sizeof(jbyte));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        if (n < (int)sizeof(jshort)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
            RETURN_RECV_ERROR(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        /* FIXME - should the error be converted to host order?? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
    } else {
47121
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1174
        n = recv_fully(socketFD,(char *)&(packet->type.cmd.cmdSet), sizeof(jbyte));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        if (n < (int)sizeof(jbyte)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
            RETURN_RECV_ERROR(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
47121
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1179
        n = recv_fully(socketFD,(char *)&(packet->type.cmd.cmd), sizeof(jbyte));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        if (n < (int)sizeof(jbyte)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
            RETURN_RECV_ERROR(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
    data_len = length - ((sizeof(jint) * 2) + (sizeof(jbyte) * 3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
    if (data_len < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        setLastError(0, "Badly formed packet received - invalid length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        return JDWPTRANSPORT_ERROR_IO_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
    } else if (data_len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        packet->type.cmd.data = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
        packet->type.cmd.data= (*callback->alloc)(data_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        if (packet->type.cmd.data == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
            RETURN_ERROR(JDWPTRANSPORT_ERROR_OUT_OF_MEMORY, "out of memory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        n = recv_fully(socketFD,(char *)packet->type.cmd.data, data_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        if (n < data_len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
            (*callback->free)(packet->type.cmd.data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
            RETURN_RECV_ERROR(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
socketTransport_getLastError(jdwpTransportEnv* env, char** msgP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
    char *msg = (char *)dbgsysTlsGet(tlsIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
    if (msg == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        return JDWPTRANSPORT_ERROR_MSG_NOT_AVAILABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
    }
896
5c02031316bf 6725543: Compiler warnings in serviceability native code
ohair
parents: 2
diff changeset
  1215
    *msgP = (*callback->alloc)((int)strlen(msg)+1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
    if (*msgP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        return JDWPTRANSPORT_ERROR_OUT_OF_MEMORY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
    strcpy(*msgP, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
47121
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1223
static jdwpTransportError JNICALL
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1224
socketTransport_setConfiguration(jdwpTransportEnv* env, jdwpTransportConfiguration* cfg) {
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1225
    const char* allowed_peers = NULL;
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1226
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1227
    if (cfg == NULL) {
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1228
        RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT,
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1229
                     "NULL pointer to transport configuration is invalid");
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1230
    }
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1231
    allowed_peers = cfg->allowed_peers;
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1232
    _peers_cnt = 0;
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1233
    if (allowed_peers != NULL) {
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1234
        size_t len = strlen(allowed_peers);
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1235
        if (len == 0) { /* Impossible: parseOptions() would reject it */
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1236
            fprintf(stderr, "Error in allow option: '%s'\n", allowed_peers);
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1237
            RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT,
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1238
                         "allow option should not be empty");
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1239
        } else if (*allowed_peers == '*') {
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1240
            if (len != 1) {
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1241
                fprintf(stderr, "Error in allow option: '%s'\n", allowed_peers);
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1242
                RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT,
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1243
                             "allow option '*' cannot be expanded");
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1244
            }
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1245
        } else {
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1246
            int err = parseAllowedPeers(allowed_peers, len);
47121
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1247
            if (err != JDWPTRANSPORT_ERROR_NONE) {
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1248
                return err;
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1249
            }
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1250
        }
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1251
    }
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1252
    return JDWPTRANSPORT_ERROR_NONE;
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1253
}
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1254
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1255
/*
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1256
 * Reads boolean system value, sets *result to
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1257
 *  - trueValue if the property is "true";
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1258
 *  - falseValue if the property is "false".
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1259
 * Doesn't change *result if the property is not set or failed to read.
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1260
 */
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1261
static int readBooleanSysProp(int *result, int trueValue, int falseValue,
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1262
    JNIEnv* jniEnv, jclass sysClass, jmethodID getPropMethod, const char *propName)
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1263
{
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1264
    jstring value;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1265
    jstring name = (*jniEnv)->NewStringUTF(jniEnv, propName);
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1266
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1267
    if (name == NULL) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1268
        return JNI_ERR;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1269
    }
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1270
    value = (jstring)(*jniEnv)->CallStaticObjectMethod(jniEnv, sysClass, getPropMethod, name);
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1271
    if ((*jniEnv)->ExceptionCheck(jniEnv)) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1272
        return JNI_ERR;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1273
    }
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1274
    if (value != NULL) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1275
        const char *theValue = (*jniEnv)->GetStringUTFChars(jniEnv, value, NULL);
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1276
        if (theValue == NULL) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1277
            return JNI_ERR;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1278
        }
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1279
        if (strcmp(theValue, "true") == 0) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1280
            *result = trueValue;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1281
        } else if (strcmp(theValue, "false") == 0) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1282
            *result = falseValue;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1283
        }
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1284
        (*jniEnv)->ReleaseStringUTFChars(jniEnv, value, theValue);
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1285
    }
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1286
    return JNI_OK;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1287
}
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1288
49440
396ea30afbd5 8200178: Remove mapfiles for JDK native libraries
ihse
parents: 48978
diff changeset
  1289
JNIEXPORT jint JNICALL
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
jdwpTransport_OnLoad(JavaVM *vm, jdwpTransportCallback* cbTablePtr,
47121
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1291
                     jint version, jdwpTransportEnv** env)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
{
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1293
    JNIEnv* jniEnv = NULL;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1294
47121
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1295
    if (version < JDWPTRANSPORT_VERSION_1_0 ||
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1296
        version > JDWPTRANSPORT_VERSION_1_1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        return JNI_EVERSION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
    if (initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
         * This library doesn't support multiple environments (yet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        return JNI_EEXIST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
    initialized = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
    jvm = vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
    callback = cbTablePtr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
    /* initialize interface table */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
    interface.GetCapabilities = &socketTransport_getCapabilities;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
    interface.Attach = &socketTransport_attach;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
    interface.StartListening = &socketTransport_startListening;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
    interface.StopListening = &socketTransport_stopListening;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
    interface.Accept = &socketTransport_accept;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
    interface.IsOpen = &socketTransport_isOpen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
    interface.Close = &socketTransport_close;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
    interface.ReadPacket = &socketTransport_readPacket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
    interface.WritePacket = &socketTransport_writePacket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
    interface.GetLastError = &socketTransport_getLastError;
47121
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1320
    if (version >= JDWPTRANSPORT_VERSION_1_1) {
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1321
        interface.SetTransportConfiguration = &socketTransport_setConfiguration;
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1322
    }
3aceb4fc0e84 8061228: Allow JDWP socket connector to accept connections from certain ip addresses only
dsamersoff
parents: 46091
diff changeset
  1323
    *env = &single_env;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
    /* initialized TLS */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
    tlsIndex = dbgsysTlsAlloc();
54884
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1327
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1328
    // retrieve network-related system properties
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1329
    do {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1330
        jclass sysClass;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1331
        jmethodID getPropMethod;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1332
        if ((*vm)->GetEnv(vm, (void **)&jniEnv, JNI_VERSION_9) != JNI_OK) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1333
            break;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1334
        }
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1335
        sysClass = (*jniEnv)->FindClass(jniEnv, "java/lang/System");
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1336
        if (sysClass == NULL) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1337
            break;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1338
        }
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1339
        getPropMethod = (*jniEnv)->GetStaticMethodID(jniEnv, sysClass,
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1340
            "getProperty", "(Ljava/lang/String;)Ljava/lang/String;");
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1341
        if (getPropMethod == NULL) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1342
            break;
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1343
        }
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1344
        readBooleanSysProp(&allowOnlyIPv4, 1, 0,
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1345
            jniEnv, sysClass, getPropMethod, "java.net.preferIPv4Stack");
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1346
        readBooleanSysProp(&preferredAddressFamily, AF_INET6, AF_INET,
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1347
            jniEnv, sysClass, getPropMethod, "java.net.preferIPv6Addresses");
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1348
    } while (0);
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1349
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1350
    if (jniEnv != NULL && (*jniEnv)->ExceptionCheck(jniEnv)) {
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1351
        (*jniEnv)->ExceptionClear(jniEnv);
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1352
    }
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1353
8a6093c186a6 8184770: JDWP support for IPv6
amenkov
parents: 49440
diff changeset
  1354
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
    return JNI_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
}