jdk/src/java.base/unix/native/libnet/net_util_md.c
author vtewari
Tue, 13 Sep 2016 17:00:06 +0530
changeset 40937 f72e61ec734b
parent 38563 3ab5f00bd5a3
child 41380 c27cf95dd7e6
permissions -rw-r--r--
8075484: SocketInputStream.socketRead0 can hang even with soTimeout set Reviewed-by: chegar, dsamersoff, msheppar, clanger
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
     2
 * Copyright (c) 1997, 2016, 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: 4814
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: 4814
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: 4814
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4814
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4814
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 <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <sys/types.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include <sys/socket.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <netinet/tcp.h>        /* Defines TCP_NODELAY, needed for 2.6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include <netinet/in.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include <net/if.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include <netdb.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include <dlfcn.h>
40937
f72e61ec734b 8075484: SocketInputStream.socketRead0 can hang even with soTimeout set
vtewari
parents: 38563
diff changeset
    36
#include <sys/time.h>
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
    37
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
    38
#ifndef _ALLBSD_SOURCE
7289
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
    39
#include <values.h>
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
    40
#else
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
    41
#include <limits.h>
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
    42
#include <sys/param.h>
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
    43
#include <sys/sysctl.h>
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
    44
#include <sys/ioctl.h>
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
    45
#ifndef MAXINT
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
    46
#define MAXINT INT_MAX
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
    47
#endif
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
    48
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
#ifdef __solaris__
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
    51
#include <sys/filio.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
#include <sys/sockio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
#include <stropts.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
#include <inet/nd.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
#ifdef __linux__
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
    58
#include <sys/ioctl.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
#include <arpa/inet.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
#include <net/route.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
#include <sys/utsname.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
#ifndef IPV6_FLOWINFO_SEND
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
#define IPV6_FLOWINFO_SEND      33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
23028
0554f6183d11 8035876: AIX build issues after '8034174: Remove use of JVM_* functions from java.net code'
simonis
parents: 23015
diff changeset
    69
#ifdef _AIX
0554f6183d11 8035876: AIX build issues after '8034174: Remove use of JVM_* functions from java.net code'
simonis
parents: 23015
diff changeset
    70
#include <sys/ioctl.h>
0554f6183d11 8035876: AIX build issues after '8034174: Remove use of JVM_* functions from java.net code'
simonis
parents: 23015
diff changeset
    71
#endif
0554f6183d11 8035876: AIX build issues after '8034174: Remove use of JVM_* functions from java.net code'
simonis
parents: 23015
diff changeset
    72
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
#include "net_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
#include "java_net_SocketOptions.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
/*
13043
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
    80
 * EXCLBIND socket options only on Solaris
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
#if defined(__solaris__) && !defined(TCP_EXCLBIND)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
#define TCP_EXCLBIND            0x21
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
#if defined(__solaris__) && !defined(UDP_EXCLBIND)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
#define UDP_EXCLBIND            0x0101
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
    89
void setDefaultScopeID(JNIEnv *env, struct sockaddr *him)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
    90
{
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
    91
#ifdef MACOSX
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
    92
    static jclass ni_class = NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
    93
    static jfieldID ni_defaultIndexID;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
    94
    if (ni_class == NULL) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
    95
        jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
    96
        CHECK_NULL(c);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
    97
        c = (*env)->NewGlobalRef(env, c);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
    98
        CHECK_NULL(c);
38563
3ab5f00bd5a3 8157811: Additional minor fixes and cleanups in Networking native code
chegar
parents: 38459
diff changeset
    99
        ni_defaultIndexID = (*env)->GetStaticFieldID(env, c, "defaultIndex", "I");
3ab5f00bd5a3 8157811: Additional minor fixes and cleanups in Networking native code
chegar
parents: 38459
diff changeset
   100
        CHECK_NULL(ni_defaultIndexID);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   101
        ni_class = c;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   102
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   103
    int defaultIndex;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   104
    struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)him;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   105
    if (sin6->sin6_family == AF_INET6 && (sin6->sin6_scope_id == 0)) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   106
        defaultIndex = (*env)->GetStaticIntField(env, ni_class,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   107
                                                 ni_defaultIndexID);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   108
        sin6->sin6_scope_id = defaultIndex;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   109
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   110
#endif
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   111
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   112
12551
5be0a162ed89 7144274: [macosx] Default IPv6 multicast interface is not being set when calling MulticastSocket.joinGroup()
khazra
parents: 12047
diff changeset
   113
int getDefaultScopeID(JNIEnv *env) {
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   114
    int defaultIndex = 0;
12551
5be0a162ed89 7144274: [macosx] Default IPv6 multicast interface is not being set when calling MulticastSocket.joinGroup()
khazra
parents: 12047
diff changeset
   115
    static jclass ni_class = NULL;
5be0a162ed89 7144274: [macosx] Default IPv6 multicast interface is not being set when calling MulticastSocket.joinGroup()
khazra
parents: 12047
diff changeset
   116
    static jfieldID ni_defaultIndexID;
5be0a162ed89 7144274: [macosx] Default IPv6 multicast interface is not being set when calling MulticastSocket.joinGroup()
khazra
parents: 12047
diff changeset
   117
    if (ni_class == NULL) {
5be0a162ed89 7144274: [macosx] Default IPv6 multicast interface is not being set when calling MulticastSocket.joinGroup()
khazra
parents: 12047
diff changeset
   118
        jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
12856
712335896cc4 7171591: getDefaultScopeID() in src/solaris/native/java/net/net_util_md.c should return a value
khazra
parents: 12688
diff changeset
   119
        CHECK_NULL_RETURN(c, 0);
12551
5be0a162ed89 7144274: [macosx] Default IPv6 multicast interface is not being set when calling MulticastSocket.joinGroup()
khazra
parents: 12047
diff changeset
   120
        c = (*env)->NewGlobalRef(env, c);
12856
712335896cc4 7171591: getDefaultScopeID() in src/solaris/native/java/net/net_util_md.c should return a value
khazra
parents: 12688
diff changeset
   121
        CHECK_NULL_RETURN(c, 0);
38563
3ab5f00bd5a3 8157811: Additional minor fixes and cleanups in Networking native code
chegar
parents: 38459
diff changeset
   122
        ni_defaultIndexID = (*env)->GetStaticFieldID(env, c, "defaultIndex", "I");
3ab5f00bd5a3 8157811: Additional minor fixes and cleanups in Networking native code
chegar
parents: 38459
diff changeset
   123
        CHECK_NULL_RETURN(ni_defaultIndexID, 0);
12551
5be0a162ed89 7144274: [macosx] Default IPv6 multicast interface is not being set when calling MulticastSocket.joinGroup()
khazra
parents: 12047
diff changeset
   124
        ni_class = c;
5be0a162ed89 7144274: [macosx] Default IPv6 multicast interface is not being set when calling MulticastSocket.joinGroup()
khazra
parents: 12047
diff changeset
   125
    }
5be0a162ed89 7144274: [macosx] Default IPv6 multicast interface is not being set when calling MulticastSocket.joinGroup()
khazra
parents: 12047
diff changeset
   126
    defaultIndex = (*env)->GetStaticIntField(env, ni_class,
5be0a162ed89 7144274: [macosx] Default IPv6 multicast interface is not being set when calling MulticastSocket.joinGroup()
khazra
parents: 12047
diff changeset
   127
                                             ni_defaultIndexID);
5be0a162ed89 7144274: [macosx] Default IPv6 multicast interface is not being set when calling MulticastSocket.joinGroup()
khazra
parents: 12047
diff changeset
   128
    return defaultIndex;
5be0a162ed89 7144274: [macosx] Default IPv6 multicast interface is not being set when calling MulticastSocket.joinGroup()
khazra
parents: 12047
diff changeset
   129
}
5be0a162ed89 7144274: [macosx] Default IPv6 multicast interface is not being set when calling MulticastSocket.joinGroup()
khazra
parents: 12047
diff changeset
   130
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   131
#define RESTARTABLE(_cmd, _result) do { \
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   132
    do { \
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   133
        _result = _cmd; \
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   134
    } while((_result == -1) && (errno == EINTR)); \
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   135
} while(0)
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   136
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   137
int NET_SocketAvailable(int s, jint *pbytes) {
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   138
    int result;
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   139
    RESTARTABLE(ioctl(s, FIONREAD, pbytes), result);
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   140
    // note: ioctl can return 0 when successful, NET_SocketAvailable
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   141
    // is expected to return 0 on failure and 1 on success.
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   142
    return (result == -1) ? 0 : 1;
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   143
}
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   144
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
#ifdef __solaris__
7289
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   146
static int init_tcp_max_buf, init_udp_max_buf;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
static int tcp_max_buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
static int udp_max_buf;
13043
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
   149
static int useExclBind = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * Get the specified parameter from the specified driver. The value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * of the parameter is assumed to be an 'int'. If the parameter
7289
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   154
 * cannot be obtained return -1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 */
