Loading core/java/android/os/INetworkManagementService.aidl +10 −0 Original line number Diff line number Diff line Loading @@ -352,4 +352,14 @@ interface INetworkManagementService void setFirewallEgressSourceRule(String addr, boolean allow); void setFirewallEgressDestRule(String addr, int port, boolean allow); void setFirewallUidRule(int uid, boolean allow); /** * Set a process (pid) to use the name servers associated with the specified interface. */ void setDnsIfaceForPid(String iface, int pid); /** * Clear a process (pid) from being associated with an interface. */ void clearDnsIfaceForPid(int pid); } services/java/com/android/server/ConnectivityService.java +43 −114 Original line number Diff line number Diff line Loading @@ -183,7 +183,7 @@ public class ConnectivityService extends IConnectivityManager.Stub { * A per Net list of the PID's that requested access to the net * used both as a refcount and for per-PID DNS selection */ private List mNetRequestersPids[]; private List<Integer> mNetRequestersPids[]; // priority order of the nettrackers // (excluding dynamically set mNetworkPreference) Loading @@ -200,7 +200,6 @@ public class ConnectivityService extends IConnectivityManager.Stub { private int mDefaultConnectionSequence = 0; private Object mDnsLock = new Object(); private int mNumDnsEntries; private boolean mDnsOverridden = false; private boolean mTestMode; Loading Loading @@ -508,15 +507,14 @@ public class ConnectivityService extends IConnectivityManager.Stub { } } mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1]; mNetRequestersPids = (List<Integer> [])new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1]; for (int i : mPriorityList) { mNetRequestersPids[i] = new ArrayList(); mNetRequestersPids[i] = new ArrayList<Integer>(); } mFeatureUsers = new ArrayList<FeatureUser>(); mNumDnsEntries = 0; mTestMode = SystemProperties.get("cm.test.mode").equals("true") && SystemProperties.get("ro.build.type").equals("eng"); Loading Loading @@ -1317,6 +1315,7 @@ public class ConnectivityService extends IConnectivityManager.Stub { Integer currentPid = new Integer(pid); mNetRequestersPids[usedNetworkType].remove(currentPid); reassessPidDns(pid, true); flushVmDnsCache(); if (mNetRequestersPids[usedNetworkType].size() != 0) { if (VDBG) { log("stopUsingNetworkFeature: net " + networkType + ": " + feature + Loading Loading @@ -1698,9 +1697,8 @@ public class ConnectivityService extends IConnectivityManager.Stub { * in accordance with network preference policies. */ if (!mNetConfigs[prevNetType].isDefault()) { List pids = mNetRequestersPids[prevNetType]; for (int i = 0; i<pids.size(); i++) { Integer pid = (Integer)pids.get(i); List<Integer> pids = mNetRequestersPids[prevNetType]; for (Integer pid : pids) { // will remove them because the net's no longer connected // need to do this now as only now do we know the pids and // can properly null things that are no longer referenced. Loading Loading @@ -2250,6 +2248,7 @@ public class ConnectivityService extends IConnectivityManager.Stub { } } if (resetDns) { flushVmDnsCache(); if (VDBG) log("resetting DNS cache for " + iface); try { mNetd.flushInterfaceDnsCache(iface); Loading Loading @@ -2416,9 +2415,10 @@ public class ConnectivityService extends IConnectivityManager.Stub { * on the highest priority active net which this process requested. * If there aren't any, clear it out */ private void reassessPidDns(int myPid, boolean doBump) private void reassessPidDns(int pid, boolean doBump) { if (VDBG) log("reassessPidDns for pid " + myPid); if (VDBG) log("reassessPidDns for pid " + pid); Integer myPid = new Integer(pid); for(int i : mPriorityList) { if (mNetConfigs[i].isDefault()) { continue; Loading @@ -2428,61 +2428,25 @@ public class ConnectivityService extends IConnectivityManager.Stub { !nt.isTeardownRequested()) { LinkProperties p = nt.getLinkProperties(); if (p == null) continue; List pids = mNetRequestersPids[i]; for (int j=0; j<pids.size(); j++) { Integer pid = (Integer)pids.get(j); if (pid.intValue() == myPid) { Collection<InetAddress> dnses = p.getDnses(); writePidDns(dnses, myPid); if (doBump) { bumpDns(); if (mNetRequestersPids[i].contains(myPid)) { try { mNetd.setDnsIfaceForPid(p.getInterfaceName(), pid); } catch (Exception e) { Slog.e(TAG, "exception reasseses pid dns: " + e); } return; } } } } // nothing found - delete for (int i = 1; ; i++) { String prop = "net.dns" + i + "." + myPid; if (SystemProperties.get(prop).length() == 0) { if (doBump) { bumpDns(); } return; } SystemProperties.set(prop, ""); } } // return true if results in a change private boolean writePidDns(Collection <InetAddress> dnses, int pid) { int j = 1; boolean changed = false; for (InetAddress dns : dnses) { String dnsString = dns.getHostAddress(); if (changed || !dnsString.equals(SystemProperties.get("net.dns" + j + "." + pid))) { changed = true; SystemProperties.set("net.dns" + j + "." + pid, dns.getHostAddress()); } j++; try { mNetd.clearDnsIfaceForPid(pid); } catch (Exception e) { Slog.e(TAG, "exception clear interface from pid: " + e); } return changed; } private void bumpDns() { /* * Bump the property that tells the name resolver library to reread * the DNS server list from the properties. */ String propVal = SystemProperties.get("net.dnschange"); int n = 0; if (propVal.length() != 0) { try { n = Integer.parseInt(propVal); } catch (NumberFormatException e) {} } SystemProperties.set("net.dnschange", "" + (n+1)); private void flushVmDnsCache() { /* * Tell the VMs to toss their DNS caches */ Loading @@ -2501,48 +2465,17 @@ public class ConnectivityService extends IConnectivityManager.Stub { } // Caller must grab mDnsLock. private boolean updateDns(String network, String iface, private void updateDns(String network, String iface, Collection<InetAddress> dnses, String domains) { boolean changed = false; int last = 0; if (dnses.size() == 0 && mDefaultDns != null) { ++last; String value = mDefaultDns.getHostAddress(); if (!value.equals(SystemProperties.get("net.dns1"))) { dnses = new ArrayList(); dnses.add(mDefaultDns); if (DBG) { loge("no dns provided for " + network + " - using " + value); } changed = true; SystemProperties.set("net.dns1", value); } } else { for (InetAddress dns : dnses) { ++last; String key = "net.dns" + last; String value = dns.getHostAddress(); if (!changed && value.equals(SystemProperties.get(key))) { continue; } if (VDBG) { log("adding dns " + value + " for " + network); loge("no dns provided for " + network + " - using " + mDefaultDns.getHostAddress()); } changed = true; SystemProperties.set(key, value); } } for (int i = last + 1; i <= mNumDnsEntries; ++i) { String key = "net.dns" + i; if (VDBG) log("erasing " + key); changed = true; SystemProperties.set(key, ""); } mNumDnsEntries = last; if (SystemProperties.get("net.dns.search").equals(domains) == false) { SystemProperties.set("net.dns.search", domains); changed = true; } if (changed) { try { mNetd.setDnsServersForInterface(iface, NetworkUtils.makeStrings(dnses), domains); mNetd.setDefaultInterfaceForDns(iface); Loading @@ -2550,8 +2483,6 @@ public class ConnectivityService extends IConnectivityManager.Stub { if (DBG) loge("exception setting default dns interface: " + e); } } return changed; } private void handleDnsConfigurationChange(int netType) { // add default net's dns entries Loading @@ -2560,12 +2491,11 @@ public class ConnectivityService extends IConnectivityManager.Stub { LinkProperties p = nt.getLinkProperties(); if (p == null) return; Collection<InetAddress> dnses = p.getDnses(); boolean changed = false; if (mNetConfigs[netType].isDefault()) { String network = nt.getNetworkInfo().getTypeName(); synchronized (mDnsLock) { if (!mDnsOverridden) { changed = updateDns(network, p.getInterfaceName(), dnses, p.getDomains()); updateDns(network, p.getInterfaceName(), dnses, p.getDomains()); } } } else { Loading @@ -2576,13 +2506,16 @@ public class ConnectivityService extends IConnectivityManager.Stub { if (DBG) loge("exception setting dns servers: " + e); } // set per-pid dns for attached secondary nets List pids = mNetRequestersPids[netType]; for (int y=0; y< pids.size(); y++) { Integer pid = (Integer)pids.get(y); changed = writePidDns(dnses, pid.intValue()); List<Integer> pids = mNetRequestersPids[netType]; for (Integer pid : pids) { try { mNetd.setDnsIfaceForPid(p.getInterfaceName(), pid); } catch (Exception e) { Slog.e(TAG, "exception setting interface for pid: " + e); } } if (changed) bumpDns(); } flushVmDnsCache(); } } Loading Loading @@ -2641,7 +2574,7 @@ public class ConnectivityService extends IConnectivityManager.Stub { pw.increaseIndent(); for (int net : mPriorityList) { String pidString = net + ": "; for (Object pid : mNetRequestersPids[net]) { for (Integer pid : mNetRequestersPids[net]) { pidString = pidString + pid.toString() + ", "; } pw.println(pidString); Loading Loading @@ -3351,14 +3284,10 @@ public class ConnectivityService extends IConnectivityManager.Stub { String domains = buffer.toString().trim(); // Apply DNS changes. boolean changed = false; synchronized (mDnsLock) { changed = updateDns("VPN", "VPN", addresses, domains); updateDns("VPN", "VPN", addresses, domains); mDnsOverridden = true; } if (changed) { bumpDns(); } // Temporarily disable the default proxy. synchronized (mDefaultProxyLock) { Loading services/java/com/android/server/NetworkManagementService.java +26 −0 Original line number Diff line number Diff line Loading @@ -1468,6 +1468,32 @@ public class NetworkManagementService extends INetworkManagementService.Stub } @Override public void setDnsIfaceForPid(String iface, int pid) throws IllegalStateException { mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); try { String cmd = "resolver setifaceforpid " + iface + " " + pid; mConnector.execute(cmd); } catch (NativeDaemonConnectorException e) { throw new IllegalStateException( "Error communicating with native deamon to set interface for pid" + iface, e); } } @Override public void clearDnsIfaceForPid(int pid) throws IllegalStateException { mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); try { String cmd = "resolver clearifaceforpid " + pid; mConnector.execute(cmd); } catch (NativeDaemonConnectorException e) { throw new IllegalStateException( "Error communicating with native deamon to clear interface for pid " + pid, e); } } /** {@inheritDoc} */ public void monitor() { if (mConnector != null) { mConnector.monitor(); Loading Loading
core/java/android/os/INetworkManagementService.aidl +10 −0 Original line number Diff line number Diff line Loading @@ -352,4 +352,14 @@ interface INetworkManagementService void setFirewallEgressSourceRule(String addr, boolean allow); void setFirewallEgressDestRule(String addr, int port, boolean allow); void setFirewallUidRule(int uid, boolean allow); /** * Set a process (pid) to use the name servers associated with the specified interface. */ void setDnsIfaceForPid(String iface, int pid); /** * Clear a process (pid) from being associated with an interface. */ void clearDnsIfaceForPid(int pid); }
services/java/com/android/server/ConnectivityService.java +43 −114 Original line number Diff line number Diff line Loading @@ -183,7 +183,7 @@ public class ConnectivityService extends IConnectivityManager.Stub { * A per Net list of the PID's that requested access to the net * used both as a refcount and for per-PID DNS selection */ private List mNetRequestersPids[]; private List<Integer> mNetRequestersPids[]; // priority order of the nettrackers // (excluding dynamically set mNetworkPreference) Loading @@ -200,7 +200,6 @@ public class ConnectivityService extends IConnectivityManager.Stub { private int mDefaultConnectionSequence = 0; private Object mDnsLock = new Object(); private int mNumDnsEntries; private boolean mDnsOverridden = false; private boolean mTestMode; Loading Loading @@ -508,15 +507,14 @@ public class ConnectivityService extends IConnectivityManager.Stub { } } mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1]; mNetRequestersPids = (List<Integer> [])new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1]; for (int i : mPriorityList) { mNetRequestersPids[i] = new ArrayList(); mNetRequestersPids[i] = new ArrayList<Integer>(); } mFeatureUsers = new ArrayList<FeatureUser>(); mNumDnsEntries = 0; mTestMode = SystemProperties.get("cm.test.mode").equals("true") && SystemProperties.get("ro.build.type").equals("eng"); Loading Loading @@ -1317,6 +1315,7 @@ public class ConnectivityService extends IConnectivityManager.Stub { Integer currentPid = new Integer(pid); mNetRequestersPids[usedNetworkType].remove(currentPid); reassessPidDns(pid, true); flushVmDnsCache(); if (mNetRequestersPids[usedNetworkType].size() != 0) { if (VDBG) { log("stopUsingNetworkFeature: net " + networkType + ": " + feature + Loading Loading @@ -1698,9 +1697,8 @@ public class ConnectivityService extends IConnectivityManager.Stub { * in accordance with network preference policies. */ if (!mNetConfigs[prevNetType].isDefault()) { List pids = mNetRequestersPids[prevNetType]; for (int i = 0; i<pids.size(); i++) { Integer pid = (Integer)pids.get(i); List<Integer> pids = mNetRequestersPids[prevNetType]; for (Integer pid : pids) { // will remove them because the net's no longer connected // need to do this now as only now do we know the pids and // can properly null things that are no longer referenced. Loading Loading @@ -2250,6 +2248,7 @@ public class ConnectivityService extends IConnectivityManager.Stub { } } if (resetDns) { flushVmDnsCache(); if (VDBG) log("resetting DNS cache for " + iface); try { mNetd.flushInterfaceDnsCache(iface); Loading Loading @@ -2416,9 +2415,10 @@ public class ConnectivityService extends IConnectivityManager.Stub { * on the highest priority active net which this process requested. * If there aren't any, clear it out */ private void reassessPidDns(int myPid, boolean doBump) private void reassessPidDns(int pid, boolean doBump) { if (VDBG) log("reassessPidDns for pid " + myPid); if (VDBG) log("reassessPidDns for pid " + pid); Integer myPid = new Integer(pid); for(int i : mPriorityList) { if (mNetConfigs[i].isDefault()) { continue; Loading @@ -2428,61 +2428,25 @@ public class ConnectivityService extends IConnectivityManager.Stub { !nt.isTeardownRequested()) { LinkProperties p = nt.getLinkProperties(); if (p == null) continue; List pids = mNetRequestersPids[i]; for (int j=0; j<pids.size(); j++) { Integer pid = (Integer)pids.get(j); if (pid.intValue() == myPid) { Collection<InetAddress> dnses = p.getDnses(); writePidDns(dnses, myPid); if (doBump) { bumpDns(); if (mNetRequestersPids[i].contains(myPid)) { try { mNetd.setDnsIfaceForPid(p.getInterfaceName(), pid); } catch (Exception e) { Slog.e(TAG, "exception reasseses pid dns: " + e); } return; } } } } // nothing found - delete for (int i = 1; ; i++) { String prop = "net.dns" + i + "." + myPid; if (SystemProperties.get(prop).length() == 0) { if (doBump) { bumpDns(); } return; } SystemProperties.set(prop, ""); } } // return true if results in a change private boolean writePidDns(Collection <InetAddress> dnses, int pid) { int j = 1; boolean changed = false; for (InetAddress dns : dnses) { String dnsString = dns.getHostAddress(); if (changed || !dnsString.equals(SystemProperties.get("net.dns" + j + "." + pid))) { changed = true; SystemProperties.set("net.dns" + j + "." + pid, dns.getHostAddress()); } j++; try { mNetd.clearDnsIfaceForPid(pid); } catch (Exception e) { Slog.e(TAG, "exception clear interface from pid: " + e); } return changed; } private void bumpDns() { /* * Bump the property that tells the name resolver library to reread * the DNS server list from the properties. */ String propVal = SystemProperties.get("net.dnschange"); int n = 0; if (propVal.length() != 0) { try { n = Integer.parseInt(propVal); } catch (NumberFormatException e) {} } SystemProperties.set("net.dnschange", "" + (n+1)); private void flushVmDnsCache() { /* * Tell the VMs to toss their DNS caches */ Loading @@ -2501,48 +2465,17 @@ public class ConnectivityService extends IConnectivityManager.Stub { } // Caller must grab mDnsLock. private boolean updateDns(String network, String iface, private void updateDns(String network, String iface, Collection<InetAddress> dnses, String domains) { boolean changed = false; int last = 0; if (dnses.size() == 0 && mDefaultDns != null) { ++last; String value = mDefaultDns.getHostAddress(); if (!value.equals(SystemProperties.get("net.dns1"))) { dnses = new ArrayList(); dnses.add(mDefaultDns); if (DBG) { loge("no dns provided for " + network + " - using " + value); } changed = true; SystemProperties.set("net.dns1", value); } } else { for (InetAddress dns : dnses) { ++last; String key = "net.dns" + last; String value = dns.getHostAddress(); if (!changed && value.equals(SystemProperties.get(key))) { continue; } if (VDBG) { log("adding dns " + value + " for " + network); loge("no dns provided for " + network + " - using " + mDefaultDns.getHostAddress()); } changed = true; SystemProperties.set(key, value); } } for (int i = last + 1; i <= mNumDnsEntries; ++i) { String key = "net.dns" + i; if (VDBG) log("erasing " + key); changed = true; SystemProperties.set(key, ""); } mNumDnsEntries = last; if (SystemProperties.get("net.dns.search").equals(domains) == false) { SystemProperties.set("net.dns.search", domains); changed = true; } if (changed) { try { mNetd.setDnsServersForInterface(iface, NetworkUtils.makeStrings(dnses), domains); mNetd.setDefaultInterfaceForDns(iface); Loading @@ -2550,8 +2483,6 @@ public class ConnectivityService extends IConnectivityManager.Stub { if (DBG) loge("exception setting default dns interface: " + e); } } return changed; } private void handleDnsConfigurationChange(int netType) { // add default net's dns entries Loading @@ -2560,12 +2491,11 @@ public class ConnectivityService extends IConnectivityManager.Stub { LinkProperties p = nt.getLinkProperties(); if (p == null) return; Collection<InetAddress> dnses = p.getDnses(); boolean changed = false; if (mNetConfigs[netType].isDefault()) { String network = nt.getNetworkInfo().getTypeName(); synchronized (mDnsLock) { if (!mDnsOverridden) { changed = updateDns(network, p.getInterfaceName(), dnses, p.getDomains()); updateDns(network, p.getInterfaceName(), dnses, p.getDomains()); } } } else { Loading @@ -2576,13 +2506,16 @@ public class ConnectivityService extends IConnectivityManager.Stub { if (DBG) loge("exception setting dns servers: " + e); } // set per-pid dns for attached secondary nets List pids = mNetRequestersPids[netType]; for (int y=0; y< pids.size(); y++) { Integer pid = (Integer)pids.get(y); changed = writePidDns(dnses, pid.intValue()); List<Integer> pids = mNetRequestersPids[netType]; for (Integer pid : pids) { try { mNetd.setDnsIfaceForPid(p.getInterfaceName(), pid); } catch (Exception e) { Slog.e(TAG, "exception setting interface for pid: " + e); } } if (changed) bumpDns(); } flushVmDnsCache(); } } Loading Loading @@ -2641,7 +2574,7 @@ public class ConnectivityService extends IConnectivityManager.Stub { pw.increaseIndent(); for (int net : mPriorityList) { String pidString = net + ": "; for (Object pid : mNetRequestersPids[net]) { for (Integer pid : mNetRequestersPids[net]) { pidString = pidString + pid.toString() + ", "; } pw.println(pidString); Loading Loading @@ -3351,14 +3284,10 @@ public class ConnectivityService extends IConnectivityManager.Stub { String domains = buffer.toString().trim(); // Apply DNS changes. boolean changed = false; synchronized (mDnsLock) { changed = updateDns("VPN", "VPN", addresses, domains); updateDns("VPN", "VPN", addresses, domains); mDnsOverridden = true; } if (changed) { bumpDns(); } // Temporarily disable the default proxy. synchronized (mDefaultProxyLock) { Loading
services/java/com/android/server/NetworkManagementService.java +26 −0 Original line number Diff line number Diff line Loading @@ -1468,6 +1468,32 @@ public class NetworkManagementService extends INetworkManagementService.Stub } @Override public void setDnsIfaceForPid(String iface, int pid) throws IllegalStateException { mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); try { String cmd = "resolver setifaceforpid " + iface + " " + pid; mConnector.execute(cmd); } catch (NativeDaemonConnectorException e) { throw new IllegalStateException( "Error communicating with native deamon to set interface for pid" + iface, e); } } @Override public void clearDnsIfaceForPid(int pid) throws IllegalStateException { mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); try { String cmd = "resolver clearifaceforpid " + pid; mConnector.execute(cmd); } catch (NativeDaemonConnectorException e) { throw new IllegalStateException( "Error communicating with native deamon to clear interface for pid " + pid, e); } } /** {@inheritDoc} */ public void monitor() { if (mConnector != null) { mConnector.monitor(); Loading