Example usage for java.net NetworkInterface getNetworkInterfaces

List of usage examples for java.net NetworkInterface getNetworkInterfaces

Introduction

In this page you can find the example usage for java.net NetworkInterface getNetworkInterfaces.

Prototype

public static Enumeration<NetworkInterface> getNetworkInterfaces() throws SocketException 

Source Link

Document

Returns an Enumeration of all the interfaces on this machine.

Usage

From source file:eu.faircode.netguard.ServiceSinkhole.java

public static List<InetAddress> getDns(Context context) {
    List<InetAddress> listDns = new ArrayList<>();
    List<String> sysDns = Util.getDefaultDNS(context);

    // Get custom DNS servers
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    boolean ip6 = prefs.getBoolean("ip6", true);
    String vpnDns1 = prefs.getString("dns", null);
    String vpnDns2 = prefs.getString("dns2", null);
    Log.i(TAG, "DNS system=" + TextUtils.join(",", sysDns) + " VPN1=" + vpnDns1 + " VPN2=" + vpnDns2);

    if (vpnDns1 != null)
        try {/*  ww w  . j  a v  a2  s  .  co m*/
            InetAddress dns = InetAddress.getByName(vpnDns1);
            if (!(dns.isLoopbackAddress() || dns.isAnyLocalAddress()) && (ip6 || dns instanceof Inet4Address))
                listDns.add(dns);
        } catch (Throwable ignored) {
        }

    if (vpnDns2 != null)
        try {
            InetAddress dns = InetAddress.getByName(vpnDns2);
            if (!(dns.isLoopbackAddress() || dns.isAnyLocalAddress()) && (ip6 || dns instanceof Inet4Address))
                listDns.add(dns);
        } catch (Throwable ex) {
            Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
        }

    // Use system DNS servers only when no two custom DNS servers specified
    if (listDns.size() <= 1)
        for (String def_dns : sysDns)
            try {
                InetAddress ddns = InetAddress.getByName(def_dns);
                if (!listDns.contains(ddns) && !(ddns.isLoopbackAddress() || ddns.isAnyLocalAddress())
                        && (ip6 || ddns instanceof Inet4Address))
                    listDns.add(ddns);
            } catch (Throwable ex) {
                Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
            }

    // Remove local DNS servers when not routing LAN
    boolean lan = prefs.getBoolean("lan", false);
    boolean use_hosts = prefs.getBoolean("filter", false) && prefs.getBoolean("use_hosts", false);
    if (lan && use_hosts) {
        List<InetAddress> listLocal = new ArrayList<>();
        try {
            Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
            if (nis != null)
                while (nis.hasMoreElements()) {
                    NetworkInterface ni = nis.nextElement();
                    if (ni != null && ni.isUp() && !ni.isLoopback()) {
                        List<InterfaceAddress> ias = ni.getInterfaceAddresses();
                        if (ias != null)
                            for (InterfaceAddress ia : ias) {
                                InetAddress hostAddress = ia.getAddress();
                                BigInteger host = new BigInteger(1, hostAddress.getAddress());

                                int prefix = ia.getNetworkPrefixLength();
                                BigInteger mask = BigInteger.valueOf(-1)
                                        .shiftLeft(hostAddress.getAddress().length * 8 - prefix);

                                for (InetAddress dns : listDns)
                                    if (hostAddress.getAddress().length == dns.getAddress().length) {
                                        BigInteger ip = new BigInteger(1, dns.getAddress());

                                        if (host.and(mask).equals(ip.and(mask))) {
                                            Log.i(TAG, "Local DNS server host=" + hostAddress + "/" + prefix
                                                    + " dns=" + dns);
                                            listLocal.add(dns);
                                        }
                                    }
                            }
                    }
                }
        } catch (Throwable ex) {
            Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
        }

        List<InetAddress> listDns4 = new ArrayList<>();
        List<InetAddress> listDns6 = new ArrayList<>();
        try {
            listDns4.add(InetAddress.getByName("8.8.8.8"));
            listDns4.add(InetAddress.getByName("8.8.4.4"));
            if (ip6) {
                listDns6.add(InetAddress.getByName("2001:4860:4860::8888"));
                listDns6.add(InetAddress.getByName("2001:4860:4860::8844"));
            }

        } catch (Throwable ex) {
            Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
        }

        for (InetAddress dns : listLocal) {
            listDns.remove(dns);
            if (dns instanceof Inet4Address) {
                if (listDns4.size() > 0) {
                    listDns.add(listDns4.get(0));
                    listDns4.remove(0);
                }
            } else {
                if (listDns6.size() > 0) {
                    listDns.add(listDns6.get(0));
                    listDns6.remove(0);
                }
            }
        }
    }

    return listDns;
}

