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