Skip to content

Commit

Permalink
8234148: DatagramSocket.setOption/getOption/supportedOption should su…
Browse files Browse the repository at this point in the history
…pport multicast options

Fix adds multicast socket options to DatagramSocket

Reviewed-by: alanb, dfuchs, vtewari
  • Loading branch information
pconcannon committed Dec 9, 2019
1 parent 3f259d8 commit 22e26b2
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 38 deletions.
Expand Up @@ -404,22 +404,8 @@ public Object getOption(int optID) throws SocketException {
ExtendedSocketOptions.getInstance(); ExtendedSocketOptions.getInstance();


private static final Set<SocketOption<?>> datagramSocketOptions = datagramSocketOptions(); private static final Set<SocketOption<?>> datagramSocketOptions = datagramSocketOptions();
private static final Set<SocketOption<?>> multicastSocketOptions = multicastSocketOptions();


private static Set<SocketOption<?>> datagramSocketOptions() { private static Set<SocketOption<?>> datagramSocketOptions() {
HashSet<SocketOption<?>> options = new HashSet<>();
options.add(StandardSocketOptions.SO_SNDBUF);
options.add(StandardSocketOptions.SO_RCVBUF);
options.add(StandardSocketOptions.SO_REUSEADDR);
options.add(StandardSocketOptions.SO_BROADCAST);
options.add(StandardSocketOptions.IP_TOS);
if (isReusePortAvailable())
options.add(StandardSocketOptions.SO_REUSEPORT);
options.addAll(ExtendedSocketOptions.datagramSocketOptions());
return Collections.unmodifiableSet(options);
}

private static Set<SocketOption<?>> multicastSocketOptions() {
HashSet<SocketOption<?>> options = new HashSet<>(); HashSet<SocketOption<?>> options = new HashSet<>();
options.add(StandardSocketOptions.SO_SNDBUF); options.add(StandardSocketOptions.SO_SNDBUF);
options.add(StandardSocketOptions.SO_RCVBUF); options.add(StandardSocketOptions.SO_RCVBUF);
Expand All @@ -437,9 +423,6 @@ private static Set<SocketOption<?>> multicastSocketOptions() {


@Override @Override
protected Set<SocketOption<?>> supportedOptions() { protected Set<SocketOption<?>> supportedOptions() {
if (isMulticast)
return multicastSocketOptions;
else
return datagramSocketOptions; return datagramSocketOptions;
} }


Expand Down
Expand Up @@ -25,9 +25,14 @@
package java.net; package java.net;


import java.io.IOException; import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import jdk.internal.access.SharedSecrets; import jdk.internal.access.SharedSecrets;
import jdk.internal.access.JavaIOFileDescriptorAccess; import jdk.internal.access.JavaIOFileDescriptorAccess;


import sun.net.ext.ExtendedSocketOptions;

/** /**
* This class defines the plain DatagramSocketImpl that is used on * This class defines the plain DatagramSocketImpl that is used on
* Windows platforms greater than or equal to Windows Vista. These * Windows platforms greater than or equal to Windows Vista. These
Expand Down Expand Up @@ -230,6 +235,19 @@ protected Object socketGetOption(int opt) throws SocketException {
return returnValue; return returnValue;
} }


@Override
protected Set<SocketOption<?>> supportedOptions() {
HashSet<SocketOption<?>> options = new HashSet<>();
options.add(StandardSocketOptions.SO_SNDBUF);
options.add(StandardSocketOptions.SO_RCVBUF);
options.add(StandardSocketOptions.SO_REUSEADDR);
options.add(StandardSocketOptions.SO_BROADCAST);
options.add(StandardSocketOptions.IP_TOS);

options.addAll(ExtendedSocketOptions.datagramSocketOptions());
return Collections.unmodifiableSet(options);
}

/* Multicast specific methods. /* Multicast specific methods.
* Multicasting on a dual layer TCP/IP stack is always done with * Multicasting on a dual layer TCP/IP stack is always done with
* TwoStacksPlainDatagramSocketImpl. This is to overcome the lack * TwoStacksPlainDatagramSocketImpl. This is to overcome the lack
Expand Down
58 changes: 58 additions & 0 deletions test/jdk/java/net/DatagramSocket/SupportedOptionsCheck.java
@@ -0,0 +1,58 @@
/*
* 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.
*/

/*
* @test
* @bug 8234148
* @library /test/lib
* @summary checks that the DatagramSocket supportedOptions set contains all
* MulticastSocket socket options
* @run testng SupportedOptionsCheck
*/

import jdk.test.lib.Platform;
import org.testng.annotations.Test;

import java.net.DatagramSocket;
import java.net.StandardSocketOptions;
import java.util.Set;

import static org.testng.Assert.assertTrue;

public class SupportedOptionsCheck {

@Test
public void checkMulticastOptionsAreReturned() throws Exception {
try (DatagramSocket ds = new DatagramSocket())
{
Set<?> options = ds.supportedOptions();
Set<?> multicastOptions = Set.of(
StandardSocketOptions.IP_MULTICAST_IF,
StandardSocketOptions.IP_MULTICAST_TTL,
StandardSocketOptions.IP_MULTICAST_LOOP);

if (!Platform.isWindows())
assertTrue(options.containsAll(multicastOptions));
}
}
}
28 changes: 7 additions & 21 deletions test/jdk/java/net/SocketOption/SupportedOptionsSet.java
@@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
Expand All @@ -22,7 +22,8 @@
*/ */


import java.io.IOException; import java.io.IOException;
import java.net.*; import java.net.ServerSocket;
import java.net.Socket;
import java.util.Set; import java.util.Set;
import static java.lang.System.out; import static java.lang.System.out;
import jdk.test.lib.net.IPSupport; import jdk.test.lib.net.IPSupport;
Expand Down Expand Up @@ -54,39 +55,24 @@ else if (args[0].equals("second"))


static void first() throws IOException { static void first() throws IOException {
try (Socket s = new Socket(); try (Socket s = new Socket();
ServerSocket ss = new ServerSocket(); ServerSocket ss = new ServerSocket())
DatagramSocket ds = new DatagramSocket(); {
MulticastSocket ms = new MulticastSocket()) {

Set<?> first = s.supportedOptions(); Set<?> first = s.supportedOptions();
Set<?> second = ss.supportedOptions(); Set<?> second = ss.supportedOptions();
assertNotEqual(first, second, assertNotEqual(first, second,
"Socket and ServerSocket should have different options."); "Socket and ServerSocket should have different options.");

first = ds.supportedOptions();
second = ms.supportedOptions();
assertNotEqual(first, second,
"DatagramSocket and MulticastSocket should have different options.");
} }
} }


/** Tests with the order of access to supportedOptions reversed. */ /** Tests with the order of access to supportedOptions reversed. */
static void second() throws IOException { static void second() throws IOException {
try (ServerSocket ss = new ServerSocket(); try (ServerSocket ss = new ServerSocket();
Socket s = new Socket(); Socket s = new Socket())
DatagramSocket ds = new DatagramSocket(); {
MulticastSocket ms = new MulticastSocket()) {

Set<?> first = ss.supportedOptions(); Set<?> first = ss.supportedOptions();
Set<?> second = s.supportedOptions(); Set<?> second = s.supportedOptions();
assertNotEqual(first, second, assertNotEqual(first, second,
"ServerSocket and Socket should have different options."); "ServerSocket and Socket should have different options.");

first = ms.supportedOptions();
second = ds.supportedOptions();
assertNotEqual(first, second,
"MulticastSocket and DatagramSocket should have different options.");

} }
} }


Expand Down

0 comments on commit 22e26b2

Please sign in to comment.