Search in sources :

Example 1 with NetworkInterface

use of java.net.NetworkInterface in project elasticsearch by elastic.

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 2 with NetworkInterface

use of java.net.NetworkInterface in project cw-omnibus by commonsguy.

the class WebServerService method raiseReadyEvent.

private void raiseReadyEvent() {
    ServerStartedEvent event = new ServerStartedEvent();
    try {
        for (Enumeration<NetworkInterface> enInterfaces = NetworkInterface.getNetworkInterfaces(); enInterfaces.hasMoreElements(); ) {
            NetworkInterface ni = enInterfaces.nextElement();
            for (Enumeration<InetAddress> enAddresses = ni.getInetAddresses(); enAddresses.hasMoreElements(); ) {
                InetAddress addr = enAddresses.nextElement();
                if (addr instanceof Inet4Address) {
                    event.addUrl("http://" + addr.getHostAddress() + ":4999" + rootPath + "/");
                }
            }
        }
    } catch (SocketException ex) {
        ex.printStackTrace();
    }
    EventBus.getDefault().removeAllStickyEvents();
    EventBus.getDefault().postSticky(event);
}
Also used : SocketException(java.net.SocketException) Inet4Address(java.net.Inet4Address) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 3 with NetworkInterface

use of java.net.NetworkInterface in project cw-omnibus by commonsguy.

the class WebServerService method raiseReadyEvent.

private void raiseReadyEvent() {
    ServerStartedEvent event = new ServerStartedEvent();
    try {
        for (Enumeration<NetworkInterface> enInterfaces = NetworkInterface.getNetworkInterfaces(); enInterfaces.hasMoreElements(); ) {
            NetworkInterface ni = enInterfaces.nextElement();
            for (Enumeration<InetAddress> enAddresses = ni.getInetAddresses(); enAddresses.hasMoreElements(); ) {
                InetAddress addr = enAddresses.nextElement();
                if (addr instanceof Inet4Address) {
                    event.addUrl("http://" + addr.getHostAddress() + ":4999" + rootPath + "/");
                }
            }
        }
    } catch (SocketException ex) {
        ex.printStackTrace();
    }
    EventBus.getDefault().removeAllStickyEvents();
    EventBus.getDefault().postSticky(event);
}
Also used : SocketException(java.net.SocketException) Inet4Address(java.net.Inet4Address) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 4 with NetworkInterface

use of java.net.NetworkInterface in project cw-omnibus by commonsguy.

the class WebServerService method raiseStartedEvent.

private void raiseStartedEvent() {
    ServerStartedEvent event = new ServerStartedEvent();
    try {
        for (Enumeration<NetworkInterface> enInterfaces = NetworkInterface.getNetworkInterfaces(); enInterfaces.hasMoreElements(); ) {
            NetworkInterface ni = enInterfaces.nextElement();
            for (Enumeration<InetAddress> enAddresses = ni.getInetAddresses(); enAddresses.hasMoreElements(); ) {
                InetAddress addr = enAddresses.nextElement();
                if (addr instanceof Inet4Address) {
                    event.addUrl("http://" + addr.getHostAddress() + ":4999");
                }
            }
        }
    } catch (SocketException e) {
        Log.e(getClass().getSimpleName(), "Exception in IP addresses", e);
    }
    EventBus.getDefault().removeAllStickyEvents();
    EventBus.getDefault().postSticky(event);
}
Also used : SocketException(java.net.SocketException) Inet4Address(java.net.Inet4Address) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 5 with NetworkInterface

use of java.net.NetworkInterface in project hadoop by apache.

the class DNS method getIPs.

/**
   * Returns all the IPs associated with the provided interface, if any, in
   * textual form.
   * 
   * @param strInterface
   *            The name of the network interface or sub-interface to query
   *            (eg eth0 or eth0:0) or the string "default"
   * @param returnSubinterfaces
   *            Whether to return IPs associated with subinterfaces of
   *            the given interface
   * @return A string vector of all the IPs associated with the provided
   *         interface. The local host IP is returned if the interface
   *         name "default" is specified or there is an I/O error looking
   *         for the given interface.
   * @throws UnknownHostException
   *             If the given interface is invalid
   * 
   */
public static String[] getIPs(String strInterface, boolean returnSubinterfaces) throws UnknownHostException {
    if ("default".equals(strInterface)) {
        return new String[] { cachedHostAddress };
    }
    NetworkInterface netIf;
    try {
        netIf = NetworkInterface.getByName(strInterface);
        if (netIf == null) {
            netIf = getSubinterface(strInterface);
        }
    } catch (SocketException e) {
        LOG.warn("I/O error finding interface " + strInterface + ": " + e.getMessage());
        return new String[] { cachedHostAddress };
    }
    if (netIf == null) {
        throw new UnknownHostException("No such interface " + strInterface);
    }
    // NB: Using a LinkedHashSet to preserve the order for callers
    // that depend on a particular element being 1st in the array.
    // For example, getDefaultIP always returns the first element.
    LinkedHashSet<InetAddress> allAddrs = new LinkedHashSet<InetAddress>();
    allAddrs.addAll(Collections.list(netIf.getInetAddresses()));
    if (!returnSubinterfaces) {
        allAddrs.removeAll(getSubinterfaceInetAddrs(netIf));
    }
    String[] ips = new String[allAddrs.size()];
    int i = 0;
    for (InetAddress addr : allAddrs) {
        ips[i++] = addr.getHostAddress();
    }
    return ips;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SocketException(java.net.SocketException) UnknownHostException(java.net.UnknownHostException) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Aggregations

NetworkInterface (java.net.NetworkInterface)569 InetAddress (java.net.InetAddress)421 SocketException (java.net.SocketException)260 ArrayList (java.util.ArrayList)105 Inet4Address (java.net.Inet4Address)101 UnknownHostException (java.net.UnknownHostException)79 IOException (java.io.IOException)67 InterfaceAddress (java.net.InterfaceAddress)66 Test (org.junit.Test)60 Inet6Address (java.net.Inet6Address)57 Enumeration (java.util.Enumeration)44 InetSocketAddress (java.net.InetSocketAddress)22 HashSet (java.util.HashSet)22 LinkedHashSet (java.util.LinkedHashSet)15 List (java.util.List)14 DatagramPacket (java.net.DatagramPacket)12 MulticastSocket (java.net.MulticastSocket)12 LinkedList (java.util.LinkedList)10 HashMap (java.util.HashMap)9 Collections (java.util.Collections)8