jdk/src/solaris/native/java/net/PlainDatagramSocketImpl.c
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8549 b924a92a7c84
child 12047 320a714614e9
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8549
diff changeset
     2
 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5200
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5200
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5200
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5200
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5200
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <errno.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <netinet/in.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <sys/types.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include <sys/socket.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include <fcntl.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#ifdef __linux__
8549
b924a92a7c84 6835668: Use of /usr/include/linux/ files creates a dependence on kernel-headers
michaelm
parents: 7668
diff changeset
    37
#include <unistd.h>
b924a92a7c84 6835668: Use of /usr/include/linux/ files creates a dependence on kernel-headers
michaelm
parents: 7668
diff changeset
    38
#include <sys/sysctl.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#include <sys/utsname.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#include <netinet/ip.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
#define IPV6_MULTICAST_IF 17
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
#ifndef SO_BSDCOMPAT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
#define SO_BSDCOMPAT  14
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
#endif
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);
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
   608
#ifdef AF_INET6
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    family = (*env)->GetIntField(env, iaObj, ia_familyID) == IPv4?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        AF_INET : AF_INET6;
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
   611
#else
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
   612
    family = AF_INET;
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
   613
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    if (family == AF_INET) { /* this api can't handle IPV6 addresses */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        int address = (*env)->GetIntField(env, iaObj, ia_addressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        (*env)->SetIntField(env, addressObj, ia_addressID, address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    return port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
Java_java_net_PlainDatagramSocketImpl_peekData(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                                           jobject packet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    char BUF[MAX_BUFFER_LEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    char *fullPacket = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    int mallocedPacket = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    jint timeout = (*env)->GetIntField(env, this, pdsi_timeoutID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    jbyteArray packetBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    jint packetBufferOffset, packetBufferLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    SOCKADDR remote_addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    if (IS_NULL(packet)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        JNU_ThrowNullPointerException(env, "packet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    packetBuffer = (*env)->GetObjectField(env, packet, dp_bufID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    if (IS_NULL(packetBuffer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        JNU_ThrowNullPointerException(env, "packet buffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    packetBufferOffset = (*env)->GetIntField(env, packet, dp_offsetID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    packetBufferLen = (*env)->GetIntField(env, packet, dp_bufLengthID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    if (timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        int ret = NET_Timeout(fd, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        if (ret == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                            "Receive timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        } else if (ret == JVM_IO_ERR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            if (errno == EBADF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "Receive failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        } else if (ret == JVM_IO_INTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                            "operation interrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    if (packetBufferLen > MAX_BUFFER_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        /* When JNI-ifying the JDK's IO routines, we turned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
         * read's and write's of byte arrays of size greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
         * than 2048 bytes into several operations of size 2048.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
         * This saves a malloc()/memcpy()/free() for big
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
         * buffers.  This is OK for file IO and TCP, but that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
         * strategy violates the semantics of a datagram protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
         * (one big send) != (several smaller sends).  So here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
         * we *must* alloc the buffer.  Note it needn't be bigger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
         * than 65,536 (0xFFFF) the max size of an IP packet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
         * anything bigger is truncated anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
         * We may want to use a smarter allocation scheme at some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
         * point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        if (packetBufferLen > MAX_PACKET_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            packetBufferLen = MAX_PACKET_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        fullPacket = (char *)malloc(packetBufferLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        if (!fullPacket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            JNU_ThrowOutOfMemoryError(env, "heap allocation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            mallocedPacket = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        fullPacket = &(BUF[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    len = SOCKADDR_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    n = NET_RecvFrom(fd, fullPacket, packetBufferLen, MSG_PEEK,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                     (struct sockaddr *)&remote_addr, &len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    /* truncate the data if the packet's length is too small */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    if (n > packetBufferLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        n = packetBufferLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    if (n == JVM_IO_ERR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        if (errno == ECONNREFUSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            int orig_errno = errno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            (void) recv(fd, fullPacket, 1, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            errno = orig_errno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        (*env)->SetIntField(env, packet, dp_offsetID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        (*env)->SetIntField(env, packet, dp_lengthID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        if (errno == ECONNREFUSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                            "ICMP Port Unreachable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            if (errno == EBADF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "Receive failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    } else if (n == JVM_IO_INTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        (*env)->SetIntField(env, packet, dp_offsetID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        (*env)->SetIntField(env, packet, dp_lengthID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                        "operation interrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
         * success - fill in received address...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
         * REMIND: Fill in an int on the packet, and create inetadd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
         * object in Java, as a performance improvement. Also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
         * construct the inetadd object lazily.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        jobject packetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
         * Check if there is an InetAddress already associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
         * packet. If so we check if it is the same source address. We
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
         * can't update any existing InetAddress because it is immutable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        packetAddress = (*env)->GetObjectField(env, packet, dp_addressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        if (packetAddress != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            if (!NET_SockaddrEqualsInetAddress(env, (struct sockaddr *)&remote_addr, packetAddress)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                /* force a new InetAddress to be created */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                packetAddress = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        if (packetAddress == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            packetAddress = NET_SockaddrToInetAddress(env, (struct sockaddr *)&remote_addr, &port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            /* stuff the new Inetaddress in the packet */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            (*env)->SetObjectField(env, packet, dp_addressID, packetAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            /* only get the new port number */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            port = NET_GetPortFromSockaddr((struct sockaddr *)&remote_addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        /* and fill in the data, remote address/port and such */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        (*env)->SetByteArrayRegion(env, packetBuffer, packetBufferOffset, n,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                                   (jbyte *)fullPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        (*env)->SetIntField(env, packet, dp_portID, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        (*env)->SetIntField(env, packet, dp_lengthID, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    if (mallocedPacket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        free(fullPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    return port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
 * Method:    receive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
 * Signature: (Ljava/net/DatagramPacket;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
Java_java_net_PlainDatagramSocketImpl_receive0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                                              jobject packet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    char BUF[MAX_BUFFER_LEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    char *fullPacket = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    int mallocedPacket = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    jint timeout = (*env)->GetIntField(env, this, pdsi_timeoutID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    jbyteArray packetBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    jint packetBufferOffset, packetBufferLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    SOCKADDR remote_addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
    jboolean retry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    jboolean connected = JNI_FALSE;
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
   819
    jobject connectedAddress = NULL;
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
   820
    jint connectedPort = 0;
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
   821
    jlong prevTime = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    if (IS_NULL(packet)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        JNU_ThrowNullPointerException(env, "packet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    packetBuffer = (*env)->GetObjectField(env, packet, dp_bufID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    if (IS_NULL(packetBuffer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        JNU_ThrowNullPointerException(env, "packet buffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    packetBufferOffset = (*env)->GetIntField(env, packet, dp_offsetID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
    packetBufferLen = (*env)->GetIntField(env, packet, dp_bufLengthID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
    if (packetBufferLen > MAX_BUFFER_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        /* When JNI-ifying the JDK's IO routines, we turned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
         * read's and write's of byte arrays of size greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
         * than 2048 bytes into several operations of size 2048.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
         * This saves a malloc()/memcpy()/free() for big
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
         * buffers.  This is OK for file IO and TCP, but that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
         * strategy violates the semantics of a datagram protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
         * (one big send) != (several smaller sends).  So here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
         * we *must* alloc the buffer.  Note it needn't be bigger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
         * than 65,536 (0xFFFF) the max size of an IP packet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
         * anything bigger is truncated anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
         * We may want to use a smarter allocation scheme at some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
         * point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        if (packetBufferLen > MAX_PACKET_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            packetBufferLen = MAX_PACKET_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        fullPacket = (char *)malloc(packetBufferLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        if (!fullPacket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
            JNU_ThrowOutOfMemoryError(env, "heap allocation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            mallocedPacket = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        fullPacket = &(BUF[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * On Linux with the 2.2 kernel we simulate connected datagrams by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * discarding packets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    if (isOldKernel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        connected = (*env)->GetBooleanField(env, this, pdsi_connected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        if (connected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
            connectedAddress = (*env)->GetObjectField(env, this, pdsi_connectedAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            connectedPort = (*env)->GetIntField(env, this, pdsi_connectedPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            if (timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                prevTime = JVM_CurrentTimeMillis(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        retry = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        if (timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            int ret = NET_Timeout(fd, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            if (ret <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                if (ret == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                    JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                                    "Receive timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                } else if (ret == JVM_IO_ERR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                    if (errno == EBADF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                         JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                     } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                         NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "Receive failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                     JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                } else if (ret == JVM_IO_INTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                    JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                                    "operation interrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                if (mallocedPacket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                    free(fullPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
         * Security Note: For Linux 2.2 with connected datagrams ensure that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
         * you receive into the stack/heap allocated buffer - do not attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
         * to receive directly into DatagramPacket's byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
         * (ie: if the virtual machine support pinning don't use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
         * GetByteArrayElements or a JNI critical section and receive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
         * directly into the byte array)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        len = SOCKADDR_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        n = NET_RecvFrom(fd, fullPacket, packetBufferLen, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                         (struct sockaddr *)&remote_addr, &len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        /* truncate the data if the packet's length is too small */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        if (n > packetBufferLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            n = packetBufferLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        if (n == JVM_IO_ERR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            (*env)->SetIntField(env, packet, dp_offsetID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            (*env)->SetIntField(env, packet, dp_lengthID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            if (errno == ECONNREFUSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                                "ICMP Port Unreachable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                if (errno == EBADF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                     JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                 } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                     NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "Receive failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        } else if (n == JVM_IO_INTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
            (*env)->SetIntField(env, packet, dp_offsetID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            (*env)->SetIntField(env, packet, dp_lengthID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                            "operation interrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
            jobject packetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
             * If we are connected then we know that the datagram that we have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
             * received is from the address that we are connected too. However
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
             * on Linux with 2.2 kernel we have to simulate this behaviour by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
             * discarding any datagrams that aren't from the connected address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            if (isOldKernel && connected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                if (NET_GetPortFromSockaddr((struct sockaddr *)&remote_addr) != connectedPort ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                    !NET_SockaddrEqualsInetAddress(env, (struct sockaddr *)&remote_addr, connectedAddress)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                     * Discard the datagram as it's not from the connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                     * address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                    retry = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                     * Adjust timeout if necessary to ensure that we adhere to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                     * timeout semantics.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                    if (timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
                        jlong newTime = JVM_CurrentTimeMillis(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
                        timeout -= (newTime - prevTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                        if (timeout <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                                    "Receive timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                            if (mallocedPacket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                                free(fullPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                        prevTime = newTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
             * success - fill in received address...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
             * REMIND: Fill in an int on the packet, and create inetadd
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
             * object in Java, as a performance improvement. Also
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
             * construct the inetadd object lazily.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
             * Check if there is an InetAddress already associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
             * packet. If so we check if it is the same source address. We
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
             * can't update any existing InetAddress because it is immutable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            packetAddress = (*env)->GetObjectField(env, packet, dp_addressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            if (packetAddress != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                if (!NET_SockaddrEqualsInetAddress(env, (struct sockaddr *)&remote_addr, packetAddress)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                    /* force a new InetAddress to be created */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                    packetAddress = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            if (packetAddress == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                packetAddress = NET_SockaddrToInetAddress(env, (struct sockaddr *)&remote_addr, &port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                /* stuff the new Inetaddress in the packet */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                (*env)->SetObjectField(env, packet, dp_addressID, packetAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                /* only get the new port number */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                port = NET_GetPortFromSockaddr((struct sockaddr *)&remote_addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            /* and fill in the data, remote address/port and such */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            (*env)->SetByteArrayRegion(env, packetBuffer, packetBufferOffset, n,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
                                       (jbyte *)fullPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            (*env)->SetIntField(env, packet, dp_portID, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            (*env)->SetIntField(env, packet, dp_lengthID, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
    } while (retry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
    if (mallocedPacket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        free(fullPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
 * Method:    datagramSocketCreate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
Java_java_net_PlainDatagramSocketImpl_datagramSocketCreate(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                                                           jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
6299
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
  1055
    int fd, t = 1;
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
  1056
#ifdef AF_INET6
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
  1057
    int domain = ipv6_available() ? AF_INET6 : AF_INET;
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
  1058
#else
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
  1059
    int domain = AF_INET;
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
  1060
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
    }
6299
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
  1067
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
  1068
    if ((fd = JVM_Socket(domain, SOCK_DGRAM, 0)) == JVM_IO_ERR) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                       "Error creating socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
6299
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
  1074
#ifdef AF_INET6
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
  1075
    /* Disable IPV6_V6ONLY to ensure dual-socket support */
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
  1076
    if (domain == AF_INET6) {
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
  1077
        int arg = 0;
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
  1078
        if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&arg,
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
  1079
                       sizeof(int)) < 0) {
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
  1080
            NET_ThrowNew(env, errno, "cannot set IPPROTO_IPV6");
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
  1081
            close(fd);
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
  1082
            return;
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
  1083
        }
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
  1084
    }
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
  1085
#endif /* AF_INET6 */
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
  1086
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (char*) &t, sizeof(int));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
    if (isOldKernel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        setsockopt(fd, SOL_SOCKET, SO_BSDCOMPAT, (char*) &t, sizeof(int));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * On Linux for IPv6 sockets we must set the hop limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     * to 1 to be compatible with default ttl of 1 for IPv4 sockets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     */
6299
a44d48dbcd50 6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents: 5506
diff changeset
  1099
    if (domain == AF_INET6) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        int ttl = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, (char *)&ttl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                   sizeof(ttl));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        if (isOldKernel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            (*env)->SetIntField(env, this, pdsi_ttlID, ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
#endif /* __linux__ */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
    (*env)->SetIntField(env, fdObj, IO_fd_fdID, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
 * Method:    datagramSocketClose
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
Java_java_net_PlainDatagramSocketImpl_datagramSocketClose(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
                                                          jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     * REMIND: PUT A LOCK AROUND THIS CODE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
    fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
    if (fd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
    (*env)->SetIntField(env, fdObj, IO_fd_fdID, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
    NET_SocketClose(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
 * Set outgoing multicast interface designated by a NetworkInterface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
 * Throw exception if failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
static void mcast_set_if_by_if_v4(JNIEnv *env, jobject this, int fd, jobject value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
    static jfieldID ni_addrsID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
    static jfieldID ia_addressID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
    struct in_addr in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
    jobjectArray addrArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
    jsize len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
    jobject addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
5200
8172fc66e135 6921111: NullPointerException in PlainDatagramSocketImpl.socketSetOption
chegar
parents: 4814
diff changeset
  1154
    if (ni_addrsID == NULL || ia_addressID == NULL) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        CHECK_NULL(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        ni_addrsID = (*env)->GetFieldID(env, c, "addrs",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
                                        "[Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        CHECK_NULL(ni_addrsID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        c = (*env)->FindClass(env,"java/net/InetAddress");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        CHECK_NULL(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        ia_addressID = (*env)->GetFieldID(env, c, "address", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
        CHECK_NULL(ia_addressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
    addrArray = (*env)->GetObjectField(env, value, ni_addrsID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
    len = (*env)->GetArrayLength(env, addrArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * Check that there is at least one address bound to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
    if (len < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
            "bad argument for IP_MULTICAST_IF2: No IP addresses bound to interface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     * We need an ipv4 address here
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
    for (i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        addr = (*env)->GetObjectArrayElement(env, addrArray, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        if ((*env)->GetIntField(env, addr, ia_familyID) == IPv4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
            in.s_addr = htonl((*env)->GetIntField(env, addr, ia_addressID));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
    if (JVM_SetSockOpt(fd, IPPROTO_IP, IP_MULTICAST_IF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
                       (const char*)&in, sizeof(in)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                       "Error setting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
 * Set outgoing multicast interface designated by a NetworkInterface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
 * Throw exception if failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
 */
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1201
#ifdef AF_INET6
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
static void mcast_set_if_by_if_v6(JNIEnv *env, jobject this, int fd, jobject value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
    static jfieldID ni_indexID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
    int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
    if (ni_indexID == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        CHECK_NULL(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        ni_indexID = (*env)->GetFieldID(env, c, "index", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        CHECK_NULL(ni_indexID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
    index = (*env)->GetIntField(env, value, ni_indexID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
    if (JVM_SetSockOpt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                       (const char*)&index, sizeof(index)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
        if (errno == EINVAL && index > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
                "IPV6_MULTICAST_IF failed (interface has IPv4 "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
                "address only?)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
                           "Error setting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     * Linux 2.2 kernel doesn't support IPV6_MULTICAST_IF socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     * option so record index for later retrival.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
    if (isOldKernel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
        (*env)->SetIntField(env, this, pdsi_multicastInterfaceID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
                            (jint)index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
}
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1238
#endif /* AF_INET6 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
 * Set outgoing multicast interface designated by an InetAddress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
 * Throw exception if failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
static void mcast_set_if_by_addr_v4(JNIEnv *env, jobject this, int fd, jobject value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
    static jfieldID ia_addressID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
    struct in_addr in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
    if (ia_addressID == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        jclass c = (*env)->FindClass(env,"java/net/InetAddress");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        CHECK_NULL(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
        ia_addressID = (*env)->GetFieldID(env, c, "address", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
        CHECK_NULL(ia_addressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
    in.s_addr = htonl( (*env)->GetIntField(env, value, ia_addressID) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
    if (JVM_SetSockOpt(fd, IPPROTO_IP, IP_MULTICAST_IF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
                       (const char*)&in, sizeof(in)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
                         "Error setting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
 * Set outgoing multicast interface designated by an InetAddress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
 * Throw exception if failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
 */
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1268
#ifdef AF_INET6
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
static void mcast_set_if_by_addr_v6(JNIEnv *env, jobject this, int fd, jobject value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
    static jclass ni_class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
    if (ni_class == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
        CHECK_NULL(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
        ni_class = (*env)->NewGlobalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
        CHECK_NULL(ni_class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
    value = Java_java_net_NetworkInterface_getByInetAddress0(env, ni_class, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
    if (value == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
        if (!(*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                 "bad argument for IP_MULTICAST_IF"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                 ": address not bound to any interface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
    mcast_set_if_by_if_v6(env, this, fd, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
}
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1290
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
 * Sets the multicast interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
 * SocketOptions.IP_MULTICAST_IF :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
 *      value is a InetAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
 *      IPv4:   set outgoing multicast interface using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
 *              IPPROTO_IP/IP_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
 *      IPv6:   Get the index of the interface to which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
 *              InetAddress is bound
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
 *              Set outgoing multicast interface using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
 *              IPPROTO_IPV6/IPV6_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
 *              On Linux 2.2 record interface index as can't
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
 *              query the multicast interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
 * SockOptions.IF_MULTICAST_IF2 :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
 *      value is a NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
 *      IPv4:   Obtain IP address bound to network interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
 *              (NetworkInterface.addres[0])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
 *              set outgoing multicast interface using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
 *              IPPROTO_IP/IP_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
 *      IPv6:   Obtain NetworkInterface.index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
 *              Set outgoing multicast interface using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
 *              IPPROTO_IPV6/IPV6_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
 *              On Linux 2.2 record interface index as can't
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
 *              query the multicast interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
static void setMulticastInterface(JNIEnv *env, jobject this, int fd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
                                  jint opt, jobject value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
    if (opt == java_net_SocketOptions_IP_MULTICAST_IF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
         * value is an InetAddress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
         */
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1326
#ifdef AF_INET6
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
        if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
            mcast_set_if_by_addr_v6(env, this, fd, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
            mcast_set_if_by_addr_v4(env, this, fd, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        mcast_set_if_by_addr_v4(env, this, fd, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
        if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
            mcast_set_if_by_addr_v6(env, this, fd, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
#endif
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1340
#else
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1341
        mcast_set_if_by_addr_v4(env, this, fd, value);
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1342
#endif  /* AF_INET6 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
    if (opt == java_net_SocketOptions_IP_MULTICAST_IF2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
         * value is a NetworkInterface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
         */
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1349
#ifdef AF_INET6
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
        if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
            mcast_set_if_by_if_v6(env, this, fd, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
            mcast_set_if_by_if_v4(env, this, fd, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        mcast_set_if_by_if_v4(env, this, fd, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
        if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
            mcast_set_if_by_if_v6(env, this, fd, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
#endif
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1363
#else
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1364
        mcast_set_if_by_if_v4(env, this, fd, value);
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1365
#endif  /* AF_INET6 */
2
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
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
 * Enable/disable local loopback of multicast datagrams.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
static void mcast_set_loop_v4(JNIEnv *env, jobject this, int fd, jobject value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
    jclass cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
    jfieldID fid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
    jboolean on;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
    char loopback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
    cls = (*env)->FindClass(env, "java/lang/Boolean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
    CHECK_NULL(cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
    fid =  (*env)->GetFieldID(env, cls, "value", "Z");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
    CHECK_NULL(fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
    on = (*env)->GetBooleanField(env, value, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
    loopback = (!on ? 1 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
    if (NET_SetSockOpt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, (const void *)&loopback, sizeof(char)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "Error setting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
 * Enable/disable local loopback of multicast datagrams.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
 */
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1395
#ifdef AF_INET6
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
static void mcast_set_loop_v6(JNIEnv *env, jobject this, int fd, jobject value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
    jclass cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
    jfieldID fid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
    jboolean on;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
    int loopback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
    cls = (*env)->FindClass(env, "java/lang/Boolean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
    CHECK_NULL(cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
    fid =  (*env)->GetFieldID(env, cls, "value", "Z");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
    CHECK_NULL(fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
    on = (*env)->GetBooleanField(env, value, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
    loopback = (!on ? 1 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
    if (NET_SetSockOpt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, (const void *)&loopback, sizeof(int)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "Error setting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
     * Can't query IPV6_MULTICAST_LOOP on Linux 2.2 kernel so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
     * store it in impl so that we can simulate getsockopt.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
    if (isOldKernel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
        (*env)->SetBooleanField(env, this, pdsi_loopbackID, on);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
}
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1425
#endif  /* AF_INET6 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
 * Sets the multicast loopback mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
static void setMulticastLoopbackMode(JNIEnv *env, jobject this, int fd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
                                  jint opt, jobject value) {
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1432
#ifdef AF_INET6
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
    if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
        mcast_set_loop_v6(env, this, fd, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
        mcast_set_loop_v4(env, this, fd, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
    mcast_set_loop_v4(env, this, fd, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
    if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
        mcast_set_loop_v6(env, this, fd, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
#endif
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1446
#else
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1447
    mcast_set_loop_v4(env, this, fd, value);
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1448
#endif  /* AF_INET6 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
 * Method:    socketSetOption
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
 * Signature: (ILjava/lang/Object;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
Java_java_net_PlainDatagramSocketImpl_socketSetOption(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
                                                      jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
                                                      jint opt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
                                                      jobject value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
    int level, optname, optlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
    union {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
        char c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
    } optval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
     * Check that socket hasn't been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
    fd = getFD(env, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
    if (fd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
     * Check argument has been provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
    if (IS_NULL(value)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
        JNU_ThrowNullPointerException(env, "value argument");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
     * Setting the multicast interface handled seperately
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
    if (opt == java_net_SocketOptions_IP_MULTICAST_IF ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
        opt == java_net_SocketOptions_IP_MULTICAST_IF2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
        setMulticastInterface(env, this, fd, opt, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
     * Setting the multicast loopback mode handled separately
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
    if (opt == java_net_SocketOptions_IP_MULTICAST_LOOP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
        setMulticastLoopbackMode(env, this, fd, opt, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
     * Map the Java level socket option to the platform specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
     * level and option name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
    if (NET_MapSocketOption(opt, &level, &optname)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Invalid option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
    switch (opt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
        case java_net_SocketOptions_SO_SNDBUF :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
        case java_net_SocketOptions_SO_RCVBUF :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
        case java_net_SocketOptions_IP_TOS :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
                jclass cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
                jfieldID fid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
                cls = (*env)->FindClass(env, "java/lang/Integer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
                CHECK_NULL(cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
                fid =  (*env)->GetFieldID(env, cls, "value", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
                CHECK_NULL(fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
                optval.i = (*env)->GetIntField(env, value, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
                optlen = sizeof(optval.i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
        case java_net_SocketOptions_SO_REUSEADDR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
        case java_net_SocketOptions_SO_BROADCAST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
                jclass cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
                jfieldID fid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
                jboolean on;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
                cls = (*env)->FindClass(env, "java/lang/Boolean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
                CHECK_NULL(cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
                fid =  (*env)->GetFieldID(env, cls, "value", "Z");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
                CHECK_NULL(fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
                on = (*env)->GetBooleanField(env, value, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
                /* SO_REUSEADDR or SO_BROADCAST */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
                optval.i = (on ? 1 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
                optlen = sizeof(optval.i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
        default :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
                "Socket option not supported by PlainDatagramSocketImp");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
    if (NET_SetSockOpt(fd, level, optname, (const void *)&optval, optlen) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "Error setting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
 * Return the multicast interface:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
 * SocketOptions.IP_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
 *      IPv4:   Query IPPROTO_IP/IP_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
 *              Create InetAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
 *              IP_MULTICAST_IF returns struct ip_mreqn on 2.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
 *              kernel but struct in_addr on 2.4 kernel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
 *      IPv6:   Query IPPROTO_IPV6 / IPV6_MULTICAST_IF or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
 *              obtain from impl is Linux 2.2 kernel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
 *              If index == 0 return InetAddress representing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
 *              anyLocalAddress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
 *              If index > 0 query NetworkInterface by index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
 *              and returns addrs[0]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
 * SocketOptions.IP_MULTICAST_IF2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
 *      IPv4:   Query IPPROTO_IP/IP_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
 *              Query NetworkInterface by IP address and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
 *              return the NetworkInterface that the address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
 *              is bound too.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
 *      IPv6:   Query IPPROTO_IPV6 / IPV6_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
 *              (except Linux .2 kernel)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
 *              Query NetworkInterface by index and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
 *              return NetworkInterface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
jobject getMulticastInterface(JNIEnv *env, jobject this, int fd, jint opt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
    jboolean isIPV4 = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
    if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
        isIPV4 = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
     * IPv4 implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
    if (isIPV4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
        static jclass inet4_class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
        static jmethodID inet4_ctrID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
        static jfieldID inet4_addrID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
        static jclass ni_class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
        static jmethodID ni_ctrID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
        static jfieldID ni_indexID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
        static jfieldID ni_addrsID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
        jobjectArray addrArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
        jobject addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
        jobject ni;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
        struct in_addr in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
        struct in_addr *inP = &in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
        int len = sizeof(struct in_addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
        struct ip_mreqn mreqn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
        if (isOldKernel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
            inP = (struct in_addr *)&mreqn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
            len = sizeof(struct ip_mreqn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
        }
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
        if (JVM_GetSockOpt(fd, IPPROTO_IP, IP_MULTICAST_IF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
                           (char *)inP, &len) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
                             "Error getting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
            return NULL;
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
         * Construct and populate an Inet4Address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
        if (inet4_class == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
            jclass c = (*env)->FindClass(env, "java/net/Inet4Address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
            CHECK_NULL_RETURN(c, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
            inet4_ctrID = (*env)->GetMethodID(env, c, "<init>", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
            CHECK_NULL_RETURN(inet4_ctrID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
            inet4_addrID = (*env)->GetFieldID(env, c, "address", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
            CHECK_NULL_RETURN(inet4_addrID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
            inet4_class = (*env)->NewGlobalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
            CHECK_NULL_RETURN(inet4_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
        addr = (*env)->NewObject(env, inet4_class, inet4_ctrID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
        CHECK_NULL_RETURN(addr, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
        (*env)->SetIntField(env, addr, inet4_addrID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
                (isOldKernel ? ntohl(mreqn.imr_address.s_addr) : ntohl(in.s_addr)) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
        (*env)->SetIntField(env, addr, inet4_addrID, ntohl(in.s_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
         * For IP_MULTICAST_IF return InetAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
        if (opt == java_net_SocketOptions_IP_MULTICAST_IF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
            return addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
         * For IP_MULTICAST_IF2 we get the NetworkInterface for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
         * this address and return it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
        if (ni_class == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
            jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
            CHECK_NULL_RETURN(c, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
            ni_ctrID = (*env)->GetMethodID(env, c, "<init>", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
            CHECK_NULL_RETURN(ni_ctrID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
            ni_indexID = (*env)->GetFieldID(env, c, "index", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
            CHECK_NULL_RETURN(ni_indexID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
            ni_addrsID = (*env)->GetFieldID(env, c, "addrs",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
                                            "[Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
            CHECK_NULL_RETURN(ni_addrsID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
            ni_class = (*env)->NewGlobalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
            CHECK_NULL_RETURN(ni_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
        ni = Java_java_net_NetworkInterface_getByInetAddress0(env, ni_class, addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
        if (ni) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
            return ni;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
         * The address doesn't appear to be bound at any known
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
         * NetworkInterface. Therefore we construct a NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
         * with this address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
        ni = (*env)->NewObject(env, ni_class, ni_ctrID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
        CHECK_NULL_RETURN(ni, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
        (*env)->SetIntField(env, ni, ni_indexID, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
        addrArray = (*env)->NewObjectArray(env, 1, inet4_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
        CHECK_NULL_RETURN(addrArray, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
        (*env)->SetObjectArrayElement(env, addrArray, 0, addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
        (*env)->SetObjectField(env, ni, ni_addrsID, addrArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
        return ni;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
     * IPv6 implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
    if ((opt == java_net_SocketOptions_IP_MULTICAST_IF) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
        (opt == java_net_SocketOptions_IP_MULTICAST_IF2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
        static jclass ni_class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
        static jmethodID ni_ctrID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
        static jfieldID ni_indexID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
        static jfieldID ni_addrsID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
        static jclass ia_class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
        static jmethodID ia_anyLocalAddressID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
        int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
        int len = sizeof(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
        jobjectArray addrArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
        jobject addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
        jobject ni;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
         * Linux 2.2 kernel doesn't support IPV6_MULTICAST_IF socke option
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
         * so use cached index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
        if (isOldKernel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
            index = (*env)->GetIntField(env, this, pdsi_multicastInterfaceID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
            if (JVM_GetSockOpt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
                               (char*)&index, &len) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
                NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
                               "Error getting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
                return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
        if (ni_class == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
            jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
            CHECK_NULL_RETURN(c, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
            ni_ctrID = (*env)->GetMethodID(env, c, "<init>", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
            CHECK_NULL_RETURN(ni_ctrID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
            ni_indexID = (*env)->GetFieldID(env, c, "index", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
            CHECK_NULL_RETURN(ni_indexID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
            ni_addrsID = (*env)->GetFieldID(env, c, "addrs",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
                                            "[Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
            CHECK_NULL_RETURN(ni_addrsID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
            ia_class = (*env)->FindClass(env, "java/net/InetAddress");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
            CHECK_NULL_RETURN(ia_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
            ia_class = (*env)->NewGlobalRef(env, ia_class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
            CHECK_NULL_RETURN(ia_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
            ia_anyLocalAddressID = (*env)->GetStaticMethodID(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
                                                             ia_class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
                                                             "anyLocalAddress",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
                                                             "()Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
            CHECK_NULL_RETURN(ia_anyLocalAddressID, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
            ni_class = (*env)->NewGlobalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
            CHECK_NULL_RETURN(ni_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
         * If multicast to a specific interface then return the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
         * interface (for IF2) or the any address on that interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
         * (for IF).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
        if (index > 0) {
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 910
diff changeset
  1774
            ni = Java_java_net_NetworkInterface_getByIndex0(env, ni_class,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
                                                                   index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
            if (ni == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
                char errmsg[255];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
                sprintf(errmsg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
                        "IPV6_MULTICAST_IF returned index to unrecognized interface: %d",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
                        index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
                JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", errmsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
                return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
             * For IP_MULTICAST_IF2 return the NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
            if (opt == java_net_SocketOptions_IP_MULTICAST_IF2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
                return ni;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
             * For IP_MULTICAST_IF return addrs[0]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
            addrArray = (*env)->GetObjectField(env, ni, ni_addrsID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
            if ((*env)->GetArrayLength(env, addrArray) < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
                JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
                    "IPV6_MULTICAST_IF returned interface without IP bindings");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
                return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
            addr = (*env)->GetObjectArrayElement(env, addrArray, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
            return addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
         * Multicast to any address - return anyLocalAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
         * or a NetworkInterface with addrs[0] set to anyLocalAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
        addr = (*env)->CallStaticObjectMethod(env, ia_class, ia_anyLocalAddressID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
                                              NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
        if (opt == java_net_SocketOptions_IP_MULTICAST_IF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
            return addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
        ni = (*env)->NewObject(env, ni_class, ni_ctrID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
        CHECK_NULL_RETURN(ni, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
        (*env)->SetIntField(env, ni, ni_indexID, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
        addrArray = (*env)->NewObjectArray(env, 1, ia_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
        CHECK_NULL_RETURN(addrArray, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
        (*env)->SetObjectArrayElement(env, addrArray, 0, addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
        (*env)->SetObjectField(env, ni, ni_addrsID, addrArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
        return ni;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
 * Returns relevant info as a jint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
 * Method:    socketGetOption
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
 * Signature: (I)Ljava/lang/Object;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
JNIEXPORT jobject JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
Java_java_net_PlainDatagramSocketImpl_socketGetOption(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
                                                      jint opt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
    int level, optname, optlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
    union {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
        char c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
    } optval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
    fd = getFD(env, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
    if (fd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
                        "socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
     * Handle IP_MULTICAST_IF seperately
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
    if (opt == java_net_SocketOptions_IP_MULTICAST_IF ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
        opt == java_net_SocketOptions_IP_MULTICAST_IF2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
        return getMulticastInterface(env, this, fd, opt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
     * SO_BINDADDR implemented using getsockname
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
    if (opt == java_net_SocketOptions_SO_BINDADDR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
        /* find out local IP address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
        SOCKADDR him;
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1871
        socklen_t len = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
        int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
        jobject iaObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
        len = SOCKADDR_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
        if (getsockname(fd, (struct sockaddr *)&him, &len) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
                           "Error getting socket name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
        iaObj = NET_SockaddrToInetAddress(env, (struct sockaddr *)&him, &port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
        return iaObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
     * Map the Java level socket option to the platform specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
     * level and option name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
    if (NET_MapSocketOption(opt, &level, &optname)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Invalid option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
     * IP_MULTICAST_LOOP socket option isn't available on Linux 2.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
     * kernel with IPv6 so return value stored in impl.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
#if defined(AF_INET6) && defined(__linux__)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
    if (isOldKernel && opt == java_net_SocketOptions_IP_MULTICAST_LOOP &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
        level == IPPROTO_IPV6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
        int mode = (int)(*env)->GetBooleanField(env, this, pdsi_loopbackID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
        return createBoolean(env, mode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
    if (opt == java_net_SocketOptions_IP_MULTICAST_LOOP &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
        level == IPPROTO_IP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
        optlen = sizeof(optval.c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
        optlen = sizeof(optval.i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
    if (NET_GetSockOpt(fd, level, optname, (void *)&optval, &optlen) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
                         "Error getting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
    switch (opt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
        case java_net_SocketOptions_IP_MULTICAST_LOOP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
            /* getLoopbackMode() returns true if IP_MULTICAST_LOOP disabled */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
            if (level == IPPROTO_IP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
                return createBoolean(env, (int)!optval.c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
                return createBoolean(env, !optval.i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
        case java_net_SocketOptions_SO_BROADCAST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
        case java_net_SocketOptions_SO_REUSEADDR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
            return createBoolean(env, optval.i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
        case java_net_SocketOptions_SO_SNDBUF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
        case java_net_SocketOptions_SO_RCVBUF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
        case java_net_SocketOptions_IP_TOS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
            return createInteger(env, optval.i);
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
    /* should never rearch here */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
 * Multicast-related calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
Java_java_net_PlainDatagramSocketImpl_setTTL(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
                                             jbyte ttl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
    jint ittl = ttl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
    if (ittl < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
        ittl += 0x100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
    Java_java_net_PlainDatagramSocketImpl_setTimeToLive(env, this, ittl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
 * Set TTL for a socket. Throw exception if failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
static void setTTL(JNIEnv *env, int fd, jint ttl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
    char ittl = (char)ttl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
    if (JVM_SetSockOpt(fd, IPPROTO_IP, IP_MULTICAST_TTL, (char*)&ittl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
                       sizeof(ittl)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
                       "Error setting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
 * Set hops limit for a socket. Throw exception if failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
 */
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1974
#ifdef AF_INET6
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
static void setHopLimit(JNIEnv *env, int fd, jint ttl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
    int ittl = (int)ttl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
    if (JVM_SetSockOpt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
                       (char*)&ittl, sizeof(ittl)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
                       "Error setting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
}
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  1983
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
 * Method:    setTTL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
 * Signature: (B)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
Java_java_net_PlainDatagramSocketImpl_setTimeToLive(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
                                                    jint ttl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
    /* it is important to cast this to a char, otherwise setsockopt gets confused */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
    /* setsockopt to be correct ttl */
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  2006
#ifdef AF_INET6
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
    if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
        setHopLimit(env, fd, ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
        setTTL(env, fd, ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
    setTTL(env, fd, ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
    if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
        setHopLimit(env, fd, ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
        if (isOldKernel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
            (*env)->SetIntField(env, this, pdsi_ttlID, ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
    }
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  2022
#endif  // __linux__
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  2023
#else
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  2024
    setTTL(env, fd, ttl);
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
  2025
#endif  /* AF_INET6 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
 * Method:    getTTL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
 * Signature: ()B
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
JNIEXPORT jbyte JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
Java_java_net_PlainDatagramSocketImpl_getTTL(JNIEnv *env, jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
    return (jbyte)Java_java_net_PlainDatagramSocketImpl_getTimeToLive(env, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
 * Method:    getTTL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
 * Signature: ()B
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
Java_java_net_PlainDatagramSocketImpl_getTimeToLive(JNIEnv *env, jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
    jint fd = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
    /* getsockopt of ttl */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
    if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
        int ttl = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
        int len = sizeof(ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
         * Linux 2.2 kernel doesn't support IPV6_MULTICAST_HOPS socket option
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
        if (isOldKernel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
            return (*env)->GetIntField(env, this, pdsi_ttlID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
        if (JVM_GetSockOpt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
                               (char*)&ttl, &len) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
                NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
                               "Error getting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
        return (jint)ttl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
    } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
#endif /* AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
            u_char ttl = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
            int len = sizeof(ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
            if (JVM_GetSockOpt(fd, IPPROTO_IP, IP_MULTICAST_TTL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
                               (char*)&ttl, &len) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
                NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
                               "Error getting socket option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
            return (jint)ttl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
 * mcast_join_leave: Join or leave a multicast group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
 * For IPv4 sockets use IP_ADD_MEMBERSHIP/IP_DROP_MEMBERSHIP socket option
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
 * to join/leave multicast group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
 * For IPv6 sockets use IPV6_ADD_MEMBERSHIP/IPV6_DROP_MEMBERSHIP socket option
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
 * to join/leave multicast group. If multicast group is an IPv4 address then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
 * an IPv4-mapped address is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
 * On Linux with IPv6 if we wish to join/leave an IPv4 multicast group then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
 * we must use the IPv4 socket options. This is because the IPv6 socket options
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
 * 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
  2108
 * kernel releases. In the future it's possible that IP_ADD_MEMBERSHIP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
 * will be updated to return ENOPROTOOPT if uses with an IPv6 socket (Solaris
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
 * already does this). Thus to cater for this we first try with the IPv4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
 * socket options and if they fail we use the IPv6 socket options. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
 * seems a reasonable failsafe solution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
static void mcast_join_leave(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
                             jobject iaObj, jobject niObj,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
                             jboolean join) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
    jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
    jint fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
    jint ipv6_join_leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
    if (IS_NULL(iaObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
        JNU_ThrowNullPointerException(env, "iaObj");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
     * Determine if this is an IPv4 or IPv6 join/leave.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
    ipv6_join_leave = ipv6_available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
    if ((*env)->GetIntField(env, iaObj, ia_familyID) == IPv4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
        ipv6_join_leave = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
     * IPv6 not compiled in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
    ipv6_join_leave = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
     * For IPv4 join use IP_ADD_MEMBERSHIP/IP_DROP_MEMBERSHIP socket option
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
     * On Linux if IPv4 or IPv6 use IP_ADD_MEMBERSHIP/IP_DROP_MEMBERSHIP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
    if (!ipv6_join_leave) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
        struct ip_mreqn mname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
        struct ip_mreq mname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
        int mname_len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
         * joinGroup(InetAddress, NetworkInterface) implementation :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
         * Linux/IPv6:  use ip_mreqn structure populated with multicast
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
         *              address and interface index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
         * IPv4:        use ip_mreq structure populated with multicast
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
         *              address and first address obtained from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
         *              NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
        if (niObj != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
#if defined(__linux__) && defined(AF_INET6)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
            if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
                static jfieldID ni_indexID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
                if (ni_indexID == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
                    jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
                    CHECK_NULL(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
                    ni_indexID = (*env)->GetFieldID(env, c, "index", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
                    CHECK_NULL(ni_indexID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
                mname.imr_multiaddr.s_addr = htonl((*env)->GetIntField(env, iaObj, ia_addressID));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
                mname.imr_address.s_addr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
                mname.imr_ifindex =  (*env)->GetIntField(env, niObj, ni_indexID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
                mname_len = sizeof(struct ip_mreqn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
                jobjectArray addrArray = (*env)->GetObjectField(env, niObj, ni_addrsID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
                jobject addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
                if ((*env)->GetArrayLength(env, addrArray) < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
                    JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
                        "bad argument for IP_ADD_MEMBERSHIP: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
                        "No IP addresses bound to interface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
                addr = (*env)->GetObjectArrayElement(env, addrArray, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
                mname.imr_multiaddr.s_addr = htonl((*env)->GetIntField(env, iaObj, ia_addressID));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
                mname.imr_address.s_addr = htonl((*env)->GetIntField(env, addr, ia_addressID));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
                mname.imr_interface.s_addr = htonl((*env)->GetIntField(env, addr, ia_addressID));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
                mname_len = sizeof(struct ip_mreq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
         * joinGroup(InetAddress) implementation :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
         * Linux/IPv6:  use ip_mreqn structure populated with multicast
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
         *              address and interface index. index obtained
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
         *              from cached value or IPV6_MULTICAST_IF.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
         * IPv4:        use ip_mreq structure populated with multicast
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
         *              address and local address obtained from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
         *              IP_MULTICAST_IF. On Linux IP_MULTICAST_IF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
         *              returns different structure depending on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
         *              kernel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
        if (niObj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
#if defined(__linux__) && defined(AF_INET6)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
            if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
                int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
                int len = sizeof(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
                if (isOldKernel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
                    index = (*env)->GetIntField(env, this, pdsi_multicastInterfaceID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
                    if (JVM_GetSockOpt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
                                       (char*)&index, &len) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
                        NET_ThrowCurrent(env, "getsockopt IPV6_MULTICAST_IF failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
                        return;
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
                mname.imr_multiaddr.s_addr = htonl((*env)->GetIntField(env, iaObj, ia_addressID));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
                mname.imr_address.s_addr = 0 ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
                mname.imr_ifindex = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
                mname_len = sizeof(struct ip_mreqn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
                struct in_addr in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
                struct in_addr *inP = &in;
6886
d818a2dfb100 6989690: java/net native code compiler warnings
chegar
parents: 6299
diff changeset
  2258
                socklen_t len = sizeof(struct in_addr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
                struct ip_mreqn mreqn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
                if (isOldKernel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
                    inP = (struct in_addr *)&mreqn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
                    len = sizeof(struct ip_mreqn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
                if (getsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, (char *)inP, &len) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
                    NET_ThrowCurrent(env, "getsockopt IP_MULTICAST_IF failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
                mname.imr_address.s_addr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
                    (isOldKernel ? mreqn.imr_address.s_addr : in.s_addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
                mname.imr_interface.s_addr = in.s_addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
                mname.imr_multiaddr.s_addr = htonl((*env)->GetIntField(env, iaObj, ia_addressID));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
                mname_len = sizeof(struct ip_mreq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
         * Join the multicast group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
        if (JVM_SetSockOpt(fd, IPPROTO_IP, (join ? IP_ADD_MEMBERSHIP:IP_DROP_MEMBERSHIP),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
                           (char *) &mname, mname_len) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
             * If IP_ADD_MEMBERSHIP returns ENOPROTOOPT on Linux and we've got
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
             * IPv6 enabled then it's possible that the kernel has been fixed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
             * so we switch to IPV6_ADD_MEMBERSHIP socket option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
             * As of 2.4.7 kernel IPV6_ADD_MEMERSHIP can't handle IPv4-mapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
             * addresses so we have to use IP_ADD_MEMERSHIP for IPv4 multicast
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
             * groups. However if the socket is an IPv6 socket then then setsockopt
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
             * should reurn ENOPROTOOPT. We assume this will be fixed in Linux
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
             * at some stage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
#if defined(__linux__) && defined(AF_INET6)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
            if (errno == ENOPROTOOPT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
                if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
                    ipv6_join_leave = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
                    errno = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
                } else  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
                    errno = ENOPROTOOPT;    /* errno can be changed by ipv6_available */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
            if (errno) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
                if (join) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
                    NET_ThrowCurrent(env, "setsockopt IP_ADD_MEMBERSHIP failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
                    if (errno == ENOENT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
                        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
                            "Not a member of the multicast group");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
                        NET_ThrowCurrent(env, "setsockopt IP_DROP_MEMBERSHIP failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
         * If we haven't switched to IPv6 socket option then we're done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
        if (!ipv6_join_leave) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
     * IPv6 join. If it's an IPv4 multicast group then we use an IPv4-mapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
     * address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
        struct ipv6_mreq mname6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
        jbyteArray ipaddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
        jbyte caddr[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
        jint family;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
        jint address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
        family = (*env)->GetIntField(env, iaObj, ia_familyID) == IPv4? AF_INET : AF_INET6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
        if (family == AF_INET) { /* will convert to IPv4-mapped address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
            memset((char *) caddr, 0, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
            address = (*env)->GetIntField(env, iaObj, ia_addressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
            caddr[10] = 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
            caddr[11] = 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
            caddr[12] = ((address >> 24) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
            caddr[13] = ((address >> 16) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
            caddr[14] = ((address >> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
            caddr[15] = (address & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
            ipaddress = (*env)->GetObjectField(env, iaObj, ia6_ipaddressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
            (*env)->GetByteArrayRegion(env, ipaddress, 0, 16, caddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
        memcpy((void *)&(mname6.ipv6mr_multiaddr), caddr, sizeof(struct in6_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
        if (IS_NULL(niObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
            int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
            int len = sizeof(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
             * 2.2 kernel doens't support IPV6_MULTICAST_IF socket option
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
            if (isOldKernel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
                index = (*env)->GetIntField(env, this, pdsi_multicastInterfaceID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
                if (JVM_GetSockOpt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
                                 (char*)&index, &len) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
                    NET_ThrowCurrent(env, "getsockopt IPV6_MULTICAST_IF failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
                    return;
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
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
             * On 2.4.8+ if we join a group with the interface set to 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
             * then the kernel records the interface it decides. This causes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
             * subsequent leave groups to fail as there is no match. Thus we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
             * pick the interface if there is a matching route.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
            if (index == 0 && !isOldKernel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
                int rt_index = getDefaultIPv6Interface(&(mname6.ipv6mr_multiaddr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
                if (rt_index > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
                    index = rt_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
            mname6.ipv6mr_interface = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
            jint idx = (*env)->GetIntField(env, niObj, ni_indexID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
            mname6.ipv6mr_interface = idx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
        /* Join the multicast group */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
        if (JVM_SetSockOpt(fd, IPPROTO_IPV6, (join ? IPV6_ADD_MEMBERSHIP : IPV6_DROP_MEMBERSHIP),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
                           (char *) &mname6, sizeof (mname6)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
            if (join) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
                NET_ThrowCurrent(env, "setsockopt IPV6_ADD_MEMBERSHIP failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
                if (errno == ENOENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
                   JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
                        "Not a member of the multicast group");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
                    NET_ThrowCurrent(env, "setsockopt IPV6_DROP_MEMBERSHIP failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
 * Method:    join
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
 * Signature: (Ljava/net/InetAddress;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
Java_java_net_PlainDatagramSocketImpl_join(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
                                           jobject iaObj, jobject niObj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
    mcast_join_leave(env, this, iaObj, niObj, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
 * Class:     java_net_PlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
 * Method:    leave
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
 * Signature: (Ljava/net/InetAddress;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
Java_java_net_PlainDatagramSocketImpl_leave(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
                                            jobject iaObj, jobject niObj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
    mcast_join_leave(env, this, iaObj, niObj, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
}