From source file:com.safi.asterisk.handler.SafletEngine.java

public static String getMacAddress() {
    try {/*  ww  w  .j a v a2 s .c om*/

        Enumeration<NetworkInterface> enm = NetworkInterface.getNetworkInterfaces();
        while (enm.hasMoreElements()) {
            // for(int i=0;i<addresses.length;i++){
            NetworkInterface ni = enm.nextElement();
            try {
                byte[] mac = ni.getHardwareAddress();

                String macTarget = getMacString(mac);
                if (StringUtils.isNotBlank(macTarget))
                    return macTarget.toUpperCase();
            } catch (Exception ignore) {
                if (log.isDebugEnabled())
                    log.debug("Skipping " + ni.getDisplayName());
            }
        }

    } catch (Exception ex) {
    }
    return "";
}

From source file:org.commonjava.maven.galley.cache.infinispan.FastLocalCacheProvider.java

private String getCurrentNodeIp() throws SocketException {

    final Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();

    Inet4Address top = null;//from   w ww . j a  va 2 s  .  co  m
    while (nis.hasMoreElements()) {
        final NetworkInterface ni = nis.nextElement();
        final Enumeration<InetAddress> ips = ni.getInetAddresses();

        while (ips.hasMoreElements()) {
            final InetAddress ip = ips.nextElement();
            if (ip instanceof Inet4Address && !ip.isLinkLocalAddress()) {
                if (top == null) {
                    top = (Inet4Address) ip;
                } else if (!top.isSiteLocalAddress() && ip.isSiteLocalAddress()) {
                    top = (Inet4Address) ip;
                }
            }
        }
    }

    if (top == null) {
        throw new IllegalStateException("[galley] IP not found.");
    }

    return top.getHostAddress();
}

From source file:com.zhengde163.netguard.ServiceSinkhole.java

