Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/MulticastSocket/NoLoopbackPackets.java
38812 views
/*1* Copyright (c) 2007, 2010, 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/*24* @test25* @bug 474217726* @summary Re-test IPv6 (and specifically MulticastSocket) with latest Linux & USAGI code27*/28import java.util.*;29import java.net.*;3031public class NoLoopbackPackets {32private static String osname;3334static boolean isWindows() {35if (osname == null)36osname = System.getProperty("os.name");37return osname.contains("Windows");38}3940private static boolean hasIPv6() throws Exception {41List<NetworkInterface> nics = Collections.list(42NetworkInterface.getNetworkInterfaces());43for (NetworkInterface nic : nics) {44if (!nic.isLoopback()) {45List<InetAddress> addrs = Collections.list(nic.getInetAddresses());46for (InetAddress addr : addrs) {47if (addr instanceof Inet6Address) {48return true;49}50}51}52}5354return false;55}5657public static void main(String[] args) throws Exception {58if (isWindows()) {59System.out.println("The test only run on non-Windows OS. Bye.");60return;61}6263if (!hasIPv6()) {64System.out.println("No IPv6 available. Bye.");65return;66}6768MulticastSocket msock = null;69List<SocketAddress> failedGroups = new ArrayList<SocketAddress>();70try {71msock = new MulticastSocket();72int port = msock.getLocalPort();7374// we will send packets to three multicast groups :-75// 224.1.1.1, ::ffff:224.1.1.2, and ff02::1:176//77List<SocketAddress> groups = new ArrayList<SocketAddress>();78groups.add(new InetSocketAddress(InetAddress.getByName("224.1.1.1"), port));79groups.add(new InetSocketAddress(InetAddress.getByName("::ffff:224.1.1.2"), port));80groups.add(new InetSocketAddress(InetAddress.getByName("ff02::1:1"), port));8182Thread sender = new Thread(new Sender(groups));83sender.setDaemon(true); // we want sender to stop when main thread exits84sender.start();8586// Now try to receive multicast packets. we should not see any of them87// since we disable loopback mode.88//89msock.setSoTimeout(5000); // 5 seconds9091byte[] buf = new byte[1024];92DatagramPacket packet = new DatagramPacket(buf, 0, buf.length);93for (SocketAddress group : groups) {94msock.joinGroup(group, null);9596try {97msock.receive(packet);9899// it is an error if we receive something100failedGroups.add(group);101} catch (SocketTimeoutException e) {102// we expect this103}104105msock.leaveGroup(group, null);106}107} finally {108if (msock != null) try { msock.close(); } catch (Exception e) {}109}110111if (failedGroups.size() > 0) {112System.out.println("We should not receive anything from following groups, but we did:");113for (SocketAddress group : failedGroups)114System.out.println(group);115throw new RuntimeException("test failed.");116}117}118119static class Sender implements Runnable {120private List<SocketAddress> sendToGroups;121122public Sender(List<SocketAddress> groups) {123sendToGroups = groups;124}125126public void run() {127byte[] buf = "hello world".getBytes();128List<DatagramPacket> packets = new ArrayList<DatagramPacket>();129130try {131for (SocketAddress group : sendToGroups) {132DatagramPacket packet = new DatagramPacket(buf, buf.length, group);133packets.add(packet);134}135136MulticastSocket msock = new MulticastSocket();137msock.setLoopbackMode(true); // disable loopback mode138for (;;) {139for (DatagramPacket packet : packets) {140msock.send(packet);141}142143Thread.sleep(1000); // 1 second144}145} catch (Exception e) {146throw new RuntimeException(e);147}148}149}150}151152153