1 /* 2 * Copyright 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.net.module.util; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.net.RouteInfo; 22 23 import java.net.Inet4Address; 24 import java.net.Inet6Address; 25 import java.net.InetAddress; 26 import java.net.UnknownHostException; 27 import java.util.Collection; 28 29 /** 30 * Collection of network common utilities. 31 * @hide 32 */ 33 public final class NetUtils { 34 35 /** 36 * Check if IP address type is consistent between two InetAddress. 37 * @return true if both are the same type. False otherwise. 38 */ addressTypeMatches(@onNull InetAddress left, @NonNull InetAddress right)39 public static boolean addressTypeMatches(@NonNull InetAddress left, 40 @NonNull InetAddress right) { 41 return (((left instanceof Inet4Address) && (right instanceof Inet4Address)) 42 || ((left instanceof Inet6Address) && (right instanceof Inet6Address))); 43 } 44 45 /** 46 * Find the route from a Collection of routes that best matches a given address. 47 * May return null if no routes are applicable. 48 * @param routes a Collection of RouteInfos to chose from 49 * @param dest the InetAddress your trying to get to 50 * @return the RouteInfo from the Collection that best fits the given address 51 */ 52 @Nullable selectBestRoute(@ullable Collection<RouteInfo> routes, @Nullable InetAddress dest)53 public static RouteInfo selectBestRoute(@Nullable Collection<RouteInfo> routes, 54 @Nullable InetAddress dest) { 55 if ((routes == null) || (dest == null)) return null; 56 57 RouteInfo bestRoute = null; 58 // pick a longest prefix match under same address type 59 for (RouteInfo route : routes) { 60 if (addressTypeMatches(route.getDestination().getAddress(), dest)) { 61 if ((bestRoute != null) 62 && (bestRoute.getDestination().getPrefixLength() 63 >= route.getDestination().getPrefixLength())) { 64 continue; 65 } 66 if (route.matches(dest)) bestRoute = route; 67 } 68 } 69 return bestRoute; 70 } 71 72 /** 73 * Get InetAddress masked with prefixLength. Will never return null. 74 * @param address the IP address to mask with 75 * @param prefixLength the prefixLength used to mask the IP 76 */ getNetworkPart(InetAddress address, int prefixLength)77 public static InetAddress getNetworkPart(InetAddress address, int prefixLength) { 78 byte[] array = address.getAddress(); 79 maskRawAddress(array, prefixLength); 80 81 InetAddress netPart = null; 82 try { 83 netPart = InetAddress.getByAddress(array); 84 } catch (UnknownHostException e) { 85 throw new RuntimeException("getNetworkPart error - " + e.toString()); 86 } 87 return netPart; 88 } 89 90 /** 91 * Masks a raw IP address byte array with the specified prefix length. 92 */ maskRawAddress(byte[] array, int prefixLength)93 public static void maskRawAddress(byte[] array, int prefixLength) { 94 if (prefixLength < 0 || prefixLength > array.length * 8) { 95 throw new RuntimeException("IP address with " + array.length 96 + " bytes has invalid prefix length " + prefixLength); 97 } 98 99 int offset = prefixLength / 8; 100 int remainder = prefixLength % 8; 101 byte mask = (byte) (0xFF << (8 - remainder)); 102 103 if (offset < array.length) array[offset] = (byte) (array[offset] & mask); 104 105 offset++; 106 107 for (; offset < array.length; offset++) { 108 array[offset] = 0; 109 } 110 } 111 } 112