22341
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents: 20821
diff changeset
   156
int net_getParam(char *driver, char *param)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    struct strioctl stri;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    char buf [64];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    int s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    int value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    s = open (driver, O_RDWR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    if (s < 0) {
7289
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   165
        return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    strncpy (buf, param, sizeof(buf));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    stri.ic_cmd = ND_GET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    stri.ic_timout = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    stri.ic_dp = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    stri.ic_len = sizeof(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    if (ioctl (s, I_STR, &stri) < 0) {
7289
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   173
        value = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        value = atoi(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    close (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
}
7289
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   180
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   181
/*
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   182
 * Iterative way to find the max value that SO_SNDBUF or SO_RCVBUF
22341
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents: 20821
diff changeset
   183
 * for Solaris versions that do not support the ioctl() in net_getParam().
7289
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   184
 * Ugly, but only called once (for each sotype).
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   185
 *
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13048
diff changeset
   186
 * As an optimization, we make a guess using the default values for Solaris
7289
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   187
 * assuming they haven't been modified with ndd.
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   188
 */
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   189
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   190
#define MAX_TCP_GUESS 1024 * 1024
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   191
#define MAX_UDP_GUESS 2 * 1024 * 1024
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   192
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   193
#define FAIL_IF_NOT_ENOBUFS if (errno != ENOBUFS) return -1
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   194
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   195
static int findMaxBuf(int fd, int opt, int sotype) {
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   196
    int a = 0;
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   197
    int b = MAXINT;
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   198
    int initial_guess;
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   199
    int limit = -1;
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   200
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   201
    if (sotype == SOCK_DGRAM) {
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   202
        initial_guess = MAX_UDP_GUESS;
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   203
    } else {
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   204
        initial_guess = MAX_TCP_GUESS;
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   205
    }
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   206
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   207
    if (setsockopt(fd, SOL_SOCKET, opt, &initial_guess, sizeof(int)) == 0) {
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   208
        initial_guess++;
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   209
        if (setsockopt(fd, SOL_SOCKET, opt, &initial_guess,sizeof(int)) < 0) {
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   210
            FAIL_IF_NOT_ENOBUFS;
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   211
            return initial_guess - 1;
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   212
        }
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   213
        a = initial_guess;
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   214
    } else {
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   215
        FAIL_IF_NOT_ENOBUFS;
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   216
        b = initial_guess - 1;
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   217
    }
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   218
    do {
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   219
        int mid = a + (b-a)/2;
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   220
        if (setsockopt(fd, SOL_SOCKET, opt, &mid, sizeof(int)) == 0) {
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   221
            limit = mid;
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   222
            a = mid + 1;
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   223
        } else {
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   224
            FAIL_IF_NOT_ENOBUFS;
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   225
            b = mid - 1;
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   226
        }
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   227
    } while (b >= a);
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   228
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   229
    return limit;
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
   230
}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
static int vinit = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
static int kernelV24 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
static int vinit24 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
int kernelIsV24 () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    if (!vinit24) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        struct utsname sysinfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        if (uname(&sysinfo) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            sysinfo.release[3] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            if (strcmp(sysinfo.release, "2.4") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                kernelV24 = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        vinit24 = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    return kernelV24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
int getScopeID (struct sockaddr *him) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    struct sockaddr_in6 *hext = (struct sockaddr_in6 *)him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    return hext->sin6_scope_id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
int cmpScopeID (unsigned int scope, struct sockaddr *him) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    struct sockaddr_in6 *hext = (struct sockaddr_in6 *)him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    return hext->sin6_scope_id == scope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
int getScopeID (struct sockaddr *him) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    struct sockaddr_in6 *him6 = (struct sockaddr_in6 *)him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    return him6->sin6_scope_id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
int cmpScopeID (unsigned int scope, struct sockaddr *him) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    struct sockaddr_in6 *him6 = (struct sockaddr_in6 *)him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    return him6->sin6_scope_id == scope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
NET_ThrowByNameWithLastError(JNIEnv *env, const char *name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                   const char *defaultDetail) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    char errmsg[255];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    sprintf(errmsg, "errno: %d, error: %s\n", errno, defaultDetail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    JNU_ThrowByNameWithLastError(env, name, errmsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
NET_ThrowCurrent(JNIEnv *env, char *msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    NET_ThrowNew(env, errno, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
NET_ThrowNew(JNIEnv *env, int errorNumber, char *msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    char fullMsg[512];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    if (!msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        msg = "no further information";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    switch(errorNumber) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    case EBADF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        jio_snprintf(fullMsg, sizeof(fullMsg), "socket closed: %s", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", fullMsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    case EINTR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        errno = errorNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        JNU_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
jfieldID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
NET_GetFileDescriptorID(JNIEnv *env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    jclass cls = (*env)->FindClass(env, "java/io/FileDescriptor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    CHECK_NULL_RETURN(cls, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    return (*env)->GetFieldID(env, cls, "fd", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   320
#if defined(DONT_ENABLE_IPV6)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   321
jint  IPv6_supported()
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   322
{
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   323
    return JNI_FALSE;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   324
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   325
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   326
#else /* !DONT_ENABLE_IPV6 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   327
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
jint  IPv6_supported()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
#ifndef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    void *ipv6_fn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    SOCKADDR sa;
910
1f53246fb014 6729881: Compiler warning in networking native code
chegar
parents: 2
diff changeset
   338
    socklen_t sa_len = sizeof(sa);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   340
    fd = socket(AF_INET6, SOCK_STREAM, 0) ;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    if (fd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
         *  TODO: We really cant tell since it may be an unrelated error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
         *  for now we will assume that AF_INET6 is not available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * If fd 0 is a socket it means we've been launched from inetd or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * xinetd. If it's a socket then check the family - if it's an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * IPv4 socket then we need to disable IPv6.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    if (getsockname(0, (struct sockaddr *)&sa, &sa_len) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        struct sockaddr *saP = (struct sockaddr *)&sa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        if (saP->sa_family != AF_INET6) {
38459
6cf90640c38c 8048518: File Descriptor Leak in src/java.base/unix/native/libnet/net_util_md.c
chegar
parents: 36115
diff changeset
   357
            close(fd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * Linux - check if any interface has an IPv6 address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * Don't need to parse the line - we just need an indication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        FILE *fP = fopen("/proc/net/if_inet6", "r");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        char buf[255];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        char *bufP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        if (fP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        bufP = fgets(buf, sizeof(buf), fP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        fclose(fP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        if (bufP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * On Solaris 8 it's possible to create INET6 sockets even
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * though IPv6 is not enabled on all interfaces. Thus we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * query the number of IPv6 addresses to verify that IPv6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * has been configured on at least one interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * On Linux it doesn't matter - if IPv6 is built-in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * kernel then IPv6 addresses will be bound automatically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * to all interfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
#ifdef SIOCGLIFNUM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        struct lifnum numifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        numifs.lifn_family = AF_INET6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        numifs.lifn_flags = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        if (ioctl(fd, SIOCGLIFNUM, (char *)&numifs) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
             * SIOCGLIFNUM failed - assume IPv6 not configured
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
         * If no IPv6 addresses then return false. If count > 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
         * it's possible that all IPv6 addresses are "down" but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
         * that's okay as they may be brought "up" while the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
         * VM is running.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        if (numifs.lifn_count == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    /* SIOCGLIFNUM not defined in build environment ??? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
#endif /* __solaris */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     *  OK we may have the stack available in the kernel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *  we should also check if the APIs are available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    ipv6_fn = JVM_FindLibraryEntry(RTLD_DEFAULT, "inet_pton");
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   434
    close(fd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    if (ipv6_fn == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        return JNI_FALSE;
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   437
    } else {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   438
        return JNI_TRUE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    }
4814
dbf72872f8d2 6905552: libnet/nio portability issues
chegar
parents: 1247
diff changeset
   440
#endif /* AF_INET6 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
}
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   442
#endif /* DONT_ENABLE_IPV6 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
   444
jint reuseport_supported()
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
   445
{
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
   446
    /* Do a simple dummy call, and try to figure out from that */
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
   447
    int one = 1;
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
   448
    int rv, s;
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
   449
    s = socket(PF_INET, SOCK_STREAM, 0);
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
   450
    if (s < 0) {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
   451
        return JNI_FALSE;
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
   452
    }
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
   453
    rv = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, (void *)&one, sizeof(one));
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
   454
    if (rv != 0) {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
   455
        rv = JNI_FALSE;
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
   456
    } else {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
   457
        rv = JNI_TRUE;
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
   458
    }
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
   459
    close(s);
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
   460
    return rv;
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
   461
}
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
   462
23033
0cc7c83fde3c 8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents: 23028
diff changeset
   463
void NET_ThrowUnknownHostExceptionWithGaiError(JNIEnv *env,
0cc7c83fde3c 8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents: 23028
diff changeset
   464
                                               const char* hostname,
0cc7c83fde3c 8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents: 23028
diff changeset
   465
                                               int gai_error)
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   466
{
6696
7201f23dae0d 6989190: SO_SNDBUF/SO_RCVBUF limits should only be checked when setsockopt fails (sol)
alanb
parents: 6670
diff changeset
   467
    int size;
7201f23dae0d 6989190: SO_SNDBUF/SO_RCVBUF limits should only be checked when setsockopt fails (sol)
alanb
parents: 6670
diff changeset
   468
    char *buf;
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   469
    const char *format = "%s: %s";
23033
0cc7c83fde3c 8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents: 23028
diff changeset
   470
    const char *error_string = gai_strerror(gai_error);
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   471
    if (error_string == NULL)
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   472
        error_string = "unknown error";
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   473
6696
7201f23dae0d 6989190: SO_SNDBUF/SO_RCVBUF limits should only be checked when setsockopt fails (sol)
alanb
parents: 6670
diff changeset
   474
    size = strlen(format) + strlen(hostname) + strlen(error_string) + 2;
7201f23dae0d 6989190: SO_SNDBUF/SO_RCVBUF limits should only be checked when setsockopt fails (sol)
alanb
parents: 6670
diff changeset
   475
    buf = (char *) malloc(size);
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   476
    if (buf) {
6696
7201f23dae0d 6989190: SO_SNDBUF/SO_RCVBUF limits should only be checked when setsockopt fails (sol)
alanb
parents: 6670
diff changeset
   477
        jstring s;
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   478
        sprintf(buf, format, hostname, error_string);
6696
7201f23dae0d 6989190: SO_SNDBUF/SO_RCVBUF limits should only be checked when setsockopt fails (sol)
alanb
parents: 6670
diff changeset
   479
        s = JNU_NewStringPlatform(env, buf);
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   480
        if (s != NULL) {
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   481
            jobject x = JNU_NewObjectByName(env,
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   482
                                            "java/net/UnknownHostException",
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   483
                                            "(Ljava/lang/String;)V", s);
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   484
            if (x != NULL)
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   485
                (*env)->Throw(env, x);
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   486
        }
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   487
        free(buf);
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   488
    }
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   489
}
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   490
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
NET_AllocSockaddr(struct sockaddr **him, int *len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        struct sockaddr_in6 *him6 = (struct sockaddr_in6*)malloc(sizeof(struct sockaddr_in6));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        *him = (struct sockaddr*)him6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        *len = sizeof(struct sockaddr_in6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
#endif /* AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            struct sockaddr_in *him4 = (struct sockaddr_in*)malloc(sizeof(struct sockaddr_in));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            *him = (struct sockaddr*)him4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            *len = sizeof(struct sockaddr_in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
#if defined(__linux__) && defined(AF_INET6)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
/* following code creates a list of addresses from the kernel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
 * routing table that are routed via the loopback address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
 * We check all destination addresses against this table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
 * and override the scope_id field to use the relevant value for "lo"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
 * in order to work-around the Linux bug that prevents packets destined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
 * for certain local addresses from being sent via a physical interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
struct loopback_route {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    struct in6_addr addr; /* destination address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    int plen; /* prefix length */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
static struct loopback_route *loRoutes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
static int nRoutes = 0; /* number of routes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
static int loRoutes_size = 16; /* initial size */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
static int lo_scope_id = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
static void initLoopbackRoutes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
void printAddr (struct in6_addr *addr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    for (i=0; i<16; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        printf ("%02x", addr->s6_addr[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    printf ("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
static jboolean needsLoopbackRoute (struct in6_addr* dest_addr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    int byte_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    int extra_bits, i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    struct loopback_route *ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    if (loRoutes == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        initLoopbackRoutes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    for (ptr = loRoutes, i=0; i<nRoutes; i++, ptr++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        struct in6_addr *target_addr=&ptr->addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        int dest_plen = ptr->plen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        byte_count = dest_plen >> 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        extra_bits = dest_plen & 0x3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        if (byte_count > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            if (memcmp(target_addr, dest_addr, byte_count)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                continue;  /* no match */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        if (extra_bits > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            unsigned char c1 = ((unsigned char *)target_addr)[byte_count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            unsigned char c2 = ((unsigned char *)&dest_addr)[byte_count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            unsigned char mask = 0xff << (8 - extra_bits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            if ((c1 & mask) != (c2 & mask)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
static void initLoopbackRoutes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    FILE *f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    char srcp[8][5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    char hopp[8][5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    int dest_plen, src_plen, use, refcnt, metric;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    unsigned long flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    char dest_str[40];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    struct in6_addr dest_addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    char device[16];
14320
d5be8d19b3ec 8000203: File descriptor leak in src/solaris/native/java/net/net_util_md.c
chegar
parents: 13999
diff changeset
   582
    struct loopback_route *loRoutesTemp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    if (loRoutes != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        free (loRoutes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    loRoutes = calloc (loRoutes_size, sizeof(struct loopback_route));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    if (loRoutes == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * Scan /proc/net/ipv6_route looking for a matching
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * route.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    if ((f = fopen("/proc/net/ipv6_route", "r")) == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        return ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                     "%4s%4s%4s%4s%4s%4s%4s%4s %02x "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                     "%4s%4s%4s%4s%4s%4s%4s%4s "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                     "%08x %08x %08x %08lx %8s",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                     dest_str, &dest_str[5], &dest_str[10], &dest_str[15],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                     &dest_str[20], &dest_str[25], &dest_str[30], &dest_str[35],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                     &dest_plen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                     srcp[0], srcp[1], srcp[2], srcp[3],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                     srcp[4], srcp[5], srcp[6], srcp[7],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                     &src_plen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                     hopp[0], hopp[1], hopp[2], hopp[3],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                     hopp[4], hopp[5], hopp[6], hopp[7],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                     &metric, &use, &refcnt, &flags, device) == 31) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
         * Some routes should be ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        if ( (dest_plen < 0 || dest_plen > 128)  ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
             (src_plen != 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
             (flags & (RTF_POLICY | RTF_FLOW)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
             ((flags & RTF_REJECT) && dest_plen == 0) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
         * Convert the destination address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        dest_str[4] = ':';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        dest_str[9] = ':';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        dest_str[14] = ':';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        dest_str[19] = ':';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        dest_str[24] = ':';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        dest_str[29] = ':';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        dest_str[34] = ':';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        dest_str[39] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        if (inet_pton(AF_INET6, dest_str, &dest_addr) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            /* not an Ipv6 address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        if (strcmp(device, "lo") != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            /* Not a loopback route */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            if (nRoutes == loRoutes_size) {
14320
d5be8d19b3ec 8000203: File descriptor leak in src/solaris/native/java/net/net_util_md.c
chegar
parents: 13999
diff changeset
   643
                loRoutesTemp = realloc (loRoutes, loRoutes_size *
d5be8d19b3ec 8000203: File descriptor leak in src/solaris/native/java/net/net_util_md.c
chegar
parents: 13999
diff changeset
   644
                                        sizeof (struct loopback_route) * 2);
d5be8d19b3ec 8000203: File descriptor leak in src/solaris/native/java/net/net_util_md.c
chegar
parents: 13999
diff changeset
   645
d5be8d19b3ec 8000203: File descriptor leak in src/solaris/native/java/net/net_util_md.c
chegar
parents: 13999
diff changeset
   646
                if (loRoutesTemp == 0) {
d5be8d19b3ec 8000203: File descriptor leak in src/solaris/native/java/net/net_util_md.c
chegar
parents: 13999
diff changeset
   647
                    free(loRoutes);
d5be8d19b3ec 8000203: File descriptor leak in src/solaris/native/java/net/net_util_md.c
chegar
parents: 13999
diff changeset
   648
                    fclose (f);
d5be8d19b3ec 8000203: File descriptor leak in src/solaris/native/java/net/net_util_md.c
chegar
parents: 13999
diff changeset
   649
                    return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                }
14320
d5be8d19b3ec 8000203: File descriptor leak in src/solaris/native/java/net/net_util_md.c
chegar
parents: 13999
diff changeset
   651
                loRoutes=loRoutesTemp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                loRoutes_size *= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            memcpy (&loRoutes[nRoutes].addr,&dest_addr,sizeof(struct in6_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            loRoutes[nRoutes].plen = dest_plen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            nRoutes ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    fclose (f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        /* now find the scope_id for "lo" */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
8554
14e626e9407b 7022269: clean up fscanf usage in Linux networking native code
chegar
parents: 7668
diff changeset
   664
        char devname[21];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        char addr6p[8][5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        int plen, scope, dad_status, if_idx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        if ((f = fopen("/proc/net/if_inet6", "r")) != NULL) {
11514
0cefabc1c81f 7123415: Some cases of network interface indexes being read incorrectly
chegar
parents: 11032
diff changeset
   669
            while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %08x %02x %02x %02x %20s\n",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                      addr6p[0], addr6p[1], addr6p[2], addr6p[3],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                      addr6p[4], addr6p[5], addr6p[6], addr6p[7],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                  &if_idx, &plen, &scope, &dad_status, devname) == 13) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                if (strcmp(devname, "lo") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                     * Found - so just return the index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                    fclose(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                    lo_scope_id = if_idx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            fclose(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
 * Following is used for binding to local addresses. Equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
 * to code above, for bind().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
struct localinterface {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    char localaddr [16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
static struct localinterface *localifs = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
static int localifsSize = 0;    /* size of array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
static int nifs = 0;            /* number of entries used in array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
/* not thread safe: make sure called once from one thread */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
static void initLocalIfs () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    FILE *f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    unsigned char staddr [16];
8554
14e626e9407b 7022269: clean up fscanf usage in Linux networking native code
chegar
parents: 7668
diff changeset
   707
    char ifname [33];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    struct localinterface *lif=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    int index, x1, x2, x3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    unsigned int u0,u1,u2,u3,u4,u5,u6,u7,u8,u9,ua,ub,uc,ud,ue,uf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    if ((f = fopen("/proc/net/if_inet6", "r")) == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        return ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    while (fscanf (f, "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x "
8554
14e626e9407b 7022269: clean up fscanf usage in Linux networking native code
chegar
parents: 7668
diff changeset
   716
                "%d %x %x %x %32s",&u0,&u1,&u2,&u3,&u4,&u5,&u6,&u7,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                &u8,&u9,&ua,&ub,&uc,&ud,&ue,&uf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                &index, &x1, &x2, &x3, ifname) == 21) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        staddr[0] = (unsigned char)u0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        staddr[1] = (unsigned char)u1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        staddr[2] = (unsigned char)u2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        staddr[3] = (unsigned char)u3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        staddr[4] = (unsigned char)u4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        staddr[5] = (unsigned char)u5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        staddr[6] = (unsigned char)u6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        staddr[7] = (unsigned char)u7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        staddr[8] = (unsigned char)u8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        staddr[9] = (unsigned char)u9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        staddr[10] = (unsigned char)ua;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        staddr[11] = (unsigned char)ub;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        staddr[12] = (unsigned char)uc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        staddr[13] = (unsigned char)ud;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        staddr[14] = (unsigned char)ue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        staddr[15] = (unsigned char)uf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        nifs ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        if (nifs > localifsSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            localifs = (struct localinterface *) realloc (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                        localifs, sizeof (struct localinterface)* (localifsSize+5));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            if (localifs == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                nifs = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                fclose (f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            lif = localifs + localifsSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            localifsSize += 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            lif ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        memcpy (lif->localaddr, staddr, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        lif->index = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    fclose (f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
/* return the scope_id (interface index) of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
 * interface corresponding to the given address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
 * returns 0 if no match found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
static int getLocalScopeID (char *addr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    struct localinterface *lif;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    if (localifs == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        initLocalIfs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    for (i=0, lif=localifs; i<nifs; i++, lif++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        if (memcmp (addr, lif->localaddr, 16) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            return lif->index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
   774
void platformInit () {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    initLoopbackRoutes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    initLocalIfs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
   779
#elif defined(_AIX)
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
   780
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
   781
/* Initialize stubs for blocking I/O workarounds (see src/solaris/native/java/net/linux_close.c) */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
   782
extern void aix_close_init();
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
   783
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
   784
void platformInit () {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
   785
    aix_close_init();
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
   786
}
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
   787
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
   790
void platformInit () {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
13043
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
   794
void parseExclusiveBindProperty(JNIEnv *env) {
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
   795
#ifdef __solaris__
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
   796
    jstring s, flagSet;
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
   797
    jclass iCls;
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
   798
    jmethodID mid;
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
   799
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
   800
    s = (*env)->NewStringUTF(env, "sun.net.useExclusiveBind");
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
   801
    CHECK_NULL(s);
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
   802
    iCls = (*env)->FindClass(env, "java/lang/System");
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
   803
    CHECK_NULL(iCls);
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
   804
    mid = (*env)->GetStaticMethodID(env, iCls, "getProperty",
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
   805
                "(Ljava/lang/String;)Ljava/lang/String;");
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
   806
    CHECK_NULL(mid);
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
   807
    flagSet = (*env)->CallStaticObjectMethod(env, iCls, mid, s);
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
   808
    if (flagSet != NULL) {
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
   809
        useExclBind = 1;
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
   810
    }
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
   811
#endif
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
   812
}
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
   813
27178
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 25859
diff changeset
   814
JNIEXPORT jint JNICALL
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 25859
diff changeset
   815
NET_EnableFastTcpLoopback(int fd) {
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 25859
diff changeset
   816
    return 0;
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 25859
diff changeset
   817
}
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 25859
diff changeset
   818
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
/* In the case of an IPv4 Inetaddress this method will return an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
 * IPv4 mapped address where IPv6 is available and v4MappedAddress is TRUE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
 * Otherwise it will return a sockaddr_in structure for an IPv4 InetAddress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
JNIEXPORT int JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
NET_InetAddressToSockaddr(JNIEnv *env, jobject iaObj, int port, struct sockaddr *him,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                          int *len, jboolean v4MappedAddress) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    jint family;
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14320
diff changeset
   827
    family = getInetAddress_family(env, iaObj);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    /* needs work. 1. family 2. clean up him6 etc deallocate memory */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    if (ipv6_available() && !(family == IPv4 && v4MappedAddress == JNI_FALSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        struct sockaddr_in6 *him6 = (struct sockaddr_in6 *)him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        jbyte caddr[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        jint address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        if (family == IPv4) { /* will convert to IPv4-mapped address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            memset((char *) caddr, 0, 16);
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14320
diff changeset
   838
            address = getInetAddress_addr(env, iaObj);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            if (address == INADDR_ANY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                /* we would always prefer IPv6 wildcard address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                   caddr[10] = 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                   caddr[11] = 0xff; */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                caddr[10] = 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                caddr[11] = 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                caddr[12] = ((address >> 24) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                caddr[13] = ((address >> 16) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                caddr[14] = ((address >> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                caddr[15] = (address & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        } else {
20821
e0d0a585aa49 8015743: Address internet addresses
michaelm
parents: 16870
diff changeset
   852
            getInet6Address_ipaddress(env, iaObj, (char *)caddr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        memset((char *)him6, 0, sizeof(struct sockaddr_in6));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        him6->sin6_port = htons(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        memcpy((void *)&(him6->sin6_addr), caddr, sizeof(struct in6_addr) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        him6->sin6_family = AF_INET6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        *len = sizeof(struct sockaddr_in6) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   860
#if defined(_ALLBSD_SOURCE) && defined(_AF_INET6)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   861
// XXXBSD: should we do something with scope id here ? see below linux comment
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   862
/* MMM: Come back to this! */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   863
#endif
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   864
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
         * On Linux if we are connecting to a link-local address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
         * we need to specify the interface in the scope_id (2.4 kernel only)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
         * If the scope was cached the we use the cached value. If not cached but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
         * specified in the Inet6Address we use that, but we first check if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
         * address needs to be routed via the loopback interface. In this case,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
         * we override the specified value with that of the loopback interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
         * If no cached value exists and no value was specified by user, then
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13048
diff changeset
   874
         * we try to determine a value from the routing table. In all these
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
         * cases the used value is cached for further use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        if (IN6_IS_ADDR_LINKLOCAL(&(him6->sin6_addr))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            int cached_scope_id = 0, scope_id = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13048
diff changeset
   881
            if (ia6_cachedscopeidID) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                cached_scope_id = (int)(*env)->GetIntField(env, iaObj, ia6_cachedscopeidID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                /* if cached value exists then use it. Otherwise, check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                 * if scope is set in the address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                if (!cached_scope_id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                    if (ia6_scopeidID) {
20821
e0d0a585aa49 8015743: Address internet addresses
michaelm
parents: 16870
diff changeset
   888
                        scope_id = getInet6Address_scopeid(env, iaObj);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                    if (scope_id != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                        /* check user-specified value for loopback case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                         * that needs to be overridden
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                        if (kernelIsV24() && needsLoopbackRoute (&him6->sin6_addr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                            cached_scope_id = lo_scope_id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                            (*env)->SetIntField(env, iaObj, ia6_cachedscopeidID, cached_scope_id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                         * Otherwise consult the IPv6 routing tables to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                         * try determine the appropriate interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                        if (kernelIsV24()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                            cached_scope_id = getDefaultIPv6Interface( &(him6->sin6_addr) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                            cached_scope_id = getLocalScopeID( (char *)&(him6->sin6_addr) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                            if (cached_scope_id == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                                cached_scope_id = getDefaultIPv6Interface( &(him6->sin6_addr) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                        (*env)->SetIntField(env, iaObj, ia6_cachedscopeidID, cached_scope_id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
             * If we have a scope_id use the extended form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
             * of sockaddr_in6.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13048
diff changeset
   921
            struct sockaddr_in6 *him6 =
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13048
diff changeset
   922
                    (struct sockaddr_in6 *)him;
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13048
diff changeset
   923
            him6->sin6_scope_id = cached_scope_id != 0 ?
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13048
diff changeset
   924
                                        cached_scope_id    : scope_id;
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13048
diff changeset
   925
            *len = sizeof(struct sockaddr_in6);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        /* handle scope_id for solaris */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        if (family != IPv4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            if (ia6_scopeidID) {
20821
e0d0a585aa49 8015743: Address internet addresses
michaelm
parents: 16870
diff changeset
   932
                him6->sin6_scope_id = getInet6Address_scopeid(env, iaObj);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
#endif /* AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            struct sockaddr_in *him4 = (struct sockaddr_in*)him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
            jint address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
            if (family == IPv6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
              JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Protocol family unavailable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
              return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            memset((char *) him4, 0, sizeof(struct sockaddr_in));
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14320
diff changeset
   946
            address = getInetAddress_addr(env, iaObj);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            him4->sin_port = htons((short) port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            him4->sin_addr.s_addr = (uint32_t) htonl(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            him4->sin_family = AF_INET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            *len = sizeof(struct sockaddr_in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
NET_SetTrafficClass(struct sockaddr *him, int trafficClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
    if (him->sa_family == AF_INET6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        struct sockaddr_in6 *him6 = (struct sockaddr_in6 *)him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        him6->sin6_flowinfo = htonl((trafficClass & 0xff) << 20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
#endif /* AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 911
diff changeset
   965
JNIEXPORT jint JNICALL
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
NET_GetPortFromSockaddr(struct sockaddr *him) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
    if (him->sa_family == AF_INET6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        return ntohs(((struct sockaddr_in6*)him)->sin6_port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
#endif /* AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                return ntohs(((struct sockaddr_in*)him)->sin_port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
NET_IsIPv4Mapped(jbyte* caddr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
    for (i = 0; i < 10; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        if (caddr[i] != 0x00) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            return 0; /* false */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
    if (((caddr[10] & 0xff) == 0xff) && ((caddr[11] & 0xff) == 0xff)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        return 1; /* true */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
    return 0; /* false */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
NET_IPv4MappedToIPv4(jbyte* caddr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    return ((caddr[12] & 0xff) << 24) | ((caddr[13] & 0xff) << 16) | ((caddr[14] & 0xff) << 8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        | (caddr[15] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
NET_IsEqual(jbyte* caddr1, jbyte* caddr2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
    for (i = 0; i < 16; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        if (caddr1[i] != caddr2[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            return 0; /* false */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
12688
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12551
diff changeset
  1010
int NET_IsZeroAddr(jbyte* caddr) {
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12551
diff changeset
  1011
    int i;
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12551
diff changeset
  1012
    for (i = 0; i < 16; i++) {
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12551
diff changeset
  1013
        if (caddr[i] != 0) {
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12551
diff changeset
  1014
            return 0;
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12551
diff changeset
  1015
        }
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12551
diff changeset
  1016
    }
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12551
diff changeset
  1017
    return 1;
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12551
diff changeset
  1018
}
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12551
diff changeset
  1019
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
 * Map the Java level socket option to the platform specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
 * level and option name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
NET_MapSocketOption(jint cmd, int *level, int *optname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
    static struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        jint cmd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        int level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        int optname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
    } const opts[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        { java_net_SocketOptions_TCP_NODELAY,           IPPROTO_TCP,    TCP_NODELAY },
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        { java_net_SocketOptions_SO_OOBINLINE,          SOL_SOCKET,     SO_OOBINLINE },
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        { java_net_SocketOptions_SO_LINGER,             SOL_SOCKET,     SO_LINGER },
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        { java_net_SocketOptions_SO_SNDBUF,             SOL_SOCKET,     SO_SNDBUF },
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        { java_net_SocketOptions_SO_RCVBUF,             SOL_SOCKET,     SO_RCVBUF },
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        { java_net_SocketOptions_SO_KEEPALIVE,          SOL_SOCKET,     SO_KEEPALIVE },
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        { java_net_SocketOptions_SO_REUSEADDR,          SOL_SOCKET,     SO_REUSEADDR },
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
  1038
        { java_net_SocketOptions_SO_REUSEPORT,          SOL_SOCKET,     SO_REUSEPORT },
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        { java_net_SocketOptions_SO_BROADCAST,          SOL_SOCKET,     SO_BROADCAST },
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        { java_net_SocketOptions_IP_TOS,                IPPROTO_IP,     IP_TOS },
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        { java_net_SocketOptions_IP_MULTICAST_IF,       IPPROTO_IP,     IP_MULTICAST_IF },
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        { java_net_SocketOptions_IP_MULTICAST_IF2,      IPPROTO_IP,     IP_MULTICAST_IF },
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        { java_net_SocketOptions_IP_MULTICAST_LOOP,     IPPROTO_IP,     IP_MULTICAST_LOOP },
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        switch (cmd) {
30963
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 28059
diff changeset
  1051
            // Different multicast options if IPv6 is enabled
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            case java_net_SocketOptions_IP_MULTICAST_IF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            case java_net_SocketOptions_IP_MULTICAST_IF2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                *level = IPPROTO_IPV6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                *optname = IPV6_MULTICAST_IF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
            case java_net_SocketOptions_IP_MULTICAST_LOOP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                *level = IPPROTO_IPV6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                *optname = IPV6_MULTICAST_LOOP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                return 0;
30963
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 28059
diff changeset
  1062
#if (defined(__solaris__) || defined(MACOSX))
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 28059
diff changeset
  1063
            // Map IP_TOS request to IPV6_TCLASS
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 28059
diff changeset
  1064
            case java_net_SocketOptions_IP_TOS:
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 28059
diff changeset
  1065
                *level = IPPROTO_IPV6;
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 28059
diff changeset
  1066
                *optname = IPV6_TCLASS;
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 28059
diff changeset
  1067
                return 0;
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 28059
diff changeset
  1068
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * Map the Java level option to the native level
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
    for (i=0; i<(int)(sizeof(opts) / sizeof(opts[0])); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        if (cmd == opts[i].cmd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
            *level = opts[i].level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
            *optname = opts[i].optname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
    /* not found */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
 * Determine the default interface for an IPv6 address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
 * 1. Scans /proc/net/ipv6_route for a matching route
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
 *    (eg: fe80::/10 or a route for the specific address).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
 *    This will tell us the interface to use (eg: "eth0").
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
 * 2. Lookup /proc/net/if_inet6 to map the interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
 *    name to an interface index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
 * Returns :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
 *      -1 if error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
 *       0 if no matching interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
 *      >1 interface index to use for the link-local address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
#if defined(__linux__) && defined(AF_INET6)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
int getDefaultIPv6Interface(struct in6_addr *target_addr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
    FILE *f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
    char srcp[8][5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
    char hopp[8][5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
    int dest_plen, src_plen, use, refcnt, metric;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
    unsigned long flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
    char dest_str[40];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
    struct in6_addr dest_addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
    char device[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
    jboolean match = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     * Scan /proc/net/ipv6_route looking for a matching
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     * route.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
    if ((f = fopen("/proc/net/ipv6_route", "r")) == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
    while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                     "%4s%4s%4s%4s%4s%4s%4s%4s %02x "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
                     "%4s%4s%4s%4s%4s%4s%4s%4s "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
                     "%08x %08x %08x %08lx %8s",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                     dest_str, &dest_str[5], &dest_str[10], &dest_str[15],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                     &dest_str[20], &dest_str[25], &dest_str[30], &dest_str[35],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
                     &dest_plen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
                     srcp[0], srcp[1], srcp[2], srcp[3],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
                     srcp[4], srcp[5], srcp[6], srcp[7],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                     &src_plen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                     hopp[0], hopp[1], hopp[2], hopp[3],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                     hopp[4], hopp[5], hopp[6], hopp[7],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
                     &metric, &use, &refcnt, &flags, device) == 31) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
         * Some routes should be ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        if ( (dest_plen < 0 || dest_plen > 128)  ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
             (src_plen != 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
             (flags & (RTF_POLICY | RTF_FLOW)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
             ((flags & RTF_REJECT) && dest_plen == 0) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
         * Convert the destination address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        dest_str[4] = ':';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        dest_str[9] = ':';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        dest_str[14] = ':';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        dest_str[19] = ':';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        dest_str[24] = ':';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        dest_str[29] = ':';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        dest_str[34] = ':';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        dest_str[39] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        if (inet_pton(AF_INET6, dest_str, &dest_addr) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
            /* not an Ipv6 address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
             * The prefix len (dest_plen) indicates the number of bits we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
             * need to match on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
             * dest_plen / 8    => number of bytes to match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
             * dest_plen % 8    => number of additional bits to match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
             * eg: fe80::/10 => match 1 byte + 2 additional bits in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
             *                  the next byte.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
            int byte_count = dest_plen >> 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
            int extra_bits = dest_plen & 0x3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
            if (byte_count > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
                if (memcmp(target_addr, &dest_addr, byte_count)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
                    continue;  /* no match */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
            if (extra_bits > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
                unsigned char c1 = ((unsigned char *)target_addr)[byte_count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
                unsigned char c2 = ((unsigned char *)&dest_addr)[byte_count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
                unsigned char mask = 0xff << (8 - extra_bits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
                if ((c1 & mask) != (c2 & mask)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
                    continue;
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
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
             * We have a match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
            match = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
    fclose(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     * If there's a match then we lookup the interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     * index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
    if (match) {
8554
14e626e9407b 7022269: clean up fscanf usage in Linux networking native code
chegar
parents: 7668
diff changeset
  1204
        char devname[21];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
        char addr6p[8][5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        int plen, scope, dad_status, if_idx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        if ((f = fopen("/proc/net/if_inet6", "r")) != NULL) {
11514
0cefabc1c81f 7123415: Some cases of network interface indexes being read incorrectly
chegar
parents: 11032
diff changeset
  1209
            while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %08x %02x %02x %02x %20s\n",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                      addr6p[0], addr6p[1], addr6p[2], addr6p[3],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                      addr6p[4], addr6p[5], addr6p[6], addr6p[7],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                  &if_idx, &plen, &scope, &dad_status, devname) == 13) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                if (strcmp(devname, device) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
                     * Found - so just return the index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
                    fclose(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
                    return if_idx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
            fclose(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
             * Couldn't open /proc/net/if_inet6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     * If we get here it means we didn't there wasn't any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     * route or we couldn't get the index of the interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
 * Wrapper for getsockopt system routine - does any necessary
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13048
diff changeset
  1242
 * pre/post processing to deal with OS specific oddities :-
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
 * On Linux the SO_SNDBUF/SO_RCVBUF values must be post-processed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
 * to compensate for an incorrect value returned by the kernel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
NET_GetSockOpt(int fd, int level, int opt, void *result,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
               int *len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
    int rv;
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1252
    socklen_t socklen = *len;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1254
    rv = getsockopt(fd, level, opt, result, &socklen);
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1255
    *len = socklen;
911
9edd1b9607e2 6730740: Fix for 6729881 has apparently broken several 64 bit tests: "Bad address"
chegar
parents: 910
diff changeset
  1256
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
    if (rv < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     * On Linux SO_SNDBUF/SO_RCVBUF aren't symmetric. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     * stems from additional socket structures in the send
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     * and receive buffers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
    if ((level == SOL_SOCKET) && ((opt == SO_SNDBUF)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
                                  || (opt == SO_RCVBUF))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
        int n = *((int *)result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        n /= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
        *((int *)result) = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1275
/* Workaround for Mac OS treating linger value as
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1276
 *  signed integer
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1277
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1278
#ifdef MACOSX
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1279
    if (level == SOL_SOCKET && opt == SO_LINGER) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1280
        struct linger* to_cast = (struct linger*)result;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1281
        to_cast->l_linger = (unsigned short)to_cast->l_linger;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1282
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1283
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
 * Wrapper for setsockopt system routine - performs any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
 * necessary pre/post processing to deal with OS specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
 * issue :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
 * On Solaris need to limit the suggested value for SO_SNDBUF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
 * and SO_RCVBUF to the kernel configured limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
 * For IP_TOS socket option need to mask off bits as this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
 * aren't automatically masked by the kernel and results in
30963
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 28059
diff changeset
  1297
 * an error.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
NET_SetSockOpt(int fd, int level, int  opt, const void *arg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
               int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
{
22605
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 22597
diff changeset
  1303
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
#ifndef IPTOS_TOS_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
#define IPTOS_TOS_MASK 0x1e
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
#ifndef IPTOS_PREC_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
#define IPTOS_PREC_MASK 0xe0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1311
#if defined(_ALLBSD_SOURCE)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1312
#if defined(KIPC_MAXSOCKBUF)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1313
    int mib[3];
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1314
    size_t rlen;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1315
#endif
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1316
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1317
    int *bufsize;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1318
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1319
#ifdef __APPLE__
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1320
    static int maxsockbuf = -1;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1321
#else
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1322
    static long maxsockbuf = -1;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1323
#endif
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1324
#endif
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1325
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     * IPPROTO/IP_TOS :-
30963
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 28059
diff changeset
  1328
     * 1. IPv6 on Solaris/Mac OS:
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 28059
diff changeset
  1329
     *    Set the TOS OR Traffic Class value to cater for
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 28059
diff changeset
  1330
     *    IPv6 and IPv4 scenarios.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     * 2. IPv6 on Linux: By default Linux ignores flowinfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
     *    field so enable IPV6_FLOWINFO_SEND so that flowinfo
25170
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 23033
diff changeset
  1333
     *    will be examined. We also set the IPv4 TOS option in this case.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
     * 3. IPv4: set socket option based on ToS and Precedence
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     *    fields (otherwise get invalid argument)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
    if (level == IPPROTO_IP && opt == IP_TOS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
        int *iptos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
#if defined(AF_INET6) && defined(__linux__)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
        if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
            int optval = 1;
25170
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 23033
diff changeset
  1343
            if (setsockopt(fd, IPPROTO_IPV6, IPV6_FLOWINFO_SEND,
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 23033
diff changeset
  1344
                           (void *)&optval, sizeof(optval)) < 0) {
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 23033
diff changeset
  1345
                return -1;
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 23033
diff changeset
  1346
            }
30963
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 28059
diff changeset
  1347
           /*
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 28059
diff changeset
  1348
            * Let's also set the IPV6_TCLASS flag.
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 28059
diff changeset
  1349
            * Linux appears to allow both IP_TOS and IPV6_TCLASS to be set
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 28059
diff changeset
  1350
            * This helps in mixed environments where IPv4 and IPv6 sockets
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 28059
diff changeset
  1351
            * are connecting.
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 28059
diff changeset
  1352
            */
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 28059
diff changeset
  1353
           if (setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS,
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 28059
diff changeset
  1354
                           arg, len) < 0) {
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 28059
diff changeset
  1355
                return -1;
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 28059
diff changeset
  1356
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        iptos = (int *)arg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        *iptos &= (IPTOS_TOS_MASK | IPTOS_PREC_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
    /*
6696
7201f23dae0d 6989190: SO_SNDBUF/SO_RCVBUF limits should only be checked when setsockopt fails (sol)
alanb
parents: 6670
diff changeset
  1365
     * SOL_SOCKET/{SO_SNDBUF,SO_RCVBUF} - On Solaris we may need to clamp
7201f23dae0d 6989190: SO_SNDBUF/SO_RCVBUF limits should only be checked when setsockopt fails (sol)
alanb
parents: 6670
diff changeset
  1366
     * the value when it exceeds the system limit.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
    if (level == SOL_SOCKET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        if (opt == SO_SNDBUF || opt == SO_RCVBUF) {
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1371
            int sotype=0;
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
  1372
            socklen_t arglen;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
            int *bufsize, maxbuf;
6696
7201f23dae0d 6989190: SO_SNDBUF/SO_RCVBUF limits should only be checked when setsockopt fails (sol)
alanb
parents: 6670
diff changeset
  1374
            int ret;
7201f23dae0d 6989190: SO_SNDBUF/SO_RCVBUF limits should only be checked when setsockopt fails (sol)
alanb
parents: 6670
diff changeset
  1375
7201f23dae0d 6989190: SO_SNDBUF/SO_RCVBUF limits should only be checked when setsockopt fails (sol)
alanb
parents: 6670
diff changeset
  1376
            /* Attempt with the original size */
7201f23dae0d 6989190: SO_SNDBUF/SO_RCVBUF limits should only be checked when setsockopt fails (sol)
alanb
parents: 6670
diff changeset
  1377
            ret = setsockopt(fd, level, opt, arg, len);
7201f23dae0d 6989190: SO_SNDBUF/SO_RCVBUF limits should only be checked when setsockopt fails (sol)
alanb
parents: 6670
diff changeset
  1378
            if ((ret == 0) || (ret == -1 && errno != ENOBUFS))
7201f23dae0d 6989190: SO_SNDBUF/SO_RCVBUF limits should only be checked when setsockopt fails (sol)
alanb
parents: 6670
diff changeset
  1379
                return ret;
7201f23dae0d 6989190: SO_SNDBUF/SO_RCVBUF limits should only be checked when setsockopt fails (sol)
alanb
parents: 6670
diff changeset
  1380
7201f23dae0d 6989190: SO_SNDBUF/SO_RCVBUF limits should only be checked when setsockopt fails (sol)
alanb
parents: 6670
diff changeset
  1381
            /* Exceeded system limit so clamp and retry */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
            arglen = sizeof(sotype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
            if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *)&sotype,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                           &arglen) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
7289
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
  1389
            /*
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
  1390
             * We try to get tcp_maxbuf (and udp_max_buf) using
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
  1391
             * an ioctl() that isn't available on all versions of Solaris.
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
  1392
             * If that fails, we use the search algorithm in findMaxBuf()
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
  1393
             */
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
  1394
            if (!init_tcp_max_buf && sotype == SOCK_STREAM) {
22341
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents: 20821
diff changeset
  1395
                tcp_max_buf = net_getParam("/dev/tcp", "tcp_max_buf");
7289
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
  1396
                if (tcp_max_buf == -1) {
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
  1397
                    tcp_max_buf = findMaxBuf(fd, opt, SOCK_STREAM);
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
  1398
                    if (tcp_max_buf == -1) {
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
  1399
                        return -1;
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
  1400
                    }
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
  1401
                }
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
  1402
                init_tcp_max_buf = 1;
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
  1403
            } else if (!init_udp_max_buf && sotype == SOCK_DGRAM) {
22341
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents: 20821
diff changeset
  1404
                udp_max_buf = net_getParam("/dev/udp", "udp_max_buf");
7289
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
  1405
                if (udp_max_buf == -1) {
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
  1406
                    udp_max_buf = findMaxBuf(fd, opt, SOCK_DGRAM);
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
  1407
                    if (udp_max_buf == -1) {
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
  1408
                        return -1;
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
  1409
                    }
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
  1410
                }
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
  1411
                init_udp_max_buf = 1;
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
  1412
            }
1352201521d9 6984182: Setting SO_RCVBUF/SO_SNDBUF to larger than tcp_max_buf fails on Solaris 11 if kernel params changed
michaelm
parents: 6696
diff changeset
  1413
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
            maxbuf = (sotype == SOCK_STREAM) ? tcp_max_buf : udp_max_buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
            bufsize = (int *)arg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
            if (*bufsize > maxbuf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
                *bufsize = maxbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
  1423
#ifdef _AIX
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
  1424
    if (level == SOL_SOCKET) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
  1425
        if (opt == SO_SNDBUF || opt == SO_RCVBUF) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
  1426
            /*
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
  1427
             * Just try to set the requested size. If it fails we will leave the
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
  1428
             * socket option as is. Setting the buffer size means only a hint in
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
  1429
             * the jse2/java software layer, see javadoc. In the previous
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
  1430
             * solution the buffer has always been truncated to a length of
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
  1431
             * 0x100000 Byte, even if the technical limit has not been reached.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
  1432
             * This kind of absolute truncation was unexpected in the jck tests.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
  1433
             */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
  1434
            int ret = setsockopt(fd, level, opt, arg, len);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
  1435
            if ((ret == 0) || (ret == -1 && errno == ENOBUFS)) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
  1436
                // Accept failure because of insufficient buffer memory resources.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
  1437
                return 0;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
  1438
            } else {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
  1439
                // Deliver all other kinds of errors.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
  1440
                return ret;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
  1441
            }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
  1442
        }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
  1443
    }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
  1444
#endif
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
  1445
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
     * On Linux the receive buffer is used for both socket
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 27178
diff changeset
  1448
     * structures and the packet payload. The implication
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
     * is that if SO_RCVBUF is too small then small packets
30963
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 28059
diff changeset
  1450
     * must be discarded.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
    if (level == SOL_SOCKET && opt == SO_RCVBUF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
        int *bufsize = (int *)arg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
        if (*bufsize < 1024) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
            *bufsize = 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1461
#if defined(_ALLBSD_SOURCE)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1462
    /*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1463
     * SOL_SOCKET/{SO_SNDBUF,SO_RCVBUF} - On FreeBSD need to
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1464
     * ensure that value is <= kern.ipc.maxsockbuf as otherwise we get
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1465
     * an ENOBUFS error.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1466
     */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1467
    if (level == SOL_SOCKET) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1468
        if (opt == SO_SNDBUF || opt == SO_RCVBUF) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1469
#ifdef KIPC_MAXSOCKBUF
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1470
            if (maxsockbuf == -1) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1471
               mib[0] = CTL_KERN;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1472
               mib[1] = KERN_IPC;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1473
               mib[2] = KIPC_MAXSOCKBUF;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1474
               rlen = sizeof(maxsockbuf);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1475
               if (sysctl(mib, 3, &maxsockbuf, &rlen, NULL, 0) == -1)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1476
                   maxsockbuf = 1024;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1477
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1478
#if 1
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1479
               /* XXXBSD: This is a hack to workaround mb_max/mb_max_adj
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1480
                  problem.  It should be removed when kern.ipc.maxsockbuf
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1481
                  will be real value. */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1482
               maxsockbuf = (maxsockbuf/5)*4;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1483
#endif
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1484
           }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1485
#elif defined(__OpenBSD__)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1486
           maxsockbuf = SB_MAX;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1487
#else
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1488
           maxsockbuf = 64 * 1024;      /* XXX: NetBSD */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1489
#endif
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1490
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1491
           bufsize = (int *)arg;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1492
           if (*bufsize > maxsockbuf) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1493
               *bufsize = maxsockbuf;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1494
           }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1495
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1496
           if (opt == SO_RCVBUF && *bufsize < 1024) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1497
                *bufsize = 1024;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1498
           }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1499
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1500
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1501
    }
22605
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 22597
diff changeset
  1502
#endif
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1503
22605
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 22597
diff changeset
  1504
#if defined(_ALLBSD_SOURCE) || defined(_AIX)
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1505
    /*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1506
     * On Solaris, SO_REUSEADDR will allow multiple datagram
22605
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 22597
diff changeset
  1507
     * sockets to bind to the same port. The network jck tests check
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1508
     * for this "feature", so we need to emulate it by turning on
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1509
     * SO_REUSEPORT as well for that combination.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1510
     */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1511
    if (level == SOL_SOCKET && opt == SO_REUSEADDR) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1512
        int sotype;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1513
        socklen_t arglen;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1514
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1515
        arglen = sizeof(sotype);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1516
        if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *)&sotype, &arglen) < 0) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1517
            return -1;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1518
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1519
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1520
        if (sotype == SOCK_DGRAM) {
22605
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 22597
diff changeset
  1521
            setsockopt(fd, level, SO_REUSEPORT, arg, len);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1522
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1523
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1524
#endif
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
  1525
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
    return setsockopt(fd, level, opt, arg, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
 * Wrapper for bind system call - performs any necessary pre/post
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
 * processing to deal with OS specific issues :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
 * Linux allows a socket to bind to 127.0.0.255 which must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
 * caught.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
 *
13043
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
  1536
 * On Solaris with IPv6 enabled we must use an exclusive
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
  1537
 * bind to guarantee a unique port number across the IPv4 and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
 * IPv6 port spaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
NET_Bind(int fd, struct sockaddr *him, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
#if defined(__solaris__) && defined(AF_INET6)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
    int level = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
    int exclbind = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
    int rv;
31711
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
  1549
    int arg, alen;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
     * ## get bugId for this issue - goes back to 1.2.2 port ##
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
     * ## When IPv6 is enabled this will be an IPv4-mapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
     * ## with family set to AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
    if (him->sa_family == AF_INET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
        struct sockaddr_in *sa = (struct sockaddr_in *)him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
        if ((ntohl(sa->sin_addr.s_addr) & 0x7f0000ff) == 0x7f0000ff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
            errno = EADDRNOTAVAIL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
31711
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
  1566
#if defined(__solaris__)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
    /*
13043
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
  1568
     * Solaris has separate IPv4 and IPv6 port spaces so we
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
     * use an exclusive bind when SO_REUSEADDR is not used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
     * give the illusion of a unified port space.
13043
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
  1571
     * This also avoids problems with IPv6 sockets connecting
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
     * to IPv4 mapped addresses whereby the socket conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
     * results in a late bind that fails because the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
     * corresponding IPv4 port is in use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
     */
31711
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
  1576
    alen = sizeof(arg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
31711
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
  1578
    if (useExclBind || getsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
  1579
                   (char *)&arg, &alen) == 0) {
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
  1580
        if (useExclBind || arg == 0) {
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
  1581
            /*
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
  1582
             * SO_REUSEADDR is disabled or sun.net.useExclusiveBind
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
  1583
             * property is true so enable TCP_EXCLBIND or
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
  1584
             * UDP_EXCLBIND
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
  1585
             */
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
  1586
            alen = sizeof(arg);
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
  1587
            if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&arg,
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
  1588
                           &alen) == 0) {
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
  1589
                if (arg == SOCK_STREAM) {
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
  1590
                    level = IPPROTO_TCP;
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
  1591
                    exclbind = TCP_EXCLBIND;
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
  1592
                } else {
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
  1593
                    level = IPPROTO_UDP;
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
  1594
                    exclbind = UDP_EXCLBIND;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
                }
31711
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
  1596
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
31711
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
  1598
            arg = 1;
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
  1599
            setsockopt(fd, level, exclbind, (char *)&arg,
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
  1600
                       sizeof(arg));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
    rv = bind(fd, him, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
#if defined(__solaris__) && defined(AF_INET6)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
    if (rv < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
        int en = errno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
        /* Restore *_EXCLBIND if the bind fails */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
        if (exclbind != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
            int arg = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
            setsockopt(fd, level, exclbind, (char *)&arg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
                       sizeof(arg));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
        errno = en;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
/**
23033
0cc7c83fde3c 8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents: 23028
diff changeset
  1625
 * Wrapper for poll with timeout on a single file descriptor.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
 * flags (defined in net_util_md.h can be any combination of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
 * NET_WAIT_READ, NET_WAIT_WRITE & NET_WAIT_CONNECT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
 * The function will return when either the socket is ready for one
30963
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 28059
diff changeset
  1631
 * of the specified operations or the timeout expired.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
 * It returns the time left from the timeout (possibly 0), or -1 if it expired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
jint
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
NET_Wait(JNIEnv *env, jint fd, jint flags, jint timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
    jlong prevTime = JVM_CurrentTimeMillis(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
    jint read_rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
    while (1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
        jlong newTime;
23033
0cc7c83fde3c 8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents: 23028
diff changeset
  1644
        struct pollfd pfd;
0cc7c83fde3c 8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents: 23028
diff changeset
  1645
        pfd.fd = fd;
0cc7c83fde3c 8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents: 23028
diff changeset
  1646
        pfd.events = 0;
0cc7c83fde3c 8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents: 23028
diff changeset
  1647
        if (flags & NET_WAIT_READ)
0cc7c83fde3c 8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents: 23028
diff changeset
  1648
          pfd.events |= POLLIN;
0cc7c83fde3c 8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents: 23028
diff changeset
  1649
        if (flags & NET_WAIT_WRITE)
0cc7c83fde3c 8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents: 23028
diff changeset
  1650
          pfd.events |= POLLOUT;
0cc7c83fde3c 8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents: 23028
diff changeset
  1651
        if (flags & NET_WAIT_CONNECT)
0cc7c83fde3c 8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents: 23028
diff changeset
  1652
          pfd.events |= POLLOUT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
23033
0cc7c83fde3c 8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents: 23028
diff changeset
  1654
        errno = 0;
0cc7c83fde3c 8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents: 23028
diff changeset
  1655
        read_rv = NET_Poll(&pfd, 1, timeout);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
        newTime = JVM_CurrentTimeMillis(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
        timeout -= (newTime - prevTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
        if (timeout <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
          return read_rv > 0 ? 0 : -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
        }
22605
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 22597
diff changeset
  1662
        prevTime = newTime;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
        if (read_rv > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
          break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
      } /* while */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
    return timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
}
40937
f72e61ec734b 8075484: SocketInputStream.socketRead0 can hang even with soTimeout set
vtewari
parents: 38563
diff changeset
  1673
f72e61ec734b 8075484: SocketInputStream.socketRead0 can hang even with soTimeout set
vtewari
parents: 38563
diff changeset
  1674
long NET_GetCurrentTime() {
f72e61ec734b 8075484: SocketInputStream.socketRead0 can hang even with soTimeout set
vtewari
parents: 38563
diff changeset
  1675
    struct timeval time;
f72e61ec734b 8075484: SocketInputStream.socketRead0 can hang even with soTimeout set
vtewari
parents: 38563
diff changeset
  1676
    gettimeofday(&time, NULL);
f72e61ec734b 8075484: SocketInputStream.socketRead0 can hang even with soTimeout set
vtewari
parents: 38563
diff changeset
  1677
    return (time.tv_sec * 1000 + time.tv_usec / 1000);
f72e61ec734b 8075484: SocketInputStream.socketRead0 can hang even with soTimeout set
vtewari
parents: 38563
diff changeset
  1678
}
f72e61ec734b 8075484: SocketInputStream.socketRead0 can hang even with soTimeout set
vtewari
parents: 38563
diff changeset
  1679
f72e61ec734b 8075484: SocketInputStream.socketRead0 can hang even with soTimeout set
vtewari
parents: 38563
diff changeset
  1680
int NET_TimeoutWithCurrentTime(int s, long timeout, long currentTime) {
f72e61ec734b 8075484: SocketInputStream.socketRead0 can hang even with soTimeout set
vtewari
parents: 38563
diff changeset
  1681
    return NET_Timeout0(s, timeout, currentTime);
f72e61ec734b 8075484: SocketInputStream.socketRead0 can hang even with soTimeout set
vtewari
parents: 38563
diff changeset
  1682
}
f72e61ec734b 8075484: SocketInputStream.socketRead0 can hang even with soTimeout set
vtewari
parents: 38563
diff changeset
  1683
f72e61ec734b 8075484: SocketInputStream.socketRead0 can hang even with soTimeout set
vtewari
parents: 38563
diff changeset
  1684
int NET_Timeout(int s, long timeout) {
f72e61ec734b 8075484: SocketInputStream.socketRead0 can hang even with soTimeout set
vtewari
parents: 38563
diff changeset
  1685
    long currentTime = (timeout > 0) ? NET_GetCurrentTime() : 0;
f72e61ec734b 8075484: SocketInputStream.socketRead0 can hang even with soTimeout set
vtewari
parents: 38563
diff changeset
  1686
    return NET_Timeout0(s, timeout, currentTime);
f72e61ec734b 8075484: SocketInputStream.socketRead0 can hang even with soTimeout set
vtewari
parents: 38563
diff changeset
  1687
}