private Builder getBuilder(List<Rule> listAllowed, List<Rule> listRule) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    boolean subnet = prefs.getBoolean("subnet", false);
    boolean tethering = prefs.getBoolean("tethering", false);
    boolean lan = prefs.getBoolean("lan", false);
    boolean ip6 = prefs.getBoolean("ip6", true);
    boolean filter = prefs.getBoolean("filter", false);
    boolean system = prefs.getBoolean("manage_system", false);

    // Build VPN service
    Builder builder = new Builder();
    builder.setSession(getString(R.string.app_name));

    // VPN address
    String vpn4 = prefs.getString("vpn4", "10.1.10.1");
    Log.i(TAG, "vpn4=" + vpn4);
    builder.addAddress(vpn4, 32);/*from  ww w. j a va  2 s.  c o m*/
    if (ip6) {
        String vpn6 = prefs.getString("vpn6", "fd00:1:fd00:1:fd00:1:fd00:1");
        Log.i(TAG, "vpn6=" + vpn6);
        builder.addAddress(vpn6, 128);
    }

    // DNS address
    if (filter)
        for (InetAddress dns : getDns(ServiceSinkhole.this)) {
            if (ip6 || dns instanceof Inet4Address) {
                Log.i(TAG, "dns=" + dns);
                builder.addDnsServer(dns);
            }
        }

    // Subnet routing
    if (subnet) {
        // Exclude IP ranges
        List<IPUtil.CIDR> listExclude = new ArrayList<>();
        listExclude.add(new IPUtil.CIDR("127.0.0.0", 8)); // localhost

        if (tethering) {
            // USB Tethering 192.168.42.x
            // Wi-Fi Tethering 192.168.43.x
            listExclude.add(new IPUtil.CIDR("192.168.42.0", 23));
        }

        if (lan) {
            try {
                Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
                while (nis.hasMoreElements()) {
                    NetworkInterface ni = nis.nextElement();
                    if (ni != null && ni.isUp() && !ni.isLoopback() && ni.getName() != null
                            && !ni.getName().startsWith("tun"))
                        for (InterfaceAddress ia : ni.getInterfaceAddresses())
                            if (ia.getAddress() instanceof Inet4Address) {
                                IPUtil.CIDR local = new IPUtil.CIDR(ia.getAddress(),
                                        ia.getNetworkPrefixLength());
                                Log.i(TAG, "Excluding " + ni.getName() + " " + local);
                                listExclude.add(local);
                            }
                }
            } catch (SocketException ex) {
                Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
            }
        }

        Configuration config = getResources().getConfiguration();
        if (config.mcc == 310 && config.mnc == 260) {
            // T-Mobile Wi-Fi calling
            listExclude.add(new IPUtil.CIDR("66.94.2.0", 24));
            listExclude.add(new IPUtil.CIDR("66.94.6.0", 23));
            listExclude.add(new IPUtil.CIDR("66.94.8.0", 22));
            listExclude.add(new IPUtil.CIDR("208.54.0.0", 16));
        }
        listExclude.add(new IPUtil.CIDR("224.0.0.0", 3)); // broadcast

        Collections.sort(listExclude);

        try {
            InetAddress start = InetAddress.getByName("0.0.0.0");
            for (IPUtil.CIDR exclude : listExclude) {
                Log.i(TAG, "Exclude " + exclude.getStart().getHostAddress() + "..."
                        + exclude.getEnd().getHostAddress());
                for (IPUtil.CIDR include : IPUtil.toCIDR(start, IPUtil.minus1(exclude.getStart())))
                    try {
                        builder.addRoute(include.address, include.prefix);
                    } catch (Throwable ex) {
                        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
                    }
                start = IPUtil.plus1(exclude.getEnd());
            }
            for (IPUtil.CIDR include : IPUtil.toCIDR("224.0.0.0", "255.255.255.255"))
                try {
                    builder.addRoute(include.address, include.prefix);
                } catch (Throwable ex) {
                    Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
                }
        } catch (UnknownHostException ex) {
            Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
        }
    } else
        builder.addRoute("0.0.0.0", 0);

    Log.i(TAG, "IPv6=" + ip6);
    if (ip6)
        builder.addRoute("0:0:0:0:0:0:0:0", 0);

    // MTU
    int mtu = jni_get_mtu();
    Log.i(TAG, "MTU=" + mtu);
    builder.setMtu(mtu);

    // Add list of allowed applications
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        if (last_connected && !filter)
            for (Rule rule : listAllowed)
                try {
                    builder.addDisallowedApplication(rule.info.packageName);
                } catch (PackageManager.NameNotFoundException ex) {
                    Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
                }
        else if (filter)
            for (Rule rule : listRule)
                if (!rule.apply || (!system && rule.system))
                    try {
                        //                            Log.i(TAG, "Not routing " + rule.info.packageName);
                        builder.addDisallowedApplication(rule.info.packageName);
                    } catch (PackageManager.NameNotFoundException ex) {
                        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
                    }

    // Build configure intent
    Intent configure = new Intent(this, ActivityMain.class);
    PendingIntent pi = PendingIntent.getActivity(this, 0, configure, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setConfigureIntent(pi);

    return builder;
}

From source file:org.apache.geode.internal.net.SocketCreator.java

/**
 * returns a set of the non-loopback InetAddresses for this machine
 *//*from  w  w w.  jav a  2 s . c  o  m*/
public static Set<InetAddress> getMyAddresses() {
    Set<InetAddress> result = new HashSet<InetAddress>();
    Set<InetAddress> locals = new HashSet<InetAddress>();
    Enumeration<NetworkInterface> interfaces;
    try {
        interfaces = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        throw new IllegalArgumentException(
                LocalizedStrings.StartupMessage_UNABLE_TO_EXAMINE_NETWORK_INTERFACES.toLocalizedString(), e);
    }
    while (interfaces.hasMoreElements()) {
        NetworkInterface face = interfaces.nextElement();
        boolean faceIsUp = false;
        try {
            faceIsUp = face.isUp();
        } catch (SocketException e) {
            InternalDistributedSystem ids = InternalDistributedSystem.getAnyInstance();
            if (ids != null) {
                logger.info("Failed to check if network interface is up. Skipping {}", face, e);
            }
        }
        if (faceIsUp) {
            Enumeration<InetAddress> addrs = face.getInetAddresses();
            while (addrs.hasMoreElements()) {
                InetAddress addr = addrs.nextElement();
                if (addr.isLoopbackAddress() || addr.isAnyLocalAddress()
                        || (!useLinkLocalAddresses && addr.isLinkLocalAddress())) {
                    locals.add(addr);
                } else {
                    result.add(addr);
                }
            } // while
        }
    } // while
      // fix for bug #42427 - allow product to run on a standalone box by using
      // local addresses if there are no non-local addresses available
    if (result.size() == 0) {
        return locals;
    } else {
        return result;
    }
}

