Skip to content

Commit 41394a3

Browse files
Robert GreenwaltAndroid (Google) Code Review
authored andcommitted
Merge "Use dns proxy a bit." into honeycomb-LTE
2 parents ac74551 + 572b704 commit 41394a3

4 files changed

Lines changed: 105 additions & 0 deletions

File tree

core/java/android/net/NetworkUtils.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.net.Inet4Address;
2121
import java.net.Inet6Address;
2222
import java.net.UnknownHostException;
23+
import java.util.Collection;
2324

2425
import android.util.Log;
2526

@@ -235,4 +236,18 @@ public static InetAddress hexToInet6Address(String addrHexString)
235236
throw new IllegalArgumentException(e);
236237
}
237238
}
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+
}
238253
}

core/java/android/os/INetworkManagementService.aidl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,4 +224,23 @@ interface INetworkManagementService
224224
*/
225225
int getInterfaceTxThrottle(String iface);
226226

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);
227246
}

services/java/com/android/server/ConnectivityService.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1783,8 +1783,19 @@ private void handleDnsConfigurationChange(int netType) {
17831783
LinkProperties p = nt.getLinkProperties();
17841784
if (p == null) return;
17851785
Collection<InetAddress> dnses = p.getDnses();
1786+
try {
1787+
mNetd.setDnsServersForInterface(p.getInterfaceName(),
1788+
NetworkUtils.makeStrings(dnses));
1789+
} catch (Exception e) {
1790+
Slog.e(TAG, "exception setting dns servers: " + e);
1791+
}
17861792
boolean changed = false;
17871793
if (mNetConfigs[netType].isDefault()) {
1794+
try {
1795+
mNetd.setDefaultInterfaceForDns(p.getInterfaceName());
1796+
} catch (Exception e) {
1797+
Slog.e(TAG, "exception setting default dns interface: " + e);
1798+
}
17881799
int j = 1;
17891800
if (dnses.size() == 0 && mDefaultDns != null) {
17901801
String dnsString = mDefaultDns.getHostAddress();

services/java/com/android/server/NetworkManagementService.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,4 +949,64 @@ public int getInterfaceRxThrottle(String iface) {
949949
public int getInterfaceTxThrottle(String iface) {
950950
return getInterfaceThrottle(iface, false);
951951
}
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+
}
9521012
}

0 commit comments

Comments
 (0)