8236441: Bound MulticastSocket fails when setting outbound interface on Windows

Reviewed-by: alanb
This commit is contained in:
Chris Hegarty 2019-12-23 09:17:00 +00:00
parent 34b08ed2a5
commit 417672bc9d
3 changed files with 190 additions and 14 deletions

View File

@ -324,6 +324,7 @@ int getAllInterfacesAndAddresses (JNIEnv *env, netif **netifPP)
goto err;
}
loopif->naddrs += c;
loopif->ipv6Index = ptr->Ipv6IfIndex;
} else {
int index = ptr->IfIndex;
if (index != 0) {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2019, 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
@ -1455,7 +1455,7 @@ static void setMulticastInterface(JNIEnv *env, jobject this, int fd, int fd1,
* address is bound to and use the IPV6_MULTICAST_IF
* option instead of IP_MULTICAST_IF
*/
if (ipv6_supported) {
if (ipv6_supported && fd1 >= 0) {
static jclass ni_class = NULL;
if (ni_class == NULL) {
jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
@ -1496,7 +1496,7 @@ static void setMulticastInterface(JNIEnv *env, jobject this, int fd, int fd1,
* On IPv4 system extract addr[0] and use the IP_MULTICAST_IF
* option. For IPv6 both must be done.
*/
if (ipv6_supported) {
if (ipv6_supported && fd1 >= 0) {
static jfieldID ni_indexID = NULL;
struct in_addr in;
int index;
@ -1508,7 +1508,6 @@ static void setMulticastInterface(JNIEnv *env, jobject this, int fd, int fd1,
CHECK_NULL(ni_indexID);
}
index = (*env)->GetIntField(env, value, ni_indexID);
if (isAdapterIpv6Enabled(env, index) != 0) {
if (setsockopt(fd1, IPPROTO_IPV6, IPV6_MULTICAST_IF,
(const char*)&index, sizeof(index)) < 0) {
@ -1523,16 +1522,18 @@ static void setMulticastInterface(JNIEnv *env, jobject this, int fd, int fd1,
return;
}
}
/* If there are any IPv4 addresses on this interface then
* repeat the operation on the IPv4 fd */
if (fd >= 0) {
/* If there are any IPv4 addresses on this interface then
* repeat the operation on the IPv4 fd */
if (getInet4AddrFromIf(env, value, &in) < 0) {
return;
}
if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF,
(const char*)&in, sizeof(in)) < 0) {
JNU_ThrowByNameWithMessageAndLastError
(env, JNU_JAVANETPKG "SocketException", "Error setting socket option");
if (getInet4AddrFromIf(env, value, &in) < 0) {
return;
}
if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF,
(const char*)&in, sizeof(in)) < 0) {
JNU_ThrowByNameWithMessageAndLastError
(env, JNU_JAVANETPKG "SocketException", "Error setting socket option");
}
}
return;
} else {
@ -1877,7 +1878,7 @@ jobject getMulticastInterface(JNIEnv *env, jobject this, int fd, int fd1, jint o
addr = (*env)->GetObjectArrayElement(env, addrArray, 0);
return addr;
} else if (index == 0) { // index == 0 typically means IPv6 not configured on the interfaces
} else if (index == 0 && fd >= 0) {
// falling back to treat interface as configured for IPv4
jobject netObject = NULL;
netObject = getIPv4NetworkInterface(env, this, fd, opt, 0);

View File

@ -0,0 +1,174 @@
/*
* Copyright (c) 2019, 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.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.MulticastSocket;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.List;
import jdk.test.lib.NetworkConfiguration;
import jdk.test.lib.net.IPSupport;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import static java.lang.String.format;
import static java.lang.System.out;
import static java.net.StandardSocketOptions.IP_MULTICAST_IF;
import static java.util.stream.Collectors.toList;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
/**
* @test
* @bug 8236441
* @summary Bound MulticastSocket fails when setting outbound interface on Windows
* @library /test/lib
* @run testng IPMulticastIF
* @run testng/othervm -Djava.net.preferIPv4Stack=true IPMulticastIF
* @run testng/othervm -Djava.net.preferIPv6Addresses=true IPMulticastIF
* @run testng/othervm -Djava.net.preferIPv6Addresses=true -Djava.net.preferIPv4Stack=true IPMulticastIF
*/
public class IPMulticastIF {
@BeforeTest
public void sanity() {
IPSupport.throwSkippedExceptionIfNonOperational();
NetworkConfiguration.printSystemConfiguration(out);
}
@DataProvider(name = "scenarios")
public Object[][] positive() throws Exception {
List<InetAddress> addrs = List.of(InetAddress.getLocalHost(),
InetAddress.getLoopbackAddress());
List<Object[]> list = new ArrayList<>();
NetworkConfiguration nc = NetworkConfiguration.probe();
addrs.stream().forEach(a -> nc.multicastInterfaces(true)
.map(nif -> new Object[] { new InetSocketAddress(a, 0), nif })
.forEach(list::add) );
return list.stream().toArray(Object[][]::new);
}
@Test(dataProvider = "scenarios")
public void testSetGetInterfaceBound(InetSocketAddress bindAddr, NetworkInterface nif)
throws Exception
{
out.println(format("\n\n--- testSetGetInterfaceBound bindAddr=[%s], nif=[%s]", bindAddr, nif));
try (MulticastSocket ms = new MulticastSocket(bindAddr)) {
ms.setNetworkInterface(nif);
NetworkInterface msNetIf = ms.getNetworkInterface();
assertEquals(msNetIf, nif);
}
}
@Test(dataProvider = "scenarios")
public void testSetGetInterfaceUnbound(InetSocketAddress ignore, NetworkInterface nif)
throws Exception
{
out.println(format("\n\n--- testSetGetInterfaceUnbound nif=[%s]", nif));
try (MulticastSocket ms = new MulticastSocket()) {
ms.setNetworkInterface(nif);
NetworkInterface msNetIf = ms.getNetworkInterface();
assertEquals(msNetIf, nif);
}
}
@Test(dataProvider = "scenarios")
public void testSetGetOptionBound(InetSocketAddress bindAddr, NetworkInterface nif)
throws Exception
{
out.println(format("\n\n--- testSetGetOptionBound bindAddr=[%s], nif=[%s]", bindAddr, nif));
try (MulticastSocket ms = new MulticastSocket(bindAddr)) {
ms.setOption(IP_MULTICAST_IF, nif);
NetworkInterface msNetIf = ms.getOption(IP_MULTICAST_IF);
assertEquals(msNetIf, nif);
}
}
@Test(dataProvider = "scenarios")
public void testSetGetOptionUnbound(InetSocketAddress ignore, NetworkInterface nif)
throws Exception
{
out.println(format("\n\n--- testSetGetOptionUnbound nif=[%s]", nif));
try (MulticastSocket ms = new MulticastSocket()) {
ms.setOption(IP_MULTICAST_IF, nif);
NetworkInterface msNetIf = ms.getOption(IP_MULTICAST_IF);
assertEquals(msNetIf, nif);
}
}
// -- get without set
@DataProvider(name = "bindAddresses")
public Object[][] bindAddresses() throws Exception {
return new Object[][] {
{ new InetSocketAddress(InetAddress.getLocalHost(), 0) },
{ new InetSocketAddress(InetAddress.getLoopbackAddress(), 0) },
};
}
@Test(dataProvider = "bindAddresses")
public void testGetInterfaceBound(InetSocketAddress bindAddr)
throws Exception
{
out.println(format("\n\n--- testGetInterfaceBound bindAddr=[%s]", bindAddr));
try (MulticastSocket ms = new MulticastSocket(bindAddr)) {
assertPlaceHolder(ms.getNetworkInterface());
}
}
@Test
public void testGettInterfaceUnbound() throws Exception {
out.println("\n\n--- testGettInterfaceUnbound ");
try (MulticastSocket ms = new MulticastSocket()) {
assertPlaceHolder(ms.getNetworkInterface());
}
}
@Test(dataProvider = "bindAddresses")
public void testGetOptionBound(InetSocketAddress bindAddr)
throws Exception
{
out.println(format("\n\n--- testGetOptionBound bindAddr=[%s]", bindAddr));
try (MulticastSocket ms = new MulticastSocket(bindAddr)) {
assertEquals(ms.getOption(IP_MULTICAST_IF), null);
}
}
@Test
public void testGetOptionUnbound() throws Exception {
out.println("\n\n--- testGetOptionUnbound ");
try (MulticastSocket ms = new MulticastSocket()) {
assertEquals(ms.getOption(IP_MULTICAST_IF), null);
}
}
// Asserts that the placeholder NetworkInterface has a single InetAddress
// that represent any local address.
static void assertPlaceHolder(NetworkInterface nif) {
List<InetAddress> addrs = nif.inetAddresses().collect(toList());
assertEquals(addrs.size(), 1);
assertTrue(addrs.get(0).isAnyLocalAddress());
}
}