1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
|
/*
* Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4742177
* @summary Re-test IPv6 (and specifically MulticastSocket) with latest Linux & USAGI code
*/
import java.net.*;
import java.util.*;
public class SetOutgoingIf {
private static int PORT = 9001;
private static String osname;
static boolean isWindows() {
if (osname == null)
osname = System.getProperty("os.name");
return osname.contains("Windows");
}
static boolean isMacOS() {
return System.getProperty("os.name").contains("OS X");
}
private static boolean hasIPv6() throws Exception {
List<NetworkInterface> nics = Collections.list(
NetworkInterface.getNetworkInterfaces());
for (NetworkInterface nic : nics) {
List<InetAddress> addrs = Collections.list(nic.getInetAddresses());
for (InetAddress addr : addrs) {
if (addr instanceof Inet6Address)
return true;
}
}
return false;
}
public static void main(String[] args) throws Exception {
if (isWindows()) {
System.out.println("The test only run on non-Windows OS. Bye.");
return;
}
if (!hasIPv6()) {
System.out.println("No IPv6 available. Bye.");
return;
}
// We need 2 or more network interfaces to run the test
//
List<NetIf> netIfs = new ArrayList<NetIf>();
int index = 1;
for (NetworkInterface nic : Collections.list(NetworkInterface.getNetworkInterfaces())) {
// we should use only network interfaces with multicast support which are in "up" state
if (!nic.isLoopback() && nic.supportsMulticast() && nic.isUp() && !isTestExcludedInterface(nic)) {
NetIf netIf = NetIf.create(nic);
// now determine what (if any) type of addresses are assigned to this interface
for (InetAddress addr : Collections.list(nic.getInetAddresses())) {
if (addr.isAnyLocalAddress())
continue;
System.out.println(" addr " + addr);
if (addr instanceof Inet4Address) {
netIf.ipv4Address(true);
} else if (addr instanceof Inet6Address) {
netIf.ipv6Address(true);
}
}
if (netIf.ipv4Address() || netIf.ipv6Address()) {
netIf.index(index++);
netIfs.add(netIf);
debug("Using: " + nic);
}
} else {
System.out.println("Ignore NetworkInterface nic == " + nic);
}
}
if (netIfs.size() <= 1) {
System.out.println("Need 2 or more network interfaces to run. Bye.");
return;
}
// We will send packets to one ipv4, and one ipv6
// multicast group using each network interface :-
// 224.1.1.1 --|
// ff02::1:1 --|--> using network interface #1
// 224.1.2.1 --|
// ff02::1:2 --|--> using network interface #2
// and so on.
//
for (NetIf netIf : netIfs) {
int NetIfIndex = netIf.index();
List<InetAddress> groups = new ArrayList<InetAddress>();
if (netIf.ipv4Address()) {
InetAddress groupv4 = InetAddress.getByName("224.1." + NetIfIndex + ".1");
groups.add(groupv4);
}
if (netIf.ipv6Address()) {
InetAddress groupv6 = InetAddress.getByName("ff02::1:" + NetIfIndex);
groups.add(groupv6);
}
debug("Adding " + groups + " groups for " + netIf.nic().getName());
netIf.groups(groups);
// use a separated thread to send to those 2 groups
Thread sender = new Thread(new Sender(netIf,
groups,
PORT));
sender.setDaemon(true); // we want sender to stop when main thread exits
sender.start();
}
// try to receive on each group, then check if the packet comes
// from the expected network interface
//
byte[] buf = new byte[1024];
for (NetIf netIf : netIfs) {
NetworkInterface nic = netIf.nic();
for (InetAddress group : netIf.groups()) {
MulticastSocket mcastsock = new MulticastSocket(PORT);
mcastsock.setSoTimeout(5000); // 5 second
DatagramPacket packet = new DatagramPacket(buf, 0, buf.length);
// the interface supports the IP multicast group
debug("Joining " + group + " on " + nic.getName());
mcastsock.joinGroup(new InetSocketAddress(group, PORT), nic);
try {
mcastsock.receive(packet);
debug("received packet on " + packet.getAddress());
} catch (Exception e) {
// test failed if any exception
throw new RuntimeException(e);
}
// now check which network interface this packet comes from
NetworkInterface from = NetworkInterface.getByInetAddress(packet.getAddress());
NetworkInterface shouldbe = nic;
if (from != null) {
if (!from.equals(shouldbe)) {
System.out.println("Packets on group "
+ group + " should come from "
+ shouldbe.getName() + ", but came from "
+ from.getName());
}
}
mcastsock.leaveGroup(new InetSocketAddress(group, PORT), nic);
}
}
}
private static boolean isTestExcludedInterface(NetworkInterface nif) {
if (isMacOS() && nif.getName().contains("awdl"))
return true;
String dName = nif.getDisplayName();
if (isWindows() && dName != null && dName.contains("Teredo"))
return true;
return false;
}
private static boolean debug = true;
static void debug(String message) {
if (debug)
System.out.println(message);
}
}
class Sender implements Runnable {
private NetIf netIf;
private List<InetAddress> groups;
private int port;
public Sender(NetIf netIf,
List<InetAddress> groups,
int port) {
this.netIf = netIf;
this.groups = groups;
this.port = port;
}
public void run() {
try {
MulticastSocket mcastsock = new MulticastSocket();
mcastsock.setNetworkInterface(netIf.nic());
List<DatagramPacket> packets = new LinkedList<DatagramPacket>();
byte[] buf = "hello world".getBytes();
for (InetAddress group : groups) {
packets.add(new DatagramPacket(buf, buf.length, new InetSocketAddress(group, port)));
}
for (;;) {
for (DatagramPacket packet : packets)
mcastsock.send(packet);
Thread.sleep(1000); // sleep 1 second
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
@SuppressWarnings("unchecked")
class NetIf {
private boolean ipv4Address; //false
private boolean ipv6Address; //false
private int index;
List<InetAddress> groups = Collections.EMPTY_LIST;
private final NetworkInterface nic;
private NetIf(NetworkInterface nic) {
this.nic = nic;
}
static NetIf create(NetworkInterface nic) {
return new NetIf(nic);
}
NetworkInterface nic() {
return nic;
}
boolean ipv4Address() {
return ipv4Address;
}
void ipv4Address(boolean ipv4Address) {
this.ipv4Address = ipv4Address;
}
boolean ipv6Address() {
return ipv6Address;
}
void ipv6Address(boolean ipv6Address) {
this.ipv6Address = ipv6Address;
}
int index() {
return index;
}
void index(int index) {
this.index = index;
}
List<InetAddress> groups() {
return groups;
}
void groups(List<InetAddress> groups) {
this.groups = groups;
}
}
|