1 /*
   2  * Copyright (c) 2001, 2002, 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 /*
  25  * @test
  26  * @bug 4686717
  27  * @summary Test MulticastSocket.setLoopbackMode
  28  * @library /test/lib
  29  * @run main/othervm SetLoopbackMode
  30  */
  31 
  32 import java.net.*;
  33 import java.io.IOException;
  34 import java.util.Enumeration;
  35 import jdk.test.lib.NetworkConfiguration;
  36 
  37 public class SetLoopbackMode {
  38 
  39     static final boolean FAILED = true;
  40     static final boolean PASSED = false;
  41 
  42     static boolean test(MulticastSocket mc, InetAddress grp) throws IOException {
  43 
  44         boolean disabled = mc.getLoopbackMode();
  45 
  46         if (disabled) {
  47             System.out.println("Loopback mode is disabled.");
  48         } else {
  49             System.out.println("Loopback mode is enabled.");
  50         }
  51 
  52         byte b[] = "hello".getBytes();
  53         DatagramPacket p = new DatagramPacket(b, b.length, grp,
  54                                 mc.getLocalPort());
  55         mc.send(p);
  56 
  57         boolean gotPacket = false;
  58 
  59         mc.setSoTimeout(1000);
  60         try {
  61             b = new byte[16];
  62             p = new DatagramPacket(b, b.length);
  63             mc.receive(p);
  64             gotPacket = true;
  65 
  66             /* purge any additional copies of the packet */
  67             for (;;) {
  68                 mc.receive(p);
  69             }
  70 
  71         } catch (SocketTimeoutException x) {
  72         }
  73 
  74         if (gotPacket && disabled) {
  75             System.out.println("Packet received (unexpected as loopback is disabled)");
  76             return FAILED;
  77         }
  78         if (!gotPacket && !disabled) {
  79             System.out.println
  80                 ("Packet not received (packet excepted as loopback is enabled)");
  81             return FAILED;
  82         }
  83 
  84         if (gotPacket && !disabled) {
  85             System.out.println("Packet received - correct.");
  86         } else {
  87             System.out.println("Packet not received - correct.");
  88         }
  89 
  90         return PASSED;
  91     }
  92 
  93     private static boolean canUseIPv6(NetworkConfiguration nc) {
  94         return nc.ip6MulticastInterfaces().toArray().length > 0;
  95     }
  96 
  97     public static void main (String args[]) throws Exception {
  98         int failures = 0;
  99         NetworkConfiguration nc = NetworkConfiguration.probe();
 100 
 101         MulticastSocket mc = new MulticastSocket();
 102         InetAddress grp = InetAddress.getByName("224.80.80.80");
 103 
 104 
 105         /*
 106          * If IPv6 is available then use IPv6 multicast group - needed
 107          * to workaround Linux IPv6 bug whereby !IPV6_MULTICAST_LOOP
 108          * doesn't prevent loopback of IPv4 multicast packets.
 109          */
 110 
 111         if (canUseIPv6(nc)) {
 112             grp = InetAddress.getByName("ff01::1");
 113         }
 114 
 115         //mc.setNetworkInterface(NetworkInterface.getByInetAddress(lb));
 116         System.out.println("\nTest will use multicast group: " + grp);
 117         mc.joinGroup(grp);
 118 
 119         System.out.println("\n******************\n");
 120 
 121         mc.setLoopbackMode(true);
 122         if (test(mc, grp) == FAILED) failures++;
 123 
 124         System.out.println("\n******************\n");
 125 
 126         mc.setLoopbackMode(false);
 127         if (test(mc, grp) == FAILED) failures++;
 128 
 129         System.out.println("\n******************\n");
 130 
 131         if (failures > 0) {
 132             throw new RuntimeException("Test failed");
 133         }
 134     }
 135 }