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