Skip to content

Commit 9a40b76

Browse files
Man CaoMartin Buchholz
Man Cao
and
Martin Buchholz
committed
8293842: IPv6-only systems throws UnsupportedOperationException for several socket/TCP options
Co-authored-by: Martin Buchholz <martin@openjdk.org> Reviewed-by: djelinski, dfuchs
1 parent bb9aa4e commit 9a40b76

File tree

5 files changed

+25
-9
lines changed

5 files changed

+25
-9
lines changed

src/java.base/share/native/libnet/net_util.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
int IPv4_supported();
3232
int IPv6_supported();
33-
int reuseport_supported();
33+
int reuseport_supported(int ipv6_available);
3434

3535
static int IPv4_available;
3636
static int IPv6_available;
@@ -80,7 +80,7 @@ DEF_JNI_OnLoad(JavaVM *vm, void *reserved)
8080
IPv6_available = IPv6_supported() & (!preferIPv4Stack);
8181

8282
/* check if SO_REUSEPORT is supported on this platform */
83-
REUSEPORT_available = reuseport_supported();
83+
REUSEPORT_available = reuseport_supported(IPv6_available);
8484
platformInit();
8585

8686
return JNI_VERSION_1_2;

src/java.base/unix/native/libnet/net_util_md.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,16 @@ jint IPv6_supported()
187187
}
188188
#endif /* DONT_ENABLE_IPV6 */
189189

190-
jint reuseport_supported()
190+
jint reuseport_supported(int ipv6_available)
191191
{
192192
/* Do a simple dummy call, and try to figure out from that */
193193
int one = 1;
194194
int rv, s;
195-
s = socket(PF_INET, SOCK_STREAM, 0);
195+
if (ipv6_available) {
196+
s = socket(PF_INET6, SOCK_STREAM, 0);
197+
} else {
198+
s = socket(PF_INET, SOCK_STREAM, 0);
199+
}
196200
if (s < 0) {
197201
return JNI_FALSE;
198202
}

src/java.base/windows/native/libnet/net_util_md.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ jint IPv6_supported()
235235
return JNI_TRUE;
236236
}
237237

238-
jint reuseport_supported()
238+
jint reuseport_supported(int ipv6_available)
239239
{
240240
/* SO_REUSEPORT is not supported on Windows */
241241
return JNI_FALSE;

src/jdk.net/linux/native/libextnet/LinuxSocketOptions.c

+8-2
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,15 @@ static jint socketOptionSupported(jint level, jint optname) {
5454
jint one = 1;
5555
jint rv, s;
5656
socklen_t sz = sizeof (one);
57-
s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
57+
/* First try IPv6; fall back to IPv4. */
58+
s = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);
5859
if (s < 0) {
59-
return 0;
60+
if (errno == EPFNOSUPPORT || errno == EAFNOSUPPORT) {
61+
s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
62+
}
63+
if (s < 0) {
64+
return 0;
65+
}
6066
}
6167
rv = getsockopt(s, level, optname, (void *) &one, &sz);
6268
if (rv != 0 && errno == ENOPROTOOPT) {

src/jdk.net/macosx/native/libextnet/MacOSXSocketOptions.c

+8-2
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,15 @@ DEF_STATIC_JNI_OnLoad
4848
static jint socketOptionSupported(jint sockopt) {
4949
jint one = 1;
5050
jint rv, s;
51-
s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
51+
/* First try IPv6; fall back to IPv4. */
52+
s = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
5253
if (s < 0) {
53-
return 0;
54+
if (errno == EPFNOSUPPORT || errno == EAFNOSUPPORT) {
55+
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
56+
}
57+
if (s < 0) {
58+
return 0;
59+
}
5460
}
5561
rv = setsockopt(s, IPPROTO_TCP, sockopt, (void *) &one, sizeof (one));
5662
if (rv != 0 && errno == ENOPROTOOPT) {

0 commit comments

Comments
 (0)