Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/macosx/classes/java/net/DefaultInterface.java
38829 views
/*1* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.net;2627/**28* Choose a network interface to be the default for29* outgoing IPv6 traffic that does not specify a scope_id (and which needs one).30* We choose the first interface that is up and is (in order of preference):31* 1. neither loopback nor point to point32* 2. point to point33* 3. loopback34* 4. none.35* Platforms that do not require a default interface implement a dummy36* that returns null.37*/3839import java.util.Enumeration;40import java.io.IOException;4142class DefaultInterface {4344private final static NetworkInterface defaultInterface =45chooseDefaultInterface();4647static NetworkInterface getDefault() {48return defaultInterface;49}5051/**52* Choose a default interface. This method returns an interface that is53* both "up" and supports multicast. This method choses an interface in54* order of preference:55* 1. neither loopback nor point to point56* 2. point to point57* 3. loopback58*59* @return the chosen interface or {@code null} if there isn't a suitable60* default61*/62private static NetworkInterface chooseDefaultInterface() {63Enumeration<NetworkInterface> nifs;6465try {66nifs = NetworkInterface.getNetworkInterfaces();67} catch (IOException ignore) {68// unable to enumate network interfaces69return null;70}7172NetworkInterface ppp = null;73NetworkInterface loopback = null;7475while (nifs.hasMoreElements()) {76NetworkInterface ni = nifs.nextElement();77try {78if (ni.isUp() && ni.supportsMulticast()) {79boolean isLoopback = ni.isLoopback();80boolean isPPP = ni.isPointToPoint();81if (!isLoopback && !isPPP) {82// found an interface that is not the loopback or a83// point-to-point interface84return ni;85}86if (ppp == null && isPPP)87ppp = ni;88if (loopback == null && isLoopback)89loopback = ni;90}91} catch (IOException skip) { }92}9394return (ppp != null) ? ppp : loopback;95}96}979899