List of usage examples for java.net InetAddress isAnyLocalAddress
public boolean isAnyLocalAddress()
From source file:eu.faircode.adblocker.ServiceSinkhole.java
public static List<InetAddress> getDns(Context context) { List<InetAddress> listDns = new ArrayList<>(); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); List<String> sysDns = Util.getDefaultDNS(context); String vpnDns = prefs.getString("dns", null); Log.i(TAG, "DNS system=" + TextUtils.join(",", sysDns) + " VPN=" + vpnDns); if (vpnDns != null) try {//from w w w. j a v a 2 s . c om InetAddress dns = InetAddress.getByName(vpnDns); if (!(dns.isLoopbackAddress() || dns.isAnyLocalAddress())) listDns.add(dns); } catch (Throwable ignored) { } for (String def_dns : sysDns) try { InetAddress ddns = InetAddress.getByName(def_dns); if (!listDns.contains(ddns) && !(ddns.isLoopbackAddress() || ddns.isAnyLocalAddress())) listDns.add(ddns); } catch (Throwable ignored) { } return listDns; }
From source file:com.vuze.plugin.azVPN_Air.Checker.java
private int findBindingAddress(StringBuilder sReply) { int newStatusID = -1; // Find our VPN binding (interface) address. Checking UDP is the best bet, // since TCP and http might be proxied List<PRUDPPacketHandler> handlers = PRUDPPacketHandlerFactory.getHandlers(); if (handlers.size() == 0) { PRUDPReleasablePacketHandler releasableHandler = PRUDPPacketHandlerFactory.getReleasableHandler(0); handlers = PRUDPPacketHandlerFactory.getHandlers(); releasableHandler.release();/* w w w. ja v a 2 s . com*/ } if (handlers.size() == 0) { addLiteralReply(sReply, CHAR_BAD + " No UDP Handlers"); newStatusID = STATUS_ID_BAD; } else { InetAddress bindIP = handlers.get(0).getBindIP(); // The "Any" field is equivalent to 0.0.0.0 in dotted-quad notation, which is unbound. // "Loopback" is 127.0.0.1, which is bound when Vuze can't bind to // user specified interface (ie. kill switched) if (bindIP.isAnyLocalAddress() || bindIP.isLoopbackAddress()) { newStatusID = handleUnboundOrLoopback(bindIP, sReply); if (newStatusID == STATUS_ID_BAD) { return newStatusID; } } else { newStatusID = handleBound(bindIP, sReply); } } return newStatusID; }
From source file:com.vuze.plugin.azVPN_Air.Checker.java
private int handleUnboundOrLoopback(InetAddress bindIP, StringBuilder sReply) { int newStatusID = STATUS_ID_OK; InetAddress newBindIP = null; NetworkInterface newBindNetworkInterface = null; String s;//w ww. j a va 2s.c om if (bindIP.isAnyLocalAddress()) { addReply(sReply, CHAR_WARN, "airvpn.vuze.unbound"); } else { addReply(sReply, CHAR_BAD, "airvpn.vuze.loopback"); } try { NetworkAdmin networkAdmin = NetworkAdmin.getSingleton(); // Find a bindable address that starts with 10. InetAddress[] bindableAddresses = networkAdmin.getBindableAddresses(); for (InetAddress bindableAddress : bindableAddresses) { if (matchesVPNIP(bindableAddress)) { newBindIP = bindableAddress; newBindNetworkInterface = NetworkInterface.getByInetAddress(newBindIP); addReply(sReply, CHAR_GOOD, "airvpn.found.bindable.vpn", new String[] { "" + newBindIP }); break; } } // Find a Network Interface that has an address that starts with 10. NetworkAdminNetworkInterface[] interfaces = networkAdmin.getInterfaces(); boolean foundNIF = false; for (NetworkAdminNetworkInterface networkAdminInterface : interfaces) { NetworkAdminNetworkInterfaceAddress[] addresses = networkAdminInterface.getAddresses(); for (NetworkAdminNetworkInterfaceAddress a : addresses) { InetAddress address = a.getAddress(); if (address instanceof Inet4Address) { if (matchesVPNIP(address)) { s = texts.getLocalisedMessageText("airvpn.possible.vpn", new String[] { "" + address, networkAdminInterface.getName() + " (" + networkAdminInterface.getDisplayName() + ")" }); if (newBindIP == null) { foundNIF = true; newBindIP = address; // Either one should work //newBindNetworkInterface = NetworkInterface.getByInetAddress(newBindIP); newBindNetworkInterface = NetworkInterface .getByName(networkAdminInterface.getName()); s = CHAR_GOOD + " " + s + ". " + texts.getLocalisedMessageText("airvpn.assuming.vpn"); } else if (address.equals(newBindIP)) { s = CHAR_GOOD + " " + s + ". " + texts.getLocalisedMessageText("airvpn.same.address"); foundNIF = true; } else { if (newStatusID != STATUS_ID_BAD) { newStatusID = STATUS_ID_WARN; } s = CHAR_WARN + " " + s + ". " + texts.getLocalisedMessageText("airvpn.not.same.address"); } addLiteralReply(sReply, s); if (rebindNetworkInterface) { // stops message below from being added, we'll rebind later foundNIF = true; } } } } } if (!foundNIF) { addReply(sReply, CHAR_BAD, "airvpn.interface.not.found"); } // Check if default routing goes through 10.*, by connecting to address // via socket. Address doesn't need to be reachable, just routable. // This works on Windows, but on Mac returns a wildcard address DatagramSocket socket = new DatagramSocket(); socket.connect(testSocketAddress, 0); InetAddress localAddress = socket.getLocalAddress(); socket.close(); if (!localAddress.isAnyLocalAddress()) { NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localAddress); s = texts.getLocalisedMessageText("airvpn.nonvuze.probable.route", new String[] { "" + localAddress, networkInterface == null ? "null" : networkInterface.getName() + " (" + networkInterface.getDisplayName() + ")" }); if ((localAddress instanceof Inet4Address) && matchesVPNIP(localAddress)) { if (newBindIP == null) { newBindIP = localAddress; newBindNetworkInterface = networkInterface; s = CHAR_GOOD + " " + s + " " + texts.getLocalisedMessageText("airvpn.assuming.vpn"); } else if (localAddress.equals(newBindIP)) { s = CHAR_GOOD + " " + s + " " + texts.getLocalisedMessageText("airvpn.same.address"); } else { // Vuze not bound. We already found a boundable address, but it's not this one /* Possibly good case: * - Vuze: unbound * - Found Bindable: 10.100.1.6 * - Default Routing: 10.255.1.1 * -> Split network */ if (newStatusID != STATUS_ID_BAD) { newStatusID = STATUS_ID_WARN; } s = CHAR_WARN + " " + s + " " + texts.getLocalisedMessageText("airvpn.not.same.future.address") + " " + texts.getLocalisedMessageText("default.routing.not.vpn.network.splitting") + " " + texts.getLocalisedMessageText( "default.routing.not.vpn.network.splitting.unbound"); } addLiteralReply(sReply, s); } else { s = CHAR_WARN + " " + s; if (!bindIP.isLoopbackAddress()) { s += " " + texts.getLocalisedMessageText("default.routing.not.vpn.network.splitting"); } if (newBindIP == null && foundNIF) { if (newStatusID != STATUS_ID_BAD) { newStatusID = STATUS_ID_WARN; } s += " " + texts .getLocalisedMessageText("default.routing.not.vpn.network.splitting.unbound"); } addLiteralReply(sReply, s); } } } catch (Exception e) { e.printStackTrace(); addReply(sReply, CHAR_BAD, "airvpn.nat.error", new String[] { e.toString() }); } if (newBindIP == null) { addReply(sReply, CHAR_BAD, "airvpn.vpn.ip.detect.fail"); return STATUS_ID_BAD; } rebindNetworkInterface(newBindNetworkInterface, newBindIP, sReply); return newStatusID; }
From source file:org.blue.star.plugins.check_ping.java
public boolean execute_check() { for (String hostname : addresses) { try {/*from w w w . j ava 2 s .co m*/ InetAddress inet = InetAddress.getByName(hostname); long execute_time = 0; long packet_loss = 0; for (int pings = 0; pings < max_packets; pings++) { boolean reachable = false; long ping_time = System.currentTimeMillis(); if (network_interface != null) { reachable = inet.isReachable(network_interface, 0, utils_h.timeout_interval); } else { reachable = inet.isReachable(utils_h.timeout_interval); } execute_time += (System.currentTimeMillis() - ping_time); if (!reachable) packet_loss++; } rta = execute_time / max_packets; pl = (int) packet_loss / max_packets * 100; if (verbose > 0) { System.out.println("rta = " + rta); System.out.println("pl = " + pl); } if (verbose > 1) { System.out.println("isAnyLocalAddress = " + inet.isAnyLocalAddress()); System.out.println("isLinkLocalAddress = " + inet.isLinkLocalAddress()); System.out.println("isLoopbackAddress = " + inet.isLoopbackAddress()); System.out.println("isMCGlobal = " + inet.isMCGlobal()); System.out.println("isMCLinkLocal = " + inet.isMCLinkLocal()); System.out.println("isMCNodeLocal = " + inet.isMCNodeLocal()); System.out.println("isMCOrgLocal = " + inet.isMCOrgLocal()); System.out.println("isMCSiteLocal = " + inet.isMCSiteLocal()); System.out.println("isMulticastAddress = " + inet.isMulticastAddress()); System.out.println("isSiteLocalAddress = " + inet.isSiteLocalAddress()); System.out.println("isReachable = " + inet.isReachable(utils_h.timeout_interval)); System.out.println("getCanonicalHostName = " + inet.getCanonicalHostName()); System.out.println("getHostAddress = " + inet.getHostAddress()); System.out.println("getHostName = " + inet.getHostName()); System.out.println("getClass.getName = " + inet.getClass().getName()); } /* The list is only used to check alternatives, if we pass don't do more */ if (packet_loss != max_packets) break; } catch (Exception e) { warn_text = e.getMessage(); e.printStackTrace(); } } if (pl >= cpl || rta >= crta || rta < 0) check_state = common_h.STATE_CRITICAL; else if (pl >= wpl || rta >= wrta) check_state = common_h.STATE_WARNING; else if (pl >= 0 && rta >= 0) check_state = common_h.STATE_OK; return true; }
From source file:android_network.hetnet.vpn_service.ActivitySettings.java
private void checkAddress(String address) throws IllegalArgumentException, UnknownHostException { if (address == null || TextUtils.isEmpty(address.trim())) throw new IllegalArgumentException("Bad address"); if (!Util.isNumericAddress(address)) throw new IllegalArgumentException("Bad address"); InetAddress idns = InetAddress.getByName(address); if (idns.isLoopbackAddress() || idns.isAnyLocalAddress()) throw new IllegalArgumentException("Bad address"); }
From source file:android_network.hetnet.vpn_service.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); 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 {/*from www . j a v a 2 s . c o m*/ InetAddress dns = InetAddress.getByName(vpnDns1); if (!(dns.isLoopbackAddress() || dns.isAnyLocalAddress())) listDns.add(dns); } catch (Throwable ignored) { } if (vpnDns2 != null) try { InetAddress dns = InetAddress.getByName(vpnDns2); if (!(dns.isLoopbackAddress() || dns.isAnyLocalAddress())) 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())) 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); if (lan) { 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")); 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); } } } } // Prefer IPv4 addresses Collections.sort(listDns, new Comparator<InetAddress>() { @Override public int compare(InetAddress a, InetAddress b) { boolean a4 = (a instanceof Inet4Address); boolean b4 = (b instanceof Inet4Address); if (a4 && !b4) return -1; else if (!a4 && b4) return 1; else return 0; } }); return listDns; }
From source file:com.vuze.plugin.azVPN_Helper.CheckerCommon.java
private final int handleFindBindingAddress(InetAddress currentBindIP, StringBuilder sReply, int numLoops) { if (currentBindIP == null) { addReply(sReply, CHAR_BAD, "!Bind IP null!", new String[] { "" + currentBindIP }); return STATUS_ID_BAD; }// ww w . j a v a2 s .co m int newStatusID = STATUS_ID_OK; Map<String, BindableInterface> mapBindableInterfaces = new HashMap<String, BindableInterface>(); BindableInterface newBind = null; String s; // The "Any" field is equivalent to 0.0.0.0 in dotted-quad notation, which is unbound. // "Loopback" is 127.0.0.1, which is bound when Vuze can't bind to // user specified interface (ie. kill switched) if (currentBindIP.isAnyLocalAddress()) { addReply(sReply, CHAR_WARN, "vpnhelper.vuze.unbound"); } else if (currentBindIP.isLoopbackAddress()) { addReply(sReply, CHAR_BAD, "vpnhelper.vuze.loopback"); } else { // bound boolean isGoodExistingBind = matchesVPNIP(currentBindIP, null); if (isGoodExistingBind) { String niName = "Unknown Interface"; try { NetworkInterface networkInterface = NetUtils.getByInetAddress(currentBindIP); niName = networkInterface.getName() + " (" + networkInterface.getDisplayName() + ")"; } catch (Throwable e) { } addReply(sReply, CHAR_GOOD, "vpnhelper.bound.good", new String[] { "" + currentBindIP, niName }); vpnIP = currentBindIP; } else { addReply(sReply, CHAR_BAD, "vpnhelper.bound.bad", new String[] { "" + currentBindIP }); } } try { boolean foundExistingVPNIP = false; NetworkAdmin networkAdmin = NetworkAdmin.getSingleton(); // Find a bindable address that starts with 10. InetAddress[] bindableAddresses = networkAdmin.getBindableAddresses(); for (InetAddress bindableAddress : bindableAddresses) { if (matchesVPNIP(bindableAddress, null)) { String hostAddress = bindableAddress.getHostAddress(); BindableInterface bi = mapBindableInterfaces.get(hostAddress); if (bi == null) { bi = new BindableInterface(bindableAddress, NetUtils.getByInetAddress(bindableAddress)); mapBindableInterfaces.put(hostAddress, bi); if (!foundExistingVPNIP && bindableAddress.equals(vpnIP)) { foundExistingVPNIP = true; } } } } // Find a Network Interface that has an address that starts with 10. NetworkAdminNetworkInterface[] interfaces = networkAdmin.getInterfaces(); /* Test reverse * for (int i = 0; i < interfaces.length / 2; i++) { NetworkAdminNetworkInterface temp = interfaces[i]; interfaces[i] = interfaces[interfaces.length - i - 1]; interfaces[interfaces.length - i - 1] = temp; } /**/ for (NetworkAdminNetworkInterface networkAdminInterface : interfaces) { NetworkAdminNetworkInterfaceAddress[] addresses = networkAdminInterface.getAddresses(); for (NetworkAdminNetworkInterfaceAddress a : addresses) { InetAddress address = a.getAddress(); if (address instanceof Inet4Address) { if (matchesVPNIP(address, null)) { String hostAddress = address.getHostAddress(); BindableInterface bi = mapBindableInterfaces.get(hostAddress); if (bi == null) { bi = new BindableInterface(address, NetUtils.getByName(networkAdminInterface.getName())); mapBindableInterfaces.put(hostAddress, bi); if (!foundExistingVPNIP && address.equals(vpnIP)) { foundExistingVPNIP = true; } } } } } } if (vpnIP != null && !foundExistingVPNIP) { String niName = "Unknown Interface"; try { NetworkInterface networkInterface = NetUtils.getByInetAddress(currentBindIP); niName = networkInterface.getName() + " (" + networkInterface.getDisplayName() + ")"; } catch (Throwable e) { } addReply(sReply, CHAR_WARN, "vpnhelper.existing.not.found", new String[] { "" + currentBindIP, niName }); if (numLoops == 0) { try { Field fldLastNICheck = NetUtils.class.getDeclaredField("last_ni_check"); fldLastNICheck.setAccessible(true); fldLastNICheck.set(null, Long.valueOf(-1)); return handleFindBindingAddress(currentBindIP, sReply, ++numLoops); } catch (Throwable t) { t.printStackTrace(); } } } BindableInterface[] array = mapBindableInterfaces.values().toArray(new BindableInterface[0]); Arrays.sort(array); for (BindableInterface bi : array) { if (!bi.isValidPrefixLength(minSubnetMaskBitCount)) { addReply(sReply, CHAR_WARN, "vpnhelper.submask.too.broad", new String[] { "" + bi.address, bi.networkInterface == null ? "null" : bi.networkInterface.getName() + " (" + bi.networkInterface.getDisplayName() + ")", "" + bi.networkPrefixLength, "" + minSubnetMaskBitCount }); } else if (bi.canReach) { addReply(sReply, CHAR_GOOD, "vpnhelper.found.bindable.vpn", new String[] { "" + bi.address, bi.networkInterface == null ? "null" : bi.networkInterface.getName() + " (" + bi.networkInterface.getDisplayName() + ")" }); } else { addReply(sReply, CHAR_WARN, "vpnhelper.not.reachable", new String[] { "" + bi.address, bi.networkInterface == null ? "null" : bi.networkInterface.getName() + " (" + bi.networkInterface.getDisplayName() + ")" }); } PluginVPNHelper.log("subnet: " + bi.networkPrefixLength + "; Score: " + bi.score); } newBind = array.length > 0 && array[0].canReach && array[0].isValidPrefixLength(minSubnetMaskBitCount) ? array[0] : null; InetAddress localAddress = null; // Check if default routing goes through 10.*, by connecting to address // via socket. Address doesn't need to be reachable, just routable. // This works on Windows, but on Mac returns a wildcard address DatagramSocket socket = new DatagramSocket(); try { socket.connect(testSocketAddress, 0); localAddress = socket.getLocalAddress(); } finally { socket.close(); } if (localAddress != null && !localAddress.isAnyLocalAddress()) { NetworkInterface networkInterface = NetUtils.getByInetAddress(localAddress); s = texts.getLocalisedMessageText("vpnhelper.nonvuze.probable.route", new String[] { "" + localAddress, networkInterface == null ? "null" : networkInterface.getName() + " (" + networkInterface.getDisplayName() + ")" }); if ((localAddress instanceof Inet4Address) && matchesVPNIP(localAddress, networkInterface)) { if (newBind == null) { int networkPrefixLength = getNetworkPrefixLength(networkInterface, localAddress); if (networkPrefixLength >= 0 && networkPrefixLength < minSubnetMaskBitCount) { s = null; addReply(sReply, CHAR_WARN, "vpnhelper.nonvuze.submask.too.broad", new String[] { "" + localAddress, networkInterface == null ? "null" : networkInterface.getName() + " (" + networkInterface.getDisplayName() + ")", "" + networkPrefixLength, "" + minSubnetMaskBitCount }); } else if (!canReach(localAddress)) { addReply(sReply, CHAR_WARN, "vpnhelper.not.reachable", new String[] { "" + localAddress, networkInterface == null ? "null" : networkInterface.getName() + " (" + networkInterface.getDisplayName() + ")" }); } else { newBind = new BindableInterface(localAddress, networkInterface); s = CHAR_GOOD + " " + s + " " + texts.getLocalisedMessageText("vpnhelper.assuming.vpn"); } } else if (localAddress.equals(newBind.address)) { s = CHAR_GOOD + " " + s + " " + texts.getLocalisedMessageText("vpnhelper.same.address"); } else { // Vuze not bound. We already found a boundable address, but it's not this one /* Possibly good case: * - Vuze: unbound * - Found Bindable: 10.100.1.6 * - Default Routing: 10.255.1.1 * -> Split network */ if (newStatusID != STATUS_ID_BAD) { newStatusID = STATUS_ID_WARN; } s = CHAR_WARN + " " + s + " " + texts.getLocalisedMessageText("vpnhelper.not.same.future.address") + " " + texts.getLocalisedMessageText("default.routing.not.vpn.network.splitting") + " " + texts.getLocalisedMessageText( "default.routing.not.vpn.network.splitting.unbound"); } if (s != null) { addLiteralReply(sReply, s); } } else { s = CHAR_WARN + " " + s; if (!currentBindIP.isLoopbackAddress()) { s += " " + texts.getLocalisedMessageText("default.routing.not.vpn.network.splitting"); } if (newBind == null) { if (newStatusID != STATUS_ID_BAD) { newStatusID = STATUS_ID_WARN; } s += " " + texts .getLocalisedMessageText("default.routing.not.vpn.network.splitting.unbound"); } addLiteralReply(sReply, s); } } } catch (Exception e) { e.printStackTrace(); addReply(sReply, CHAR_BAD, "vpnhelper.nat.error", new String[] { e.toString() }); } if (newBind == null) { addReply(sReply, CHAR_BAD, "vpnhelper.vpn.ip.detect.fail"); String configBindIP = config.getCoreStringParameter(PluginConfig.CORE_PARAM_STRING_LOCAL_BIND_IP); if (configBindIP != null && configBindIP.length() > 0) { addReply(sReply, CHAR_WARN, "vpnhelper" + (currentBindIP.isLoopbackAddress() ? ".existing.bind.kept.loopback" : ".existing.bind.kept"), new String[] { configBindIP }); if (currentBindIP.isLoopbackAddress()) { if (numLoops == 0) { try { Field fldLastNICheck = NetUtils.class.getDeclaredField("last_ni_check"); fldLastNICheck.setAccessible(true); fldLastNICheck.set(null, Long.valueOf(-1)); return handleFindBindingAddress(currentBindIP, sReply, ++numLoops); } catch (Throwable t) { t.printStackTrace(); } } } } return STATUS_ID_BAD; } rebindNetworkInterface(newBind.networkInterface, newBind.address, sReply); return newStatusID; }
From source file:kx.c.java
/** * Prepare socket for kdb+ ipc comms//from w ww. j a v a2 s . c o m * @param x socket to setup * @throws IOException an I/O error occurs. */ void io(Socket x) throws IOException { s = x; s.setTcpNoDelay(true); InetAddress addr = s.getInetAddress(); l = addr.isAnyLocalAddress() || addr.isLoopbackAddress(); i = new DataInputStream(s.getInputStream()); o = s.getOutputStream(); s.setKeepAlive(true); }
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 {/*from w w w . j a v a2s. 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:org.nuxeo.launcher.config.ConfigurationGenerator.java
private void evalLoopbackURL() throws ConfigurationException { String loopbackURL = userConfig.getProperty(PARAM_LOOPBACK_URL); if (loopbackURL != null) { log.debug("Using configured loop back url: " + loopbackURL); return;//from w w w. j a va 2s .co m } InetAddress bindAddress = getBindAddress(); // Address and ports already checked by #checkAddressesAndPorts try { if (bindAddress.isAnyLocalAddress()) { boolean preferIPv6 = "false".equals(System.getProperty("java.net.preferIPv4Stack")) && "true".equals(System.getProperty("java.net.preferIPv6Addresses")); bindAddress = preferIPv6 ? InetAddress.getByName("::1") : InetAddress.getByName("127.0.0.1"); log.debug("Bind address is \"ANY\", using local address instead: " + bindAddress); } } catch (UnknownHostException e) { log.debug(e, e); log.error(e.getMessage()); } String httpPort = userConfig.getProperty(PARAM_HTTP_PORT); String contextPath = userConfig.getProperty(PARAM_CONTEXT_PATH); // Is IPv6 or IPv4 ? if (bindAddress instanceof Inet6Address) { loopbackURL = "http://[" + bindAddress.getHostAddress() + "]:" + httpPort + contextPath; } else { loopbackURL = "http://" + bindAddress.getHostAddress() + ":" + httpPort + contextPath; } log.debug("Set as loop back URL: " + loopbackURL); defaultConfig.setProperty(PARAM_LOOPBACK_URL, loopbackURL); }