Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/macosx/classes/java/net/DefaultInterface.java
38829 views
1
/*
2
* Copyright (c) 2011, 2013, 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.util.Enumeration;
41
import java.io.IOException;
42
43
class DefaultInterface {
44
45
private final static 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
}
98
99