Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/MulticastSocket/Promiscuous.java
38812 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*2223/* @test24* @bug 801449925* @summary Test for interference when two sockets are bound to the same26* port but joined to different multicast groups27* @run main Promiscuous28* @run main/othervm -Djava.net.preferIPv4Stack=true Promiscuous29*/3031import java.io.IOException;32import static java.lang.System.out;33import java.net.*;3435public class Promiscuous {3637static final int TIMEOUT = 5 * 1000; // 5 secs38static int id = 1000;3940static void receive(MulticastSocket mc, boolean datagramExpected, int id)41throws IOException42{43byte[] ba = new byte[100];44DatagramPacket p = new DatagramPacket(ba, ba.length);45try {46mc.receive(p);47int recvId = Integer.parseInt(48new String(p.getData(), 0, p.getLength(), "UTF-8"));49if (datagramExpected) {50if (recvId != id)51throw new RuntimeException("Unexpected id, got " + recvId52+ ", expected: " + id);53out.printf("Received message as expected, %s\n", p.getAddress());54} else {55throw new RuntimeException("Unexpected message received, "56+ p.getAddress());57}58} catch (SocketTimeoutException e) {59if (datagramExpected)60throw new RuntimeException("Expected message not received, "61+ e.getMessage());62else63out.printf("Message not received, as expected\n");64}65}6667static void test(InetAddress group1, InetAddress group2)68throws IOException69{70try (MulticastSocket mc1 = new MulticastSocket();71MulticastSocket mc2 = new MulticastSocket(mc1.getLocalPort());72DatagramSocket ds = new DatagramSocket()) {73final int port = mc1.getLocalPort();74out.printf("Using port: %d\n", port);7576mc1.setSoTimeout(TIMEOUT);77mc2.setSoTimeout(TIMEOUT);78int nextId = id;79byte[] msg = Integer.toString(nextId).getBytes("UTF-8");80DatagramPacket p = new DatagramPacket(msg, msg.length);81p.setAddress(group1);82p.setPort(port);8384mc1.joinGroup(group1);85out.printf("mc1 joined the MC group: %s\n", group1);86mc2.joinGroup(group2);87out.printf("mc2 joined the MC group: %s\n", group2);8889out.printf("Sending datagram to: %s/%d\n", group1, port);90ds.send(p);9192// the packet should be received by mc1 only93receive(mc1, true, nextId);94receive(mc2, false, 0);9596nextId = ++id;97msg = Integer.toString(nextId).getBytes("UTF-8");98p = new DatagramPacket(msg, msg.length);99p.setAddress(group2);100p.setPort(port);101102out.printf("Sending datagram to: %s/%d\n", group2, port);103ds.send(p);104105// the packet should be received by mc2 only106receive(mc2, true, nextId);107receive(mc1, false, 0);108109mc1.leaveGroup(group1);110mc2.leaveGroup(group2);111}112}113114public static void main(String args[]) throws IOException {115String os = System.getProperty("os.name");116117// Requires IP_MULTICAST_ALL on Linux (new since 2.6.31) so skip118// on older kernels. Note that we skip on <= version 3 to keep the119// parsing simple120if (os.equals("Linux")) {121String osversion = System.getProperty("os.version");122String[] vers = osversion.split("\\.", 0);123int major = Integer.parseInt(vers[0]);124if (major < 3) {125System.out.format("Kernel version is %s, test skipped%n", osversion);126return;127}128}129130// multicast groups used for the test131InetAddress ip4Group1 = InetAddress.getByName("224.7.8.9");132InetAddress ip4Group2 = InetAddress.getByName("225.4.5.6");133134test(ip4Group1, ip4Group2);135}136}137138139