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.net.*;
0N/Aimport java.util.*;
0N/A
0N/A
0N/Apublic class SetOutgoingIf {
0N/A private static int PORT = 9001;
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 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 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 // We need 2 or more network interfaces to run the test
0N/A //
1720N/A List<NetIf> netIfs = new ArrayList<NetIf>();
1720N/A int index = 1;
0N/A for (NetworkInterface nic : Collections.list(NetworkInterface.getNetworkInterfaces())) {
1703N/A // we should use only network interfaces with multicast support which are in "up" state
1720N/A if (!nic.isLoopback() && nic.supportsMulticast() && nic.isUp()) {
1720N/A NetIf netIf = NetIf.create(nic);
1720N/A
1720N/A // now determine what (if any) type of addresses are assigned to this interface
1720N/A for (InetAddress addr : Collections.list(nic.getInetAddresses())) {
2612N/A if (addr.isAnyLocalAddress())
2612N/A continue;
2612N/A
2612N/A System.out.println(" addr " + addr);
1720N/A if (addr instanceof Inet4Address) {
1720N/A netIf.ipv4Address(true);
1720N/A } else if (addr instanceof Inet6Address) {
1720N/A netIf.ipv6Address(true);
1720N/A }
1720N/A }
1720N/A if (netIf.ipv4Address() || netIf.ipv6Address()) {
1720N/A netIf.index(index++);
1720N/A netIfs.add(netIf);
1720N/A debug("Using: " + nic);
1720N/A }
1720N/A }
0N/A }
1720N/A if (netIfs.size() <= 1) {
0N/A System.out.println("Need 2 or more network interfaces to run. Bye.");
0N/A return;
0N/A }
0N/A
1720N/A // We will send packets to one ipv4, and one ipv6
0N/A // multicast group using each network interface :-
0N/A // 224.1.1.1 --|
1720N/A // ff02::1:1 --|--> using network interface #1
0N/A // 224.1.2.1 --|
1720N/A // ff02::1:2 --|--> using network interface #2
0N/A // and so on.
0N/A //
1720N/A for (NetIf netIf : netIfs) {
1720N/A int NetIfIndex = netIf.index();
1720N/A List<InetAddress> groups = new ArrayList<InetAddress>();
0N/A
1720N/A if (netIf.ipv4Address()) {
1720N/A InetAddress groupv4 = InetAddress.getByName("224.1." + NetIfIndex + ".1");
1720N/A groups.add(groupv4);
1720N/A }
1720N/A if (netIf.ipv6Address()) {
1720N/A InetAddress groupv6 = InetAddress.getByName("ff02::1:" + NetIfIndex);
1720N/A groups.add(groupv6);
1720N/A }
1720N/A
1720N/A debug("Adding " + groups + " groups for " + netIf.nic().getName());
1720N/A netIf.groups(groups);
1720N/A
1720N/A // use a separated thread to send to those 2 groups
1720N/A Thread sender = new Thread(new Sender(netIf,
1720N/A groups,
1720N/A PORT));
0N/A sender.setDaemon(true); // we want sender to stop when main thread exits
0N/A sender.start();
0N/A }
0N/A
0N/A // try to receive on each group, then check if the packet comes
0N/A // from the expected network interface
0N/A //
0N/A byte[] buf = new byte[1024];
1720N/A for (NetIf netIf : netIfs) {
1720N/A NetworkInterface nic = netIf.nic();
1720N/A for (InetAddress group : netIf.groups()) {
1720N/A MulticastSocket mcastsock = new MulticastSocket(PORT);
1720N/A mcastsock.setSoTimeout(5000); // 5 second
1720N/A DatagramPacket packet = new DatagramPacket(buf, 0, buf.length);
0N/A
1720N/A // the interface supports the IP multicast group
1720N/A debug("Joining " + group + " on " + nic.getName());
1720N/A mcastsock.joinGroup(new InetSocketAddress(group, PORT), nic);
1720N/A
1720N/A try {
1720N/A mcastsock.receive(packet);
1720N/A debug("received packet on " + packet.getAddress());
1720N/A } catch (Exception e) {
1720N/A // test failed if any exception
1720N/A throw new RuntimeException(e);
1720N/A }
0N/A
1720N/A // now check which network interface this packet comes from
1720N/A NetworkInterface from = NetworkInterface.getByInetAddress(packet.getAddress());
1720N/A NetworkInterface shouldbe = nic;
1720N/A if (!from.equals(shouldbe)) {
1720N/A System.out.println("Packets on group "
1720N/A + group + " should come from "
1720N/A + shouldbe.getName() + ", but came from "
1720N/A + from.getName());
1720N/A //throw new RuntimeException("Test failed.");
1720N/A }
1720N/A
1720N/A mcastsock.leaveGroup(new InetSocketAddress(group, PORT), nic);
0N/A }
1720N/A }
1720N/A }
0N/A
1720N/A private static boolean debug = true;
1720N/A
1720N/A static void debug(String message) {
1720N/A if (debug)
1720N/A System.out.println(message);
0N/A }
0N/A}
0N/A
0N/Aclass Sender implements Runnable {
1720N/A private NetIf netIf;
1720N/A private List<InetAddress> groups;
0N/A private int port;
0N/A
1720N/A public Sender(NetIf netIf,
1720N/A List<InetAddress> groups,
1720N/A int port) {
1720N/A this.netIf = netIf;
1720N/A this.groups = groups;
0N/A this.port = port;
0N/A }
0N/A
0N/A public void run() {
0N/A try {
0N/A MulticastSocket mcastsock = new MulticastSocket();
1720N/A mcastsock.setNetworkInterface(netIf.nic());
1720N/A List<DatagramPacket> packets = new LinkedList<DatagramPacket>();
0N/A
0N/A byte[] buf = "hello world".getBytes();
1720N/A for (InetAddress group : groups) {
1720N/A packets.add(new DatagramPacket(buf, buf.length, new InetSocketAddress(group, port)));
1720N/A }
0N/A
0N/A for (;;) {
1720N/A for (DatagramPacket packet : packets)
1720N/A mcastsock.send(packet);
0N/A
1720N/A Thread.sleep(1000); // sleep 1 second
0N/A }
0N/A } catch (Exception e) {
0N/A throw new RuntimeException(e);
0N/A }
0N/A }
0N/A}
1720N/A
1720N/A@SuppressWarnings("unchecked")
1720N/Aclass NetIf {
1720N/A private boolean ipv4Address; //false
1720N/A private boolean ipv6Address; //false
1720N/A private int index;
1720N/A List<InetAddress> groups = Collections.EMPTY_LIST;
1720N/A private final NetworkInterface nic;
1720N/A
1720N/A private NetIf(NetworkInterface nic) {
1720N/A this.nic = nic;
1720N/A }
1720N/A
1720N/A static NetIf create(NetworkInterface nic) {
1720N/A return new NetIf(nic);
1720N/A }
1720N/A
1720N/A NetworkInterface nic() {
1720N/A return nic;
1720N/A }
1720N/A
1720N/A boolean ipv4Address() {
1720N/A return ipv4Address;
1720N/A }
1720N/A
1720N/A void ipv4Address(boolean ipv4Address) {
1720N/A this.ipv4Address = ipv4Address;
1720N/A }
1720N/A
1720N/A boolean ipv6Address() {
1720N/A return ipv6Address;
1720N/A }
1720N/A
1720N/A void ipv6Address(boolean ipv6Address) {
1720N/A this.ipv6Address = ipv6Address;
1720N/A }
1720N/A
1720N/A int index() {
1720N/A return index;
1720N/A }
1720N/A
1720N/A void index(int index) {
1720N/A this.index = index;
1720N/A }
1720N/A
1720N/A List<InetAddress> groups() {
1720N/A return groups;
1720N/A }
1720N/A
1720N/A void groups(List<InetAddress> groups) {
1720N/A this.groups = groups;
1720N/A }
1720N/A}
1720N/A