1 /* |
|
2 * Copyright (c) 2007, 2017, 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 import java.net.*; |
|
25 import java.util.*; |
|
26 import java.io.IOException; |
|
27 |
|
28 /** |
|
29 * Helper class for multicasting tests. |
|
30 */ |
|
31 |
|
32 class NetworkConfiguration { |
|
33 |
|
34 private Map<NetworkInterface,List<InetAddress>> ip4Interfaces; |
|
35 private Map<NetworkInterface,List<InetAddress>> ip6Interfaces; |
|
36 |
|
37 private NetworkConfiguration(Map<NetworkInterface,List<InetAddress>> ip4Interfaces, |
|
38 Map<NetworkInterface,List<InetAddress>> ip6Interfaces) |
|
39 { |
|
40 this.ip4Interfaces = ip4Interfaces; |
|
41 this.ip6Interfaces = ip6Interfaces; |
|
42 } |
|
43 |
|
44 Iterable<NetworkInterface> ip4Interfaces() { |
|
45 return ip4Interfaces.keySet(); |
|
46 } |
|
47 |
|
48 Iterable<NetworkInterface> ip6Interfaces() { |
|
49 return ip6Interfaces.keySet(); |
|
50 } |
|
51 |
|
52 Iterable<InetAddress> ip4Addresses(NetworkInterface nif) { |
|
53 return ip4Interfaces.get(nif); |
|
54 } |
|
55 |
|
56 Iterable<InetAddress> ip6Addresses(NetworkInterface nif) { |
|
57 return ip6Interfaces.get(nif); |
|
58 } |
|
59 |
|
60 private static final boolean isMacOs = |
|
61 System.getProperty("os.name").equals("Mac OS X"); |
|
62 |
|
63 static NetworkConfiguration probe() throws IOException { |
|
64 Map<NetworkInterface,List<InetAddress>> ip4Interfaces = |
|
65 new HashMap<NetworkInterface,List<InetAddress>>(); |
|
66 Map<NetworkInterface,List<InetAddress>> ip6Interfaces = |
|
67 new HashMap<NetworkInterface,List<InetAddress>>(); |
|
68 |
|
69 // find the interfaces that support IPv4 and IPv6 |
|
70 List<NetworkInterface> nifs = Collections |
|
71 .list(NetworkInterface.getNetworkInterfaces()); |
|
72 for (NetworkInterface nif: nifs) { |
|
73 // ignore intertaces that are down or don't support multicast |
|
74 if (!nif.isUp() || !nif.supportsMulticast() || nif.isLoopback() |
|
75 || (isMacOs && nif.getName().contains("awdl"))) |
|
76 continue; |
|
77 |
|
78 List<InetAddress> addrs = Collections.list(nif.getInetAddresses()); |
|
79 for (InetAddress addr: addrs) { |
|
80 if (!addr.isAnyLocalAddress()) { |
|
81 if (addr instanceof Inet4Address) { |
|
82 List<InetAddress> list = ip4Interfaces.get(nif); |
|
83 if (list == null) { |
|
84 list = new LinkedList<InetAddress>(); |
|
85 } |
|
86 list.add(addr); |
|
87 ip4Interfaces.put(nif, list); |
|
88 } else if (addr instanceof Inet6Address) { |
|
89 List<InetAddress> list = ip6Interfaces.get(nif); |
|
90 if (list == null) { |
|
91 list = new LinkedList<InetAddress>(); |
|
92 } |
|
93 list.add(addr); |
|
94 ip6Interfaces.put(nif, list); |
|
95 } |
|
96 } |
|
97 } |
|
98 } |
|
99 return new NetworkConfiguration(ip4Interfaces, ip6Interfaces); |
|
100 } |
|
101 } |
|