From source file:com.adito.server.DefaultAditoServerFactory.java

private void displaySystemInfo() throws SocketException {

    if (useDevConfig) {
        LOG.warn("Development environment enabled. Do not use this on a production server.");
    }//from   w ww .  ja va  2  s  .c  om

    if (LOG.isInfoEnabled()) {
        LOG.info("Version is " + ContextHolder.getContext().getVersion());
        LOG.info("Java version is " + SystemProperties.get("java.version"));
        LOG.info("Server is installed on " + hostname + "/" + hostAddress);
        LOG.info("Configuration: " + CONF_DIR.getAbsolutePath());
    }

    if (SystemProperties.get("java.vm.name", "").indexOf("GNU") > -1
            || SystemProperties.get("java.vm.name", "").indexOf("libgcj") > -1) {
        System.out.println("********** WARNING **********");
        System.out.println("The system has detected that the Java runtime is GNU/GCJ");
        System.out.println("Adito does not work correctly with this Java runtime");
        System.out.println("you should reconfigure with a different runtime");
        System.out.println("*****************************");

        LOG.warn("********** WARNING **********");
        LOG.warn("The system has detected that the Java runtime is GNU/GCJ");
        LOG.warn("Adito may not work correctly with this Java runtime");
        LOG.warn("you should reconfigure with a different runtime");
        LOG.warn("*****************************");

    }

    Enumeration e = NetworkInterface.getNetworkInterfaces();

    while (e.hasMoreElements()) {
        NetworkInterface netface = (NetworkInterface) e.nextElement();
        if (LOG.isInfoEnabled()) {
            LOG.info("Net interface: " + netface.getName());
        }

        Enumeration e2 = netface.getInetAddresses();

        while (e2.hasMoreElements()) {
            InetAddress ip = (InetAddress) e2.nextElement();
            if (LOG.isInfoEnabled()) {
                LOG.info("IP address: " + ip.toString());
            }
        }
    }

    if (LOG.isInfoEnabled()) {
        LOG.info("System properties follow:");
    }
    Properties sysProps = System.getProperties();
    for (Map.Entry entry : sysProps.entrySet()) {
        int idx = 0;
        String val = (String) entry.getValue();
        while (true) {
            if (entry.getKey().equals("java.class.path")) {
                StringTokenizer t = new StringTokenizer(entry.getValue().toString(),
                        SystemProperties.get("path.separator", ","));
                while (t.hasMoreTokens()) {
                    String s = t.nextToken();
                    if (LOG.isInfoEnabled()) {
                        LOG.info("java.class.path=" + s);
                    }
                }
                break;
            } else {
                if ((val.length() - idx) > 256) {
                    if (LOG.isInfoEnabled()) {
                        LOG.info("  " + entry.getKey() + "=" + val.substring(idx, idx + 256));
                    }
                    idx += 256;
                } else {
                    if (LOG.isInfoEnabled()) {
                        LOG.info("  " + entry.getKey() + "=" + val.substring(idx));
                    }
                    break;
                }
            }
        }
    }
}

From source file:com.adito.server.Main.java

