src/java.base/unix/native/libnet/net_util_md.c
author mbaesken
Fri, 22 Nov 2019 10:29:12 +0100
changeset 59243 fb1d9bf1be2b
parent 55375 96c7427456f9
permissions -rw-r--r--
8234629: remove unused functions from libnet Reviewed-by: alanb, vtewari
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
53349
d3aa93570779 8007606: Handle realloc() failure in unix/native/libnet/net_util_md.c correctly
igerasim
parents: 53335
diff changeset
     2
 * Copyright (c) 1997, 2019, 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
 */
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41578
diff changeset
    25
#include <dlfcn.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <errno.h>
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41578
diff changeset
    27
#include <net/if.h>
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41578
diff changeset
    28
#include <netinet/tcp.h> // defines TCP_NODELAY
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41578
diff changeset
    29
#include <stdlib.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <string.h>
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41578
diff changeset
    31
#include <sys/ioctl.h>
40937
f72e61ec734b 8075484: SocketInputStream.socketRead0 can hang even with soTimeout set
vtewari
parents: 38563
diff changeset
    32
#include <sys/time.h>
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
    33
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41578
diff changeset
    34
#if defined(__linux__)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include <arpa/inet.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include <net/route.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include <sys/utsname.h>
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41578
diff changeset
    38
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41578
diff changeset
    40
#if defined(__solaris__)
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41578
diff changeset
    41
#include <inet/nd.h>
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41578
diff changeset
    42
#include <limits.h>
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41578
diff changeset
    43
#include <stropts.h>
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41578
diff changeset
    44
#include <sys/filio.h>
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41578
diff changeset
    45
#include <sys/sockio.h>
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41578
diff changeset
    46
#endif
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41578
diff changeset
    47
42475
c80ab3b11401 8170920: SO_RCVBUF and SO_SNDBUF options problem for network channels on MacOS
michaelm
parents: 41834
diff changeset
    48
#if defined(MACOSX)
c80ab3b11401 8170920: SO_RCVBUF and SO_SNDBUF options problem for network channels on MacOS
michaelm
parents: 41834
diff changeset
    49
#include <sys/sysctl.h>
c80ab3b11401 8170920: SO_RCVBUF and SO_SNDBUF options problem for network channels on MacOS
michaelm
parents: 41834
diff changeset
    50
#endif
c80ab3b11401 8170920: SO_RCVBUF and SO_SNDBUF options problem for network channels on MacOS
michaelm
parents: 41834
diff changeset
    51
45061
74b09ee3cd55 8179905: Remove the use of gettimeofday in Networking code
vtewari
parents: 44921
diff changeset
    52
#include "jvm.h"
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41578
diff changeset
    53
#include "net_util.h"
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41578
diff changeset
    54
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41578
diff changeset
    55
#include "java_net_SocketOptions.h"
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41578
diff changeset
    56
#include "java_net_InetAddress.h"
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41578
diff changeset
    57
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41578
diff changeset
    58
#if defined(__linux__) && !defined(IPV6_FLOWINFO_SEND)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
#define IPV6_FLOWINFO_SEND      33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41578
diff changeset
    62
#if defined(__solaris__) && !defined(MAXINT)
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41578
diff changeset
    63
#define MAXINT INT_MAX
23028
0554f6183d11 8035876: AIX build issues after '8034174: Remove use of JVM_* functions from java.net code'
simonis
parents: 23015
diff changeset
    64
#endif
0554f6183d11 8035876: AIX build issues after '8034174: Remove use of JVM_* functions from java.net code'
simonis
parents: 23015
diff changeset
    65
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
/*
13043
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
    67
 * EXCLBIND socket options only on Solaris
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
#if defined(__solaris__) && !defined(TCP_EXCLBIND)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
#define TCP_EXCLBIND            0x21
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
#if defined(__solaris__) && !defined(UDP_EXCLBIND)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
#define UDP_EXCLBIND            0x0101
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
    76
#define RESTARTABLE(_cmd, _result) do { \
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
    77
    do { \
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
    78
        _result = _cmd; \
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
    79
    } while((_result == -1) && (errno == EINTR)); \
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
    80
} while(0)
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
    81
53422
6f02e036630e 8217461: (ch) Add Net.available to return the number of bytes in the socket input buffer
alanb
parents: 53349
diff changeset
    82
int NET_SocketAvailable(int s, int *pbytes) {
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
    83
    int result;
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
    84
    RESTARTABLE(ioctl(s, FIONREAD, pbytes), result);
53422
6f02e036630e 8217461: (ch) Add Net.available to return the number of bytes in the socket input buffer
alanb
parents: 53349
diff changeset
    85
    return result;
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
    86
}
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
    87
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
#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
    89
static int init_tcp_max_buf, init_udp_max_buf;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
static int tcp_max_buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
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
    92
static int useExclBind = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * Get the specified parameter from the specified driver. The value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * 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
    97
 * cannot be obtained return -1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 */
22341
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents: 20821
diff changeset
    99
