Search in sources :

Example 91 with InterfaceAddress

use of java.net.InterfaceAddress in project DistributedFractalNetwork by Budder21.

the class Utils method getBroadcastAddress.

/**
 * This method finds and returns the broadcast InetAddress of the specific
 * router being accessed
 *
 * @return the broadcast InetAddress of the specific router being accessed
 */
public static InetAddress getBroadcastAddress() {
    try {
        Set<InetAddress> set = new LinkedHashSet<>();
        Enumeration<NetworkInterface> nicList = NetworkInterface.getNetworkInterfaces();
        for (; nicList.hasMoreElements(); ) {
            NetworkInterface nic = nicList.nextElement();
            if (nic.isUp() && !nic.isLoopback()) {
                for (InterfaceAddress ia : nic.getInterfaceAddresses()) set.add(ia.getBroadcast());
            }
        }
        return Arrays.asList(set.toArray(new InetAddress[0])).get(0);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) InterfaceAddress(java.net.InterfaceAddress) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress) SocketException(java.net.SocketException)

Example 92 with InterfaceAddress

use of java.net.InterfaceAddress in project nutz by nutzam.

the class Networks method networkItems.

public static Map<String, NetworkItem> networkItems() {
    Map<String, NetworkItem> netFaces = new LinkedHashMap<String, NetworkItem>();
    try {
        Enumeration<NetworkInterface> network = NetworkInterface.getNetworkInterfaces();
        while (network.hasMoreElements()) {
            NetworkItem netItem = new NetworkItem();
            NetworkInterface face = network.nextElement();
            byte[] data = face.getHardwareAddress();
            try {
                if (data != null && data.length > 0) {
                    StringBuilder sb = new StringBuilder();
                    for (byte b : data) sb.append(Strings.toHex(b, 2));
                    netItem.setMac(sb.toString().toUpperCase());
                    if (netItem.getMac().startsWith("000000000"))
                        continue;
                }
            } catch (Throwable e) {
            }
            List<InterfaceAddress> addrs = face.getInterfaceAddresses();
            if (addrs != null && !addrs.isEmpty()) {
                for (InterfaceAddress interfaceAddress : addrs) {
                    InetAddress iaddr = interfaceAddress.getAddress();
                    String ip = iaddr.getHostAddress();
                    if (ip == null || ip.length() == 0)
                        continue;
                    if (!netItem.hasName())
                        netItem.setName(iaddr.getHostName());
                    if (ip.contains("."))
                        netItem.setIpv4(ip);
                    else
                        netItem.setIpv6(ip);
                }
            }
            netItem.setMtu(face.getMTU());
            netItem.setDisplay(face.getDisplayName());
            if (!netItem.hasName()) {
                netItem.setName(face.getName());
            }
            if (netItem.getIpv4() == null && netItem.getMac() == null && netItem.getMtu() < 1 && !face.getName().startsWith("eth"))
                continue;
            netFaces.put(face.getName(), netItem);
        }
    } catch (Throwable e) {
    }
    if (Lang.isWin() && netFaces.size() > 0) {
        for (Entry<String, NetworkItem> en : netFaces.entrySet()) {
            NetworkItem item = en.getValue();
            if (item != null && ipOk(item.getIpv4()) && item.getIpv4().startsWith("10.")) {
                netFaces.put("tun0", item);
                break;
            }
        }
    }
    return netFaces;
}
Also used : InterfaceAddress(java.net.InterfaceAddress) NetworkInterface(java.net.NetworkInterface) LinkedHashMap(java.util.LinkedHashMap) InetAddress(java.net.InetAddress)

Example 93 with InterfaceAddress

use of java.net.InterfaceAddress in project Fling by entertailion.

the class FlingFrame method getPreferredInetAddress.

