Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/MulticastSocket/Promiscuous.java
41152 views
1
/*
2
* Copyright (c) 2013, 2020, 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
/* @test
25
* @bug 8014499 8219804
26
* @library /test/lib
27
* @summary Test for interference when two sockets are bound to the same
28
* port but joined to different multicast groups
29
* @run main/othervm -Djdk.net.usePlainDatagramSocketImpl Promiscuous
30
* @run main Promiscuous
31
* @run main/othervm -Djava.net.preferIPv4Stack=true Promiscuous
32
*/
33
34
import java.io.IOException;
35
import static java.lang.System.out;
36
37
import java.io.UncheckedIOException;
38
import java.net.*;
39
40
import jdk.test.lib.NetworkConfiguration;
41
import jdk.test.lib.net.IPSupport;
42
43
public class Promiscuous {
44
45
static final int TIMEOUT = 5 * 1000; // 5 secs
46
static int id = 1000;
47
48
static void receive(MulticastSocket mc, boolean datagramExpected, int id)
49
throws IOException
50
{
51
byte[] ba = new byte[100];
52
DatagramPacket p;
53
try {
54
String data = null;
55
while (true) {
56
p = new DatagramPacket(ba, ba.length);
57
mc.receive(p);
58
data = new String(p.getData(), 0, p.getLength(), "UTF-8");
59
if (data.length() > UUID.length() && data.startsWith(UUID)) {
60
data = data.substring(UUID.length());
61
break;
62
}
63
logUnexpected(p);
64
}
65
int recvId = Integer.parseInt(data);
66
if (datagramExpected) {
67
if (recvId != id)
68
throw new RuntimeException("Unexpected id, got " + recvId
69
+ ", expected: " + id);
70
out.printf("Received message as expected, %s\n", p.getAddress());
71
} else {
72
throw new RuntimeException("Unexpected message received, "
73
+ p.getAddress());
74
}
75
} catch (SocketTimeoutException e) {
76
if (datagramExpected)
77
throw new RuntimeException(mc.getLocalSocketAddress()
78
+ ": Expected message not received, "
79
+ e.getMessage());
80
else
81
out.printf("Message not received, as expected\n");
82
}
83
}
84
85
static void logUnexpected(DatagramPacket p) {
86
byte[] ba = p.getData();
87
System.out.printf("Unexpected packet: length: %d. First three bytes: %d, %d, %d\n",
88
p.getLength(), ba[0], ba[1], ba[2]);
89
}
90
91
static final String UUID; // process-id : currentTimeMillis
92
93
static {
94
String s1 = Long.toString(ProcessHandle.current().pid());
95
String s2 = Long.toString(System.currentTimeMillis());
96
UUID = "<" + s1 + s2 + ">";
97
}
98
99
static SocketAddress toSocketAddress(InetAddress group) {
100
return new InetSocketAddress(group, 0);
101
}
102
103
static void test(InetAddress group1, InetAddress group2)
104
throws IOException
105
{
106
try (MulticastSocket mc1 = new MulticastSocket();
107
MulticastSocket mc2 = new MulticastSocket(mc1.getLocalPort());
108
DatagramSocket ds = new DatagramSocket()) {
109
final int port = mc1.getLocalPort();
110
out.printf("Using port: %d\n", port);
111
112
mc1.setSoTimeout(TIMEOUT);
113
mc2.setSoTimeout(TIMEOUT);
114
int nextId = id;
115
byte[] msg = (UUID + Integer.toString(nextId)).getBytes("UTF-8");
116
DatagramPacket p = new DatagramPacket(msg, msg.length);
117
p.setAddress(group1);
118
p.setPort(port);
119
120
// join groups on all network interfaces
121
NetworkConfiguration.probe()
122
.ip4MulticastInterfaces(false)
123
.forEach((nic) -> {
124
try {
125
mc1.joinGroup(toSocketAddress(group1), nic);
126
out.printf("mc1 joined the MC group on %s: %s\n",
127
nic.getDisplayName(), group1);
128
mc2.joinGroup(toSocketAddress(group2), nic);
129
out.printf("mc2 joined the MC group on %s: %s\n",
130
nic.getDisplayName(), group2);
131
} catch (IOException io) {
132
throw new UncheckedIOException(io);
133
}
134
});
135
136
out.printf("Sending datagram to: %s/%d\n", group1, port);
137
ds.send(p);
138
139
// the packet should be received by mc1 only
140
receive(mc1, true, nextId);
141
receive(mc2, false, 0);
142
143
nextId = ++id;
144
msg = (UUID + Integer.toString(nextId)).getBytes("UTF-8");
145
p = new DatagramPacket(msg, msg.length);
146
p.setAddress(group2);
147
p.setPort(port);
148
149
out.printf("Sending datagram to: %s/%d\n", group2, port);
150
ds.send(p);
151
152
// the packet should be received by mc2 only
153
receive(mc2, true, nextId);
154
receive(mc1, false, 0);
155
156
// leave groups on all network interfaces
157
NetworkConfiguration.probe()
158
.ip4MulticastInterfaces(false)
159
.forEach((nic) -> {
160
try {
161
mc1.leaveGroup(toSocketAddress(group1), nic);
162
out.printf("mc1 left the MC group on %s: %s\n",
163
nic.getDisplayName(), group1);
164
mc2.leaveGroup(toSocketAddress(group2), nic);
165
out.printf("mc2 left the MC group on %s: %s\n",
166
nic.getDisplayName(), group2);
167
} catch (IOException io) {
168
throw new UncheckedIOException(io);
169
}
170
});
171
}
172
}
173
174
public static void main(String args[]) throws IOException {
175
IPSupport.throwSkippedExceptionIfNonOperational();
176
String os = System.getProperty("os.name");
177
178
// Requires IP_MULTICAST_ALL on Linux (new since 2.6.31) so skip
179
// on older kernels. Note that we skip on <= version 3 to keep the
180
// parsing simple
181
if (os.equals("Linux")) {
182
String osversion = System.getProperty("os.version");
183
String[] vers = osversion.split("\\.", 0);
184
int major = Integer.parseInt(vers[0]);
185
if (major < 3) {
186
System.out.format("Kernel version is %s, test skipped%n", osversion);
187
return;
188
}
189
}
190
191
// multicast groups used for the test
192
InetAddress ip4Group1 = InetAddress.getByName("224.1.1.120");
193
InetAddress ip4Group2 = InetAddress.getByName("224.1.1.121");
194
195
test(ip4Group1, ip4Group2);
196
}
197
}
198
199