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