jdk/src/share/transport/socket/socketTransport.c
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 896 5c02031316bf
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1998-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <errno.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include <ctype.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include "jdwpTransport.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include "sysSocket.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * The Socket Transport Library.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * This module is an implementation of the Java Debug Wire Protocol Transport
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Service Provider Interface - see src/share/javavm/export/jdwpTransport.h.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
static int serverSocketFD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
static int socketFD = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
static jdwpTransportCallback *callback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
static JavaVM *jvm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
static int tlsIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
static jboolean initialized;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
static struct jdwpTransportNativeInterface_ interface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
static jdwpTransportEnv single_env = (jdwpTransportEnv)&interface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
#define RETURN_ERROR(err, msg) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        if (1==1) { \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            setLastError(err, msg); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            return err; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
#define RETURN_IO_ERROR(msg)    RETURN_ERROR(JDWPTRANSPORT_ERROR_IO_ERROR, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
#define RETURN_RECV_ERROR(n) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        if (n == 0) { \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            RETURN_ERROR(JDWPTRANSPORT_ERROR_IO_ERROR, "premature EOF"); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        } else { \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            RETURN_IO_ERROR("recv error"); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
#define HEADER_SIZE     11
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
#define MAX_DATA_SIZE 1000
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * Record the last error for this thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
setLastError(jdwpTransportError err, char *newmsg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    char buf[255];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    char *msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /* get any I/O first in case any system calls override errno */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    if (err == JDWPTRANSPORT_ERROR_IO_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        dbgsysGetLastIOError(buf, sizeof(buf));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    msg = (char *)dbgsysTlsGet(tlsIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    if (msg != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        (*callback->free)(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    if (err == JDWPTRANSPORT_ERROR_IO_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        char *join_str = ": ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        int msg_len = strlen(newmsg) + strlen(join_str) + strlen(buf) + 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        msg = (*callback->alloc)(msg_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (msg != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            strcpy(msg, newmsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            strcat(msg, join_str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            strcat(msg, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        msg = (*callback->alloc)(strlen(newmsg)+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (msg != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            strcpy(msg, newmsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    dbgsysTlsPut(tlsIndex, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * Return the last error for this thread (may be NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
static char*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
getLastError() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    return (char *)dbgsysTlsGet(tlsIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
static jdwpTransportError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
setOptions(int fd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    jvalue dontcare;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    int err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    dontcare.i = 0;  /* keep compiler happy */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    err = dbgsysSetSocketOption(fd, SO_REUSEADDR, JNI_TRUE, dontcare);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    if (err < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        RETURN_IO_ERROR("setsockopt SO_REUSEADDR failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    err = dbgsysSetSocketOption(fd, TCP_NODELAY, JNI_TRUE, dontcare);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    if (err < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        RETURN_IO_ERROR("setsockopt TCPNODELAY failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
static jdwpTransportError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
handshake(int fd, jlong timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    char *hello = "JDWP-Handshake";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    char b[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    int rv, received, i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    if (timeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        dbgsysConfigureBlocking(fd, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    received = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    while (received < (int)strlen(hello)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        char *buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (timeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            rv = dbgsysPoll(fd, JNI_TRUE, JNI_FALSE, (long)timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            if (rv <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                setLastError(0, "timeout during handshake");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                return JDWPTRANSPORT_ERROR_IO_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        buf = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        buf += received;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        n = dbgsysRecv(fd, buf, strlen(hello)-received, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (n == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            setLastError(0, "handshake failed - connection prematurally closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            return JDWPTRANSPORT_ERROR_IO_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (n < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            RETURN_IO_ERROR("recv failed during handshake");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        received += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    if (timeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        dbgsysConfigureBlocking(fd, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    for (i=0; i<(int)strlen(hello); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (b[i] != hello[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            char msg[64];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            strcpy(msg, "handshake failed - received >");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            strncat(msg, b, strlen(hello));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            strcat(msg, "< - excepted >");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            strcat(msg, hello);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            strcat(msg, "<");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            setLastError(0, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            return JDWPTRANSPORT_ERROR_IO_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    if (dbgsysSend(fd, hello, strlen(hello), 0) != (int)strlen(hello)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        RETURN_IO_ERROR("send failed during handshake");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
static jdwpTransportError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
parseAddress(const char *address, struct sockaddr_in *sa, UINT32 defaultHost) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    char *colon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    memset((void *)sa,0,sizeof(struct sockaddr_in));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    sa->sin_family = AF_INET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /* check for host:port or port */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    colon = strchr(address, ':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    if (colon == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        u_short port = (u_short)atoi(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        sa->sin_port = dbgsysHostToNetworkShort(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        sa->sin_addr.s_addr = dbgsysHostToNetworkLong(defaultHost);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        char *buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        char *hostname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        u_short port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        UINT32 addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        buf = (*callback->alloc)(strlen(address)+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        if (buf == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            RETURN_ERROR(JDWPTRANSPORT_ERROR_OUT_OF_MEMORY, "out of memory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        strcpy(buf, address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        buf[colon - address] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        hostname = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        port = atoi(colon + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        sa->sin_port = dbgsysHostToNetworkShort(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
         * First see if the host is a literal IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
         * If not then try to resolve it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        addr = dbgsysInetAddr(hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        if (addr == 0xffffffff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            struct hostent *hp = dbgsysGetHostByName(hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            if (hp == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                /* don't use RETURN_IO_ERROR as unknown host is normal */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                setLastError(0, "gethostbyname: unknown host");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                (*callback->free)(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                return JDWPTRANSPORT_ERROR_IO_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            /* lookup was successful */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            memcpy(&(sa->sin_addr), hp->h_addr_list[0], hp->h_length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            sa->sin_addr.s_addr = addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        (*callback->free)(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
socketTransport_getCapabilities(jdwpTransportEnv* env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        JDWPTransportCapabilities* capabilitiesPtr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    JDWPTransportCapabilities result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    memset(&result, 0, sizeof(result));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    result.can_timeout_attach = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    result.can_timeout_accept = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    result.can_timeout_handshake = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    *capabilitiesPtr = result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
socketTransport_startListening(jdwpTransportEnv* env, const char* address,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                               char** actualAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    struct sockaddr_in sa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    int err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    memset((void *)&sa,0,sizeof(struct sockaddr_in));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    sa.sin_family = AF_INET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /* no address provided */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    if ((address == NULL) || (address[0] == '\0')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        address = "0";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    err = parseAddress(address, &sa, INADDR_ANY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    if (err != JDWPTRANSPORT_ERROR_NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        return err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    serverSocketFD = dbgsysSocket(AF_INET, SOCK_STREAM, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    if (serverSocketFD < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        RETURN_IO_ERROR("socket creation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    err = setOptions(serverSocketFD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    if (err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        return err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    err = dbgsysBind(serverSocketFD, (struct sockaddr *)&sa, sizeof(sa));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    if (err < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        RETURN_IO_ERROR("bind failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    err = dbgsysListen(serverSocketFD, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    if (err < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        RETURN_IO_ERROR("listen failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        char buf[20];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        int len = sizeof(sa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        jint portNum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        err = dbgsysGetSocketName(serverSocketFD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                               (struct sockaddr *)&sa, &len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        portNum = dbgsysNetworkToHostShort(sa.sin_port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        sprintf(buf, "%d", portNum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        *actualAddress = (*callback->alloc)(strlen(buf) + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        if (*actualAddress == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            RETURN_ERROR(JDWPTRANSPORT_ERROR_OUT_OF_MEMORY, "out of memory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            strcpy(*actualAddress, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
socketTransport_accept(jdwpTransportEnv* env, jlong acceptTimeout, jlong handshakeTimeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    int socketLen, err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    struct sockaddr_in socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    jlong startTime = (jlong)0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * Use a default handshake timeout if not specified - this avoids an indefinite
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * hang in cases where something other than a debugger connects to our port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    if (handshakeTimeout == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        handshakeTimeout = 2000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
         * If there is an accept timeout then we put the socket in non-blocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
         * mode and poll for a connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        if (acceptTimeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            int rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            dbgsysConfigureBlocking(serverSocketFD, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            startTime = dbgsysCurrentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            rv = dbgsysPoll(serverSocketFD, JNI_TRUE, JNI_FALSE, (long)acceptTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            if (rv <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                /* set the last error here as could be overridden by configureBlocking */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                if (rv == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    setLastError(JDWPTRANSPORT_ERROR_IO_ERROR, "poll failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                /* restore blocking state */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                dbgsysConfigureBlocking(serverSocketFD, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                if (rv == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                    RETURN_ERROR(JDWPTRANSPORT_ERROR_TIMEOUT, "timed out waiting for connection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    return JDWPTRANSPORT_ERROR_IO_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
         * Accept the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        memset((void *)&socket,0,sizeof(struct sockaddr_in));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        socketLen = sizeof(socket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        socketFD = dbgsysAccept(serverSocketFD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                                (struct sockaddr *)&socket,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                                &socketLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        /* set the last error here as could be overridden by configureBlocking */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        if (socketFD < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            setLastError(JDWPTRANSPORT_ERROR_IO_ERROR, "accept failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
         * Restore the blocking state - note that the accepted socket may be in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
         * blocking or non-blocking mode (platform dependent). However as there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
         * is a handshake timeout set then it will go into non-blocking mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
         * anyway for the handshake.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        if (acceptTimeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            dbgsysConfigureBlocking(serverSocketFD, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        if (socketFD < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            return JDWPTRANSPORT_ERROR_IO_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        /* handshake with the debugger */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        err = handshake(socketFD, handshakeTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
         * If the handshake fails then close the connection. If there if an accept
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
         * timeout then we must adjust the timeout for the next poll.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        if (err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            fprintf(stderr, "Debugger failed to attach: %s\n", getLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            dbgsysSocketClose(socketFD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            socketFD = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            if (acceptTimeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                long endTime = dbgsysCurrentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                acceptTimeout -= (endTime - startTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                if (acceptTimeout <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                    setLastError(JDWPTRANSPORT_ERROR_IO_ERROR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                        "timeout waiting for debugger to connect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                    return JDWPTRANSPORT_ERROR_IO_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    } while (socketFD < 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
socketTransport_stopListening(jdwpTransportEnv *env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    if (serverSocketFD < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_STATE, "connection not open");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    if (dbgsysSocketClose(serverSocketFD) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        RETURN_IO_ERROR("close failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    serverSocketFD = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
socketTransport_attach(jdwpTransportEnv* env, const char* addressString, jlong attachTimeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                       jlong handshakeTimeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    struct sockaddr_in sa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    int err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    if (addressString == NULL || addressString[0] == '\0') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, "address is missing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    err = parseAddress(addressString, &sa, 0x7f000001);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    if (err != JDWPTRANSPORT_ERROR_NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        return err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    socketFD = dbgsysSocket(AF_INET, SOCK_STREAM, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    if (socketFD < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        RETURN_IO_ERROR("unable to create socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    err = setOptions(socketFD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    if (err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        return err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * To do a timed connect we make the socket non-blocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * and poll with a timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    if (attachTimeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        dbgsysConfigureBlocking(socketFD, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    err = dbgsysConnect(socketFD, (struct sockaddr *)&sa, sizeof(sa));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    if (err == DBG_EINPROGRESS && attachTimeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        err = dbgsysFinishConnect(socketFD, (long)attachTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        if (err == DBG_ETIMEOUT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            dbgsysConfigureBlocking(socketFD, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            RETURN_ERROR(JDWPTRANSPORT_ERROR_TIMEOUT, "connect timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    if (err < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        RETURN_IO_ERROR("connect failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    if (attachTimeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        dbgsysConfigureBlocking(socketFD, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    err = handshake(socketFD, handshakeTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    if (err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        dbgsysSocketClose(socketFD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        socketFD = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        return err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
static jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
socketTransport_isOpen(jdwpTransportEnv* env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    if (socketFD >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
socketTransport_close(jdwpTransportEnv* env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    int fd = socketFD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    socketFD = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    if (fd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    if (dbgsysSocketClose(fd) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
         * close failed - it's pointless to restore socketFD here because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
         * any subsequent close will likely fail aswell.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        RETURN_IO_ERROR("close failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
socketTransport_writePacket(jdwpTransportEnv* env, const jdwpPacket *packet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    jint len, data_len, id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * room for header and up to MAX_DATA_SIZE data bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    char header[HEADER_SIZE + MAX_DATA_SIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    jbyte *data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    /* packet can't be null */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    if (packet == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, "packet is NULL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    len = packet->type.cmd.len;         /* includes header */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    data_len = len - HEADER_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    /* bad packet */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    if (data_len < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, "invalid length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    /* prepare the header for transmission */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    len = (jint)dbgsysHostToNetworkLong(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    id = (jint)dbgsysHostToNetworkLong(packet->type.cmd.id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    memcpy(header + 0, &len, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    memcpy(header + 4, &id, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    header[8] = packet->type.cmd.flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    if (packet->type.cmd.flags & JDWPTRANSPORT_FLAGS_REPLY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        jshort errorCode =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            dbgsysHostToNetworkShort(packet->type.reply.errorCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        memcpy(header + 9, &errorCode, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        header[9] = packet->type.cmd.cmdSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        header[10] = packet->type.cmd.cmd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    data = packet->type.cmd.data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    /* Do one send for short packets, two for longer ones */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    if (data_len <= MAX_DATA_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        memcpy(header + HEADER_SIZE, data, data_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        if (dbgsysSend(socketFD, (char *)&header, HEADER_SIZE + data_len, 0) !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            HEADER_SIZE + data_len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            RETURN_IO_ERROR("send failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        memcpy(header + HEADER_SIZE, data, MAX_DATA_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        if (dbgsysSend(socketFD, (char *)&header, HEADER_SIZE + MAX_DATA_SIZE, 0) !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            HEADER_SIZE + MAX_DATA_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            RETURN_IO_ERROR("send failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        /* Send the remaining data bytes right out of the data area. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        if (dbgsysSend(socketFD, (char *)data + MAX_DATA_SIZE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                       data_len - MAX_DATA_SIZE, 0) != data_len - MAX_DATA_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            RETURN_IO_ERROR("send failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
static jint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
recv_fully(int f, char *buf, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    int nbytes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    while (nbytes < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        int res = dbgsysRecv(f, buf + nbytes, len - nbytes, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        if (res < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        } else if (res == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            break; /* eof, return nbytes which is less than len */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        nbytes += res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    return nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
socketTransport_readPacket(jdwpTransportEnv* env, jdwpPacket* packet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    jint length, data_len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    jint n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    /* packet can't be null */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    if (packet == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, "packet is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    /* read the length field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    n = recv_fully(socketFD, (char *)&length, sizeof(jint));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    /* check for EOF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    if (n == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        packet->type.cmd.len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    if (n != sizeof(jint)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        RETURN_RECV_ERROR(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    length = (jint)dbgsysNetworkToHostLong(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    packet->type.cmd.len = length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    n = recv_fully(socketFD,(char *)&(packet->type.cmd.id),sizeof(jint));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    if (n < (int)sizeof(jint)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        RETURN_RECV_ERROR(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    packet->type.cmd.id = (jint)dbgsysNetworkToHostLong(packet->type.cmd.id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    n = recv_fully(socketFD,(char *)&(packet->type.cmd.flags),sizeof(jbyte));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    if (n < (int)sizeof(jbyte)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        RETURN_RECV_ERROR(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    if (packet->type.cmd.flags & JDWPTRANSPORT_FLAGS_REPLY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        n = recv_fully(socketFD,(char *)&(packet->type.reply.errorCode),sizeof(jbyte));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        if (n < (int)sizeof(jshort)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            RETURN_RECV_ERROR(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        /* FIXME - should the error be converted to host order?? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        n = recv_fully(socketFD,(char *)&(packet->type.cmd.cmdSet),sizeof(jbyte));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        if (n < (int)sizeof(jbyte)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            RETURN_RECV_ERROR(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        n = recv_fully(socketFD,(char *)&(packet->type.cmd.cmd),sizeof(jbyte));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        if (n < (int)sizeof(jbyte)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            RETURN_RECV_ERROR(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    data_len = length - ((sizeof(jint) * 2) + (sizeof(jbyte) * 3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    if (data_len < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        setLastError(0, "Badly formed packet received - invalid length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        return JDWPTRANSPORT_ERROR_IO_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    } else if (data_len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        packet->type.cmd.data = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        packet->type.cmd.data= (*callback->alloc)(data_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        if (packet->type.cmd.data == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            RETURN_ERROR(JDWPTRANSPORT_ERROR_OUT_OF_MEMORY, "out of memory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        n = recv_fully(socketFD,(char *)packet->type.cmd.data, data_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        if (n < data_len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            (*callback->free)(packet->type.cmd.data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            RETURN_RECV_ERROR(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
socketTransport_getLastError(jdwpTransportEnv* env, char** msgP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    char *msg = (char *)dbgsysTlsGet(tlsIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    if (msg == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        return JDWPTRANSPORT_ERROR_MSG_NOT_AVAILABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    *msgP = (*callback->alloc)(strlen(msg)+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    if (*msgP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        return JDWPTRANSPORT_ERROR_OUT_OF_MEMORY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    strcpy(*msgP, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
jdwpTransport_OnLoad(JavaVM *vm, jdwpTransportCallback* cbTablePtr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                     jint version, jdwpTransportEnv** result)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    if (version != JDWPTRANSPORT_VERSION_1_0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        return JNI_EVERSION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    if (initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
         * This library doesn't support multiple environments (yet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        return JNI_EEXIST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    initialized = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    jvm = vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    callback = cbTablePtr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    /* initialize interface table */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    interface.GetCapabilities = &socketTransport_getCapabilities;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    interface.Attach = &socketTransport_attach;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    interface.StartListening = &socketTransport_startListening;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    interface.StopListening = &socketTransport_stopListening;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    interface.Accept = &socketTransport_accept;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    interface.IsOpen = &socketTransport_isOpen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    interface.Close = &socketTransport_close;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    interface.ReadPacket = &socketTransport_readPacket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    interface.WritePacket = &socketTransport_writePacket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    interface.GetLastError = &socketTransport_getLastError;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    *result = &single_env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    /* initialized TLS */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    tlsIndex = dbgsysTlsAlloc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    return JNI_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
}