private void displaySystemInfo() throws SocketException {

    ///* w  w  w . j a  va 2  s.com*/

    if (useDevConfig) {
        log.warn("Development environment enabled. Do not use this on a production server.");
    }

    if (log.isInfoEnabled()) {
        log.info("Version is " + ContextHolder.getContext().getVersion());
        log.info("Java version is " + SystemProperties.get("java.version"));
        log.info("Server is installed on " + hostname + "/" + hostAddress);
        log.info("Configuration: " + CONF_DIR.getAbsolutePath());
    }

    if (SystemProperties.get("java.vm.name", "").indexOf("GNU") > -1
            || SystemProperties.get("java.vm.name", "").indexOf("libgcj") > -1) {
        System.out.println("********** WARNING **********");
        System.out.println("The system has detected that the Java runtime is GNU/GCJ");
        System.out.println("Adito does not work correctly with this Java runtime");
        System.out.println("you should reconfigure with a different runtime");
        System.out.println("*****************************");

        log.warn("********** WARNING **********");
        log.warn("The system has detected that the Java runtime is GNU/GCJ");
        log.warn("Adito may not work correctly with this Java runtime");
        log.warn("you should reconfigure with a different runtime");
        log.warn("*****************************");

    }

    Enumeration e = NetworkInterface.getNetworkInterfaces();

    while (e.hasMoreElements()) {
        NetworkInterface netface = (NetworkInterface) e.nextElement();
        if (log.isInfoEnabled())
            log.info("Net interface: " + netface.getName());

        Enumeration e2 = netface.getInetAddresses();

        while (e2.hasMoreElements()) {
            InetAddress ip = (InetAddress) e2.nextElement();
            if (log.isInfoEnabled())
                log.info("IP address: " + ip.toString());
        }
    }

    if (log.isInfoEnabled())
        log.info("System properties follow:");
    Properties sysProps = System.getProperties();
    for (Iterator i = sysProps.entrySet().iterator(); i.hasNext();) {
        Map.Entry entry = (Map.Entry) i.next();
        int idx = 0;
        String val = (String) entry.getValue();
        while (true) {
            if (entry.getKey().equals("java.class.path")) {
                StringTokenizer t = new StringTokenizer(entry.getValue().toString(),
                        SystemProperties.get("path.separator", ","));
                while (t.hasMoreTokens()) {
                    String s = t.nextToken();
                    if (log.isInfoEnabled())
                        log.info("java.class.path=" + s);
                }
                break;
            } else {
                if ((val.length() - idx) > 256) {
                    if (log.isInfoEnabled())
                        log.info("  " + entry.getKey() + "=" + val.substring(idx, idx + 256));
                    idx += 256;
                } else {
                    if (log.isInfoEnabled())
                        log.info("  " + entry.getKey() + "=" + val.substring(idx));
                    break;
                }
            }
        }
    }
}

From source file:android_network.hetnet.vpn_service.ServiceSinkhole.java

