Search in sources :

Example 1 with InterfaceAddress

use of java.net.InterfaceAddress 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 InterfaceAddress

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

the class ExtraCounter method buildNetworkSnapshot.

public void buildNetworkSnapshot() {
    try {
        NetworkInformation netInfo = new NetworkInformation();
        netInfo.setTotalMemory(Runtime.getRuntime().maxMemory());
        netInfo.setAvailableMemory(Runtime.getRuntime().freeMemory());
        String sparkIp = System.getenv("SPARK_PUBLIC_DNS");
        if (sparkIp != null) {
            // if spark ip is defined, we just use it, and don't bother with other interfaces
            netInfo.addIpAddress(sparkIp);
        } else {
            // sparkIp wasn't defined, so we'll go for heuristics here
            List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
            for (NetworkInterface networkInterface : interfaces) {
                if (networkInterface.isLoopback() || !networkInterface.isUp())
                    continue;
                for (InterfaceAddress address : networkInterface.getInterfaceAddresses()) {
                    String addr = address.getAddress().getHostAddress();
                    if (addr == null || addr.isEmpty() || addr.contains(":"))
                        continue;
                    netInfo.getIpAddresses().add(addr);
                }
            }
        }
        networkInformation.add(netInfo);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : InterfaceAddress(java.net.InterfaceAddress) NetworkInterface(java.net.NetworkInterface)

Example 3 with InterfaceAddress

use of java.net.InterfaceAddress in project openhab1-addons by openhab.

the class HarmonyHubDiscovery method sendDiscoveryMessage.

/**
     * Send broadcast message over all active interfaces
     *
     * @param discoverString
     *            String to be used for the discovery
     */
private void sendDiscoveryMessage(String discoverString) {
    DatagramSocket bcSend = null;
    try {
        bcSend = new DatagramSocket();
        bcSend.setBroadcast(true);
        byte[] sendData = discoverString.getBytes();
        // Broadcast the message over all the network interfaces
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface networkInterface = interfaces.nextElement();
            if (networkInterface.isLoopback() || !networkInterface.isUp()) {
                continue;
            }
            for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
                InetAddress[] broadcast = new InetAddress[3];
                broadcast[0] = InetAddress.getByName("224.0.0.1");
                broadcast[1] = InetAddress.getByName("255.255.255.255");
                broadcast[2] = interfaceAddress.getBroadcast();
                broadcast[3] = InetAddress.getByName(optionalHost);
                for (InetAddress bc : broadcast) {
                    // Send the broadcast package!
                    if (bc != null && !bc.isLoopbackAddress()) {
                        try {
                            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, bc, DISCO_PORT);
                            bcSend.send(sendPacket);
                        } catch (IOException e) {
                            logger.error("IO error during HarmonyHub discovery: {}", e.getMessage());
                        } catch (Exception e) {
                            logger.error(e.getMessage(), e);
                        }
                        logger.trace("Request packet sent to: {} Interface: {}", bc.getHostAddress(), networkInterface.getDisplayName());
                    }
                }
            }
        }
    } catch (IOException e) {
        logger.debug("IO error during HarmonyHub discovery: {}", e.getMessage());
    } finally {
        try {
            if (bcSend != null) {
                bcSend.close();
            }
        } catch (Exception e) {
        // Ignore
        }
    }
}
Also used : DatagramSocket(java.net.DatagramSocket) InterfaceAddress(java.net.InterfaceAddress) DatagramPacket(java.net.DatagramPacket) NetworkInterface(java.net.NetworkInterface) IOException(java.io.IOException) InetAddress(java.net.InetAddress) IOException(java.io.IOException)

Example 4 with InterfaceAddress

use of java.net.InterfaceAddress in project openhab1-addons by openhab.

the class SsdpDiscovery method getBroadCastAddress.

private static List<InetAddress> getBroadCastAddress() throws Exception {
    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    List<InetAddress> addresses = new ArrayList<InetAddress>();
    addresses.add(InetAddress.getByName("255.255.255.255"));
    while (interfaces.hasMoreElements()) {
        NetworkInterface networkInterface = interfaces.nextElement();
        if (networkInterface.isLoopback() || !networkInterface.supportsMulticast()) {
            // Don't want to broadcast to the loopback interface
            continue;
        }
        for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
            InetAddress broadcast = interfaceAddress.getBroadcast();
            if (broadcast != null) {
                addresses.add(broadcast);
            }
        }
    }
    return addresses;
}
Also used : InterfaceAddress(java.net.InterfaceAddress) ArrayList(java.util.ArrayList) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 5 with InterfaceAddress

