1 /* 2 * Copyright (c) 2013, 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 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 Promiscuous 30 * @run main Promiscuous 31 * @run main/othervm -Djava.net.preferIPv4Stack=true Promiscuous 32 * @key randomness 33 */ 34 35 import java.nio.ByteBuffer; 36 import java.nio.channels.*; 37 import java.net.*; 38 import static java.net.StandardProtocolFamily.*; 39 import java.util.*; 40 import java.io.IOException; 41 import java.util.stream.Collectors; 42 43 import jdk.test.lib.NetworkConfiguration; 44 45 public class Promiscuous { 46 47 static final Random rand = new Random(); 48 49 static final ProtocolFamily UNSPEC = new ProtocolFamily() { 50 public String name() { 51 return "UNSPEC"; 52 } 53 }; 54 55 /** 56 * Sends a datagram to the given multicast group 57 */ 58 static int sendDatagram(NetworkInterface nif, 59 InetAddress group, 60 int port) 61 throws IOException 62 { 63 ProtocolFamily family = (group instanceof Inet6Address) ? 64 StandardProtocolFamily.INET6 : StandardProtocolFamily.INET; 65 DatagramChannel dc = DatagramChannel.open(family) 66 .setOption(StandardSocketOptions.IP_MULTICAST_IF, nif); 67 int id = rand.nextInt(); 68 byte[] msg = Integer.toString(id).getBytes("UTF-8"); 69 ByteBuffer buf = ByteBuffer.wrap(msg); 70 System.out.format("Send message -> group %s (id=0x%x)\n", 71 group.getHostAddress(), id); 72 dc.send(buf, new InetSocketAddress(group, port)); 73 dc.close(); 74 return id; 75 } 76 77 /** 78 * Wait (with timeout) for datagram. The {@code datagramExepcted} 79 * parameter indicates whether a datagram is expected, and if 80 * {@true} then {@code id} is the identifier in the payload. 81 */ 82 static void receiveDatagram(DatagramChannel dc, 83 String name, 84 boolean datagramExepcted, 85 int id) 86 throws IOException 87 { 88 System.out.println("Checking if received by " + name); 89 90 Selector sel = Selector.open(); 91 dc.configureBlocking(false); 92 dc.register(sel, SelectionKey.OP_READ); 93 ByteBuffer buf = ByteBuffer.allocateDirect(100); 94 95 try { 96 for (;;) { 97 System.out.println("Waiting to receive message"); 98 sel.select(5*1000); 99 SocketAddress sa = dc.receive(buf); 100 101 // no datagram received 102 if (sa == null) { 103 if (datagramExepcted) { 104 throw new RuntimeException("Expected message not received"); 105 } 106 System.out.println("No message received (correct)"); 107 return; 108 } 109 110 // datagram received 111 112 InetAddress sender = ((InetSocketAddress)sa).getAddress(); 113 buf.flip(); 114 byte[] bytes = new byte[buf.remaining()]; 115 buf.get(bytes); 116 String s = new String(bytes, "UTF-8"); 117 int receivedId = -1; 118 try { 119 receivedId = Integer.parseInt(s); 120 System.out.format("Received message from %s (id=0x%x)\n", 121 sender, receivedId); 122 } catch (NumberFormatException x) { 123 System.out.format("Received message from %s (msg=%s)\n", sender, s); 124 } 125 126 if (!datagramExepcted) { 127 if (receivedId == id) 128 throw new RuntimeException("Message not expected"); 129 System.out.println("Message ignored (has wrong id)"); 130 } else { 131 if (receivedId == id) { 132 System.out.println("Message expected"); 133 return; 134 } 135 System.out.println("Message ignored (wrong sender)"); 136 } 137 138 sel.selectedKeys().clear(); 139 buf.rewind(); 140 } 141 } finally { 142 sel.close(); 143 } 144 } 145 146 static void test(ProtocolFamily family, 147 NetworkInterface nif, 148 InetAddress group1, 149 InetAddress group2) 150 throws IOException 151 { 152 153 System.out.format("%nTest family=%s%n", family.name()); 154 155 DatagramChannel dc1 = (family == UNSPEC) ? 156 DatagramChannel.open() : DatagramChannel.open(family); 157 DatagramChannel dc2 = (family == UNSPEC) ? 158 DatagramChannel.open() : DatagramChannel.open(family); 159 160 try { 161 dc1.setOption(StandardSocketOptions.SO_REUSEADDR, true); 162 dc2.setOption(StandardSocketOptions.SO_REUSEADDR, true); 163 164 dc1.bind(new InetSocketAddress(0)); 165 int port = dc1.socket().getLocalPort(); 166 dc2.bind(new InetSocketAddress(port)); 167 168 System.out.format("dc1 joining [%s]:%d @ %s\n", 169 group1.getHostAddress(), port, nif.getName()); 170 System.out.format("dc2 joining [%s]:%d @ %s\n", 171 group2.getHostAddress(), port, nif.getName()); 172 173 dc1.join(group1, nif); 174 dc2.join(group2, nif); 175 176 int id = sendDatagram(nif, group1, port); 177 178 receiveDatagram(dc1, "dc1", true, id); 179 receiveDatagram(dc2, "dc2", false, id); 180 181 id = sendDatagram(nif, group2, port); 182 183 receiveDatagram(dc1, "dc1", false, id); 184 receiveDatagram(dc2, "dc2", true, id); 185 186 } finally { 187 dc1.close(); 188 dc2.close(); 189 } 190 } 191 192 public static void main(String[] args) throws IOException { 193 String os = System.getProperty("os.name"); 194 195 // Requires IP_MULTICAST_ALL on Linux (new since 2.6.31) so skip 196 // on older kernels. Note that we skip on <= version 3 to keep the 197 // parsing simple 198 if (os.equals("Linux")) { 199 String osversion = System.getProperty("os.version"); 200 String[] vers = osversion.split("\\.", 0); 201 int major = Integer.parseInt(vers[0]); 202 if (major < 3) { 203 System.out.format("Kernel version is %s, test skipped%n", osversion); 204 return; 205 } 206 } 207 208 // get local network configuration to use 209 NetworkConfiguration config = NetworkConfiguration.probe(); 210 211 // multicast groups used for the test 212 InetAddress ip4Group1 = InetAddress.getByName("225.4.5.6"); 213 InetAddress ip4Group2 = InetAddress.getByName("225.4.6.6"); 214 215 for (NetworkInterface nif: config.ip4MulticastInterfaces() 216 .collect(Collectors.toList())) { 217 InetAddress source = config.ip4Addresses(nif).iterator().next(); 218 test(INET, nif, ip4Group1, ip4Group2); 219 220 // Solaris and Linux allow IPv6 sockets join IPv4 multicast groups 221 if (os.equals("SunOS") || os.equals("Linux")) 222 test(UNSPEC, nif, ip4Group1, ip4Group2); 223 } 224 } 225 }