1 /* 2 * Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved. 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 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 24 /* @test 25 * @bug 8014377 8241786 26 * @summary Test for interference when two sockets are bound to the same 27 * port but joined to different multicast groups 28 * @library /test/lib 29 * @build jdk.test.lib.NetworkConfiguration 30 * jdk.test.lib.Platform 31 * Promiscuous 32 * @run main Promiscuous 33 * @run main/othervm -Djava.net.preferIPv4Stack=true Promiscuous 34 * @key randomness 35 */ 36 37 import java.nio.ByteBuffer; 38 import java.nio.channels.*; 39 import java.net.*; 40 import static java.net.StandardProtocolFamily.*; 41 import java.util.*; 42 import java.io.IOException; 43 import java.util.stream.Collectors; 44 45 import jdk.test.lib.NetworkConfiguration; 46 import jdk.test.lib.net.IPSupport; 47 48 public class Promiscuous { 49 50 static final Random rand = new Random(); 51 52 static final ProtocolFamily UNSPEC = new ProtocolFamily() { 53 public String name() { 54 return "UNSPEC"; 55 } 56 }; 57 58 /** 59 * Sends a datagram to the given multicast group 60 */ 61 static int sendDatagram(NetworkInterface nif, 62 InetAddress group, 63 int port) 64 throws IOException 65 { 66 ProtocolFamily family = (group instanceof Inet6Address) ? 67 StandardProtocolFamily.INET6 : StandardProtocolFamily.INET; 68 int id = rand.nextInt(); 69 try (DatagramChannel dc = DatagramChannel.open(family)) { 70 dc.setOption(StandardSocketOptions.IP_MULTICAST_IF, nif); 71 byte[] msg = Integer.toString(id).getBytes("UTF-8"); 72 ByteBuffer buf = ByteBuffer.wrap(msg); 73 System.out.format("Send message -> group %s (id=0x%x)\n", 74 group.getHostAddress(), id); 75 dc.send(buf, new InetSocketAddress(group, port)); 76 } 77 return id; 78 } 79 80 /** 81 * Wait (with timeout) for datagram. The {@code datagramExepcted} 82 * parameter indicates whether a datagram is expected, and if 83 * {@true} then {@code id} is the identifier in the payload. 84 */ 85 static void receiveDatagram(DatagramChannel dc, 86 String name, 87 boolean datagramExepcted, 88 int id) 89 throws IOException 90 { 91 System.out.println("Checking if received by " + name); 92 93 Selector sel = Selector.open(); 94 dc.configureBlocking(false); 95 dc.register(sel, SelectionKey.OP_READ); 96 ByteBuffer buf = ByteBuffer.allocateDirect(100); 97 98 try { 99 for (;;) { 100 System.out.println("Waiting to receive message"); 101 sel.select(5*1000); 102 SocketAddress sa = dc.receive(buf); 103 104 // no datagram received 105 if (sa == null) { 106 if (datagramExepcted) { 107 throw new RuntimeException("Expected message not received"); 108 } 109 System.out.println("No message received (correct)"); 110 return; 111 } 112 113 // datagram received 114 115 InetAddress sender = ((InetSocketAddress)sa).getAddress(); 116 buf.flip(); 117 byte[] bytes = new byte[buf.remaining()]; 118 buf.get(bytes); 119 String s = new String(bytes, "UTF-8"); 120 int receivedId = -1; 121 try { 122 receivedId = Integer.parseInt(s); 123 System.out.format("Received message from %s (id=0x%x)\n", 124 sender, receivedId); 125 } catch (NumberFormatException x) { 126 System.out.format("Received message from %s (msg=%s)\n", sender, s); 127 } 128 129 if (!datagramExepcted) { 130 if (receivedId == id) 131 throw new RuntimeException("Message not expected"); 132 System.out.println("Message ignored (has wrong id)"); 133 } else { 134 if (receivedId == id) { 135 System.out.println("Message expected"); 136 return; 137 } 138 System.out.println("Message ignored (wrong sender)"); 139 } 140 141 sel.selectedKeys().clear(); 142 buf.rewind(); 143 } 144 } finally { 145 sel.close(); 146 } 147 } 148 149 static void test(ProtocolFamily family, 150 NetworkInterface nif, 151 InetAddress group1, 152 InetAddress group2) 153 throws IOException 154 { 155 156 System.out.format("%nTest family=%s%n", family.name()); 157 158 DatagramChannel dc1 = (family == UNSPEC) ? 159 DatagramChannel.open() : DatagramChannel.open(family); 160 DatagramChannel dc2 = (family == UNSPEC) ? 161 DatagramChannel.open() : DatagramChannel.open(family); 162 163 try { 164 dc1.setOption(StandardSocketOptions.SO_REUSEADDR, true); 165 dc2.setOption(StandardSocketOptions.SO_REUSEADDR, true); 166 167 dc1.bind(new InetSocketAddress(0)); 168 int port = dc1.socket().getLocalPort(); 169 dc2.bind(new InetSocketAddress(port)); 170 171 System.out.format("dc1 joining [%s]:%d @ %s\n", 172 group1.getHostAddress(), port, nif.getName()); 173 System.out.format("dc2 joining [%s]:%d @ %s\n", 174 group2.getHostAddress(), port, nif.getName()); 175 176 dc1.join(group1, nif); 177 dc2.join(group2, nif); 178 179 int id = sendDatagram(nif, group1, port); 180 181 receiveDatagram(dc1, "dc1", true, id); 182 receiveDatagram(dc2, "dc2", false, id); 183 184 id = sendDatagram(nif, group2, port); 185 186 receiveDatagram(dc1, "dc1", false, id); 187 receiveDatagram(dc2, "dc2", true, id); 188 189 } finally { 190 dc1.close(); 191 dc2.close(); 192 } 193 } 194 195 public static void main(String[] args) throws IOException { 196 IPSupport.throwSkippedExceptionIfNonOperational(); 197 198 String os = System.getProperty("os.name"); 199 200 // Requires IP_MULTICAST_ALL on Linux (new since 2.6.31) so skip 201 // on older kernels. Note that we skip on <= version 3 to keep the 202 // parsing simple 203 if (os.equals("Linux")) { 204 String osversion = System.getProperty("os.version"); 205 String[] vers = osversion.split("\\.", 0); 206 int major = Integer.parseInt(vers[0]); 207 if (major < 3) { 208 System.out.format("Kernel version is %s, test skipped%n", osversion); 209 return; 210 } 211 } 212 213 // get local network configuration to use 214 NetworkConfiguration config = NetworkConfiguration.probe(); 215 216 // multicast groups used for the test 217 InetAddress ip4Group1 = InetAddress.getByName("225.4.5.6"); 218 InetAddress ip4Group2 = InetAddress.getByName("225.4.6.6"); 219 220 for (NetworkInterface nif: config.ip4MulticastInterfaces() 221 .collect(Collectors.toList())) { 222 InetAddress source = config.ip4Addresses(nif).iterator().next(); 223 test(INET, nif, ip4Group1, ip4Group2); 224 225 // Solaris and Linux allow IPv6 sockets join IPv4 multicast groups 226 if (os.equals("Linux")) 227 test(UNSPEC, nif, ip4Group1, ip4Group2); 228 } 229 } 230 }