Move shared methods to NetUtils

The getNetworkPart() method is used by Settings and wifi. The
method was inside NetworkUtils, but NetworkUtils is moving
to connectivity mainline module. Methods in NetworkUtils will
not be accessible outside module. Thus, move the shared method
and the corresponding helper method to share lib.

Bug: 172183305
Test: atest FrameworksNetTests
Change-Id: Ic39f0debf1146dd84db962857dcead6b498aec49
diff --git a/common/framework/com/android/net/module/util/NetUtils.java b/common/framework/com/android/net/module/util/NetUtils.java
index 4331b65..f08257a 100644
--- a/common/framework/com/android/net/module/util/NetUtils.java
+++ b/common/framework/com/android/net/module/util/NetUtils.java
@@ -23,6 +23,7 @@
 import java.net.Inet4Address;
 import java.net.Inet6Address;
 import java.net.InetAddress;
+import java.net.UnknownHostException;
 import java.util.Collection;
 
 /**
@@ -67,4 +68,44 @@
         }
         return bestRoute;
     }
+
+    /**
+     * Get InetAddress masked with prefixLength.  Will never return null.
+     * @param address the IP address to mask with
+     * @param prefixLength the prefixLength used to mask the IP
+     */
+    public static InetAddress getNetworkPart(InetAddress address, int prefixLength) {
+        byte[] array = address.getAddress();
+        maskRawAddress(array, prefixLength);
+
+        InetAddress netPart = null;
+        try {
+            netPart = InetAddress.getByAddress(array);
+        } catch (UnknownHostException e) {
+            throw new RuntimeException("getNetworkPart error - " + e.toString());
+        }
+        return netPart;
+    }
+
+    /**
+     *  Masks a raw IP address byte array with the specified prefix length.
+     */
+    public static void maskRawAddress(byte[] array, int prefixLength) {
+        if (prefixLength < 0 || prefixLength > array.length * 8) {
+            throw new RuntimeException("IP address with " + array.length
+                    + " bytes has invalid prefix length " + prefixLength);
+        }
+
+        int offset = prefixLength / 8;
+        int remainder = prefixLength % 8;
+        byte mask = (byte) (0xFF << (8 - remainder));
+
+        if (offset < array.length) array[offset] = (byte) (array[offset] & mask);
+
+        offset++;
+
+        for (; offset < array.length; offset++) {
+            array[offset] = 0;
+        }
+    }
 }