private Builder getBuilder(List<Rule> listAllowed, List<Rule> listRule) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    boolean subnet = prefs.getBoolean("subnet", false);
    boolean tethering = prefs.getBoolean("tethering", false);
    boolean lan = prefs.getBoolean("lan", false);
    boolean ip6 = prefs.getBoolean("ip6", true);
    boolean filter = prefs.getBoolean("filter", false);
    boolean system = prefs.getBoolean("manage_system", false);

    // Build VPN service
    Builder builder = new Builder();
    builder.setSession(getString(R.string.app_name));

    // VPN address
    String vpn4 = prefs.getString("vpn4", "10.1.10.1");
    Log.i(TAG, "vpn4=" + vpn4);
    builder.addAddress(vpn4, 32);//  w w  w.ja v a2 s . c  o  m
    if (ip6) {
        String vpn6 = prefs.getString("vpn6", "fd00:1:fd00:1:fd00:1:fd00:1");
        Log.i(TAG, "vpn6=" + vpn6);
        builder.addAddress(vpn6, 128);
    }

    // DNS address
    if (filter)
        for (InetAddress dns : getDns(ServiceSinkhole.this)) {
            if (ip6 || dns instanceof Inet4Address) {
                Log.i(TAG, "dns=" + dns);
                builder.addDnsServer(dns);
            }
        }

    // Subnet routing
    if (subnet) {
        // Exclude IP ranges
        List<IPUtil.CIDR> listExclude = new ArrayList<>();
        listExclude.add(new IPUtil.CIDR("127.0.0.0", 8)); // localhost

        if (tethering) {
            // USB Tethering 192.168.42.x
            // Wi-Fi Tethering 192.168.43.x
            listExclude.add(new IPUtil.CIDR("192.168.42.0", 23));
        }

        if (lan) {
            try {
                Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
                while (nis.hasMoreElements()) {
                    NetworkInterface ni = nis.nextElement();
                    if (ni != null && ni.isUp() && !ni.isLoopback() && ni.getName() != null
                            && !ni.getName().startsWith("tun"))
                        for (InterfaceAddress ia : ni.getInterfaceAddresses())
                            if (ia.getAddress() instanceof Inet4Address) {
                                IPUtil.CIDR local = new IPUtil.CIDR(ia.getAddress(),
                                        ia.getNetworkPrefixLength());
                                Log.i(TAG, "Excluding " + ni.getName() + " " + local);
                                listExclude.add(local);
                            }
                }
            } catch (SocketException ex) {
                Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
            }
        }

        Configuration config = getResources().getConfiguration();
        if (config.mcc == 310 && config.mnc == 260) {
            // T-Mobile Wi-Fi calling
            listExclude.add(new IPUtil.CIDR("66.94.2.0", 24));
            listExclude.add(new IPUtil.CIDR("66.94.6.0", 23));
            listExclude.add(new IPUtil.CIDR("66.94.8.0", 22));
            listExclude.add(new IPUtil.CIDR("208.54.0.0", 16));
        }
        listExclude.add(new IPUtil.CIDR("224.0.0.0", 3)); // broadcast

        Collections.sort(listExclude);

        try {
            InetAddress start = InetAddress.getByName("0.0.0.0");
            for (IPUtil.CIDR exclude : listExclude) {
                Log.i(TAG, "Exclude " + exclude.getStart().getHostAddress() + "..."
                        + exclude.getEnd().getHostAddress());
                for (IPUtil.CIDR include : IPUtil.toCIDR(start, IPUtil.minus1(exclude.getStart())))
                    try {
                        builder.addRoute(include.address, include.prefix);
                    } catch (Throwable ex) {
                        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
                    }
                start = IPUtil.plus1(exclude.getEnd());
            }
            for (IPUtil.CIDR include : IPUtil.toCIDR("224.0.0.0", "255.255.255.255"))
                try {
                    builder.addRoute(include.address, include.prefix);
                } catch (Throwable ex) {
                    Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
                }
        } catch (UnknownHostException ex) {
            Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
        }
    } else
        builder.addRoute("0.0.0.0", 0);

    Log.i(TAG, "IPv6=" + ip6);
    if (ip6)
        builder.addRoute("0:0:0:0:0:0:0:0", 0);

    // MTU
    int mtu = jni_get_mtu();
    Log.i(TAG, "MTU=" + mtu);
    builder.setMtu(mtu);

    //         Add list of allowed applications
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        if (last_connected && !filter)
            for (Rule rule : listAllowed)
                try {
                    builder.addDisallowedApplication(rule.info.packageName);
                } catch (PackageManager.NameNotFoundException ex) {
                    Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
                }
        else if (filter)
            for (Rule rule : listRule)
                if (!rule.apply || (!system && rule.system))
                    try {
                        Log.i(TAG, "Not routing " + rule.info.packageName);
                        builder.addDisallowedApplication(rule.info.packageName);
                    } catch (PackageManager.NameNotFoundException ex) {
                        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
                    }

    // Build configure intent
    Intent configure = new Intent(this, MainActivity.class);
    PendingIntent pi = PendingIntent.getActivity(this, 0, configure, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setConfigureIntent(pi);

    return builder;
}

From source file:org.apache.flink.runtime.taskmanager.TaskManager.java

/**
 * Determines the IP address of the interface from which the TaskManager can connect to the given JobManager
 * IP address./*from  w  w w  . j a v  a2s.c o  m*/
 * 
 * @param jobManagerAddress The socket address to connect to.
 * @return The IP address of the interface that connects to the JobManager.
 * @throws IOException If no connection could be established.
 */
