Allow VPNs to add/remove link addresses dynamically.

Bug: 15409819
Change-Id: If91fc6891d7ce04060362c6cde8c57462394c4e8
diff --git a/core/java/android/net/VpnService.java b/core/java/android/net/VpnService.java
index 9b66997..99bccd0 100644
--- a/core/java/android/net/VpnService.java
+++ b/core/java/android/net/VpnService.java
@@ -212,8 +212,12 @@
      * @see Builder#addAddress
      */
     public boolean addAddress(InetAddress address, int prefixLength) {
-        // TODO
-        return true;
+        check(address, prefixLength);
+        try {
+            return getService().addVpnAddress(address.getHostAddress(), prefixLength);
+        } catch (RemoteException e) {
+            throw new IllegalStateException(e);
+        }
     }
 
     /**
@@ -236,8 +240,12 @@
      * @return {@code true} on success.
      */
     public boolean removeAddress(InetAddress address, int prefixLength) {
-        // TODO
-        return true;
+        check(address, prefixLength);
+        try {
+            return getService().removeVpnAddress(address.getHostAddress(), prefixLength);
+        } catch (RemoteException e) {
+            throw new IllegalStateException(e);
+        }
     }
 
     /**
@@ -286,6 +294,26 @@
     }
 
     /**
+     * Private method to validate address and prefixLength.
+     */
+    private static void check(InetAddress address, int prefixLength) {
+        if (address.isLoopbackAddress()) {
+            throw new IllegalArgumentException("Bad address");
+        }
+        if (address instanceof Inet4Address) {
+            if (prefixLength < 0 || prefixLength > 32) {
+                throw new IllegalArgumentException("Bad prefixLength");
+            }
+        } else if (address instanceof Inet6Address) {
+            if (prefixLength < 0 || prefixLength > 128) {
+                throw new IllegalArgumentException("Bad prefixLength");
+            }
+        } else {
+            throw new IllegalArgumentException("Unsupported family");
+        }
+    }
+
+    /**
      * Helper class to create a VPN interface. This class should be always
      * used within the scope of the outer {@link VpnService}.
      *
@@ -337,26 +365,6 @@
         }
 
         /**
-         * Private method to validate address and prefixLength.
-         */
-        private void check(InetAddress address, int prefixLength) {
-            if (address.isLoopbackAddress()) {
-                throw new IllegalArgumentException("Bad address");
-            }
-            if (address instanceof Inet4Address) {
-                if (prefixLength < 0 || prefixLength > 32) {
-                    throw new IllegalArgumentException("Bad prefixLength");
-                }
-            } else if (address instanceof Inet6Address) {
-                if (prefixLength < 0 || prefixLength > 128) {
-                    throw new IllegalArgumentException("Bad prefixLength");
-                }
-            } else {
-                throw new IllegalArgumentException("Unsupported family");
-            }
-        }
-
-        /**
          * Add a network address to the VPN interface. Both IPv4 and IPv6
          * addresses are supported. At least one address must be set before
          * calling {@link #establish}.