Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/MulticastSocket/SetOutgoingIf.java
38821 views
1
/*
2
* Copyright (c) 2007, 2010, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/*
25
* @test
26
* @bug 4742177
27
* @summary Re-test IPv6 (and specifically MulticastSocket) with latest Linux & USAGI code
28
*/
29
import java.net.*;
30
import java.util.*;
31
32
33
public class SetOutgoingIf {
34
private static int PORT = 9001;
35
private static String osname;
36
37
static boolean isWindows() {
38
if (osname == null)
39
osname = System.getProperty("os.name");
40
return osname.contains("Windows");
41
}
42
43
private static boolean hasIPv6() throws Exception {
44
List<NetworkInterface> nics = Collections.list(
45
NetworkInterface.getNetworkInterfaces());
46
for (NetworkInterface nic : nics) {
47
List<InetAddress> addrs = Collections.list(nic.getInetAddresses());
48
for (InetAddress addr : addrs) {
49
if (addr instanceof Inet6Address)
50
return true;
51
}
52
}
53
54
return false;
55
}
56
57
public static void main(String[] args) throws Exception {
58
if (isWindows()) {
59
System.out.println("The test only run on non-Windows OS. Bye.");
60
return;
61
}
62
63
if (!hasIPv6()) {
64
System.out.println("No IPv6 available. Bye.");
65
return;
66
}
67
68
// We need 2 or more network interfaces to run the test
69
//
70
List<NetIf> netIfs = new ArrayList<NetIf>();
71
int index = 1;
72
for (NetworkInterface nic : Collections.list(NetworkInterface.getNetworkInterfaces())) {
73
// we should use only network interfaces with multicast support which are in "up" state
74
if (!nic.isLoopback() && nic.supportsMulticast() && nic.isUp()) {
75
NetIf netIf = NetIf.create(nic);
76
77
// now determine what (if any) type of addresses are assigned to this interface
78
for (InetAddress addr : Collections.list(nic.getInetAddresses())) {
79
if (addr.isAnyLocalAddress())
80
continue;
81
82
System.out.println(" addr " + addr);
83
if (addr instanceof Inet4Address) {
84
netIf.ipv4Address(true);
85
} else if (addr instanceof Inet6Address) {
86
netIf.ipv6Address(true);
87
}
88
}
89
if (netIf.ipv4Address() || netIf.ipv6Address()) {
90
netIf.index(index++);
91
netIfs.add(netIf);
92
debug("Using: " + nic);
93
}
94
}
95
}
96
if (netIfs.size() <= 1) {
97
System.out.println("Need 2 or more network interfaces to run. Bye.");
98
return;
99
}
100
101
// We will send packets to one ipv4, and one ipv6
102
// multicast group using each network interface :-
103
// 224.1.1.1 --|
104
// ff02::1:1 --|--> using network interface #1
105
// 224.1.2.1 --|
106
// ff02::1:2 --|--> using network interface #2
107
// and so on.
108
//
109
for (NetIf netIf : netIfs) {
110
int NetIfIndex = netIf.index();
111
List<InetAddress> groups = new ArrayList<InetAddress>();
112
113
if (netIf.ipv4Address()) {
114
InetAddress groupv4 = InetAddress.getByName("224.1." + NetIfIndex + ".1");
115
groups.add(groupv4);
116
}
117
if (netIf.ipv6Address()) {
118
InetAddress groupv6 = InetAddress.getByName("ff02::1:" + NetIfIndex);
119
groups.add(groupv6);
120
}
121
122
debug("Adding " + groups + " groups for " + netIf.nic().getName());
123
netIf.groups(groups);
124
125
// use a separated thread to send to those 2 groups
126
Thread sender = new Thread(new Sender(netIf,
127
groups,
128
PORT));
129
sender.setDaemon(true); // we want sender to stop when main thread exits
130
sender.start();
131
}
132
133
// try to receive on each group, then check if the packet comes
134
// from the expected network interface
135
//
136
byte[] buf = new byte[1024];
137
for (NetIf netIf : netIfs) {
138
NetworkInterface nic = netIf.nic();
139
for (InetAddress group : netIf.groups()) {
140
MulticastSocket mcastsock = new MulticastSocket(PORT);
141
mcastsock.setSoTimeout(5000); // 5 second
142
DatagramPacket packet = new DatagramPacket(buf, 0, buf.length);
143
144
// the interface supports the IP multicast group
145
debug("Joining " + group + " on " + nic.getName());
146
mcastsock.joinGroup(new InetSocketAddress(group, PORT), nic);
147
148
try {
149
mcastsock.receive(packet);
150
debug("received packet on " + packet.getAddress());
151
} catch (Exception e) {
152
// test failed if any exception
153
throw new RuntimeException(e);
154
}
155
156
// now check which network interface this packet comes from
157
NetworkInterface from = NetworkInterface.getByInetAddress(packet.getAddress());
158
NetworkInterface shouldbe = nic;
159
if (!from.equals(shouldbe)) {
160
System.out.println("Packets on group "
161
+ group + " should come from "
162
+ shouldbe.getName() + ", but came from "
163
+ from.getName());
164
//throw new RuntimeException("Test failed.");
165
}
166
167
mcastsock.leaveGroup(new InetSocketAddress(group, PORT), nic);
168
}
169
}
170
}
171
172
private static boolean debug = true;
173
174
static void debug(String message) {
175
if (debug)
176
System.out.println(message);
177
}
178
}
179
180
class Sender implements Runnable {
181
private NetIf netIf;
182
private List<InetAddress> groups;
183
private int port;
184
185
public Sender(NetIf netIf,
186
List<InetAddress> groups,
187
int port) {
188
this.netIf = netIf;
189
this.groups = groups;
190
this.port = port;
191
}
192
193
public void run() {
194
try {
195
MulticastSocket mcastsock = new MulticastSocket();
196
mcastsock.setNetworkInterface(netIf.nic());
197
List<DatagramPacket> packets = new LinkedList<DatagramPacket>();
198
199
byte[] buf = "hello world".getBytes();
200
for (InetAddress group : groups) {
201
packets.add(new DatagramPacket(buf, buf.length, new InetSocketAddress(group, port)));
202
}
203
204
for (;;) {
205
for (DatagramPacket packet : packets)
206
mcastsock.send(packet);
207
208
Thread.sleep(1000); // sleep 1 second
209
}
210
} catch (Exception e) {
211
throw new RuntimeException(e);
212
}
213
}
214
}
215
216
@SuppressWarnings("unchecked")
217
class NetIf {
218
private boolean ipv4Address; //false
219
private boolean ipv6Address; //false
220
private int index;
221
List<InetAddress> groups = Collections.EMPTY_LIST;
222
private final NetworkInterface nic;
223
224
private NetIf(NetworkInterface nic) {
225
this.nic = nic;
226
}
227
228
static NetIf create(NetworkInterface nic) {
229
return new NetIf(nic);
230
}
231
232
NetworkInterface nic() {
233
return nic;
234
}
235
236
boolean ipv4Address() {
237
return ipv4Address;
238
}
239
240
void ipv4Address(boolean ipv4Address) {
241
this.ipv4Address = ipv4Address;
242
}
243
244
boolean ipv6Address() {
245
return ipv6Address;
246
}
247
248
void ipv6Address(boolean ipv6Address) {
249
this.ipv6Address = ipv6Address;
250
}
251
252
int index() {
253
return index;
254
}
255
256
void index(int index) {
257
this.index = index;
258
}
259
260
List<InetAddress> groups() {
261
return groups;
262
}
263
264
void groups(List<InetAddress> groups) {
265
this.groups = groups;
266
}
267
}
268
269
270