jdk/src/solaris/native/java/net/PlainDatagramSocketImpl.c
author xdono
Tue, 28 Jul 2009 12:12:54 -0700
changeset 3288 db82a42da273
parent 1247 b4c26443dee5
child 4814 dbf72872f8d2
permissions -rw-r--r--
6862919: Update copyright year Summary: Update copyright for files that have been modified in 2009, up to 07/09 Reviewed-by: tbell, ohair
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
1247
b4c26443dee5 6754988: Update copyright year
xdono
parents: 1097
diff changeset
     2
 * Copyright 1997-2008 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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__
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include <linux/unistd.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#include <linux/sysctl.h>
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
#ifndef IPTOS_TOS_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
#define IPTOS_TOS_MASK 0x1e
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
#ifndef IPTOS_PREC_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
#define IPTOS_PREC_MASK 0xe0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
#include "net_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
#include "java_net_SocketOptions.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
#include "java_net_PlainDatagramSocketImpl.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
#include "java_net_NetworkInterface.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
/************************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
static jfieldID IO_fd_fdID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
static jfieldID pdsi_fdID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
static jfieldID pdsi_timeoutID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
static jfieldID pdsi_trafficClassID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
static jfieldID pdsi_localPortID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
static jfieldID pdsi_connected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
static jfieldID pdsi_connectedAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
static jfieldID pdsi_connectedPort;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
static jboolean isOldKernel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
#if defined(__linux__) && defined(AF_INET6)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
static jfieldID pdsi_multicastInterfaceID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
static jfieldID pdsi_loopbackID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
static jfieldID pdsi_ttlID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    Java_java_net_InetAddress_init(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    Java_java_net_Inet4Address_init(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    Java_java_net_Inet6Address_init(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    Java_java_net_NetworkInterface_init(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * We need to determine if this is a 2.2 kernel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    if (uname(&sysinfo) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        sysinfo.release[3] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        isOldKernel = (strcmp(sysinfo.release, "2.2") == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
         * uname failed - move to plan B and examine /proc/version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
         * If this fails assume that /proc has changed and that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
         * this must be new /proc format and hence new kernel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        FILE *fP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        isOldKernel = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if ((fP = fopen("/proc/version", "r")) != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            char ver[25];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            if (fgets(ver, sizeof(ver), fP) != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                isOldKernel = (strstr(ver, "2.2.") != NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            fclose(fP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    pdsi_multicastInterfaceID = (*env)->GetFieldID(env, cls, "multicastInterface", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    CHECK_NULL(pdsi_multicastInterfaceID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    pdsi_loopbackID = (*env)->GetFieldID(env, cls, "loopbackMode", "Z");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    CHECK_NULL(pdsi_loopbackID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    pdsi_ttlID = (*env)->GetFieldID(env, cls, "ttl", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    CHECK_NULL(pdsi_ttlID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
 * Method:    bind
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
 * Signature: (ILjava/net/InetAddress;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
Java_java_net_PlainDatagramSocketImpl_bind0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                                           jint localport, jobject iaObj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /* fdObj is the FileDescriptor field on this */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /* fd is an int field on fdObj */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    SOCKADDR him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    if (IS_NULL(iaObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        JNU_ThrowNullPointerException(env, "iaObj is null.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /* bind */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    if (NET_InetAddressToSockaddr(env, iaObj, localport, (struct sockaddr *)&him, &len, JNI_TRUE) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
      return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    if (NET_Bind(fd, (struct sockaddr *)&him, len) < 0)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        if (errno == EADDRINUSE || errno == EADDRNOTAVAIL ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            errno == EPERM || errno == EACCES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "BindException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                            "Bind failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                            "Bind failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /* intialize the local port */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    if (localport == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        /* Now that we're a connected socket, let's extract the port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
         * that the system chose for us and store it in the Socket object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        if (JVM_GetSockName(fd, (struct sockaddr *)&him, &len) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                            "Error getting socket name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        localport = NET_GetPortFromSockaddr((struct sockaddr *)&him);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        (*env)->SetIntField(env, this, pdsi_localPortID, localport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        (*env)->SetIntField(env, this, pdsi_localPortID, localport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
 * Method:    connect0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
 * Signature: (Ljava/net/InetAddress;I)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
Java_java_net_PlainDatagramSocketImpl_connect0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                                               jobject address, jint port) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /* The object's field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    /* The fdObj'fd */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    jint fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /* The packetAddress address, family and port */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    SOCKADDR rmtaddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    if (IS_NULL(address)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        JNU_ThrowNullPointerException(env, "address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    if (NET_InetAddressToSockaddr(env, address, port, (struct sockaddr *)&rmtaddr, &len, JNI_TRUE) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
      return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    if (isOldKernel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        int t = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        setsockopt(fd, SOL_SOCKET, SO_BSDCOMPAT, (char*) &t, sizeof(int));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        if (JVM_Connect(fd, (struct sockaddr *)&rmtaddr, len) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                            "Connect failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
 * Method:    disconnect0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
Java_java_net_PlainDatagramSocketImpl_disconnect0(JNIEnv *env, jobject this, jint family) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    /* The object's field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    /* The fdObj'fd */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    jint fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    SOCKADDR addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    if (isOldKernel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        int t = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        setsockopt(fd, SOL_SOCKET, SO_BSDCOMPAT, (char*) &t, sizeof(int));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        memset(&addr, 0, sizeof(addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            struct sockaddr_in6 *him6 = (struct sockaddr_in6 *)&addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            him6->sin6_family = AF_UNSPEC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            len = sizeof(struct sockaddr_in6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            struct sockaddr_in *him4 = (struct sockaddr_in*)&addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            him4->sin_family = AF_UNSPEC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            len = sizeof(struct sockaddr_in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        JVM_Connect(fd, (struct sockaddr *)&addr, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        // After disconnecting a UDP socket, Linux kernel will set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        // local port to zero if the port number comes from implicit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        // bind. Successive send/recv on the same socket will fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        // So bind again with former port number here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        int localPort = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        if (JVM_GetSockName(fd, (struct sockaddr *)&addr, &len) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        }
910
1f53246fb014 6729881: Compiler warning in networking native code
chegar
parents: 2
diff changeset
   372
        localPort = NET_GetPortFromSockaddr((struct sockaddr *)&addr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        if (localPort == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            localPort = (*env)->GetIntField(env, this, pdsi_localPortID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            if (((struct sockaddr*)&addr)->sa_family == AF_INET6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                ((struct sockaddr_in6*)&addr)->sin6_port = htons(localPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
#endif /* AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                ((struct sockaddr_in*)&addr)->sin_port = htons(localPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            NET_Bind(fd, (struct sockaddr *)&addr, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    JVM_Connect(fd, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
 * Method:    send
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
 * Signature: (Ljava/net/DatagramPacket;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
Java_java_net_PlainDatagramSocketImpl_send(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                                           jobject packet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    char BUF[MAX_BUFFER_LEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    char *fullPacket = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    int ret, mallocedPacket = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    /* The object's field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    jint trafficClass = (*env)->GetIntField(env, this, pdsi_trafficClassID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    jbyteArray packetBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    jobject packetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    jint packetBufferOffset, packetBufferLen, packetPort;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    jboolean connected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    /* The fdObj'fd */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    jint fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    SOCKADDR rmtaddr, *rmtaddrP=&rmtaddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    if (IS_NULL(packet)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        JNU_ThrowNullPointerException(env, "packet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    connected = (*env)->GetBooleanField(env, this, pdsi_connected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    packetBuffer = (*env)->GetObjectField(env, packet, dp_bufID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    packetAddress = (*env)->GetObjectField(env, packet, dp_addressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    if (IS_NULL(packetBuffer) || IS_NULL(packetAddress)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        JNU_ThrowNullPointerException(env, "null buffer || null address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    packetBufferOffset = (*env)->GetIntField(env, packet, dp_offsetID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    packetBufferLen = (*env)->GetIntField(env, packet, dp_lengthID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    if (connected && !isOldKernel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    if (connected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        /* arg to NET_Sendto () null in this case */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        rmtaddrP = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        packetPort = (*env)->GetIntField(env, packet, dp_portID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        if (NET_InetAddressToSockaddr(env, packetAddress, packetPort, (struct sockaddr *)&rmtaddr, &len, JNI_TRUE) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
          return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    if (packetBufferLen > MAX_BUFFER_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        /* When JNI-ifying the JDK's IO routines, we turned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
         * read's and write's of byte arrays of size greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
         * than 2048 bytes into several operations of size 2048.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
         * This saves a malloc()/memcpy()/free() for big
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
         * buffers.  This is OK for file IO and TCP, but that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
         * strategy violates the semantics of a datagram protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
         * (one big send) != (several smaller sends).  So here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
         * we *must* alloc the buffer.  Note it needn't be bigger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
         * than 65,536 (0xFFFF) the max size of an IP packet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
         * Anything bigger should be truncated anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
         * We may want to use a smarter allocation scheme at some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
         * point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        if (packetBufferLen > MAX_PACKET_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            packetBufferLen = MAX_PACKET_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        fullPacket = (char *)malloc(packetBufferLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        if (!fullPacket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            JNU_ThrowOutOfMemoryError(env, "heap allocation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            mallocedPacket = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        fullPacket = &(BUF[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    (*env)->GetByteArrayRegion(env, packetBuffer, packetBufferOffset, packetBufferLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                               (jbyte *)fullPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    if (trafficClass != 0 && ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        NET_SetTrafficClass((struct sockaddr *)&rmtaddr, trafficClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
#endif /* AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * Send the datagram.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * If we are connected it's possible that sendto will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * ECONNREFUSED indicating that an ICMP port unreachable has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    ret = NET_SendTo(fd, fullPacket, packetBufferLen, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                     (struct sockaddr *)rmtaddrP, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    if (ret < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        switch (ret) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            case JVM_IO_ERR :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                if (errno == ECONNREFUSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                    JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                            "ICMP Port Unreachable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                    NET_ThrowByNameWithLastError(env, "java/io/IOException", "sendto failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            case JVM_IO_INTR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                JNU_ThrowByName(env, "java/io/InterruptedIOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                                "operation interrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    if (mallocedPacket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        free(fullPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
 * Method:    peek
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
 * Signature: (Ljava/net/InetAddress;)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
Java_java_net_PlainDatagramSocketImpl_peek(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                                           jobject addressObj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    jint timeout = (*env)->GetIntField(env, this, pdsi_timeoutID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    jint fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    ssize_t n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    SOCKADDR remote_addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    char buf[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    jint family;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    jobject iaObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    if (IS_NULL(addressObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        JNU_ThrowNullPointerException(env, "Null address in peek()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    if (timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        int ret = NET_Timeout(fd, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        if (ret == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                            "Peek timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        } else if (ret == JVM_IO_ERR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            if (errno == EBADF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "Peek failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        } else if (ret == JVM_IO_INTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                            "operation interrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            return ret; /* WARNING: SHOULD WE REALLY RETURN -2??? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    len = SOCKADDR_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    n = NET_RecvFrom(fd, buf, 1, MSG_PEEK,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                     (struct sockaddr *)&remote_addr, &len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    if (n == JVM_IO_ERR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        if (errno == ECONNREFUSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            int orig_errno = errno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            (void) recv(fd, buf, 1, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            errno = orig_errno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        if (errno == ECONNREFUSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                            "ICMP Port Unreachable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            if (errno == EBADF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "Peek failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    } else if (n == JVM_IO_INTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        JNU_ThrowByName(env, "java/io/InterruptedIOException", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    iaObj = NET_SockaddrToInetAddress(env, (struct sockaddr *)&remote_addr, &port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    family = (*env)->GetIntField(env, iaObj, ia_familyID) == IPv4?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        AF_INET : AF_INET6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    if (family == AF_INET) { /* this api can't handle IPV6 addresses */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        int address = (*env)->GetIntField(env, iaObj, ia_addressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        (*env)->SetIntField(env, addressObj, ia_addressID, address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    return port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
Java_java_net_PlainDatagramSocketImpl_peekData(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                                           jobject packet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    char BUF[MAX_BUFFER_LEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    char *fullPacket = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    int mallocedPacket = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    jint timeout = (*env)->GetIntField(env, this, pdsi_timeoutID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    jbyteArray packetBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    jint packetBufferOffset, packetBufferLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    SOCKADDR remote_addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    if (IS_NULL(packet)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        JNU_ThrowNullPointerException(env, "packet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    packetBuffer = (*env)->GetObjectField(env, packet, dp_bufID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    if (IS_NULL(packetBuffer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        JNU_ThrowNullPointerException(env, "packet buffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    packetBufferOffset = (*env)->GetIntField(env, packet, dp_offsetID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    packetBufferLen = (*env)->GetIntField(env, packet, dp_bufLengthID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    if (timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        int ret = NET_Timeout(fd, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        if (ret == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                            "Receive timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        } else if (ret == JVM_IO_ERR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            if (errno == EBADF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "Receive failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        } else if (ret == JVM_IO_INTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                            "operation interrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    if (packetBufferLen > MAX_BUFFER_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        /* When JNI-ifying the JDK's IO routines, we turned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
         * read's and write's of byte arrays of size greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
         * than 2048 bytes into several operations of size 2048.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
         * This saves a malloc()/memcpy()/free() for big
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
         * buffers.  This is OK for file IO and TCP, but that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
         * strategy violates the semantics of a datagram protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
         * (one big send) != (several smaller sends).  So here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
         * we *must* alloc the buffer.  Note it needn't be bigger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
         * than 65,536 (0xFFFF) the max size of an IP packet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
         * anything bigger is truncated anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
         * We may want to use a smarter allocation scheme at some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
         * point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        if (packetBufferLen > MAX_PACKET_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            packetBufferLen = MAX_PACKET_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        fullPacket = (char *)malloc(packetBufferLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        if (!fullPacket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            JNU_ThrowOutOfMemoryError(env, "heap allocation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            mallocedPacket = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        fullPacket = &(BUF[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    len = SOCKADDR_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    n = NET_RecvFrom(fd, fullPacket, packetBufferLen, MSG_PEEK,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                     (struct sockaddr *)&remote_addr, &len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    /* truncate the data if the packet's length is too small */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    if (n > packetBufferLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        n = packetBufferLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    if (n == JVM_IO_ERR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        if (errno == ECONNREFUSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            int orig_errno = errno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            (void) recv(fd, fullPacket, 1, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            errno = orig_errno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        (*env)->SetIntField(env, packet, dp_offsetID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        (*env)->SetIntField(env, packet, dp_lengthID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        if (errno == ECONNREFUSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                            "ICMP Port Unreachable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            if (errno == EBADF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "Receive failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    } else if (n == JVM_IO_INTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        (*env)->SetIntField(env, packet, dp_offsetID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        (*env)->SetIntField(env, packet, dp_lengthID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                        "operation interrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
         * success - fill in received address...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
         * REMIND: Fill in an int on the packet, and create inetadd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
         * object in Java, as a performance improvement. Also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
         * construct the inetadd object lazily.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        jobject packetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
         * Check if there is an InetAddress already associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
         * packet. If so we check if it is the same source address. We
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
         * can't update any existing InetAddress because it is immutable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        packetAddress = (*env)->GetObjectField(env, packet, dp_addressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        if (packetAddress != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            if (!NET_SockaddrEqualsInetAddress(env, (struct sockaddr *)&remote_addr, packetAddress)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                /* force a new InetAddress to be created */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                packetAddress = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        if (packetAddress == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            packetAddress = NET_SockaddrToInetAddress(env, (struct sockaddr *)&remote_addr, &port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            /* stuff the new Inetaddress in the packet */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            (*env)->SetObjectField(env, packet, dp_addressID, packetAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            /* only get the new port number */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            port = NET_GetPortFromSockaddr((struct sockaddr *)&remote_addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        /* and fill in the data, remote address/port and such */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        (*env)->SetByteArrayRegion(env, packetBuffer, packetBufferOffset, n,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                                   (jbyte *)fullPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        (*env)->SetIntField(env, packet, dp_portID, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        (*env)->SetIntField(env, packet, dp_lengthID, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    if (mallocedPacket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        free(fullPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
    return port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
 * Method:    receive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
 * Signature: (Ljava/net/DatagramPacket;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
Java_java_net_PlainDatagramSocketImpl_receive0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                                              jobject packet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
    char BUF[MAX_BUFFER_LEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
    char *fullPacket = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
    int mallocedPacket = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    jint timeout = (*env)->GetIntField(env, this, pdsi_timeoutID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    jbyteArray packetBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
    jint packetBufferOffset, packetBufferLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
    SOCKADDR remote_addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    jboolean retry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    jboolean connected = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    jobject connectedAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
    jint connectedPort;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    jlong prevTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    if (IS_NULL(packet)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        JNU_ThrowNullPointerException(env, "packet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    packetBuffer = (*env)->GetObjectField(env, packet, dp_bufID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    if (IS_NULL(packetBuffer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        JNU_ThrowNullPointerException(env, "packet buffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    packetBufferOffset = (*env)->GetIntField(env, packet, dp_offsetID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    packetBufferLen = (*env)->GetIntField(env, packet, dp_bufLengthID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    if (packetBufferLen > MAX_BUFFER_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        /* When JNI-ifying the JDK's IO routines, we turned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
         * read's and write's of byte arrays of size greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
         * than 2048 bytes into several operations of size 2048.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
         * This saves a malloc()/memcpy()/free() for big
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
         * buffers.  This is OK for file IO and TCP, but that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
         * strategy violates the semantics of a datagram protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
         * (one big send) != (several smaller sends).  So here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
         * we *must* alloc the buffer.  Note it needn't be bigger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
         * than 65,536 (0xFFFF) the max size of an IP packet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
         * anything bigger is truncated anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
         * We may want to use a smarter allocation scheme at some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
         * point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        if (packetBufferLen > MAX_PACKET_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            packetBufferLen = MAX_PACKET_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        fullPacket = (char *)malloc(packetBufferLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        if (!fullPacket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            JNU_ThrowOutOfMemoryError(env, "heap allocation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            mallocedPacket = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        fullPacket = &(BUF[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * On Linux with the 2.2 kernel we simulate connected datagrams by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * discarding packets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    if (isOldKernel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        connected = (*env)->GetBooleanField(env, this, pdsi_connected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        if (connected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            connectedAddress = (*env)->GetObjectField(env, this, pdsi_connectedAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            connectedPort = (*env)->GetIntField(env, this, pdsi_connectedPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            if (timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                prevTime = JVM_CurrentTimeMillis(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        retry = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        if (timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            int ret = NET_Timeout(fd, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            if (ret <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                if (ret == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                    JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                                    "Receive timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                } else if (ret == JVM_IO_ERR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                    if (errno == EBADF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                         JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                     } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                         NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "Receive failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                     JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                } else if (ret == JVM_IO_INTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                    JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                                    "operation interrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                if (mallocedPacket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                    free(fullPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
         * Security Note: For Linux 2.2 with connected datagrams ensure that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
         * you receive into the stack/heap allocated buffer - do not attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
         * to receive directly into DatagramPacket's byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
         * (ie: if the virtual machine support pinning don't use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
         * GetByteArrayElements or a JNI critical section and receive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
         * directly into the byte array)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        len = SOCKADDR_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        n = NET_RecvFrom(fd, fullPacket, packetBufferLen, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                         (struct sockaddr *)&remote_addr, &len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        /* truncate the data if the packet's length is too small */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        if (n > packetBufferLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            n = packetBufferLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        if (n == JVM_IO_ERR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
            (*env)->SetIntField(env, packet, dp_offsetID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            (*env)->SetIntField(env, packet, dp_lengthID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
            if (errno == ECONNREFUSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                                "ICMP Port Unreachable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                if (errno == EBADF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                     JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                 } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                     NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "Receive failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        } else if (n == JVM_IO_INTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            (*env)->SetIntField(env, packet, dp_offsetID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            (*env)->SetIntField(env, packet, dp_lengthID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                            "operation interrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            jobject packetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
             * If we are connected then we know that the datagram that we have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
             * received is from the address that we are connected too. However
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
             * on Linux with 2.2 kernel we have to simulate this behaviour by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
             * discarding any datagrams that aren't from the connected address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
            if (isOldKernel && connected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                if (NET_GetPortFromSockaddr((struct sockaddr *)&remote_addr) != connectedPort ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                    !NET_SockaddrEqualsInetAddress(env, (struct sockaddr *)&remote_addr, connectedAddress)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                     * Discard the datagram as it's not from the connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                     * address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                    retry = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                     * Adjust timeout if necessary to ensure that we adhere to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                     * timeout semantics.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                    if (timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                        jlong newTime = JVM_CurrentTimeMillis(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                        timeout -= (newTime - prevTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                        if (timeout <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
                                    "Receive timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
                            if (mallocedPacket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                                free(fullPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                        prevTime = newTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
             * success - fill in received address...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
             * REMIND: Fill in an int on the packet, and create inetadd
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
             * object in Java, as a performance improvement. Also
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
             * construct the inetadd object lazily.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
             * Check if there is an InetAddress already associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
             * packet. If so we check if it is the same source address. We
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
             * can't update any existing InetAddress because it is immutable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            packetAddress = (*env)->GetObjectField(env, packet, dp_addressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            if (packetAddress != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                if (!NET_SockaddrEqualsInetAddress(env, (struct sockaddr *)&remote_addr, packetAddress)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                    /* force a new InetAddress to be created */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                    packetAddress = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            if (packetAddress == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                packetAddress = NET_SockaddrToInetAddress(env, (struct sockaddr *)&remote_addr, &port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                /* stuff the new Inetaddress in the packet */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                (*env)->SetObjectField(env, packet, dp_addressID, packetAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                /* only get the new port number */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                port = NET_GetPortFromSockaddr((struct sockaddr *)&remote_addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
            /* and fill in the data, remote address/port and such */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            (*env)->SetByteArrayRegion(env, packetBuffer, packetBufferOffset, n,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                                       (jbyte *)fullPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            (*env)->SetIntField(env, packet, dp_portID, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            (*env)->SetIntField(env, packet, dp_lengthID, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
    } while (retry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
    if (mallocedPacket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        free(fullPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
 * Method:    datagramSocketCreate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
Java_java_net_PlainDatagramSocketImpl_datagramSocketCreate(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
                                                           jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
    int t = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
            fd =  JVM_Socket(AF_INET6, SOCK_DGRAM, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
#endif /* AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                fd =  JVM_Socket(AF_INET, SOCK_DGRAM, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
    if (fd == JVM_IO_ERR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                       "Error creating socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (char*) &t, sizeof(int));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
    if (isOldKernel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        setsockopt(fd, SOL_SOCKET, SO_BSDCOMPAT, (char*) &t, sizeof(int));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     * On Linux for IPv6 sockets we must set the hop limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     * to 1 to be compatible with default ttl of 1 for IPv4 sockets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
    if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        int ttl = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, (char *)&ttl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                   sizeof(ttl));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        if (isOldKernel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
            (*env)->SetIntField(env, this, pdsi_ttlID, ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
#endif /* __linux__ */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
    (*env)->SetIntField(env, fdObj, IO_fd_fdID, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
 * Method:    datagramSocketClose
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
Java_java_net_PlainDatagramSocketImpl_datagramSocketClose(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                                                          jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     * REMIND: PUT A LOCK AROUND THIS CODE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
    fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
    if (fd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
    (*env)->SetIntField(env, fdObj, IO_fd_fdID, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
    NET_SocketClose(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
 * Set outgoing multicast interface designated by a NetworkInterface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
 * Throw exception if failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
static void mcast_set_if_by_if_v4(JNIEnv *env, jobject this, int fd, jobject value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
    static jfieldID ni_addrsID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
    static jfieldID ia_addressID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
    struct in_addr in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
    jobjectArray addrArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
    jsize len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
    jobject addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
    if (ni_addrsID == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        CHECK_NULL(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        ni_addrsID = (*env)->GetFieldID(env, c, "addrs",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
                                        "[Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        CHECK_NULL(ni_addrsID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        c = (*env)->FindClass(env,"java/net/InetAddress");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        CHECK_NULL(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        ia_addressID = (*env)->GetFieldID(env, c, "address", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        CHECK_NULL(ia_addressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
    addrArray = (*env)->GetObjectField(env, value, ni_addrsID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
    len = (*env)->GetArrayLength(env, addrArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * Check that there is at least one address bound to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
    if (len < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
            "bad argument for IP_MULTICAST_IF2: No IP addresses bound to interface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     * We need an ipv4 address here
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
    for (i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        addr = (*env)->GetObjectArrayElement(env, addrArray, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        if ((*env)->GetIntField(env, addr, ia_familyID) == IPv4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
            in.s_addr = htonl((*env)->GetIntField(env, addr, ia_addressID));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
    if (JVM_SetSockOpt(fd, IPPROTO_IP, IP_MULTICAST_IF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
                       (const char*)&in, sizeof(in)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
                       "Error setting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
 * Set outgoing multicast interface designated by a NetworkInterface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
 * Throw exception if failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
static void mcast_set_if_by_if_v6(JNIEnv *env, jobject this, int fd, jobject value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
    static jfieldID ni_indexID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
    int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
    if (ni_indexID == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        CHECK_NULL(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        ni_indexID = (*env)->GetFieldID(env, c, "index", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        CHECK_NULL(ni_indexID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
    index = (*env)->GetIntField(env, value, ni_indexID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
    if (JVM_SetSockOpt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                       (const char*)&index, sizeof(index)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        if (errno == EINVAL && index > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
                "IPV6_MULTICAST_IF failed (interface has IPv4 "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
                "address only?)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                           "Error setting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     * Linux 2.2 kernel doesn't support IPV6_MULTICAST_IF socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     * option so record index for later retrival.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
    if (isOldKernel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        (*env)->SetIntField(env, this, pdsi_multicastInterfaceID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
                            (jint)index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
 * Set outgoing multicast interface designated by an InetAddress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
 * Throw exception if failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
static void mcast_set_if_by_addr_v4(JNIEnv *env, jobject this, int fd, jobject value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
    static jfieldID ia_addressID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
    struct in_addr in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
    if (ia_addressID == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        jclass c = (*env)->FindClass(env,"java/net/InetAddress");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        CHECK_NULL(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
        ia_addressID = (*env)->GetFieldID(env, c, "address", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
        CHECK_NULL(ia_addressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
    in.s_addr = htonl( (*env)->GetIntField(env, value, ia_addressID) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
    if (JVM_SetSockOpt(fd, IPPROTO_IP, IP_MULTICAST_IF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
                       (const char*)&in, sizeof(in)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
                         "Error setting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
 * Set outgoing multicast interface designated by an InetAddress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
 * Throw exception if failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
static void mcast_set_if_by_addr_v6(JNIEnv *env, jobject this, int fd, jobject value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
    static jclass ni_class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
    if (ni_class == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        CHECK_NULL(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        ni_class = (*env)->NewGlobalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        CHECK_NULL(ni_class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
    value = Java_java_net_NetworkInterface_getByInetAddress0(env, ni_class, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
    if (value == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        if (!(*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
                 "bad argument for IP_MULTICAST_IF"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
                 ": address not bound to any interface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
    mcast_set_if_by_if_v6(env, this, fd, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
 * Sets the multicast interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
 * SocketOptions.IP_MULTICAST_IF :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
 *      value is a InetAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
 *      IPv4:   set outgoing multicast interface using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
 *              IPPROTO_IP/IP_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
 *      IPv6:   Get the index of the interface to which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
 *              InetAddress is bound
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
 *              Set outgoing multicast interface using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
 *              IPPROTO_IPV6/IPV6_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
 *              On Linux 2.2 record interface index as can't
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
 *              query the multicast interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
 * SockOptions.IF_MULTICAST_IF2 :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
 *      value is a NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
 *      IPv4:   Obtain IP address bound to network interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
 *              (NetworkInterface.addres[0])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
 *              set outgoing multicast interface using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
 *              IPPROTO_IP/IP_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
 *      IPv6:   Obtain NetworkInterface.index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
 *              Set outgoing multicast interface using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
 *              IPPROTO_IPV6/IPV6_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
 *              On Linux 2.2 record interface index as can't
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
 *              query the multicast interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
static void setMulticastInterface(JNIEnv *env, jobject this, int fd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
                                  jint opt, jobject value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
    if (opt == java_net_SocketOptions_IP_MULTICAST_IF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
         * value is an InetAddress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
        if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
            mcast_set_if_by_addr_v6(env, this, fd, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
            mcast_set_if_by_addr_v4(env, this, fd, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
        mcast_set_if_by_addr_v4(env, this, fd, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
        if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
            mcast_set_if_by_addr_v6(env, this, fd, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
    if (opt == java_net_SocketOptions_IP_MULTICAST_IF2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
         * value is a NetworkInterface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
        if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
            mcast_set_if_by_if_v6(env, this, fd, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
            mcast_set_if_by_if_v4(env, this, fd, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        mcast_set_if_by_if_v4(env, this, fd, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
        if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
            mcast_set_if_by_if_v6(env, this, fd, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
 * Enable/disable local loopback of multicast datagrams.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
static void mcast_set_loop_v4(JNIEnv *env, jobject this, int fd, jobject value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
    jclass cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
    jfieldID fid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
    jboolean on;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
    char loopback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
    cls = (*env)->FindClass(env, "java/lang/Boolean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
    CHECK_NULL(cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
    fid =  (*env)->GetFieldID(env, cls, "value", "Z");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
    CHECK_NULL(fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
    on = (*env)->GetBooleanField(env, value, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
    loopback = (!on ? 1 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
    if (NET_SetSockOpt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, (const void *)&loopback, sizeof(char)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "Error setting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
 * Enable/disable local loopback of multicast datagrams.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
static void mcast_set_loop_v6(JNIEnv *env, jobject this, int fd, jobject value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
    jclass cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
    jfieldID fid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
    jboolean on;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
    int loopback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
    cls = (*env)->FindClass(env, "java/lang/Boolean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
    CHECK_NULL(cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
    fid =  (*env)->GetFieldID(env, cls, "value", "Z");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
    CHECK_NULL(fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
    on = (*env)->GetBooleanField(env, value, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
    loopback = (!on ? 1 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
    if (NET_SetSockOpt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, (const void *)&loopback, sizeof(int)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "Error setting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     * Can't query IPV6_MULTICAST_LOOP on Linux 2.2 kernel so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     * store it in impl so that we can simulate getsockopt.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
    if (isOldKernel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
        (*env)->SetBooleanField(env, this, pdsi_loopbackID, on);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
 * Sets the multicast loopback mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
static void setMulticastLoopbackMode(JNIEnv *env, jobject this, int fd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
                                  jint opt, jobject value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
    if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
        mcast_set_loop_v6(env, this, fd, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
        mcast_set_loop_v4(env, this, fd, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
    mcast_set_loop_v4(env, this, fd, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
    if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
        mcast_set_loop_v6(env, this, fd, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
 * Method:    socketSetOption
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
 * Signature: (ILjava/lang/Object;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
Java_java_net_PlainDatagramSocketImpl_socketSetOption(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
                                                      jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
                                                      jint opt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
                                                      jobject value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
    int level, optname, optlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
    union {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
        char c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
    } optval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
     * Check that socket hasn't been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
    fd = getFD(env, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
    if (fd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
     * Check argument has been provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
    if (IS_NULL(value)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
        JNU_ThrowNullPointerException(env, "value argument");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
     * Setting the multicast interface handled seperately
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
    if (opt == java_net_SocketOptions_IP_MULTICAST_IF ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
        opt == java_net_SocketOptions_IP_MULTICAST_IF2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
        setMulticastInterface(env, this, fd, opt, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
        return;
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
     * Setting the multicast loopback mode handled separately
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
    if (opt == java_net_SocketOptions_IP_MULTICAST_LOOP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
        setMulticastLoopbackMode(env, this, fd, opt, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
     * Map the Java level socket option to the platform specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
     * level and option name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
    if (NET_MapSocketOption(opt, &level, &optname)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Invalid option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
    switch (opt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
        case java_net_SocketOptions_SO_SNDBUF :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
        case java_net_SocketOptions_SO_RCVBUF :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
        case java_net_SocketOptions_IP_TOS :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
                jclass cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
                jfieldID fid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
                cls = (*env)->FindClass(env, "java/lang/Integer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
                CHECK_NULL(cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
                fid =  (*env)->GetFieldID(env, cls, "value", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
                CHECK_NULL(fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
                optval.i = (*env)->GetIntField(env, value, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
                optlen = sizeof(optval.i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
        case java_net_SocketOptions_SO_REUSEADDR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
        case java_net_SocketOptions_SO_BROADCAST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
                jclass cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
                jfieldID fid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
                jboolean on;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
                cls = (*env)->FindClass(env, "java/lang/Boolean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
                CHECK_NULL(cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
                fid =  (*env)->GetFieldID(env, cls, "value", "Z");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
                CHECK_NULL(fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
                on = (*env)->GetBooleanField(env, value, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
                /* SO_REUSEADDR or SO_BROADCAST */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
                optval.i = (on ? 1 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
                optlen = sizeof(optval.i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
        default :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
                "Socket option not supported by PlainDatagramSocketImp");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
    if (NET_SetSockOpt(fd, level, optname, (const void *)&optval, optlen) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "Error setting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
 * Return the multicast interface:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
 * SocketOptions.IP_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
 *      IPv4:   Query IPPROTO_IP/IP_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
 *              Create InetAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
 *              IP_MULTICAST_IF returns struct ip_mreqn on 2.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
 *              kernel but struct in_addr on 2.4 kernel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
 *      IPv6:   Query IPPROTO_IPV6 / IPV6_MULTICAST_IF or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
 *              obtain from impl is Linux 2.2 kernel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
 *              If index == 0 return InetAddress representing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
 *              anyLocalAddress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
 *              If index > 0 query NetworkInterface by index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
 *              and returns addrs[0]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
 * SocketOptions.IP_MULTICAST_IF2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
 *      IPv4:   Query IPPROTO_IP/IP_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
 *              Query NetworkInterface by IP address and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
 *              return the NetworkInterface that the address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
 *              is bound too.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
 *      IPv6:   Query IPPROTO_IPV6 / IPV6_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
 *              (except Linux .2 kernel)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
 *              Query NetworkInterface by index and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
 *              return NetworkInterface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
jobject getMulticastInterface(JNIEnv *env, jobject this, int fd, jint opt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
    jboolean isIPV4 = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
    if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
        isIPV4 = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
     * IPv4 implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
    if (isIPV4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
        static jclass inet4_class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
        static jmethodID inet4_ctrID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
        static jfieldID inet4_addrID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
        static jclass ni_class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
        static jmethodID ni_ctrID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
        static jfieldID ni_indexID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
        static jfieldID ni_addrsID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
        jobjectArray addrArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
        jobject addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
        jobject ni;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
        struct in_addr in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
        struct in_addr *inP = &in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
        int len = sizeof(struct in_addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
        struct ip_mreqn mreqn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
        if (isOldKernel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
            inP = (struct in_addr *)&mreqn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
            len = sizeof(struct ip_mreqn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
        if (JVM_GetSockOpt(fd, IPPROTO_IP, IP_MULTICAST_IF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
                           (char *)inP, &len) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
                             "Error getting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
         * Construct and populate an Inet4Address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
        if (inet4_class == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
            jclass c = (*env)->FindClass(env, "java/net/Inet4Address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
            CHECK_NULL_RETURN(c, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
            inet4_ctrID = (*env)->GetMethodID(env, c, "<init>", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
            CHECK_NULL_RETURN(inet4_ctrID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
            inet4_addrID = (*env)->GetFieldID(env, c, "address", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
            CHECK_NULL_RETURN(inet4_addrID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
            inet4_class = (*env)->NewGlobalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
            CHECK_NULL_RETURN(inet4_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
        addr = (*env)->NewObject(env, inet4_class, inet4_ctrID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
        CHECK_NULL_RETURN(addr, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
        (*env)->SetIntField(env, addr, inet4_addrID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
                (isOldKernel ? ntohl(mreqn.imr_address.s_addr) : ntohl(in.s_addr)) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
        (*env)->SetIntField(env, addr, inet4_addrID, ntohl(in.s_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
         * For IP_MULTICAST_IF return InetAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
        if (opt == java_net_SocketOptions_IP_MULTICAST_IF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
            return addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
         * For IP_MULTICAST_IF2 we get the NetworkInterface for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
         * this address and return it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
        if (ni_class == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
            jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
            CHECK_NULL_RETURN(c, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
            ni_ctrID = (*env)->GetMethodID(env, c, "<init>", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
            CHECK_NULL_RETURN(ni_ctrID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
            ni_indexID = (*env)->GetFieldID(env, c, "index", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
            CHECK_NULL_RETURN(ni_indexID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
            ni_addrsID = (*env)->GetFieldID(env, c, "addrs",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
                                            "[Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
            CHECK_NULL_RETURN(ni_addrsID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
            ni_class = (*env)->NewGlobalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
            CHECK_NULL_RETURN(ni_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
        ni = Java_java_net_NetworkInterface_getByInetAddress0(env, ni_class, addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
        if (ni) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
            return ni;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
         * The address doesn't appear to be bound at any known
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
         * NetworkInterface. Therefore we construct a NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
         * with this address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
        ni = (*env)->NewObject(env, ni_class, ni_ctrID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
        CHECK_NULL_RETURN(ni, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
        (*env)->SetIntField(env, ni, ni_indexID, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
        addrArray = (*env)->NewObjectArray(env, 1, inet4_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
        CHECK_NULL_RETURN(addrArray, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
        (*env)->SetObjectArrayElement(env, addrArray, 0, addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
        (*env)->SetObjectField(env, ni, ni_addrsID, addrArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
        return ni;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
     * IPv6 implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
    if ((opt == java_net_SocketOptions_IP_MULTICAST_IF) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
        (opt == java_net_SocketOptions_IP_MULTICAST_IF2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
        static jclass ni_class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
        static jmethodID ni_ctrID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
        static jfieldID ni_indexID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
        static jfieldID ni_addrsID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
        static jclass ia_class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
        static jmethodID ia_anyLocalAddressID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
        int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
        int len = sizeof(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
        jobjectArray addrArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
        jobject addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
        jobject ni;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
         * Linux 2.2 kernel doesn't support IPV6_MULTICAST_IF socke option
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
         * so use cached index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
        if (isOldKernel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
            index = (*env)->GetIntField(env, this, pdsi_multicastInterfaceID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
            if (JVM_GetSockOpt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
                               (char*)&index, &len) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
                NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
                               "Error getting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
                return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
        if (ni_class == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
            jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
            CHECK_NULL_RETURN(c, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
            ni_ctrID = (*env)->GetMethodID(env, c, "<init>", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
            CHECK_NULL_RETURN(ni_ctrID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
            ni_indexID = (*env)->GetFieldID(env, c, "index", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
            CHECK_NULL_RETURN(ni_indexID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
            ni_addrsID = (*env)->GetFieldID(env, c, "addrs",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
                                            "[Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
            CHECK_NULL_RETURN(ni_addrsID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
            ia_class = (*env)->FindClass(env, "java/net/InetAddress");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
            CHECK_NULL_RETURN(ia_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
            ia_class = (*env)->NewGlobalRef(env, ia_class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
            CHECK_NULL_RETURN(ia_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
            ia_anyLocalAddressID = (*env)->GetStaticMethodID(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
                                                             ia_class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
                                                             "anyLocalAddress",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
                                                             "()Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
            CHECK_NULL_RETURN(ia_anyLocalAddressID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
            ni_class = (*env)->NewGlobalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
            CHECK_NULL_RETURN(ni_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
         * If multicast to a specific interface then return the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
         * interface (for IF2) or the any address on that interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
         * (for IF).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
        if (index > 0) {
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 910
diff changeset
  1744
            ni = Java_java_net_NetworkInterface_getByIndex0(env, ni_class,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
                                                                   index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
            if (ni == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
                char errmsg[255];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
                sprintf(errmsg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
                        "IPV6_MULTICAST_IF returned index to unrecognized interface: %d",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
                        index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
                JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", errmsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
                return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
             * For IP_MULTICAST_IF2 return the NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
            if (opt == java_net_SocketOptions_IP_MULTICAST_IF2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
                return ni;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
             * For IP_MULTICAST_IF return addrs[0]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
            addrArray = (*env)->GetObjectField(env, ni, ni_addrsID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
            if ((*env)->GetArrayLength(env, addrArray) < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
                JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
                    "IPV6_MULTICAST_IF returned interface without IP bindings");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
                return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
            addr = (*env)->GetObjectArrayElement(env, addrArray, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
            return addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
         * Multicast to any address - return anyLocalAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
         * or a NetworkInterface with addrs[0] set to anyLocalAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
        addr = (*env)->CallStaticObjectMethod(env, ia_class, ia_anyLocalAddressID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
                                              NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
        if (opt == java_net_SocketOptions_IP_MULTICAST_IF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
            return addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
        ni = (*env)->NewObject(env, ni_class, ni_ctrID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
        CHECK_NULL_RETURN(ni, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
        (*env)->SetIntField(env, ni, ni_indexID, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
        addrArray = (*env)->NewObjectArray(env, 1, ia_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
        CHECK_NULL_RETURN(addrArray, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
        (*env)->SetObjectArrayElement(env, addrArray, 0, addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
        (*env)->SetObjectField(env, ni, ni_addrsID, addrArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
        return ni;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
 * Returns relevant info as a jint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
 * Method:    socketGetOption
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
 * Signature: (I)Ljava/lang/Object;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
JNIEXPORT jobject JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
Java_java_net_PlainDatagramSocketImpl_socketGetOption(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
                                                      jint opt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
    int level, optname, optlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
    union {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
        char c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
    } optval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
    fd = getFD(env, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
    if (fd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
                        "socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
     * Handle IP_MULTICAST_IF seperately
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
    if (opt == java_net_SocketOptions_IP_MULTICAST_IF ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
        opt == java_net_SocketOptions_IP_MULTICAST_IF2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
        return getMulticastInterface(env, this, fd, opt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
     * SO_BINDADDR implemented using getsockname
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
    if (opt == java_net_SocketOptions_SO_BINDADDR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
        /* find out local IP address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
        SOCKADDR him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
        int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
        int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
        jobject iaObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
        len = SOCKADDR_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
        if (getsockname(fd, (struct sockaddr *)&him, &len) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
                           "Error getting socket name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
        iaObj = NET_SockaddrToInetAddress(env, (struct sockaddr *)&him, &port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
        return iaObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
     * Map the Java level socket option to the platform specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
     * level and option name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
    if (NET_MapSocketOption(opt, &level, &optname)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Invalid option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
     * IP_MULTICAST_LOOP socket option isn't available on Linux 2.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
     * kernel with IPv6 so return value stored in impl.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
#if defined(AF_INET6) && defined(__linux__)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
    if (isOldKernel && opt == java_net_SocketOptions_IP_MULTICAST_LOOP &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
        level == IPPROTO_IPV6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
        int mode = (int)(*env)->GetBooleanField(env, this, pdsi_loopbackID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
        return createBoolean(env, mode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
    if (opt == java_net_SocketOptions_IP_MULTICAST_LOOP &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
        level == IPPROTO_IP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
        optlen = sizeof(optval.c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
        optlen = sizeof(optval.i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
    if (NET_GetSockOpt(fd, level, optname, (void *)&optval, &optlen) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
                         "Error getting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
    switch (opt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
        case java_net_SocketOptions_IP_MULTICAST_LOOP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
            /* getLoopbackMode() returns true if IP_MULTICAST_LOOP disabled */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
            if (level == IPPROTO_IP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
                return createBoolean(env, (int)!optval.c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
                return createBoolean(env, !optval.i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
        case java_net_SocketOptions_SO_BROADCAST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
        case java_net_SocketOptions_SO_REUSEADDR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
            return createBoolean(env, optval.i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
        case java_net_SocketOptions_SO_SNDBUF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
        case java_net_SocketOptions_SO_RCVBUF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
        case java_net_SocketOptions_IP_TOS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
            return createInteger(env, optval.i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
    /* should never rearch here */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
 * Multicast-related calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
Java_java_net_PlainDatagramSocketImpl_setTTL(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
                                             jbyte ttl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
    jint ittl = ttl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
    if (ittl < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
        ittl += 0x100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
    Java_java_net_PlainDatagramSocketImpl_setTimeToLive(env, this, ittl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
 * Set TTL for a socket. Throw exception if failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
static void setTTL(JNIEnv *env, int fd, jint ttl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
    char ittl = (char)ttl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
    if (JVM_SetSockOpt(fd, IPPROTO_IP, IP_MULTICAST_TTL, (char*)&ittl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
                       sizeof(ittl)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
                       "Error setting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
 * Set hops limit for a socket. Throw exception if failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
static void setHopLimit(JNIEnv *env, int fd, jint ttl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
    int ittl = (int)ttl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
    if (JVM_SetSockOpt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
                       (char*)&ittl, sizeof(ittl)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
                       "Error setting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
 * Method:    setTTL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
 * Signature: (B)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
Java_java_net_PlainDatagramSocketImpl_setTimeToLive(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
                                                    jint ttl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
    /* it is important to cast this to a char, otherwise setsockopt gets confused */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
    /* setsockopt to be correct ttl */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
    if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
        setHopLimit(env, fd, ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
        setTTL(env, fd, ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
    setTTL(env, fd, ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
    if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
        setHopLimit(env, fd, ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
        if (isOldKernel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
            (*env)->SetIntField(env, this, pdsi_ttlID, ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
 * Method:    getTTL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
 * Signature: ()B
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
JNIEXPORT jbyte JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
Java_java_net_PlainDatagramSocketImpl_getTTL(JNIEnv *env, jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
    return (jbyte)Java_java_net_PlainDatagramSocketImpl_getTimeToLive(env, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
}
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
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
 * Method:    getTTL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
 * Signature: ()B
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
Java_java_net_PlainDatagramSocketImpl_getTimeToLive(JNIEnv *env, jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
    jint fd = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
    /* getsockopt of ttl */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
    if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
        int ttl = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
        int len = sizeof(ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
         * Linux 2.2 kernel doesn't support IPV6_MULTICAST_HOPS socket option
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
        if (isOldKernel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
            return (*env)->GetIntField(env, this, pdsi_ttlID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
        if (JVM_GetSockOpt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
                               (char*)&ttl, &len) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
                NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
                               "Error getting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
        return (jint)ttl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
    } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
#endif /* AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
            u_char ttl = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
            int len = sizeof(ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
            if (JVM_GetSockOpt(fd, IPPROTO_IP, IP_MULTICAST_TTL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
                               (char*)&ttl, &len) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
                NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
                               "Error getting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
            return (jint)ttl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
 * mcast_join_leave: Join or leave a multicast group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
 * For IPv4 sockets use IP_ADD_MEMBERSHIP/IP_DROP_MEMBERSHIP socket option
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
 * to join/leave multicast group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
 * For IPv6 sockets use IPV6_ADD_MEMBERSHIP/IPV6_DROP_MEMBERSHIP socket option
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
 * to join/leave multicast group. If multicast group is an IPv4 address then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
 * an IPv4-mapped address is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
 * On Linux with IPv6 if we wish to join/leave an IPv4 multicast group then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
 * we must use the IPv4 socket options. This is because the IPv6 socket options
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
 * 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
  2072
 * kernel releases. In the future it's possible that IP_ADD_MEMBERSHIP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
 * will be updated to return ENOPROTOOPT if uses with an IPv6 socket (Solaris
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
 * already does this). Thus to cater for this we first try with the IPv4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
 * socket options and if they fail we use the IPv6 socket options. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
 * seems a reasonable failsafe solution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
static void mcast_join_leave(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
                             jobject iaObj, jobject niObj,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
                             jboolean join) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
    jint fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
    jint ipv6_join_leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
    if (IS_NULL(iaObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
        JNU_ThrowNullPointerException(env, "iaObj");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
     * Determine if this is an IPv4 or IPv6 join/leave.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
    ipv6_join_leave = ipv6_available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
    if ((*env)->GetIntField(env, iaObj, ia_familyID) == IPv4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
        ipv6_join_leave = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
     * IPv6 not compiled in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
    ipv6_join_leave = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
     * For IPv4 join use IP_ADD_MEMBERSHIP/IP_DROP_MEMBERSHIP socket option
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
     * On Linux if IPv4 or IPv6 use IP_ADD_MEMBERSHIP/IP_DROP_MEMBERSHIP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
    if (!ipv6_join_leave) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
        struct ip_mreqn mname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
        struct ip_mreq mname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
        int mname_len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
         * joinGroup(InetAddress, NetworkInterface) implementation :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
         * Linux/IPv6:  use ip_mreqn structure populated with multicast
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
         *              address and interface index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
         * IPv4:        use ip_mreq structure populated with multicast
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
         *              address and first address obtained from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
         *              NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
        if (niObj != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
#if defined(__linux__) && defined(AF_INET6)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
            if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
                static jfieldID ni_indexID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
                if (ni_indexID == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
                    jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
                    CHECK_NULL(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
                    ni_indexID = (*env)->GetFieldID(env, c, "index", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
                    CHECK_NULL(ni_indexID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
                mname.imr_multiaddr.s_addr = htonl((*env)->GetIntField(env, iaObj, ia_addressID));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
                mname.imr_address.s_addr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
                mname.imr_ifindex =  (*env)->GetIntField(env, niObj, ni_indexID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
                mname_len = sizeof(struct ip_mreqn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
                jobjectArray addrArray = (*env)->GetObjectField(env, niObj, ni_addrsID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
                jobject addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
                if ((*env)->GetArrayLength(env, addrArray) < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
                    JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
                        "bad argument for IP_ADD_MEMBERSHIP: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
                        "No IP addresses bound to interface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
                addr = (*env)->GetObjectArrayElement(env, addrArray, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
                mname.imr_multiaddr.s_addr = htonl((*env)->GetIntField(env, iaObj, ia_addressID));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
                mname.imr_address.s_addr = htonl((*env)->GetIntField(env, addr, ia_addressID));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
                mname.imr_interface.s_addr = htonl((*env)->GetIntField(env, addr, ia_addressID));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
                mname_len = sizeof(struct ip_mreq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
         * joinGroup(InetAddress) implementation :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
         * Linux/IPv6:  use ip_mreqn structure populated with multicast
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
         *              address and interface index. index obtained
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
         *              from cached value or IPV6_MULTICAST_IF.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
         * IPv4:        use ip_mreq structure populated with multicast
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
         *              address and local address obtained from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
         *              IP_MULTICAST_IF. On Linux IP_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
         *              returns different structure depending on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
         *              kernel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
        if (niObj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
#if defined(__linux__) && defined(AF_INET6)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
            if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
                int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
                int len = sizeof(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
                if (isOldKernel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
                    index = (*env)->GetIntField(env, this, pdsi_multicastInterfaceID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
                    if (JVM_GetSockOpt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
                                       (char*)&index, &len) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
                        NET_ThrowCurrent(env, "getsockopt IPV6_MULTICAST_IF failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
                mname.imr_multiaddr.s_addr = htonl((*env)->GetIntField(env, iaObj, ia_addressID));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
                mname.imr_address.s_addr = 0 ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
                mname.imr_ifindex = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
                mname_len = sizeof(struct ip_mreqn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
                struct in_addr in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
                struct in_addr *inP = &in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
                int len = sizeof(struct in_addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
                struct ip_mreqn mreqn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
                if (isOldKernel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
                    inP = (struct in_addr *)&mreqn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
                    len = sizeof(struct ip_mreqn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
                if (getsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, (char *)inP, &len) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
                    NET_ThrowCurrent(env, "getsockopt IP_MULTICAST_IF failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
                mname.imr_address.s_addr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
                    (isOldKernel ? mreqn.imr_address.s_addr : in.s_addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
                mname.imr_interface.s_addr = in.s_addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
                mname.imr_multiaddr.s_addr = htonl((*env)->GetIntField(env, iaObj, ia_addressID));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
                mname_len = sizeof(struct ip_mreq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
         * Join the multicast group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
        if (JVM_SetSockOpt(fd, IPPROTO_IP, (join ? IP_ADD_MEMBERSHIP:IP_DROP_MEMBERSHIP),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
                           (char *) &mname, mname_len) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
             * If IP_ADD_MEMBERSHIP returns ENOPROTOOPT on Linux and we've got
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
             * IPv6 enabled then it's possible that the kernel has been fixed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
             * so we switch to IPV6_ADD_MEMBERSHIP socket option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
             * As of 2.4.7 kernel IPV6_ADD_MEMERSHIP can't handle IPv4-mapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
             * addresses so we have to use IP_ADD_MEMERSHIP for IPv4 multicast
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
             * groups. However if the socket is an IPv6 socket then then setsockopt
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
             * should reurn ENOPROTOOPT. We assume this will be fixed in Linux
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
             * at some stage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
#if defined(__linux__) && defined(AF_INET6)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
            if (errno == ENOPROTOOPT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
                if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
                    ipv6_join_leave = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
                    errno = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
                } else  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
                    errno = ENOPROTOOPT;    /* errno can be changed by ipv6_available */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
            if (errno) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
                if (join) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
                    NET_ThrowCurrent(env, "setsockopt IP_ADD_MEMBERSHIP failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
                    if (errno == ENOENT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
                        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
                            "Not a member of the multicast group");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
                        NET_ThrowCurrent(env, "setsockopt IP_DROP_MEMBERSHIP failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
         * If we haven't switched to IPv6 socket option then we're done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
        if (!ipv6_join_leave) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
     * IPv6 join. If it's an IPv4 multicast group then we use an IPv4-mapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
     * address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
        struct ipv6_mreq mname6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
        jbyteArray ipaddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
        jbyte caddr[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
        jint family;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
        jint address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
        family = (*env)->GetIntField(env, iaObj, ia_familyID) == IPv4? AF_INET : AF_INET6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
        if (family == AF_INET) { /* will convert to IPv4-mapped address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
            memset((char *) caddr, 0, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
            address = (*env)->GetIntField(env, iaObj, ia_addressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
            caddr[10] = 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
            caddr[11] = 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
            caddr[12] = ((address >> 24) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
            caddr[13] = ((address >> 16) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
            caddr[14] = ((address >> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
            caddr[15] = (address & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
            ipaddress = (*env)->GetObjectField(env, iaObj, ia6_ipaddressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
            (*env)->GetByteArrayRegion(env, ipaddress, 0, 16, caddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
        memcpy((void *)&(mname6.ipv6mr_multiaddr), caddr, sizeof(struct in6_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
        if (IS_NULL(niObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
            int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
            int len = sizeof(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
             * 2.2 kernel doens't support IPV6_MULTICAST_IF socket option
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
            if (isOldKernel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
                index = (*env)->GetIntField(env, this, pdsi_multicastInterfaceID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
                if (JVM_GetSockOpt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
                                 (char*)&index, &len) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
                    NET_ThrowCurrent(env, "getsockopt IPV6_MULTICAST_IF failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
             * On 2.4.8+ if we join a group with the interface set to 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
             * then the kernel records the interface it decides. This causes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
             * subsequent leave groups to fail as there is no match. Thus we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
             * pick the interface if there is a matching route.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
            if (index == 0 && !isOldKernel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
                int rt_index = getDefaultIPv6Interface(&(mname6.ipv6mr_multiaddr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
                if (rt_index > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
                    index = rt_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
            mname6.ipv6mr_interface = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
            jint idx = (*env)->GetIntField(env, niObj, ni_indexID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
            mname6.ipv6mr_interface = idx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
        /* Join the multicast group */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
        if (JVM_SetSockOpt(fd, IPPROTO_IPV6, (join ? IPV6_ADD_MEMBERSHIP : IPV6_DROP_MEMBERSHIP),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
                           (char *) &mname6, sizeof (mname6)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
            if (join) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
                NET_ThrowCurrent(env, "setsockopt IPV6_ADD_MEMBERSHIP failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
                if (errno == ENOENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
                   JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
                        "Not a member of the multicast group");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
                    NET_ThrowCurrent(env, "setsockopt IPV6_DROP_MEMBERSHIP failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
 * Method:    join
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
 * Signature: (Ljava/net/InetAddress;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
Java_java_net_PlainDatagramSocketImpl_join(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
                                           jobject iaObj, jobject niObj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
    mcast_join_leave(env, this, iaObj, niObj, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
 * Method:    leave
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
 * Signature: (Ljava/net/InetAddress;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
Java_java_net_PlainDatagramSocketImpl_leave(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
                                            jobject iaObj, jobject niObj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
    mcast_join_leave(env, this, iaObj, niObj, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
}