use of java.net.InterfaceAddress in project openhab1-addons by openhab.

the class MaxCubeDiscover method discoverIp.

/**
     * Automatic UDP discovery of a MAX!Cube
     * 
     * @return if the cube is found, returns the IP address as a string. Otherwise returns null
     */
public static final String discoverIp() {
    String maxCubeIP = null;
    String maxCubeName = null;
    String rfAddress = null;
    Logger logger = LoggerFactory.getLogger(MaxCubeDiscover.class);
    DatagramSocket bcReceipt = null;
    DatagramSocket bcSend = null;
    // Find the MaxCube using UDP broadcast
    try {
        bcSend = new DatagramSocket();
        bcSend.setBroadcast(true);
        byte[] sendData = "eQ3Max*\0**********I".getBytes();
        // Broadcast the message over all the network interfaces
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface networkInterface = interfaces.nextElement();
            if (networkInterface.isLoopback() || !networkInterface.isUp()) {
                continue;
            }
            for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
                InetAddress[] broadcast = new InetAddress[2];
                broadcast[0] = InetAddress.getByName("224.0.0.1");
                broadcast[1] = interfaceAddress.getBroadcast();
                for (InetAddress bc : broadcast) {
                    // Send the broadcast package!
                    if (bc != null) {
                        try {
                            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, bc, 23272);
                            bcSend.send(sendPacket);
                        } catch (IOException e) {
                            logger.debug("IO error during MAX! Cube discovery: {}", e.getMessage());
                        } catch (Exception e) {
                            logger.debug(e.getMessage());
                            logger.debug(Utils.getStackTrace(e));
                        }
                        logger.trace("Request packet sent to: {} Interface: {}", bc.getHostAddress(), networkInterface.getDisplayName());
                    }
                }
            }
        }
        logger.trace("Done looping over all network interfaces. Now waiting for a reply!");
        bcSend.close();
        bcReceipt = new DatagramSocket(23272);
        bcReceipt.setReuseAddress(true);
        // Wait for a response
        byte[] recvBuf = new byte[15000];
        DatagramPacket receivePacket = new DatagramPacket(recvBuf, recvBuf.length);
        bcReceipt.receive(receivePacket);
        // We have a response
        logger.trace("Broadcast response from server: {}", receivePacket.getAddress());
        // Check if the message is correct
        String message = new String(receivePacket.getData()).trim();
        if (message.startsWith("eQ3Max")) {
            maxCubeIP = receivePacket.getAddress().getHostAddress();
            maxCubeName = message.substring(0, 8);
            rfAddress = message.substring(8, 18);
            logger.debug("Found at: {}", maxCubeIP);
            logger.debug("Name    : {}", maxCubeName);
            logger.debug("Serial  : {}", rfAddress);
            logger.trace("Message : {}", message);
        } else {
            logger.info("No Max!Cube gateway found on network");
        }
    } catch (IOException e) {
        logger.debug("IO error during MAX! Cube discovery: {}", e.getMessage());
    } catch (Exception e) {
        logger.debug(e.getMessage());
        logger.debug(Utils.getStackTrace(e));
    } finally {
        try {
            if (bcReceipt != null) {
                bcReceipt.close();
            }
        } catch (Exception e) {
            logger.debug(e.toString());
        }
        try {
            if (bcSend != null) {
                bcSend.close();
            }
        } catch (Exception e) {
            logger.debug(e.toString());
        }
    }
    return maxCubeIP;
}
Also used : DatagramSocket(java.net.DatagramSocket) InterfaceAddress(java.net.InterfaceAddress) DatagramPacket(java.net.DatagramPacket) NetworkInterface(java.net.NetworkInterface) IOException(java.io.IOException) Logger(org.slf4j.Logger) InetAddress(java.net.InetAddress) IOException(java.io.IOException)

Aggregations

InterfaceAddress (java.net.InterfaceAddress)96 NetworkInterface (java.net.NetworkInterface)65 InetAddress (java.net.InetAddress)54 Inet4Address (java.net.Inet4Address)26 SocketException (java.net.SocketException)25 Test (org.junit.Test)19 ArrayList (java.util.ArrayList)16 IOException (java.io.IOException)9 Inet6Address (java.net.Inet6Address)7 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