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