4638N/A/*
4638N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
4638N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4638N/A *
4638N/A * This code is free software; you can redistribute it and/or modify it
4638N/A * under the terms of the GNU General Public License version 2 only, as
4638N/A * published by the Free Software Foundation. Oracle designates this
4638N/A * particular file as subject to the "Classpath" exception as provided
4638N/A * by Oracle in the LICENSE file that accompanied this code.
4638N/A *
4638N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4638N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4638N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4638N/A * version 2 for more details (a copy is included in the LICENSE file that
4638N/A * accompanied this code).
4638N/A *
4638N/A * You should have received a copy of the GNU General Public License version
4638N/A * 2 along with this work; if not, write to the Free Software Foundation,
4638N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4638N/A *
4638N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4638N/A * or visit www.oracle.com if you need additional information or have any
4638N/A * questions.
4638N/A */
4638N/A
4638N/Apackage java.net;
4638N/A
4638N/A/**
4638N/A * Choose a network inteface to be the default for
4638N/A * outgoing IPv6 traffic that does not specify a scope_id (and which needs one).
4638N/A * We choose the first interface that is up and is (in order of preference):
4638N/A * 1. neither loopback nor point to point
4638N/A * 2. point to point
4638N/A * 3. loopback
4638N/A * 4. none.
4638N/A * Platforms that do not require a default interface implement a dummy
4638N/A * that returns null.
4638N/A */
4638N/A
4638N/Aimport java.util.Enumeration;
4638N/Aimport java.io.IOException;
4638N/A
4638N/Aclass DefaultInterface {
4638N/A
4638N/A private final static NetworkInterface defaultInterface =
4638N/A chooseDefaultInterface();
4638N/A
4638N/A static NetworkInterface getDefault() {
4638N/A return defaultInterface;
4638N/A }
4638N/A
4638N/A /**
4638N/A * Choose a default interface. This method returns an interface that is
4638N/A * both "up" and supports multicast. This method choses an interface in
4638N/A * order of preference:
4638N/A * 1. neither loopback nor point to point
4638N/A * 2. point to point
4638N/A * 3. loopback
4638N/A *
4638N/A * @return the chosen interface or {@code null} if there isn't a suitable
4638N/A * default
4638N/A */
4638N/A private static NetworkInterface chooseDefaultInterface() {
4638N/A Enumeration<NetworkInterface> nifs;
4638N/A
4638N/A try {
4638N/A nifs = NetworkInterface.getNetworkInterfaces();
4638N/A } catch (IOException ignore) {
4638N/A // unable to enumate network interfaces
4638N/A return null;
4638N/A }
4638N/A
4638N/A NetworkInterface ppp = null;
4638N/A NetworkInterface loopback = null;
4638N/A
4638N/A while (nifs.hasMoreElements()) {
4638N/A NetworkInterface ni = nifs.nextElement();
4638N/A try {
4638N/A if (ni.isUp() && ni.supportsMulticast()) {
4638N/A boolean isLoopback = ni.isLoopback();
4638N/A boolean isPPP = ni.isPointToPoint();
4638N/A if (!isLoopback && !isPPP) {
4638N/A // found an interface that is not the loopback or a
4638N/A // point-to-point interface
4638N/A return ni;
4638N/A }
4638N/A if (ppp == null && isPPP)
4638N/A ppp = ni;
4638N/A if (loopback == null && isLoopback)
4638N/A loopback = ni;
4638N/A }
4638N/A } catch (IOException skip) { }
4638N/A }
4638N/A
4638N/A return (ppp != null) ? ppp : loopback;
4638N/A }
4638N/A}