2
|
1 |
/*
|
|
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 |
*/
|
|
29 |
import java.util.*;
|
|
30 |
import java.net.*;
|
|
31 |
|
|
32 |
|
|
33 |
public class NoLoopbackPackets {
|
|
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 |
if (!nic.isLoopback()) {
|
|
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 |
}
|
|
56 |
|
|
57 |
return false;
|
|
58 |
}
|
|
59 |
|
|
60 |
public static void main(String[] args) throws Exception {
|
|
61 |
if (isWindows()) {
|
|
62 |
System.out.println("The test only run on non-Windows OS. Bye.");
|
|
63 |
return;
|
|
64 |
}
|
|
65 |
|
|
66 |
if (!hasIPv6()) {
|
|
67 |
System.out.println("No IPv6 available. Bye.");
|
|
68 |
return;
|
|
69 |
}
|
|
70 |
|
|
71 |
// we will send packets to three multicast groups :-
|
|
72 |
// 224.1.1.1, ::ffff:224.1.1.2, and ff02::1:1
|
|
73 |
//
|
|
74 |
List<SocketAddress> groups = new ArrayList<SocketAddress>();
|
|
75 |
groups.add(new InetSocketAddress(InetAddress.getByName("224.1.1.1"), PORT));
|
|
76 |
groups.add(new InetSocketAddress(InetAddress.getByName("::ffff:224.1.1.2"), PORT));
|
|
77 |
groups.add(new InetSocketAddress(InetAddress.getByName("ff02::1:1"), PORT));
|
|
78 |
|
|
79 |
Thread sender = new Thread(new Sender(groups));
|
|
80 |
sender.setDaemon(true); // we want sender to stop when main thread exits
|
|
81 |
sender.start();
|
|
82 |
|
|
83 |
// Now try to receive multicast packets. we should not see any of them
|
|
84 |
// since we disable loopback mode.
|
|
85 |
//
|
|
86 |
MulticastSocket msock = new MulticastSocket(PORT);
|
|
87 |
msock.setSoTimeout(5000); // 5 seconds
|
|
88 |
|
|
89 |
byte[] buf = new byte[1024];
|
|
90 |
DatagramPacket packet = new DatagramPacket(buf, 0, buf.length);
|
|
91 |
List<SocketAddress> failedGroups = new ArrayList<SocketAddress>();
|
|
92 |
for (SocketAddress group : groups) {
|
|
93 |
msock.joinGroup(group, null);
|
|
94 |
|
|
95 |
try {
|
|
96 |
msock.receive(packet);
|
|
97 |
|
|
98 |
// it is an error if we receive something
|
|
99 |
failedGroups.add(group);
|
|
100 |
} catch (SocketTimeoutException e) {
|
|
101 |
// we expect this
|
|
102 |
}
|
|
103 |
|
|
104 |
msock.leaveGroup(group, null);
|
|
105 |
}
|
|
106 |
|
|
107 |
if (failedGroups.size() > 0) {
|
|
108 |
System.out.println("We should not receive anything from following groups, but we did:");
|
|
109 |
for (SocketAddress group : failedGroups)
|
|
110 |
System.out.println(group);
|
|
111 |
throw new RuntimeException("test failed.");
|
|
112 |
}
|
|
113 |
}
|
|
114 |
}
|
|
115 |
|
|
116 |
class Sender implements Runnable {
|
|
117 |
private List<SocketAddress> sendToGroups;
|
|
118 |
|
|
119 |
public Sender(List<SocketAddress> groups) {
|
|
120 |
sendToGroups = groups;
|
|
121 |
}
|
|
122 |
|
|
123 |
public void run() {
|
|
124 |
byte[] buf = "hello world".getBytes();
|
|
125 |
List<DatagramPacket> packets = new ArrayList<DatagramPacket>();
|
|
126 |
|
|
127 |
try {
|
|
128 |
for (SocketAddress group : sendToGroups) {
|
|
129 |
DatagramPacket packet = new DatagramPacket(buf, buf.length, group);
|
|
130 |
packets.add(packet);
|
|
131 |
}
|
|
132 |
|
|
133 |
MulticastSocket msock = new MulticastSocket();
|
|
134 |
msock.setLoopbackMode(true); // disable loopback mode
|
|
135 |
for (;;) {
|
|
136 |
for (DatagramPacket packet : packets) {
|
|
137 |
msock.send(packet);
|
|
138 |
}
|
|
139 |
|
|
140 |
Thread.currentThread().sleep(1000); // 1 second
|
|
141 |
}
|
|
142 |
} catch (Exception e) {
|
|
143 |
throw new RuntimeException(e);
|
|
144 |
}
|
|
145 |
}
|
|
146 |
}
|