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