6914801: IPv6 unavailable if stdin is a socket

Reviewed-by: michaelm
This commit is contained in:
Daniel Jeliński 2023-01-09 07:39:12 +00:00
parent d03a5d9580
commit 8d17d1ee6f
7 changed files with 177 additions and 38 deletions

View File

@ -123,18 +123,7 @@ jint IPv6_supported()
*/
return JNI_FALSE;
}
/*
* If fd 0 is a socket it means we may have been launched from inetd or
* xinetd. If it's a socket then check the family - if it's an
* IPv4 socket then we need to disable IPv6.
*/
if (getsockname(0, &sa.sa, &sa_len) == 0) {
if (sa.sa.sa_family == AF_INET) {
close(fd);
return JNI_FALSE;
}
}
close(fd);
/**
* Linux - check if any interface has an IPv6 address.
@ -147,13 +136,11 @@ jint IPv6_supported()
char *bufP;
if (fP == NULL) {
close(fd);
return JNI_FALSE;
}
bufP = fgets(buf, sizeof(buf), fP);
fclose(fP);
if (bufP == NULL) {
close(fd);
return JNI_FALSE;
}
}
@ -164,7 +151,6 @@ jint IPv6_supported()
* we should also check if the APIs are available.
*/
ipv6_fn = JVM_FindLibraryEntry(RTLD_DEFAULT, "inet_pton");
close(fd);
if (ipv6_fn == NULL ) {
return JNI_FALSE;
} else {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -38,10 +38,6 @@
#include "sun_nio_ch_InheritedChannel.h"
static int toInetFamily(SOCKETADDRESS *sa) {
return (sa->sa.sa_family == (ipv6_available() ? AF_INET6 : AF_INET));
}
JNIEXPORT void JNICALL
Java_sun_nio_ch_InheritedChannel_initIDs(JNIEnv *env, jclass cla)
{
@ -58,9 +54,7 @@ Java_sun_nio_ch_InheritedChannel_inetPeerAddress0(JNIEnv *env, jclass cla, jint
jint remote_port;
if (getpeername(fd, &sa.sa, &len) == 0) {
if (toInetFamily(&sa)) {
remote_ia = NET_SockaddrToInetAddress(env, &sa, (int *)&remote_port);
}
remote_ia = NET_SockaddrToInetAddress(env, &sa, (int *)&remote_port);
}
return remote_ia;
@ -89,9 +83,7 @@ Java_sun_nio_ch_InheritedChannel_peerPort0(JNIEnv *env, jclass cla, jint fd)
jint remote_port = -1;
if (getpeername(fd, (struct sockaddr *)&sa.sa, &len) == 0) {
if (toInetFamily(&sa)) {
NET_SockaddrToInetAddress(env, &sa, (int *)&remote_port);
}
NET_SockaddrToInetAddress(env, &sa, (int *)&remote_port);
}
return remote_port;

View File

@ -0,0 +1,95 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.nio.ByteBuffer;
import java.nio.channels.Channel;
import java.nio.channels.SocketChannel;
public class CheckIPv6Service {
static boolean isIPv6Available() {
try {
new ServerSocket(0,0, InetAddress.getByAddress(new byte[16])).close();
return true;
} catch (Exception e) {
return false;
}
}
private static void doIt(SocketChannel sc, int closeAfter, int delay) throws IOException {
ByteBuffer bb = ByteBuffer.allocate(1024);
int total = 0;
for (;;) {
bb.clear();
int n = sc.read(bb);
if (n < 0) {
break;
}
total += n;
// echo
bb.flip();
sc.write(bb);
// close after X bytes?
if (closeAfter > 0 && total >= closeAfter) {
break;
}
}
sc.close();
if (delay > 0) {
try {
Thread.currentThread().sleep(delay);
} catch (InterruptedException x) { }
}
}
public static void main(String args[]) throws IOException {
// check if IPv6 is available; if it is, behave like EchoService.
if (!isIPv6Available()) {
return;
}
Channel c = System.inheritedChannel();
if (c == null) {
return;
}
if (c instanceof SocketChannel) {
int closeAfter = 0;
int delay = 0;
if (args.length > 0) {
closeAfter = Integer.parseInt(args[0]);
}
if (args.length > 1) {
delay = Integer.parseInt(args[1]);
}
doIt((SocketChannel)c, closeAfter, delay);
}
}
}

View File

@ -0,0 +1,56 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.io.IOException;
/**
* This test verifies that a service launched with IPv4 inherited channel
* can use IPv6 networking; this used to be impossible, see JDK-6914801
*/
public class CheckIPv6Test {
private static int failures = 0;
private static final String SERVICE = "CheckIPv6Service";
public static void main(String args[]) throws IOException {
if (!CheckIPv6Service.isIPv6Available()) {
System.out.println("IPv6 not available. Test skipped.");
return;
}
try {
EchoTest.TCPEchoTest(SERVICE);
System.out.println("IPv6 test passed.");
} catch (Exception x) {
System.err.println(x);
failures++;
}
if (failures > 0) {
throw new RuntimeException("Test failed - see log for details");
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -57,8 +57,8 @@ public class EchoTest {
* a reply (with timeout). Once the reply is received it is checked to ensure
* that it matches the original message.
*/
private static void TCPEchoTest() throws IOException {
SocketChannel sc = Launcher.launchWithInetSocketChannel(ECHO_SERVICE, null);
static void TCPEchoTest(String echoService) throws IOException {
SocketChannel sc = Launcher.launchWithInetSocketChannel(echoService, null);
String msg = "Where's that damn torpedo?";
int repeat = 100;
@ -160,7 +160,7 @@ public class EchoTest {
// TCP echo
try {
TCPEchoTest();
TCPEchoTest(ECHO_SERVICE);
System.out.println("TCP echo test passed.");
} catch (Exception x) {
System.err.println(x);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -23,7 +23,7 @@
/*
* @test
* @bug 4673940 4930794 8211842
* @bug 4673940 4930794 8211842 6914801
* @summary Unit tests for inetd feature
* @requires (os.family == "linux" | os.family == "mac")
* @library /test/lib
@ -35,6 +35,7 @@
* jdk.test.lib.process.*
* UnixSocketTest StateTest StateTestService EchoTest EchoService
* UnixDomainChannelTest CloseTest Launcher Util
* CheckIPv6Test CheckIPv6Service
* @run testng/othervm/native InheritedChannelTest
* @key intermittent
*/
@ -79,6 +80,7 @@ public class InheritedChannelTest {
{ "UnixSocketTest", List.of(UnixSocketTest.class.getName())},
{ "StateTest", List.of(StateTest.class.getName(), "-Dtest.classes="+TEST_CLASSES)},
{ "EchoTest", List.of(EchoTest.class.getName()) },
{ "CheckIPv6Test", List.of(CheckIPv6Test.class.getName()) },
{ "CloseTest", List.of(CloseTest.class.getName()) },
// run StateTest with a SecurityManager set

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -36,6 +36,7 @@ import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.file.Files;
import static java.net.StandardProtocolFamily.INET;
import static java.net.StandardProtocolFamily.UNIX;
public class Launcher {
@ -100,11 +101,18 @@ public class Launcher {
String... args)
throws IOException
{
try (ServerSocketChannel ssc = ServerSocketChannel.open()) {
ssc.socket().bind(new InetSocketAddress(InetAddress.getLocalHost(), 0));
InetSocketAddress isa = new InetSocketAddress(InetAddress.getLocalHost(),
ssc.socket().getLocalPort());
SocketChannel sc1 = SocketChannel.open(isa);
ServerSocketChannel ch;
try {
ch = ServerSocketChannel.open(INET);
System.out.println("Using INET (IPv4) channel");
} catch (Exception e) {
ch = ServerSocketChannel.open();
System.out.println("Using default channel (probably IPv6)");
}
try (ServerSocketChannel ssc = ch) {
ssc.socket().bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
System.out.println("Socket bound to " + ssc.getLocalAddress());
SocketChannel sc1 = SocketChannel.open(ssc.getLocalAddress());
try (SocketChannel sc2 = ssc.accept()) {
launch(className, options, args, Util.getFD(sc2));
}