jdk/test/java/net/MulticastSocket/SetOutgoingIf.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 3945 391194a1b78a
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4742177
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Re-test IPv6 (and specifically MulticastSocket) with latest Linux & USAGI code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
public class SetOutgoingIf {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    private static int PORT = 9001;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    private static String osname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    static boolean isWindows() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        if (osname == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
            osname = System.getProperty("os.name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        return osname.contains("Windows");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static boolean hasIPv6() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        List<NetworkInterface> nics = Collections.list(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
                                        NetworkInterface.getNetworkInterfaces());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        for (NetworkInterface nic : nics) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            List<InetAddress> addrs = Collections.list(nic.getInetAddresses());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            for (InetAddress addr : addrs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                if (addr instanceof Inet6Address)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        if (isWindows()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            System.out.println("The test only run on non-Windows OS. Bye.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if (!hasIPv6()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            System.out.println("No IPv6 available. Bye.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        // We need 2 or more network interfaces to run the test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        List<NetworkInterface> nics = new ArrayList<NetworkInterface>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        for (NetworkInterface nic : Collections.list(NetworkInterface.getNetworkInterfaces())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            if (!nic.isLoopback())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                nics.add(nic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (nics.size() <= 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            System.out.println("Need 2 or more network interfaces to run. Bye.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        // We will send packets to one ipv4, one ipv4-mapped, and one ipv6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        // multicast group using each network interface :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        //      224.1.1.1        --|
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        //      ::ffff:224.1.1.2 -----> using network interface #1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        //      ff02::1:1        --|
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        //      224.1.2.1        --|
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        //      ::ffff:224.1.2.2 -----> using network interface #2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        //      ff02::1:2        --|
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        // and so on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        List<InetAddress> groups = new ArrayList<InetAddress>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        for (int i = 0; i < nics.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            InetAddress groupv4 = InetAddress.getByName("224.1." + (i+1) + ".1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            InetAddress groupv4mapped = InetAddress.getByName("::ffff:224.1." + (i+1) + ".2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            InetAddress groupv6 = InetAddress.getByName("ff02::1:" + (i+1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            groups.add(groupv4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            groups.add(groupv4mapped);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            groups.add(groupv6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            // use a separated thread to send to those 3 groups
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            Thread sender = new Thread(new Sender(nics.get(i), groupv4, groupv4mapped, groupv6, PORT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            sender.setDaemon(true); // we want sender to stop when main thread exits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            sender.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        // try to receive on each group, then check if the packet comes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        // from the expected network interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        byte[] buf = new byte[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        for (InetAddress group : groups) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        MulticastSocket mcastsock = new MulticastSocket(PORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        mcastsock.setSoTimeout(5000);   // 5 second
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            DatagramPacket packet = new DatagramPacket(buf, 0, buf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            mcastsock.joinGroup(new InetSocketAddress(group, PORT), nics.get(groups.indexOf(group) / 3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                mcastsock.receive(packet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                // test failed if any exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            // now check which network interface this packet comes from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            NetworkInterface from = NetworkInterface.getByInetAddress(packet.getAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            NetworkInterface shouldbe = nics.get(groups.indexOf(group) / 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            if (!from.equals(shouldbe)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                System.out.println("Packets on group "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                                    + group + " should come from "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                                    + shouldbe.getName() + ", but came from "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                                    + from.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                //throw new RuntimeException("Test failed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            mcastsock.leaveGroup(new InetSocketAddress(group, PORT), nics.get(groups.indexOf(group) / 3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
class Sender implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    private NetworkInterface nic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    private InetAddress group1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private InetAddress group2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    private InetAddress group3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public Sender(NetworkInterface nic,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    InetAddress groupv4, InetAddress groupv4mapped, InetAddress groupv6,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    int port) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        this.nic = nic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        group1 = groupv4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        group2 = groupv4mapped;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        group3 = groupv6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        this.port = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            MulticastSocket mcastsock = new MulticastSocket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            mcastsock.setNetworkInterface(nic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            byte[] buf = "hello world".getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            DatagramPacket packet1 = new DatagramPacket(buf, buf.length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                                        new InetSocketAddress(group1, port));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            DatagramPacket packet2 = new DatagramPacket(buf, buf.length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                                        new InetSocketAddress(group2, port));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            DatagramPacket packet3 = new DatagramPacket(buf, buf.length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                                        new InetSocketAddress(group3, port));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                mcastsock.send(packet1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                mcastsock.send(packet2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                mcastsock.send(packet3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                Thread.currentThread().sleep(1000);   // sleep 1 second
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
}