jdk/src/windows/native/sun/nio/ch/DatagramChannelImpl.c
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 1152 29d6145d1097
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 2001-2007 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "jlong.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <io.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include "sun_nio_ch_DatagramChannelImpl.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include "nio.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include "nio_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include "net_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include <winsock2.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
static jfieldID isa_addrID;     /* address in java.net.InetSocketAddress */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
static jfieldID isa_portID;     /* port in java.net.InetSocketAddress */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
static jfieldID dci_senderID;   /* sender in sun.nio.ch.DatagramChannelImpl */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
static jfieldID dci_senderAddrID; /* sender InetAddress in sun.nio.ch.DatagramChannelImpl */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
static jfieldID dci_senderPortID; /* sender port in sun.nio.ch.DatagramChannelImpl */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
static jfieldID ia_addrID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
static jfieldID ia_famID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
static jclass isa_class;        /* java.net.InetSocketAddress */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
static jclass ia_class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
static jmethodID isa_ctorID;    /*   .InetSocketAddress(InetAddress, int) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
static jmethodID ia_ctorID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * Returns JNI_TRUE if DatagramChannelImpl has already cached an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * InetAddress/port corresponding to the socket address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
static jboolean isSenderCached(JNIEnv *env, jobject this, struct sockaddr_in *sa) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    jobject senderAddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /* shouldn't happen until we have dual IPv4/IPv6 stack (post-XP ?) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    if (sa->sin_family != AF_INET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * Compare source address to cached InetAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    senderAddr = (*env)->GetObjectField(env, this, dci_senderAddrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    if (senderAddr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    if ((jint)ntohl(sa->sin_addr.s_addr) !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        (*env)->GetIntField(env, senderAddr, ia_addrID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Compare source port to cached port
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    if ((jint)ntohs(sa->sin_port) !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        (*env)->GetIntField(env, this, dci_senderPortID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
Java_sun_nio_ch_DatagramChannelImpl_initIDs(JNIEnv *env, jclass clazz)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    clazz = (*env)->FindClass(env, "java/net/InetSocketAddress");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    isa_class = (*env)->NewGlobalRef(env, clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    isa_ctorID = (*env)->GetMethodID(env, clazz, "<init>",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                                     "(Ljava/net/InetAddress;I)V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    isa_addrID = (*env)->GetFieldID(env, clazz, "addr",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                                    "Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    isa_portID = (*env)->GetFieldID(env, clazz, "port", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    clazz = (*env)->FindClass(env, "sun/nio/ch/DatagramChannelImpl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    dci_senderID = (*env)->GetFieldID(env, clazz, "sender",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                                      "Ljava/net/SocketAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    dci_senderAddrID = (*env)->GetFieldID(env, clazz,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                                          "cachedSenderInetAddress",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                                          "Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    dci_senderPortID = (*env)->GetFieldID(env, clazz,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                                          "cachedSenderPort", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    clazz = (*env)->FindClass(env, "java/net/Inet4Address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    ia_class = (*env)->NewGlobalRef(env, clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    ia_addrID = (*env)->GetFieldID(env, clazz, "address", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    ia_famID = (*env)->GetFieldID(env, clazz, "family", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    ia_ctorID = (*env)->GetMethodID(env, clazz, "<init>", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * Return JNI_TRUE if this Windows edition supports ICMP Port Unreachable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
__inline static jboolean supportPortUnreachable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    static jboolean initDone;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    static jboolean portUnreachableSupported;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    if (!initDone) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        OSVERSIONINFO ver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        ver.dwOSVersionInfoSize = sizeof(ver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        GetVersionEx(&ver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (ver.dwPlatformId == VER_PLATFORM_WIN32_NT && ver.dwMajorVersion >= 5) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            portUnreachableSupported = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            portUnreachableSupported = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        initDone = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    return portUnreachableSupported;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * This function "purges" all outstanding ICMP port unreachable packets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * outstanding on a socket and returns JNI_TRUE if any ICMP messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * have been purged. The rational for purging is to emulate normal BSD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * behaviour whereby receiving a "connection reset" status resets the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
jboolean purgeOutstandingICMP(JNIEnv *env, jclass clazz, jint fd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    jboolean got_icmp = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    char buf[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    fd_set tbl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    struct timeval t = { 0, 0 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    struct sockaddr_in rmtaddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    int addrlen = sizeof(rmtaddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * A no-op if this OS doesn't support it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    if (!supportPortUnreachable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Peek at the queue to see if there is an ICMP port unreachable. If there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * is then receive it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    FD_ZERO(&tbl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    FD_SET((u_int)fd, &tbl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    while(1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (select(/*ignored*/fd+1, &tbl, 0, 0, &t) <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (recvfrom(fd, buf, 1, MSG_PEEK,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                     (struct sockaddr *)&rmtaddr, &addrlen) != SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (WSAGetLastError() != WSAECONNRESET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            /* some other error - we don't care here */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        recvfrom(fd, buf, 1, 0,  (struct sockaddr *)&rmtaddr, &addrlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        got_icmp = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    return got_icmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
Java_sun_nio_ch_DatagramChannelImpl_disconnect0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                                                jobject fdo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    jint fd = fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    int rv = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    struct sockaddr_in psa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    int sa_len = sizeof(psa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    memset(&psa, 0, sa_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    rv = connect((SOCKET)fd, (struct sockaddr *)&psa, sa_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    if (rv == SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        handleSocketError(env, WSAGetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
Java_sun_nio_ch_DatagramChannelImpl_receive0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                                            jobject fdo, jlong address,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                                            jint len, jboolean connected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    jint fd = fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    void *buf = (void *)jlong_to_ptr(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    struct sockaddr_in psa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    int sa_len = sizeof(psa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    BOOL retry = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    jint n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        retry = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        n = recvfrom((SOCKET)fd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                     (char *)buf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                     len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                     0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                     (struct sockaddr *)&psa,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                     &sa_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if (n == SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            int theErr = (jint)WSAGetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            if (theErr == WSAEMSGSIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                /* Spec says the rest of the data will be discarded... */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                n = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            } else if (theErr == WSAECONNRESET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                purgeOutstandingICMP(env, this, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                if (connected == JNI_FALSE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    retry = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                    JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                    return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            } else if (theErr == WSAEWOULDBLOCK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                return IOS_UNAVAILABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            } else return handleSocketError(env, theErr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    } while (retry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    if (!isSenderCached(env, this, &psa)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        int port = ntohs(psa.sin_port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        jobject ia = (*env)->NewObject(env, ia_class, ia_ctorID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        jobject isa = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        if (psa.sin_family != AF_INET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                            "Protocol family unavailable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (ia != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            // populate InetAddress (assumes AF_INET)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            (*env)->SetIntField(env, ia, ia_addrID, ntohl(psa.sin_addr.s_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            // create InetSocketAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            isa = (*env)->NewObject(env, isa_class, isa_ctorID, ia, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if (isa == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            JNU_ThrowOutOfMemoryError(env, "heap allocation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        // update cachedSenderInetAddress/cachedSenderPort
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        (*env)->SetObjectField(env, this, dci_senderAddrID, ia);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        (*env)->SetIntField(env, this, dci_senderPortID, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        // update sender
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        (*env)->SetObjectField(env, this, dci_senderID, isa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
Java_sun_nio_ch_DatagramChannelImpl_send0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                                            jobject fdo, jlong address,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                                            jint len, jobject dest)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    jint fd = fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    void *buf = (void *)jlong_to_ptr(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    SOCKETADDRESS psa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    int sa_len = sizeof(psa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    jint rv = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    jobject destAddress = (*env)->GetObjectField(env, dest, isa_addrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    jint destPort = (*env)->GetIntField(env, dest, isa_portID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    if (NET_InetAddressToSockaddr(env, destAddress, destPort,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                                  (struct sockaddr *)&psa,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                                   &sa_len, JNI_FALSE) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
      return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    rv = sendto((SOCKET)fd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
               buf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
               len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
               0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
               (struct sockaddr *)&psa,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
               sa_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    if (rv == SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        int theErr = (jint)WSAGetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        if (theErr == WSAEWOULDBLOCK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            return IOS_UNAVAILABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        return handleSocketError(env, (jint)WSAGetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
}