private InterfaceAddress getPreferredInetAddress(String prefix) {
    InterfaceAddress selectedInterfaceAddress = null;
    try {
        Enumeration<NetworkInterface> list = NetworkInterface.getNetworkInterfaces();
        while (list.hasMoreElements()) {
            NetworkInterface iface = list.nextElement();
            if (iface == null)
                continue;
            Log.d(LOG_TAG, "interface=" + iface.getName());
            Iterator<InterfaceAddress> it = iface.getInterfaceAddresses().iterator();
            while (it.hasNext()) {
                InterfaceAddress interfaceAddress = it.next();
                if (interfaceAddress == null)
                    continue;
                InetAddress address = interfaceAddress.getAddress();
                Log.d(LOG_TAG, "address=" + address);
                if (address instanceof Inet4Address) {
                    // same subnet as the selected ChromeCast device
                    if (address.getHostAddress().toString().startsWith(prefix)) {
                        return interfaceAddress;
                    }
                }
            }
        }
    } catch (Exception ex) {
    }
    return selectedInterfaceAddress;
}
Also used : Inet4Address(java.net.Inet4Address) InterfaceAddress(java.net.InterfaceAddress) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 94 with InterfaceAddress

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

the class HttpServer method findLocalIpAddress.

private static String findLocalIpAddress() throws IOException {
    String defaultIpOverride = System.getenv("CALCITE_LOCAL_IP");
    if (defaultIpOverride != null) {
        return defaultIpOverride;
    } else {
        final InetAddress address = InetAddress.getLocalHost();
        if (address.isLoopbackAddress()) {
            // interfaces.
            for (NetworkInterface ni : iterable(NetworkInterface.getNetworkInterfaces())) {
                for (InterfaceAddress interfaceAddress : ni.getInterfaceAddresses()) {
                    final InetAddress addr = interfaceAddress.getAddress();
                    if (!addr.isLinkLocalAddress() && !addr.isLoopbackAddress() && addr instanceof Inet4Address) {
                        // We've found an address that looks reasonable!
                        logWarning("Your hostname, " + InetAddress.getLocalHost().getHostName() + " resolves to a loopback address: " + address.getHostAddress() + "; using " + addr.getHostAddress() + " instead (on interface " + ni.getName() + ")");
                        logWarning("Set CALCITE_LOCAL_IP if you need to bind to another address");
                        return addr.getHostAddress();
                    }
                }
            }
            logWarning("Your hostname, " + InetAddress.getLocalHost().getHostName() + " resolves to a loopback address: " + address.getHostAddress() + ", but we couldn't find any external IP address!");
            logWarning("Set CALCITE_LOCAL_IP if you need to bind to another address");
        }
        return address.getHostAddress();
    }
}
Also used : Inet4Address(java.net.Inet4Address) InterfaceAddress(java.net.InterfaceAddress) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 95 with InterfaceAddress

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

the class NetUtils method getLocalCidrs.

public static String[] getLocalCidrs() {
    final String defaultHostIp = getDefaultHostIp();
    final List<String> cidrList = new ArrayList<String>();
    try {
        for (final NetworkInterface ifc : IteratorUtil.enumerationAsIterable(NetworkInterface.getNetworkInterfaces())) {
            if (ifc.isUp() && !ifc.isVirtual() && !ifc.isLoopback()) {
                for (final InterfaceAddress address : ifc.getInterfaceAddresses()) {
                    final InetAddress addr = address.getAddress();
                    final int prefixLength = address.getNetworkPrefixLength();
                    if (prefixLength < MAX_CIDR && prefixLength > 0) {
                        final String ip = addr.getHostAddress();
                        if (ip.equalsIgnoreCase(defaultHostIp)) {
                            cidrList.add(ipAndNetMaskToCidr(ip, getCidrNetmask(prefixLength)));
                        }
                    }
                }
            }
        }
    } catch (final SocketException e) {
        s_logger.warn("UnknownHostException in getLocalCidrs().", e);
    }
    return cidrList.toArray(new String[0]);
}
Also used : SocketException(java.net.SocketException) InterfaceAddress(java.net.InterfaceAddress) ArrayList(java.util.ArrayList) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

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