jdk/src/java.base/unix/native/libnet/PlainDatagramSocketImpl.c
author martin
Thu, 30 Oct 2014 07:31:41 -0700
changeset 28059 e576535359cc
parent 25859 3317bb8137f4
child 28549 a80ec9ad6324
permissions -rw-r--r--
8067377: My hobby: caning, then then canning, the the can-can Summary: Fix ALL the stutters! Reviewed-by: rriggs, mchung, lancea
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) {
23048
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   504
        int ret = NET_Timeout(fd, timeout);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        if (ret == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                            "Peek timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            return ret;
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   509
        } else if (ret == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            if (errno == EBADF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
23048
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   512
            } else if (errno == ENOMEM) {
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   513
                 JNU_ThrowOutOfMemoryError(env, "NET_Timeout native heap allocation failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "Peek failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   521
    n = NET_RecvFrom(fd, buf, 1, MSG_PEEK, (struct sockaddr *)&remote_addr, &slen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   523
    if (n == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        if (errno == ECONNREFUSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            int orig_errno = errno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            (void) recv(fd, buf, 1, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            errno = orig_errno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        if (errno == ECONNREFUSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                            "ICMP Port Unreachable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            if (errno == EBADF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "Peek failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    iaObj = NET_SockaddrToInetAddress(env, (struct sockaddr *)&remote_addr, &port);
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
   546
#ifdef AF_INET6
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   547
    family = getInetAddress_family(env, iaObj) == IPv4? AF_INET : AF_INET6;
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
   548
#else
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
   549
    family = AF_INET;
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
   550
#endif
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
   551
    if (family == AF_INET) { /* this API can't handle IPV6 addresses */
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   552
        int address = getInetAddress_addr(env, iaObj);
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   553
        setInetAddress_addr(env, addressObj, address);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    return port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
Java_java_net_PlainDatagramSocketImpl_peekData(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                                           jobject packet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    char BUF[MAX_BUFFER_LEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    char *fullPacket = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    int mallocedPacket = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    jint timeout = (*env)->GetIntField(env, this, pdsi_timeoutID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    jbyteArray packetBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    jint packetBufferOffset, packetBufferLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    SOCKADDR remote_addr;
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   575
    socklen_t slen = SOCKADDR_LEN;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    if (IS_NULL(packet)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        JNU_ThrowNullPointerException(env, "packet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    packetBuffer = (*env)->GetObjectField(env, packet, dp_bufID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    if (IS_NULL(packetBuffer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        JNU_ThrowNullPointerException(env, "packet buffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    packetBufferOffset = (*env)->GetIntField(env, packet, dp_offsetID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    packetBufferLen = (*env)->GetIntField(env, packet, dp_bufLengthID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    if (timeout) {
23048
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   599
        int ret = NET_Timeout(fd, timeout);
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) {
23048
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   605
            if (errno == ENOMEM) {
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   606
                JNU_ThrowOutOfMemoryError(env, "NET_Timeout native heap allocation failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
#ifdef __linux__
23048
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   608
            } else if (errno == EBADF) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "Receive failed");
23048
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   612
#else
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   613
            } else {
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   614
                JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   615
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    if (packetBufferLen > MAX_BUFFER_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        /* 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
   624
         * reads and writes of byte arrays of size greater
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
         * than 2048 bytes into several operations of size 2048.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
         * This saves a malloc()/memcpy()/free() for big
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
         * buffers.  This is OK for file IO and TCP, but that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
         * strategy violates the semantics of a datagram protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
         * (one big send) != (several smaller sends).  So here
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
   630
         * 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
   631
         * than 65,536 (0xFFFF), the max size of an IP packet.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
         * anything bigger is truncated anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
         * We may want to use a smarter allocation scheme at some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
         * point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        if (packetBufferLen > MAX_PACKET_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            packetBufferLen = MAX_PACKET_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        fullPacket = (char *)malloc(packetBufferLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        if (!fullPacket) {
13245
7ab3ef5b9520 7181353: Update error message to distinguish native OOM and java OOM in net
zhouyx
parents: 12551
diff changeset
   643
            JNU_ThrowOutOfMemoryError(env, "Peek buffer native heap allocation failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            mallocedPacket = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        fullPacket = &(BUF[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    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
   653
                     (struct sockaddr *)&remote_addr, &slen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    /* truncate the data if the packet's length is too small */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    if (n > packetBufferLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        n = packetBufferLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    }
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   658
    if (n == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        if (errno == ECONNREFUSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            int orig_errno = errno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            (void) recv(fd, fullPacket, 1, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            errno = orig_errno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        (*env)->SetIntField(env, packet, dp_offsetID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        (*env)->SetIntField(env, packet, dp_lengthID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        if (errno == ECONNREFUSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                            "ICMP Port Unreachable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            if (errno == EBADF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "Receive failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
         * success - fill in received address...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
         * REMIND: Fill in an int on the packet, and create inetadd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
         * object in Java, as a performance improvement. Also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
         * construct the inetadd object lazily.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        jobject packetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
         * Check if there is an InetAddress already associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
         * packet. If so we check if it is the same source address. We
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
         * can't update any existing InetAddress because it is immutable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        packetAddress = (*env)->GetObjectField(env, packet, dp_addressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        if (packetAddress != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            if (!NET_SockaddrEqualsInetAddress(env, (struct sockaddr *)&remote_addr, packetAddress)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                /* force a new InetAddress to be created */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                packetAddress = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        if (packetAddress == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            packetAddress = NET_SockaddrToInetAddress(env, (struct sockaddr *)&remote_addr, &port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            /* stuff the new Inetaddress in the packet */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            (*env)->SetObjectField(env, packet, dp_addressID, packetAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            /* only get the new port number */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            port = NET_GetPortFromSockaddr((struct sockaddr *)&remote_addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        /* and fill in the data, remote address/port and such */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        (*env)->SetByteArrayRegion(env, packetBuffer, packetBufferOffset, n,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                                   (jbyte *)fullPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        (*env)->SetIntField(env, packet, dp_portID, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        (*env)->SetIntField(env, packet, dp_lengthID, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    if (mallocedPacket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        free(fullPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    return port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
 * Method:    receive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
 * Signature: (Ljava/net/DatagramPacket;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
Java_java_net_PlainDatagramSocketImpl_receive0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                                              jobject packet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    char BUF[MAX_BUFFER_LEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    char *fullPacket = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    int mallocedPacket = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    jint timeout = (*env)->GetIntField(env, this, pdsi_timeoutID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    jbyteArray packetBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    jint packetBufferOffset, packetBufferLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    SOCKADDR remote_addr;
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   745
    socklen_t slen = SOCKADDR_LEN;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    jboolean retry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    jboolean connected = JNI_FALSE;
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
   749
    jobject connectedAddress = NULL;
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
   750
    jint connectedPort = 0;
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
   751
    jlong prevTime = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    if (IS_NULL(packet)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        JNU_ThrowNullPointerException(env, "packet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    packetBuffer = (*env)->GetObjectField(env, packet, dp_bufID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    if (IS_NULL(packetBuffer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        JNU_ThrowNullPointerException(env, "packet buffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    packetBufferOffset = (*env)->GetIntField(env, packet, dp_offsetID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    packetBufferLen = (*env)->GetIntField(env, packet, dp_bufLengthID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    if (packetBufferLen > MAX_BUFFER_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        /* 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
   778
         * reads and writes of byte arrays of size greater
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
         * than 2048 bytes into several operations of size 2048.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
         * This saves a malloc()/memcpy()/free() for big
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
         * buffers.  This is OK for file IO and TCP, but that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
         * strategy violates the semantics of a datagram protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
         * (one big send) != (several smaller sends).  So here
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
   784
         * 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
   785
         * than 65,536 (0xFFFF) the max size of an IP packet,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
         * anything bigger is truncated anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
         * We may want to use a smarter allocation scheme at some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
         * point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        if (packetBufferLen > MAX_PACKET_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            packetBufferLen = MAX_PACKET_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        fullPacket = (char *)malloc(packetBufferLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        if (!fullPacket) {
13245
7ab3ef5b9520 7181353: Update error message to distinguish native OOM and java OOM in net
zhouyx
parents: 12551
diff changeset
   797
            JNU_ThrowOutOfMemoryError(env, "Receive buffer native heap allocation failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            mallocedPacket = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        fullPacket = &(BUF[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        retry = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        if (timeout) {
23048
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   810
            int ret = NET_Timeout(fd, timeout);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            if (ret <= 0) {
23048
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   812
                if (ret == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                    JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                                    "Receive timed out");
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   815
                } else if (ret == -1) {
23048
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   816
                    if (errno == ENOMEM) {
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   817
                        JNU_ThrowOutOfMemoryError(env, "NET_Timeout native heap allocation failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
#ifdef __linux__
23048
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   819
                    } else if (errno == EBADF) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                         JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
23048
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   821
                    } else {
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   822
                        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "Receive failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
#else
23048
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   824
                    } else {
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   825
                        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
#endif
23048
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   827
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                if (mallocedPacket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                    free(fullPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        n = NET_RecvFrom(fd, fullPacket, packetBufferLen, 0,
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   839
                         (struct sockaddr *)&remote_addr, &slen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        /* truncate the data if the packet's length is too small */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        if (n > packetBufferLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            n = packetBufferLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        }
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   844
        if (n == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            (*env)->SetIntField(env, packet, dp_offsetID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            (*env)->SetIntField(env, packet, dp_lengthID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            if (errno == ECONNREFUSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                                "ICMP Port Unreachable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                if (errno == EBADF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                     JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                 } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                     NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "Receive failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            jobject packetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
             * success - fill in received address...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
             * REMIND: Fill in an int on the packet, and create inetadd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
             * object in Java, as a performance improvement. Also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
             * construct the inetadd object lazily.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
             * Check if there is an InetAddress already associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
             * packet. If so we check if it is the same source address. We
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
             * can't update any existing InetAddress because it is immutable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            packetAddress = (*env)->GetObjectField(env, packet, dp_addressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            if (packetAddress != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                if (!NET_SockaddrEqualsInetAddress(env, (struct sockaddr *)&remote_addr, packetAddress)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                    /* force a new InetAddress to be created */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                    packetAddress = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            if (packetAddress == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                packetAddress = NET_SockaddrToInetAddress(env, (struct sockaddr *)&remote_addr, &port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                /* stuff the new Inetaddress in the packet */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                (*env)->SetObjectField(env, packet, dp_addressID, packetAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                /* only get the new port number */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                port = NET_GetPortFromSockaddr((struct sockaddr *)&remote_addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            /* and fill in the data, remote address/port and such */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            (*env)->SetByteArrayRegion(env, packetBuffer, packetBufferOffset, n,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                                       (jbyte *)fullPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            (*env)->SetIntField(env, packet, dp_portID, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
            (*env)->SetIntField(env, packet, dp_lengthID, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    } while (retry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    if (mallocedPacket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        free(fullPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
 * Method:    datagramSocketCreate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
Java_java_net_PlainDatagramSocketImpl_datagramSocketCreate(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                                                           jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   912
    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
   913
#ifdef AF_INET6
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   914
    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
   915
#else
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   916
    int domain = AF_INET;
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   917
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    }
6299
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   924
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   925
    if ((fd = socket(domain, SOCK_DGRAM, 0)) == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                       "Error creating socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
6299
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   931
#ifdef AF_INET6
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   932
    /* 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
   933
    if (domain == AF_INET6) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   934
        arg = 0;
6299
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   935
        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
   936
                       sizeof(int)) < 0) {
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   937
            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
   938
            close(fd);
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   939
            return;
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   940
        }
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   941
    }
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   942
#endif /* AF_INET6 */
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   943
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   944
#ifdef __APPLE__
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   945
    arg = 65507;
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   946
    if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF,
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   947
                   (char *)&arg, sizeof(arg)) < 0) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   948
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   949
                        strerror(errno));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   950
        return;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   951
    }
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   952
    if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF,
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   953
                   (char *)&arg, sizeof(arg)) < 0) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   954
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   955
                        strerror(errno));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   956
        return;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   957
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   958
#endif /* __APPLE__ */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   959
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (char*) &t, sizeof(int));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
18534
2d131cf5db63 8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents: 16870
diff changeset
   962
#if defined(__linux__)
2d131cf5db63 8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents: 16870
diff changeset
   963
     arg = 0;
2d131cf5db63 8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents: 16870
diff changeset
   964
     int level = (domain == AF_INET6) ? IPPROTO_IPV6 : IPPROTO_IP;
2d131cf5db63 8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents: 16870
diff changeset
   965
     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
   966
         (errno != ENOPROTOOPT)) {
2d131cf5db63 8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents: 16870
diff changeset
   967
         JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
2d131cf5db63 8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents: 16870
diff changeset
   968
                         strerror(errno));
2d131cf5db63 8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents: 16870
diff changeset
   969
         close(fd);
2d131cf5db63 8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents: 16870
diff changeset
   970
         return;
2d131cf5db63 8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents: 16870
diff changeset
   971
     }
2d131cf5db63 8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents: 16870
diff changeset
   972
#endif
2d131cf5db63 8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents: 16870
diff changeset
   973
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
   974
#if defined (__linux__) && defined (AF_INET6)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * 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
   977
     * to 1 to be compatible with default TTL of 1 for IPv4 sockets.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     */
6299
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
   979
    if (domain == AF_INET6) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        int ttl = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, (char *)&ttl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                   sizeof(ttl));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
#endif /* __linux__ */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
    (*env)->SetIntField(env, fdObj, IO_fd_fdID, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
 * Method:    datagramSocketClose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
Java_java_net_PlainDatagramSocketImpl_datagramSocketClose(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                                                          jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     * REMIND: PUT A LOCK AROUND THIS CODE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
    if (IS_NULL(fdObj)) {
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
    fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    if (fd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
    (*env)->SetIntField(env, fdObj, IO_fd_fdID, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
    NET_SocketClose(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
 * Set outgoing multicast interface designated by a NetworkInterface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
 * Throw exception if failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
static void mcast_set_if_by_if_v4(JNIEnv *env, jobject this, int fd, jobject value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
    static jfieldID ni_addrsID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
    struct in_addr in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    jobjectArray addrArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
    jsize len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    jobject addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1027
    if (ni_addrsID == NULL ) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        CHECK_NULL(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        ni_addrsID = (*env)->GetFieldID(env, c, "addrs",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                                        "[Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        CHECK_NULL(ni_addrsID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
    addrArray = (*env)->GetObjectField(env, value, ni_addrsID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
    len = (*env)->GetArrayLength(env, addrArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     * Check that there is at least one address bound to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
    if (len < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
            "bad argument for IP_MULTICAST_IF2: No IP addresses bound to interface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     * We need an ipv4 address here
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
    for (i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        addr = (*env)->GetObjectArrayElement(env, addrArray, i);
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1053
        if (getInetAddress_family(env, addr) == IPv4) {
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1054
            in.s_addr = htonl(getInetAddress_addr(env, addr));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1059
    if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF,
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1060
                   (const char*)&in, sizeof(in)) < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
                       "Error setting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
 * Set outgoing multicast interface designated by a NetworkInterface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
 * Throw exception if failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
 */
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1070
#ifdef AF_INET6
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
static void mcast_set_if_by_if_v6(JNIEnv *env, jobject this, int fd, jobject value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
    static jfieldID ni_indexID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
    int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
    if (ni_indexID == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        CHECK_NULL(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        ni_indexID = (*env)->GetFieldID(env, c, "index", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        CHECK_NULL(ni_indexID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
    index = (*env)->GetIntField(env, value, ni_indexID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1083
    if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1084
                   (const char*)&index, sizeof(index)) < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        if (errno == EINVAL && index > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                "IPV6_MULTICAST_IF failed (interface has IPv4 "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                "address only?)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                           "Error setting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
}
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1097
#endif /* AF_INET6 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
 * Set outgoing multicast interface designated by an InetAddress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
 * Throw exception if failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
static void mcast_set_if_by_addr_v4(JNIEnv *env, jobject this, int fd, jobject value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
    struct in_addr in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1106
    in.s_addr = htonl( getInetAddress_addr(env, value) );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1108
    if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF,
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1109
                   (const char*)&in, sizeof(in)) < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                         "Error setting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
 * Set outgoing multicast interface designated by an InetAddress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
 * Throw exception if failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
 */
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1119
#ifdef AF_INET6
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
static void mcast_set_if_by_addr_v6(JNIEnv *env, jobject this, int fd, jobject value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
    static jclass ni_class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
    if (ni_class == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        CHECK_NULL(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        ni_class = (*env)->NewGlobalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        CHECK_NULL(ni_class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
    value = Java_java_net_NetworkInterface_getByInetAddress0(env, ni_class, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
    if (value == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        if (!(*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                 "bad argument for IP_MULTICAST_IF"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
                 ": address not bound to any interface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
    mcast_set_if_by_if_v6(env, this, fd, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
}
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1141
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
 * Sets the multicast interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
 * SocketOptions.IP_MULTICAST_IF :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
 *      value is a InetAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
 *      IPv4:   set outgoing multicast interface using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
 *              IPPROTO_IP/IP_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
 *      IPv6:   Get the index of the interface to which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
 *              InetAddress is bound
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
 *              Set outgoing multicast interface using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
 *              IPPROTO_IPV6/IPV6_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
 * SockOptions.IF_MULTICAST_IF2 :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
 *      value is a NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
 *      IPv4:   Obtain IP address bound to network interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
 *              (NetworkInterface.addres[0])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
 *              set outgoing multicast interface using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
 *              IPPROTO_IP/IP_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
 *      IPv6:   Obtain NetworkInterface.index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
 *              Set outgoing multicast interface using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
 *              IPPROTO_IPV6/IPV6_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
static void setMulticastInterface(JNIEnv *env, jobject this, int fd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
                                  jint opt, jobject value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
    if (opt == java_net_SocketOptions_IP_MULTICAST_IF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
         * value is an InetAddress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
         */
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1173
#ifdef AF_INET6
13574
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1174
#ifdef __linux__
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1175
        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
  1176
        if (ipv6_available()) {
22095
e613710a08a6 8027903: java/net/MulticastSocket/SetGetNetworkInterfaceTest.java throws java.net.SocketException: Cannot assign requested address
msheppar
parents: 20821
diff changeset
  1177
            if ((*env)->ExceptionCheck(env)){
e613710a08a6 8027903: java/net/MulticastSocket/SetGetNetworkInterfaceTest.java throws java.net.SocketException: Cannot assign requested address
msheppar
parents: 20821
diff changeset
  1178
                (*env)->ExceptionClear(env);
e613710a08a6 8027903: java/net/MulticastSocket/SetGetNetworkInterfaceTest.java throws java.net.SocketException: Cannot assign requested address
msheppar
parents: 20821
diff changeset
  1179
            }
13574
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1180
            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
  1181
        }
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1182
#else  /* __linux__ not defined */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
            mcast_set_if_by_addr_v6(env, this, fd, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
            mcast_set_if_by_addr_v4(env, this, fd, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        }
13574
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1188
#endif  /* __linux__ */
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1189
#else
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1190
        mcast_set_if_by_addr_v4(env, this, fd, value);
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1191
#endif  /* AF_INET6 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
    if (opt == java_net_SocketOptions_IP_MULTICAST_IF2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
         * value is a NetworkInterface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
         */
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1198
#ifdef AF_INET6
13574
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1199
#ifdef __linux__
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1200
        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
  1201
        if (ipv6_available()) {
22095
e613710a08a6 8027903: java/net/MulticastSocket/SetGetNetworkInterfaceTest.java throws java.net.SocketException: Cannot assign requested address
msheppar
parents: 20821
diff changeset
  1202
            if ((*env)->ExceptionCheck(env)){
e613710a08a6 8027903: java/net/MulticastSocket/SetGetNetworkInterfaceTest.java throws java.net.SocketException: Cannot assign requested address
msheppar
parents: 20821
diff changeset
  1203
                (*env)->ExceptionClear(env);
e613710a08a6 8027903: java/net/MulticastSocket/SetGetNetworkInterfaceTest.java throws java.net.SocketException: Cannot assign requested address
msheppar
parents: 20821
diff changeset
  1204
            }
13574
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1205
            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
  1206
        }
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1207
#else  /* __linux__ not defined */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
            mcast_set_if_by_if_v6(env, this, fd, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
            mcast_set_if_by_if_v4(env, this, fd, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
        }
13574
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1213
#endif  /* __linux__ */
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1214
#else
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1215
        mcast_set_if_by_if_v4(env, this, fd, value);
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1216
#endif  /* AF_INET6 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
 * Enable/disable local loopback of multicast datagrams.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
static void mcast_set_loop_v4(JNIEnv *env, jobject this, int fd, jobject value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
    jclass cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
    jfieldID fid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
    jboolean on;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
    char loopback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
    cls = (*env)->FindClass(env, "java/lang/Boolean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
    CHECK_NULL(cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
    fid =  (*env)->GetFieldID(env, cls, "value", "Z");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
    CHECK_NULL(fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
    on = (*env)->GetBooleanField(env, value, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
    loopback = (!on ? 1 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
    if (NET_SetSockOpt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, (const void *)&loopback, sizeof(char)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "Error setting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
 * Enable/disable local loopback of multicast datagrams.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
 */
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1246
#ifdef AF_INET6
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
static void mcast_set_loop_v6(JNIEnv *env, jobject this, int fd, jobject value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
    jclass cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
    jfieldID fid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
    jboolean on;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
    int loopback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
    cls = (*env)->FindClass(env, "java/lang/Boolean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
    CHECK_NULL(cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
    fid =  (*env)->GetFieldID(env, cls, "value", "Z");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
    CHECK_NULL(fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
    on = (*env)->GetBooleanField(env, value, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
    loopback = (!on ? 1 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
    if (NET_SetSockOpt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, (const void *)&loopback, sizeof(int)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "Error setting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
}
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1267
#endif  /* AF_INET6 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
 * Sets the multicast loopback mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
static void setMulticastLoopbackMode(JNIEnv *env, jobject this, int fd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
                                  jint opt, jobject value) {
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1274
#ifdef AF_INET6
13574
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1275
#ifdef __linux__
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1276
    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
  1277
    if (ipv6_available()) {
22095
e613710a08a6 8027903: java/net/MulticastSocket/SetGetNetworkInterfaceTest.java throws java.net.SocketException: Cannot assign requested address
msheppar
parents: 20821
diff changeset
  1278
        if ((*env)->ExceptionCheck(env)){
e613710a08a6 8027903: java/net/MulticastSocket/SetGetNetworkInterfaceTest.java throws java.net.SocketException: Cannot assign requested address
msheppar
parents: 20821
diff changeset
  1279
            (*env)->ExceptionClear(env);
e613710a08a6 8027903: java/net/MulticastSocket/SetGetNetworkInterfaceTest.java throws java.net.SocketException: Cannot assign requested address
msheppar
parents: 20821
diff changeset
  1280
        }
13574
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1281
        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
  1282
    }
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1283
#else  /* __linux__ not defined */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
    if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
        mcast_set_loop_v6(env, this, fd, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
        mcast_set_loop_v4(env, this, fd, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
    }
13574
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1289
#endif  /* __linux__ */
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1290
#else
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1291
    mcast_set_loop_v4(env, this, fd, value);
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1292
#endif  /* AF_INET6 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
 * Method:    socketSetOption
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
 * Signature: (ILjava/lang/Object;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
Java_java_net_PlainDatagramSocketImpl_socketSetOption(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                                                      jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
                                                      jint opt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
                                                      jobject value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
    int level, optname, optlen;
14195
57fe6c437882 8000206: Uninitialized variable in PlainDatagramSocketImpl.c
chegar
parents: 13999
diff changeset
  1307
    int optval;
57fe6c437882 8000206: Uninitialized variable in PlainDatagramSocketImpl.c
chegar
parents: 13999
diff changeset
  1308
    optlen = sizeof(int);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
     * Check that socket hasn't been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
    fd = getFD(env, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
    if (fd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     * Check argument has been provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
    if (IS_NULL(value)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
        JNU_ThrowNullPointerException(env, "value argument");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
    /*
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
  1329
     * Setting the multicast interface handled separately
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
    if (opt == java_net_SocketOptions_IP_MULTICAST_IF ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
        opt == java_net_SocketOptions_IP_MULTICAST_IF2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
        setMulticastInterface(env, this, fd, opt, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
     * Setting the multicast loopback mode handled separately
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
    if (opt == java_net_SocketOptions_IP_MULTICAST_LOOP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
        setMulticastLoopbackMode(env, this, fd, opt, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
     * Map the Java level socket option to the platform specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
     * level and option name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
    if (NET_MapSocketOption(opt, &level, &optname)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Invalid option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
    switch (opt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
        case java_net_SocketOptions_SO_SNDBUF :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
        case java_net_SocketOptions_SO_RCVBUF :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        case java_net_SocketOptions_IP_TOS :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
                jclass cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
                jfieldID fid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
                cls = (*env)->FindClass(env, "java/lang/Integer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
                CHECK_NULL(cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
                fid =  (*env)->GetFieldID(env, cls, "value", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
                CHECK_NULL(fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
14195
57fe6c437882 8000206: Uninitialized variable in PlainDatagramSocketImpl.c
chegar
parents: 13999
diff changeset
  1368
                optval = (*env)->GetIntField(env, value, fid);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
        case java_net_SocketOptions_SO_REUSEADDR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
        case java_net_SocketOptions_SO_BROADCAST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
                jclass cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
                jfieldID fid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                jboolean on;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
                cls = (*env)->FindClass(env, "java/lang/Boolean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                CHECK_NULL(cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                fid =  (*env)->GetFieldID(env, cls, "value", "Z");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
                CHECK_NULL(fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
                on = (*env)->GetBooleanField(env, value, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
                /* SO_REUSEADDR or SO_BROADCAST */
14195
57fe6c437882 8000206: Uninitialized variable in PlainDatagramSocketImpl.c
chegar
parents: 13999
diff changeset
  1387
                optval = (on ? 1 : 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
        default :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
                "Socket option not supported by PlainDatagramSocketImp");
22651
bfc6ca5245fd 8033689: PlainDatagramSocketImpl missing returns after throwing an exception
chegar
parents: 22646
diff changeset
  1395
            return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
    if (NET_SetSockOpt(fd, level, optname, (const void *)&optval, optlen) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "Error setting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
 * Return the multicast interface:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
 * SocketOptions.IP_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
 *      IPv4:   Query IPPROTO_IP/IP_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
 *              Create InetAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
 *              IP_MULTICAST_IF returns struct ip_mreqn on 2.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
 *              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
  1414
 *      IPv6:   Query IPPROTO_IPV6 / IPV6_MULTICAST_IF
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
 *              If index == 0 return InetAddress representing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
 *              anyLocalAddress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
 *              If index > 0 query NetworkInterface by index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
 *              and returns addrs[0]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
 * SocketOptions.IP_MULTICAST_IF2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
 *      IPv4:   Query IPPROTO_IP/IP_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
 *              Query NetworkInterface by IP address and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
 *              return the NetworkInterface that the address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
 *              is bound too.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
 *      IPv6:   Query IPPROTO_IPV6 / IPV6_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
 *              (except Linux .2 kernel)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
 *              Query NetworkInterface by index and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
 *              return NetworkInterface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
jobject getMulticastInterface(JNIEnv *env, jobject this, int fd, jint opt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
    jboolean isIPV4 = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
    if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
        isIPV4 = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
     * IPv4 implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
    if (isIPV4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
        static jclass inet4_class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
        static jmethodID inet4_ctrID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
        static jclass ni_class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
        static jmethodID ni_ctrID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
        static jfieldID ni_indexID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
        static jfieldID ni_addrsID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
        jobjectArray addrArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
        jobject addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
        jobject ni;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
        struct in_addr in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
        struct in_addr *inP = &in;
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1457
        socklen_t len = sizeof(struct in_addr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1459
        if (getsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF,
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1460
                       (char *)inP, &len) < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
                             "Error getting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
         * Construct and populate an Inet4Address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
        if (inet4_class == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
            jclass c = (*env)->FindClass(env, "java/net/Inet4Address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
            CHECK_NULL_RETURN(c, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
            inet4_ctrID = (*env)->GetMethodID(env, c, "<init>", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
            CHECK_NULL_RETURN(inet4_ctrID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
            inet4_class = (*env)->NewGlobalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
            CHECK_NULL_RETURN(inet4_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
        addr = (*env)->NewObject(env, inet4_class, inet4_ctrID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
        CHECK_NULL_RETURN(addr, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1480
        setInetAddress_addr(env, addr, ntohl(in.s_addr));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
         * For IP_MULTICAST_IF return InetAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
        if (opt == java_net_SocketOptions_IP_MULTICAST_IF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
            return addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
         * For IP_MULTICAST_IF2 we get the NetworkInterface for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
         * this address and return it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
        if (ni_class == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
            jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
            CHECK_NULL_RETURN(c, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
            ni_ctrID = (*env)->GetMethodID(env, c, "<init>", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
            CHECK_NULL_RETURN(ni_ctrID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
            ni_indexID = (*env)->GetFieldID(env, c, "index", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
            CHECK_NULL_RETURN(ni_indexID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
            ni_addrsID = (*env)->GetFieldID(env, c, "addrs",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
                                            "[Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
            CHECK_NULL_RETURN(ni_addrsID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
            ni_class = (*env)->NewGlobalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
            CHECK_NULL_RETURN(ni_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
        ni = Java_java_net_NetworkInterface_getByInetAddress0(env, ni_class, addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
        if (ni) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
            return ni;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
         * The address doesn't appear to be bound at any known
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
         * NetworkInterface. Therefore we construct a NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
         * with this address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
        ni = (*env)->NewObject(env, ni_class, ni_ctrID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
        CHECK_NULL_RETURN(ni, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
        (*env)->SetIntField(env, ni, ni_indexID, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
        addrArray = (*env)->NewObjectArray(env, 1, inet4_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
        CHECK_NULL_RETURN(addrArray, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
        (*env)->SetObjectArrayElement(env, addrArray, 0, addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
        (*env)->SetObjectField(env, ni, ni_addrsID, addrArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
        return ni;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
     * IPv6 implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
    if ((opt == java_net_SocketOptions_IP_MULTICAST_IF) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
        (opt == java_net_SocketOptions_IP_MULTICAST_IF2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
        static jclass ni_class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
        static jmethodID ni_ctrID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
        static jfieldID ni_indexID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
        static jfieldID ni_addrsID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
        static jclass ia_class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
        static jmethodID ia_anyLocalAddressID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
        int index;
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1543
        socklen_t len = sizeof(index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
        jobjectArray addrArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
        jobject addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
        jobject ni;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1549
        if (getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1550
                       (char*)&index, &len) < 0) {
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
  1551
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
  1552
                           "Error getting socket option");
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
  1553
            return NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
        if (ni_class == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
            jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
            CHECK_NULL_RETURN(c, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
            ni_ctrID = (*env)->GetMethodID(env, c, "<init>", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
            CHECK_NULL_RETURN(ni_ctrID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
            ni_indexID = (*env)->GetFieldID(env, c, "index", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
            CHECK_NULL_RETURN(ni_indexID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
            ni_addrsID = (*env)->GetFieldID(env, c, "addrs",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
                                            "[Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
            CHECK_NULL_RETURN(ni_addrsID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
            ia_class = (*env)->FindClass(env, "java/net/InetAddress");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
            CHECK_NULL_RETURN(ia_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
            ia_class = (*env)->NewGlobalRef(env, ia_class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
            CHECK_NULL_RETURN(ia_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
            ia_anyLocalAddressID = (*env)->GetStaticMethodID(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
                                                             ia_class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
                                                             "anyLocalAddress",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
                                                             "()Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
            CHECK_NULL_RETURN(ia_anyLocalAddressID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
            ni_class = (*env)->NewGlobalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
            CHECK_NULL_RETURN(ni_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
         * If multicast to a specific interface then return the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
         * interface (for IF2) or the any address on that interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
         * (for IF).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
        if (index > 0) {
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 910
diff changeset
  1586
            ni = Java_java_net_NetworkInterface_getByIndex0(env, ni_class,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
                                                                   index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
            if (ni == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
                char errmsg[255];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
                sprintf(errmsg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
                        "IPV6_MULTICAST_IF returned index to unrecognized interface: %d",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
                        index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
                JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", errmsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
                return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
             * For IP_MULTICAST_IF2 return the NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
            if (opt == java_net_SocketOptions_IP_MULTICAST_IF2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
                return ni;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
             * For IP_MULTICAST_IF return addrs[0]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
            addrArray = (*env)->GetObjectField(env, ni, ni_addrsID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
            if ((*env)->GetArrayLength(env, addrArray) < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
                JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
                    "IPV6_MULTICAST_IF returned interface without IP bindings");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
                return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
            addr = (*env)->GetObjectArrayElement(env, addrArray, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
            return addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
         * Multicast to any address - return anyLocalAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
         * or a NetworkInterface with addrs[0] set to anyLocalAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
        addr = (*env)->CallStaticObjectMethod(env, ia_class, ia_anyLocalAddressID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
                                              NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
        if (opt == java_net_SocketOptions_IP_MULTICAST_IF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
            return addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
        ni = (*env)->NewObject(env, ni_class, ni_ctrID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
        CHECK_NULL_RETURN(ni, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
        (*env)->SetIntField(env, ni, ni_indexID, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
        addrArray = (*env)->NewObjectArray(env, 1, ia_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
        CHECK_NULL_RETURN(addrArray, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
        (*env)->SetObjectArrayElement(env, addrArray, 0, addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
        (*env)->SetObjectField(env, ni, ni_addrsID, addrArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
        return ni;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
 * Returns relevant info as a jint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
 * Method:    socketGetOption
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
 * Signature: (I)Ljava/lang/Object;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
JNIEXPORT jobject JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
Java_java_net_PlainDatagramSocketImpl_socketGetOption(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
                                                      jint opt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
    int level, optname, optlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
    union {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
        char c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
    } optval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
    fd = getFD(env, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
    if (fd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
                        "socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
    /*
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
  1669
     * Handle IP_MULTICAST_IF separately
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
    if (opt == java_net_SocketOptions_IP_MULTICAST_IF ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
        opt == java_net_SocketOptions_IP_MULTICAST_IF2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
        return getMulticastInterface(env, this, fd, opt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
     * SO_BINDADDR implemented using getsockname
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
    if (opt == java_net_SocketOptions_SO_BINDADDR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
        /* find out local IP address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
        SOCKADDR him;
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1683
        socklen_t len = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
        int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
        jobject iaObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
        len = SOCKADDR_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
        if (getsockname(fd, (struct sockaddr *)&him, &len) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
                           "Error getting socket name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
        iaObj = NET_SockaddrToInetAddress(env, (struct sockaddr *)&him, &port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
        return iaObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
     * Map the Java level socket option to the platform specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
     * level and option name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
    if (NET_MapSocketOption(opt, &level, &optname)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Invalid option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
    if (opt == java_net_SocketOptions_IP_MULTICAST_LOOP &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
        level == IPPROTO_IP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
        optlen = sizeof(optval.c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
        optlen = sizeof(optval.i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
    if (NET_GetSockOpt(fd, level, optname, (void *)&optval, &optlen) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
                         "Error getting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
    switch (opt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
        case java_net_SocketOptions_IP_MULTICAST_LOOP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
            /* getLoopbackMode() returns true if IP_MULTICAST_LOOP disabled */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
            if (level == IPPROTO_IP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
                return createBoolean(env, (int)!optval.c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
                return createBoolean(env, !optval.i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
        case java_net_SocketOptions_SO_BROADCAST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
        case java_net_SocketOptions_SO_REUSEADDR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
            return createBoolean(env, optval.i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
        case java_net_SocketOptions_SO_SNDBUF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
        case java_net_SocketOptions_SO_RCVBUF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
        case java_net_SocketOptions_IP_TOS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
            return createInteger(env, optval.i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
  1741
    /* should never reach here */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
 * Multicast-related calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
Java_java_net_PlainDatagramSocketImpl_setTTL(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
                                             jbyte ttl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
    jint ittl = ttl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
    if (ittl < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
        ittl += 0x100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
    Java_java_net_PlainDatagramSocketImpl_setTimeToLive(env, this, ittl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
 * Set TTL for a socket. Throw exception if failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
static void setTTL(JNIEnv *env, int fd, jint ttl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
    char ittl = (char)ttl;
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1764
    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
  1765
                   sizeof(ittl)) < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
                       "Error setting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
 * Set hops limit for a socket. Throw exception if failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
 */
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1774
#ifdef AF_INET6
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
static void setHopLimit(JNIEnv *env, int fd, jint ttl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
    int ittl = (int)ttl;
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1777
    if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1778
                   (char*)&ittl, sizeof(ittl)) < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
                       "Error setting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
}
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1783
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
 * Method:    setTTL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
 * Signature: (B)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
Java_java_net_PlainDatagramSocketImpl_setTimeToLive(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
                                                    jint ttl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
    /* it is important to cast this to a char, otherwise setsockopt gets confused */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
    }
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
  1805
    /* setsockopt to be correct TTL */
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1806
#ifdef AF_INET6
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
    setTTL(env, fd, ttl);
22651
bfc6ca5245fd 8033689: PlainDatagramSocketImpl missing returns after throwing an exception
chegar
parents: 22646
diff changeset
  1809
    JNU_CHECK_EXCEPTION(env);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
    if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
        setHopLimit(env, fd, ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
    }
13574
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1813
#else  /*  __linux__ not defined */
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1814
    if (ipv6_available()) {
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1815
        setHopLimit(env, fd, ttl);
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1816
    } else {
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1817
        setTTL(env, fd, ttl);
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1818
    }
9d58db93e184 7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents: 13245
diff changeset
  1819
#endif  /* __linux__ */
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1820
#else
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1821
    setTTL(env, fd, ttl);
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1822
#endif  /* AF_INET6 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
 * Method:    getTTL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
 * Signature: ()B
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
JNIEXPORT jbyte JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
Java_java_net_PlainDatagramSocketImpl_getTTL(JNIEnv *env, jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
    return (jbyte)Java_java_net_PlainDatagramSocketImpl_getTimeToLive(env, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
 * Method:    getTTL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
 * Signature: ()B
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
Java_java_net_PlainDatagramSocketImpl_getTimeToLive(JNIEnv *env, jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
    jint fd = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
    }
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
  1854
    /* getsockopt of TTL */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
    if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
        int ttl = 0;
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1858
        socklen_t len = sizeof(ttl);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1860
        if (getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1861
                       (char*)&ttl, &len) < 0) {
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1862
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1863
                                         "Error getting socket option");
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1864
            return -1;
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1865
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
        return (jint)ttl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
    } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
#endif /* AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
            u_char ttl = 0;
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1871
            socklen_t len = sizeof(ttl);
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1872
            if (getsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL,
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1873
                           (char*)&ttl, &len) < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
                NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
                               "Error getting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
            return (jint)ttl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
 * mcast_join_leave: Join or leave a multicast group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
 * For IPv4 sockets use IP_ADD_MEMBERSHIP/IP_DROP_MEMBERSHIP socket option
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
 * to join/leave multicast group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
 * For IPv6 sockets use IPV6_ADD_MEMBERSHIP/IPV6_DROP_MEMBERSHIP socket option
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
 * to join/leave multicast group. If multicast group is an IPv4 address then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
 * an IPv4-mapped address is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
 * On Linux with IPv6 if we wish to join/leave an IPv4 multicast group then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
 * we must use the IPv4 socket options. This is because the IPv6 socket options
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
 * 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
  1896
 * kernel releases. In the future it's possible that IP_ADD_MEMBERSHIP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
 * will be updated to return ENOPROTOOPT if uses with an IPv6 socket (Solaris
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
 * already does this). Thus to cater for this we first try with the IPv4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
 * socket options and if they fail we use the IPv6 socket options. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
 * seems a reasonable failsafe solution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
static void mcast_join_leave(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
                             jobject iaObj, jobject niObj,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
                             jboolean join) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
    jint fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
    jint ipv6_join_leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
    if (IS_NULL(iaObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
        JNU_ThrowNullPointerException(env, "iaObj");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
     * Determine if this is an IPv4 or IPv6 join/leave.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
    ipv6_join_leave = ipv6_available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
#ifdef __linux__
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1929
    if (getInetAddress_family(env, iaObj) == IPv4) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
        ipv6_join_leave = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
     * IPv6 not compiled in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
    ipv6_join_leave = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
     * For IPv4 join use IP_ADD_MEMBERSHIP/IP_DROP_MEMBERSHIP socket option
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
     * On Linux if IPv4 or IPv6 use IP_ADD_MEMBERSHIP/IP_DROP_MEMBERSHIP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
    if (!ipv6_join_leave) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
        struct ip_mreqn mname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
        struct ip_mreq mname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
        int mname_len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
         * joinGroup(InetAddress, NetworkInterface) implementation :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
         * Linux/IPv6:  use ip_mreqn structure populated with multicast
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
         *              address and interface index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
         * IPv4:        use ip_mreq structure populated with multicast
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
         *              address and first address obtained from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
         *              NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
        if (niObj != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
#if defined(__linux__) && defined(AF_INET6)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
            if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
                static jfieldID ni_indexID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
                if (ni_indexID == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
                    jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
                    CHECK_NULL(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
                    ni_indexID = (*env)->GetFieldID(env, c, "index", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
                    CHECK_NULL(ni_indexID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1976
                mname.imr_multiaddr.s_addr = htonl(getInetAddress_addr(env, iaObj));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
                mname.imr_address.s_addr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
                mname.imr_ifindex =  (*env)->GetIntField(env, niObj, ni_indexID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
                mname_len = sizeof(struct ip_mreqn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
                jobjectArray addrArray = (*env)->GetObjectField(env, niObj, ni_addrsID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
                jobject addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
                if ((*env)->GetArrayLength(env, addrArray) < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
                    JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
                        "bad argument for IP_ADD_MEMBERSHIP: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
                        "No IP addresses bound to interface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
                addr = (*env)->GetObjectArrayElement(env, addrArray, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1994
                mname.imr_multiaddr.s_addr = htonl(getInetAddress_addr(env, iaObj));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
#ifdef __linux__
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1996
                mname.imr_address.s_addr = htonl(getInetAddress_addr(env, addr));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
#else
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  1998
                mname.imr_interface.s_addr = htonl(getInetAddress_addr(env, addr));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
                mname_len = sizeof(struct ip_mreq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
         * joinGroup(InetAddress) implementation :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
         * Linux/IPv6:  use ip_mreqn structure populated with multicast
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
         *              address and interface index. index obtained
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
         *              from cached value or IPV6_MULTICAST_IF.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
         * IPv4:        use ip_mreq structure populated with multicast
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
         *              address and local address obtained from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
         *              IP_MULTICAST_IF. On Linux IP_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
         *              returns different structure depending on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
         *              kernel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
        if (niObj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
#if defined(__linux__) && defined(AF_INET6)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
            if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
                int index;
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  2025
                socklen_t len = sizeof(index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  2027
                if (getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  2028
                               (char*)&index, &len) < 0) {
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
  2029
                    NET_ThrowCurrent(env, "getsockopt IPV6_MULTICAST_IF failed");
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
  2030
                    return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  2033
                mname.imr_multiaddr.s_addr = htonl(getInetAddress_addr(env, iaObj));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
                mname.imr_address.s_addr = 0 ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
                mname.imr_ifindex = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
                mname_len = sizeof(struct ip_mreqn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
                struct in_addr in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
                struct in_addr *inP = &in;
6886
d818a2dfb100 6989690: java/net native code compiler warnings
chegar
parents: 6299
diff changeset
  2042
                socklen_t len = sizeof(struct in_addr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
                if (getsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, (char *)inP, &len) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
                    NET_ThrowCurrent(env, "getsockopt IP_MULTICAST_IF failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
#ifdef __linux__
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
  2050
                mname.imr_address.s_addr = in.s_addr;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
                mname.imr_interface.s_addr = in.s_addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
#endif
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  2055
                mname.imr_multiaddr.s_addr = htonl(getInetAddress_addr(env, iaObj));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
                mname_len = sizeof(struct ip_mreq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
         * Join the multicast group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
         */
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  2064
        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
  2065
                       (char *) &mname, mname_len) < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
             * If IP_ADD_MEMBERSHIP returns ENOPROTOOPT on Linux and we've got
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
             * IPv6 enabled then it's possible that the kernel has been fixed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
             * 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
  2071
             * 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
  2072
             * addresses so we have to use IP_ADD_MEMBERSHIP for IPv4 multicast
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
  2073
             * groups. However if the socket is an IPv6 socket then setsockopt
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
  2074
             * should return ENOPROTOOPT. We assume this will be fixed in Linux
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
             * at some stage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
#if defined(__linux__) && defined(AF_INET6)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
            if (errno == ENOPROTOOPT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
                if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
                    ipv6_join_leave = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
                    errno = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
                } else  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
                    errno = ENOPROTOOPT;    /* errno can be changed by ipv6_available */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
            if (errno) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
                if (join) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
                    NET_ThrowCurrent(env, "setsockopt IP_ADD_MEMBERSHIP failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
                    if (errno == ENOENT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
                        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
                            "Not a member of the multicast group");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
                        NET_ThrowCurrent(env, "setsockopt IP_DROP_MEMBERSHIP failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
                }
22651
bfc6ca5245fd 8033689: PlainDatagramSocketImpl missing returns after throwing an exception
chegar
parents: 22646
diff changeset
  2097
                return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
         * If we haven't switched to IPv6 socket option then we're done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
        if (!ipv6_join_leave) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
     * IPv6 join. If it's an IPv4 multicast group then we use an IPv4-mapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
     * address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
        struct ipv6_mreq mname6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
        jbyteArray ipaddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
        jbyte caddr[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
        jint family;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
        jint address;
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  2121
        family = getInetAddress_family(env, iaObj) == IPv4? AF_INET : AF_INET6;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
        if (family == AF_INET) { /* will convert to IPv4-mapped address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
            memset((char *) caddr, 0, 16);
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
  2124
            address = getInetAddress_addr(env, iaObj);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
            caddr[10] = 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
            caddr[11] = 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
            caddr[12] = ((address >> 24) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
            caddr[13] = ((address >> 16) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
            caddr[14] = ((address >> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
            caddr[15] = (address & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
        } else {
22956
322ce28a7e20 8034182: Misc. warnings in java.net code
alanb
parents: 22651
diff changeset
  2134
            getInet6Address_ipaddress(env, iaObj, (char*)caddr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
        memcpy((void *)&(mname6.ipv6mr_multiaddr), caddr, sizeof(struct in6_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
        if (IS_NULL(niObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
            int index;
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  2140
            socklen_t len = sizeof(index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  2142
            if (getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  2143
                           (char*)&index, &len) < 0) {
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
  2144
                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
  2145
                return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
             * On 2.4.8+ if we join a group with the interface set to 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
             * then the kernel records the interface it decides. This causes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
             * subsequent leave groups to fail as there is no match. Thus we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
             * pick the interface if there is a matching route.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
             */
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13574
diff changeset
  2155
            if (index == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
                int rt_index = getDefaultIPv6Interface(&(mname6.ipv6mr_multiaddr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
                if (rt_index > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
                    index = rt_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
#endif
12551
5be0a162ed89 7144274: [macosx] Default IPv6 multicast interface is not being set when calling MulticastSocket.joinGroup()
khazra
parents: 12047
diff changeset
  2162
#ifdef MACOSX
5be0a162ed89 7144274: [macosx] Default IPv6 multicast interface is not being set when calling MulticastSocket.joinGroup()
khazra
parents: 12047
diff changeset
  2163
            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
  2164
                index = getDefaultScopeID(env);
5be0a162ed89 7144274: [macosx] Default IPv6 multicast interface is not being set when calling MulticastSocket.joinGroup()
khazra
parents: 12047
diff changeset
  2165
            }
5be0a162ed89 7144274: [macosx] Default IPv6 multicast interface is not being set when calling MulticastSocket.joinGroup()
khazra
parents: 12047
diff changeset
  2166
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
            mname6.ipv6mr_interface = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
            jint idx = (*env)->GetIntField(env, niObj, ni_indexID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
            mname6.ipv6mr_interface = idx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
  2173
#if defined(_ALLBSD_SOURCE)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
  2174
#define ADD_MEMBERSHIP          IPV6_JOIN_GROUP
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
  2175
#define DRP_MEMBERSHIP          IPV6_LEAVE_GROUP
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
  2176
#define S_ADD_MEMBERSHIP        "IPV6_JOIN_GROUP"
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
  2177
#define S_DRP_MEMBERSHIP        "IPV6_LEAVE_GROUP"
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
  2178
#else
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
  2179
#define ADD_MEMBERSHIP          IPV6_ADD_MEMBERSHIP
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
  2180
#define DRP_MEMBERSHIP          IPV6_DROP_MEMBERSHIP
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
  2181
#define S_ADD_MEMBERSHIP        "IPV6_ADD_MEMBERSHIP"
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
  2182
#define S_DRP_MEMBERSHIP        "IPV6_DROP_MEMBERSHIP"
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
  2183
#endif
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
  2184
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
        /* Join the multicast group */
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  2186
        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
  2187
                       (char *) &mname6, sizeof (mname6)) < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
            if (join) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
  2190
                NET_ThrowCurrent(env, "setsockopt " S_ADD_MEMBERSHIP " failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
                if (errno == ENOENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
                   JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
                        "Not a member of the multicast group");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
                } else {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
  2196
                    NET_ThrowCurrent(env, "setsockopt " S_DRP_MEMBERSHIP " failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
 * Method:    join
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
 * Signature: (Ljava/net/InetAddress;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
Java_java_net_PlainDatagramSocketImpl_join(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
                                           jobject iaObj, jobject niObj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
    mcast_join_leave(env, this, iaObj, niObj, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
 * Method:    leave
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
 * Signature: (Ljava/net/InetAddress;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
Java_java_net_PlainDatagramSocketImpl_leave(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
                                            jobject iaObj, jobject niObj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
    mcast_join_leave(env, this, iaObj, niObj, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
}