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