1 /* 2 * Copyright (C) 2020 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.os.Parcel; 21 import android.util.Log; 22 23 24 import java.net.Inet6Address; 25 import java.net.InetAddress; 26 import java.net.UnknownHostException; 27 28 /** 29 * Collection of utilities to interact with {@link InetAddress} 30 * @hide 31 */ 32 public class InetAddressUtils { 33 34 private static final String TAG = InetAddressUtils.class.getSimpleName(); 35 private static final int INET6_ADDR_LENGTH = 16; 36 37 /** 38 * Writes an InetAddress to a parcel. The address may be null. This is likely faster than 39 * calling writeSerializable. 40 * @hide 41 */ parcelInetAddress(Parcel parcel, InetAddress address, int flags)42 public static void parcelInetAddress(Parcel parcel, InetAddress address, int flags) { 43 byte[] addressArray = (address != null) ? address.getAddress() : null; 44 parcel.writeByteArray(addressArray); 45 if (address instanceof Inet6Address) { 46 final Inet6Address v6Address = (Inet6Address) address; 47 final boolean hasScopeId = v6Address.getScopeId() != 0; 48 parcel.writeBoolean(hasScopeId); 49 if (hasScopeId) parcel.writeInt(v6Address.getScopeId()); 50 } 51 52 } 53 54 /** 55 * Reads an InetAddress from a parcel. Returns null if the address that was written was null 56 * or if the data is invalid. 57 * @hide 58 */ unparcelInetAddress(Parcel in)59 public static InetAddress unparcelInetAddress(Parcel in) { 60 byte[] addressArray = in.createByteArray(); 61 if (addressArray == null) { 62 return null; 63 } 64 65 try { 66 if (addressArray.length == INET6_ADDR_LENGTH) { 67 final boolean hasScopeId = in.readBoolean(); 68 final int scopeId = hasScopeId ? in.readInt() : 0; 69 return Inet6Address.getByAddress(null /* host */, addressArray, scopeId); 70 } 71 72 return InetAddress.getByAddress(addressArray); 73 } catch (UnknownHostException e) { 74 return null; 75 } 76 } 77 78 /** 79 * Create a Inet6Address with scope id if it is a link local address. Otherwise, returns the 80 * original address. 81 */ withScopeId(@onNull final Inet6Address addr, int scopeid)82 public static Inet6Address withScopeId(@NonNull final Inet6Address addr, int scopeid) { 83 if (!addr.isLinkLocalAddress()) { 84 return addr; 85 } 86 try { 87 return Inet6Address.getByAddress(null /* host */, addr.getAddress(), 88 scopeid); 89 } catch (UnknownHostException impossible) { 90 Log.wtf(TAG, "Cannot construct scoped Inet6Address with Inet6Address.getAddress(" 91 + addr.getHostAddress() + "): ", impossible); 92 return null; 93 } 94 } 95 InetAddressUtils()96 private InetAddressUtils() {} 97 } 98