Search in sources :

Example 91 with NetworkInterface

use of java.net.NetworkInterface in project teaTime by ancfdy.

the class AppSysMgr method getSysLocalIpAddress.

/**
     * 获取手机IP地址
     * @return String 手机IP地址
     */
public String getSysLocalIpAddress() {
    String hostAddress = null;
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    hostAddress = inetAddress.getHostAddress();
                }
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
        AppLogMessageMgr.e("AppSysMgr-->>getSysLocalIpAddress", e.getMessage().toString());
    }
    AppLogMessageMgr.i("AppSysMgr-->>getSysLocalIpAddress", hostAddress);
    return hostAddress;
}
Also used : SocketException(java.net.SocketException) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 92 with NetworkInterface

use of java.net.NetworkInterface in project teaTime by ancfdy.

the class AppSysMgr method getSysLocalIpAddress.

/**
     * 获取手机IP地址
     * @return String 手机IP地址
     */
public String getSysLocalIpAddress() {
    String hostAddress = null;
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    hostAddress = inetAddress.getHostAddress();
                }
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
        AppLogMessageMgr.e("AppSysMgr-->>getSysLocalIpAddress", e.getMessage().toString());
    }
    AppLogMessageMgr.i("AppSysMgr-->>getSysLocalIpAddress", hostAddress);
    return hostAddress;
}
Also used : SocketException(java.net.SocketException) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 93 with NetworkInterface

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

the class ServerResourceBase method configure.

@Override
public boolean configure(final String name, Map<String, Object> params) throws ConfigurationException {
    _name = name;
    String publicNic = (String) params.get("public.network.device");
    if (publicNic == null) {
        publicNic = "xenbr1";
    }
    String privateNic = (String) params.get("private.network.device");
    if (privateNic == null) {
        privateNic = "xenbr0";
    }
    final String storageNic = (String) params.get("storage.network.device");
    final String storageNic2 = (String) params.get("storage.network.device.2");
    _privateNic = getNetworkInterface(privateNic);
    _publicNic = getNetworkInterface(publicNic);
    _storageNic = getNetworkInterface(storageNic);
    _storageNic2 = getNetworkInterface(storageNic2);
    if (_privateNic == null) {
        s_logger.warn("Nics are not specified in properties file/db, will try to autodiscover");
        Enumeration<NetworkInterface> nics = null;
        try {
            nics = NetworkInterface.getNetworkInterfaces();
            if (nics == null || !nics.hasMoreElements()) {
                throw new ConfigurationException("Private NIC is not configured");
            }
        } catch (final SocketException e) {
            throw new ConfigurationException("Private NIC is not configured");
        }
        while (nics.hasMoreElements()) {
            final NetworkInterface nic = nics.nextElement();
            final String nicName = nic.getName();
            //  try {
            if (//nic.isUp() &&
            !nic.isVirtual() && !nicName.startsWith("vnif") && !nicName.startsWith("vnbr") && !nicName.startsWith("peth") && !nicName.startsWith("vif") && !nicName.startsWith("virbr") && !nicName.contains(":")) {
                final String[] info = NetUtils.getNicParams(nicName);
                if (info != null && info[0] != null) {
                    _privateNic = nic;
                    s_logger.info("Designating private to be nic " + nicName);
                    break;
                }
            }
            //      } catch (final SocketException e) {
            //        s_logger.warn("Error looking at " + nicName, e);
            //  }
            s_logger.debug("Skipping nic " + nicName);
        }
        if (_privateNic == null) {
            throw new ConfigurationException("Private NIC is not configured");
        }
    }
    String[] infos = NetUtils.getNetworkParams(_privateNic);
    if (infos == null) {
        s_logger.warn("Incorrect details for private Nic during initialization of ServerResourceBase");
        return false;
    }
    params.put("host.ip", infos[0]);
    params.put("host.mac.address", infos[1]);
    return true;
}
Also used : SocketException(java.net.SocketException) ConfigurationException(javax.naming.ConfigurationException) NetworkInterface(java.net.NetworkInterface)

Example 94 with NetworkInterface

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

the class ServerResourceBase method getNetworkInterface.

protected NetworkInterface getNetworkInterface(String nicName) {
    s_logger.debug("Retrieving network interface: " + nicName);
    if (nicName == null) {
        return null;
    }
    if (nicName.trim().length() == 0) {
        return null;
    }
    nicName = nicName.trim();
    NetworkInterface nic;
    try {
        nic = NetworkInterface.getByName(nicName);
        if (nic == null) {
            s_logger.debug("Unable to get network interface for " + nicName);
            return null;
        }
        return nic;
    } catch (final SocketException e) {
        s_logger.warn("Unable to get network interface for " + nicName, e);
        return null;
    }
}
Also used : SocketException(java.net.SocketException) NetworkInterface(java.net.NetworkInterface)

Example 95 with NetworkInterface

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

the class NetUtils method getDefaultHostIp.

public static String getDefaultHostIp() {
    if (SystemUtils.IS_OS_WINDOWS) {
        final Pattern pattern = Pattern.compile("\\s*0.0.0.0\\s*0.0.0.0\\s*(\\S*)\\s*(\\S*)\\s*");
        try {
            final Process result = Runtime.getRuntime().exec("route print -4");
            final BufferedReader output = new BufferedReader(new InputStreamReader(result.getInputStream()));
            String line = output.readLine();
            while (line != null) {
                final Matcher matcher = pattern.matcher(line);
                if (matcher.find()) {
                    return matcher.group(2);
                }
                line = output.readLine();
            }
        } catch (final IOException e) {
            s_logger.debug("Caught IOException", e);
        }
        return null;
    } else {
        NetworkInterface nic = null;
        final String pubNic = getDefaultEthDevice();
        if (pubNic == null) {
            return null;
        }
        try {
            nic = NetworkInterface.getByName(pubNic);
        } catch (final SocketException e) {
            return null;
        }
        String[] info = null;
        try {
            info = NetUtils.getNetworkParams(nic);
        } catch (final NullPointerException ignored) {
            s_logger.debug("Caught NullPointerException when trying to getDefaultHostIp");
        }
        if (info != null) {
            return info[0];
        }
        return null;
    }
}
Also used : Pattern(java.util.regex.Pattern) SocketException(java.net.SocketException) InputStreamReader(java.io.InputStreamReader) Matcher(java.util.regex.Matcher) BufferedReader(java.io.BufferedReader) NetworkInterface(java.net.NetworkInterface) IOException(java.io.IOException)

Aggregations

NetworkInterface (java.net.NetworkInterface)225 InetAddress (java.net.InetAddress)178 SocketException (java.net.SocketException)112 ArrayList (java.util.ArrayList)42 Inet4Address (java.net.Inet4Address)39 UnknownHostException (java.net.UnknownHostException)36 InterfaceAddress (java.net.InterfaceAddress)32 Inet6Address (java.net.Inet6Address)29 IOException (java.io.IOException)25 Enumeration (java.util.Enumeration)9 InetSocketAddress (java.net.InetSocketAddress)7 HashSet (java.util.HashSet)7 LinkedHashSet (java.util.LinkedHashSet)7 Command (com.android.server.NativeDaemonConnector.Command)6 Test (org.junit.Test)6 DatagramSocket (java.net.DatagramSocket)5 File (java.io.File)4 MulticastSocket (java.net.MulticastSocket)4 List (java.util.List)4 SharedPreferences (android.content.SharedPreferences)3