int net_getParam(char *driver, char *param)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    struct strioctl stri;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    char buf [64];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    int s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    int value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    s = open (driver, O_RDWR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    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
   108
        return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    strncpy (buf, param, sizeof(buf));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    stri.ic_cmd = ND_GET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    stri.ic_timout = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    stri.ic_dp = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    stri.ic_len = sizeof(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    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
   116
        value = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        value = atoi(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    close (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
}
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
   123
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
   124
/*
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
   125
 * 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
   126
 * 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
   127
 * 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
   128
 *
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13048
diff changeset
   129
 * 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
   130
 * 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
   131
 */
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
   132
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
   133
#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
   134
#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
   135
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
   136
#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
   137
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
   138
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
   139
    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
   140
    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
   141
    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
   142
    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
   143
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
   144
    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
   145
        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
   146
    } 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
   147
        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
   148
    }
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
   149
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
   150
    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
   151
        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
   152
        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
   153
            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
   154
            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
   155
        }
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
   156
        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
   157
    } 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
   158
        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
   159
        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
   160
    }
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
   161
    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
   162
        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
   163
        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
   164
            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
   165
            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
   166
        } 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
   167
            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
   168
            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
   169
        }
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
   170
    } 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
   171
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
   172
    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
   173
}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
NET_ThrowByNameWithLastError(JNIEnv *env, const char *name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                   const char *defaultDetail) {
41578
6bc194dea076 8157749: Improve handling of DNS error replies
msheppar
parents: 41380
diff changeset
   179
    JNU_ThrowByNameWithMessageAndLastError(env, name, defaultDetail);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
NET_ThrowCurrent(JNIEnv *env, char *msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    NET_ThrowNew(env, errno, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
NET_ThrowNew(JNIEnv *env, int errorNumber, char *msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    char fullMsg[512];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    if (!msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        msg = "no further information";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    switch(errorNumber) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    case EBADF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        jio_snprintf(fullMsg, sizeof(fullMsg), "socket closed: %s", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", fullMsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    case EINTR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        errno = errorNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        JNU_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
jfieldID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
NET_GetFileDescriptorID(JNIEnv *env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    jclass cls = (*env)->FindClass(env, "java/io/FileDescriptor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    CHECK_NULL_RETURN(cls, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    return (*env)->GetFieldID(env, cls, "fd", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
54627
22323f20401b 8222562: Don't set IPV6_V6ONLY when IPv4 is not available
aeubanks
parents: 53422
diff changeset
   217
jint  IPv4_supported()
22323f20401b 8222562: Don't set IPV6_V6ONLY when IPv4 is not available
aeubanks
parents: 53422
diff changeset
   218
{
22323f20401b 8222562: Don't set IPV6_V6ONLY when IPv4 is not available
aeubanks
parents: 53422
diff changeset
   219
    int fd = socket(AF_INET, SOCK_STREAM, 0) ;
22323f20401b 8222562: Don't set IPV6_V6ONLY when IPv4 is not available
aeubanks
parents: 53422
diff changeset
   220
    if (fd < 0) {
22323f20401b 8222562: Don't set IPV6_V6ONLY when IPv4 is not available
aeubanks
parents: 53422
diff changeset
   221
        return JNI_FALSE;
22323f20401b 8222562: Don't set IPV6_V6ONLY when IPv4 is not available
aeubanks
parents: 53422
diff changeset
   222
    }
22323f20401b 8222562: Don't set IPV6_V6ONLY when IPv4 is not available
aeubanks
parents: 53422
diff changeset
   223
    close(fd);
22323f20401b 8222562: Don't set IPV6_V6ONLY when IPv4 is not available
aeubanks
parents: 53422
diff changeset
   224
    return JNI_TRUE;
22323f20401b 8222562: Don't set IPV6_V6ONLY when IPv4 is not available
aeubanks
parents: 53422
diff changeset
   225
}
22323f20401b 8222562: Don't set IPV6_V6ONLY when IPv4 is not available
aeubanks
parents: 53422
diff changeset
   226
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   227
#if defined(DONT_ENABLE_IPV6)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   228
jint  IPv6_supported()
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   229
{
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   230
    return JNI_FALSE;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   231
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   232
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   233
#else /* !DONT_ENABLE_IPV6 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   234
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
jint  IPv6_supported()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    void *ipv6_fn;
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 40937
diff changeset
   239
    SOCKETADDRESS sa;
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 40937
diff changeset
   240
    socklen_t sa_len = sizeof(SOCKETADDRESS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   242
    fd = socket(AF_INET6, SOCK_STREAM, 0) ;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    if (fd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
         *  TODO: We really cant tell since it may be an unrelated error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
         *  for now we will assume that AF_INET6 is not available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /*
52778
dbbf46b13d52 8211842: IPv6_supported wrongly returns false when unix domain socket is bound to fd 0
michaelm
parents: 52499
diff changeset
   252
     * If fd 0 is a socket it means we may have been launched from inetd or
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * xinetd. If it's a socket then check the family - if it's an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * IPv4 socket then we need to disable IPv6.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     */
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 40937
diff changeset
   256
    if (getsockname(0, &sa.sa, &sa_len) == 0) {
52778
dbbf46b13d52 8211842: IPv6_supported wrongly returns false when unix domain socket is bound to fd 0
michaelm
parents: 52499
diff changeset
   257
        if (sa.sa.sa_family == AF_INET) {
38459
6cf90640c38c 8048518: File Descriptor Leak in src/java.base/unix/native/libnet/net_util_md.c
chegar
parents: 36115
diff changeset
   258
            close(fd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * Linux - check if any interface has an IPv6 address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * Don't need to parse the line - we just need an indication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        FILE *fP = fopen("/proc/net/if_inet6", "r");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        char buf[255];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        char *bufP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if (fP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        bufP = fgets(buf, sizeof(buf), fP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        fclose(fP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        if (bufP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * On Solaris 8 it's possible to create INET6 sockets even
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * though IPv6 is not enabled on all interfaces. Thus we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * query the number of IPv6 addresses to verify that IPv6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * has been configured on at least one interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * On Linux it doesn't matter - if IPv6 is built-in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * kernel then IPv6 addresses will be bound automatically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * to all interfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
#ifdef SIOCGLIFNUM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        struct lifnum numifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        numifs.lifn_family = AF_INET6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        numifs.lifn_flags = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        if (ioctl(fd, SIOCGLIFNUM, (char *)&numifs) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
             * SIOCGLIFNUM failed - assume IPv6 not configured
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
         * If no IPv6 addresses then return false. If count > 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
         * it's possible that all IPv6 addresses are "down" but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
         * that's okay as they may be brought "up" while the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
         * VM is running.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        if (numifs.lifn_count == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    /* SIOCGLIFNUM not defined in build environment ??? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
#endif /* __solaris */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *  OK we may have the stack available in the kernel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *  we should also check if the APIs are available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    ipv6_fn = JVM_FindLibraryEntry(RTLD_DEFAULT, "inet_pton");
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   335
    close(fd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    if (ipv6_fn == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        return JNI_FALSE;
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   338
    } else {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   339
        return JNI_TRUE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
}
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   342
#endif /* DONT_ENABLE_IPV6 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
   344
jint reuseport_supported()
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
   345
{
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
   346
    /* Do a simple dummy call, and try to figure out from that */
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
   347
    int one = 1;
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
   348
    int rv, s;
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
   349
    s = socket(PF_INET, SOCK_STREAM, 0);
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
   350
    if (s < 0) {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
   351
        return JNI_FALSE;
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
   352
    }
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
   353
    rv = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, (void *)&one, sizeof(one));
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
   354
    if (rv != 0) {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
   355
        rv = JNI_FALSE;
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
   356
    } else {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
   357
        rv = JNI_TRUE;
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
   358
    }
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
   359
    close(s);
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
   360
    return rv;
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
   361
}
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
   362
23033
0cc7c83fde3c 8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents: 23028
diff changeset
   363
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
   364
                                               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
   365
                                               int gai_error)
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   366
{
6696
7201f23dae0d 6989190: SO_SNDBUF/SO_RCVBUF limits should only be checked when setsockopt fails (sol)
alanb
parents: 6670
diff changeset
   367
    int size;
7201f23dae0d 6989190: SO_SNDBUF/SO_RCVBUF limits should only be checked when setsockopt fails (sol)
alanb
parents: 6670
diff changeset
   368
    char *buf;
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   369
    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
   370
    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
   371
    if (error_string == NULL)
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   372
        error_string = "unknown error";
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   373
6696
7201f23dae0d 6989190: SO_SNDBUF/SO_RCVBUF limits should only be checked when setsockopt fails (sol)
alanb
parents: 6670
diff changeset
   374
    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
   375
    buf = (char *) malloc(size);
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   376
    if (buf) {
6696
7201f23dae0d 6989190: SO_SNDBUF/SO_RCVBUF limits should only be checked when setsockopt fails (sol)
alanb
parents: 6670
diff changeset
   377
        jstring s;
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   378
        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
   379
        s = JNU_NewStringPlatform(env, buf);
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   380
        if (s != NULL) {
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   381
            jobject x = JNU_NewObjectByName(env,
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   382
                                            "java/net/UnknownHostException",
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   383
                                            "(Ljava/lang/String;)V", s);
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   384
            if (x != NULL)
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   385
                (*env)->Throw(env, x);
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   386
        }
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   387
        free(buf);
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   388
    }
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   389
}
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   390
55375
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents: 54627
diff changeset
   391
#if defined(_AIX)
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
   392
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
   393
/* 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
   394
extern void aix_close_init();
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
   395
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
   396
void platformInit () {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
   397
    aix_close_init();
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
   398
}
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
   399
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
   402
void platformInit () {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
13043
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
   406
void parseExclusiveBindProperty(JNIEnv *env) {
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
   407
#ifdef __solaris__
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
   408
    jstring s, flagSet;
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
   409
    jclass iCls;
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
   410
    jmethodID mid;
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
   411
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
   412
    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
   413
    CHECK_NULL(s);
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
   414
    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
   415
    CHECK_NULL(iCls);
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
   416
    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
   417
                "(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
   418
    CHECK_NULL(mid);
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
   419
    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
   420
    if (flagSet != NULL) {
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
   421
        useExclBind = 1;
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
   422
    }
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
   423
#endif
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
   424
}
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
   425
27178
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 25859
diff changeset
   426
JNIEXPORT jint JNICALL
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 25859
diff changeset
   427
NET_EnableFastTcpLoopback(int fd) {
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 25859
diff changeset
   428
    return 0;
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 25859
diff changeset
   429
}
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 25859
diff changeset
   430
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   431
/**
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   432
 * See net_util.h for documentation
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   433
 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
JNIEXPORT int JNICALL
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   435
NET_InetAddressToSockaddr(JNIEnv *env, jobject iaObj, int port,
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   436
                          SOCKETADDRESS *sa, int *len,
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   437
                          jboolean v4MappedAddress)
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   438
{
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   439
    jint family = getInetAddress_family(env, iaObj);
51151
d6b131d2bc8b 8193419: Better Internet address support
vtewari
parents: 49704
diff changeset
   440
    JNU_CHECK_EXCEPTION_RETURN(env, -1);
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   441
    memset((char *)sa, 0, sizeof(SOCKETADDRESS));
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   442
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   443
    if (ipv6_available() &&
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   444
        !(family == java_net_InetAddress_IPv4 &&
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   445
          v4MappedAddress == JNI_FALSE))
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   446
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        jbyte caddr[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        jint address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41578
diff changeset
   450
        if (family == java_net_InetAddress_IPv4) {
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41578
diff changeset
   451
            // convert to IPv4-mapped address
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   452
            memset((char *)caddr, 0, 16);
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14320
diff changeset
   453
            address = getInetAddress_addr(env, iaObj);
51151
d6b131d2bc8b 8193419: Better Internet address support
vtewari
parents: 49704
diff changeset
   454
            JNU_CHECK_EXCEPTION_RETURN(env, -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            if (address == INADDR_ANY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                /* we would always prefer IPv6 wildcard address
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   457
                 * caddr[10] = 0xff;
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   458
                 * caddr[11] = 0xff; */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                caddr[10] = 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                caddr[11] = 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                caddr[12] = ((address >> 24) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                caddr[13] = ((address >> 16) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                caddr[14] = ((address >> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                caddr[15] = (address & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        } else {
20821
e0d0a585aa49 8015743: Address internet addresses
michaelm
parents: 16870
diff changeset
   468
            getInet6Address_ipaddress(env, iaObj, (char *)caddr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        }
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   470
        sa->sa6.sin6_port = htons(port);
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   471
        memcpy((void *)&sa->sa6.sin6_addr, caddr, sizeof(struct in6_addr));
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   472
        sa->sa6.sin6_family = AF_INET6;
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   473
        if (len != NULL) {
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   474
            *len = sizeof(struct sockaddr_in6);
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   475
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   477
        /* handle scope_id */
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41578
diff changeset
   478
        if (family != java_net_InetAddress_IPv4) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            if (ia6_scopeidID) {
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   480
                sa->sa6.sin6_scope_id = getInet6Address_scopeid(env, iaObj);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        }
41834
99c4f72c0a91 8168771: Remove #ifdef AF_INET6 guards in libnet native coding
clanger
parents: 41771
diff changeset
   483
    } else {
99c4f72c0a91 8168771: Remove #ifdef AF_INET6 guards in libnet native coding
clanger
parents: 41771
diff changeset
   484
        jint address;
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   485
        if (family != java_net_InetAddress_IPv4) {
41834
99c4f72c0a91 8168771: Remove #ifdef AF_INET6 guards in libnet native coding
clanger
parents: 41771
diff changeset
   486
            JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Protocol family unavailable");
99c4f72c0a91 8168771: Remove #ifdef AF_INET6 guards in libnet native coding
clanger
parents: 41771
diff changeset
   487
            return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        }
41834
99c4f72c0a91 8168771: Remove #ifdef AF_INET6 guards in libnet native coding
clanger
parents: 41771
diff changeset
   489
        address = getInetAddress_addr(env, iaObj);
51151
d6b131d2bc8b 8193419: Better Internet address support
vtewari
parents: 49704
diff changeset
   490
        JNU_CHECK_EXCEPTION_RETURN(env, -1);
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   491
        sa->sa4.sin_port = htons(port);
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   492
        sa->sa4.sin_addr.s_addr = htonl(address);
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   493
        sa->sa4.sin_family = AF_INET;
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   494
        if (len != NULL) {
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   495
            *len = sizeof(struct sockaddr_in);
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   496
        }
41834
99c4f72c0a91 8168771: Remove #ifdef AF_INET6 guards in libnet native coding
clanger
parents: 41771
diff changeset
   497
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
void
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   502
NET_SetTrafficClass(SOCKETADDRESS *sa, int trafficClass) {
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   503
    if (sa->sa.sa_family == AF_INET6) {
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   504
        sa->sa6.sin6_flowinfo = htonl((trafficClass & 0xff) << 20);
41834
99c4f72c0a91 8168771: Remove #ifdef AF_INET6 guards in libnet native coding
clanger
parents: 41771
diff changeset
   505
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
NET_IsIPv4Mapped(jbyte* caddr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    for (i = 0; i < 10; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        if (caddr[i] != 0x00) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            return 0; /* false */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    if (((caddr[10] & 0xff) == 0xff) && ((caddr[11] & 0xff) == 0xff)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        return 1; /* true */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    return 0; /* false */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
NET_IPv4MappedToIPv4(jbyte* caddr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    return ((caddr[12] & 0xff) << 24) | ((caddr[13] & 0xff) << 16) | ((caddr[14] & 0xff) << 8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        | (caddr[15] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
NET_IsEqual(jbyte* caddr1, jbyte* caddr2) {
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
        if (caddr1[i] != caddr2[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            return 0; /* false */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
12688
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12551
diff changeset
   540
int NET_IsZeroAddr(jbyte* caddr) {
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12551
diff changeset
   541
    int i;
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12551
diff changeset
   542
    for (i = 0; i < 16; i++) {
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12551
diff changeset
   543
        if (caddr[i] != 0) {
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12551
diff changeset
   544
            return 0;
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12551
diff changeset
   545
        }
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12551
diff changeset
   546
    }
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12551
diff changeset
   547
    return 1;
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12551
diff changeset
   548
}
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12551
diff changeset
   549
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
 * Map the Java level socket option to the platform specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
 * level and option name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
NET_MapSocketOption(jint cmd, int *level, int *optname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    static struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        jint cmd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        int level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        int optname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    } const opts[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        { java_net_SocketOptions_TCP_NODELAY,           IPPROTO_TCP,    TCP_NODELAY },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        { java_net_SocketOptions_SO_OOBINLINE,          SOL_SOCKET,     SO_OOBINLINE },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        { java_net_SocketOptions_SO_LINGER,             SOL_SOCKET,     SO_LINGER },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        { java_net_SocketOptions_SO_SNDBUF,             SOL_SOCKET,     SO_SNDBUF },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        { java_net_SocketOptions_SO_RCVBUF,             SOL_SOCKET,     SO_RCVBUF },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        { java_net_SocketOptions_SO_KEEPALIVE,          SOL_SOCKET,     SO_KEEPALIVE },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        { java_net_SocketOptions_SO_REUSEADDR,          SOL_SOCKET,     SO_REUSEADDR },
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 31711
diff changeset
   568
        { java_net_SocketOptions_SO_REUSEPORT,          SOL_SOCKET,     SO_REUSEPORT },
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        { java_net_SocketOptions_SO_BROADCAST,          SOL_SOCKET,     SO_BROADCAST },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        { java_net_SocketOptions_IP_TOS,                IPPROTO_IP,     IP_TOS },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        { java_net_SocketOptions_IP_MULTICAST_IF,       IPPROTO_IP,     IP_MULTICAST_IF },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        { java_net_SocketOptions_IP_MULTICAST_IF2,      IPPROTO_IP,     IP_MULTICAST_IF },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        { java_net_SocketOptions_IP_MULTICAST_LOOP,     IPPROTO_IP,     IP_MULTICAST_LOOP },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        switch (cmd) {
30963
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 28059
diff changeset
   580
            // Different multicast options if IPv6 is enabled
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            case java_net_SocketOptions_IP_MULTICAST_IF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            case java_net_SocketOptions_IP_MULTICAST_IF2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                *level = IPPROTO_IPV6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                *optname = IPV6_MULTICAST_IF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            case java_net_SocketOptions_IP_MULTICAST_LOOP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                *level = IPPROTO_IPV6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                *optname = IPV6_MULTICAST_LOOP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                return 0;
30963
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 28059
diff changeset
   591
#if (defined(__solaris__) || defined(MACOSX))
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 28059
diff changeset
   592
            // 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
   593
            case java_net_SocketOptions_IP_TOS:
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 28059
diff changeset
   594
                *level = IPPROTO_IPV6;
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 28059
diff changeset
   595
                *optname = IPV6_TCLASS;
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 28059
diff changeset
   596
                return 0;
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 28059
diff changeset
   597
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * Map the Java level option to the native level
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    for (i=0; i<(int)(sizeof(opts) / sizeof(opts[0])); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        if (cmd == opts[i].cmd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            *level = opts[i].level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            *optname = opts[i].optname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    /* not found */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
 * Wrapper for getsockopt system routine - does any necessary
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13048
diff changeset
   618
 * pre/post processing to deal with OS specific oddities :-
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
 * On Linux the SO_SNDBUF/SO_RCVBUF values must be post-processed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
 * to compensate for an incorrect value returned by the kernel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
NET_GetSockOpt(int fd, int level, int opt, void *result,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
               int *len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    int rv;
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   628
    socklen_t socklen = *len;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   630
    rv = getsockopt(fd, level, opt, result, &socklen);
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   631
    *len = socklen;
911
9edd1b9607e2 6730740: Fix for 6729881 has apparently broken several 64 bit tests: "Bad address"
chegar
parents: 910
diff changeset
   632
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    if (rv < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * On Linux SO_SNDBUF/SO_RCVBUF aren't symmetric. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * stems from additional socket structures in the send
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * and receive buffers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    if ((level == SOL_SOCKET) && ((opt == SO_SNDBUF)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                                  || (opt == SO_RCVBUF))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        int n = *((int *)result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        n /= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        *((int *)result) = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   651
/* Workaround for Mac OS treating linger value as
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   652
 *  signed integer
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   653
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   654
#ifdef MACOSX
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   655
    if (level == SOL_SOCKET && opt == SO_LINGER) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   656
        struct linger* to_cast = (struct linger*)result;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   657
        to_cast->l_linger = (unsigned short)to_cast->l_linger;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   658
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   659
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
 * Wrapper for setsockopt system routine - performs any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
 * necessary pre/post processing to deal with OS specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
 * issue :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
 * On Solaris need to limit the suggested value for SO_SNDBUF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
 * and SO_RCVBUF to the kernel configured limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
 * For IP_TOS socket option need to mask off bits as this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
 * 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
   673
 * an error.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
NET_SetSockOpt(int fd, int level, int  opt, const void *arg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
               int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
{
22605
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 22597
diff changeset
   679
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
#ifndef IPTOS_TOS_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
#define IPTOS_TOS_MASK 0x1e
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
#ifndef IPTOS_PREC_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
#define IPTOS_PREC_MASK 0xe0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   687
#if defined(_ALLBSD_SOURCE)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   688
#if defined(KIPC_MAXSOCKBUF)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   689
    int mib[3];
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   690
    size_t rlen;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   691
#endif
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   692
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   693
    int *bufsize;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   694
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   695
#ifdef __APPLE__
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   696
    static int maxsockbuf = -1;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   697
#else
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   698
    static long maxsockbuf = -1;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   699
#endif
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   700
#endif
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   701
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * IPPROTO/IP_TOS :-
30963
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 28059
diff changeset
   704
     * 1. IPv6 on Solaris/Mac OS:
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 28059
diff changeset
   705
     *    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
   706
     *    IPv6 and IPv4 scenarios.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * 2. IPv6 on Linux: By default Linux ignores flowinfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     *    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
   709
     *    will be examined. We also set the IPv4 TOS option in this case.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * 3. IPv4: set socket option based on ToS and Precedence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     *    fields (otherwise get invalid argument)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    if (level == IPPROTO_IP && opt == IP_TOS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        int *iptos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
41834
99c4f72c0a91 8168771: Remove #ifdef AF_INET6 guards in libnet native coding
clanger
parents: 41771
diff changeset
   716
#if defined(__linux__)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        if (ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            int optval = 1;
25170
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 23033
diff changeset
   719
            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
   720
                           (void *)&optval, sizeof(optval)) < 0) {
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 23033
diff changeset
   721
                return -1;
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 23033
diff changeset
   722
            }
30963
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 28059
diff changeset
   723
           /*
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 28059
diff changeset
   724
            * 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
   725
            * 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
   726
            * 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
   727
            * are connecting.
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 28059
diff changeset
   728
            */
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 28059
diff changeset
   729
           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
   730
                           arg, len) < 0) {
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 28059
diff changeset
   731
                return -1;
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 28059
diff changeset
   732
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        iptos = (int *)arg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        *iptos &= (IPTOS_TOS_MASK | IPTOS_PREC_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    /*
6696
7201f23dae0d 6989190: SO_SNDBUF/SO_RCVBUF limits should only be checked when setsockopt fails (sol)
alanb
parents: 6670
diff changeset
   741
     * 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
   742
     * the value when it exceeds the system limit.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    if (level == SOL_SOCKET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        if (opt == SO_SNDBUF || opt == SO_RCVBUF) {
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   747
            int sotype=0;
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   748
            socklen_t arglen;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            int *bufsize, maxbuf;
6696
7201f23dae0d 6989190: SO_SNDBUF/SO_RCVBUF limits should only be checked when setsockopt fails (sol)
alanb
parents: 6670
diff changeset
   750
            int ret;
7201f23dae0d 6989190: SO_SNDBUF/SO_RCVBUF limits should only be checked when setsockopt fails (sol)
alanb
parents: 6670
diff changeset
   751
7201f23dae0d 6989190: SO_SNDBUF/SO_RCVBUF limits should only be checked when setsockopt fails (sol)
alanb
parents: 6670
diff changeset
   752
            /* 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
   753
            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
   754
            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
   755
                return ret;
7201f23dae0d 6989190: SO_SNDBUF/SO_RCVBUF limits should only be checked when setsockopt fails (sol)
alanb
parents: 6670
diff changeset
   756
7201f23dae0d 6989190: SO_SNDBUF/SO_RCVBUF limits should only be checked when setsockopt fails (sol)
alanb
parents: 6670
diff changeset
   757
            /* Exceeded system limit so clamp and retry */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            arglen = sizeof(sotype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *)&sotype,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                           &arglen) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
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
   765
            /*
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
   766
             * 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
   767
             * 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
   768
             * 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
   769
             */
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
   770
            if (!init_tcp_max_buf && sotype == SOCK_STREAM) {
22341
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents: 20821
diff changeset
   771
                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
   772
                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
   773
                    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
   774
                    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
   775
                        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
   776
                    }
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
   777
                }
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
   778
                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
   779
            } 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
   780
                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
   781
                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
   782
                    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
   783
                    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
   784
                        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
   785
                    }
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
   786
                }
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
   787
                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
   788
            }
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
   789
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            maxbuf = (sotype == SOCK_STREAM) ? tcp_max_buf : udp_max_buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            bufsize = (int *)arg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            if (*bufsize > maxbuf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                *bufsize = maxbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
   799
#ifdef _AIX
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
   800
    if (level == SOL_SOCKET) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
   801
        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
   802
            /*
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
   803
             * 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
   804
             * 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
   805
             * 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
   806
             * 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
   807
             * 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
   808
             * 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
   809
             */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
   810
            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
   811
            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
   812
                // 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
   813
                return 0;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
   814
            } else {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
   815
                // 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
   816
                return ret;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
   817
            }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
   818
        }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
   819
    }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
   820
#endif
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents: 20821
diff changeset
   821
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * 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
   824
     * structures and the packet payload. The implication
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     * 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
   826
     * must be discarded.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    if (level == SOL_SOCKET && opt == SO_RCVBUF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        int *bufsize = (int *)arg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        if (*bufsize < 1024) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            *bufsize = 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   837
#if defined(_ALLBSD_SOURCE)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   838
    /*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   839
     * SOL_SOCKET/{SO_SNDBUF,SO_RCVBUF} - On FreeBSD need to
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   840
     * 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
   841
     * an ENOBUFS error.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   842
     */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   843
    if (level == SOL_SOCKET) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   844
        if (opt == SO_SNDBUF || opt == SO_RCVBUF) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   845
#ifdef KIPC_MAXSOCKBUF
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   846
            if (maxsockbuf == -1) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   847
               mib[0] = CTL_KERN;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   848
               mib[1] = KERN_IPC;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   849
               mib[2] = KIPC_MAXSOCKBUF;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   850
               rlen = sizeof(maxsockbuf);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   851
               if (sysctl(mib, 3, &maxsockbuf, &rlen, NULL, 0) == -1)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   852
                   maxsockbuf = 1024;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   853
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   854
#if 1
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   855
               /* 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
   856
                  problem.  It should be removed when kern.ipc.maxsockbuf
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   857
                  will be real value. */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   858
               maxsockbuf = (maxsockbuf/5)*4;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   859
#endif
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   860
           }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   861
#elif defined(__OpenBSD__)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   862
           maxsockbuf = SB_MAX;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   863
#else
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   864
           maxsockbuf = 64 * 1024;      /* XXX: NetBSD */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   865
#endif
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   866
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   867
           bufsize = (int *)arg;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   868
           if (*bufsize > maxsockbuf) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   869
               *bufsize = maxsockbuf;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   870
           }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   871
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   872
           if (opt == SO_RCVBUF && *bufsize < 1024) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   873
                *bufsize = 1024;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   874
           }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   875
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   876
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   877
    }
22605
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 22597
diff changeset
   878
#endif
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   879
22605
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 22597
diff changeset
   880
#if defined(_ALLBSD_SOURCE) || defined(_AIX)
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   881
    /*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   882
     * 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
   883
     * 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
   884
     * 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
   885
     * SO_REUSEPORT as well for that combination.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   886
     */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   887
    if (level == SOL_SOCKET && opt == SO_REUSEADDR) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   888
        int sotype;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   889
        socklen_t arglen;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   890
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   891
        arglen = sizeof(sotype);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   892
        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
   893
            return -1;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   894
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   895
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   896
        if (sotype == SOCK_DGRAM) {
22605
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 22597
diff changeset
   897
            setsockopt(fd, level, SO_REUSEPORT, arg, len);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   898
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   899
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   900
#endif
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11514
diff changeset
   901
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    return setsockopt(fd, level, opt, arg, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
 * Wrapper for bind system call - performs any necessary pre/post
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
 * processing to deal with OS specific issues :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
 * Linux allows a socket to bind to 127.0.0.255 which must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
 * caught.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
 *
13043
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
   912
 * 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
   913
 * bind to guarantee a unique port number across the IPv4 and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
 * IPv6 port spaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
int
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   918
NET_Bind(int fd, SOCKETADDRESS *sa, int len)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
{
41834
99c4f72c0a91 8168771: Remove #ifdef AF_INET6 guards in libnet native coding
clanger
parents: 41771
diff changeset
   920
#if defined(__solaris__)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    int level = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
    int exclbind = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
    int rv;
31711
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
   925
    int arg, alen;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * ## get bugId for this issue - goes back to 1.2.2 port ##
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * ## When IPv6 is enabled this will be an IPv4-mapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * ## with family set to AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     */
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   933
    if (sa->sa.sa_family == AF_INET) {
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   934
        if ((ntohl(sa->sa4.sin_addr.s_addr) & 0x7f0000ff) == 0x7f0000ff) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            errno = EADDRNOTAVAIL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
31711
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
   941
#if defined(__solaris__)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
    /*
13043
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 12047
diff changeset
   943
     * Solaris has separate IPv4 and IPv6 port spaces so we
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * use an exclusive bind when SO_REUSEADDR is not used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * 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
   946
     * This also avoids problems with IPv6 sockets connecting
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * to IPv4 mapped addresses whereby the socket conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * results in a late bind that fails because the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * corresponding IPv4 port is in use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     */
31711
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
   951
    alen = sizeof(arg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   953
    if (useExclBind ||
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   954
        getsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&arg, &alen) == 0)
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   955
    {
31711
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
   956
        if (useExclBind || arg == 0) {
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
   957
            /*
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
   958
             * SO_REUSEADDR is disabled or sun.net.useExclusiveBind
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
   959
             * property is true so enable TCP_EXCLBIND or
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
   960
             * UDP_EXCLBIND
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
   961
             */
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
   962
            alen = sizeof(arg);
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   963
            if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&arg, &alen) == 0)
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   964
            {
31711
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
   965
                if (arg == SOCK_STREAM) {
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
   966
                    level = IPPROTO_TCP;
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
   967
                    exclbind = TCP_EXCLBIND;
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
   968
                } else {
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
   969
                    level = IPPROTO_UDP;
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
   970
                    exclbind = UDP_EXCLBIND;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                }
31711
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
   972
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
31711
cca718f2fcf0 8075738: Better multi-JVM sharing
robm
parents: 30963
diff changeset
   974
            arg = 1;
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   975
            setsockopt(fd, level, exclbind, (char *)&arg, sizeof(arg));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 42475
diff changeset
   981
    rv = bind(fd, &sa->sa, len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
41834
99c4f72c0a91 8168771: Remove #ifdef AF_INET6 guards in libnet native coding
clanger
parents: 41771
diff changeset
   983
#if defined(__solaris__)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
    if (rv < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        int en = errno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        /* Restore *_EXCLBIND if the bind fails */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        if (exclbind != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
            int arg = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            setsockopt(fd, level, exclbind, (char *)&arg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                       sizeof(arg));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        errno = en;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
/**
23033
0cc7c83fde3c 8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents: 23028
diff changeset
  1000
 * Wrapper for poll with timeout on a single file descriptor.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
 * flags (defined in net_util_md.h can be any combination of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
 * NET_WAIT_READ, NET_WAIT_WRITE & NET_WAIT_CONNECT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
 * 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
  1006
 * of the specified operations or the timeout expired.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
 * It returns the time left from the timeout (possibly 0), or -1 if it expired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
52073
1c8745e31fa3 8212000: Verify exported symbols in java.base (libnet, libnio/ch)
prappo
parents: 51151
diff changeset
  1011
jint
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
NET_Wait(JNIEnv *env, jint fd, jint flags, jint timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
{
45061
74b09ee3cd55 8179905: Remove the use of gettimeofday in Networking code
vtewari
parents: 44921
diff changeset
  1014
    jlong prevNanoTime = JVM_NanoTime(env, 0);
74b09ee3cd55 8179905: Remove the use of gettimeofday in Networking code
vtewari
parents: 44921
diff changeset
  1015
    jlong nanoTimeout = (jlong) timeout * NET_NSEC_PER_MSEC;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
    jint read_rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
    while (1) {
45061
74b09ee3cd55 8179905: Remove the use of gettimeofday in Networking code
vtewari
parents: 44921
diff changeset
  1019
        jlong newNanoTime;
23033
0cc7c83fde3c 8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents: 23028
diff changeset
  1020
        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
  1021
        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
  1022
        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
  1023
        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
  1024
          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
  1025
        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
  1026
          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
  1027
        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
  1028
          pfd.events |= POLLOUT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
23033
0cc7c83fde3c 8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents: 23028
diff changeset
  1030
        errno = 0;
45061
74b09ee3cd55 8179905: Remove the use of gettimeofday in Networking code
vtewari
parents: 44921
diff changeset
  1031
        read_rv = NET_Poll(&pfd, 1, nanoTimeout / NET_NSEC_PER_MSEC);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
45061
74b09ee3cd55 8179905: Remove the use of gettimeofday in Networking code
vtewari
parents: 44921
diff changeset
  1033
        newNanoTime = JVM_NanoTime(env, 0);
74b09ee3cd55 8179905: Remove the use of gettimeofday in Networking code
vtewari
parents: 44921
diff changeset
  1034
        nanoTimeout -= (newNanoTime - prevNanoTime);
74b09ee3cd55 8179905: Remove the use of gettimeofday in Networking code
vtewari
parents: 44921
diff changeset
  1035
        if (nanoTimeout < NET_NSEC_PER_MSEC) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
          return read_rv > 0 ? 0 : -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        }
45061
74b09ee3cd55 8179905: Remove the use of gettimeofday in Networking code
vtewari
parents: 44921
diff changeset
  1038
        prevNanoTime = newNanoTime;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        if (read_rv > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
          break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
      } /* while */
45061
74b09ee3cd55 8179905: Remove the use of gettimeofday in Networking code
vtewari
parents: 44921
diff changeset
  1044
    return (nanoTimeout / NET_NSEC_PER_MSEC);
44921
0672237e13c0 8179602: Backout fix for JDK-8165437 due to breakage on 32-bit Linux
michaelm
parents: 44916
diff changeset
  1045
}