1 /* 2 * Copyright (C) 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 android.net.shared; 18 19 import static android.net.shared.ParcelableUtil.fromParcelableArray; 20 import static android.net.shared.ParcelableUtil.toParcelableArray; 21 22 import android.net.PrivateDnsConfigParcel; 23 import android.text.TextUtils; 24 25 import java.net.InetAddress; 26 import java.util.Arrays; 27 28 /** @hide */ 29 public class PrivateDnsConfig { 30 public final boolean useTls; 31 public final String hostname; 32 public final InetAddress[] ips; 33 PrivateDnsConfig()34 public PrivateDnsConfig() { 35 this(false); 36 } 37 PrivateDnsConfig(boolean useTls)38 public PrivateDnsConfig(boolean useTls) { 39 this.useTls = useTls; 40 this.hostname = ""; 41 this.ips = new InetAddress[0]; 42 } 43 PrivateDnsConfig(String hostname, InetAddress[] ips)44 public PrivateDnsConfig(String hostname, InetAddress[] ips) { 45 this.useTls = !TextUtils.isEmpty(hostname); 46 this.hostname = useTls ? hostname : ""; 47 this.ips = (ips != null) ? ips : new InetAddress[0]; 48 } 49 PrivateDnsConfig(PrivateDnsConfig cfg)50 public PrivateDnsConfig(PrivateDnsConfig cfg) { 51 useTls = cfg.useTls; 52 hostname = cfg.hostname; 53 ips = cfg.ips; 54 } 55 56 /** 57 * Indicates whether this is a strict mode private DNS configuration. 58 */ inStrictMode()59 public boolean inStrictMode() { 60 return useTls && !TextUtils.isEmpty(hostname); 61 } 62 63 @Override toString()64 public String toString() { 65 return PrivateDnsConfig.class.getSimpleName() 66 + "{" + useTls + ":" + hostname + "/" + Arrays.toString(ips) + "}"; 67 } 68 69 /** 70 * Create a stable AIDL-compatible parcel from the current instance. 71 */ toParcel()72 public PrivateDnsConfigParcel toParcel() { 73 final PrivateDnsConfigParcel parcel = new PrivateDnsConfigParcel(); 74 parcel.hostname = hostname; 75 parcel.ips = toParcelableArray( 76 Arrays.asList(ips), IpConfigurationParcelableUtil::parcelAddress, String.class); 77 78 return parcel; 79 } 80 81 /** 82 * Build a configuration from a stable AIDL-compatible parcel. 83 */ fromParcel(PrivateDnsConfigParcel parcel)84 public static PrivateDnsConfig fromParcel(PrivateDnsConfigParcel parcel) { 85 InetAddress[] ips = new InetAddress[parcel.ips.length]; 86 ips = fromParcelableArray(parcel.ips, IpConfigurationParcelableUtil::unparcelAddress) 87 .toArray(ips); 88 return new PrivateDnsConfig(parcel.hostname, ips); 89 } 90 } 91