1 /*
2  * Copyright (c) 2007, 2011, 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 
NetworkConfiguration(Map<NetworkInterface,List<InetAddress>> ip4Interfaces, Map<NetworkInterface,List<InetAddress>> ip6Interfaces)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 
ip4Interfaces()44     Iterable<NetworkInterface> ip4Interfaces() {
45         return ip4Interfaces.keySet();
46     }
47 
ip6Interfaces()48     Iterable<NetworkInterface> ip6Interfaces() {
49         return ip6Interfaces.keySet();
50     }
51 
ip4Addresses(NetworkInterface nif)52     Iterable<InetAddress> ip4Addresses(NetworkInterface nif) {
53         return ip4Interfaces.get(nif);
54     }
55 
ip6Addresses(NetworkInterface nif)56     Iterable<InetAddress> ip6Addresses(NetworkInterface nif) {
57         return ip6Interfaces.get(nif);
58     }
59 
60     // IPv6 not supported for Windows XP/Server 2003
isIPv6Supported()61     static boolean isIPv6Supported() {
62         if (System.getProperty("os.name").startsWith("Windows")) {
63             String ver = System.getProperty("os.version");
64             int major = Integer.parseInt(ver.split("\\.")[0]);
65             return (major >= 6);
66         }
67         return true;
68     }
69 
probe()70     static NetworkConfiguration probe() throws IOException {
71         Map<NetworkInterface,List<InetAddress>> ip4Interfaces =
72             new HashMap<NetworkInterface,List<InetAddress>>();
73         Map<NetworkInterface,List<InetAddress>> ip6Interfaces =
74             new HashMap<NetworkInterface,List<InetAddress>>();
75         boolean isIPv6Supported = isIPv6Supported();
76 
77         // find the interfaces that support IPv4 and IPv6
78         List<NetworkInterface> nifs = Collections
79             .list(NetworkInterface.getNetworkInterfaces());
80         for (NetworkInterface nif: nifs) {
81             // ignore intertaces that are down or don't support multicast
82             if (!nif.isUp() || !nif.supportsMulticast() || nif.isLoopback())
83                 continue;
84 
85             List<InetAddress> addrs = Collections.list(nif.getInetAddresses());
86             for (InetAddress addr: addrs) {
87                 if (!addr.isAnyLocalAddress()) {
88                     if (addr instanceof Inet4Address) {
89                         List<InetAddress> list = ip4Interfaces.get(nif);
90                         if (list == null) {
91                             list = new LinkedList<InetAddress>();
92                         }
93                         list.add(addr);
94                         ip4Interfaces.put(nif, list);
95                     } else if (isIPv6Supported && (addr instanceof Inet6Address)) {
96                         List<InetAddress> list = ip6Interfaces.get(nif);
97                         if (list == null) {
98                             list = new LinkedList<InetAddress>();
99                         }
100                         list.add(addr);
101                         ip6Interfaces.put(nif, list);
102                     }
103                 }
104             }
105         }
106         return new NetworkConfiguration(ip4Interfaces, ip6Interfaces);
107     }
108 }
109