blob: d793f19621570b14a70a51dba17a83e898a00001 [file] [log] [blame]
duke6e45e102007-12-01 00:00:00 +00001/*
2 * Copyright 2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
26 * @bug 4742177
27 * @summary Re-test IPv6 (and specifically MulticastSocket) with latest Linux & USAGI code
28 */
29import java.net.*;
30import java.util.concurrent.*;
31import java.util.*;
32
33
34public class SetOutgoingIf {
35 private static int PORT = 9001;
36 private static String osname;
37
38 static boolean isWindows() {
39 if (osname == null)
40 osname = System.getProperty("os.name");
41 return osname.contains("Windows");
42 }
43
44 private static boolean hasIPv6() throws Exception {
45 List<NetworkInterface> nics = Collections.list(
46 NetworkInterface.getNetworkInterfaces());
47 for (NetworkInterface nic : nics) {
48 List<InetAddress> addrs = Collections.list(nic.getInetAddresses());
49 for (InetAddress addr : addrs) {
50 if (addr instanceof Inet6Address)
51 return true;
52 }
53 }
54
55 return false;
56 }
57
58 public static void main(String[] args) throws Exception {
59 if (isWindows()) {
60 System.out.println("The test only run on non-Windows OS. Bye.");
61 return;
62 }
63
64 if (!hasIPv6()) {
65 System.out.println("No IPv6 available. Bye.");
66 return;
67 }
68
69 // We need 2 or more network interfaces to run the test
70 //
71 List<NetworkInterface> nics = new ArrayList<NetworkInterface>();
72 for (NetworkInterface nic : Collections.list(NetworkInterface.getNetworkInterfaces())) {
ptisnovs535d6aa2009-09-30 11:49:10 +020073 // we should use only network interfaces with multicast support which are in "up" state
74 if (!nic.isLoopback() && nic.supportsMulticast() && nic.isUp())
duke6e45e102007-12-01 00:00:00 +000075 nics.add(nic);
76 }
77 if (nics.size() <= 1) {
78 System.out.println("Need 2 or more network interfaces to run. Bye.");
79 return;
80 }
81
82 // We will send packets to one ipv4, one ipv4-mapped, and one ipv6
83 // multicast group using each network interface :-
84 // 224.1.1.1 --|
85 // ::ffff:224.1.1.2 -----> using network interface #1
86 // ff02::1:1 --|
87 // 224.1.2.1 --|
88 // ::ffff:224.1.2.2 -----> using network interface #2
89 // ff02::1:2 --|
90 // and so on.
91 //
92 List<InetAddress> groups = new ArrayList<InetAddress>();
93 for (int i = 0; i < nics.size(); i++) {
94 InetAddress groupv4 = InetAddress.getByName("224.1." + (i+1) + ".1");
95 InetAddress groupv4mapped = InetAddress.getByName("::ffff:224.1." + (i+1) + ".2");
96 InetAddress groupv6 = InetAddress.getByName("ff02::1:" + (i+1));
97 groups.add(groupv4);
98 groups.add(groupv4mapped);
99 groups.add(groupv6);
100
101 // use a separated thread to send to those 3 groups
102 Thread sender = new Thread(new Sender(nics.get(i), groupv4, groupv4mapped, groupv6, PORT));
103 sender.setDaemon(true); // we want sender to stop when main thread exits
104 sender.start();
105 }
106
107 // try to receive on each group, then check if the packet comes
108 // from the expected network interface
109 //
110 byte[] buf = new byte[1024];
111 for (InetAddress group : groups) {
112 MulticastSocket mcastsock = new MulticastSocket(PORT);
113 mcastsock.setSoTimeout(5000); // 5 second
114 DatagramPacket packet = new DatagramPacket(buf, 0, buf.length);
115
116 mcastsock.joinGroup(new InetSocketAddress(group, PORT), nics.get(groups.indexOf(group) / 3));
117
118 try {
119 mcastsock.receive(packet);
120 } catch (Exception e) {
121 // test failed if any exception
122 throw new RuntimeException(e);
123 }
124
125 // now check which network interface this packet comes from
126 NetworkInterface from = NetworkInterface.getByInetAddress(packet.getAddress());
127 NetworkInterface shouldbe = nics.get(groups.indexOf(group) / 3);
128 if (!from.equals(shouldbe)) {
129 System.out.println("Packets on group "
130 + group + " should come from "
131 + shouldbe.getName() + ", but came from "
132 + from.getName());
133 //throw new RuntimeException("Test failed.");
134 }
135
136 mcastsock.leaveGroup(new InetSocketAddress(group, PORT), nics.get(groups.indexOf(group) / 3));
137 }
138 }
139}
140
141class Sender implements Runnable {
142 private NetworkInterface nic;
143 private InetAddress group1;
144 private InetAddress group2;
145 private InetAddress group3;
146 private int port;
147
148 public Sender(NetworkInterface nic,
149 InetAddress groupv4, InetAddress groupv4mapped, InetAddress groupv6,
150 int port) {
151 this.nic = nic;
152 group1 = groupv4;
153 group2 = groupv4mapped;
154 group3 = groupv6;
155 this.port = port;
156 }
157
158 public void run() {
159 try {
160 MulticastSocket mcastsock = new MulticastSocket();
161 mcastsock.setNetworkInterface(nic);
162
163 byte[] buf = "hello world".getBytes();
164 DatagramPacket packet1 = new DatagramPacket(buf, buf.length,
165 new InetSocketAddress(group1, port));
166 DatagramPacket packet2 = new DatagramPacket(buf, buf.length,
167 new InetSocketAddress(group2, port));
168 DatagramPacket packet3 = new DatagramPacket(buf, buf.length,
169 new InetSocketAddress(group3, port));
170
171 for (;;) {
172 mcastsock.send(packet1);
173 mcastsock.send(packet2);
174 mcastsock.send(packet3);
175
176 Thread.currentThread().sleep(1000); // sleep 1 second
177 }
178 } catch (Exception e) {
179 throw new RuntimeException(e);
180 }
181 }
182}