frameworks/base
Révision | 572b7048a6ed6cf6c5f6bc6c9d542dc377d601ff (tree) |
---|---|
l'heure | 2011-07-26 09:00:13 |
Auteur | Robert Greenwalt <rgreenwalt@goog...> |
Commiter | Robert Greenwalt |
Use dns proxy a bit.
Sets the current default interface and sets the dns per interface.
port of changes 23041 and 22098 from opensource.
bug:5060618
Change-Id: I80e7ef88727eeb8ff2b48059f69b270e5a6b5c16
@@ -20,6 +20,7 @@ import java.net.InetAddress; | ||
20 | 20 | import java.net.Inet4Address; |
21 | 21 | import java.net.Inet6Address; |
22 | 22 | import java.net.UnknownHostException; |
23 | +import java.util.Collection; | |
23 | 24 | |
24 | 25 | import android.util.Log; |
25 | 26 |
@@ -235,4 +236,18 @@ public class NetworkUtils { | ||
235 | 236 | throw new IllegalArgumentException(e); |
236 | 237 | } |
237 | 238 | } |
239 | + | |
240 | + /** | |
241 | + * Create a string array of host addresses from a collection of InetAddresses | |
242 | + * @param addrs a Collection of InetAddresses | |
243 | + * @return an array of Strings containing their host addresses | |
244 | + */ | |
245 | + public static String[] makeStrings(Collection<InetAddress> addrs) { | |
246 | + String[] result = new String[addrs.size()]; | |
247 | + int i=0; | |
248 | + for (InetAddress addr : addrs) { | |
249 | + result[i++] = addr.getHostAddress(); | |
250 | + } | |
251 | + return result; | |
252 | + } | |
238 | 253 | } |
@@ -224,4 +224,23 @@ interface INetworkManagementService | ||
224 | 224 | */ |
225 | 225 | int getInterfaceTxThrottle(String iface); |
226 | 226 | |
227 | + /** | |
228 | + * Sets the name fo the default interface in the DNS resolver. | |
229 | + */ | |
230 | + void setDefaultInterfaceForDns(String iface); | |
231 | + | |
232 | + /** | |
233 | + * Bind name servers to an interface in the DNS resolver. | |
234 | + */ | |
235 | + void setDnsServersForInterface(String iface, in String[] servers); | |
236 | + | |
237 | + /** | |
238 | + * Flush the DNS cache associated with the default interface. | |
239 | + */ | |
240 | + void flushDefaultDnsCache(); | |
241 | + | |
242 | + /** | |
243 | + * Flush the DNS cache associated with the specified interface. | |
244 | + */ | |
245 | + void flushInterfaceDnsCache(String iface); | |
227 | 246 | } |
@@ -1757,8 +1757,19 @@ public class ConnectivityService extends IConnectivityManager.Stub { | ||
1757 | 1757 | LinkProperties p = nt.getLinkProperties(); |
1758 | 1758 | if (p == null) return; |
1759 | 1759 | Collection<InetAddress> dnses = p.getDnses(); |
1760 | + try { | |
1761 | + mNetd.setDnsServersForInterface(p.getInterfaceName(), | |
1762 | + NetworkUtils.makeStrings(dnses)); | |
1763 | + } catch (Exception e) { | |
1764 | + Slog.e(TAG, "exception setting dns servers: " + e); | |
1765 | + } | |
1760 | 1766 | boolean changed = false; |
1761 | 1767 | if (mNetConfigs[netType].isDefault()) { |
1768 | + try { | |
1769 | + mNetd.setDefaultInterfaceForDns(p.getInterfaceName()); | |
1770 | + } catch (Exception e) { | |
1771 | + Slog.e(TAG, "exception setting default dns interface: " + e); | |
1772 | + } | |
1762 | 1773 | int j = 1; |
1763 | 1774 | if (dnses.size() == 0 && mDefaultDns != null) { |
1764 | 1775 | String dnsString = mDefaultDns.getHostAddress(); |
@@ -949,4 +949,64 @@ class NetworkManagementService extends INetworkManagementService.Stub { | ||
949 | 949 | public int getInterfaceTxThrottle(String iface) { |
950 | 950 | return getInterfaceThrottle(iface, false); |
951 | 951 | } |
952 | + | |
953 | + public void setDefaultInterfaceForDns(String iface) throws IllegalStateException { | |
954 | + mContext.enforceCallingOrSelfPermission( | |
955 | + android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); | |
956 | + try { | |
957 | + String cmd = "resolver setdefaultif " + iface; | |
958 | + | |
959 | + mConnector.doCommand(cmd); | |
960 | + } catch (NativeDaemonConnectorException e) { | |
961 | + throw new IllegalStateException( | |
962 | + "Error communicating with native daemon to set default interface", e); | |
963 | + } | |
964 | + } | |
965 | + | |
966 | + public void setDnsServersForInterface(String iface, String[] servers) | |
967 | + throws IllegalStateException { | |
968 | + mContext.enforceCallingOrSelfPermission(android.Manifest.permission.CHANGE_NETWORK_STATE, | |
969 | + "NetworkManagementService"); | |
970 | + try { | |
971 | + String cmd = "resolver setifdns " + iface; | |
972 | + for (String s : servers) { | |
973 | + InetAddress a = NetworkUtils.numericToInetAddress(s); | |
974 | + if (a.isAnyLocalAddress() == false) { | |
975 | + cmd += " " + a.getHostAddress(); | |
976 | + } | |
977 | + } | |
978 | + mConnector.doCommand(cmd); | |
979 | + } catch (IllegalArgumentException e) { | |
980 | + throw new IllegalStateException("Error setting dnsn for interface", e); | |
981 | + } catch (NativeDaemonConnectorException e) { | |
982 | + throw new IllegalStateException( | |
983 | + "Error communicating with native daemon to set dns for interface", e); | |
984 | + } | |
985 | + } | |
986 | + | |
987 | + public void flushDefaultDnsCache() throws IllegalStateException { | |
988 | + mContext.enforceCallingOrSelfPermission( | |
989 | + android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); | |
990 | + try { | |
991 | + String cmd = "resolver flushdefaultif"; | |
992 | + | |
993 | + mConnector.doCommand(cmd); | |
994 | + } catch (NativeDaemonConnectorException e) { | |
995 | + throw new IllegalStateException( | |
996 | + "Error communicating with native daemon to flush default interface", e); | |
997 | + } | |
998 | + } | |
999 | + | |
1000 | + public void flushInterfaceDnsCache(String iface) throws IllegalStateException { | |
1001 | + mContext.enforceCallingOrSelfPermission( | |
1002 | + android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService"); | |
1003 | + try { | |
1004 | + String cmd = "resolver flushif " + iface; | |
1005 | + | |
1006 | + mConnector.doCommand(cmd); | |
1007 | + } catch (NativeDaemonConnectorException e) { | |
1008 | + throw new IllegalStateException( | |
1009 | + "Error communicating with native daemon to flush interface " + iface, e); | |
1010 | + } | |
1011 | + } | |
952 | 1012 | } |