private static InetAddress getTaskManagerAddress(InetSocketAddress jobManagerAddress) throws IOException {
    AddressDetectionState strategy = AddressDetectionState.ADDRESS;

    while (true) {
        Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
        while (e.hasMoreElements()) {
            NetworkInterface n = e.nextElement();
            Enumeration<InetAddress> ee = n.getInetAddresses();
            while (ee.hasMoreElements()) {
                InetAddress i = ee.nextElement();
                switch (strategy) {
                case ADDRESS:
                    if (hasCommonPrefix(jobManagerAddress.getAddress().getAddress(), i.getAddress())) {
                        if (tryToConnect(i, jobManagerAddress, strategy.getTimeout())) {
                            LOG.info("Determined " + i + " as the TaskTracker's own IP address");
                            return i;
                        }
                    }
                    break;
                case FAST_CONNECT:
                case SLOW_CONNECT:
                    boolean correct = tryToConnect(i, jobManagerAddress, strategy.getTimeout());
                    if (correct) {
                        LOG.info("Determined " + i + " as the TaskTracker's own IP address");
                        return i;
                    }
                    break;
                default:
                    throw new RuntimeException("Unkown address detection strategy: " + strategy);
                }
            }
        }
        // state control
        switch (strategy) {
        case ADDRESS:
            strategy = AddressDetectionState.FAST_CONNECT;
            break;
        case FAST_CONNECT:
            strategy = AddressDetectionState.SLOW_CONNECT;
            break;
        case SLOW_CONNECT:
            throw new RuntimeException("The TaskManager is unable to connect to the JobManager (Address: '"
                    + jobManagerAddress + "').");
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Defaulting to detection strategy {}", strategy);
        }
    }
}

From source file:com.sslexplorer.server.Main.java

private void displaySystemInfo() throws SocketException {

    ///*  w  w  w .  j a  v a2  s .co m*/

    if (useDevConfig) {
        log.warn("Development environment enabled. Do not use this on a production server.");
    }

    if (log.isInfoEnabled()) {
        log.info("Version is " + ContextHolder.getContext().getVersion());
        log.info("Java version is " + SystemProperties.get("java.version"));
        log.info("Server is installed on " + hostname + "/" + hostAddress);
        log.info("Configuration: " + CONF_DIR.getAbsolutePath());
    }

    if (SystemProperties.get("java.vm.name", "").indexOf("GNU") > -1
            || SystemProperties.get("java.vm.name", "").indexOf("libgcj") > -1) {
        System.out.println("********** WARNING **********");
        System.out.println("The system has detected that the Java runtime is GNU/GCJ");
        System.out.println("SSL-Explorer does not work correctly with this Java runtime");
        System.out.println("you should reconfigure with a different runtime");
        System.out.println("*****************************");

        log.warn("********** WARNING **********");
        log.warn("The system has detected that the Java runtime is GNU/GCJ");
        log.warn("SSL-Explorer may not work correctly with this Java runtime");
        log.warn("you should reconfigure with a different runtime");
        log.warn("*****************************");

    }

    Enumeration e = NetworkInterface.getNetworkInterfaces();

    while (e.hasMoreElements()) {
        NetworkInterface netface = (NetworkInterface) e.nextElement();
        if (log.isInfoEnabled())
            log.info("Net interface: " + netface.getName());

        Enumeration e2 = netface.getInetAddresses();

        while (e2.hasMoreElements()) {
            InetAddress ip = (InetAddress) e2.nextElement();
            if (log.isInfoEnabled())
                log.info("IP address: " + ip.toString());
        }
    }

    if (log.isInfoEnabled())
        log.info("System properties follow:");
    Properties sysProps = System.getProperties();
    for (Iterator i = sysProps.entrySet().iterator(); i.hasNext();) {
        Map.Entry entry = (Map.Entry) i.next();
        int idx = 0;
        String val = (String) entry.getValue();
        while (true) {
            if (entry.getKey().equals("java.class.path")) {
                StringTokenizer t = new StringTokenizer(entry.getValue().toString(),
                        SystemProperties.get("path.separator", ","));
                while (t.hasMoreTokens()) {
                    String s = t.nextToken();
                    if (log.isInfoEnabled())
                        log.info("java.class.path=" + s);
                }
                break;
            } else {
                if ((val.length() - idx) > 256) {
                    if (log.isInfoEnabled())
                        log.info("  " + entry.getKey() + "=" + val.substring(idx, idx + 256));
                    idx += 256;
                } else {
                    if (log.isInfoEnabled())
                        log.info("  " + entry.getKey() + "=" + val.substring(idx));
                    break;
                }
            }
        }
    }
}