Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/MulticastSocket/SetOutgoingIf.java
38821 views
/*1* Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 474217726* @summary Re-test IPv6 (and specifically MulticastSocket) with latest Linux & USAGI code27*/28import java.net.*;29import java.util.*;303132public class SetOutgoingIf {33private static int PORT = 9001;34private static String osname;3536static boolean isWindows() {37if (osname == null)38osname = System.getProperty("os.name");39return osname.contains("Windows");40}4142private static boolean hasIPv6() throws Exception {43List<NetworkInterface> nics = Collections.list(44NetworkInterface.getNetworkInterfaces());45for (NetworkInterface nic : nics) {46List<InetAddress> addrs = Collections.list(nic.getInetAddresses());47for (InetAddress addr : addrs) {48if (addr instanceof Inet6Address)49return true;50}51}5253return false;54}5556public static void main(String[] args) throws Exception {57if (isWindows()) {58System.out.println("The test only run on non-Windows OS. Bye.");59return;60}6162if (!hasIPv6()) {63System.out.println("No IPv6 available. Bye.");64return;65}6667// We need 2 or more network interfaces to run the test68//69List<NetIf> netIfs = new ArrayList<NetIf>();70int index = 1;71for (NetworkInterface nic : Collections.list(NetworkInterface.getNetworkInterfaces())) {72// we should use only network interfaces with multicast support which are in "up" state73if (!nic.isLoopback() && nic.supportsMulticast() && nic.isUp()) {74NetIf netIf = NetIf.create(nic);7576// now determine what (if any) type of addresses are assigned to this interface77for (InetAddress addr : Collections.list(nic.getInetAddresses())) {78if (addr.isAnyLocalAddress())79continue;8081System.out.println(" addr " + addr);82if (addr instanceof Inet4Address) {83netIf.ipv4Address(true);84} else if (addr instanceof Inet6Address) {85netIf.ipv6Address(true);86}87}88if (netIf.ipv4Address() || netIf.ipv6Address()) {89netIf.index(index++);90netIfs.add(netIf);91debug("Using: " + nic);92}93}94}95if (netIfs.size() <= 1) {96System.out.println("Need 2 or more network interfaces to run. Bye.");97return;98}99100// We will send packets to one ipv4, and one ipv6101// multicast group using each network interface :-102// 224.1.1.1 --|103// ff02::1:1 --|--> using network interface #1104// 224.1.2.1 --|105// ff02::1:2 --|--> using network interface #2106// and so on.107//108for (NetIf netIf : netIfs) {109int NetIfIndex = netIf.index();110List<InetAddress> groups = new ArrayList<InetAddress>();111112if (netIf.ipv4Address()) {113InetAddress groupv4 = InetAddress.getByName("224.1." + NetIfIndex + ".1");114groups.add(groupv4);115}116if (netIf.ipv6Address()) {117InetAddress groupv6 = InetAddress.getByName("ff02::1:" + NetIfIndex);118groups.add(groupv6);119}120121debug("Adding " + groups + " groups for " + netIf.nic().getName());122netIf.groups(groups);123124// use a separated thread to send to those 2 groups125Thread sender = new Thread(new Sender(netIf,126groups,127PORT));128sender.setDaemon(true); // we want sender to stop when main thread exits129sender.start();130}131132// try to receive on each group, then check if the packet comes133// from the expected network interface134//135byte[] buf = new byte[1024];136for (NetIf netIf : netIfs) {137NetworkInterface nic = netIf.nic();138for (InetAddress group : netIf.groups()) {139MulticastSocket mcastsock = new MulticastSocket(PORT);140mcastsock.setSoTimeout(5000); // 5 second141DatagramPacket packet = new DatagramPacket(buf, 0, buf.length);142143// the interface supports the IP multicast group144debug("Joining " + group + " on " + nic.getName());145mcastsock.joinGroup(new InetSocketAddress(group, PORT), nic);146147try {148mcastsock.receive(packet);149debug("received packet on " + packet.getAddress());150} catch (Exception e) {151// test failed if any exception152throw new RuntimeException(e);153}154155// now check which network interface this packet comes from156NetworkInterface from = NetworkInterface.getByInetAddress(packet.getAddress());157NetworkInterface shouldbe = nic;158if (!from.equals(shouldbe)) {159System.out.println("Packets on group "160+ group + " should come from "161+ shouldbe.getName() + ", but came from "162+ from.getName());163//throw new RuntimeException("Test failed.");164}165166mcastsock.leaveGroup(new InetSocketAddress(group, PORT), nic);167}168}169}170171private static boolean debug = true;172173static void debug(String message) {174if (debug)175System.out.println(message);176}177}178179class Sender implements Runnable {180private NetIf netIf;181private List<InetAddress> groups;182private int port;183184public Sender(NetIf netIf,185List<InetAddress> groups,186int port) {187this.netIf = netIf;188this.groups = groups;189this.port = port;190}191192public void run() {193try {194MulticastSocket mcastsock = new MulticastSocket();195mcastsock.setNetworkInterface(netIf.nic());196List<DatagramPacket> packets = new LinkedList<DatagramPacket>();197198byte[] buf = "hello world".getBytes();199for (InetAddress group : groups) {200packets.add(new DatagramPacket(buf, buf.length, new InetSocketAddress(group, port)));201}202203for (;;) {204for (DatagramPacket packet : packets)205mcastsock.send(packet);206207Thread.sleep(1000); // sleep 1 second208}209} catch (Exception e) {210throw new RuntimeException(e);211}212}213}214215@SuppressWarnings("unchecked")216class NetIf {217private boolean ipv4Address; //false218private boolean ipv6Address; //false219private int index;220List<InetAddress> groups = Collections.EMPTY_LIST;221private final NetworkInterface nic;222223private NetIf(NetworkInterface nic) {224this.nic = nic;225}226227static NetIf create(NetworkInterface nic) {228return new NetIf(nic);229}230231NetworkInterface nic() {232return nic;233}234235boolean ipv4Address() {236return ipv4Address;237}238239void ipv4Address(boolean ipv4Address) {240this.ipv4Address = ipv4Address;241}242243boolean ipv6Address() {244return ipv6Address;245}246247void ipv6Address(boolean ipv6Address) {248this.ipv6Address = ipv6Address;249}250251int index() {252return index;253}254255void index(int index) {256this.index = index;257}258259List<InetAddress> groups() {260return groups;261}262263void groups(List<InetAddress> groups) {264this.groups = groups;265}266}267268269270