src/java.base/unix/native/libnet/PlainDatagramSocketImpl.c
author pconcannon
Thu, 28 Nov 2019 16:49:52 +0000
changeset 59313 6f12009ea9d7
parent 55375 96c7427456f9
permissions -rw-r--r--
8233307: MulticastSocket getOption(IP_MULTICAST_IF) returns interface when not set Summary: The MulticastSocket method getOption has been changed to conform to the behavior described in StandardSocketOptions.IP_MULTICAST_IF. Reviewed-by: chegar, dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
53381
e1da82072c79 8217311: Improve Exception thrown when MulticastSocket.setInterface fails on AIX(Unix)
clanger
parents: 52670
diff changeset
     2
 * Copyright (c) 1997, 2019, 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
#include <errno.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <string.h>
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
    28
#include <sys/ioctl.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
    30
#if defined(__solaris__)
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
    31
#include <sys/filio.h>
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
    32
#endif
32503
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 30963
diff changeset
    33
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
    34
#include "net_util.h"
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
    35
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
    36
#include "java_net_PlainDatagramSocketImpl.h"
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
    37
#include "java_net_InetAddress.h"
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
    38
#include "java_net_NetworkInterface.h"
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
    39
#include "java_net_SocketOptions.h"
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
    40
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
#define IPV6_MULTICAST_IF 17
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
#ifndef SO_BSDCOMPAT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
#define SO_BSDCOMPAT  14
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
#endif
18534
2d131cf5db63 8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents: 16870
diff changeset
    46
/**
2d131cf5db63 8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents: 16870
diff changeset
    47
 * IP_MULTICAST_ALL has been supported since kernel version 2.6.31
2d131cf5db63 8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents: 16870
diff changeset
    48
 * but we may be building on a machine that is older than that.
2d131cf5db63 8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents: 16870
diff changeset
    49
 */
2d131cf5db63 8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents: 16870
diff changeset
    50
#ifndef IP_MULTICAST_ALL
2d131cf5db63 8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents: 16870
diff changeset
    51
#define IP_MULTICAST_ALL      49
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
#endif
18540
1f637992378a 8017271: Crash may occur in java.net.DualStackPlainSocketImpl::initIDs due to unchecked values returned from JNI functions
chegar
parents: 18534
diff changeset
    53
#endif  //  __linux__
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
    55
#ifdef __solaris__
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
    56
#ifndef BSD_COMP
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
    57
#define BSD_COMP
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
    58
#endif
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
    59
