src/java.base/windows/native/libnet/TwoStacksPlainDatagramSocketImpl.c
author pconcannon
Thu, 28 Nov 2019 16:49:52 +0000
changeset 59313 6f12009ea9d7
parent 55375 96c7427456f9
permissions -rw-r--r--
8233307: MulticastSocket getOption(IP_MULTICAST_IF) returns interface when not set Summary: The MulticastSocket method getOption has been changed to conform to the behavior described in StandardSocketOptions.IP_MULTICAST_IF. Reviewed-by: chegar, dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
49446
a11d3a5ca20b 8200304: TwoStacksPlainDatagramSocketImpl and socket cleaner
chegar
parents: 47216
diff changeset
     2
 * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5200
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5200
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5200
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5200
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5200
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41594
diff changeset
    25
#include <malloc.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41594
diff changeset
    27
#include "net_util.h"
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41594
diff changeset
    28
#include "NetworkInterface.h"
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41594
diff changeset
    29
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41594
diff changeset
    30
#include "java_net_TwoStacksPlainDatagramSocketImpl.h"
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41594
diff changeset
    31
#include "java_net_SocketOptions.h"
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41594
diff changeset
    32
#include "java_net_NetworkInterface.h"
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41594
diff changeset
    33
#include "java_net_InetAddress.h"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#ifndef IPTOS_TOS_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#define IPTOS_TOS_MASK 0x1e
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#ifndef IPTOS_PREC_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#define IPTOS_PREC_MASK 0xe0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
#define IN_CLASSD(i)    (((long)(i) & 0xf0000000) == 0xe0000000)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
#define IN_MULTICAST(i) IN_CLASSD(i)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
    46
