0N/A/*
3261N/A * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4742177
0N/A * @summary Re-test IPv6 (and specifically MulticastSocket) with latest Linux & USAGI code
0N/A */
0N/Aimport java.util.*;
0N/Aimport java.net.*;
0N/A
0N/Apublic class NoLoopbackPackets {
0N/A private static String osname;
0N/A
0N/A static boolean isWindows() {
0N/A if (osname == null)
0N/A osname = System.getProperty("os.name");
0N/A return osname.contains("Windows");
0N/A }
0N/A
0N/A private static boolean hasIPv6() throws Exception {
0N/A List<NetworkInterface> nics = Collections.list(
0N/A NetworkInterface.getNetworkInterfaces());
0N/A for (NetworkInterface nic : nics) {
0N/A if (!nic.isLoopback()) {
0N/A List<InetAddress> addrs = Collections.list(nic.getInetAddresses());
0N/A for (InetAddress addr : addrs) {
0N/A if (addr instanceof Inet6Address) {
0N/A return true;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A return false;
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A if (isWindows()) {
0N/A System.out.println("The test only run on non-Windows OS. Bye.");
0N/A return;
0N/A }
0N/A
0N/A if (!hasIPv6()) {
0N/A System.out.println("No IPv6 available. Bye.");
0N/A return;
0N/A }
0N/A
2222N/A MulticastSocket msock = null;
2222N/A List<SocketAddress> failedGroups = new ArrayList<SocketAddress>();
2222N/A try {
2222N/A msock = new MulticastSocket();
2222N/A int port = msock.getLocalPort();
0N/A
2222N/A // we will send packets to three multicast groups :-
2222N/A // 224.1.1.1, ::ffff:224.1.1.2, and ff02::1:1
2222N/A //
2222N/A List<SocketAddress> groups = new ArrayList<SocketAddress>();
2222N/A groups.add(new InetSocketAddress(InetAddress.getByName("224.1.1.1"), port));
2222N/A groups.add(new InetSocketAddress(InetAddress.getByName("::ffff:224.1.1.2"), port));
2222N/A groups.add(new InetSocketAddress(InetAddress.getByName("ff02::1:1"), port));
2222N/A
2222N/A Thread sender = new Thread(new Sender(groups));
2222N/A sender.setDaemon(true); // we want sender to stop when main thread exits
2222N/A sender.start();
0N/A
2222N/A // Now try to receive multicast packets. we should not see any of them
2222N/A // since we disable loopback mode.
2222N/A //
2222N/A msock.setSoTimeout(5000); // 5 seconds
2222N/A
2222N/A byte[] buf = new byte[1024];
2222N/A DatagramPacket packet = new DatagramPacket(buf, 0, buf.length);
2222N/A for (SocketAddress group : groups) {
2222N/A msock.joinGroup(group, null);
0N/A
2222N/A try {
2222N/A msock.receive(packet);
0N/A
2222N/A // it is an error if we receive something
2222N/A failedGroups.add(group);
2222N/A } catch (SocketTimeoutException e) {
2222N/A // we expect this
2222N/A }
0N/A
2222N/A msock.leaveGroup(group, null);
0N/A }
2222N/A } finally {
2222N/A if (msock != null) try { msock.close(); } catch (Exception e) {}
0N/A }
0N/A
0N/A if (failedGroups.size() > 0) {
0N/A System.out.println("We should not receive anything from following groups, but we did:");
0N/A for (SocketAddress group : failedGroups)
0N/A System.out.println(group);
0N/A throw new RuntimeException("test failed.");
0N/A }
0N/A }
2222N/A
2222N/A static class Sender implements Runnable {
2222N/A private List<SocketAddress> sendToGroups;
0N/A
2222N/A public Sender(List<SocketAddress> groups) {
2222N/A sendToGroups = groups;
2222N/A }
0N/A
2222N/A public void run() {
2222N/A byte[] buf = "hello world".getBytes();
2222N/A List<DatagramPacket> packets = new ArrayList<DatagramPacket>();
0N/A
2222N/A try {
2222N/A for (SocketAddress group : sendToGroups) {
2222N/A DatagramPacket packet = new DatagramPacket(buf, buf.length, group);
2222N/A packets.add(packet);
0N/A }
0N/A
2222N/A MulticastSocket msock = new MulticastSocket();
2222N/A msock.setLoopbackMode(true); // disable loopback mode
2222N/A for (;;) {
2222N/A for (DatagramPacket packet : packets) {
2222N/A msock.send(packet);
2222N/A }
2222N/A
2222N/A Thread.sleep(1000); // 1 second
2222N/A }
2222N/A } catch (Exception e) {
2222N/A throw new RuntimeException(e);
0N/A }
0N/A }
0N/A }
0N/A}