@TargetApi(23) private static List<InetAddress> getDnsServers(Context context) { List<InetAddress> servers = new ArrayList<>(); ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); //Network[] networks = connectivityManager == null ? null : connectivityManager.getAllNetworks(); Network[] networks = connectivityManager == null ? null : new Network[]{connectivityManager.getActiveNetwork()}; if (networks == null) { return servers; } for(int i = 0; i < networks.length; ++i) { LinkProperties linkProperties = connectivityManager.getLinkProperties(networks[i]); if (linkProperties != null) { servers.addAll(linkProperties.getDnsServers()); } } for(InetAddress server : servers) { Log.d("dns","DNS server: " + Strings.nullToEmpty(server.getHostName()) + " (" + server.getHostAddress() + ")"); } return servers; }
private Network findVpnNetwork() { ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); Network[] networks = cm.getAllNetworks(); for (Network network : networks) { LinkProperties linkProperties = cm.getLinkProperties(network); List<LinkAddress> addresses = linkProperties.getLinkAddresses(); for (LinkAddress addr : addresses) { if (addr.getAddress().equals(VPN_ADDRESS)) { return network; } } } return null; }
@Override protected void after(XParam param) throws Throwable { switch (mMethod) { case getAddresses: case getAllAddresses: if (param.getResult() != null) if (isRestricted(param)) param.setResult(new ArrayList<InetAddress>()); break; case getAllLinkAddresses: case getLinkAddresses: if (param.getResult() != null) if (isRestricted(param)) param.setResult(new ArrayList<LinkAddress>()); break; case getStackedLinks: if (param.getResult() != null) if (isRestricted(param)) param.setResult(new ArrayList<LinkProperties>()); break; } }
@TargetApi(21) private static List<InetAddress> getDnsServers(Context context) { List<InetAddress> servers = new ArrayList<>(); ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); Network[] networks = connectivityManager == null ? null : connectivityManager.getAllNetworks(); if (networks == null) { return getDnsServersPreLollipop(); } for(int i = 0; i < networks.length; ++i) { LinkProperties linkProperties = connectivityManager.getLinkProperties(networks[i]); if (linkProperties != null) { servers.addAll(linkProperties.getDnsServers()); } } if (servers.size() > 0) { Log.d(Config.LOGTAG,"used lollipop variant to discover dns servers in "+networks.length+" networks"); } return servers.size() > 0 ? servers : getDnsServersPreLollipop(); }
@TargetApi(Build.VERSION_CODES.M) @CalledByNative private static byte[][] getDnsServers() { ConnectivityManager connectivityManager = (ConnectivityManager) ContextUtils.getApplicationContext().getSystemService( Context.CONNECTIVITY_SERVICE); if (connectivityManager == null) { return new byte[0][0]; } Network network = connectivityManager.getActiveNetwork(); if (network == null) { return new byte[0][0]; } LinkProperties linkProperties = connectivityManager.getLinkProperties(network); if (linkProperties == null) { return new byte[0][0]; } List<InetAddress> dnsServersList = linkProperties.getDnsServers(); byte[][] dnsServers = new byte[dnsServersList.size()][]; for (int i = 0; i < dnsServersList.size(); i++) { dnsServers[i] = dnsServersList.get(i).getAddress(); } return dnsServers; }
@TargetApi(Build.VERSION_CODES.M) private String getActiveInterface() { final Network network = cm.getActiveNetwork(); if (network == null) { return null; } final LinkProperties linkInfo = cm.getLinkProperties(network); if (linkInfo == null) { return null; } final List<LinkAddress> linkAddress = linkInfo.getLinkAddresses(); if (linkAddress.isEmpty()) { return null; } final InetAddress address = linkAddress.get(0).getAddress(); return address.getHostAddress(); }
@TargetApi(21) private static List<InetAddress> getDnsServers(Context context) { List<InetAddress> servers = new ArrayList<>(); ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); Network[] networks = connectivityManager == null ? null : connectivityManager.getAllNetworks(); if (networks == null) { return getDnsServersPreLollipop(); } for(int i = 0; i < networks.length; ++i) { LinkProperties linkProperties = connectivityManager.getLinkProperties(networks[i]); if (linkProperties != null) { if (hasDefaultRoute(linkProperties)) { servers.addAll(0, linkProperties.getDnsServers()); } else { servers.addAll(linkProperties.getDnsServers()); } } } if (servers.size() > 0) { Log.d(Config.LOGTAG, "used lollipop variant to discover dns servers in " + networks.length + " networks"); } return servers.size() > 0 ? servers : getDnsServersPreLollipop(); }
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private String getIp(LinkProperties lp) { List<LinkAddress> las = lp.getLinkAddresses(); for(LinkAddress la: las) { InetAddress inetAddress = la.getAddress(); if (inetAddress instanceof Inet4Address) { //Log.d(TAG, lp.getInterfaceName() + ": " + inetAddress.getHostAddress()); return inetAddress.getHostAddress(); } } return null; }
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private LinkProperties getLP(ConnectivityManager connMgr, int cap) { Network nets[] = connMgr.getAllNetworks(); for (Network n: nets) { LinkProperties lp = connMgr.getLinkProperties(n); NetworkCapabilities np = connMgr.getNetworkCapabilities(n); String iname = lp.getInterfaceName(); if (iname != null && np != null) { //Log.d(TAG, ">>> " + iname + ": " + np.hasTransport(cap)); if (np.hasTransport(cap)) return lp; } } return null; }
public static List<String> getDefaultDNS(Context context) { String dns1 = null; String dns2 = null; if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); Network an = cm.getActiveNetwork(); if (an != null) { LinkProperties lp = cm.getLinkProperties(an); if (lp != null) { List<InetAddress> dns = lp.getDnsServers(); if (dns != null) { if (dns.size() > 0) dns1 = dns.get(0).getHostAddress(); if (dns.size() > 1) dns2 = dns.get(1).getHostAddress(); for (InetAddress d : dns) Log.i(TAG, "DNS from LP: " + d.getHostAddress()); } } } } else { dns1 = jni_getprop("net.dns1"); dns2 = jni_getprop("net.dns2"); } List<String> listDns = new ArrayList<>(); listDns.add(TextUtils.isEmpty(dns1) ? "8.8.8.8" : dns1); listDns.add(TextUtils.isEmpty(dns2) ? "8.8.4.4" : dns2); return listDns; }
@Override public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) { super.onLinkPropertiesChanged(network, linkProperties); if (!linkProperties.toString().equals(mLastLinkProperties)) { mLastLinkProperties = linkProperties.toString(); VpnStatus.logDebug(String.format("Linkproperties of %s: %s", network, linkProperties)); } }
@Override public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) { // A link property change may indicate the IP address changes. // so forward the new NetworkInformation to the observer. Logging.d(TAG, "link properties changed: " + linkProperties.toString()); onNetworkChanged(network); }
@SuppressLint("NewApi") IPAddress[] getIPAddresses(LinkProperties linkProperties) { IPAddress[] ipAddresses = new IPAddress[linkProperties.getLinkAddresses().size()]; int i = 0; for (LinkAddress linkAddress : linkProperties.getLinkAddresses()) { ipAddresses[i] = new IPAddress(linkAddress.getAddress().getAddress()); ++i; } return ipAddresses; }
@Override @TargetApi(21) public String[] getDnsServerAddresses() { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); Network[] networks = connectivityManager == null ? null : connectivityManager.getAllNetworks(); if (networks == null) { return new String[0]; } List<String> servers = new ArrayList<>(); int vpnOffset = 0; for(Network network : networks) { LinkProperties linkProperties = connectivityManager.getLinkProperties(network); NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network); if (linkProperties != null) { if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_VPN) { final List<String> tmp = getIPv4First(linkProperties.getDnsServers()); servers.addAll(0, tmp); vpnOffset += tmp.size(); } else if (hasDefaultRoute(linkProperties)) { servers.addAll(vpnOffset, getIPv4First(linkProperties.getDnsServers())); } else { servers.addAll(getIPv4First(linkProperties.getDnsServers())); } } } return servers.toArray(new String[servers.size()]); }
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private static boolean hasDefaultRoute(LinkProperties linkProperties) { for(RouteInfo route: linkProperties.getRoutes()) { if (route.isDefaultRoute()) { return true; } } return false; }
@Override @TargetApi(21) public String[] getDnsServerAddresses() { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); Network[] networks = connectivityManager == null ? null : connectivityManager.getAllNetworks(); if (networks == null) { return new String[0]; } final Network activeNetwork = getActiveNetwork(connectivityManager); List<String> servers = new ArrayList<>(); int vpnOffset = 0; for (Network network : networks) { LinkProperties linkProperties = connectivityManager.getLinkProperties(network); if (linkProperties == null) { continue; } NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network); final boolean isActiveNetwork = network.equals(activeNetwork); if (networkInfo != null && isActiveNetwork && networkInfo.getType() == ConnectivityManager.TYPE_VPN) { final List<String> tmp = getIPv4First(linkProperties.getDnsServers()); servers.addAll(0, tmp); vpnOffset += tmp.size(); } else if (hasDefaultRoute(linkProperties) || isActiveNetwork) { servers.addAll(vpnOffset, getIPv4First(linkProperties.getDnsServers())); } else { servers.addAll(getIPv4First(linkProperties.getDnsServers())); } } return servers.toArray(new String[servers.size()]); }
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private static boolean hasDefaultRoute(LinkProperties linkProperties) { for (RouteInfo route : linkProperties.getRoutes()) { if (route.isDefaultRoute()) { return true; } } return false; }
public static List<String> getDefaultDNS(Context context) { String dns1 = null; String dns2 = null; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); Network an = cm.getActiveNetwork(); if (an != null) { LinkProperties lp = cm.getLinkProperties(an); if (lp != null) { List<InetAddress> dns = lp.getDnsServers(); if (dns != null) { if (dns.size() > 0) dns1 = dns.get(0).getHostAddress(); if (dns.size() > 1) dns2 = dns.get(1).getHostAddress(); for (InetAddress d : dns) Log.i(TAG, "DNS from LP: " + d.getHostAddress()); } } } } else { dns1 = jni_getprop("net.dns1"); dns2 = jni_getprop("net.dns2"); } List<String> listDns = new ArrayList<>(); listDns.add(TextUtils.isEmpty(dns1) ? "8.8.8.8" : dns1); listDns.add(TextUtils.isEmpty(dns2) ? "8.8.4.4" : dns2); return listDns; }
@Override @TargetApi(21) public String[] getDnsServerAddresses() { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); Network[] networks = connectivityManager == null ? null : connectivityManager.getAllNetworks(); if (networks == null) { return new String[0]; } final Network activeNetwork = getActiveNetwork(connectivityManager); List<String> servers = new ArrayList<>(); int vpnOffset = 0; for(Network network : networks) { LinkProperties linkProperties = connectivityManager.getLinkProperties(network); if (linkProperties == null) { continue; } NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network); final boolean isActiveNetwork = network.equals(activeNetwork); if (networkInfo != null && isActiveNetwork && networkInfo.getType() == ConnectivityManager.TYPE_VPN) { final List<String> tmp = getIPv4First(linkProperties.getDnsServers()); servers.addAll(0, tmp); vpnOffset += tmp.size(); } else if (hasDefaultRoute(linkProperties) || isActiveNetwork) { servers.addAll(vpnOffset, getIPv4First(linkProperties.getDnsServers())); } else { servers.addAll(getIPv4First(linkProperties.getDnsServers())); } } return servers.toArray(new String[servers.size()]); }
@SuppressLint("NewApi") private NetworkInformation networkToInfo(Network network) { LinkProperties linkProperties = connectivityManager.getLinkProperties(network); // getLinkProperties will return null if the network is unknown. if (linkProperties == null) { Logging.w(TAG, "Detected unknown network: " + network.toString()); return null; } if (linkProperties.getInterfaceName() == null) { Logging.w(TAG, "Null interface name for network " + network.toString()); return null; } NetworkState networkState = getNetworkState(network); if (networkState.connected && networkState.getNetworkType() == ConnectivityManager.TYPE_VPN) { // If a VPN network is in place, we can find the underlying network type via querying the // active network info thanks to // https://android.googlesource.com/platform/frameworks/base/+/d6a7980d networkState = getNetworkState(); } ConnectionType connectionType = getConnectionType(networkState); if (connectionType == ConnectionType.CONNECTION_NONE) { // This may not be an error. The OS may signal a network event with connection type // NONE when the network disconnects. Logging.d(TAG, "Network " + network.toString() + " is disconnected"); return null; } // Some android device may return a CONNECTION_UNKNOWN_CELLULAR or CONNECTION_UNKNOWN type, // which appears to be usable. Just log them here. if (connectionType == ConnectionType.CONNECTION_UNKNOWN || connectionType == ConnectionType.CONNECTION_UNKNOWN_CELLULAR) { Logging.d(TAG, "Network " + network.toString() + " connection type is " + connectionType + " because it has type " + networkState.getNetworkType() + " and subtype " + networkState.getNetworkSubType()); } NetworkInformation networkInformation = new NetworkInformation(linkProperties.getInterfaceName(), connectionType, networkToNetId(network), getIPAddresses(linkProperties)); return networkInformation; }
@Override public void onLinkPropertiesChanged(Network network, LinkProperties properties) { ConnectivityMonitor.connectionStatusChanged(); }
@Override public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) { super.onLinkPropertiesChanged(network, linkProperties); }
@SuppressLint("NewApi") private void showIpConfigFields() { WifiConfiguration config = null; mView.findViewById(R.id.ip_fields).setVisibility(View.VISIBLE); if (mAccessPoint != null && mAccessPoint.networkId != INVALID_NETWORK_ID) { config = mAccessPoint.getConfig(); } if (mIpSettingsSpinner.getSelectedItemPosition() == STATIC_IP) { mView.findViewById(R.id.staticip).setVisibility(View.VISIBLE); if (mIpAddressView == null) { mIpAddressView = (TextView) mView.findViewById(R.id.ipaddress); mIpAddressView.addTextChangedListener(this); mGatewayView = (TextView) mView.findViewById(R.id.gateway); mGatewayView.addTextChangedListener(this); mNetworkPrefixLengthView = (TextView) mView.findViewById( R.id.network_prefix_length); mNetworkPrefixLengthView.addTextChangedListener(this); mDns1View = (TextView) mView.findViewById(R.id.dns1); mDns1View.addTextChangedListener(this); mDns2View = (TextView) mView.findViewById(R.id.dns2); mDns2View.addTextChangedListener(this); } if (config != null) { LinkProperties linkProperties = config.linkProperties; Iterator<LinkAddress> iterator = linkProperties.getLinkAddresses().iterator(); if (iterator.hasNext()) { LinkAddress linkAddress = iterator.next(); mIpAddressView.setText(linkAddress.getAddress().getHostAddress()); mNetworkPrefixLengthView.setText(Integer.toString(linkAddress .getNetworkPrefixLength())); } for (RouteInfo route : linkProperties.getRoutes()) { if (route.isDefaultRoute()) { mGatewayView.setText(route.getGateway().getHostAddress()); break; } } Iterator<InetAddress> dnsIterator = linkProperties.getDnses().iterator(); if (dnsIterator.hasNext()) { mDns1View.setText(dnsIterator.next().getHostAddress()); } if (dnsIterator.hasNext()) { mDns2View.setText(dnsIterator.next().getHostAddress()); } } } else { mView.findViewById(R.id.staticip).setVisibility(View.GONE); } }
static public NetworkStatus checkConnectivity(Context context) { NetworkStatus networkStatus = NetworkStatus.NetworkStatusNone; RCLogger.d(TAG, "checkConnectivity()"); ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); try { NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo(); if (activeNetwork != null) { if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI && activeNetwork.isConnected()) { RCLogger.w(TAG, "Connectivity status: WIFI"); networkStatus = NetworkStatus.NetworkStatusWiFi; } if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE && activeNetwork.isConnected()) { RCLogger.w(TAG, "Connectivity status: CELLULAR DATA"); networkStatus = NetworkStatus.NetworkStatusCellular; } if (activeNetwork.getType() == ConnectivityManager.TYPE_ETHERNET && activeNetwork.isConnected()) { RCLogger.w(TAG, "Connectivity status: ETHERNET"); networkStatus = NetworkStatus.NetworkStatusEthernet; } } // Sadly we cannot use getActiveNetwork right away as its added in API 23 (and we want to support > 21), so let's iterate // until we find above activeNetwork for (Network network : connectivityManager.getAllNetworks()) { NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network); if (networkInfo.isConnected() && (networkInfo.getType() == activeNetwork.getType()) && (networkInfo.getSubtype() == activeNetwork.getSubtype()) && (networkInfo.getExtraInfo().equals(activeNetwork.getExtraInfo()))) { LinkProperties linkProperties = connectivityManager.getLinkProperties(network); //Log.d("DnsInfo", "iface = " + linkProperties.getInterfaceName()); //Log.d("DnsInfo", "dns = " + linkProperties.getDnsServers()); //Log.d("DnsInfo", "domains search = " + linkProperties.getDomains()); StringBuilder stringBuilder = new StringBuilder(); for (InetAddress inetAddress : linkProperties.getDnsServers()) { if (stringBuilder.length() != 0) { stringBuilder.append(","); } stringBuilder.append(inetAddress.getHostAddress()); } // From Oreo and above we need to explicitly retrieve and provide the name servers used for our DNS queries. // Reason for that is that prior to Oreo underlying dnsjava library for jain-sip.ext (i.e. providing facilities for DNS SRV), // used some system properties prefixed 'net.dns1', etc. The problem is that those got removed in Oreo so we have to use // alternative means, and that is to use 'dns.server' that 'dnsjava' tries to use before trying 'net.dns1'; if (stringBuilder.length() != 0) { RCLogger.i(TAG, "Updating DNS servers for dnsjava with: " + stringBuilder.toString() + ", i/f: " + linkProperties.getInterfaceName()); System.setProperty("dns.server", stringBuilder.toString()); } if (linkProperties.getDomains() != null) { RCLogger.i(TAG, "Updating DNS search domains for dnsjava with: " + linkProperties.getDomains() + ", i/f: " + linkProperties.getInterfaceName()); System.setProperty("dns.search", linkProperties.getDomains()); } } } } catch (NullPointerException e) { throw new RuntimeException("Failed to retrieve active networks info", e); } if (networkStatus == NetworkStatus.NetworkStatusNone) { RCLogger.w(TAG, "Connectivity status: NONE"); } return networkStatus; }
@TargetApi(Build.VERSION_CODES.M) private Bundle analyzeActiveNetwork(Context context) { final Bundle result = new Bundle(); final ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); final Network network = cm.getActiveNetwork(); if (network == null) { result.putString(context.getString(R.string.network_interface_pref), context.getString(R.string.no_connection)); return result; } final LinkProperties properties = cm.getLinkProperties(network); final NetworkInfo amm = cm.getNetworkInfo(network); final String interfaceName = properties.getInterfaceName(); final NetworkInterface iface = InterfaceUtil.resolveInterfaceByName(interfaceName); if (iface == null) { String explanation = amm.getReason(); if (TextUtils.isEmpty(explanation)) { final String err = context.getString(R.string.unable_to_get_if_info); explanation = TextUtils.isEmpty(interfaceName) ? err: interfaceName + ": " + err; } result.putString(context.getString(R.string.network_interface_pref), explanation); return result; } final NetworkCapabilities netcaps = cm.getNetworkCapabilities(network); String netDesc = amm.getTypeName(); final String subtupe = amm.getSubtypeName(); if (!TextUtils.isEmpty(subtupe)) { netDesc = interfaceName + ": " + netDesc + ' ' + subtupe + " ↑ " + netcaps.getLinkUpstreamBandwidthKbps() + " Kbps" + " ↓ " + netcaps.getLinkDownstreamBandwidthKbps() + " Kbps"; } result.putString(context.getString(R.string.network_interface_pref), netDesc); return result; }