Search in sources :

Example 51 with InterfaceAddress

use of java.net.InterfaceAddress in project crate by crate.

the class IfConfig method doLogging.

/**
 * perform actual logging: might throw exception if things go wrong
 */
private static void doLogging() throws IOException {
    StringBuilder msg = new StringBuilder();
    for (NetworkInterface nic : NetworkUtils.getInterfaces()) {
        msg.append(System.lineSeparator());
        // ordinary name
        msg.append(nic.getName());
        msg.append(System.lineSeparator());
        // display name (e.g. on windows)
        if (!nic.getName().equals(nic.getDisplayName())) {
            msg.append(INDENT);
            msg.append(nic.getDisplayName());
            msg.append(System.lineSeparator());
        }
        // addresses: v4 first, then v6
        List<InterfaceAddress> addresses = nic.getInterfaceAddresses();
        for (InterfaceAddress address : addresses) {
            if (address.getAddress() instanceof Inet6Address == false) {
                msg.append(INDENT);
                msg.append(formatAddress(address));
                msg.append(System.lineSeparator());
            }
        }
        for (InterfaceAddress address : addresses) {
            if (address.getAddress() instanceof Inet6Address) {
                msg.append(INDENT);
                msg.append(formatAddress(address));
                msg.append(System.lineSeparator());
            }
        }
        // hardware address
        byte[] hardware = nic.getHardwareAddress();
        if (hardware != null) {
            msg.append(INDENT);
            msg.append("hardware ");
            for (int i = 0; i < hardware.length; i++) {
                if (i > 0) {
                    msg.append(":");
                }
                msg.append(String.format(Locale.ROOT, "%02X", hardware[i]));
            }
            msg.append(System.lineSeparator());
        }
        // attributes
        msg.append(INDENT);
        msg.append(formatFlags(nic));
        msg.append(System.lineSeparator());
    }
    LOGGER.debug("configuration:{}{}", System.lineSeparator(), msg);
}
Also used : InterfaceAddress(java.net.InterfaceAddress) NetworkInterface(java.net.NetworkInterface) Inet6Address(java.net.Inet6Address)

Example 52 with InterfaceAddress

use of java.net.InterfaceAddress in project zeppelin by apache.

the class RemoteInterpreterUtils method findAvailableHostAddress.

public static String findAvailableHostAddress() throws UnknownHostException, SocketException {
    String zeppelinServerIP = System.getenv("ZEPPELIN_LOCAL_IP");
    if (zeppelinServerIP != null) {
        return zeppelinServerIP;
    }
    InetAddress address = InetAddress.getLocalHost();
    if (address.isLoopbackAddress()) {
        for (NetworkInterface networkInterface : Collections.list(NetworkInterface.getNetworkInterfaces())) {
            if (!networkInterface.isLoopback()) {
                for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
                    InetAddress a = interfaceAddress.getAddress();
                    if (a instanceof Inet4Address) {
                        return a.getHostAddress();
                    }
                }
            }
        }
    }
    return address.getHostAddress();
}
Also used : Inet4Address(java.net.Inet4Address) InterfaceAddress(java.net.InterfaceAddress) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 53 with InterfaceAddress

use of java.net.InterfaceAddress in project cloudstack by apache.

the class NetUtils method getAllDefaultNicIps.

public static List<String> getAllDefaultNicIps() {
    final List<String> addrs = new ArrayList<>();
    final String pubNic = getDefaultEthDevice();
    if (pubNic == null) {
        return addrs;
    }
    NetworkInterface nic = null;
    try {
        nic = NetworkInterface.getByName(pubNic);
    } catch (final SocketException e) {
        return addrs;
    }
    for (InterfaceAddress address : nic.getInterfaceAddresses()) {
        addrs.add(address.getAddress().getHostAddress().split("%")[0]);
    }
    return addrs;
}
Also used : SocketException(java.net.SocketException) InterfaceAddress(java.net.InterfaceAddress) ArrayList(java.util.ArrayList) NetworkInterface(java.net.NetworkInterface)

Example 54 with InterfaceAddress

use of java.net.InterfaceAddress in project platformlayer by platformlayer.

the class InetAddressUtils method getLocalAddresses.

public static List<InetAddress> getLocalAddresses() {
    List<InetAddress> addresses = Lists.newArrayList();
    Enumeration<NetworkInterface> networkInterfaces;
    try {
        networkInterfaces = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        throw new IllegalStateException("Error reading network addresses", e);
    }
    while (networkInterfaces.hasMoreElements()) {
        NetworkInterface networkInterface = networkInterfaces.nextElement();
        List<InterfaceAddress> interfaceAddresses = networkInterface.getInterfaceAddresses();
        for (InterfaceAddress interfaceAddress : interfaceAddresses) {
            InetAddress address = interfaceAddress.getAddress();
            addresses.add(address);
        }
    }
    return addresses;
}
Also used : SocketException(java.net.SocketException) InterfaceAddress(java.net.InterfaceAddress) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 55 with InterfaceAddress

use of java.net.InterfaceAddress in project AndroidUtilCode by Blankj.

the class NetworkUtils method getBroadcastIpAddress.

/**
 * Return the ip address of broadcast.
 *
 * @return the ip address of broadcast
 */
public static String getBroadcastIpAddress() {
    try {
        Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
        LinkedList<InetAddress> adds = new LinkedList<>();
        while (nis.hasMoreElements()) {
            NetworkInterface ni = nis.nextElement();
            if (!ni.isUp() || ni.isLoopback())
                continue;
            List<InterfaceAddress> ias = ni.getInterfaceAddresses();
            for (int i = 0, size = ias.size(); i < size; i++) {
                InterfaceAddress ia = ias.get(i);
                InetAddress broadcast = ia.getBroadcast();
                if (broadcast != null) {
                    return broadcast.getHostAddress();
                }
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    return "";
}
Also used : SocketException(java.net.SocketException) InterfaceAddress(java.net.InterfaceAddress) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress) LinkedList(java.util.LinkedList) SuppressLint(android.annotation.SuppressLint)

Aggregations

InterfaceAddress (java.net.InterfaceAddress)104 NetworkInterface (java.net.NetworkInterface)69 InetAddress (java.net.InetAddress)60 SocketException (java.net.SocketException)27 Inet4Address (java.net.Inet4Address)26 Test (org.junit.Test)19 ArrayList (java.util.ArrayList)18 IOException (java.io.IOException)9 Inet6Address (java.net.Inet6Address)8 UnknownHostException (java.net.UnknownHostException)7 NotifyListener (net.mm2d.upnp.SsdpNotifyReceiver.NotifyListener)7 Command (com.android.server.NativeDaemonConnector.Command)6 LinkAddress (android.net.LinkAddress)5 DatagramPacket (java.net.DatagramPacket)5 List (java.util.List)5 InetSocketAddress (java.net.InetSocketAddress)4 SharedPreferences (android.content.SharedPreferences)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Enumeration (java.util.Enumeration)3 LinkedList (java.util.LinkedList)3