import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
-import java.io.InputStreamReader;
import java.io.IOException;
-import java.lang.IllegalStateException;
-import java.net.InetAddress;
+import java.io.InputStreamReader;
import java.net.Inet4Address;
+import java.net.InetAddress;
+ import java.net.UnknownHostException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.NoSuchElementException;
+import java.util.StringTokenizer;
import java.util.concurrent.CountDownLatch;
+import libcore.io.IoUtils;
+
/**
* @hide
*/
return getInterfaceThrottle(iface, false);
}
+ /**
+ * Split given line into {@link ArrayList}.
+ */
+ private static void splitLine(String line, ArrayList<String> outSplit) {
+ outSplit.clear();
+
+ final StringTokenizer t = new StringTokenizer(line);
+ while (t.hasMoreTokens()) {
+ outSplit.add(t.nextToken());
+ }
+ }
+
+ /**
+ * Zip the two given {@link ArrayList} as key and value pairs into
+ * {@link HashMap}.
+ */
+ private static void parseLine(
+ ArrayList<String> keys, ArrayList<String> values, HashMap<String, String> outParsed) {
+ outParsed.clear();
+
+ final int size = Math.min(keys.size(), values.size());
+ for (int i = 0; i < size; i++) {
+ outParsed.put(keys.get(i), values.get(i));
+ }
+ }
+
+ /**
+ * Utility method to read a single plain-text {@link Long} from the given
+ * {@link File}, usually from a {@code /proc/} filesystem.
+ */
+ private static long readSingleLongFromFile(File file) {
+ try {
+ final byte[] buffer = IoUtils.readFileAsByteArray(file.toString());
+ return Long.parseLong(new String(buffer).trim());
+ } catch (NumberFormatException e) {
+ return -1;
+ } catch (IOException e) {
+ return -1;
+ }
+ }
++
+ public void setDefaultInterfaceForDns(String iface) throws IllegalStateException {
+ mContext.enforceCallingOrSelfPermission(
+ android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService");
+ try {
+ String cmd = "resolver setdefaultif " + iface;
+
+ mConnector.doCommand(cmd);
+ } catch (NativeDaemonConnectorException e) {
+ throw new IllegalStateException(
+ "Error communicating with native daemon to set default interface", e);
+ }
+ }
+
+ public void setDnsServersForInterface(String iface, String[] servers)
+ throws IllegalStateException {
+ mContext.enforceCallingOrSelfPermission(android.Manifest.permission.CHANGE_NETWORK_STATE,
+ "NetworkManagementService");
+ try {
+ String cmd = "resolver setifdns " + iface;
+ for (String s : servers) {
+ if (s != null && !"0.0.0.0".equals(s) &&
+ !"::".equals(s) && !"0:0:0:0:0:0:0:0".equals(s)) {
+ cmd += " " + InetAddress.getByName(s).getHostAddress();
+ }
+ }
+
+ mConnector.doCommand(cmd);
+ } catch (UnknownHostException e) {
+ throw new IllegalStateException("failed to resolve dns address.", e);
+ } catch (NativeDaemonConnectorException e) {
+ throw new IllegalStateException(
+ "Error communicating with native deamon to set dns for interface", e);
+ }
+ }
+
+ public void flushDefaultDnsCache() throws IllegalStateException {
+ mContext.enforceCallingOrSelfPermission(
+ android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService");
+ try {
+ String cmd = "resolver flushdefaultif";
+
+ mConnector.doCommand(cmd);
+ } catch (NativeDaemonConnectorException e) {
+ throw new IllegalStateException(
+ "Error communicating with native deamon to flush default interface", e);
+ }
+ }
+
+ public void flushInterfaceDnsCache(String iface) throws IllegalStateException {
+ mContext.enforceCallingOrSelfPermission(
+ android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService");
+ try {
+ String cmd = "resolver flushif " + iface;
+
+ mConnector.doCommand(cmd);
+ } catch (NativeDaemonConnectorException e) {
+ throw new IllegalStateException(
+ "Error communicating with native deamon to flush interface " + iface, e);
+ }
+ }
}