33 * 2. point to point
34 * 3. loopback
35 * 4. none.
36 * Platforms that do not require a default interface implement a dummy
37 * that returns null.
38 */
39
40 import java.util.Enumeration;
41 import java.io.IOException;
42
43 class DefaultInterface {
44
45 private static final NetworkInterface defaultInterface =
46 chooseDefaultInterface();
47
48 static NetworkInterface getDefault() {
49 return defaultInterface;
50 }
51
52 /**
53 * Choose a default interface. This method returns an interface that is
54 * both "up" and supports multicast. This method choses an interface in
55 * order of preference:
56 * 1. neither loopback nor point to point
57 * 2. point to point
58 * 3. loopback
59 *
60 * @return the chosen interface or {@code null} if there isn't a suitable
61 * default
62 */
63 private static NetworkInterface chooseDefaultInterface() {
64 Enumeration<NetworkInterface> nifs;
65
66 try {
67 nifs = NetworkInterface.getNetworkInterfaces();
68 } catch (IOException ignore) {
69 // unable to enumate network interfaces
70 return null;
71 }
72
73 NetworkInterface ppp = null;
74 NetworkInterface loopback = null;
75
76 while (nifs.hasMoreElements()) {
77 NetworkInterface ni = nifs.nextElement();
78 try {
79 if (ni.isUp() && ni.supportsMulticast()) {
80 boolean isLoopback = ni.isLoopback();
81 boolean isPPP = ni.isPointToPoint();
82 if (!isLoopback && !isPPP) {
83 // found an interface that is not the loopback or a
84 // point-to-point interface
85 return ni;
86 }
87 if (ppp == null && isPPP)
88 ppp = ni;
89 if (loopback == null && isLoopback)
90 loopback = ni;
91 }
92 } catch (IOException skip) { }
93 }
94
95 return (ppp != null) ? ppp : loopback;
96 }
97 }
|
33 * 2. point to point
34 * 3. loopback
35 * 4. none.
36 * Platforms that do not require a default interface implement a dummy
37 * that returns null.
38 */
39
40 import java.util.Enumeration;
41 import java.io.IOException;
42
43 class DefaultInterface {
44
45 private static final NetworkInterface defaultInterface =
46 chooseDefaultInterface();
47
48 static NetworkInterface getDefault() {
49 return defaultInterface;
50 }
51
52 /**
53 * Choose a default interface. This method returns the first interface that
54 * is both "up" and supports multicast. This method chooses an interface in
55 * order of preference:
56 * 1. neither loopback nor point to point
57 * ( prefer interfaces with dual IP support )
58 * 2. point to point
59 * 3. loopback
60 *
61 * @return the chosen interface or {@code null} if there isn't a suitable
62 * default
63 */
64 private static NetworkInterface chooseDefaultInterface() {
65 Enumeration<NetworkInterface> nifs;
66
67 try {
68 nifs = NetworkInterface.getNetworkInterfaces();
69 } catch (IOException ignore) {
70 // unable to enumerate network interfaces
71 return null;
72 }
73
74 NetworkInterface preferred = null;
75 NetworkInterface ppp = null;
76 NetworkInterface loopback = null;
77
78 while (nifs.hasMoreElements()) {
79 NetworkInterface ni = nifs.nextElement();
80 try {
81 if (!ni.isUp() || !ni.supportsMulticast())
82 continue;
83
84 boolean ip4 = false, ip6 = false;
85 Enumeration<InetAddress> addrs = ni.getInetAddresses();
86 while (addrs.hasMoreElements()) {
87 InetAddress addr = addrs.nextElement();
88 if (!addr.isAnyLocalAddress()) {
89 if (addr instanceof Inet4Address) {
90 ip4 = true;
91 } else if (addr instanceof Inet6Address) {
92 ip6 = true;
93 }
94 }
95 }
96
97 boolean isLoopback = ni.isLoopback();
98 boolean isPPP = ni.isPointToPoint();
99 if (!isLoopback && !isPPP) {
100 // found an interface that is not the loopback or a
101 // point-to-point interface
102 if (preferred == null) {
103 preferred = ni;
104 } else if (ip4 == true && ip6 == true){
105 return ni;
106 }
107 }
108 if (ppp == null && isPPP)
109 ppp = ni;
110 if (loopback == null && isLoopback)
111 loopback = ni;
112
113 } catch (IOException skip) { }
114 }
115
116 if (preferred != null) {
117 return preferred;
118 } else {
119 return (ppp != null) ? ppp : loopback;
120 }
121 }
122 }
|