NoLoopbackPackets.java revision 2222
0N/A/*
2808N/A * Copyright 2007 Sun Microsystems, Inc. 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 *
1472N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
1472N/A * CA 95054 USA or visit www.sun.com if you need additional information or
1472N/A * have any 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
258N/A static boolean isWindows() {
258N/A if (osname == null)
258N/A osname = System.getProperty("os.name");
258N/A return osname.contains("Windows");
258N/A }
258N/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 }
2772N/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
0N/A MulticastSocket msock = null;
0N/A List<SocketAddress> failedGroups = new ArrayList<SocketAddress>();
0N/A try {
0N/A msock = new MulticastSocket();
0N/A int port = msock.getLocalPort();
0N/A
0N/A // we will send packets to three multicast groups :-
0N/A // 224.1.1.1, ::ffff:224.1.1.2, and ff02::1:1
0N/A //
0N/A List<SocketAddress> groups = new ArrayList<SocketAddress>();
0N/A groups.add(new InetSocketAddress(InetAddress.getByName("224.1.1.1"), port));
0N/A groups.add(new InetSocketAddress(InetAddress.getByName("::ffff:224.1.1.2"), port));
0N/A groups.add(new InetSocketAddress(InetAddress.getByName("ff02::1:1"), port));
0N/A
0N/A Thread sender = new Thread(new Sender(groups));
2808N/A sender.setDaemon(true); // we want sender to stop when main thread exits
0N/A sender.start();
0N/A
0N/A // Now try to receive multicast packets. we should not see any of them
0N/A // since we disable loopback mode.
0N/A //
0N/A msock.setSoTimeout(5000); // 5 seconds
0N/A
2772N/A byte[] buf = new byte[1024];
0N/A DatagramPacket packet = new DatagramPacket(buf, 0, buf.length);
2772N/A for (SocketAddress group : groups) {
0N/A msock.joinGroup(group, null);
0N/A
0N/A try {
0N/A msock.receive(packet);
0N/A
0N/A // it is an error if we receive something
0N/A failedGroups.add(group);
0N/A } catch (SocketTimeoutException e) {
0N/A // we expect this
0N/A }
0N/A
0N/A msock.leaveGroup(group, null);
0N/A }
0N/A } finally {
0N/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 }
0N/A
0N/A static class Sender implements Runnable {
0N/A private List<SocketAddress> sendToGroups;
0N/A
0N/A public Sender(List<SocketAddress> groups) {
0N/A sendToGroups = groups;
0N/A }
258N/A
258N/A public void run() {
258N/A byte[] buf = "hello world".getBytes();
0N/A List<DatagramPacket> packets = new ArrayList<DatagramPacket>();
0N/A
0N/A try {
0N/A for (SocketAddress group : sendToGroups) {
0N/A DatagramPacket packet = new DatagramPacket(buf, buf.length, group);
0N/A packets.add(packet);
0N/A }
0N/A
0N/A MulticastSocket msock = new MulticastSocket();
0N/A msock.setLoopbackMode(true); // disable loopback mode
0N/A for (;;) {
0N/A for (DatagramPacket packet : packets) {
0N/A msock.send(packet);
2772N/A }
0N/A
0N/A Thread.sleep(1000); // 1 second
0N/A }
0N/A } catch (Exception e) {
0N/A throw new RuntimeException(e);
0N/A }
0N/A }
0N/A }
0N/A}
0N/A