test/jdk/java/net/MulticastSocket/SetOutgoingIf.java
author phh
Sat, 30 Nov 2019 14:33:05 -0800
changeset 59330 5b96c12f909d
parent 47216 71c04702a3d5
permissions -rw-r--r--
8234541: C1 emits an empty message when it inlines successfully Summary: Use "inline" as the message when successfull Reviewed-by: thartmann, mdoerr Contributed-by: navy.xliu@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
45122
8cfb711b103b 7155591: test/java/net/MulticastSocket/SetOutgoingIf.java fails on macOS
msheppar
parents: 30810
diff changeset
     2
 * Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3962
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3962
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3962
diff changeset
    21
 * questions.
2
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.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public class SetOutgoingIf {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    private static int PORT = 9001;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    private static String osname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    static boolean isWindows() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        if (osname == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
            osname = System.getProperty("os.name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        return osname.contains("Windows");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
45122
8cfb711b103b 7155591: test/java/net/MulticastSocket/SetOutgoingIf.java fails on macOS
msheppar
parents: 30810
diff changeset
    43
    static boolean isMacOS() {
8cfb711b103b 7155591: test/java/net/MulticastSocket/SetOutgoingIf.java fails on macOS
msheppar
parents: 30810
diff changeset
    44
        return System.getProperty("os.name").contains("OS X");
8cfb711b103b 7155591: test/java/net/MulticastSocket/SetOutgoingIf.java fails on macOS
msheppar
parents: 30810
diff changeset
    45
    }
8cfb711b103b 7155591: test/java/net/MulticastSocket/SetOutgoingIf.java fails on macOS
msheppar
parents: 30810
diff changeset
    46
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static boolean hasIPv6() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        List<NetworkInterface> nics = Collections.list(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                                        NetworkInterface.getNetworkInterfaces());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        for (NetworkInterface nic : nics) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            List<InetAddress> addrs = Collections.list(nic.getInetAddresses());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            for (InetAddress addr : addrs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                if (addr instanceof Inet6Address)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        if (isWindows()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            System.out.println("The test only run on non-Windows OS. Bye.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        if (!hasIPv6()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            System.out.println("No IPv6 available. Bye.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        // We need 2 or more network interfaces to run the test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        //
3962
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
    74
        List<NetIf> netIfs = new ArrayList<NetIf>();
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
    75
        int index = 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        for (NetworkInterface nic : Collections.list(NetworkInterface.getNetworkInterfaces())) {
3945
391194a1b78a 6884837: JTReg test SetOutgoingIf is not correct
ptisnovs
parents: 2
diff changeset
    77
            // we should use only network interfaces with multicast support which are in "up" state
45122
8cfb711b103b 7155591: test/java/net/MulticastSocket/SetOutgoingIf.java fails on macOS
msheppar
parents: 30810
diff changeset
    78
            if (!nic.isLoopback() && nic.supportsMulticast() && nic.isUp() && !isTestExcludedInterface(nic)) {
3962
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
    79
                NetIf netIf = NetIf.create(nic);
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
    80
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
    81
                // now determine what (if any) type of addresses are assigned to this interface
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
    82
                for (InetAddress addr : Collections.list(nic.getInetAddresses())) {
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    83
                    if (addr.isAnyLocalAddress())
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    84
                        continue;
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    85
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    86
                    System.out.println("    addr " + addr);
3962
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
    87
                    if (addr instanceof Inet4Address) {
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
    88
                        netIf.ipv4Address(true);
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
    89
                    } else if (addr instanceof Inet6Address) {
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
    90
                        netIf.ipv6Address(true);
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
    91
                    }
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
    92
                }
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
    93
                if (netIf.ipv4Address() || netIf.ipv6Address()) {
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
    94
                    netIf.index(index++);
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
    95
                    netIfs.add(netIf);
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
    96
                    debug("Using: " + nic);
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
    97
                }
45122
8cfb711b103b 7155591: test/java/net/MulticastSocket/SetOutgoingIf.java fails on macOS
msheppar
parents: 30810
diff changeset
    98
            } else {
8cfb711b103b 7155591: test/java/net/MulticastSocket/SetOutgoingIf.java fails on macOS
msheppar
parents: 30810
diff changeset
    99
                System.out.println("Ignore NetworkInterface nic == " + nic);
3962
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   100
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
3962
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   102
        if (netIfs.size() <= 1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            System.out.println("Need 2 or more network interfaces to run. Bye.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
3962
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   107
        // We will send packets to one ipv4, and one ipv6
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        // multicast group using each network interface :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        //      224.1.1.1        --|
3962
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   110
        //      ff02::1:1        --|--> using network interface #1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        //      224.1.2.1        --|
3962
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   112
        //      ff02::1:2        --|--> using network interface #2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        // and so on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        //
3962
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   115
        for (NetIf netIf : netIfs) {
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   116
            int NetIfIndex = netIf.index();
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   117
            List<InetAddress> groups = new ArrayList<InetAddress>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
3962
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   119
            if (netIf.ipv4Address()) {
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   120
                InetAddress groupv4 = InetAddress.getByName("224.1." + NetIfIndex + ".1");
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   121
                groups.add(groupv4);
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   122
            }
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   123
            if (netIf.ipv6Address()) {
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   124
                InetAddress groupv6 = InetAddress.getByName("ff02::1:" + NetIfIndex);
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   125
                groups.add(groupv6);
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   126
            }
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   127
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   128
            debug("Adding " + groups + " groups for " + netIf.nic().getName());
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   129
            netIf.groups(groups);
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   130
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   131
            // use a separated thread to send to those 2 groups
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   132
            Thread sender = new Thread(new Sender(netIf,
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   133
                                                  groups,
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   134
                                                  PORT));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            sender.setDaemon(true); // we want sender to stop when main thread exits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            sender.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        // try to receive on each group, then check if the packet comes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        // from the expected network interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        byte[] buf = new byte[1024];
3962
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   143
        for (NetIf netIf : netIfs) {
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   144
            NetworkInterface nic = netIf.nic();
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   145
            for (InetAddress group : netIf.groups()) {
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   146
                MulticastSocket mcastsock = new MulticastSocket(PORT);
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   147
                mcastsock.setSoTimeout(5000);   // 5 second
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   148
                DatagramPacket packet = new DatagramPacket(buf, 0, buf.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
3962
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   150
                // the interface supports the IP multicast group
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   151
                debug("Joining " + group + " on " + nic.getName());
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   152
                mcastsock.joinGroup(new InetSocketAddress(group, PORT), nic);
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   153
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   154
                try {
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   155
                    mcastsock.receive(packet);
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   156
                    debug("received packet on " + packet.getAddress());
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   157
                } catch (Exception e) {
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   158
                    // test failed if any exception
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   159
                    throw new RuntimeException(e);
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   160
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
3962
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   162
                // now check which network interface this packet comes from
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   163
                NetworkInterface from = NetworkInterface.getByInetAddress(packet.getAddress());
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   164
                NetworkInterface shouldbe = nic;
30810
cbb29036f026 8077377: java/net/MulticastSocket/SetOutgoingIf.java fails intermittently with NullPointerException
msheppar
parents: 7668
diff changeset
   165
                if (from != null) {
cbb29036f026 8077377: java/net/MulticastSocket/SetOutgoingIf.java fails intermittently with NullPointerException
msheppar
parents: 7668
diff changeset
   166
                    if (!from.equals(shouldbe)) {
cbb29036f026 8077377: java/net/MulticastSocket/SetOutgoingIf.java fails intermittently with NullPointerException
msheppar
parents: 7668
diff changeset
   167
                        System.out.println("Packets on group "
3962
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   168
                                        + group + " should come from "
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   169
                                        + shouldbe.getName() + ", but came from "
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   170
                                        + from.getName());
30810
cbb29036f026 8077377: java/net/MulticastSocket/SetOutgoingIf.java fails intermittently with NullPointerException
msheppar
parents: 7668
diff changeset
   171
                    }
3962
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   172
                }
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   173
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   174
                mcastsock.leaveGroup(new InetSocketAddress(group, PORT), nic);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            }
3962
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   176
        }
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   177
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
45122
8cfb711b103b 7155591: test/java/net/MulticastSocket/SetOutgoingIf.java fails on macOS
msheppar
parents: 30810
diff changeset
   179
    private static boolean isTestExcludedInterface(NetworkInterface nif) {
8cfb711b103b 7155591: test/java/net/MulticastSocket/SetOutgoingIf.java fails on macOS
msheppar
parents: 30810
diff changeset
   180
        if (isMacOS() && nif.getName().contains("awdl"))
8cfb711b103b 7155591: test/java/net/MulticastSocket/SetOutgoingIf.java fails on macOS
msheppar
parents: 30810
diff changeset
   181
            return true;
8cfb711b103b 7155591: test/java/net/MulticastSocket/SetOutgoingIf.java fails on macOS
msheppar
parents: 30810
diff changeset
   182
        String dName = nif.getDisplayName();
8cfb711b103b 7155591: test/java/net/MulticastSocket/SetOutgoingIf.java fails on macOS
msheppar
parents: 30810
diff changeset
   183
        if (isWindows() && dName != null && dName.contains("Teredo"))
8cfb711b103b 7155591: test/java/net/MulticastSocket/SetOutgoingIf.java fails on macOS
msheppar
parents: 30810
diff changeset
   184
            return true;
8cfb711b103b 7155591: test/java/net/MulticastSocket/SetOutgoingIf.java fails on macOS
msheppar
parents: 30810
diff changeset
   185
        return false;
8cfb711b103b 7155591: test/java/net/MulticastSocket/SetOutgoingIf.java fails on macOS
msheppar
parents: 30810
diff changeset
   186
    }
8cfb711b103b 7155591: test/java/net/MulticastSocket/SetOutgoingIf.java fails on macOS
msheppar
parents: 30810
diff changeset
   187
3962
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   188
    private static boolean debug = true;
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   189
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   190
    static void debug(String message) {
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   191
        if (debug)
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   192
            System.out.println(message);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
class Sender implements Runnable {
3962
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   197
    private NetIf netIf;
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   198
    private List<InetAddress> groups;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    private int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
3962
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   201
    public Sender(NetIf netIf,
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   202
                  List<InetAddress> groups,
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   203
                  int port) {
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   204
        this.netIf = netIf;
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   205
        this.groups = groups;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        this.port = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            MulticastSocket mcastsock = new MulticastSocket();
3962
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   212
            mcastsock.setNetworkInterface(netIf.nic());
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   213
            List<DatagramPacket> packets = new LinkedList<DatagramPacket>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            byte[] buf = "hello world".getBytes();
3962
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   216
            for (InetAddress group : groups) {
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   217
                packets.add(new DatagramPacket(buf, buf.length, new InetSocketAddress(group, port)));
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   218
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            for (;;) {
3962
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   221
                for (DatagramPacket packet : packets)
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   222
                    mcastsock.send(packet);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
3962
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   224
                Thread.sleep(1000);   // sleep 1 second
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
}
3962
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   231
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   232
@SuppressWarnings("unchecked")
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   233
class NetIf {
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   234
    private boolean ipv4Address; //false
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   235
    private boolean ipv6Address; //false
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   236
    private int index;
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   237
    List<InetAddress> groups = Collections.EMPTY_LIST;
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   238
    private final NetworkInterface nic;
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   239
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   240
    private NetIf(NetworkInterface nic) {
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   241
        this.nic = nic;
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   242
    }
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   243
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   244
    static NetIf create(NetworkInterface nic) {
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   245
        return new NetIf(nic);
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   246
    }
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   247
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   248
    NetworkInterface nic() {
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   249
        return nic;
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   250
    }
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   251
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   252
    boolean ipv4Address() {
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   253
        return ipv4Address;
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   254
    }
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   255
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   256
    void ipv4Address(boolean ipv4Address) {
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   257
        this.ipv4Address = ipv4Address;
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   258
    }
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   259
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   260
    boolean ipv6Address() {
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   261
        return ipv6Address;
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   262
    }
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   263
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   264
    void ipv6Address(boolean ipv6Address) {
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   265
        this.ipv6Address = ipv6Address;
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   266
    }
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   267
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   268
    int index() {
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   269
        return index;
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   270
    }
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   271
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   272
    void index(int index) {
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   273
        this.index = index;
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   274
    }
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   275
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   276
    List<InetAddress> groups() {
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   277
        return groups;
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   278
    }
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   279
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   280
    void groups(List<InetAddress> groups) {
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   281
        this.groups = groups;
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   282
    }
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   283
}
cf907ac8ca4c 6887364: SetOutgoingIf.java fails if run on multihomed machine without PIv6 on all interfaces
chegar
parents: 3945
diff changeset
   284