OSDN Git Service

Merge 37dbfcaf
authorJean-Baptiste Queru <jbq@google.com>
Thu, 7 Jul 2011 15:46:09 +0000 (08:46 -0700)
committerJean-Baptiste Queru <jbq@google.com>
Thu, 7 Jul 2011 15:46:09 +0000 (08:46 -0700)
Change-Id: Iae55ecada8dc231c88bb87b32e8bd5e7b6a47bd0

1  2 
core/java/android/os/INetworkManagementService.aidl
services/java/com/android/server/NetworkManagementService.java

@@@ -50,19 -49,14 +50,20 @@@ import java.io.DataInputStream
  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
   */
@@@ -1190,44 -950,64 +1191,105 @@@ class NetworkManagementService extends 
          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);
+         }
+     }
  }