author | martin |
Tue, 15 Sep 2015 21:56:04 -0700 | |
changeset 32649 | 2ee9017c7597 |
parent 25859 | 3317bb8137f4 |
child 44544 | fd2a9f5c6ff0 |
permissions | -rw-r--r-- |
12047 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
21278
diff
changeset
|
2 |
* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. |
12047 | 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 |
/** |
|
21278 | 29 |
* Choose a network interface to be the default for |
12047 | 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 |
||
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
25859
diff
changeset
|
45 |
private static final NetworkInterface defaultInterface = |
12047 | 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 |
} |