Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/macosx/classes/java/net/DefaultInterface.java
41133 views
1
/*
2
* Copyright (c) 2011, 2021, 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package java.net;
27
28
/**
29
* Choose a network interface to be the default for
30
* outgoing IPv6 traffic that does not specify a scope_id (and which needs one).
31
* We choose the first interface that is up and is (in order of preference):
32
* 1. neither loopback nor point to point
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.security.AccessController;
41
import java.security.PrivilegedAction;
42
import java.util.Enumeration;
43
import java.io.IOException;
44
45
class DefaultInterface {
46
47
private static final NetworkInterface defaultInterface =
48
chooseDefaultInterface();
49
50
static NetworkInterface getDefault() {
51
return defaultInterface;
52
}
53
54
/**
55
* Choose a default interface. This method returns the first interface that
56
* is both "up" and supports multicast. This method chooses an interface in
57
* order of preference, using the following algorithm:
58
*
59
* <pre>
60
* Interfaces that are down, or don't support multicasting, are skipped.
61
* In steps 1-4 below, PPP and loopback interfaces are skipped.
62
*
63
* 1. The first interface that has at least an IPv4 address, and an IPv6 address,
64
* and a non link-local IP address, is picked.
65
*
66
* 2. If none is found, then the first interface that has at least an
67
* IPv4 address, and an IPv6 address is picked.
68
*
69
* 3. If none is found, then the first interface that has at least a
70
* non link local IP address is picked.
71
*
72
* 4. If none is found, then the first non loopback and non PPP interface
73
* is picked.
74
*
75
* 5. If none is found then first PPP interface is picked.
76
*
77
* 6. If none is found, then the first loopback interface is picked.
78
*
79
* 7. If none is found, then null is returned.
80
* </pre>
81
*
82
* @return the chosen interface or {@code null} if there isn't a suitable
83
* default
84
*/
85
private static NetworkInterface chooseDefaultInterface() {
86
Enumeration<NetworkInterface> nifs;
87
88
try {
89
nifs = NetworkInterface.getNetworkInterfaces();
90
} catch (IOException ignore) {
91
// unable to enumerate network interfaces
92
return null;
93
}
94
95
NetworkInterface preferred = null;
96
NetworkInterface dual = null;
97
NetworkInterface nonLinkLocal = null;
98
NetworkInterface ppp = null;
99
NetworkInterface loopback = null;
100
101
while (nifs.hasMoreElements()) {
102
NetworkInterface ni = nifs.nextElement();
103
try {
104
if (!ni.isUp() || !ni.supportsMulticast())
105
continue;
106
107
boolean ip4 = false, ip6 = false, isNonLinkLocal = false;
108
PrivilegedAction<Enumeration<InetAddress>> pa = ni::getInetAddresses;
109
@SuppressWarnings("removal")
110
Enumeration<InetAddress> addrs = AccessController.doPrivileged(pa);
111
while (addrs.hasMoreElements()) {
112
InetAddress addr = addrs.nextElement();
113
if (!addr.isAnyLocalAddress()) {
114
if (addr instanceof Inet4Address) {
115
ip4 = true;
116
} else if (addr instanceof Inet6Address) {
117
ip6 = true;
118
}
119
if (!addr.isLinkLocalAddress()) {
120
isNonLinkLocal = true;
121
}
122
}
123
}
124
125
boolean isLoopback = ni.isLoopback();
126
boolean isPPP = ni.isPointToPoint();
127
if (!isLoopback && !isPPP) {
128
// found an interface that is not the loopback or a
129
// point-to-point interface
130
if (preferred == null) {
131
preferred = ni;
132
}
133
if (ip4 && ip6) {
134
if (isNonLinkLocal) return ni;
135
if (dual == null) dual = ni;
136
}
137
if (nonLinkLocal == null) {
138
if (isNonLinkLocal) nonLinkLocal = ni;
139
}
140
}
141
if (ppp == null && isPPP)
142
ppp = ni;
143
if (loopback == null && isLoopback)
144
loopback = ni;
145
146
} catch (IOException skip) { }
147
}
148
149
if (dual != null) {
150
return dual;
151
} else if (nonLinkLocal != null) {
152
return nonLinkLocal;
153
} else if (preferred != null) {
154
return preferred;
155
} else {
156
return (ppp != null) ? ppp : loopback;
157
}
158
}
159
}
160
161