extern int getAllInterfacesAndAddresses(JNIEnv *env, netif **netifPP);
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
    47
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
/************************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * TwoStacksPlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
static jfieldID IO_fd_fdID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
static jfieldID pdsi_trafficClassID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
jfieldID pdsi_fdID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
jfieldID pdsi_fd1ID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
jfieldID pdsi_fduseID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
jfieldID pdsi_lastfdID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
jfieldID pdsi_timeoutID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
jfieldID pdsi_localPortID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
jfieldID pdsi_connected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
static jclass ia4_clazz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
static jmethodID ia4_ctor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * Notes about UDP/IPV6 on Windows (XP and 2003 server):
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * fd always points to the IPv4 fd, and fd1 points to the IPv6 fd.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * Both fds are used when we bind to a wild-card address. When a specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * address is used, only one of them is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * Returns a java.lang.Integer based on 'i'
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
jobject createInteger(JNIEnv *env, int i) {
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
    78
    static jclass i_class = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    static jmethodID i_ctrID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    static jfieldID i_valueID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    if (i_class == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        jclass c = (*env)->FindClass(env, "java/lang/Integer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        CHECK_NULL_RETURN(c, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        i_ctrID = (*env)->GetMethodID(env, c, "<init>", "(I)V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        CHECK_NULL_RETURN(i_ctrID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        i_class = (*env)->NewGlobalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        CHECK_NULL_RETURN(i_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
    91
    return (*env)->NewObject(env, i_class, i_ctrID, i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * Returns a java.lang.Boolean based on 'b'
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
jobject createBoolean(JNIEnv *env, int b) {
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
    98
    static jclass b_class = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    static jmethodID b_ctrID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    static jfieldID b_valueID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    if (b_class == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        jclass c = (*env)->FindClass(env, "java/lang/Boolean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        CHECK_NULL_RETURN(c, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        b_ctrID = (*env)->GetMethodID(env, c, "<init>", "(Z)V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        CHECK_NULL_RETURN(b_ctrID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        b_class = (*env)->NewGlobalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        CHECK_NULL_RETURN(b_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   111
    return (*env)->NewObject(env, b_class, b_ctrID, (jboolean)(b!=0));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
static int getFD(JNIEnv *env, jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    if (fdObj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    return (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
static int getFD1(JNIEnv *env, jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fd1ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    if (fdObj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    return (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * This function "purges" all outstanding ICMP port unreachable packets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * outstanding on a socket and returns JNI_TRUE if any ICMP messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * have been purged. The rational for purging is to emulate normal BSD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * behaviour whereby receiving a "connection reset" status resets the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
static jboolean purgeOutstandingICMP(JNIEnv *env, jobject this, jint fd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    jboolean got_icmp = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    char buf[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    fd_set tbl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    struct timeval t = { 0, 0 };
22069
d55e36f6c0c9 7102702: java/net/PortUnreachableException/OneExceptionOnly.java failing
msheppar
parents: 19866
diff changeset
   145
    SOCKETADDRESS rmtaddr;
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   146
    int addrlen = sizeof(SOCKETADDRESS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
25551
62ad60d30644 8040810: Uninitialised memory in jdk/src/windows/native/java/net: net_util_md.c, TwoStacksPlainSocketImpl.c, TwoStacksPlainDatagramSocketImpl.c, DualStackPlainSocketImpl.c, DualStackPlainDatagramSocketImpl.c
msheppar
parents: 23015
diff changeset
   148
    memset((char *)&rmtaddr, 0, sizeof(rmtaddr));
62ad60d30644 8040810: Uninitialised memory in jdk/src/windows/native/java/net: net_util_md.c, TwoStacksPlainSocketImpl.c, TwoStacksPlainDatagramSocketImpl.c, DualStackPlainSocketImpl.c, DualStackPlainDatagramSocketImpl.c
msheppar
parents: 23015
diff changeset
   149
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * Peek at the queue to see if there is an ICMP port unreachable. If there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * is then receive it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    FD_ZERO(&tbl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    FD_SET(fd, &tbl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    while(1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (select(/*ignored*/fd+1, &tbl, 0, 0, &t) <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   160
        if (recvfrom(fd, buf, 1, MSG_PEEK, &rmtaddr.sa, &addrlen) != SOCKET_ERROR) {
2
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 (WSAGetLastError() != WSAECONNRESET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            /* some other error - we don't care here */
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
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   168
        recvfrom(fd, buf, 1, 0, &rmtaddr.sa, &addrlen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        got_icmp = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    return got_icmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 * Class:     java_net_TwoStacksPlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 * Method:    init
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
Java_java_net_TwoStacksPlainDatagramSocketImpl_init(JNIEnv *env, jclass cls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /* get fieldIDs */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    pdsi_fdID = (*env)->GetFieldID(env, cls, "fd", "Ljava/io/FileDescriptor;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    CHECK_NULL(pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    pdsi_fd1ID = (*env)->GetFieldID(env, cls, "fd1", "Ljava/io/FileDescriptor;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    CHECK_NULL(pdsi_fd1ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    pdsi_timeoutID = (*env)->GetFieldID(env, cls, "timeout", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    CHECK_NULL(pdsi_timeoutID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    pdsi_fduseID = (*env)->GetFieldID(env, cls, "fduse", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    CHECK_NULL(pdsi_fduseID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    pdsi_lastfdID = (*env)->GetFieldID(env, cls, "lastfd", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    CHECK_NULL(pdsi_lastfdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    pdsi_trafficClassID = (*env)->GetFieldID(env, cls, "trafficClass", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    CHECK_NULL(pdsi_trafficClassID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    pdsi_localPortID = (*env)->GetFieldID(env, cls, "localPort", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    CHECK_NULL(pdsi_localPortID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    pdsi_connected = (*env)->GetFieldID(env, cls, "connected", "Z");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    CHECK_NULL(pdsi_connected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    cls = (*env)->FindClass(env, "java/io/FileDescriptor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    CHECK_NULL(cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    IO_fd_fdID = NET_GetFileDescriptorID(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    CHECK_NULL(IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    ia4_clazz = (*env)->FindClass(env, "java/net/Inet4Address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    CHECK_NULL(ia4_clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    ia4_clazz = (*env)->NewGlobalRef(env, ia4_clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    CHECK_NULL(ia4_clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    ia4_ctor = (*env)->GetMethodID(env, ia4_clazz, "<init>", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    CHECK_NULL(ia4_ctor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
Java_java_net_TwoStacksPlainDatagramSocketImpl_bind0(JNIEnv *env, jobject this,
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   216
                                           jint port, jobject addressObj,
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   217
                                           jboolean exclBind) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    jobject fd1Obj = (*env)->GetObjectField(env, this, pdsi_fd1ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    int ipv6_supported = ipv6_available();
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   221
    int fd, fd1 = -1, lcladdrlen = 0;
51153
78f16a9f7563 8196224: Even better Internet address support
igerasim
parents: 51151
diff changeset
   222
    jint family;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    SOCKETADDRESS lcladdr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
51153
78f16a9f7563 8196224: Even better Internet address support
igerasim
parents: 51151
diff changeset
   225
    family = getInetAddress_family(env, addressObj);
78f16a9f7563 8196224: Even better Internet address support
igerasim
parents: 51151
diff changeset
   226
    JNU_CHECK_EXCEPTION(env);
78f16a9f7563 8196224: Even better Internet address support
igerasim
parents: 51151
diff changeset
   227
    if (family == java_net_InetAddress_IPv6 && !ipv6_supported) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                        "Protocol family not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    if (IS_NULL(fdObj) || (ipv6_supported && IS_NULL(fd1Obj))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (ipv6_supported) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            fd1 = (*env)->GetIntField(env, fd1Obj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   241
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    if (IS_NULL(addressObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        JNU_ThrowNullPointerException(env, "argument address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   247
    if (NET_InetAddressToSockaddr(env, addressObj, port, &lcladdr,
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   248
                                  &lcladdrlen, JNI_FALSE) != 0) {
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   249
        return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    if (ipv6_supported) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        struct ipv6bind v6bind;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        v6bind.addr = &lcladdr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        v6bind.ipv4_fd = fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        v6bind.ipv6_fd = fd1;
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
   257
        if (NET_BindV6(&v6bind, exclBind) != -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            /* check if the fds have changed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            if (v6bind.ipv4_fd != fd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                fd = v6bind.ipv4_fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                if (fd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    /* socket is closed. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    (*env)->SetObjectField(env, this, pdsi_fdID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    /* socket was re-created */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    (*env)->SetIntField(env, fdObj, IO_fd_fdID, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            if (v6bind.ipv6_fd != fd1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                fd1 = v6bind.ipv6_fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                if (fd1 == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    /* socket is closed. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    (*env)->SetObjectField(env, this, pdsi_fd1ID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    /* socket was re-created */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    (*env)->SetIntField(env, fd1Obj, IO_fd_fdID, fd1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        } else {
26458
d6a5aed9204b 7010989: Duplicate closure of file descriptors leads to unexpected and incorrect closure of sockets
igerasim
parents: 25859
diff changeset
   280
            /* NET_BindV6() closes both sockets upon a failure */
d6a5aed9204b 7010989: Duplicate closure of file descriptors leads to unexpected and incorrect closure of sockets
igerasim
parents: 25859
diff changeset
   281
            (*env)->SetObjectField(env, this, pdsi_fdID, NULL);
d6a5aed9204b 7010989: Duplicate closure of file descriptors leads to unexpected and incorrect closure of sockets
igerasim
parents: 25859
diff changeset
   282
            (*env)->SetObjectField(env, this, pdsi_fd1ID, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            NET_ThrowCurrent (env, "Cannot bind");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    } else {
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   287
        if (NET_WinBind(fd, &lcladdr, lcladdrlen, exclBind) == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            if (WSAGetLastError() == WSAEACCES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                WSASetLastError(WSAEADDRINUSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            }
49446
a11d3a5ca20b 8200304: TwoStacksPlainDatagramSocketImpl and socket cleaner
chegar
parents: 47216
diff changeset
   291
            (*env)->SetObjectField(env, this, pdsi_fdID, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            NET_ThrowCurrent(env, "Cannot bind");
49446
a11d3a5ca20b 8200304: TwoStacksPlainDatagramSocketImpl and socket cleaner
chegar
parents: 47216
diff changeset
   293
            closesocket(fd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    if (port == 0) {
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   299
        if (getsockname(fd == -1 ? fd1 : fd, &lcladdr.sa, &lcladdrlen) == -1) {
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   300
            NET_ThrowCurrent(env, "getsockname");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   303
        port = ntohs((u_short)GET_PORT(&lcladdr));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    (*env)->SetIntField(env, this, pdsi_localPortID, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
 * Class:     java_net_TwoStacksPlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
 * Method:    connect0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
 * Signature: (Ljava/net/InetAddress;I)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
JNIEXPORT void JNICALL
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   316
Java_java_net_TwoStacksPlainDatagramSocketImpl_connect0
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   317
  (JNIEnv *env, jobject this, jobject address, jint port)
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   318
{
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    jobject fd1Obj = (*env)->GetObjectField(env, this, pdsi_fd1ID);
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   321
    jint fd = -1, fd1 = -1, fdc, family;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    SOCKETADDRESS rmtaddr;
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   323
    int rmtaddrlen = 0;
49892
8bed781a8d9c 8202154: Remove unused code in java.base/windows/native/libnet
igerasim
parents: 49446
diff changeset
   324
    DWORD x1, x2; /* ignored result codes */
8bed781a8d9c 8202154: Remove unused code in java.base/windows/native/libnet
igerasim
parents: 49446
diff changeset
   325
    int res, t;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    if (IS_NULL(fdObj) && IS_NULL(fd1Obj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   332
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    if (!IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   336
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    if (!IS_NULL(fd1Obj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        fd1 = (*env)->GetIntField(env, fd1Obj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    if (IS_NULL(address)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        JNU_ThrowNullPointerException(env, "address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   346
    family = getInetAddress_family(env, address);
51151
d6b131d2bc8b 8193419: Better Internet address support
vtewari
parents: 49892
diff changeset
   347
    JNU_CHECK_EXCEPTION(env);
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   348
    if (family == java_net_InetAddress_IPv6 && !ipv6_available()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                        "Protocol family not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41594
diff changeset
   354
    fdc = family == java_net_InetAddress_IPv4 ? fd : fd1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
49892
8bed781a8d9c 8202154: Remove unused code in java.base/windows/native/libnet
igerasim
parents: 49446
diff changeset
   356
    /* SIO_UDP_CONNRESET fixes a bug introduced in Windows 2000, which
8bed781a8d9c 8202154: Remove unused code in java.base/windows/native/libnet
igerasim
parents: 49446
diff changeset
   357
     * returns connection reset errors on connected UDP sockets (as well
8bed781a8d9c 8202154: Remove unused code in java.base/windows/native/libnet
igerasim
parents: 49446
diff changeset
   358
     * as connected sockets). The solution is to only enable this feature
8bed781a8d9c 8202154: Remove unused code in java.base/windows/native/libnet
igerasim
parents: 49446
diff changeset
   359
     * when the socket is connected
8bed781a8d9c 8202154: Remove unused code in java.base/windows/native/libnet
igerasim
parents: 49446
diff changeset
   360
     */
8bed781a8d9c 8202154: Remove unused code in java.base/windows/native/libnet
igerasim
parents: 49446
diff changeset
   361
    t = TRUE;
8bed781a8d9c 8202154: Remove unused code in java.base/windows/native/libnet
igerasim
parents: 49446
diff changeset
   362
    res = WSAIoctl(fdc, SIO_UDP_CONNRESET, &t, sizeof(t), &x1, sizeof(x1), &x2, 0, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   364
    if (NET_InetAddressToSockaddr(env, address, port, &rmtaddr,
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   365
                                  &rmtaddrlen, JNI_FALSE) != 0) {
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   366
        return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   369
    if (connect(fdc, &rmtaddr.sa, rmtaddrlen) == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        NET_ThrowCurrent(env, "connect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
 * Class:     java_net_TwoStacksPlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
 * Method:    disconnect0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
Java_java_net_TwoStacksPlainDatagramSocketImpl_disconnect0(JNIEnv *env, jobject this, jint family) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    /* The object's field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    jobject fdObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    /* The fdObj'fd */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    jint fd, len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    SOCKETADDRESS addr;
49892
8bed781a8d9c 8202154: Remove unused code in java.base/windows/native/libnet
igerasim
parents: 49446
diff changeset
   388
    DWORD x1 = 0, x2 = 0; /* ignored result codes */
8bed781a8d9c 8202154: Remove unused code in java.base/windows/native/libnet
igerasim
parents: 49446
diff changeset
   389
    int t;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41594
diff changeset
   391
    if (family == java_net_InetAddress_IPv4) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41594
diff changeset
   393
        len = sizeof(struct sockaddr_in);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        fdObj = (*env)->GetObjectField(env, this, pdsi_fd1ID);
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41594
diff changeset
   396
        len = sizeof(struct sockaddr_in6);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        /* disconnect doesn't throw any exceptions */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
25551
62ad60d30644 8040810: Uninitialised memory in jdk/src/windows/native/java/net: net_util_md.c, TwoStacksPlainSocketImpl.c, TwoStacksPlainDatagramSocketImpl.c, DualStackPlainSocketImpl.c, DualStackPlainDatagramSocketImpl.c
msheppar
parents: 23015
diff changeset
   405
    memset((char *)&addr, 0, len);
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   406
    connect(fd, &addr.sa, len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * use SIO_UDP_CONNRESET
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * to disable ICMP port unreachable handling here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     */
49892
8bed781a8d9c 8202154: Remove unused code in java.base/windows/native/libnet
igerasim
parents: 49446
diff changeset
   412
    t = FALSE;
8bed781a8d9c 8202154: Remove unused code in java.base/windows/native/libnet
igerasim
parents: 49446
diff changeset
   413
    WSAIoctl(fd, SIO_UDP_CONNRESET, &t, sizeof(t), &x1, sizeof(x1), &x2, 0, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
 * Class:     java_net_TwoStacksPlainDatagramSocketImpl
55375
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents: 51153
diff changeset
   418
 * Method:    send0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
 * Signature: (Ljava/net/DatagramPacket;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
JNIEXPORT void JNICALL
55375
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents: 51153
diff changeset
   422
Java_java_net_TwoStacksPlainDatagramSocketImpl_send0
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   423
  (JNIEnv *env, jobject this, jobject packet)
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   424
{
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    char BUF[MAX_BUFFER_LEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    char *fullPacket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    jobject fdObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    jint fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    jobject iaObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    jint family;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    jint packetBufferOffset, packetBufferLen, packetPort;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    jbyteArray packetBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    jboolean connected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   437
    SOCKETADDRESS rmtaddr;
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   438
    struct sockaddr *addrp = 0;
25551
62ad60d30644 8040810: Uninitialised memory in jdk/src/windows/native/java/net: net_util_md.c, TwoStacksPlainSocketImpl.c, TwoStacksPlainDatagramSocketImpl.c, DualStackPlainSocketImpl.c, DualStackPlainDatagramSocketImpl.c
msheppar
parents: 23015
diff changeset
   439
    int addrlen = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    if (IS_NULL(packet)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        JNU_ThrowNullPointerException(env, "null packet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    iaObj = (*env)->GetObjectField(env, packet, dp_addressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    packetPort = (*env)->GetIntField(env, packet, dp_portID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    packetBufferOffset = (*env)->GetIntField(env, packet, dp_offsetID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    packetBuffer = (jbyteArray)(*env)->GetObjectField(env, packet, dp_bufID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    connected = (*env)->GetBooleanField(env, this, pdsi_connected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    if (IS_NULL(iaObj) || IS_NULL(packetBuffer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        JNU_ThrowNullPointerException(env, "null address || null buffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   458
    family = getInetAddress_family(env, iaObj);
51151
d6b131d2bc8b 8193419: Better Internet address support
vtewari
parents: 49892
diff changeset
   459
    JNU_CHECK_EXCEPTION(env);
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41594
diff changeset
   460
    if (family == java_net_InetAddress_IPv4) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        if (!ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                        "Protocol not allowed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        fdObj = (*env)->GetObjectField(env, this, pdsi_fd1ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    packetBufferLen = (*env)->GetIntField(env, packet, dp_lengthID);
17925
e8d88b57f82e 8008972: Memory leak: Java_java_net_TwoStacksPlainDatagramSocketImpl_receive0 [parfait]
jzavgren
parents: 16870
diff changeset
   479
    /* Note: the buffer needn't be greater than 65,536 (0xFFFF)...
e8d88b57f82e 8008972: Memory leak: Java_java_net_TwoStacksPlainDatagramSocketImpl_receive0 [parfait]
jzavgren
parents: 16870
diff changeset
   480
     * the maximum size of an IP packet. Anything bigger is truncated anyway.
e8d88b57f82e 8008972: Memory leak: Java_java_net_TwoStacksPlainDatagramSocketImpl_receive0 [parfait]
jzavgren
parents: 16870
diff changeset
   481
     */
e8d88b57f82e 8008972: Memory leak: Java_java_net_TwoStacksPlainDatagramSocketImpl_receive0 [parfait]
jzavgren
parents: 16870
diff changeset
   482
    if (packetBufferLen > MAX_PACKET_LEN) {
e8d88b57f82e 8008972: Memory leak: Java_java_net_TwoStacksPlainDatagramSocketImpl_receive0 [parfait]
jzavgren
parents: 16870
diff changeset
   483
        packetBufferLen = MAX_PACKET_LEN;
e8d88b57f82e 8008972: Memory leak: Java_java_net_TwoStacksPlainDatagramSocketImpl_receive0 [parfait]
jzavgren
parents: 16870
diff changeset
   484
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   486
    // sockaddr arg to sendto() is null if already connected
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   487
    if (!connected) {
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   488
        if (NET_InetAddressToSockaddr(env, iaObj, packetPort, &rmtaddr,
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   489
                                      &addrlen, JNI_FALSE) != 0) {
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   490
            return;
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   491
        }
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   492
        addrp = &rmtaddr.sa;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    if (packetBufferLen > MAX_BUFFER_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        /* When JNI-ifying the JDK's IO routines, we turned
17925
e8d88b57f82e 8008972: Memory leak: Java_java_net_TwoStacksPlainDatagramSocketImpl_receive0 [parfait]
jzavgren
parents: 16870
diff changeset
   497
         * reads and writes of byte arrays of size greater
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
         * than 2048 bytes into several operations of size 2048.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
         * This saves a malloc()/memcpy()/free() for big
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
         * buffers.  This is OK for file IO and TCP, but that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
         * strategy violates the semantics of a datagram protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
         * (one big send) != (several smaller sends).  So here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
         * we *must* alloc the buffer.  Note it needn't be bigger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
         * than 65,536 (0xFFFF) the max size of an IP packet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
         * anything bigger is truncated anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        fullPacket = (char *)malloc(packetBufferLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        if (!fullPacket) {
13245
7ab3ef5b9520 7181353: Update error message to distinguish native OOM and java OOM in net
zhouyx
parents: 10693
diff changeset
   509
            JNU_ThrowOutOfMemoryError(env, "Send buf native heap allocation failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        fullPacket = &(BUF[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   516
    (*env)->GetByteArrayRegion(env, packetBuffer, packetBufferOffset,
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   517
                               packetBufferLen, (jbyte *)fullPacket);
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   518
    if (sendto(fd, fullPacket, packetBufferLen, 0, addrp,
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   519
               addrlen) == SOCKET_ERROR)
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   520
    {
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   521
        NET_ThrowCurrent(env, "Datagram send failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    if (packetBufferLen > MAX_BUFFER_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        free(fullPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
 * check which socket was last serviced when there was data on both sockets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
 * Only call this if sure that there is data on both sockets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
static int checkLastFD (JNIEnv *env, jobject this, int fd, int fd1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    int nextfd, lastfd = (*env)->GetIntField(env, this, pdsi_lastfdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    if (lastfd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        /* arbitrary. Choose fd */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        (*env)->SetIntField(env, this, pdsi_lastfdID, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        if (lastfd == fd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            nextfd = fd1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            nextfd = fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        (*env)->SetIntField(env, this, pdsi_lastfdID, nextfd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        return nextfd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
 * Class:     java_net_TwoStacksPlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
 * Method:    peek
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
 * Signature: (Ljava/net/InetAddress;)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
Java_java_net_TwoStacksPlainDatagramSocketImpl_peek(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                                           jobject addressObj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    jint timeout = (*env)->GetIntField(env, this, pdsi_timeoutID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    jint fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    /* The address and family fields of addressObj */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    jint address, family;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    int n;
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   567
    SOCKETADDRESS remote_addr;
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   568
    jint remote_addrsize = sizeof(SOCKETADDRESS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    char buf[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    BOOL retry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    jlong prevTime = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        if (fd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
           JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                           "socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
           return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    if (IS_NULL(addressObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        JNU_ThrowNullPointerException(env, "Null address in peek()");
41594
e54e81e9be7b 8168405: Pending exceptions in java.base/windows/native
prappo
parents: 41380
diff changeset
   586
        return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    } else {
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   588
        address = getInetAddress_addr(env, addressObj);
51151
d6b131d2bc8b 8193419: Better Internet address support
vtewari
parents: 49892
diff changeset
   589
        JNU_CHECK_EXCEPTION_RETURN(env, -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        /* We only handle IPv4 for now. Will support IPv6 once its in the os */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        family = AF_INET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        retry = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
         * If a timeout has been specified then we select on the socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
         * waiting for a read event or a timeout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        if (timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            int ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            prevTime = JVM_CurrentTimeMillis(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            ret = NET_Timeout (fd, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            if (ret == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                                "Peek timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                return ret;
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   609
            } else if (ret == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                NET_ThrowCurrent(env, "timeout in datagram socket peek");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        /* now try the peek */
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   616
        n = recvfrom(fd, buf, 1, MSG_PEEK, &remote_addr.sa, &remote_addrsize);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   618
        if (n == SOCKET_ERROR) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            if (WSAGetLastError() == WSAECONNRESET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                jboolean connected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                 * An icmp port unreachable - we must receive this as Windows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                 * does not reset the state of the socket until this has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                 * received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                purgeOutstandingICMP(env, this, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                connected =  (*env)->GetBooleanField(env, this, pdsi_connected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                if (connected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                    JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                                       "ICMP Port Unreachable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                 * If a timeout was specified then we need to adjust it because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                 * we may have used up some of the timeout befor the icmp port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                 * unreachable arrived.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                if (timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                    jlong newTime = JVM_CurrentTimeMillis(env, 0);
910
1f53246fb014 6729881: Compiler warning in networking native code
chegar
parents: 2
diff changeset
   643
                    timeout -= (jint)(newTime - prevTime);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                    if (timeout <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                                "Receive timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                    prevTime = newTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                /* Need to retry the recv */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                retry = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    } while (retry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   658
    if (n == SOCKET_ERROR && WSAGetLastError() != WSAEMSGSIZE) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        NET_ThrowCurrent(env, "Datagram peek failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    }
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   662
    setInetAddress_addr(env, addressObj, ntohl(remote_addr.sa4.sin_addr.s_addr));
51151
d6b131d2bc8b 8193419: Better Internet address support
vtewari
parents: 49892
diff changeset
   663
    JNU_CHECK_EXCEPTION_RETURN(env, -1);
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41594
diff changeset
   664
    setInetAddress_family(env, addressObj, java_net_InetAddress_IPv4);
51151
d6b131d2bc8b 8193419: Better Internet address support
vtewari
parents: 49892
diff changeset
   665
    JNU_CHECK_EXCEPTION_RETURN(env, -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    /* return port */
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   668
    return ntohs(remote_addr.sa4.sin_port);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
Java_java_net_TwoStacksPlainDatagramSocketImpl_peekData(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                                           jobject packet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     char BUF[MAX_BUFFER_LEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    char *fullPacket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    jobject fd1Obj = (*env)->GetObjectField(env, this, pdsi_fd1ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    jint timeout = (*env)->GetIntField(env, this, pdsi_timeoutID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    jbyteArray packetBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    jint packetBufferOffset, packetBufferLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   684
    int fd = -1, fd1 = -1, fduse, nsockets = 0, errorCode;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
910
1f53246fb014 6729881: Compiler warning in networking native code
chegar
parents: 2
diff changeset
   687
    int checkBoth = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    SOCKETADDRESS remote_addr;
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   690
    jint remote_addrsize = sizeof(SOCKETADDRESS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    BOOL retry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    jlong prevTime = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    if (!IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        if (fd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
           JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                           "socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
           return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        nsockets = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    if (!IS_NULL(fd1Obj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        fd1 = (*env)->GetIntField(env, fd1Obj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        if (fd1 < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
           JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                           "socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
           return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        nsockets ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    switch (nsockets) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
      case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                       "socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
      case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        if (!IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
           fduse = fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
           fduse = fd1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
      case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        checkBoth = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    if (IS_NULL(packet)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        JNU_ThrowNullPointerException(env, "packet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    packetBuffer = (*env)->GetObjectField(env, packet, dp_bufID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    if (IS_NULL(packetBuffer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        JNU_ThrowNullPointerException(env, "packet buffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    packetBufferOffset = (*env)->GetIntField(env, packet, dp_offsetID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    packetBufferLen = (*env)->GetIntField(env, packet, dp_bufLengthID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    if (packetBufferLen > MAX_BUFFER_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        /* When JNI-ifying the JDK's IO routines, we turned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
         * read's and write's of byte arrays of size greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
         * than 2048 bytes into several operations of size 2048.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
         * This saves a malloc()/memcpy()/free() for big
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
         * buffers.  This is OK for file IO and TCP, but that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
         * strategy violates the semantics of a datagram protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
         * (one big send) != (several smaller sends).  So here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
         * we *must* alloc the buffer.  Note it needn't be bigger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
         * than 65,536 (0xFFFF) the max size of an IP packet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
         * anything bigger is truncated anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        fullPacket = (char *)malloc(packetBufferLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        if (!fullPacket) {
13245
7ab3ef5b9520 7181353: Update error message to distinguish native OOM and java OOM in net
zhouyx
parents: 10693
diff changeset
   761
            JNU_ThrowOutOfMemoryError(env, "Native heap allocation failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        fullPacket = &(BUF[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        int ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        retry = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
         * If a timeout has been specified then we select on the socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
         * waiting for a read event or a timeout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        if (checkBoth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            int t = timeout == 0 ? -1: timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            prevTime = JVM_CurrentTimeMillis(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            ret = NET_Timeout2 (fd, fd1, t, &fduse);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            /* all subsequent calls to recv() or select() will use the same fd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
             * for this call to peek() */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            if (ret <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                if (ret == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                    JNU_ThrowByName(env,JNU_JAVANETPKG "SocketTimeoutException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                                        "Peek timed out");
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   786
                } else if (ret == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                    NET_ThrowCurrent(env, "timeout in datagram socket peek");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                if (packetBufferLen > MAX_BUFFER_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                    free(fullPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            if (ret == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                fduse = checkLastFD (env, this, fd, fd1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            checkBoth = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        } else if (timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            if (prevTime == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                prevTime = JVM_CurrentTimeMillis(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            ret = NET_Timeout (fduse, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            if (ret <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                if (ret == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                    JNU_ThrowByName(env,JNU_JAVANETPKG "SocketTimeoutException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                                    "Receive timed out");
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   807
                } else if (ret == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                    JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                                    "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                if (packetBufferLen > MAX_BUFFER_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                    free(fullPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        /* receive the packet */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        n = recvfrom(fduse, fullPacket, packetBufferLen, MSG_PEEK,
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   820
                     &remote_addr.sa, &remote_addrsize);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        port = (int) ntohs ((u_short) GET_PORT((SOCKETADDRESS *)&remote_addr));
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   822
        if (n == SOCKET_ERROR) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            if (WSAGetLastError() == WSAECONNRESET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                jboolean connected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                 * An icmp port unreachable - we must receive this as Windows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                 * does not reset the state of the socket until this has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                 * received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                purgeOutstandingICMP(env, this, fduse);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                connected = (*env)->GetBooleanField(env, this, pdsi_connected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                if (connected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                    JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                                       "ICMP Port Unreachable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                    if (packetBufferLen > MAX_BUFFER_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                        free(fullPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                 * If a timeout was specified then we need to adjust it because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                 * we may have used up some of the timeout befor the icmp port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                 * unreachable arrived.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                if (timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                    jlong newTime = JVM_CurrentTimeMillis(env, 0);
910
1f53246fb014 6729881: Compiler warning in networking native code
chegar
parents: 2
diff changeset
   851
                    timeout -= (jint)(newTime - prevTime);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                    if (timeout <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                                "Receive timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                        if (packetBufferLen > MAX_BUFFER_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                            free(fullPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                    prevTime = newTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                retry = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    } while (retry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    /* truncate the data if the packet's length is too small */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    if (n > packetBufferLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        n = packetBufferLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
    if (n < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        errorCode = WSAGetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        /* check to see if it's because the buffer was too small */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        if (errorCode == WSAEMSGSIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            /* it is because the buffer is too small. It's UDP, it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
             * unreliable, it's all good. discard the rest of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
             * data..
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            n = packetBufferLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            /* failure */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
            (*env)->SetIntField(env, packet, dp_lengthID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    if (n == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "socket closed");
41594
e54e81e9be7b 8168405: Pending exceptions in java.base/windows/native
prappo
parents: 41380
diff changeset
   887
        if (packetBufferLen > MAX_BUFFER_LEN) {
e54e81e9be7b 8168405: Pending exceptions in java.base/windows/native
prappo
parents: 41380
diff changeset
   888
            free(fullPacket);
e54e81e9be7b 8168405: Pending exceptions in java.base/windows/native
prappo
parents: 41380
diff changeset
   889
        }
e54e81e9be7b 8168405: Pending exceptions in java.base/windows/native
prappo
parents: 41380
diff changeset
   890
        return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
    } else if (n == -2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                        "operation interrupted");
41594
e54e81e9be7b 8168405: Pending exceptions in java.base/windows/native
prappo
parents: 41380
diff changeset
   894
        if (packetBufferLen > MAX_BUFFER_LEN) {
e54e81e9be7b 8168405: Pending exceptions in java.base/windows/native
prappo
parents: 41380
diff changeset
   895
            free(fullPacket);
e54e81e9be7b 8168405: Pending exceptions in java.base/windows/native
prappo
parents: 41380
diff changeset
   896
        }
e54e81e9be7b 8168405: Pending exceptions in java.base/windows/native
prappo
parents: 41380
diff changeset
   897
        return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    } else if (n < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        NET_ThrowCurrent(env, "Datagram receive failed");
41594
e54e81e9be7b 8168405: Pending exceptions in java.base/windows/native
prappo
parents: 41380
diff changeset
   900
        if (packetBufferLen > MAX_BUFFER_LEN) {
e54e81e9be7b 8168405: Pending exceptions in java.base/windows/native
prappo
parents: 41380
diff changeset
   901
            free(fullPacket);
e54e81e9be7b 8168405: Pending exceptions in java.base/windows/native
prappo
parents: 41380
diff changeset
   902
        }
e54e81e9be7b 8168405: Pending exceptions in java.base/windows/native
prappo
parents: 41380
diff changeset
   903
        return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        jobject packetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
         * Check if there is an InetAddress already associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
         * packet. If so we check if it is the same source address. We
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
         * can't update any existing InetAddress because it is immutable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        packetAddress = (*env)->GetObjectField(env, packet, dp_addressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        if (packetAddress != NULL) {
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   914
            if (!NET_SockaddrEqualsInetAddress(env, &remote_addr,
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   915
                                               packetAddress)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                /* force a new InetAddress to be created */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                packetAddress = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        if (packetAddress == NULL) {
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
   921
            packetAddress = NET_SockaddrToInetAddress(env, &remote_addr,
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   922
                                                      &port);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            /* stuff the new Inetaddress in the packet */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            (*env)->SetObjectField(env, packet, dp_addressID, packetAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        /* populate the packet */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        (*env)->SetByteArrayRegion(env, packetBuffer, packetBufferOffset, n,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                                   (jbyte *)fullPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        (*env)->SetIntField(env, packet, dp_portID, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        (*env)->SetIntField(env, packet, dp_lengthID, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
    /* make sure receive() picks up the right fd */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    (*env)->SetIntField(env, this, pdsi_fduseID, fduse);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
    if (packetBufferLen > MAX_BUFFER_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        free(fullPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
    return port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
 * Class:     java_net_TwoStacksPlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
 * Method:    receive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
 * Signature: (Ljava/net/DatagramPacket;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
Java_java_net_TwoStacksPlainDatagramSocketImpl_receive0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                                              jobject packet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
    char BUF[MAX_BUFFER_LEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    char *fullPacket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    jobject fd1Obj = (*env)->GetObjectField(env, this, pdsi_fd1ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
    jint timeout = (*env)->GetIntField(env, this, pdsi_timeoutID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    jbyteArray packetBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
    jint packetBufferOffset, packetBufferLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    int ipv6_supported = ipv6_available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
    /* as a result of the changes for ipv6, peek() or peekData()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * must be called prior to receive() so that fduse can be set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     */
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   964
    int fd = -1, fd1 = -1, fduse, errorCode;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
    int n, nsockets=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
    SOCKETADDRESS remote_addr;
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   968
    jint remote_addrsize = sizeof(SOCKETADDRESS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
    BOOL retry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
    jlong prevTime = 0, selectTime=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
    jboolean connected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
    if (IS_NULL(fdObj) && IS_NULL(fd1Obj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
    if (!IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        nsockets ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
    if (!IS_NULL(fd1Obj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        fd1 = (*env)->GetIntField(env, fd1Obj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        nsockets ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    if (nsockets == 2) { /* need to choose one of them */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        /* was fduse set in peek? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        fduse = (*env)->GetIntField(env, this, pdsi_fduseID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        if (fduse == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            /* not set in peek(), must select on both sockets */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
            int ret, t = (timeout == 0) ? -1: timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            ret = NET_Timeout2 (fd, fd1, t, &fduse);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
            if (ret == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                fduse = checkLastFD (env, this, fd, fd1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            } else if (ret <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                if (ret == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                    JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                                    "Receive timed out");
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1001
                } else if (ret == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                    JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                                    "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
    } else if (!ipv6_supported) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        fduse = fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
    } else if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        /* ipv6 supported: and this socket bound to an IPV6 only address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        fduse = fd1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        /* ipv6 supported: and this socket bound to an IPV4 only address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        fduse = fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
    if (IS_NULL(packet)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        JNU_ThrowNullPointerException(env, "packet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
    packetBuffer = (*env)->GetObjectField(env, packet, dp_bufID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
    if (IS_NULL(packetBuffer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        JNU_ThrowNullPointerException(env, "packet buffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
    packetBufferOffset = (*env)->GetIntField(env, packet, dp_offsetID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
    packetBufferLen = (*env)->GetIntField(env, packet, dp_bufLengthID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
    if (packetBufferLen > MAX_BUFFER_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        /* When JNI-ifying the JDK's IO routines, we turned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
         * read's and write's of byte arrays of size greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
         * than 2048 bytes into several operations of size 2048.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
         * This saves a malloc()/memcpy()/free() for big
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
         * buffers.  This is OK for file IO and TCP, but that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
         * strategy violates the semantics of a datagram protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
         * (one big send) != (several smaller sends).  So here
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
         * we *must* alloc the buffer.  Note it needn't be bigger
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
         * than 65,536 (0xFFFF) the max size of an IP packet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
         * anything bigger is truncated anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        fullPacket = (char *)malloc(packetBufferLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        if (!fullPacket) {
13245
7ab3ef5b9520 7181353: Update error message to distinguish native OOM and java OOM in net
zhouyx
parents: 10693
diff changeset
  1048
            JNU_ThrowOutOfMemoryError(env, "Receive buf native heap allocation failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        fullPacket = &(BUF[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
    /*
49892
8bed781a8d9c 8202154: Remove unused code in java.base/windows/native/libnet
igerasim
parents: 49446
diff changeset
  1058
     * If we are not connected then we need to know if a timeout has been
8bed781a8d9c 8202154: Remove unused code in java.base/windows/native/libnet
igerasim
parents: 49446
diff changeset
  1059
     * specified and if so we need to pick up the current time. These are
8bed781a8d9c 8202154: Remove unused code in java.base/windows/native/libnet
igerasim
parents: 49446
diff changeset
  1060
     * required in order to implement the semantics of timeout, viz :-
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * timeout set to t1 but ICMP port unreachable arrives in t2 where
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * t2 < t1. In this case we must discard the ICMP packets and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * wait for the next packet up to a maximum of t1 minus t2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
    connected = (*env)->GetBooleanField(env, this, pdsi_connected);
49892
8bed781a8d9c 8202154: Remove unused code in java.base/windows/native/libnet
igerasim
parents: 49446
diff changeset
  1066
    if (!connected && timeout && !ipv6_supported) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        prevTime = JVM_CurrentTimeMillis(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    if (timeout && nsockets == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        int ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        ret = NET_Timeout(fduse, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        if (ret <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
            if (ret == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                                "Receive timed out");
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1077
            } else if (ret == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
                                "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
            if (packetBufferLen > MAX_BUFFER_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
                free(fullPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * Loop only if we discarding ICMP port unreachable packets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
    do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        retry = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        /* receive the packet */
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
  1095
        n = recvfrom(fduse, fullPacket, packetBufferLen, 0, &remote_addr.sa,
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
  1096
                     &remote_addrsize);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1098
        if (n == SOCKET_ERROR) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
            if (WSAGetLastError() == WSAECONNRESET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                 * An icmp port unreachable has been received - consume any other
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                 * outstanding packets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                purgeOutstandingICMP(env, this, fduse);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                 * If connected throw a PortUnreachableException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                if (connected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                    JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                                       "ICMP Port Unreachable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
                    if (packetBufferLen > MAX_BUFFER_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                        free(fullPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
                 * If a timeout was specified then we need to adjust it because
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                 * we may have used up some of the timeout before the icmp port
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
                 * unreachable arrived.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                if (timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                    int ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
                    jlong newTime = JVM_CurrentTimeMillis(env, 0);
910
1f53246fb014 6729881: Compiler warning in networking native code
chegar
parents: 2
diff changeset
  1129
                    timeout -= (jint)(newTime - prevTime);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
                    prevTime = newTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                    if (timeout <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                        ret = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                        ret = NET_Timeout(fduse, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                    if (ret <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                        if (ret == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
                            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
                                            "Receive timed out");
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1142
                        } else if (ret == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
                            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                                            "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
                        if (packetBufferLen > MAX_BUFFER_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
                            free(fullPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
                 * An ICMP port unreachable was received but we are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
                 * not connected so ignore it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
                retry = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
    } while (retry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
    /* truncate the data if the packet's length is too small */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
    if (n > packetBufferLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        n = packetBufferLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
    if (n < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        errorCode = WSAGetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        /* check to see if it's because the buffer was too small */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        if (errorCode == WSAEMSGSIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
            /* it is because the buffer is too small. It's UDP, it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
             * unreliable, it's all good. discard the rest of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
             * data..
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
            n = packetBufferLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
            /* failure */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
            (*env)->SetIntField(env, packet, dp_lengthID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
    if (n == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
    } else if (n == -2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
                        "operation interrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
    } else if (n < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        NET_ThrowCurrent(env, "Datagram receive failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
    } else {
25551
62ad60d30644 8040810: Uninitialised memory in jdk/src/windows/native/java/net: net_util_md.c, TwoStacksPlainSocketImpl.c, TwoStacksPlainDatagramSocketImpl.c, DualStackPlainSocketImpl.c, DualStackPlainDatagramSocketImpl.c
msheppar
parents: 23015
diff changeset
  1188
        int port = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        jobject packetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
         * Check if there is an InetAddress already associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
         * packet. If so we check if it is the same source address. We
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
         * can't update any existing InetAddress because it is immutable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        packetAddress = (*env)->GetObjectField(env, packet, dp_addressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        if (packetAddress != NULL) {
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
  1198
            if (!NET_SockaddrEqualsInetAddress(env, &remote_addr,
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
  1199
                                               packetAddress)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
                /* force a new InetAddress to be created */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
                packetAddress = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        if (packetAddress == NULL) {
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
  1205
            packetAddress = NET_SockaddrToInetAddress(env, &remote_addr,
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
  1206
                                                      &port);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
            /* stuff the new Inetaddress in the packet */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
            (*env)->SetObjectField(env, packet, dp_addressID, packetAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
            /* only get the new port number */
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
  1211
            port = NET_GetPortFromSockaddr(&remote_addr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        /* populate the packet */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        (*env)->SetByteArrayRegion(env, packetBuffer, packetBufferOffset, n,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                                   (jbyte *)fullPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
        (*env)->SetIntField(env, packet, dp_portID, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        (*env)->SetIntField(env, packet, dp_lengthID, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
    if (packetBufferLen > MAX_BUFFER_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        free(fullPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
 * Class:     java_net_TwoStacksPlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
 * Method:    datagramSocketCreate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
Java_java_net_TwoStacksPlainDatagramSocketImpl_datagramSocketCreate(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
                                                           jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
    jobject fd1Obj = (*env)->GetObjectField(env, this, pdsi_fd1ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
    int fd, fd1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
    int t = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
    DWORD x1, x2; /* ignored result codes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
    int ipv6_supported = ipv6_available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
    int arg = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
    if (IS_NULL(fdObj) || (ipv6_supported && IS_NULL(fd1Obj))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
        fd =  (int) socket (AF_INET, SOCK_DGRAM, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
    }
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1248
    if (fd == SOCKET_ERROR) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        NET_ThrowCurrent(env, "Socket creation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
    SetHandleInformation((HANDLE)(UINT_PTR)fd, HANDLE_FLAG_INHERIT, FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
    (*env)->SetIntField(env, fdObj, IO_fd_fdID, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
    NET_SetSockOpt(fd, SOL_SOCKET, SO_BROADCAST, (char*)&t, sizeof(BOOL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
    if (ipv6_supported) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        /* SIO_UDP_CONNRESET fixes a bug introduced in Windows 2000, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
         * returns connection reset errors un connected UDP sockets (as well
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
         * as connected sockets. The solution is to only enable this feature
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
         * when the socket is connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
        t = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
        WSAIoctl(fd,SIO_UDP_CONNRESET,&t,sizeof(t),&x1,sizeof(x1),&x2,0,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        t = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        fd1 = socket (AF_INET6, SOCK_DGRAM, 0);
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1266
        if (fd1 == SOCKET_ERROR) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
            NET_ThrowCurrent(env, "Socket creation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        NET_SetSockOpt(fd1, SOL_SOCKET, SO_BROADCAST, (char*)&t, sizeof(BOOL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
        t = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        WSAIoctl(fd1,SIO_UDP_CONNRESET,&t,sizeof(t),&x1,sizeof(x1),&x2,0,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
        (*env)->SetIntField(env, fd1Obj, IO_fd_fdID, fd1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
        SetHandleInformation((HANDLE)(UINT_PTR)fd1, HANDLE_FLAG_INHERIT, FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        /* drop the second fd */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        (*env)->SetObjectField(env, this, pdsi_fd1ID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
 * Class:     java_net_TwoStacksPlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
 * Method:    datagramSocketClose
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
Java_java_net_TwoStacksPlainDatagramSocketImpl_datagramSocketClose(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                                                          jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
     * REMIND: PUT A LOCK AROUND THIS CODE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
    jobject fd1Obj = (*env)->GetObjectField(env, this, pdsi_fd1ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
    int ipv6_supported = ipv6_available();
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
  1295
    int fd = -1, fd1 = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
    if (IS_NULL(fdObj) && (!ipv6_supported || IS_NULL(fd1Obj))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
    if (!IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        if (fd != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
            (*env)->SetIntField(env, fdObj, IO_fd_fdID, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
            NET_SocketClose(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
    if (ipv6_supported && fd1Obj != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        fd1 = (*env)->GetIntField(env, fd1Obj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
        if (fd1 == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
        (*env)->SetIntField(env, fd1Obj, IO_fd_fdID, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
        NET_SocketClose(fd1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
 * check the addresses attached to the NetworkInterface object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
 * and return the first one (of the requested family Ipv4 or Ipv6)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
 * in *iaddr
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
static int getInetAddrFromIf (JNIEnv *env, int family, jobject nif, jobject *iaddr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
    jobjectArray addrArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
    static jfieldID ni_addrsID=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
    jsize len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
    jobject addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1333
    if (ni_addrsID == NULL ) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
        jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        CHECK_NULL_RETURN (c, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
        ni_addrsID = (*env)->GetFieldID(env, c, "addrs",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
                                        "[Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
        CHECK_NULL_RETURN (ni_addrsID, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
    addrArray = (*env)->GetObjectField(env, nif, ni_addrsID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
    len = (*env)->GetArrayLength(env, addrArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     * Check that there is at least one address bound to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     * interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
    if (len < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
            "bad argument for IP_MULTICAST_IF2: No IP addresses bound to interface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
    for (i=0; i<len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
        int fam;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        addr = (*env)->GetObjectArrayElement(env, addrArray, i);
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1356
        fam = getInetAddress_family(env, addr);
51151
d6b131d2bc8b 8193419: Better Internet address support
vtewari
parents: 49892
diff changeset
  1357
        JNU_CHECK_EXCEPTION_RETURN(env, -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        if (fam == family) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
            *iaddr = addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
static int getInet4AddrFromIf (JNIEnv *env, jobject nif, struct in_addr *iaddr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
    jobject addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41594
diff changeset
  1370
    int ret = getInetAddrFromIf(env, java_net_InetAddress_IPv4, nif, &addr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
    if (ret == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1375
    iaddr->s_addr = htonl(getInetAddress_addr(env, addr));
51151
d6b131d2bc8b 8193419: Better Internet address support
vtewari
parents: 49892
diff changeset
  1376
    JNU_CHECK_EXCEPTION_RETURN(env, -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
/* Get the multicasting index from the interface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
static int getIndexFromIf (JNIEnv *env, jobject nif) {
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
  1383
    static jfieldID ni_indexID = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
    if (ni_indexID == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
        CHECK_NULL_RETURN(c, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
        ni_indexID = (*env)->GetFieldID(env, c, "index", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
        CHECK_NULL_RETURN(ni_indexID, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
    return (*env)->GetIntField(env, nif, ni_indexID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
19866
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1395
static int isAdapterIpv6Enabled(JNIEnv *env, int index) {
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1396
  netif *ifList, *curr;
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1397
  int ipv6Enabled = 0;
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
  1398
  if (getAllInterfacesAndAddresses(env, &ifList) < 0) {
19866
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1399
      return ipv6Enabled;
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1400
  }
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1401
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1402
  /* search by index */
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1403
  curr = ifList;
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1404
  while (curr != NULL) {
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1405
      if (index == curr->index) {
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1406
          break;
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1407
      }
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1408
      curr = curr->next;
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1409
  }
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1410
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1411
  /* if found ipv6Index != 0 then interface is configured with IPV6 */
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1412
  if ((curr != NULL) && (curr->ipv6Index !=0)) {
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1413
      ipv6Enabled = 1;
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1414
  }
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1415
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1416
  /* release the interface list */
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1417
  free_netif(ifList);
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1418
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1419
  return ipv6Enabled;
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1420
}
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1421
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
 * Sets the multicast interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
 * SocketOptions.IP_MULTICAST_IF (argument is an InetAddress) :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
 *      IPv4:   set outgoing multicast interface using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
 *              IPPROTO_IP/IP_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
 *      IPv6:   Get the interface to which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
 *              InetAddress is bound
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
 *              and do same as SockOptions.IF_MULTICAST_IF2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
 * SockOptions.IF_MULTICAST_IF2 (argument is a NetworkInterface ) :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
 *      For each stack:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
 *      IPv4:   Obtain IP address bound to network interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
 *              (NetworkInterface.addres[0])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
 *              set outgoing multicast interface using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
 *              IPPROTO_IP/IP_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
 *      IPv6:   Obtain NetworkInterface.index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
 *              Set outgoing multicast interface using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
 *              IPPROTO_IPV6/IPV6_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
static void setMulticastInterface(JNIEnv *env, jobject this, int fd, int fd1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
                                  jint opt, jobject value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
    int ipv6_supported = ipv6_available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
    if (opt == java_net_SocketOptions_IP_MULTICAST_IF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
         * value is an InetAddress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
         * On IPv4 system use IP_MULTICAST_IF socket option
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
         * On IPv6 system get the NetworkInterface that this IP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
         * address is bound to and use the IPV6_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
         * option instead of IP_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
        if (ipv6_supported) {
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
  1459
            static jclass ni_class = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
            if (ni_class == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
                jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
                CHECK_NULL(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
                ni_class = (*env)->NewGlobalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
                CHECK_NULL(ni_class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
            value = Java_java_net_NetworkInterface_getByInetAddress0(env, ni_class, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
            if (value == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
                if (!(*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
                    JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
                         "bad argument for IP_MULTICAST_IF"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
                         ": address not bound to any interface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
            opt = java_net_SocketOptions_IP_MULTICAST_IF2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
            struct in_addr in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1480
            in.s_addr = htonl(getInetAddress_addr(env, value));
51151
d6b131d2bc8b 8193419: Better Internet address support
vtewari
parents: 49892
diff changeset
  1481
            JNU_CHECK_EXCEPTION(env);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
            if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
                               (const char*)&in, sizeof(in)) < 0) {
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 32846
diff changeset
  1484
                JNU_ThrowByNameWithMessageAndLastError
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 32846
diff changeset
  1485
                    (env, JNU_JAVANETPKG "SocketException", "Error setting socket option");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
    if (opt == java_net_SocketOptions_IP_MULTICAST_IF2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
         * value is a NetworkInterface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
         * On IPv6 system get the index of the interface and use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
         * IPV6_MULTICAST_IF socket option
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
         * On IPv4 system extract addr[0] and use the IP_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
         * option. For IPv6 both must be done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
        if (ipv6_supported) {
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
  1500
            static jfieldID ni_indexID = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
            struct in_addr in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
            int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
            if (ni_indexID == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
                jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
                CHECK_NULL(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
                ni_indexID = (*env)->GetFieldID(env, c, "index", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
                CHECK_NULL(ni_indexID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
            index = (*env)->GetIntField(env, value, ni_indexID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 32846
diff changeset
  1512
            if (isAdapterIpv6Enabled(env, index) != 0) {
19866
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1513
                if (setsockopt(fd1, IPPROTO_IPV6, IPV6_MULTICAST_IF,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
                               (const char*)&index, sizeof(index)) < 0) {
19866
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1515
                    if (errno == EINVAL && index > 0) {
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1516
                        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1517
                            "IPV6_MULTICAST_IF failed (interface has IPv4 "
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1518
                            "address only?)");
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1519
                    } else {
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 32846
diff changeset
  1520
                        JNU_ThrowByNameWithMessageAndLastError
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 32846
diff changeset
  1521
                            (env, JNU_JAVANETPKG "SocketException", "Error setting socket option");
19866
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1522
                    }
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1523
                    return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
            /* If there are any IPv4 addresses on this interface then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
             * repeat the operation on the IPv4 fd */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 32846
diff changeset
  1529
            if (getInet4AddrFromIf(env, value, &in) < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
            if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
                               (const char*)&in, sizeof(in)) < 0) {
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 32846
diff changeset
  1534
                JNU_ThrowByNameWithMessageAndLastError
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 32846
diff changeset
  1535
                    (env, JNU_JAVANETPKG "SocketException", "Error setting socket option");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
            struct in_addr in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
            if (getInet4AddrFromIf (env, value, &in) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
                if ((*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
                JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
                        "no InetAddress instances of requested type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
            if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
                               (const char*)&in, sizeof(in)) < 0) {
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 32846
diff changeset
  1552
                JNU_ThrowByNameWithMessageAndLastError
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 32846
diff changeset
  1553
                    (env, JNU_JAVANETPKG "SocketException", "Error setting socket option");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
 * Class:     java_net_TwoStacksPlainDatagramSocketImpl
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16870
diff changeset
  1562
 * Method:    socketNativeSetOption
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
 * Signature: (ILjava/lang/Object;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
JNIEXPORT void JNICALL
41214
855ac576eb77 8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents: 39318
diff changeset
  1566
Java_java_net_TwoStacksPlainDatagramSocketImpl_socketNativeSetOption
855ac576eb77 8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents: 39318
diff changeset
  1567
  (JNIEnv *env,jobject this, jint opt,jobject value)
855ac576eb77 8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents: 39318
diff changeset
  1568
{
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
  1569
    int fd = -1, fd1 = -1;
25551
62ad60d30644 8040810: Uninitialised memory in jdk/src/windows/native/java/net: net_util_md.c, TwoStacksPlainSocketImpl.c, TwoStacksPlainDatagramSocketImpl.c, DualStackPlainSocketImpl.c, DualStackPlainDatagramSocketImpl.c
msheppar
parents: 23015
diff changeset
  1570
    int levelv4 = 0, levelv6 = 0, optnamev4 = 0, optnamev6 = 0, optlen = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
    union {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
        char c;
25551
62ad60d30644 8040810: Uninitialised memory in jdk/src/windows/native/java/net: net_util_md.c, TwoStacksPlainSocketImpl.c, TwoStacksPlainDatagramSocketImpl.c, DualStackPlainSocketImpl.c, DualStackPlainDatagramSocketImpl.c
msheppar
parents: 23015
diff changeset
  1574
    } optval = { 0 };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
    int ipv6_supported = ipv6_available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
    fd = getFD(env, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
    if (ipv6_supported) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
        fd1 = getFD1(env, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
    if (fd < 0 && fd1 < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
    if ((opt == java_net_SocketOptions_IP_MULTICAST_IF) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
        (opt == java_net_SocketOptions_IP_MULTICAST_IF2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
        setMulticastInterface(env, this, fd, fd1, opt, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
     * Map the Java level socket option to the platform specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
     * level(s) and option name(s).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
    if (fd1 != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
        if (NET_MapSocketOptionV6(opt, &levelv6, &optnamev6)) {
41214
855ac576eb77 8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents: 39318
diff changeset
  1599
            JNU_ThrowByName(env, "java/net/SocketException", "Invalid option");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
    if (fd != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
        if (NET_MapSocketOption(opt, &levelv4, &optnamev4)) {
41214
855ac576eb77 8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents: 39318
diff changeset
  1605
            JNU_ThrowByName(env, "java/net/SocketException", "Invalid option");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
    switch (opt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
        case java_net_SocketOptions_SO_SNDBUF :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
        case java_net_SocketOptions_SO_RCVBUF :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
        case java_net_SocketOptions_IP_TOS :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
                jclass cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
                jfieldID fid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
                cls = (*env)->FindClass(env, "java/lang/Integer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
                CHECK_NULL(cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
                fid =  (*env)->GetFieldID(env, cls, "value", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
                CHECK_NULL(fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
                optval.i = (*env)->GetIntField(env, value, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
                optlen = sizeof(optval.i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
        case java_net_SocketOptions_SO_REUSEADDR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
        case java_net_SocketOptions_SO_BROADCAST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
        case java_net_SocketOptions_IP_MULTICAST_LOOP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
                jclass cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
                jfieldID fid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
                jboolean on;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
                cls = (*env)->FindClass(env, "java/lang/Boolean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
                CHECK_NULL(cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
                fid =  (*env)->GetFieldID(env, cls, "value", "Z");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
                CHECK_NULL(fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
                on = (*env)->GetBooleanField(env, value, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
                optval.i = (on ? 1 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
                 * setLoopbackMode (true) disables IP_MULTICAST_LOOP rather
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
                 * than enabling it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
                if (opt == java_net_SocketOptions_IP_MULTICAST_LOOP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
                    optval.i = !optval.i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
                optlen = sizeof(optval.i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
        default :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
                "Socket option not supported by PlainDatagramSocketImp");
41594
e54e81e9be7b 8168405: Pending exceptions in java.base/windows/native
prappo
parents: 41380
diff changeset
  1657
            return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
    if (fd1 != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
        if (NET_SetSockOpt(fd1, levelv6, optnamev6, (void *)&optval, optlen) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
            NET_ThrowCurrent(env, "setsockopt IPv6");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
    if (fd != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
        if (NET_SetSockOpt(fd, levelv4, optnamev4, (void *)&optval, optlen) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
            NET_ThrowCurrent(env, "setsockopt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
 *
19866
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1677
 * called by getMulticastInterface to retrieve a NetworkInterface
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1678
 * configured for IPv4.
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1679
 * The ipv4Mode parameter, is a closet boolean, which allows for a NULL return,
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1680
 * or forces the creation of a NetworkInterface object with null data.
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1681
 * It relates to its calling context in getMulticastInterface.
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1682
 * ipv4Mode == 1, the context is IPV4 processing only.
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1683
 * ipv4Mode == 0, the context is IPV6 processing
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1684
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
 */
19866
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1686
static jobject getIPv4NetworkInterface (JNIEnv *env, jobject this, int fd, jint opt, int ipv4Mode) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
        static jclass inet4_class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
        static jmethodID inet4_ctrID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
19866
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1690
        static jclass ni_class; static jmethodID ni_ctrID;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
        static jfieldID ni_indexID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
        static jfieldID ni_addrsID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
        jobject addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
        jobject ni;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
        struct in_addr in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
        struct in_addr *inP = &in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
        int len = sizeof(struct in_addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
        if (getsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
                           (char *)inP, &len) < 0) {
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 32846
diff changeset
  1702
            JNU_ThrowByNameWithMessageAndLastError
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 32846
diff changeset
  1703
                (env, JNU_JAVANETPKG "SocketException", "Error getting socket option");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
         * Construct and populate an Inet4Address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
        if (inet4_class == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
            jclass c = (*env)->FindClass(env, "java/net/Inet4Address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
            CHECK_NULL_RETURN(c, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
            inet4_ctrID = (*env)->GetMethodID(env, c, "<init>", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
            CHECK_NULL_RETURN(inet4_ctrID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
            inet4_class = (*env)->NewGlobalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
            CHECK_NULL_RETURN(inet4_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
        addr = (*env)->NewObject(env, inet4_class, inet4_ctrID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
        CHECK_NULL_RETURN(addr, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1721
        setInetAddress_addr(env, addr, ntohl(in.s_addr));
51151
d6b131d2bc8b 8193419: Better Internet address support
vtewari
parents: 49892
diff changeset
  1722
        JNU_CHECK_EXCEPTION_RETURN(env, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
         * For IP_MULTICAST_IF return InetAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
        if (opt == java_net_SocketOptions_IP_MULTICAST_IF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
            return addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
         * For IP_MULTICAST_IF2 we get the NetworkInterface for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
         * this address and return it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
        if (ni_class == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
            jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
            CHECK_NULL_RETURN(c, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
            ni_ctrID = (*env)->GetMethodID(env, c, "<init>", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
            CHECK_NULL_RETURN(ni_ctrID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
            ni_indexID = (*env)->GetFieldID(env, c, "index", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
            CHECK_NULL_RETURN(ni_indexID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
            ni_addrsID = (*env)->GetFieldID(env, c, "addrs",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
                                            "[Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
            CHECK_NULL_RETURN(ni_addrsID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
            ni_class = (*env)->NewGlobalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
            CHECK_NULL_RETURN(ni_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
        ni = Java_java_net_NetworkInterface_getByInetAddress0(env, ni_class, addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
        if (ni) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
            return ni;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
        }
59313
6f12009ea9d7 8233307: MulticastSocket getOption(IP_MULTICAST_IF) returns interface when not set
pconcannon
parents: 55375
diff changeset
  1751
        return NULL;
19866
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1752
}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
19866
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1754
/*
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1755
 * Return the multicast interface:
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1756
 *
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1757
 * SocketOptions.IP_MULTICAST_IF
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1758
 *      IPv4:   Query IPPROTO_IP/IP_MULTICAST_IF
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1759
 *              Create InetAddress
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1760
 *              IP_MULTICAST_IF returns struct ip_mreqn on 2.2
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1761
 *              kernel but struct in_addr on 2.4 kernel
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1762
 *      IPv6:   Query IPPROTO_IPV6 / IPV6_MULTICAST_IF or
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1763
 *              obtain from impl is Linux 2.2 kernel
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1764
 *              If index == 0 return InetAddress representing
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1765
 *              anyLocalAddress.
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1766
 *              If index > 0 query NetworkInterface by index
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1767
 *              and returns addrs[0]
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1768
 *
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1769
 * SocketOptions.IP_MULTICAST_IF2
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1770
 *      IPv4:   Query IPPROTO_IP/IP_MULTICAST_IF
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1771
 *              Query NetworkInterface by IP address and
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1772
 *              return the NetworkInterface that the address
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1773
 *              is bound too.
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1774
 *      IPv6:   Query IPPROTO_IPV6 / IPV6_MULTICAST_IF
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1775
 *              (except Linux .2 kernel)
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1776
 *              Query NetworkInterface by index and
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1777
 *              return NetworkInterface.
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1778
 */
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1779
jobject getMulticastInterface(JNIEnv *env, jobject this, int fd, int fd1, jint opt) {
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1780
    jboolean isIPV4 = !ipv6_available() || fd1 == -1;
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1781
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1782
    /*
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1783
     * IPv4 implementation
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1784
     */
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1785
    if (isIPV4) {
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1786
        jobject netObject = NULL; // return is either an addr or a netif
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1787
        netObject = getIPv4NetworkInterface(env, this, fd, opt, 1);
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1788
        return netObject;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
     * IPv6 implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
    if ((opt == java_net_SocketOptions_IP_MULTICAST_IF) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
        (opt == java_net_SocketOptions_IP_MULTICAST_IF2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
        static jclass ni_class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
        static jmethodID ni_ctrID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
        static jfieldID ni_indexID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
        static jfieldID ni_addrsID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
        static jclass ia_class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
        static jmethodID ia_anyLocalAddressID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
        int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
        int len = sizeof(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
        jobjectArray addrArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
        jobject addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
        jobject ni;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
            if (getsockopt(fd1, IPPROTO_IPV6, IPV6_MULTICAST_IF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
                               (char*)&index, &len) < 0) {
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 32846
diff changeset
  1814
                JNU_ThrowByNameWithMessageAndLastError
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 32846
diff changeset
  1815
                    (env, JNU_JAVANETPKG "SocketException", "Error getting socket option");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
                return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
        if (ni_class == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
            jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
            CHECK_NULL_RETURN(c, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
            ni_ctrID = (*env)->GetMethodID(env, c, "<init>", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
            CHECK_NULL_RETURN(ni_ctrID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
            ni_indexID = (*env)->GetFieldID(env, c, "index", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
            CHECK_NULL_RETURN(ni_indexID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
            ni_addrsID = (*env)->GetFieldID(env, c, "addrs",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
                                            "[Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
            CHECK_NULL_RETURN(ni_addrsID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
            ia_class = (*env)->FindClass(env, "java/net/InetAddress");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
            CHECK_NULL_RETURN(ia_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
            ia_class = (*env)->NewGlobalRef(env, ia_class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
            CHECK_NULL_RETURN(ia_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
            ia_anyLocalAddressID = (*env)->GetStaticMethodID(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
                                                             ia_class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
                                                             "anyLocalAddress",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
                                                             "()Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
            CHECK_NULL_RETURN(ia_anyLocalAddressID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
            ni_class = (*env)->NewGlobalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
            CHECK_NULL_RETURN(ni_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
         * If multicast to a specific interface then return the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
         * interface (for IF2) or the any address on that interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
         * (for IF).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
        if (index > 0) {
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 910
diff changeset
  1850
            ni = Java_java_net_NetworkInterface_getByIndex0(env, ni_class,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
                                                                   index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
            if (ni == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
                char errmsg[255];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
                sprintf(errmsg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
                        "IPV6_MULTICAST_IF returned index to unrecognized interface: %d",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
                        index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
                JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", errmsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
                return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
             * For IP_MULTICAST_IF2 return the NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
            if (opt == java_net_SocketOptions_IP_MULTICAST_IF2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
                return ni;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
             * For IP_MULTICAST_IF return addrs[0]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
            addrArray = (*env)->GetObjectField(env, ni, ni_addrsID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
            if ((*env)->GetArrayLength(env, addrArray) < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
                JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
                    "IPV6_MULTICAST_IF returned interface without IP bindings");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
                return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
            addr = (*env)->GetObjectArrayElement(env, addrArray, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
            return addr;
19866
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1880
        } else if (index == 0) { // index == 0 typically means IPv6 not configured on the interfaces
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1881
            // falling back to treat interface as configured for IPv4
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1882
            jobject netObject = NULL;
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1883
            netObject = getIPv4NetworkInterface(env, this, fd, opt, 0);
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1884
            if (netObject != NULL) {
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1885
                return netObject;
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1886
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
}
19866
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1891
f5e8a8275078 6458027: Disabling IPv6 on a specific network interface causes problems
msheppar
parents: 18253
diff changeset
  1892
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
 * Returns relevant info as a jint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
 * Class:     java_net_TwoStacksPlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
 * Method:    socketGetOption
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
 * Signature: (I)Ljava/lang/Object;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
JNIEXPORT jobject JNICALL
41214
855ac576eb77 8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents: 39318
diff changeset
  1901
Java_java_net_TwoStacksPlainDatagramSocketImpl_socketGetOption
855ac576eb77 8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents: 39318
diff changeset
  1902
  (JNIEnv *env, jobject this, jint opt)
855ac576eb77 8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents: 39318
diff changeset
  1903
{
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
  1904
    int fd = -1, fd1 = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
    int level, optname, optlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
    union {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
        int i;
25551
62ad60d30644 8040810: Uninitialised memory in jdk/src/windows/native/java/net: net_util_md.c, TwoStacksPlainSocketImpl.c, TwoStacksPlainDatagramSocketImpl.c, DualStackPlainSocketImpl.c, DualStackPlainDatagramSocketImpl.c
msheppar
parents: 23015
diff changeset
  1908
    } optval = {0};
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
    int ipv6_supported = ipv6_available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
    fd = getFD(env, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
    if (ipv6_supported) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
        fd1 = getFD1(env, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
    if (fd < 0 && fd1 < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
     * Handle IP_MULTICAST_IF separately
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
    if (opt == java_net_SocketOptions_IP_MULTICAST_IF ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
        opt == java_net_SocketOptions_IP_MULTICAST_IF2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
        return getMulticastInterface(env, this, fd, fd1, opt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
     * Map the Java level socket option to the platform specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
     * level and option name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
    if (NET_MapSocketOption(opt, &level, &optname)) {
41214
855ac576eb77 8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents: 39318
diff changeset
  1935
        JNU_ThrowByName(env, "java/net/SocketException", "Invalid option");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
    if (fd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
        if (NET_MapSocketOptionV6(opt, &level, &optname)) {
41214
855ac576eb77 8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents: 39318
diff changeset
  1941
            JNU_ThrowByName(env, "java/net/SocketException", "Invalid option");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
        fd = fd1; /* must be IPv6 only */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
    optlen = sizeof(optval.i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
    if (NET_GetSockOpt(fd, level, optname, (void *)&optval, &optlen) < 0) {
32846
5383225ebd0d 8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents: 32503
diff changeset
  1949
        char tmpbuf[255];
5383225ebd0d 8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents: 32503
diff changeset
  1950
        int size = 0;
5383225ebd0d 8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents: 32503
diff changeset
  1951
        char errmsg[255 + 31];
5383225ebd0d 8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents: 32503
diff changeset
  1952
        getErrorString(errno, tmpbuf, sizeof(tmpbuf));
5383225ebd0d 8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents: 32503
diff changeset
  1953
        sprintf(errmsg, "error getting socket option: %s", tmpbuf);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", errmsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
    switch (opt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
        case java_net_SocketOptions_SO_BROADCAST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
        case java_net_SocketOptions_SO_REUSEADDR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
            return createBoolean(env, optval.i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
        case java_net_SocketOptions_IP_MULTICAST_LOOP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
            /* getLoopbackMode() returns true if IP_MULTICAST_LOOP is disabled */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
            return createBoolean(env, !optval.i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
        case java_net_SocketOptions_SO_SNDBUF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
        case java_net_SocketOptions_SO_RCVBUF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
        case java_net_SocketOptions_IP_TOS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
            return createInteger(env, optval.i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
        default :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
                "Socket option not supported by TwoStacksPlainDatagramSocketImpl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
/*
10693
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  1981
 * Returns local address of the socket.
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  1982
 *
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  1983
 * Class:     java_net_TwoStacksPlainDatagramSocketImpl
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  1984
 * Method:    socketLocalAddress
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  1985
 * Signature: (I)Ljava/lang/Object;
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  1986
 */
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  1987
JNIEXPORT jobject JNICALL
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
  1988
Java_java_net_TwoStacksPlainDatagramSocketImpl_socketLocalAddress
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
  1989
  (JNIEnv *env, jobject this, jint family)
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
  1990
{
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
  1991
    int fd = -1, fd1 = -1;
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
  1992
    SOCKETADDRESS sa;
10693
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  1993
    int len = 0;
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  1994
    int port;
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  1995
    jobject iaObj;
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  1996
    int ipv6_supported = ipv6_available();
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  1997
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  1998
    fd = getFD(env, this);
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  1999
    if (ipv6_supported) {
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  2000
        fd1 = getFD1(env, this);
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  2001
    }
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  2002
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  2003
    if (fd < 0 && fd1 < 0) {
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  2004
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  2005
                        "Socket closed");
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  2006
        return NULL;
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  2007
    }
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  2008
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  2009
    /* find out local IP address */
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  2010
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
  2011
    len = sizeof(struct sockaddr_in);
10693
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  2012
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  2013
    /* family==-1 when socket is not connected */
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41594
diff changeset
  2014
    if ((family == java_net_InetAddress_IPv6) || (family == -1 && fd == -1)) {
10693
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  2015
        fd = fd1; /* must be IPv6 only */
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41594
diff changeset
  2016
        len = sizeof(struct sockaddr_in6);
10693
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  2017
    }
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  2018
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  2019
    if (fd == -1) {
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  2020
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  2021
                        "Socket closed");
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  2022
        return NULL;
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  2023
    }
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  2024
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
  2025
    if (getsockname(fd, &sa.sa, &len) == -1) {
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 32846
diff changeset
  2026
        JNU_ThrowByNameWithMessageAndLastError
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 32846
diff changeset
  2027
            (env, JNU_JAVANETPKG "SocketException", "Error getting socket name");
10693
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  2028
        return NULL;
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  2029
    }
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
  2030
    iaObj = NET_SockaddrToInetAddress(env, &sa, &port);
10693
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  2031
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  2032
    return iaObj;
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  2033
}
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  2034
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 7668
diff changeset
  2035
/*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
 * Class:     java_net_TwoStacksPlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
 * Method:    setTimeToLive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
 * Signature: (I)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
Java_java_net_TwoStacksPlainDatagramSocketImpl_setTimeToLive(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
                                                    jint ttl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
    jobject fd1Obj = (*env)->GetObjectField(env, this, pdsi_fd1ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
    int fd = -1, fd1 = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
    int ittl = (int)ttl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
    if (IS_NULL(fdObj) && IS_NULL(fd1Obj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
      if (!IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
      if (!IS_NULL(fd1Obj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
        fd1 = (*env)->GetIntField(env, fd1Obj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
    /* setsockopt to be correct ttl */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
    if (fd >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
      if (NET_SetSockOpt(fd, IPPROTO_IP, IP_MULTICAST_TTL, (char*)&ittl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
                         sizeof (ittl)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
        NET_ThrowCurrent(env, "set IP_MULTICAST_TTL failed");
41594
e54e81e9be7b 8168405: Pending exceptions in java.base/windows/native
prappo
parents: 41380
diff changeset
  2067
        return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
    if (fd1 >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
      if (NET_SetSockOpt(fd1, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, (char *)&ittl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
                         sizeof(ittl)) <0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
        NET_ThrowCurrent(env, "set IPV6_MULTICAST_HOPS failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
 * Class:     java_net_TwoStacksPlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
 * Method:    setTTL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
 * Signature: (B)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
Java_java_net_TwoStacksPlainDatagramSocketImpl_setTTL(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
                                             jbyte ttl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
    Java_java_net_TwoStacksPlainDatagramSocketImpl_setTimeToLive(env, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
                                                        (jint)ttl & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
 * Class:     java_net_TwoStacksPlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
 * Method:    getTimeToLive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
 * Signature: ()I
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
Java_java_net_TwoStacksPlainDatagramSocketImpl_getTimeToLive(JNIEnv *env, jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
    jobject fd1Obj = (*env)->GetObjectField(env, this, pdsi_fd1ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
    int fd = -1, fd1 = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
    int ttl = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
    int len = sizeof(ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
    if (IS_NULL(fdObj) && IS_NULL(fd1Obj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
      if (!IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
      if (!IS_NULL(fd1Obj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
        fd1 = (*env)->GetIntField(env, fd1Obj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
    /* getsockopt of ttl */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
    if (fd >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
      if (NET_GetSockOpt(fd, IPPROTO_IP, IP_MULTICAST_TTL, (char*)&ttl, &len) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
        NET_ThrowCurrent(env, "get IP_MULTICAST_TTL failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
      return (jint)ttl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
    if (fd1 >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
      if (NET_GetSockOpt(fd1, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, (char*)&ttl, &len) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
        NET_ThrowCurrent(env, "get IP_MULTICAST_TTL failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
      return (jint)ttl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
 * Class:     java_net_TwoStacksPlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
 * Method:    getTTL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
 * Signature: ()B
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
JNIEXPORT jbyte JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
Java_java_net_TwoStacksPlainDatagramSocketImpl_getTTL(JNIEnv *env, jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
    int result = Java_java_net_TwoStacksPlainDatagramSocketImpl_getTimeToLive(env, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
    return (jbyte)result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
/* join/leave the named group on the named interface, or if no interface specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
 * then the interface set with setInterfac(), or the default interface otherwise */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
static void mcast_join_leave(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
                             jobject iaObj, jobject niObj,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
                             jboolean join)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
    jobject fd1Obj = (*env)->GetObjectField(env, this, pdsi_fd1ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
    jint fd = -1, fd1 = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
    SOCKETADDRESS name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
    struct ip_mreq mname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
    struct ipv6_mreq mname6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
    struct in_addr in;
25551
62ad60d30644 8040810: Uninitialised memory in jdk/src/windows/native/java/net: net_util_md.c, TwoStacksPlainSocketImpl.c, TwoStacksPlainDatagramSocketImpl.c, DualStackPlainSocketImpl.c, DualStackPlainDatagramSocketImpl.c
msheppar
parents: 23015
diff changeset
  2163
    DWORD ifindex = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
    int len, family;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
    int ipv6_supported = ipv6_available();
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
  2167
    int cmd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
25551
62ad60d30644 8040810: Uninitialised memory in jdk/src/windows/native/java/net: net_util_md.c, TwoStacksPlainSocketImpl.c, TwoStacksPlainDatagramSocketImpl.c, DualStackPlainSocketImpl.c, DualStackPlainDatagramSocketImpl.c
msheppar
parents: 23015
diff changeset
  2169
    memset((char *)&in, 0, sizeof(in));
62ad60d30644 8040810: Uninitialised memory in jdk/src/windows/native/java/net: net_util_md.c, TwoStacksPlainSocketImpl.c, TwoStacksPlainDatagramSocketImpl.c, DualStackPlainSocketImpl.c, DualStackPlainDatagramSocketImpl.c
msheppar
parents: 23015
diff changeset
  2170
    memset((char *)&name, 0, sizeof(name));
62ad60d30644 8040810: Uninitialised memory in jdk/src/windows/native/java/net: net_util_md.c, TwoStacksPlainSocketImpl.c, TwoStacksPlainDatagramSocketImpl.c, DualStackPlainSocketImpl.c, DualStackPlainDatagramSocketImpl.c
msheppar
parents: 23015
diff changeset
  2171
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
    if (IS_NULL(fdObj) && IS_NULL(fd1Obj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
    if (!IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
    if (ipv6_supported && !IS_NULL(fd1Obj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
        fd1 = (*env)->GetIntField(env, fd1Obj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
    if (IS_NULL(iaObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
        JNU_ThrowNullPointerException(env, "address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
  2189
    if (NET_InetAddressToSockaddr(env, iaObj, 0, &name, &len, JNI_FALSE) != 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
      return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
    /* Set the multicast group address in the ip_mreq field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
     * eventually this check should be done by the security manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
     */
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
  2196
    family = name.sa.sa_family;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
    if (family == AF_INET) {
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
  2199
        int address = name.sa4.sin_addr.s_addr;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
        if (!IN_MULTICAST(ntohl(address))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "not in multicast");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
        mname.imr_multiaddr.s_addr = address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
        if (fd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
          JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Can't join an IPv4 group on an IPv6 only socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
          return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
        if (IS_NULL(niObj)) {
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41771
diff changeset
  2210
            len = sizeof(in);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
            if (NET_GetSockOpt(fd, IPPROTO_IP, IP_MULTICAST_IF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
                           (char *)&in, &len) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
                NET_ThrowCurrent(env, "get IP_MULTICAST_IF failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
            mname.imr_interface.s_addr = in.s_addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
            if (getInet4AddrFromIf (env, niObj, &mname.imr_interface) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
                NET_ThrowCurrent(env, "no Inet4Address associated with interface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
        cmd = join ? IP_ADD_MEMBERSHIP: IP_DROP_MEMBERSHIP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
        /* Join the multicast group */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
        if (NET_SetSockOpt(fd, IPPROTO_IP, cmd, (char *) &mname, sizeof (mname)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
            if (WSAGetLastError() == WSAENOBUFS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
                JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
                    "IP_ADD_MEMBERSHIP failed (out of hardware filters?)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
                JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException","error setting options");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
    } else /* AF_INET6 */ {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
        if (ipv6_supported) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
            struct in6_addr *address;
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
  2238
            address = &name.sa6.sin6_addr;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
            if (!IN6_IS_ADDR_MULTICAST(address)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
                JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "not in6 multicast");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
            mname6.ipv6mr_multiaddr = *address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "IPv6 not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
        if (fd1 < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
          JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Can't join an IPv6 group on a IPv4 socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
          return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
        if (IS_NULL(niObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
            len = sizeof (ifindex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
            if (NET_GetSockOpt(fd1, IPPROTO_IPV6, IPV6_MULTICAST_IF, &ifindex, &len) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
                NET_ThrowCurrent(env, "get IPV6_MULTICAST_IF failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
            ifindex = getIndexFromIf (env, niObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
            if (ifindex == -1) {
41594
e54e81e9be7b 8168405: Pending exceptions in java.base/windows/native
prappo
parents: 41380
diff changeset
  2261
                if ((*env)->ExceptionOccurred(env)) {
e54e81e9be7b 8168405: Pending exceptions in java.base/windows/native
prappo
parents: 41380
diff changeset
  2262
                    return;
e54e81e9be7b 8168405: Pending exceptions in java.base/windows/native
prappo
parents: 41380
diff changeset
  2263
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
                NET_ThrowCurrent(env, "get ifindex failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
        mname6.ipv6mr_interface = ifindex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
        cmd = join ? IPV6_ADD_MEMBERSHIP: IPV6_DROP_MEMBERSHIP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
        /* Join the multicast group */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
        if (NET_SetSockOpt(fd1, IPPROTO_IPV6, cmd, (char *) &mname6, sizeof (mname6)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
            if (WSAGetLastError() == WSAENOBUFS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
                JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
                    "IP_ADD_MEMBERSHIP failed (out of hardware filters?)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
                JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException","error setting options");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
 * Class:     java_net_TwoStacksPlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
 * Method:    join
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
 * Signature: (Ljava/net/InetAddress;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
Java_java_net_TwoStacksPlainDatagramSocketImpl_join(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
                                           jobject iaObj, jobject niObj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
    mcast_join_leave (env, this, iaObj, niObj, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
 * Class:     java_net_TwoStacksPlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
 * Method:    leave
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
 * Signature: (Ljava/net/InetAddress;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
Java_java_net_TwoStacksPlainDatagramSocketImpl_leave(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
                                            jobject iaObj, jobject niObj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
    mcast_join_leave (env, this, iaObj, niObj, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
}
32503
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2308
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2309
/*
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2310
 * Class:     java_net_TwoStacksPlainDatagramSocketImpl
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2311
 * Method:    dataAvailable
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2312
 * Signature: ()I
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2313
 */
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2314
JNIEXPORT jint JNICALL Java_java_net_TwoStacksPlainDatagramSocketImpl_dataAvailable
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2315
(JNIEnv *env, jobject this) {
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2316
    SOCKET fd;
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2317
    SOCKET fd1;
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2318
    int  rv = -1, rv1 = -1;
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2319
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2320
    jobject fd1Obj;
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2321
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2322
    if (!IS_NULL(fdObj)) {
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2323
        int retval = 0;
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2324
        fd = (SOCKET)(*env)->GetIntField(env, fdObj, IO_fd_fdID);
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2325
        rv = ioctlsocket(fd, FIONREAD, &retval);
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2326
        if (retval > 0) {
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2327
            return retval;
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2328
        }
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2329
    }
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2330
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2331
    fd1Obj = (*env)->GetObjectField(env, this, pdsi_fd1ID);
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2332
    if (!IS_NULL(fd1Obj)) {
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2333
        int retval = 0;
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2334
        fd1 = (SOCKET)(*env)->GetIntField(env, fd1Obj, IO_fd_fdID);
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2335
        rv1 = ioctlsocket(fd1, FIONREAD, &retval);
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2336
        if (retval > 0) {
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2337
            return retval;
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2338
        }
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2339
    }
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2340
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2341
    if (rv < 0 && rv1 < 0) {
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2342
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2343
                        "Socket closed");
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2344
        return -1;
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2345
    }
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2346
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2347
    return 0;
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 26458
diff changeset
  2348
}