#endif
32503
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 30963
diff changeset
    60
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
#ifndef IPTOS_TOS_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
#define IPTOS_TOS_MASK 0x1e
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
#ifndef IPTOS_PREC_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
#define IPTOS_PREC_MASK 0xe0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
/************************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
static jfieldID IO_fd_fdID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
static jfieldID pdsi_fdID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
static jfieldID pdsi_timeoutID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
static jfieldID pdsi_trafficClassID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
static jfieldID pdsi_localPortID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
static jfieldID pdsi_connected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
static jfieldID pdsi_connectedAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
static jfieldID pdsi_connectedPort;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * Returns a java.lang.Integer based on 'i'
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
static jobject createInteger(JNIEnv *env, int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    static jclass i_class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    static jmethodID i_ctrID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    if (i_class == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        jclass c = (*env)->FindClass(env, "java/lang/Integer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        CHECK_NULL_RETURN(c, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        i_ctrID = (*env)->GetMethodID(env, c, "<init>", "(I)V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        CHECK_NULL_RETURN(i_ctrID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        i_class = (*env)->NewGlobalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        CHECK_NULL_RETURN(i_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
    98
    return (*env)->NewObject(env, i_class, i_ctrID, i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * Returns a java.lang.Boolean based on 'b'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
static jobject createBoolean(JNIEnv *env, int b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    static jclass b_class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    static jmethodID b_ctrID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    if (b_class == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        jclass c = (*env)->FindClass(env, "java/lang/Boolean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        CHECK_NULL_RETURN(c, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        b_ctrID = (*env)->GetMethodID(env, c, "<init>", "(Z)V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        CHECK_NULL_RETURN(b_ctrID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        b_class = (*env)->NewGlobalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        CHECK_NULL_RETURN(b_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   117
    return (*env)->NewObject(env, b_class, b_ctrID, (jboolean)(b != 0));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * Returns the fd for a PlainDatagramSocketImpl or -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * if closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
static int getFD(JNIEnv *env, jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    if (fdObj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    return (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * Method:    init
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
Java_java_net_PlainDatagramSocketImpl_init(JNIEnv *env, jclass cls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    pdsi_fdID = (*env)->GetFieldID(env, cls, "fd",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                                   "Ljava/io/FileDescriptor;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    CHECK_NULL(pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    pdsi_timeoutID = (*env)->GetFieldID(env, cls, "timeout", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    CHECK_NULL(pdsi_timeoutID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    pdsi_trafficClassID = (*env)->GetFieldID(env, cls, "trafficClass", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    CHECK_NULL(pdsi_trafficClassID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    pdsi_localPortID = (*env)->GetFieldID(env, cls, "localPort", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    CHECK_NULL(pdsi_localPortID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    pdsi_connected = (*env)->GetFieldID(env, cls, "connected", "Z");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    CHECK_NULL(pdsi_connected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    pdsi_connectedAddress = (*env)->GetFieldID(env, cls, "connectedAddress",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                                               "Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    CHECK_NULL(pdsi_connectedAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    pdsi_connectedPort = (*env)->GetFieldID(env, cls, "connectedPort", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    CHECK_NULL(pdsi_connectedPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    IO_fd_fdID = NET_GetFileDescriptorID(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    CHECK_NULL(IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
22646
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 22095
diff changeset
   160
    initInetAddressIDs(env);
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 22095
diff changeset
   161
    JNU_CHECK_EXCEPTION(env);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    Java_java_net_NetworkInterface_init(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * Method:    bind
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 * Signature: (ILjava/net/InetAddress;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
Java_java_net_PlainDatagramSocketImpl_bind0(JNIEnv *env, jobject this,
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   172
                                            jint localport, jobject iaObj) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /* fdObj is the FileDescriptor field on this */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /* fd is an int field on fdObj */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    int len = 0;
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   178
    SOCKETADDRESS sa;
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   179
    socklen_t slen = sizeof(SOCKETADDRESS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    if (IS_NULL(iaObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        JNU_ThrowNullPointerException(env, "iaObj is null.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /* bind */
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   195
    if (NET_InetAddressToSockaddr(env, iaObj, localport, &sa, &len,
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   196
                                  JNI_TRUE) != 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
      return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   200
    if (NET_Bind(fd, &sa, len) < 0)  {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        if (errno == EADDRINUSE || errno == EADDRNOTAVAIL ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            errno == EPERM || errno == EACCES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "BindException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                            "Bind failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        } else {
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
   206
            JNU_ThrowByNameWithMessageAndLastError
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
   207
                (env, JNU_JAVANETPKG "SocketException", "Bind failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
   212
    /* initialize the local port */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    if (localport == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        /* Now that we're a connected socket, let's extract the port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
         * that the system chose for us and store it in the Socket object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
         */
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   217
        if (getsockname(fd, &sa.sa, &slen) == -1) {
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
   218
            JNU_ThrowByNameWithMessageAndLastError
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
   219
                (env, JNU_JAVANETPKG "SocketException", "Error getting socket name");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   223
        localport = NET_GetPortFromSockaddr(&sa);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        (*env)->SetIntField(env, this, pdsi_localPortID, localport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        (*env)->SetIntField(env, this, pdsi_localPortID, localport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
 * Method:    connect0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
 * Signature: (Ljava/net/InetAddress;I)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
Java_java_net_PlainDatagramSocketImpl_connect0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                                               jobject address, jint port) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /* The object's field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    /* The fdObj'fd */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    jint fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /* The packetAddress address, family and port */
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   244
    SOCKETADDRESS rmtaddr;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    if (IS_NULL(address)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        JNU_ThrowNullPointerException(env, "address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   259
    if (NET_InetAddressToSockaddr(env, address, port, &rmtaddr, &len,
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   260
                                  JNI_TRUE) != 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
      return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   264
    if (NET_Connect(fd, &rmtaddr.sa, len) == -1) {
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
   265
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException",
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
   266
                        "Connect failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
 * Method:    disconnect0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
Java_java_net_PlainDatagramSocketImpl_disconnect0(JNIEnv *env, jobject this, jint family) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    /* The object's field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    /* The fdObj'fd */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    jint fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   282
#if defined(__linux__) || defined(_ALLBSD_SOURCE)
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   283
    SOCKETADDRESS addr;
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   284
    socklen_t len;
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   285
#if defined(__linux__)
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   286
    int localPort = 0;
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   287
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   295
#if defined(__linux__) || defined(_ALLBSD_SOURCE)
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   296
    memset(&addr, 0, sizeof(addr));
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   297
    if (ipv6_available()) {
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   298
        addr.sa6.sin6_family = AF_UNSPEC;
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   299
        len = sizeof(struct sockaddr_in6);
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   300
    } else {
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   301
        addr.sa4.sin_family = AF_UNSPEC;
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   302
        len = sizeof(struct sockaddr_in);
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   303
    }
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   304
    NET_Connect(fd, &addr.sa, len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   306
#if defined(__linux__)
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   307
    if (getsockname(fd, &addr.sa, &len) == -1)
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   308
        return;
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
   309
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   310
    localPort = NET_GetPortFromSockaddr(&addr);
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   311
    if (localPort == 0) {
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   312
        localPort = (*env)->GetIntField(env, this, pdsi_localPortID);
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   313
        if (addr.sa.sa_family == AF_INET6) {
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   314
            addr.sa6.sin6_port = htons(localPort);
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   315
        } else {
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   316
            addr.sa4.sin_port = htons(localPort);
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   317
        }
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
   318
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   319
        NET_Bind(fd, &addr, len);
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   320
    }
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
   321
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   322
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
#else
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   324
    NET_Connect(fd, 0, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
 * Class:     java_net_PlainDatagramSocketImpl
55375
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents: 54627
diff changeset
   330
 * Method:    send0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
 * Signature: (Ljava/net/DatagramPacket;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
JNIEXPORT void JNICALL
55375
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents: 54627
diff changeset
   334
Java_java_net_PlainDatagramSocketImpl_send0(JNIEnv *env, jobject this,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                                           jobject packet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    char BUF[MAX_BUFFER_LEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    char *fullPacket = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    int ret, mallocedPacket = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    /* The object's field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    jint trafficClass = (*env)->GetIntField(env, this, pdsi_trafficClassID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    jbyteArray packetBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    jobject packetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    jint packetBufferOffset, packetBufferLen, packetPort;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    jboolean connected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /* The fdObj'fd */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    jint fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   352
    SOCKETADDRESS rmtaddr;
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   353
    struct sockaddr *rmtaddrP = 0;
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   354
    int len = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    if (IS_NULL(packet)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        JNU_ThrowNullPointerException(env, "packet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    connected = (*env)->GetBooleanField(env, this, pdsi_connected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    packetBuffer = (*env)->GetObjectField(env, packet, dp_bufID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    packetAddress = (*env)->GetObjectField(env, packet, dp_addressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    if (IS_NULL(packetBuffer) || IS_NULL(packetAddress)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        JNU_ThrowNullPointerException(env, "null buffer || null address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    packetBufferOffset = (*env)->GetIntField(env, packet, dp_offsetID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    packetBufferLen = (*env)->GetIntField(env, packet, dp_lengthID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   380
    // arg to NET_Sendto() null, if connected
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   381
    if (!connected) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        packetPort = (*env)->GetIntField(env, packet, dp_portID);
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   383
        if (NET_InetAddressToSockaddr(env, packetAddress, packetPort, &rmtaddr,
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   384
                                      &len, JNI_TRUE) != 0) {
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   385
            return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        }
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   387
        rmtaddrP = &rmtaddr.sa;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    if (packetBufferLen > MAX_BUFFER_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        /* When JNI-ifying the JDK's IO routines, we turned
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
   392
         * reads and writes of byte arrays of size greater
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
         * than 2048 bytes into several operations of size 2048.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
         * This saves a malloc()/memcpy()/free() for big
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
         * buffers.  This is OK for file IO and TCP, but that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
         * strategy violates the semantics of a datagram protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
         * (one big send) != (several smaller sends).  So here
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
   398
         * we *must* allocate the buffer.  Note it needn't be bigger
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
   399
         * than 65,536 (0xFFFF), the max size of an IP packet.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
         * Anything bigger should be truncated anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
         * We may want to use a smarter allocation scheme at some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
         * point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        if (packetBufferLen > MAX_PACKET_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            packetBufferLen = MAX_PACKET_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        fullPacket = (char *)malloc(packetBufferLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        if (!fullPacket) {
13245
7ab3ef5b9520 7181353: Update error message to distinguish native OOM and java OOM in net
zhouyx
parents: 12551
diff changeset
   411
            JNU_ThrowOutOfMemoryError(env, "Send buffer native heap allocation failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            mallocedPacket = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        fullPacket = &(BUF[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    (*env)->GetByteArrayRegion(env, packetBuffer, packetBufferOffset, packetBufferLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                               (jbyte *)fullPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    if (trafficClass != 0 && ipv6_available()) {
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   423
        NET_SetTrafficClass(&rmtaddr, trafficClass);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * Send the datagram.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * If we are connected it's possible that sendto will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * ECONNREFUSED indicating that an ICMP port unreachable has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     */
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   433
    ret = NET_SendTo(fd, fullPacket, packetBufferLen, 0, rmtaddrP, len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    if (ret < 0) {
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   436
        if (errno == ECONNREFUSED) {
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   437
            JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                            "ICMP Port Unreachable");
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   439
        } else {
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
   440
            JNU_ThrowIOExceptionWithLastError(env, "sendto failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        }
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 (mallocedPacket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        free(fullPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
 * Method:    peek
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
 * Signature: (Ljava/net/InetAddress;)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
Java_java_net_PlainDatagramSocketImpl_peek(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                                           jobject addressObj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    jint timeout = (*env)->GetIntField(env, this, pdsi_timeoutID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    jint fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    ssize_t n;
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   463
    SOCKETADDRESS rmtaddr;
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   464
    socklen_t slen = sizeof(SOCKETADDRESS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    char buf[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    jint family;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    jobject iaObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    if (IS_NULL(addressObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        JNU_ThrowNullPointerException(env, "Null address in peek()");
22651
bfc6ca5245fd 8033689: PlainDatagramSocketImpl missing returns after throwing an exception
chegar
parents: 22646
diff changeset
   477
        return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    if (timeout) {
45061
74b09ee3cd55 8179905: Remove the use of gettimeofday in Networking code
vtewari
parents: 44921
diff changeset
   480
        int ret = NET_Timeout(env, fd, timeout, JVM_NanoTime(env, 0));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        if (ret == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                            "Peek timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            return ret;
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   485
        } else if (ret == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            if (errno == EBADF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
23048
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   488
            } else if (errno == ENOMEM) {
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   489
                 JNU_ThrowOutOfMemoryError(env, "NET_Timeout native heap allocation failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            } else {
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
   491
                 JNU_ThrowByNameWithMessageAndLastError
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
   492
                     (env, JNU_JAVANETPKG "SocketException", "Peek failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   498
    n = NET_RecvFrom(fd, buf, 1, MSG_PEEK, &rmtaddr.sa, &slen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   500
    if (n == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        if (errno == ECONNREFUSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            int orig_errno = errno;
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   505
            recv(fd, buf, 1, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            errno = orig_errno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        if (errno == ECONNREFUSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                            "ICMP Port Unreachable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            if (errno == EBADF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            } else {
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
   516
                 JNU_ThrowByNameWithMessageAndLastError
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
   517
                     (env, JNU_JAVANETPKG "SocketException", "Peek failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   523
    iaObj = NET_SockaddrToInetAddress(env, &rmtaddr, &port);
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
   524
    family = getInetAddress_family(env, iaObj) == java_net_InetAddress_IPv4 ?
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
   525
        AF_INET : AF_INET6;
51151
d6b131d2bc8b 8193419: Better Internet address support
vtewari
parents: 47216
diff changeset
   526
    JNU_CHECK_EXCEPTION_RETURN(env, -1);
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
   527
    if (family == AF_INET) { /* this API can't handle IPV6 addresses */
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   528
        int address = getInetAddress_addr(env, iaObj);
51151
d6b131d2bc8b 8193419: Better Internet address support
vtewari
parents: 47216
diff changeset
   529
        JNU_CHECK_EXCEPTION_RETURN(env, -1);
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   530
        setInetAddress_addr(env, addressObj, address);
51151
d6b131d2bc8b 8193419: Better Internet address support
vtewari
parents: 47216
diff changeset
   531
        JNU_CHECK_EXCEPTION_RETURN(env, -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    return port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
Java_java_net_PlainDatagramSocketImpl_peekData(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                                           jobject packet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    char BUF[MAX_BUFFER_LEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    char *fullPacket = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    int mallocedPacket = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    jint timeout = (*env)->GetIntField(env, this, pdsi_timeoutID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    jbyteArray packetBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    jint packetBufferOffset, packetBufferLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    int n;
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   549
    SOCKETADDRESS rmtaddr;
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   550
    socklen_t slen = sizeof(SOCKETADDRESS);
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   551
    int port = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    if (IS_NULL(packet)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        JNU_ThrowNullPointerException(env, "packet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    packetBuffer = (*env)->GetObjectField(env, packet, dp_bufID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    if (IS_NULL(packetBuffer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        JNU_ThrowNullPointerException(env, "packet buffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    packetBufferOffset = (*env)->GetIntField(env, packet, dp_offsetID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    packetBufferLen = (*env)->GetIntField(env, packet, dp_bufLengthID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    if (timeout) {
45061
74b09ee3cd55 8179905: Remove the use of gettimeofday in Networking code
vtewari
parents: 44921
diff changeset
   574
        int ret = NET_Timeout(env, fd, timeout, JVM_NanoTime(env, 0));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        if (ret == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                            "Receive timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            return -1;
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   579
        } else if (ret == -1) {
23048
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   580
            if (errno == ENOMEM) {
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   581
                JNU_ThrowOutOfMemoryError(env, "NET_Timeout native heap allocation failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
#ifdef __linux__
23048
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   583
            } else if (errno == EBADF) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            } else {
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
   586
                JNU_ThrowByNameWithMessageAndLastError
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
   587
                    (env, JNU_JAVANETPKG "SocketException", "Receive failed");
23048
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   588
#else
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   589
            } else {
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   590
                JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   591
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    if (packetBufferLen > MAX_BUFFER_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        /* When JNI-ifying the JDK's IO routines, we turned
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
   600
         * reads and writes of byte arrays of size greater
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
         * than 2048 bytes into several operations of size 2048.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
         * This saves a malloc()/memcpy()/free() for big
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
         * buffers.  This is OK for file IO and TCP, but that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
         * strategy violates the semantics of a datagram protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
         * (one big send) != (several smaller sends).  So here
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
   606
         * we *must* allocate the buffer.  Note it needn't be bigger
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
   607
         * than 65,536 (0xFFFF), the max size of an IP packet.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
         * anything bigger is truncated anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
         * We may want to use a smarter allocation scheme at some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
         * point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        if (packetBufferLen > MAX_PACKET_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            packetBufferLen = MAX_PACKET_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        fullPacket = (char *)malloc(packetBufferLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        if (!fullPacket) {
13245
7ab3ef5b9520 7181353: Update error message to distinguish native OOM and java OOM in net
zhouyx
parents: 12551
diff changeset
   619
            JNU_ThrowOutOfMemoryError(env, "Peek buffer native heap allocation failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            mallocedPacket = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        fullPacket = &(BUF[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    n = NET_RecvFrom(fd, fullPacket, packetBufferLen, MSG_PEEK,
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   629
                     &rmtaddr.sa, &slen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    /* truncate the data if the packet's length is too small */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    if (n > packetBufferLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        n = packetBufferLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    }
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   634
    if (n == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        if (errno == ECONNREFUSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            int orig_errno = errno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            (void) recv(fd, fullPacket, 1, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            errno = orig_errno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        (*env)->SetIntField(env, packet, dp_offsetID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        (*env)->SetIntField(env, packet, dp_lengthID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        if (errno == ECONNREFUSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                            "ICMP Port Unreachable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            if (errno == EBADF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            } else {
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
   652
                 JNU_ThrowByNameWithMessageAndLastError
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
   653
                     (env, JNU_JAVANETPKG "SocketException", "Receive failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
         * success - fill in received address...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
         * REMIND: Fill in an int on the packet, and create inetadd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
         * object in Java, as a performance improvement. Also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
         * construct the inetadd object lazily.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        jobject packetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
         * Check if there is an InetAddress already associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
         * packet. If so we check if it is the same source address. We
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
         * can't update any existing InetAddress because it is immutable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        packetAddress = (*env)->GetObjectField(env, packet, dp_addressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        if (packetAddress != NULL) {
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   674
            if (!NET_SockaddrEqualsInetAddress(env, &rmtaddr, packetAddress)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                /* force a new InetAddress to be created */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                packetAddress = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        }
52670
eb6864cdcd61 8203672: JNI exception pending in PlainSocketImpl.c
vtewari
parents: 51153
diff changeset
   679
        if (!(*env)->ExceptionCheck(env)){
eb6864cdcd61 8203672: JNI exception pending in PlainSocketImpl.c
vtewari
parents: 51153
diff changeset
   680
            if (packetAddress == NULL ) {
eb6864cdcd61 8203672: JNI exception pending in PlainSocketImpl.c
vtewari
parents: 51153
diff changeset
   681
                packetAddress = NET_SockaddrToInetAddress(env, &rmtaddr, &port);
eb6864cdcd61 8203672: JNI exception pending in PlainSocketImpl.c
vtewari
parents: 51153
diff changeset
   682
                /* stuff the new InetAddress in the packet */
eb6864cdcd61 8203672: JNI exception pending in PlainSocketImpl.c
vtewari
parents: 51153
diff changeset
   683
                (*env)->SetObjectField(env, packet, dp_addressID, packetAddress);
eb6864cdcd61 8203672: JNI exception pending in PlainSocketImpl.c
vtewari
parents: 51153
diff changeset
   684
            } else {
eb6864cdcd61 8203672: JNI exception pending in PlainSocketImpl.c
vtewari
parents: 51153
diff changeset
   685
                /* only get the new port number */
eb6864cdcd61 8203672: JNI exception pending in PlainSocketImpl.c
vtewari
parents: 51153
diff changeset
   686
                port = NET_GetPortFromSockaddr(&rmtaddr);
eb6864cdcd61 8203672: JNI exception pending in PlainSocketImpl.c
vtewari
parents: 51153
diff changeset
   687
            }
eb6864cdcd61 8203672: JNI exception pending in PlainSocketImpl.c
vtewari
parents: 51153
diff changeset
   688
            /* and fill in the data, remote address/port and such */
eb6864cdcd61 8203672: JNI exception pending in PlainSocketImpl.c
vtewari
parents: 51153
diff changeset
   689
            (*env)->SetByteArrayRegion(env, packetBuffer, packetBufferOffset, n,
eb6864cdcd61 8203672: JNI exception pending in PlainSocketImpl.c
vtewari
parents: 51153
diff changeset
   690
                                    (jbyte *)fullPacket);
eb6864cdcd61 8203672: JNI exception pending in PlainSocketImpl.c
vtewari
parents: 51153
diff changeset
   691
            (*env)->SetIntField(env, packet, dp_portID, port);
eb6864cdcd61 8203672: JNI exception pending in PlainSocketImpl.c
vtewari
parents: 51153
diff changeset
   692
            (*env)->SetIntField(env, packet, dp_lengthID, n);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    if (mallocedPacket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        free(fullPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    return port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
 * Method:    receive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
 * Signature: (Ljava/net/DatagramPacket;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
Java_java_net_PlainDatagramSocketImpl_receive0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                                              jobject packet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    char BUF[MAX_BUFFER_LEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    char *fullPacket = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    int mallocedPacket = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    jint timeout = (*env)->GetIntField(env, this, pdsi_timeoutID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    jbyteArray packetBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    jint packetBufferOffset, packetBufferLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    int n;
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   723
    SOCKETADDRESS rmtaddr;
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   724
    socklen_t slen = sizeof(SOCKETADDRESS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    jboolean retry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    jboolean connected = JNI_FALSE;
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
   728
    jobject connectedAddress = NULL;
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
   729
    jint connectedPort = 0;
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
   730
    jlong prevTime = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    if (IS_NULL(packet)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        JNU_ThrowNullPointerException(env, "packet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    packetBuffer = (*env)->GetObjectField(env, packet, dp_bufID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    if (IS_NULL(packetBuffer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        JNU_ThrowNullPointerException(env, "packet buffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    packetBufferOffset = (*env)->GetIntField(env, packet, dp_offsetID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    packetBufferLen = (*env)->GetIntField(env, packet, dp_bufLengthID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    if (packetBufferLen > MAX_BUFFER_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        /* When JNI-ifying the JDK's IO routines, we turned
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
   757
         * reads and writes of byte arrays of size greater
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
         * than 2048 bytes into several operations of size 2048.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
         * This saves a malloc()/memcpy()/free() for big
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
         * buffers.  This is OK for file IO and TCP, but that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
         * strategy violates the semantics of a datagram protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
         * (one big send) != (several smaller sends).  So here
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
   763
         * we *must* allocate the buffer.  Note it needn't be bigger
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
   764
         * than 65,536 (0xFFFF) the max size of an IP packet,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
         * anything bigger is truncated anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
         * We may want to use a smarter allocation scheme at some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
         * point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        if (packetBufferLen > MAX_PACKET_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            packetBufferLen = MAX_PACKET_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        fullPacket = (char *)malloc(packetBufferLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        if (!fullPacket) {
13245
7ab3ef5b9520 7181353: Update error message to distinguish native OOM and java OOM in net
zhouyx
parents: 12551
diff changeset
   776
            JNU_ThrowOutOfMemoryError(env, "Receive buffer native heap allocation failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            mallocedPacket = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        fullPacket = &(BUF[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        retry = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        if (timeout) {
45061
74b09ee3cd55 8179905: Remove the use of gettimeofday in Networking code
vtewari
parents: 44921
diff changeset
   789
            int ret = NET_Timeout(env, fd, timeout, JVM_NanoTime(env, 0));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            if (ret <= 0) {
23048
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   791
                if (ret == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                    JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                                    "Receive timed out");
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   794
                } else if (ret == -1) {
23048
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   795
                    if (errno == ENOMEM) {
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   796
                        JNU_ThrowOutOfMemoryError(env, "NET_Timeout native heap allocation failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
#ifdef __linux__
23048
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   798
                    } else if (errno == EBADF) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                         JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
23048
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   800
                    } else {
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
   801
                        JNU_ThrowByNameWithMessageAndLastError
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
   802
                            (env, JNU_JAVANETPKG "SocketException", "Receive failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
#else
23048
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   804
                    } else {
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   805
                        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
#endif
23048
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   807
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                if (mallocedPacket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                    free(fullPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        n = NET_RecvFrom(fd, fullPacket, packetBufferLen, 0,
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
   819
                         &rmtaddr.sa, &slen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        /* truncate the data if the packet's length is too small */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        if (n > packetBufferLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            n = packetBufferLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        }
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   824
        if (n == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            (*env)->SetIntField(env, packet, dp_offsetID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            (*env)->SetIntField(env, packet, dp_lengthID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            if (errno == ECONNREFUSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                                "ICMP Port Unreachable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                if (errno == EBADF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                     JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                 } else {
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
   834
                     JNU_ThrowByNameWithMessageAndLastError
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
   835
                         (env, JNU_JAVANETPKG "SocketException", "Receive failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            jobject packetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
             * success - fill in received address...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
             * REMIND: Fill in an int on the packet, and create inetadd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
             * object in Java, as a performance improvement. Also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
             * construct the inetadd object lazily.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
             * Check if there is an InetAddress already associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
             * packet. If so we check if it is the same source address. We
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
             * can't update any existing InetAddress because it is immutable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            packetAddress = (*env)->GetObjectField(env, packet, dp_addressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            if (packetAddress != NULL) {
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   857
                if (!NET_SockaddrEqualsInetAddress(env, &rmtaddr,
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   858
                                                   packetAddress)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                    /* force a new InetAddress to be created */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                    packetAddress = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            if (packetAddress == NULL) {
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   864
                packetAddress = NET_SockaddrToInetAddress(env, &rmtaddr, &port);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                /* stuff the new Inetaddress in the packet */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                (*env)->SetObjectField(env, packet, dp_addressID, packetAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                /* only get the new port number */
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   869
                port = NET_GetPortFromSockaddr(&rmtaddr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            /* and fill in the data, remote address/port and such */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            (*env)->SetByteArrayRegion(env, packetBuffer, packetBufferOffset, n,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                                       (jbyte *)fullPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            (*env)->SetIntField(env, packet, dp_portID, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            (*env)->SetIntField(env, packet, dp_lengthID, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    } while (retry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    if (mallocedPacket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        free(fullPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
 * Method:    datagramSocketCreate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
Java_java_net_PlainDatagramSocketImpl_datagramSocketCreate(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                                                           jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   894
    int arg, fd, t = 1;
32846
5383225ebd0d 8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents: 32652
diff changeset
   895
    char tmpbuf[1024];
6299
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   896
    int domain = ipv6_available() ? AF_INET6 : AF_INET;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    }
6299
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   903
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   904
    if ((fd = socket(domain, SOCK_DGRAM, 0)) == -1) {
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
   905
        JNU_ThrowByNameWithMessageAndLastError
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
   906
            (env, JNU_JAVANETPKG "SocketException", "Error creating socket");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
54627
22323f20401b 8222562: Don't set IPV6_V6ONLY when IPv4 is not available
aeubanks
parents: 54219
diff changeset
   910
    /*
22323f20401b 8222562: Don't set IPV6_V6ONLY when IPv4 is not available
aeubanks
parents: 54219
diff changeset
   911
     * If IPv4 is available, disable IPV6_V6ONLY to ensure dual-socket support.
22323f20401b 8222562: Don't set IPV6_V6ONLY when IPv4 is not available
aeubanks
parents: 54219
diff changeset
   912
     */
22323f20401b 8222562: Don't set IPV6_V6ONLY when IPv4 is not available
aeubanks
parents: 54219
diff changeset
   913
    if (domain == AF_INET6 && ipv4_available()) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   914
        arg = 0;
6299
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   915
        if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&arg,
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   916
                       sizeof(int)) < 0) {
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   917
            NET_ThrowNew(env, errno, "cannot set IPPROTO_IPV6");
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   918
            close(fd);
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   919
            return;
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   920
        }
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   921
    }
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   922
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   923
#ifdef __APPLE__
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   924
    arg = 65507;
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   925
    if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF,
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   926
                   (char *)&arg, sizeof(arg)) < 0) {
32846
5383225ebd0d 8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents: 32652
diff changeset
   927
        getErrorString(errno, tmpbuf, sizeof(tmpbuf));
5383225ebd0d 8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents: 32652
diff changeset
   928
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", tmpbuf);
32652
aed323c3a01d 8073542: File Leak in jdk/src/java/base/unix/native/libnet/PlainDatagramSocketImpl.c
dfuchs
parents: 32503
diff changeset
   929
        close(fd);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   930
        return;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   931
    }
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   932
    if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF,
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   933
                   (char *)&arg, sizeof(arg)) < 0) {
32846
5383225ebd0d 8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents: 32652
diff changeset
   934
        getErrorString(errno, tmpbuf, sizeof(tmpbuf));
5383225ebd0d 8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents: 32652
diff changeset
   935
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", tmpbuf);
32652
aed323c3a01d 8073542: File Leak in jdk/src/java/base/unix/native/libnet/PlainDatagramSocketImpl.c
dfuchs
parents: 32503
diff changeset
   936
        close(fd);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   937
        return;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   938
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   939
#endif /* __APPLE__ */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   940
32652
aed323c3a01d 8073542: File Leak in jdk/src/java/base/unix/native/libnet/PlainDatagramSocketImpl.c
dfuchs
parents: 32503
diff changeset
   941
    if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (char*) &t, sizeof (int)) < 0) {
32846
5383225ebd0d 8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents: 32652
diff changeset
   942
        getErrorString(errno, tmpbuf, sizeof(tmpbuf));
5383225ebd0d 8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents: 32652
diff changeset
   943
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", tmpbuf);
32652
aed323c3a01d 8073542: File Leak in jdk/src/java/base/unix/native/libnet/PlainDatagramSocketImpl.c
dfuchs
parents: 32503
diff changeset
   944
        close(fd);
aed323c3a01d 8073542: File Leak in jdk/src/java/base/unix/native/libnet/PlainDatagramSocketImpl.c
dfuchs
parents: 32503
diff changeset
   945
        return;
aed323c3a01d 8073542: File Leak in jdk/src/java/base/unix/native/libnet/PlainDatagramSocketImpl.c
dfuchs
parents: 32503
diff changeset
   946
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
18534
2d131cf5db63 8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents: 16870
diff changeset
   948
#if defined(__linux__)
2d131cf5db63 8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents: 16870
diff changeset
   949
     arg = 0;
2d131cf5db63 8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents: 16870
diff changeset
   950
     int level = (domain == AF_INET6) ? IPPROTO_IPV6 : IPPROTO_IP;
2d131cf5db63 8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents: 16870
diff changeset
   951
     if ((setsockopt(fd, level, IP_MULTICAST_ALL, (char*)&arg, sizeof(arg)) < 0) &&
32846
5383225ebd0d 8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents: 32652
diff changeset
   952
           (errno != ENOPROTOOPT))
5383225ebd0d 8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents: 32652
diff changeset
   953
    {
5383225ebd0d 8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents: 32652
diff changeset
   954
        getErrorString(errno, tmpbuf, sizeof(tmpbuf));
5383225ebd0d 8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents: 32652
diff changeset
   955
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", tmpbuf);
18534
2d131cf5db63 8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents: 16870
diff changeset
   956
         close(fd);
2d131cf5db63 8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents: 16870
diff changeset
   957
         return;
2d131cf5db63 8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents: 16870
diff changeset
   958
     }
2d131cf5db63 8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents: 16870
diff changeset
   959
#endif
2d131cf5db63 8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents: 16870
diff changeset
   960
41834
99c4f72c0a91 8168771: Remove #ifdef AF_INET6 guards in libnet native coding
clanger
parents: 41771
diff changeset
   961
#if defined (__linux__)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * On Linux for IPv6 sockets we must set the hop limit
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
   964
     * to 1 to be compatible with default TTL of 1 for IPv4 sockets.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     */
6299
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   966
    if (domain == AF_INET6) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        int ttl = 1;
32652
aed323c3a01d 8073542: File Leak in jdk/src/java/base/unix/native/libnet/PlainDatagramSocketImpl.c
dfuchs
parents: 32503
diff changeset
   968
        if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, (char *) &ttl,
aed323c3a01d 8073542: File Leak in jdk/src/java/base/unix/native/libnet/PlainDatagramSocketImpl.c
dfuchs
parents: 32503
diff changeset
   969
                sizeof (ttl)) < 0) {
32846
5383225ebd0d 8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents: 32652
diff changeset
   970
            getErrorString(errno, tmpbuf, sizeof(tmpbuf));
5383225ebd0d 8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents: 32652
diff changeset
   971
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", tmpbuf);
32652
aed323c3a01d 8073542: File Leak in jdk/src/java/base/unix/native/libnet/PlainDatagramSocketImpl.c
dfuchs
parents: 32503
diff changeset
   972
            close(fd);
aed323c3a01d 8073542: File Leak in jdk/src/java/base/unix/native/libnet/PlainDatagramSocketImpl.c
dfuchs
parents: 32503
diff changeset
   973
            return;
aed323c3a01d 8073542: File Leak in jdk/src/java/base/unix/native/libnet/PlainDatagramSocketImpl.c
dfuchs
parents: 32503
diff changeset
   974
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
#endif /* __linux__ */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
    (*env)->SetIntField(env, fdObj, IO_fd_fdID, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
 * Method:    datagramSocketClose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
Java_java_net_PlainDatagramSocketImpl_datagramSocketClose(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                                                          jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * REMIND: PUT A LOCK AROUND THIS CODE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
    if (fd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
    (*env)->SetIntField(env, fdObj, IO_fd_fdID, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
    NET_SocketClose(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
 * Set outgoing multicast interface designated by a NetworkInterface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
 * Throw exception if failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
static void mcast_set_if_by_if_v4(JNIEnv *env, jobject this, int fd, jobject value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
    static jfieldID ni_addrsID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
    struct in_addr in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
    jobjectArray addrArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
    jsize len;
51153
78f16a9f7563 8196224: Even better Internet address support
igerasim
parents: 51151
diff changeset
  1016
    jint family;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
    jobject addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1020
    if (ni_addrsID == NULL ) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        CHECK_NULL(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        ni_addrsID = (*env)->GetFieldID(env, c, "addrs",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                                        "[Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        CHECK_NULL(ni_addrsID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
    addrArray = (*env)->GetObjectField(env, value, ni_addrsID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
    len = (*env)->GetArrayLength(env, addrArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * Check that there is at least one address bound to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     * interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
    if (len < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            "bad argument for IP_MULTICAST_IF2: No IP addresses bound to interface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * We need an ipv4 address here
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     */
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
  1044
    in.s_addr = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
    for (i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        addr = (*env)->GetObjectArrayElement(env, addrArray, i);
51153
78f16a9f7563 8196224: Even better Internet address support
igerasim
parents: 51151
diff changeset
  1047
        family = getInetAddress_family(env, addr);
78f16a9f7563 8196224: Even better Internet address support
igerasim
parents: 51151
diff changeset
  1048
        JNU_CHECK_EXCEPTION(env);
78f16a9f7563 8196224: Even better Internet address support
igerasim
parents: 51151
diff changeset
  1049
        if (family == java_net_InetAddress_IPv4) {
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1050
            in.s_addr = htonl(getInetAddress_addr(env, addr));
51151
d6b131d2bc8b 8193419: Better Internet address support
vtewari
parents: 47216
diff changeset
  1051
            JNU_CHECK_EXCEPTION(env);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1056
    if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF,
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
  1057
                   (const char *)&in, sizeof(in)) < 0) {
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
  1058
        JNU_ThrowByNameWithMessageAndLastError
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
  1059
            (env, JNU_JAVANETPKG "SocketException", "Error setting socket option");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
 * Set outgoing multicast interface designated by a NetworkInterface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
 * Throw exception if failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
static void mcast_set_if_by_if_v6(JNIEnv *env, jobject this, int fd, jobject value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
    static jfieldID ni_indexID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
    int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
    if (ni_indexID == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        CHECK_NULL(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        ni_indexID = (*env)->GetFieldID(env, c, "index", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        CHECK_NULL(ni_indexID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
    index = (*env)->GetIntField(env, value, ni_indexID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1079
    if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1080
                   (const char*)&index, sizeof(index)) < 0) {
53381
e1da82072c79 8217311: Improve Exception thrown when MulticastSocket.setInterface fails on AIX(Unix)
clanger
parents: 52670
diff changeset
  1081
        if ((errno == EINVAL || errno == EADDRNOTAVAIL) && index > 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                "IPV6_MULTICAST_IF failed (interface has IPv4 "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                "address only?)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        } else {
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
  1086
            JNU_ThrowByNameWithMessageAndLastError
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
  1087
                (env, JNU_JAVANETPKG "SocketException", "Error setting socket option");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
 * Set outgoing multicast interface designated by an InetAddress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
 * Throw exception if failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
static void mcast_set_if_by_addr_v4(JNIEnv *env, jobject this, int fd, jobject value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
    struct in_addr in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1100
    in.s_addr = htonl( getInetAddress_addr(env, value) );
51151
d6b131d2bc8b 8193419: Better Internet address support
vtewari
parents: 47216
diff changeset
  1101
    JNU_CHECK_EXCEPTION(env);
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1102
    if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF,
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1103
                   (const char*)&in, sizeof(in)) < 0) {
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
  1104
        JNU_ThrowByNameWithMessageAndLastError
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
  1105
            (env, JNU_JAVANETPKG "SocketException", "Error setting socket option");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
 * Set outgoing multicast interface designated by an InetAddress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
 * Throw exception if failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
static void mcast_set_if_by_addr_v6(JNIEnv *env, jobject this, int fd, jobject value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
    static jclass ni_class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
    if (ni_class == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        CHECK_NULL(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        ni_class = (*env)->NewGlobalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        CHECK_NULL(ni_class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
    value = Java_java_net_NetworkInterface_getByInetAddress0(env, ni_class, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
    if (value == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        if (!(*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                 "bad argument for IP_MULTICAST_IF"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                 ": address not bound to any interface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
    mcast_set_if_by_if_v6(env, this, fd, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
 * Sets the multicast interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
 * SocketOptions.IP_MULTICAST_IF :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
 *      value is a InetAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
 *      IPv4:   set outgoing multicast interface using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
 *              IPPROTO_IP/IP_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
 *      IPv6:   Get the index of the interface to which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
 *              InetAddress is bound
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
 *              Set outgoing multicast interface using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
 *              IPPROTO_IPV6/IPV6_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
 * SockOptions.IF_MULTICAST_IF2 :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
 *      value is a NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
 *      IPv4:   Obtain IP address bound to network interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
 *              (NetworkInterface.addres[0])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
 *              set outgoing multicast interface using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
 *              IPPROTO_IP/IP_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
 *      IPv6:   Obtain NetworkInterface.index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
 *              Set outgoing multicast interface using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
 *              IPPROTO_IPV6/IPV6_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
static void setMulticastInterface(JNIEnv *env, jobject this, int fd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
                                  jint opt, jobject value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
    if (opt == java_net_SocketOptions_IP_MULTICAST_IF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
         * value is an InetAddress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
         */
13574
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1165
#ifdef __linux__
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1166
        mcast_set_if_by_addr_v4(env, this, fd, value);
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1167
        if (ipv6_available()) {
22095
e613710a08a6 8027903: java/net/MulticastSocket/SetGetNetworkInterfaceTest.java throws java.net.SocketException: Cannot assign requested address
msheppar
parents: 20821
diff changeset
  1168
            if ((*env)->ExceptionCheck(env)){
e613710a08a6 8027903: java/net/MulticastSocket/SetGetNetworkInterfaceTest.java throws java.net.SocketException: Cannot assign requested address
msheppar
parents: 20821
diff changeset
  1169
                (*env)->ExceptionClear(env);
e613710a08a6 8027903: java/net/MulticastSocket/SetGetNetworkInterfaceTest.java throws java.net.SocketException: Cannot assign requested address
msheppar
parents: 20821
diff changeset
  1170
            }
13574
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1171
            mcast_set_if_by_addr_v6(env, this, fd, value);
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1172
        }
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1173
#else  /* __linux__ not defined */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
            mcast_set_if_by_addr_v6(env, this, fd, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
            mcast_set_if_by_addr_v4(env, this, fd, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        }
13574
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1179
#endif  /* __linux__ */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
    if (opt == java_net_SocketOptions_IP_MULTICAST_IF2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
         * value is a NetworkInterface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
         */
13574
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1186
#ifdef __linux__
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1187
        mcast_set_if_by_if_v4(env, this, fd, value);
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1188
        if (ipv6_available()) {
22095
e613710a08a6 8027903: java/net/MulticastSocket/SetGetNetworkInterfaceTest.java throws java.net.SocketException: Cannot assign requested address
msheppar
parents: 20821
diff changeset
  1189
            if ((*env)->ExceptionCheck(env)){
e613710a08a6 8027903: java/net/MulticastSocket/SetGetNetworkInterfaceTest.java throws java.net.SocketException: Cannot assign requested address
msheppar
parents: 20821
diff changeset
  1190
                (*env)->ExceptionClear(env);
e613710a08a6 8027903: java/net/MulticastSocket/SetGetNetworkInterfaceTest.java throws java.net.SocketException: Cannot assign requested address
msheppar
parents: 20821
diff changeset
  1191
            }
13574
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1192
            mcast_set_if_by_if_v6(env, this, fd, value);
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1193
        }
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1194
#else  /* __linux__ not defined */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
            mcast_set_if_by_if_v6(env, this, fd, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
            mcast_set_if_by_if_v4(env, this, fd, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        }
13574
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1200
#endif  /* __linux__ */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
 * Enable/disable local loopback of multicast datagrams.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
static void mcast_set_loop_v4(JNIEnv *env, jobject this, int fd, jobject value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
    jclass cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
    jfieldID fid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
    jboolean on;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
    char loopback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
    cls = (*env)->FindClass(env, "java/lang/Boolean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
    CHECK_NULL(cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
    fid =  (*env)->GetFieldID(env, cls, "value", "Z");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
    CHECK_NULL(fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
    on = (*env)->GetBooleanField(env, value, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
    loopback = (!on ? 1 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
  1221
    if (NET_SetSockOpt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
  1222
                       (const void *)&loopback, sizeof(char)) < 0) {
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
  1223
        JNU_ThrowByNameWithMessageAndLastError
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
  1224
            (env, JNU_JAVANETPKG "SocketException", "Error setting socket option");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
 * Enable/disable local loopback of multicast datagrams.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
static void mcast_set_loop_v6(JNIEnv *env, jobject this, int fd, jobject value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
    jclass cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
    jfieldID fid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
    jboolean on;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
    int loopback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
    cls = (*env)->FindClass(env, "java/lang/Boolean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
    CHECK_NULL(cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
    fid =  (*env)->GetFieldID(env, cls, "value", "Z");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
    CHECK_NULL(fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
    on = (*env)->GetBooleanField(env, value, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
    loopback = (!on ? 1 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
  1246
    if (NET_SetSockOpt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
  1247
                       (const void *)&loopback, sizeof(int)) < 0) {
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
  1248
        JNU_ThrowByNameWithMessageAndLastError
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
  1249
            (env, JNU_JAVANETPKG "SocketException", "Error setting socket option");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
 * Sets the multicast loopback mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
static void setMulticastLoopbackMode(JNIEnv *env, jobject this, int fd,
41214
855ac576eb77 8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents: 39318
diff changeset
  1259
                                     jint opt, jobject value) {
13574
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1260
#ifdef __linux__
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1261
    mcast_set_loop_v4(env, this, fd, value);
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1262
    if (ipv6_available()) {
22095
e613710a08a6 8027903: java/net/MulticastSocket/SetGetNetworkInterfaceTest.java throws java.net.SocketException: Cannot assign requested address
msheppar
parents: 20821
diff changeset
  1263
        if ((*env)->ExceptionCheck(env)){
e613710a08a6 8027903: java/net/MulticastSocket/SetGetNetworkInterfaceTest.java throws java.net.SocketException: Cannot assign requested address
msheppar
parents: 20821
diff changeset
  1264
            (*env)->ExceptionClear(env);
e613710a08a6 8027903: java/net/MulticastSocket/SetGetNetworkInterfaceTest.java throws java.net.SocketException: Cannot assign requested address
msheppar
parents: 20821
diff changeset
  1265
        }
13574
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1266
        mcast_set_loop_v6(env, this, fd, value);
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1267
    }
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1268
#else  /* __linux__ not defined */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
    if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        mcast_set_loop_v6(env, this, fd, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        mcast_set_loop_v4(env, this, fd, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
    }
13574
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1274
#endif  /* __linux__ */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
 * Class:     java_net_PlainDatagramSocketImpl
30963
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 28549
diff changeset
  1279
 * Method:    socketSetOption0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
 * Signature: (ILjava/lang/Object;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
JNIEXPORT void JNICALL
41214
855ac576eb77 8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents: 39318
diff changeset
  1283
Java_java_net_PlainDatagramSocketImpl_socketSetOption0
855ac576eb77 8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents: 39318
diff changeset
  1284
  (JNIEnv *env, jobject this, jint opt, jobject value)
855ac576eb77 8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents: 39318
diff changeset
  1285
{
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
    int level, optname, optlen;
14195
57fe6c437882 8000206: Uninitialized variable in PlainDatagramSocketImpl.c
chegar
parents: 13999
diff changeset
  1288
    int optval;
57fe6c437882 8000206: Uninitialized variable in PlainDatagramSocketImpl.c
chegar
parents: 13999
diff changeset
  1289
    optlen = sizeof(int);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
     * Check that socket hasn't been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
    fd = getFD(env, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
    if (fd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
     * Check argument has been provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
    if (IS_NULL(value)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
        JNU_ThrowNullPointerException(env, "value argument");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
    /*
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
  1310
     * Setting the multicast interface handled separately
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
    if (opt == java_net_SocketOptions_IP_MULTICAST_IF ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
        opt == java_net_SocketOptions_IP_MULTICAST_IF2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
        setMulticastInterface(env, this, fd, opt, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
     * Setting the multicast loopback mode handled separately
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
    if (opt == java_net_SocketOptions_IP_MULTICAST_LOOP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
        setMulticastLoopbackMode(env, this, fd, opt, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
     * Map the Java level socket option to the platform specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     * level and option name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
    if (NET_MapSocketOption(opt, &level, &optname)) {
41214
855ac576eb77 8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents: 39318
diff changeset
  1332
        JNU_ThrowByName(env, "java/net/SocketException", "Invalid option");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
    switch (opt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        case java_net_SocketOptions_SO_SNDBUF :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
        case java_net_SocketOptions_SO_RCVBUF :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
        case java_net_SocketOptions_IP_TOS :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
                jclass cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
                jfieldID fid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
                cls = (*env)->FindClass(env, "java/lang/Integer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                CHECK_NULL(cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                fid =  (*env)->GetFieldID(env, cls, "value", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
                CHECK_NULL(fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
14195
57fe6c437882 8000206: Uninitialized variable in PlainDatagramSocketImpl.c
chegar
parents: 13999
diff changeset
  1349
                optval = (*env)->GetIntField(env, value, fid);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
        case java_net_SocketOptions_SO_REUSEADDR:
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32846
diff changeset
  1354
        case java_net_SocketOptions_SO_REUSEPORT:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        case java_net_SocketOptions_SO_BROADCAST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
                jclass cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
                jfieldID fid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
                jboolean on;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
                cls = (*env)->FindClass(env, "java/lang/Boolean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
                CHECK_NULL(cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
                fid =  (*env)->GetFieldID(env, cls, "value", "Z");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
                CHECK_NULL(fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
                on = (*env)->GetBooleanField(env, value, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
                /* SO_REUSEADDR or SO_BROADCAST */
14195
57fe6c437882 8000206: Uninitialized variable in PlainDatagramSocketImpl.c
chegar
parents: 13999
diff changeset
  1369
                optval = (on ? 1 : 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
        default :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
                "Socket option not supported by PlainDatagramSocketImp");
22651
bfc6ca5245fd 8033689: PlainDatagramSocketImpl missing returns after throwing an exception
chegar
parents: 22646
diff changeset
  1377
            return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
    if (NET_SetSockOpt(fd, level, optname, (const void *)&optval, optlen) < 0) {
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
  1382
        JNU_ThrowByNameWithMessageAndLastError
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
  1383
            (env, JNU_JAVANETPKG "SocketException", "Error setting socket option");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
 * Return the multicast interface:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
 * SocketOptions.IP_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
 *      IPv4:   Query IPPROTO_IP/IP_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
 *              Create InetAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
 *              IP_MULTICAST_IF returns struct ip_mreqn on 2.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
 *              kernel but struct in_addr on 2.4 kernel
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
  1397
 *      IPv6:   Query IPPROTO_IPV6 / IPV6_MULTICAST_IF
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
 *              If index == 0 return InetAddress representing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
 *              anyLocalAddress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
 *              If index > 0 query NetworkInterface by index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
 *              and returns addrs[0]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
 * SocketOptions.IP_MULTICAST_IF2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
 *      IPv4:   Query IPPROTO_IP/IP_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
 *              Query NetworkInterface by IP address and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
 *              return the NetworkInterface that the address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
 *              is bound too.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
 *      IPv6:   Query IPPROTO_IPV6 / IPV6_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
 *              (except Linux .2 kernel)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
 *              Query NetworkInterface by index and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
 *              return NetworkInterface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
jobject getMulticastInterface(JNIEnv *env, jobject this, int fd, jint opt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
    jboolean isIPV4 = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
    if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
        isIPV4 = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
     * IPv4 implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
    if (isIPV4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
        static jclass inet4_class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
        static jmethodID inet4_ctrID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
        static jclass ni_class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
        static jmethodID ni_ctrID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
        static jfieldID ni_indexID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
        static jfieldID ni_addrsID;
28549
a80ec9ad6324 8056264: Multicast support improvements
msheppar
parents: 28059
diff changeset
  1431
        static jfieldID ni_nameID;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
        jobjectArray addrArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
        jobject addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
        jobject ni;
28549
a80ec9ad6324 8056264: Multicast support improvements
msheppar
parents: 28059
diff changeset
  1436
        jobject ni_name;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
        struct in_addr in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
        struct in_addr *inP = &in;
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1440
        socklen_t len = sizeof(struct in_addr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1442
        if (getsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF,
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1443
                       (char *)inP, &len) < 0) {
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
  1444
            JNU_ThrowByNameWithMessageAndLastError
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
  1445
                (env, JNU_JAVANETPKG "SocketException", "Error getting socket option");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
         * Construct and populate an Inet4Address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
        if (inet4_class == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
            jclass c = (*env)->FindClass(env, "java/net/Inet4Address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
            CHECK_NULL_RETURN(c, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
            inet4_ctrID = (*env)->GetMethodID(env, c, "<init>", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
            CHECK_NULL_RETURN(inet4_ctrID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
            inet4_class = (*env)->NewGlobalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
            CHECK_NULL_RETURN(inet4_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
        addr = (*env)->NewObject(env, inet4_class, inet4_ctrID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
        CHECK_NULL_RETURN(addr, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1463
        setInetAddress_addr(env, addr, ntohl(in.s_addr));
51151
d6b131d2bc8b 8193419: Better Internet address support
vtewari
parents: 47216
diff changeset
  1464
        JNU_CHECK_EXCEPTION_RETURN(env, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
         * For IP_MULTICAST_IF return InetAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
        if (opt == java_net_SocketOptions_IP_MULTICAST_IF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
            return addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
         * For IP_MULTICAST_IF2 we get the NetworkInterface for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
         * this address and return it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
        if (ni_class == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
            jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
            CHECK_NULL_RETURN(c, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
            ni_ctrID = (*env)->GetMethodID(env, c, "<init>", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
            CHECK_NULL_RETURN(ni_ctrID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
            ni_indexID = (*env)->GetFieldID(env, c, "index", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
            CHECK_NULL_RETURN(ni_indexID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
            ni_addrsID = (*env)->GetFieldID(env, c, "addrs",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
                                            "[Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
            CHECK_NULL_RETURN(ni_addrsID, NULL);
28549
a80ec9ad6324 8056264: Multicast support improvements
msheppar
parents: 28059
diff changeset
  1487
            ni_nameID = (*env)->GetFieldID(env, c,"name", "Ljava/lang/String;");
a80ec9ad6324 8056264: Multicast support improvements
msheppar
parents: 28059
diff changeset
  1488
            CHECK_NULL_RETURN(ni_nameID, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
            ni_class = (*env)->NewGlobalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
            CHECK_NULL_RETURN(ni_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
        ni = Java_java_net_NetworkInterface_getByInetAddress0(env, ni_class, addr);
54219
35e8d1eb4040 8170494: JNI exception pending in PlainDatagramSocketImpl.c
igerasim
parents: 53381
diff changeset
  1493
        JNU_CHECK_EXCEPTION_RETURN(env, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
        if (ni) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
            return ni;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
        }
59313
6f12009ea9d7 8233307: MulticastSocket getOption(IP_MULTICAST_IF) returns interface when not set
pconcannon
parents: 55375
diff changeset
  1497
        return NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
     * IPv6 implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
    if ((opt == java_net_SocketOptions_IP_MULTICAST_IF) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
        (opt == java_net_SocketOptions_IP_MULTICAST_IF2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
        static jclass ni_class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
        static jmethodID ni_ctrID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
        static jfieldID ni_indexID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
        static jfieldID ni_addrsID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
        static jclass ia_class;
28549
a80ec9ad6324 8056264: Multicast support improvements
msheppar
parents: 28059
diff changeset
  1512
        static jfieldID ni_nameID;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
        static jmethodID ia_anyLocalAddressID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
28549
a80ec9ad6324 8056264: Multicast support improvements
msheppar
parents: 28059
diff changeset
  1515
        int index = 0;
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1516
        socklen_t len = sizeof(index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
        jobjectArray addrArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
        jobject addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
        jobject ni;
28549
a80ec9ad6324 8056264: Multicast support improvements
msheppar
parents: 28059
diff changeset
  1521
        jobject ni_name;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1523
        if (getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1524
                       (char*)&index, &len) < 0) {
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
  1525
            JNU_ThrowByNameWithMessageAndLastError
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
  1526
                (env, JNU_JAVANETPKG "SocketException", "Error getting socket option");
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
  1527
            return NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
        if (ni_class == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
            jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
            CHECK_NULL_RETURN(c, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
            ni_ctrID = (*env)->GetMethodID(env, c, "<init>", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
            CHECK_NULL_RETURN(ni_ctrID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
            ni_indexID = (*env)->GetFieldID(env, c, "index", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
            CHECK_NULL_RETURN(ni_indexID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
            ni_addrsID = (*env)->GetFieldID(env, c, "addrs",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
                                            "[Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
            CHECK_NULL_RETURN(ni_addrsID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
            ia_class = (*env)->FindClass(env, "java/net/InetAddress");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
            CHECK_NULL_RETURN(ia_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
            ia_class = (*env)->NewGlobalRef(env, ia_class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
            CHECK_NULL_RETURN(ia_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
            ia_anyLocalAddressID = (*env)->GetStaticMethodID(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
                                                             ia_class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
                                                             "anyLocalAddress",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
                                                             "()Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
            CHECK_NULL_RETURN(ia_anyLocalAddressID, NULL);
28549
a80ec9ad6324 8056264: Multicast support improvements
msheppar
parents: 28059
diff changeset
  1550
            ni_nameID = (*env)->GetFieldID(env, c,"name", "Ljava/lang/String;");
a80ec9ad6324 8056264: Multicast support improvements
msheppar
parents: 28059
diff changeset
  1551
            CHECK_NULL_RETURN(ni_nameID, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
            ni_class = (*env)->NewGlobalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
            CHECK_NULL_RETURN(ni_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
         * If multicast to a specific interface then return the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
         * interface (for IF2) or the any address on that interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
         * (for IF).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
        if (index > 0) {
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 910
diff changeset
  1562
            ni = Java_java_net_NetworkInterface_getByIndex0(env, ni_class,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
                                                                   index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
            if (ni == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
                char errmsg[255];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
                sprintf(errmsg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
                        "IPV6_MULTICAST_IF returned index to unrecognized interface: %d",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
                        index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
                JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", errmsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
                return NULL;
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
             * For IP_MULTICAST_IF2 return the NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
            if (opt == java_net_SocketOptions_IP_MULTICAST_IF2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
                return ni;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
             * For IP_MULTICAST_IF return addrs[0]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
            addrArray = (*env)->GetObjectField(env, ni, ni_addrsID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
            if ((*env)->GetArrayLength(env, addrArray) < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
                JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
                    "IPV6_MULTICAST_IF returned interface without IP bindings");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
                return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
            addr = (*env)->GetObjectArrayElement(env, addrArray, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
            return addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
         * Multicast to any address - return anyLocalAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
         * or a NetworkInterface with addrs[0] set to anyLocalAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
        addr = (*env)->CallStaticObjectMethod(env, ia_class, ia_anyLocalAddressID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
                                              NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
        if (opt == java_net_SocketOptions_IP_MULTICAST_IF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
            return addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
 * Returns relevant info as a jint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
 * Method:    socketGetOption
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
 * Signature: (I)Ljava/lang/Object;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
JNIEXPORT jobject JNICALL
41214
855ac576eb77 8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents: 39318
diff changeset
  1618
Java_java_net_PlainDatagramSocketImpl_socketGetOption
855ac576eb77 8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents: 39318
diff changeset
  1619
  (JNIEnv *env, jobject this, jint opt)
855ac576eb77 8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents: 39318
diff changeset
  1620
{
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
    int level, optname, optlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
    union {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
        char c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
    } optval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
    fd = getFD(env, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
    if (fd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
                        "socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
    /*
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
  1636
     * Handle IP_MULTICAST_IF separately
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
    if (opt == java_net_SocketOptions_IP_MULTICAST_IF ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
        opt == java_net_SocketOptions_IP_MULTICAST_IF2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
        return getMulticastInterface(env, this, fd, opt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
     * SO_BINDADDR implemented using getsockname
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
    if (opt == java_net_SocketOptions_SO_BINDADDR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
        /* find out local IP address */
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
  1649
        SOCKETADDRESS sa;
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 41214
diff changeset
  1650
        socklen_t len = sizeof(SOCKETADDRESS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
        int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
        jobject iaObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
  1654
        if (getsockname(fd, &sa.sa, &len) == -1) {
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
  1655
            JNU_ThrowByNameWithMessageAndLastError
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
  1656
                (env, JNU_JAVANETPKG "SocketException", "Error getting socket name");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
        }
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
  1659
        iaObj = NET_SockaddrToInetAddress(env, &sa, &port);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
        return iaObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
     * Map the Java level socket option to the platform specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
     * level and option name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
    if (NET_MapSocketOption(opt, &level, &optname)) {
41214
855ac576eb77 8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents: 39318
diff changeset
  1669
        JNU_ThrowByName(env, "java/net/SocketException", "Invalid option");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
    if (opt == java_net_SocketOptions_IP_MULTICAST_LOOP &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
        level == IPPROTO_IP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
        optlen = sizeof(optval.c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
        optlen = sizeof(optval.i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
    if (NET_GetSockOpt(fd, level, optname, (void *)&optval, &optlen) < 0) {
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
  1681
        JNU_ThrowByNameWithMessageAndLastError
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
  1682
            (env, JNU_JAVANETPKG "SocketException", "Error getting socket option");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
    switch (opt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
        case java_net_SocketOptions_IP_MULTICAST_LOOP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
            /* getLoopbackMode() returns true if IP_MULTICAST_LOOP disabled */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
            if (level == IPPROTO_IP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
                return createBoolean(env, (int)!optval.c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
                return createBoolean(env, !optval.i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
        case java_net_SocketOptions_SO_BROADCAST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
        case java_net_SocketOptions_SO_REUSEADDR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
            return createBoolean(env, optval.i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32846
diff changeset
  1699
        case java_net_SocketOptions_SO_REUSEPORT:
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32846
diff changeset
  1700
            return createBoolean(env, optval.i);
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32846
diff changeset
  1701
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
        case java_net_SocketOptions_SO_SNDBUF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
        case java_net_SocketOptions_SO_RCVBUF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
        case java_net_SocketOptions_IP_TOS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
            return createInteger(env, optval.i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
  1709
    /* should never reach here */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
 * Multicast-related calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
Java_java_net_PlainDatagramSocketImpl_setTTL(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
                                             jbyte ttl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
    jint ittl = ttl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
    if (ittl < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
        ittl += 0x100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
    Java_java_net_PlainDatagramSocketImpl_setTimeToLive(env, this, ittl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
 * Set TTL for a socket. Throw exception if failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
static void setTTL(JNIEnv *env, int fd, jint ttl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
    char ittl = (char)ttl;
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1732
    if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, (char*)&ittl,
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1733
                   sizeof(ittl)) < 0) {
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
  1734
        JNU_ThrowByNameWithMessageAndLastError
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
  1735
            (env, JNU_JAVANETPKG "SocketException", "Error setting socket option");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
 * Set hops limit for a socket. Throw exception if failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
static void setHopLimit(JNIEnv *env, int fd, jint ttl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
    int ittl = (int)ttl;
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1744
    if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1745
                   (char*)&ittl, sizeof(ittl)) < 0) {
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
  1746
        JNU_ThrowByNameWithMessageAndLastError
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
  1747
            (env, JNU_JAVANETPKG "SocketException", "Error setting socket option");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
 * Method:    setTTL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
 * Signature: (B)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
Java_java_net_PlainDatagramSocketImpl_setTimeToLive(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
                                                    jint ttl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
    /* it is important to cast this to a char, otherwise setsockopt gets confused */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
    }
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
  1771
    /* setsockopt to be correct TTL */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
    setTTL(env, fd, ttl);
22651
bfc6ca5245fd 8033689: PlainDatagramSocketImpl missing returns after throwing an exception
chegar
parents: 22646
diff changeset
  1774
    JNU_CHECK_EXCEPTION(env);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
    if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
        setHopLimit(env, fd, ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
    }
13574
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1778
#else  /*  __linux__ not defined */
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1779
    if (ipv6_available()) {
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1780
        setHopLimit(env, fd, ttl);
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1781
    } else {
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1782
        setTTL(env, fd, ttl);
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1783
    }
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1784
#endif  /* __linux__ */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
 * Method:    getTTL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
 * Signature: ()B
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
JNIEXPORT jbyte JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
Java_java_net_PlainDatagramSocketImpl_getTTL(JNIEnv *env, jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
    return (jbyte)Java_java_net_PlainDatagramSocketImpl_getTimeToLive(env, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
 * Method:    getTTL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
 * Signature: ()B
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
Java_java_net_PlainDatagramSocketImpl_getTimeToLive(JNIEnv *env, jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
    jint fd = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
    }
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
  1816
    /* getsockopt of TTL */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
    if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
        int ttl = 0;
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1819
        socklen_t len = sizeof(ttl);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1821
        if (getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1822
                       (char*)&ttl, &len) < 0) {
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
  1823
            JNU_ThrowByNameWithMessageAndLastError
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 36115
diff changeset
  1824
                (env, JNU_JAVANETPKG "SocketException", "Error getting socket option");
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1825
            return -1;
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1826
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
        return (jint)ttl;
41834
99c4f72c0a91 8168771: Remove #ifdef AF_INET6 guards in libnet native coding
clanger
parents: 41771
diff changeset
  1828
    } else {
99c4f72c0a91 8168771: Remove #ifdef AF_INET6 guards in libnet native coding
clanger
parents: 41771
diff changeset
  1829
        u_char ttl = 0;
99c4f72c0a91 8168771: Remove #ifdef AF_INET6 guards in libnet native coding
clanger
parents: 41771
diff changeset
  1830
        socklen_t len = sizeof(ttl);
99c4f72c0a91 8168771: Remove #ifdef AF_INET6 guards in libnet native coding
clanger
parents: 41771
diff changeset
  1831
        if (getsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL,
99c4f72c0a91 8168771: Remove #ifdef AF_INET6 guards in libnet native coding
clanger
parents: 41771
diff changeset
  1832
                       (char*)&ttl, &len) < 0) {
99c4f72c0a91 8168771: Remove #ifdef AF_INET6 guards in libnet native coding
clanger
parents: 41771
diff changeset
  1833
            JNU_ThrowByNameWithMessageAndLastError
99c4f72c0a91 8168771: Remove #ifdef AF_INET6 guards in libnet native coding
clanger
parents: 41771
diff changeset
  1834
                (env, JNU_JAVANETPKG "SocketException", "Error getting socket option");
99c4f72c0a91 8168771: Remove #ifdef AF_INET6 guards in libnet native coding
clanger
parents: 41771
diff changeset
  1835
            return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
        }
41834
99c4f72c0a91 8168771: Remove #ifdef AF_INET6 guards in libnet native coding
clanger
parents: 41771
diff changeset
  1837
        return (jint)ttl;
99c4f72c0a91 8168771: Remove #ifdef AF_INET6 guards in libnet native coding
clanger
parents: 41771
diff changeset
  1838
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
 * mcast_join_leave: Join or leave a multicast group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
 * For IPv4 sockets use IP_ADD_MEMBERSHIP/IP_DROP_MEMBERSHIP socket option
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
 * to join/leave multicast group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
 * For IPv6 sockets use IPV6_ADD_MEMBERSHIP/IPV6_DROP_MEMBERSHIP socket option
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
 * to join/leave multicast group. If multicast group is an IPv4 address then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
 * an IPv4-mapped address is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
 * On Linux with IPv6 if we wish to join/leave an IPv4 multicast group then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
 * we must use the IPv4 socket options. This is because the IPv6 socket options
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
 * don't support IPv4-mapped addresses. This is true as per 2.2.19 and 2.4.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
 * kernel releases. In the future it's possible that IP_ADD_MEMBERSHIP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
 * will be updated to return ENOPROTOOPT if uses with an IPv6 socket (Solaris
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
 * already does this). Thus to cater for this we first try with the IPv4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
 * socket options and if they fail we use the IPv6 socket options. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
 * seems a reasonable failsafe solution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
static void mcast_join_leave(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
                             jobject iaObj, jobject niObj,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
                             jboolean join) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
    jint fd;
51153
78f16a9f7563 8196224: Even better Internet address support
igerasim
parents: 51151
diff changeset
  1867
    jint family;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
    jint ipv6_join_leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
    if (IS_NULL(iaObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
        JNU_ThrowNullPointerException(env, "iaObj");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
     * Determine if this is an IPv4 or IPv6 join/leave.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
    ipv6_join_leave = ipv6_available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
#ifdef __linux__
51153
78f16a9f7563 8196224: Even better Internet address support
igerasim
parents: 51151
diff changeset
  1888
    family = getInetAddress_family(env, iaObj);
78f16a9f7563 8196224: Even better Internet address support
igerasim
parents: 51151
diff changeset
  1889
    JNU_CHECK_EXCEPTION(env);
78f16a9f7563 8196224: Even better Internet address support
igerasim
parents: 51151
diff changeset
  1890
    if (family == java_net_InetAddress_IPv4) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
        ipv6_join_leave = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
     * For IPv4 join use IP_ADD_MEMBERSHIP/IP_DROP_MEMBERSHIP socket option
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
     * On Linux if IPv4 or IPv6 use IP_ADD_MEMBERSHIP/IP_DROP_MEMBERSHIP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
    if (!ipv6_join_leave) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
        struct ip_mreqn mname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
        struct ip_mreq mname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
        int mname_len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
         * joinGroup(InetAddress, NetworkInterface) implementation :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
         * Linux/IPv6:  use ip_mreqn structure populated with multicast
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
         *              address and interface index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
         * IPv4:        use ip_mreq structure populated with multicast
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
         *              address and first address obtained from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
         *              NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
        if (niObj != NULL) {
41834
99c4f72c0a91 8168771: Remove #ifdef AF_INET6 guards in libnet native coding
clanger
parents: 41771
diff changeset
  1919
#if defined(__linux__)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
            if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
                static jfieldID ni_indexID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
                if (ni_indexID == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
                    jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
                    CHECK_NULL(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
                    ni_indexID = (*env)->GetFieldID(env, c, "index", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
                    CHECK_NULL(ni_indexID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1930
                mname.imr_multiaddr.s_addr = htonl(getInetAddress_addr(env, iaObj));
51151
d6b131d2bc8b 8193419: Better Internet address support
vtewari
parents: 47216
diff changeset
  1931
                JNU_CHECK_EXCEPTION(env);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
                mname.imr_address.s_addr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
                mname.imr_ifindex =  (*env)->GetIntField(env, niObj, ni_indexID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
                mname_len = sizeof(struct ip_mreqn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
                jobjectArray addrArray = (*env)->GetObjectField(env, niObj, ni_addrsID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
                jobject addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
                if ((*env)->GetArrayLength(env, addrArray) < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
                    JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
                        "bad argument for IP_ADD_MEMBERSHIP: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
                        "No IP addresses bound to interface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
                addr = (*env)->GetObjectArrayElement(env, addrArray, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1949
                mname.imr_multiaddr.s_addr = htonl(getInetAddress_addr(env, iaObj));
51151
d6b131d2bc8b 8193419: Better Internet address support
vtewari
parents: 47216
diff changeset
  1950
                JNU_CHECK_EXCEPTION(env);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
#ifdef __linux__
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1952
                mname.imr_address.s_addr = htonl(getInetAddress_addr(env, addr));
51151
d6b131d2bc8b 8193419: Better Internet address support
vtewari
parents: 47216
diff changeset
  1953
                JNU_CHECK_EXCEPTION(env);
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
  1954
                mname.imr_ifindex = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
#else
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1956
                mname.imr_interface.s_addr = htonl(getInetAddress_addr(env, addr));
51151
d6b131d2bc8b 8193419: Better Internet address support
vtewari
parents: 47216
diff changeset
  1957
                JNU_CHECK_EXCEPTION(env);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
                mname_len = sizeof(struct ip_mreq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
         * joinGroup(InetAddress) implementation :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
         * Linux/IPv6:  use ip_mreqn structure populated with multicast
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
         *              address and interface index. index obtained
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
         *              from cached value or IPV6_MULTICAST_IF.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
         * IPv4:        use ip_mreq structure populated with multicast
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
         *              address and local address obtained from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
         *              IP_MULTICAST_IF. On Linux IP_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
         *              returns different structure depending on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
         *              kernel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
        if (niObj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
41834
99c4f72c0a91 8168771: Remove #ifdef AF_INET6 guards in libnet native coding
clanger
parents: 41771
diff changeset
  1980
#if defined(__linux__)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
            if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
                int index;
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1984
                socklen_t len = sizeof(index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1986
                if (getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1987
                               (char*)&index, &len) < 0) {
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
  1988
                    NET_ThrowCurrent(env, "getsockopt IPV6_MULTICAST_IF failed");
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
  1989
                    return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1992
                mname.imr_multiaddr.s_addr = htonl(getInetAddress_addr(env, iaObj));
51151
d6b131d2bc8b 8193419: Better Internet address support
vtewari
parents: 47216
diff changeset
  1993
                JNU_CHECK_EXCEPTION(env);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
                mname.imr_address.s_addr = 0 ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
                mname.imr_ifindex = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
                mname_len = sizeof(struct ip_mreqn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
                struct in_addr in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
                struct in_addr *inP = &in;
6886
d818a2dfb100 6989690: java/net native code compiler warnings
chegar
parents: 6299
diff changeset
  2002
                socklen_t len = sizeof(struct in_addr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
                if (getsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, (char *)inP, &len) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
                    NET_ThrowCurrent(env, "getsockopt IP_MULTICAST_IF failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
#ifdef __linux__
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
  2010
                mname.imr_address.s_addr = in.s_addr;
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
  2011
                mname.imr_ifindex = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
                mname.imr_interface.s_addr = in.s_addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
#endif
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  2015
                mname.imr_multiaddr.s_addr = htonl(getInetAddress_addr(env, iaObj));
51151
d6b131d2bc8b 8193419: Better Internet address support
vtewari
parents: 47216
diff changeset
  2016
                JNU_CHECK_EXCEPTION(env);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
                mname_len = sizeof(struct ip_mreq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
         * Join the multicast group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
         */
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  2025
        if (setsockopt(fd, IPPROTO_IP, (join ? IP_ADD_MEMBERSHIP:IP_DROP_MEMBERSHIP),
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  2026
                       (char *) &mname, mname_len) < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
             * If IP_ADD_MEMBERSHIP returns ENOPROTOOPT on Linux and we've got
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
             * IPv6 enabled then it's possible that the kernel has been fixed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
             * so we switch to IPV6_ADD_MEMBERSHIP socket option.
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
  2032
             * As of 2.4.7 kernel IPV6_ADD_MEMBERSHIP can't handle IPv4-mapped
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
  2033
             * addresses so we have to use IP_ADD_MEMBERSHIP for IPv4 multicast
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
  2034
             * groups. However if the socket is an IPv6 socket then setsockopt
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
  2035
             * should return ENOPROTOOPT. We assume this will be fixed in Linux
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
             * at some stage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
             */
41834
99c4f72c0a91 8168771: Remove #ifdef AF_INET6 guards in libnet native coding
clanger
parents: 41771
diff changeset
  2038
#if defined(__linux__)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
            if (errno == ENOPROTOOPT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
                if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
                    ipv6_join_leave = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
                    errno = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
                } else  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
                    errno = ENOPROTOOPT;    /* errno can be changed by ipv6_available */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
            if (errno) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
                if (join) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
                    NET_ThrowCurrent(env, "setsockopt IP_ADD_MEMBERSHIP failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
                    if (errno == ENOENT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
                        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
                            "Not a member of the multicast group");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
                        NET_ThrowCurrent(env, "setsockopt IP_DROP_MEMBERSHIP failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
                }
22651
bfc6ca5245fd 8033689: PlainDatagramSocketImpl missing returns after throwing an exception
chegar
parents: 22646
diff changeset
  2058
                return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
         * If we haven't switched to IPv6 socket option then we're done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
        if (!ipv6_join_leave) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
     * IPv6 join. If it's an IPv4 multicast group then we use an IPv4-mapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
     * address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
        struct ipv6_mreq mname6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
        jbyteArray ipaddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
        jbyte caddr[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
        jint family;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
        jint address;
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
  2081
        family = getInetAddress_family(env, iaObj) == java_net_InetAddress_IPv4 ?
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
  2082
            AF_INET : AF_INET6;
51151
d6b131d2bc8b 8193419: Better Internet address support
vtewari
parents: 47216
diff changeset
  2083
        JNU_CHECK_EXCEPTION(env);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
        if (family == AF_INET) { /* will convert to IPv4-mapped address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
            memset((char *) caddr, 0, 16);
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  2086
            address = getInetAddress_addr(env, iaObj);
51151
d6b131d2bc8b 8193419: Better Internet address support
vtewari
parents: 47216
diff changeset
  2087
            JNU_CHECK_EXCEPTION(env);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
            caddr[10] = 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
            caddr[11] = 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
            caddr[12] = ((address >> 24) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
            caddr[13] = ((address >> 16) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
            caddr[14] = ((address >> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
            caddr[15] = (address & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
        } else {
22956
322ce28a7e20 8034182: Misc. warnings in java.net code
alanb
parents: 22651
diff changeset
  2096
            getInet6Address_ipaddress(env, iaObj, (char*)caddr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
        memcpy((void *)&(mname6.ipv6mr_multiaddr), caddr, sizeof(struct in6_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
        if (IS_NULL(niObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
            int index;
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  2102
            socklen_t len = sizeof(index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  2104
            if (getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  2105
                           (char*)&index, &len) < 0) {
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
  2106
                NET_ThrowCurrent(env, "getsockopt IPV6_MULTICAST_IF failed");
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  2107
                return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
            mname6.ipv6mr_interface = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
            jint idx = (*env)->GetIntField(env, niObj, ni_indexID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
            mname6.ipv6mr_interface = idx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
  2115
#if defined(_ALLBSD_SOURCE)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
  2116
#define ADD_MEMBERSHIP          IPV6_JOIN_GROUP
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
  2117
#define DRP_MEMBERSHIP          IPV6_LEAVE_GROUP
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
  2118
#define S_ADD_MEMBERSHIP        "IPV6_JOIN_GROUP"
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
  2119
#define S_DRP_MEMBERSHIP        "IPV6_LEAVE_GROUP"
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
  2120
#else
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
  2121
#define ADD_MEMBERSHIP          IPV6_ADD_MEMBERSHIP
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
  2122
#define DRP_MEMBERSHIP          IPV6_DROP_MEMBERSHIP
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
  2123
#define S_ADD_MEMBERSHIP        "IPV6_ADD_MEMBERSHIP"
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
  2124
#define S_DRP_MEMBERSHIP        "IPV6_DROP_MEMBERSHIP"
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
  2125
#endif
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
  2126
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
        /* Join the multicast group */
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  2128
        if (setsockopt(fd, IPPROTO_IPV6, (join ? ADD_MEMBERSHIP : DRP_MEMBERSHIP),
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  2129
                       (char *) &mname6, sizeof (mname6)) < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
            if (join) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
  2132
                NET_ThrowCurrent(env, "setsockopt " S_ADD_MEMBERSHIP " failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
                if (errno == ENOENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
                   JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
                        "Not a member of the multicast group");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
                } else {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
  2138
                    NET_ThrowCurrent(env, "setsockopt " S_DRP_MEMBERSHIP " failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
 * Method:    join
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
 * Signature: (Ljava/net/InetAddress;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
Java_java_net_PlainDatagramSocketImpl_join(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
                                           jobject iaObj, jobject niObj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
    mcast_join_leave(env, this, iaObj, niObj, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
 * Method:    leave
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
 * Signature: (Ljava/net/InetAddress;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
Java_java_net_PlainDatagramSocketImpl_leave(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
                                            jobject iaObj, jobject niObj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
    mcast_join_leave(env, this, iaObj, niObj, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
}
32503
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 30963
diff changeset
  2168
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 30963
diff changeset
  2169
/*
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 30963
diff changeset
  2170
 * Class:     java_net_PlainDatagramSocketImpl
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 30963
diff changeset
  2171
 * Method:    dataAvailable
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 30963
diff changeset
  2172
 * Signature: ()I
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 30963
diff changeset
  2173
 */
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 30963
diff changeset
  2174
JNIEXPORT jint JNICALL
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 30963
diff changeset
  2175
Java_java_net_PlainDatagramSocketImpl_dataAvailable(JNIEnv *env, jobject this)
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 30963
diff changeset
  2176
{
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 30963
diff changeset
  2177
    int fd, retval;
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 30963
diff changeset
  2178
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 30963
diff changeset
  2179
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 30963
diff changeset
  2180
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 30963
diff changeset
  2181
    if (IS_NULL(fdObj)) {
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 30963
diff changeset
  2182
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 30963
diff changeset
  2183
                        "Socket closed");
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 30963
diff changeset
  2184
        return -1;
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 30963
diff changeset
  2185
    }
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 30963
diff changeset
  2186
    fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 30963
diff changeset
  2187
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 30963
diff changeset
  2188
    if (ioctl(fd, FIONREAD, &retval) < 0) {
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 30963
diff changeset
  2189
        return -1;
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 30963
diff changeset
  2190
    }
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 30963
diff changeset
  2191
    return retval;
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 30963
diff changeset
  2192
}