blob: 651494d1c99c0d200ad9ba193db90eeccff31f76 [file] [log] [blame]
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001/*
Wink Saville6e809972010-09-21 09:15:35 -07002 * Copyright (C) 2010 The Android Open Source Project
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07003 *
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
17package android.net;
18
Jeff Sharkeyeb2c2c72014-08-11 15:22:51 -070019import android.annotation.NonNull;
Jeff Sharkey9da2f1e2014-08-14 12:55:00 -070020import android.annotation.Nullable;
paulhu7610bc72018-12-12 17:52:57 +080021import android.annotation.SystemApi;
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +090022import android.annotation.TestApi;
Artur Satayev26958002019-12-10 17:47:52 +000023import android.compat.annotation.UnsupportedAppUsage;
Aaron Huang94df39a2019-12-17 00:33:18 +080024import android.net.util.LinkPropertiesUtils;
25import android.net.util.LinkPropertiesUtils.CompareResult;
Mathew Inwood55418ea2018-12-20 15:30:45 +000026import android.os.Build;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070027import android.os.Parcel;
Rubin Xu1bb5c082017-09-05 18:40:49 +010028import android.os.Parcelable;
John Wang4e900092011-04-04 12:35:42 -070029import android.text.TextUtils;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070030
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -080031import java.net.Inet4Address;
Lorenzo Colitti4faa0272013-08-08 11:00:12 +090032import java.net.Inet6Address;
Rubin Xu1bb5c082017-09-05 18:40:49 +010033import java.net.InetAddress;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070034import java.net.UnknownHostException;
35import java.util.ArrayList;
36import java.util.Collection;
Robert Greenwalt37e65eb2010-08-30 10:56:47 -070037import java.util.Collections;
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -080038import java.util.Hashtable;
Robert Greenwaltdf2b8782014-06-06 10:30:11 -070039import java.util.List;
Lorenzo Colittic17a1b92014-06-12 23:10:17 +090040import java.util.Objects;
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +090041import java.util.StringJoiner;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070042
43/**
Robert Greenwalt37e65eb2010-08-30 10:56:47 -070044 * Describes the properties of a network link.
Robert Greenwalt992564e2011-02-09 13:56:06 -080045 *
46 * A link represents a connection to a network.
47 * It may have multiple addresses and multiple gateways,
Robert Greenwalt4f05d552014-05-18 22:01:38 -070048 * multiple dns servers but only one http proxy and one
49 * network interface.
Robert Greenwalt992564e2011-02-09 13:56:06 -080050 *
Robert Greenwalt4f05d552014-05-18 22:01:38 -070051 * Note that this is just a holder of data. Modifying it
52 * does not affect live networks.
Robert Greenwalt992564e2011-02-09 13:56:06 -080053 *
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070054 */
Robert Greenwalte595b972014-06-12 16:24:38 -070055public final class LinkProperties implements Parcelable {
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -080056 // The interface described by the network link.
Mathew Inwood55418ea2018-12-20 15:30:45 +000057 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Robert Greenwalt4717c262012-10-31 14:32:53 -070058 private String mIfaceName;
paulhud9736de2019-03-08 16:35:20 +080059 private final ArrayList<LinkAddress> mLinkAddresses = new ArrayList<>();
60 private final ArrayList<InetAddress> mDnses = new ArrayList<>();
Hongshikfa425012018-06-28 20:42:19 +090061 // PCSCF addresses are addresses of SIP proxies that only exist for the IMS core service.
paulhud9736de2019-03-08 16:35:20 +080062 private final ArrayList<InetAddress> mPcscfs = new ArrayList<InetAddress>();
63 private final ArrayList<InetAddress> mValidatedPrivateDnses = new ArrayList<>();
dalykd9201342018-01-17 14:20:55 -050064 private boolean mUsePrivateDns;
65 private String mPrivateDnsServerName;
Robert Greenwalt8058f622012-11-09 10:52:27 -080066 private String mDomains;
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +090067 private ArrayList<RouteInfo> mRoutes = new ArrayList<>();
ruibin zhangad2689c2019-05-23 19:35:30 +080068 private Inet4Address mDhcpServerAddress;
Jason Monk207900c2014-04-25 15:00:09 -040069 private ProxyInfo mHttpProxy;
sy.yun9d9b74a2013-09-02 05:24:09 +090070 private int mMtu;
Robert Greenwalt3f05bf42014-08-06 12:00:25 -070071 // in the format "rmem_min,rmem_def,rmem_max,wmem_min,wmem_def,wmem_max"
72 private String mTcpBufferSizes;
Lorenzo Colitti59b34472019-01-08 09:58:59 +090073 private IpPrefix mNat64Prefix;
Valentin Iftimec86ebba2019-09-24 13:32:13 +020074 private boolean mWakeOnLanSupported;
Remi NGUYEN VANead1ef42019-12-17 16:45:42 +090075 private Uri mCaptivePortalApiUrl;
76 private CaptivePortalData mCaptivePortalData;
77
78 /**
79 * Indicates whether parceling should preserve fields that are set based on permissions of
80 * the process receiving the {@link LinkProperties}.
81 */
82 private final transient boolean mParcelSensitiveFields;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070083
w1997615afd812014-08-05 15:18:11 -070084 private static final int MIN_MTU = 68;
Benedict Wongfeb69c12019-11-01 16:45:08 -070085 /* package-visibility - Used in other files (such as Ikev2VpnProfile) as minimum iface MTU. */
86 static final int MIN_MTU_V6 = 1280;
w1997615afd812014-08-05 15:18:11 -070087 private static final int MAX_MTU = 10000;
88
Remi NGUYEN VANf705ea62019-12-18 14:47:06 +090089 private static final int INET6_ADDR_LENGTH = 16;
90
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -080091 // Stores the properties of links that are "stacked" above this link.
92 // Indexed by interface name to allow modification and to prevent duplicates being added.
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +090093 private Hashtable<String, LinkProperties> mStackedLinks = new Hashtable<>();
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -080094
Robert Greenwaltdf2b8782014-06-06 10:30:11 -070095 /**
96 * @hide
97 */
Artur Satayevfc46be72019-11-04 17:50:59 +000098 @UnsupportedAppUsage(implicitMember =
99 "values()[Landroid/net/LinkProperties$ProvisioningChange;")
Erik Klinecd7ed162015-05-21 16:15:02 +0900100 public enum ProvisioningChange {
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100101 @UnsupportedAppUsage
Erik Klinecd7ed162015-05-21 16:15:02 +0900102 STILL_NOT_PROVISIONED,
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100103 @UnsupportedAppUsage
Erik Klinecd7ed162015-05-21 16:15:02 +0900104 LOST_PROVISIONING,
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100105 @UnsupportedAppUsage
Erik Klinecd7ed162015-05-21 16:15:02 +0900106 GAINED_PROVISIONING,
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100107 @UnsupportedAppUsage
Erik Klinecd7ed162015-05-21 16:15:02 +0900108 STILL_PROVISIONED,
109 }
110
111 /**
112 * Compare the provisioning states of two LinkProperties instances.
113 *
114 * @hide
115 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100116 @UnsupportedAppUsage
Erik Klinecd7ed162015-05-21 16:15:02 +0900117 public static ProvisioningChange compareProvisioning(
118 LinkProperties before, LinkProperties after) {
119 if (before.isProvisioned() && after.isProvisioned()) {
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900120 // On dual-stack networks, DHCPv4 renewals can occasionally fail.
Erik Klinecd7ed162015-05-21 16:15:02 +0900121 // When this happens, IPv6-reachable services continue to function
122 // normally but IPv4-only services (naturally) fail.
123 //
124 // When an application using an IPv4-only service reports a bad
125 // network condition to the framework, attempts to re-validate
126 // the network succeed (since we support IPv6-only networks) and
127 // nothing is changed.
128 //
129 // For users, this is confusing and unexpected behaviour, and is
130 // not necessarily easy to diagnose. Therefore, we treat changing
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900131 // from a dual-stack network to an IPv6-only network equivalent to
Erik Klinecd7ed162015-05-21 16:15:02 +0900132 // a total loss of provisioning.
133 //
134 // For one such example of this, see b/18867306.
135 //
Erik Kline1ad4e222015-08-14 12:16:55 +0900136 // Additionally, losing IPv6 provisioning can result in TCP
137 // connections getting stuck until timeouts fire and other
138 // baffling failures. Therefore, loss of either IPv4 or IPv6 on a
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900139 // previously dual-stack network is deemed a lost of provisioning.
paulhud9736de2019-03-08 16:35:20 +0800140 if ((before.isIpv4Provisioned() && !after.isIpv4Provisioned())
141 || (before.isIpv6Provisioned() && !after.isIpv6Provisioned())) {
Erik Klinecd7ed162015-05-21 16:15:02 +0900142 return ProvisioningChange.LOST_PROVISIONING;
143 }
144 return ProvisioningChange.STILL_PROVISIONED;
145 } else if (before.isProvisioned() && !after.isProvisioned()) {
146 return ProvisioningChange.LOST_PROVISIONING;
147 } else if (!before.isProvisioned() && after.isProvisioned()) {
148 return ProvisioningChange.GAINED_PROVISIONING;
149 } else { // !before.isProvisioned() && !after.isProvisioned()
150 return ProvisioningChange.STILL_NOT_PROVISIONED;
151 }
152 }
153
154 /**
paulhud9736de2019-03-08 16:35:20 +0800155 * Constructs a new {@code LinkProperties} with default values.
Erik Klinecd7ed162015-05-21 16:15:02 +0900156 */
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700157 public LinkProperties() {
Remi NGUYEN VANead1ef42019-12-17 16:45:42 +0900158 mParcelSensitiveFields = false;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700159 }
160
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -0700161 /**
162 * @hide
163 */
Remi NGUYEN VAN299a7cc2019-01-23 21:35:52 +0900164 @SystemApi
165 @TestApi
paulhud9736de2019-03-08 16:35:20 +0800166 public LinkProperties(@Nullable LinkProperties source) {
Remi NGUYEN VANead1ef42019-12-17 16:45:42 +0900167 this(source, false /* parcelSensitiveFields */);
168 }
169
Automerger Merge Worker0acf9102020-03-17 16:02:25 +0000170 /**
171 * Create a copy of a {@link LinkProperties} that may preserve fields that were set
172 * based on the permissions of the process that originally received it.
173 *
174 * <p>By default {@link LinkProperties} does not preserve such fields during parceling, as
175 * they should not be shared outside of the process that receives them without appropriate
176 * checks.
177 * @param parcelSensitiveFields Whether the sensitive fields should be kept when parceling
178 * @hide
179 */
180 @SystemApi
181 @TestApi
182 public LinkProperties(@Nullable LinkProperties source, boolean parcelSensitiveFields) {
Remi NGUYEN VANead1ef42019-12-17 16:45:42 +0900183 mParcelSensitiveFields = parcelSensitiveFields;
184 if (source == null) return;
185 mIfaceName = source.mIfaceName;
186 mLinkAddresses.addAll(source.mLinkAddresses);
187 mDnses.addAll(source.mDnses);
188 mValidatedPrivateDnses.addAll(source.mValidatedPrivateDnses);
189 mUsePrivateDns = source.mUsePrivateDns;
190 mPrivateDnsServerName = source.mPrivateDnsServerName;
191 mPcscfs.addAll(source.mPcscfs);
192 mDomains = source.mDomains;
193 mRoutes.addAll(source.mRoutes);
194 mHttpProxy = (source.mHttpProxy == null) ? null : new ProxyInfo(source.mHttpProxy);
195 for (LinkProperties l: source.mStackedLinks.values()) {
196 addStackedLink(l);
Irfan Sheriffef6c1432010-08-30 20:37:17 -0700197 }
Remi NGUYEN VANead1ef42019-12-17 16:45:42 +0900198 setMtu(source.mMtu);
199 setDhcpServerAddress(source.getDhcpServerAddress());
200 mTcpBufferSizes = source.mTcpBufferSizes;
201 mNat64Prefix = source.mNat64Prefix;
202 mWakeOnLanSupported = source.mWakeOnLanSupported;
203 mCaptivePortalApiUrl = source.mCaptivePortalApiUrl;
204 mCaptivePortalData = source.mCaptivePortalData;
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700205 }
206
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700207 /**
208 * Sets the interface name for this link. All {@link RouteInfo} already set for this
209 * will have their interface changed to match this new value.
210 *
211 * @param iface The name of the network interface used for this link.
212 */
paulhud9736de2019-03-08 16:35:20 +0800213 public void setInterfaceName(@Nullable String iface) {
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700214 mIfaceName = iface;
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900215 ArrayList<RouteInfo> newRoutes = new ArrayList<>(mRoutes.size());
Lorenzo Colitti45b9a5b2013-03-08 11:30:39 -0800216 for (RouteInfo route : mRoutes) {
217 newRoutes.add(routeWithInterface(route));
218 }
219 mRoutes = newRoutes;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700220 }
221
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700222 /**
223 * Gets the interface name for this link. May be {@code null} if not set.
224 *
225 * @return The interface name set for this link or {@code null}.
226 */
Jeff Sharkey9da2f1e2014-08-14 12:55:00 -0700227 public @Nullable String getInterfaceName() {
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700228 return mIfaceName;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700229 }
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700230
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700231 /**
232 * @hide
233 */
Aaron Huang8b322ed2019-10-02 20:07:38 +0800234 @SystemApi
paulhud9736de2019-03-08 16:35:20 +0800235 public @NonNull List<String> getAllInterfaceNames() {
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900236 List<String> interfaceNames = new ArrayList<>(mStackedLinks.size() + 1);
237 if (mIfaceName != null) interfaceNames.add(mIfaceName);
Lorenzo Colitti4aa9bcf2013-03-20 19:22:58 +0900238 for (LinkProperties stacked: mStackedLinks.values()) {
239 interfaceNames.addAll(stacked.getAllInterfaceNames());
240 }
241 return interfaceNames;
242 }
243
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +0900244 /**
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700245 * Returns all the addresses on this link. We often think of a link having a single address,
246 * however, particularly with Ipv6 several addresses are typical. Note that the
247 * {@code LinkProperties} actually contains {@link LinkAddress} objects which also include
248 * prefix lengths for each address. This is a simplified utility alternative to
249 * {@link LinkProperties#getLinkAddresses}.
250 *
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900251 * @return An unmodifiable {@link List} of {@link InetAddress} for this link.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700252 * @hide
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +0900253 */
Aaron Huang8b322ed2019-10-02 20:07:38 +0800254 @SystemApi
paulhud9736de2019-03-08 16:35:20 +0800255 public @NonNull List<InetAddress> getAddresses() {
256 final List<InetAddress> addresses = new ArrayList<>();
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700257 for (LinkAddress linkAddress : mLinkAddresses) {
258 addresses.add(linkAddress.getAddress());
259 }
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700260 return Collections.unmodifiableList(addresses);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700261 }
262
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +0900263 /**
264 * Returns all the addresses on this link and all the links stacked above it.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700265 * @hide
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +0900266 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100267 @UnsupportedAppUsage
paulhud9736de2019-03-08 16:35:20 +0800268 public @NonNull List<InetAddress> getAllAddresses() {
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900269 List<InetAddress> addresses = new ArrayList<>();
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +0900270 for (LinkAddress linkAddress : mLinkAddresses) {
271 addresses.add(linkAddress.getAddress());
272 }
273 for (LinkProperties stacked: mStackedLinks.values()) {
274 addresses.addAll(stacked.getAllAddresses());
275 }
276 return addresses;
277 }
278
Lorenzo Colitti64483942013-11-15 18:43:52 +0900279 private int findLinkAddressIndex(LinkAddress address) {
280 for (int i = 0; i < mLinkAddresses.size(); i++) {
281 if (mLinkAddresses.get(i).isSameAddressAs(address)) {
282 return i;
283 }
Lorenzo Colitti4faa0272013-08-08 11:00:12 +0900284 }
Lorenzo Colitti64483942013-11-15 18:43:52 +0900285 return -1;
Lorenzo Colitti4faa0272013-08-08 11:00:12 +0900286 }
287
288 /**
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700289 * Adds a {@link LinkAddress} to this {@code LinkProperties} if a {@link LinkAddress} of the
290 * same address/prefix does not already exist. If it does exist it is replaced.
Lorenzo Colitti64483942013-11-15 18:43:52 +0900291 * @param address The {@code LinkAddress} to add.
292 * @return true if {@code address} was added or updated, false otherwise.
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700293 * @hide
Lorenzo Colitti64483942013-11-15 18:43:52 +0900294 */
Remi NGUYEN VAN5c5f1ba2019-01-29 12:08:43 +0900295 @SystemApi
296 @TestApi
paulhud9736de2019-03-08 16:35:20 +0800297 public boolean addLinkAddress(@NonNull LinkAddress address) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900298 if (address == null) {
299 return false;
300 }
301 int i = findLinkAddressIndex(address);
302 if (i < 0) {
303 // Address was not present. Add it.
304 mLinkAddresses.add(address);
305 return true;
306 } else if (mLinkAddresses.get(i).equals(address)) {
307 // Address was present and has same properties. Do nothing.
308 return false;
309 } else {
310 // Address was present and has different properties. Update it.
311 mLinkAddresses.set(i, address);
312 return true;
313 }
314 }
315
316 /**
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700317 * Removes a {@link LinkAddress} from this {@code LinkProperties}. Specifically, matches
318 * and {@link LinkAddress} with the same address and prefix.
319 *
320 * @param toRemove A {@link LinkAddress} specifying the address to remove.
Lorenzo Colitti4faa0272013-08-08 11:00:12 +0900321 * @return true if the address was removed, false if it did not exist.
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700322 * @hide
Lorenzo Colitti4faa0272013-08-08 11:00:12 +0900323 */
Remi NGUYEN VAN5c5f1ba2019-01-29 12:08:43 +0900324 @SystemApi
325 @TestApi
paulhud9736de2019-03-08 16:35:20 +0800326 public boolean removeLinkAddress(@NonNull LinkAddress toRemove) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900327 int i = findLinkAddressIndex(toRemove);
328 if (i >= 0) {
329 mLinkAddresses.remove(i);
330 return true;
331 }
332 return false;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700333 }
334
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +0900335 /**
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700336 * Returns all the {@link LinkAddress} on this link. Typically a link will have
337 * one IPv4 address and one or more IPv6 addresses.
338 *
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700339 * @return An unmodifiable {@link List} of {@link LinkAddress} for this link.
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +0900340 */
paulhud9736de2019-03-08 16:35:20 +0800341 public @NonNull List<LinkAddress> getLinkAddresses() {
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700342 return Collections.unmodifiableList(mLinkAddresses);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700343 }
344
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +0900345 /**
346 * Returns all the addresses on this link and all the links stacked above it.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700347 * @hide
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +0900348 */
Aaron Huang8b322ed2019-10-02 20:07:38 +0800349 @SystemApi
350 public @NonNull List<LinkAddress> getAllLinkAddresses() {
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900351 List<LinkAddress> addresses = new ArrayList<>(mLinkAddresses);
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +0900352 for (LinkProperties stacked: mStackedLinks.values()) {
353 addresses.addAll(stacked.getAllLinkAddresses());
354 }
355 return addresses;
356 }
357
Lorenzo Colitti22f407b2013-08-23 20:54:49 +0900358 /**
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700359 * Replaces the {@link LinkAddress} in this {@code LinkProperties} with
360 * the given {@link Collection} of {@link LinkAddress}.
361 *
362 * @param addresses The {@link Collection} of {@link LinkAddress} to set in this
363 * object.
Lorenzo Colitti22f407b2013-08-23 20:54:49 +0900364 */
paulhud9736de2019-03-08 16:35:20 +0800365 public void setLinkAddresses(@NonNull Collection<LinkAddress> addresses) {
Lorenzo Colitti22f407b2013-08-23 20:54:49 +0900366 mLinkAddresses.clear();
367 for (LinkAddress address: addresses) {
368 addLinkAddress(address);
369 }
370 }
371
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700372 /**
Lorenzo Colitti309a75d2014-06-24 00:34:39 +0900373 * Adds the given {@link InetAddress} to the list of DNS servers, if not present.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700374 *
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700375 * @param dnsServer The {@link InetAddress} to add to the list of DNS servers.
Lorenzo Colitti309a75d2014-06-24 00:34:39 +0900376 * @return true if the DNS server was added, false if it was already present.
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700377 * @hide
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700378 */
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +0900379 @TestApi
380 @SystemApi
paulhud9736de2019-03-08 16:35:20 +0800381 public boolean addDnsServer(@NonNull InetAddress dnsServer) {
Lorenzo Colitti309a75d2014-06-24 00:34:39 +0900382 if (dnsServer != null && !mDnses.contains(dnsServer)) {
383 mDnses.add(dnsServer);
384 return true;
385 }
386 return false;
387 }
388
389 /**
Erik Klinecd7ed162015-05-21 16:15:02 +0900390 * Removes the given {@link InetAddress} from the list of DNS servers.
391 *
392 * @param dnsServer The {@link InetAddress} to remove from the list of DNS servers.
393 * @return true if the DNS server was removed, false if it did not exist.
394 * @hide
395 */
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +0900396 @TestApi
397 @SystemApi
paulhud9736de2019-03-08 16:35:20 +0800398 public boolean removeDnsServer(@NonNull InetAddress dnsServer) {
paulhu3d67f532019-03-22 16:35:06 +0800399 return mDnses.remove(dnsServer);
Erik Klinecd7ed162015-05-21 16:15:02 +0900400 }
401
402 /**
Lorenzo Colitti309a75d2014-06-24 00:34:39 +0900403 * Replaces the DNS servers in this {@code LinkProperties} with
404 * the given {@link Collection} of {@link InetAddress} objects.
405 *
Chalard Jean03dbf6b2018-04-11 16:36:41 +0900406 * @param dnsServers The {@link Collection} of DNS servers to set in this object.
Lorenzo Colitti309a75d2014-06-24 00:34:39 +0900407 */
paulhud9736de2019-03-08 16:35:20 +0800408 public void setDnsServers(@NonNull Collection<InetAddress> dnsServers) {
Lorenzo Colitti309a75d2014-06-24 00:34:39 +0900409 mDnses.clear();
410 for (InetAddress dnsServer: dnsServers) {
411 addDnsServer(dnsServer);
412 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700413 }
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700414
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700415 /**
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -0700416 * Returns all the {@link InetAddress} for DNS servers on this link.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700417 *
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900418 * @return An unmodifiable {@link List} of {@link InetAddress} for DNS servers on
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700419 * this link.
420 */
paulhud9736de2019-03-08 16:35:20 +0800421 public @NonNull List<InetAddress> getDnsServers() {
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700422 return Collections.unmodifiableList(mDnses);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700423 }
424
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700425 /**
dalykd9201342018-01-17 14:20:55 -0500426 * Set whether private DNS is currently in use on this network.
427 *
428 * @param usePrivateDns The private DNS state.
429 * @hide
430 */
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +0900431 @TestApi
432 @SystemApi
dalykd9201342018-01-17 14:20:55 -0500433 public void setUsePrivateDns(boolean usePrivateDns) {
434 mUsePrivateDns = usePrivateDns;
435 }
436
437 /**
438 * Returns whether private DNS is currently in use on this network. When
439 * private DNS is in use, applications must not send unencrypted DNS
440 * queries as doing so could reveal private user information. Furthermore,
441 * if private DNS is in use and {@link #getPrivateDnsServerName} is not
442 * {@code null}, DNS queries must be sent to the specified DNS server.
443 *
444 * @return {@code true} if private DNS is in use, {@code false} otherwise.
445 */
446 public boolean isPrivateDnsActive() {
447 return mUsePrivateDns;
448 }
449
450 /**
451 * Set the name of the private DNS server to which private DNS queries
452 * should be sent when in strict mode. This value should be {@code null}
453 * when private DNS is off or in opportunistic mode.
454 *
455 * @param privateDnsServerName The private DNS server name.
456 * @hide
457 */
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +0900458 @TestApi
459 @SystemApi
dalykd9201342018-01-17 14:20:55 -0500460 public void setPrivateDnsServerName(@Nullable String privateDnsServerName) {
461 mPrivateDnsServerName = privateDnsServerName;
462 }
463
464 /**
ruibin zhangad2689c2019-05-23 19:35:30 +0800465 * Set DHCP server address.
466 *
467 * @param serverAddress the server address to set.
468 */
469 public void setDhcpServerAddress(@Nullable Inet4Address serverAddress) {
470 mDhcpServerAddress = serverAddress;
471 }
472
473 /**
474 * Get DHCP server address
475 *
476 * @return The current DHCP server address.
477 */
478 public @Nullable Inet4Address getDhcpServerAddress() {
479 return mDhcpServerAddress;
480 }
481
482 /**
dalykd9201342018-01-17 14:20:55 -0500483 * Returns the private DNS server name that is in use. If not {@code null},
484 * private DNS is in strict mode. In this mode, applications should ensure
485 * that all DNS queries are encrypted and sent to this hostname and that
486 * queries are only sent if the hostname's certificate is valid. If
487 * {@code null} and {@link #isPrivateDnsActive} is {@code true}, private
488 * DNS is in opportunistic mode, and applications should ensure that DNS
489 * queries are encrypted and sent to a DNS server returned by
490 * {@link #getDnsServers}. System DNS will handle each of these cases
491 * correctly, but applications implementing their own DNS lookups must make
492 * sure to follow these requirements.
493 *
494 * @return The private DNS server name.
495 */
496 public @Nullable String getPrivateDnsServerName() {
497 return mPrivateDnsServerName;
498 }
499
500 /**
Chalard Jean03dbf6b2018-04-11 16:36:41 +0900501 * Adds the given {@link InetAddress} to the list of validated private DNS servers,
502 * if not present. This is distinct from the server name in that these are actually
503 * resolved addresses.
504 *
505 * @param dnsServer The {@link InetAddress} to add to the list of validated private DNS servers.
506 * @return true if the DNS server was added, false if it was already present.
507 * @hide
508 */
paulhud9736de2019-03-08 16:35:20 +0800509 public boolean addValidatedPrivateDnsServer(@NonNull InetAddress dnsServer) {
Chalard Jean03dbf6b2018-04-11 16:36:41 +0900510 if (dnsServer != null && !mValidatedPrivateDnses.contains(dnsServer)) {
511 mValidatedPrivateDnses.add(dnsServer);
512 return true;
513 }
514 return false;
515 }
516
517 /**
518 * Removes the given {@link InetAddress} from the list of validated private DNS servers.
519 *
520 * @param dnsServer The {@link InetAddress} to remove from the list of validated private DNS
521 * servers.
522 * @return true if the DNS server was removed, false if it did not exist.
523 * @hide
524 */
paulhud9736de2019-03-08 16:35:20 +0800525 public boolean removeValidatedPrivateDnsServer(@NonNull InetAddress dnsServer) {
526 return mValidatedPrivateDnses.remove(dnsServer);
Chalard Jean03dbf6b2018-04-11 16:36:41 +0900527 }
528
529 /**
530 * Replaces the validated private DNS servers in this {@code LinkProperties} with
531 * the given {@link Collection} of {@link InetAddress} objects.
532 *
533 * @param dnsServers The {@link Collection} of validated private DNS servers to set in this
534 * object.
535 * @hide
536 */
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +0900537 @TestApi
538 @SystemApi
paulhud9736de2019-03-08 16:35:20 +0800539 public void setValidatedPrivateDnsServers(@NonNull Collection<InetAddress> dnsServers) {
Chalard Jean03dbf6b2018-04-11 16:36:41 +0900540 mValidatedPrivateDnses.clear();
541 for (InetAddress dnsServer: dnsServers) {
542 addValidatedPrivateDnsServer(dnsServer);
543 }
544 }
545
546 /**
547 * Returns all the {@link InetAddress} for validated private DNS servers on this link.
548 * These are resolved from the private DNS server name.
549 *
paulhud9736de2019-03-08 16:35:20 +0800550 * @return An unmodifiable {@link List} of {@link InetAddress} for validated private
Chalard Jean03dbf6b2018-04-11 16:36:41 +0900551 * DNS servers on this link.
552 * @hide
553 */
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +0900554 @TestApi
555 @SystemApi
paulhud9736de2019-03-08 16:35:20 +0800556 public @NonNull List<InetAddress> getValidatedPrivateDnsServers() {
Chalard Jean03dbf6b2018-04-11 16:36:41 +0900557 return Collections.unmodifiableList(mValidatedPrivateDnses);
558 }
559
560 /**
Hongshikfa425012018-06-28 20:42:19 +0900561 * Adds the given {@link InetAddress} to the list of PCSCF servers, if not present.
562 *
563 * @param pcscfServer The {@link InetAddress} to add to the list of PCSCF servers.
564 * @return true if the PCSCF server was added, false otherwise.
565 * @hide
566 */
Aaron Huang8b322ed2019-10-02 20:07:38 +0800567 @SystemApi
paulhud9736de2019-03-08 16:35:20 +0800568 public boolean addPcscfServer(@NonNull InetAddress pcscfServer) {
Hongshikfa425012018-06-28 20:42:19 +0900569 if (pcscfServer != null && !mPcscfs.contains(pcscfServer)) {
570 mPcscfs.add(pcscfServer);
571 return true;
572 }
573 return false;
574 }
575
576 /**
577 * Removes the given {@link InetAddress} from the list of PCSCF servers.
578 *
paulhud9736de2019-03-08 16:35:20 +0800579 * @param pcscfServer The {@link InetAddress} to remove from the list of PCSCF servers.
Hongshikfa425012018-06-28 20:42:19 +0900580 * @return true if the PCSCF server was removed, false otherwise.
581 * @hide
582 */
paulhud9736de2019-03-08 16:35:20 +0800583 public boolean removePcscfServer(@NonNull InetAddress pcscfServer) {
584 return mPcscfs.remove(pcscfServer);
Hongshikfa425012018-06-28 20:42:19 +0900585 }
586
587 /**
588 * Replaces the PCSCF servers in this {@code LinkProperties} with
589 * the given {@link Collection} of {@link InetAddress} objects.
590 *
paulhud9736de2019-03-08 16:35:20 +0800591 * @param pcscfServers The {@link Collection} of PCSCF servers to set in this object.
Hongshikfa425012018-06-28 20:42:19 +0900592 * @hide
593 */
Remi NGUYEN VAN299a7cc2019-01-23 21:35:52 +0900594 @SystemApi
595 @TestApi
paulhud9736de2019-03-08 16:35:20 +0800596 public void setPcscfServers(@NonNull Collection<InetAddress> pcscfServers) {
Hongshikfa425012018-06-28 20:42:19 +0900597 mPcscfs.clear();
598 for (InetAddress pcscfServer: pcscfServers) {
599 addPcscfServer(pcscfServer);
600 }
601 }
602
603 /**
604 * Returns all the {@link InetAddress} for PCSCF servers on this link.
605 *
606 * @return An unmodifiable {@link List} of {@link InetAddress} for PCSCF servers on
607 * this link.
608 * @hide
609 */
Remi NGUYEN VAN299a7cc2019-01-23 21:35:52 +0900610 @SystemApi
611 @TestApi
paulhud9736de2019-03-08 16:35:20 +0800612 public @NonNull List<InetAddress> getPcscfServers() {
Hongshikfa425012018-06-28 20:42:19 +0900613 return Collections.unmodifiableList(mPcscfs);
614 }
615
616 /**
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700617 * Sets the DNS domain search path used on this link.
618 *
619 * @param domains A {@link String} listing in priority order the comma separated
620 * domains to search when resolving host names on this link.
621 */
paulhud9736de2019-03-08 16:35:20 +0800622 public void setDomains(@Nullable String domains) {
Robert Greenwalt8058f622012-11-09 10:52:27 -0800623 mDomains = domains;
624 }
625
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700626 /**
paulhud9736de2019-03-08 16:35:20 +0800627 * Get the DNS domains search path set for this link. May be {@code null} if not set.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700628 *
paulhud9736de2019-03-08 16:35:20 +0800629 * @return A {@link String} containing the comma separated domains to search when resolving host
630 * names on this link or {@code null}.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700631 */
paulhud9736de2019-03-08 16:35:20 +0800632 public @Nullable String getDomains() {
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700633 return mDomains;
634 }
635
636 /**
637 * Sets the Maximum Transmission Unit size to use on this link. This should not be used
638 * unless the system default (1500) is incorrect. Values less than 68 or greater than
639 * 10000 will be ignored.
640 *
641 * @param mtu The MTU to use for this link.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700642 */
sy.yun9d9b74a2013-09-02 05:24:09 +0900643 public void setMtu(int mtu) {
644 mMtu = mtu;
645 }
646
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700647 /**
648 * Gets any non-default MTU size set for this link. Note that if the default is being used
649 * this will return 0.
650 *
651 * @return The mtu value set for this link.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700652 */
sy.yun9d9b74a2013-09-02 05:24:09 +0900653 public int getMtu() {
654 return mMtu;
655 }
656
Robert Greenwalt3f05bf42014-08-06 12:00:25 -0700657 /**
658 * Sets the tcp buffers sizes to be used when this link is the system default.
659 * Should be of the form "rmem_min,rmem_def,rmem_max,wmem_min,wmem_def,wmem_max".
660 *
661 * @param tcpBufferSizes The tcp buffers sizes to use.
662 *
663 * @hide
664 */
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +0900665 @TestApi
666 @SystemApi
paulhud9736de2019-03-08 16:35:20 +0800667 public void setTcpBufferSizes(@Nullable String tcpBufferSizes) {
Robert Greenwalt3f05bf42014-08-06 12:00:25 -0700668 mTcpBufferSizes = tcpBufferSizes;
669 }
670
671 /**
paulhud9736de2019-03-08 16:35:20 +0800672 * Gets the tcp buffer sizes. May be {@code null} if not set.
Robert Greenwalt3f05bf42014-08-06 12:00:25 -0700673 *
paulhud9736de2019-03-08 16:35:20 +0800674 * @return the tcp buffer sizes to use when this link is the system default or {@code null}.
Robert Greenwalt3f05bf42014-08-06 12:00:25 -0700675 *
676 * @hide
677 */
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +0900678 @TestApi
679 @SystemApi
paulhud9736de2019-03-08 16:35:20 +0800680 public @Nullable String getTcpBufferSizes() {
Robert Greenwalt3f05bf42014-08-06 12:00:25 -0700681 return mTcpBufferSizes;
682 }
683
Lorenzo Colitti45b9a5b2013-03-08 11:30:39 -0800684 private RouteInfo routeWithInterface(RouteInfo route) {
685 return new RouteInfo(
686 route.getDestination(),
687 route.getGateway(),
Lorenzo Colitti4b0f8e62014-09-19 01:49:05 +0900688 mIfaceName,
Lorenzo Colittid86407b2020-03-18 07:52:25 +0000689 route.getType(),
690 route.getMtu());
691 }
692
Treehugger Robot5dfba4b2020-03-24 06:53:37 +0000693 private int findRouteIndexByRouteKey(RouteInfo route) {
Lorenzo Colittid86407b2020-03-18 07:52:25 +0000694 for (int i = 0; i < mRoutes.size(); i++) {
Treehugger Robot5dfba4b2020-03-24 06:53:37 +0000695 if (mRoutes.get(i).getRouteKey().equals(route.getRouteKey())) {
Lorenzo Colittid86407b2020-03-18 07:52:25 +0000696 return i;
697 }
698 }
699 return -1;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700700 }
Lorenzo Colitti45b9a5b2013-03-08 11:30:39 -0800701
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700702 /**
Lorenzo Colittid86407b2020-03-18 07:52:25 +0000703 * Adds a {@link RouteInfo} to this {@code LinkProperties}, if a {@link RouteInfo}
Treehugger Robot5dfba4b2020-03-24 06:53:37 +0000704 * with the same {@link RouteInfo.RouteKey} with different properties
705 * (e.g., different MTU), it will be updated. If the {@link RouteInfo} had an
706 * interface name set and that differs from the interface set for this
707 * {@code LinkProperties} an {@link IllegalArgumentException} will be thrown.
708 * The proper course is to add either un-named or properly named {@link RouteInfo}.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700709 *
710 * @param route A {@link RouteInfo} to add to this object.
Lorenzo Colittid86407b2020-03-18 07:52:25 +0000711 * @return {@code true} was added or updated, false otherwise.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700712 */
paulhud9736de2019-03-08 16:35:20 +0800713 public boolean addRoute(@NonNull RouteInfo route) {
714 String routeIface = route.getInterface();
715 if (routeIface != null && !routeIface.equals(mIfaceName)) {
716 throw new IllegalArgumentException(
717 "Route added with non-matching interface: " + routeIface
718 + " vs. " + mIfaceName);
719 }
720 route = routeWithInterface(route);
Lorenzo Colittid86407b2020-03-18 07:52:25 +0000721
Treehugger Robot5dfba4b2020-03-24 06:53:37 +0000722 int i = findRouteIndexByRouteKey(route);
Lorenzo Colittid86407b2020-03-18 07:52:25 +0000723 if (i == -1) {
724 // Route was not present. Add it.
paulhud9736de2019-03-08 16:35:20 +0800725 mRoutes.add(route);
726 return true;
Lorenzo Colittid86407b2020-03-18 07:52:25 +0000727 } else if (mRoutes.get(i).equals(route)) {
728 // Route was present and has same properties. Do nothing.
729 return false;
730 } else {
731 // Route was present and has different properties. Update it.
732 mRoutes.set(i, route);
733 return true;
Lorenzo Colitti45b9a5b2013-03-08 11:30:39 -0800734 }
Lorenzo Colittic17a1b92014-06-12 23:10:17 +0900735 }
736
737 /**
738 * Removes a {@link RouteInfo} from this {@code LinkProperties}, if present. The route must
739 * specify an interface and the interface must match the interface of this
740 * {@code LinkProperties}, or it will not be removed.
741 *
Lorenzo Colittid86407b2020-03-18 07:52:25 +0000742 * @param route A {@link RouteInfo} specifying the route to remove.
Lorenzo Colittic17a1b92014-06-12 23:10:17 +0900743 * @return {@code true} if the route was removed, {@code false} if it was not present.
744 *
745 * @hide
746 */
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +0900747 @TestApi
748 @SystemApi
paulhud9736de2019-03-08 16:35:20 +0800749 public boolean removeRoute(@NonNull RouteInfo route) {
750 return Objects.equals(mIfaceName, route.getInterface()) && mRoutes.remove(route);
Lorenzo Colitti45b9a5b2013-03-08 11:30:39 -0800751 }
752
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800753 /**
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700754 * Returns all the {@link RouteInfo} set on this link.
755 *
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700756 * @return An unmodifiable {@link List} of {@link RouteInfo} for this link.
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800757 */
paulhud9736de2019-03-08 16:35:20 +0800758 public @NonNull List<RouteInfo> getRoutes() {
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700759 return Collections.unmodifiableList(mRoutes);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700760 }
761
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800762 /**
Rubin Xu1bb5c082017-09-05 18:40:49 +0100763 * Make sure this LinkProperties instance contains routes that cover the local subnet
764 * of its link addresses. Add any route that is missing.
765 * @hide
766 */
767 public void ensureDirectlyConnectedRoutes() {
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900768 for (LinkAddress addr : mLinkAddresses) {
Rubin Xu1bb5c082017-09-05 18:40:49 +0100769 addRoute(new RouteInfo(addr, null, mIfaceName));
770 }
771 }
772
773 /**
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800774 * Returns all the routes on this link and all the links stacked above it.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700775 * @hide
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800776 */
Aaron Huang8b322ed2019-10-02 20:07:38 +0800777 @SystemApi
paulhud9736de2019-03-08 16:35:20 +0800778 public @NonNull List<RouteInfo> getAllRoutes() {
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900779 List<RouteInfo> routes = new ArrayList<>(mRoutes);
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800780 for (LinkProperties stacked: mStackedLinks.values()) {
781 routes.addAll(stacked.getAllRoutes());
782 }
Robert Greenwalt6629bcd2013-03-15 11:28:50 -0700783 return routes;
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800784 }
785
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700786 /**
787 * Sets the recommended {@link ProxyInfo} to use on this link, or {@code null} for none.
788 * Note that Http Proxies are only a hint - the system recommends their use, but it does
789 * not enforce it and applications may ignore them.
790 *
Erik Klineb36a3132015-06-26 19:21:34 +0900791 * @param proxy A {@link ProxyInfo} defining the HTTP Proxy to use on this link.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700792 */
paulhud9736de2019-03-08 16:35:20 +0800793 public void setHttpProxy(@Nullable ProxyInfo proxy) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700794 mHttpProxy = proxy;
795 }
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700796
797 /**
798 * Gets the recommended {@link ProxyInfo} (or {@code null}) set on this link.
799 *
paulhud9736de2019-03-08 16:35:20 +0800800 * @return The {@link ProxyInfo} set on this link or {@code null}.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700801 */
paulhud9736de2019-03-08 16:35:20 +0800802 public @Nullable ProxyInfo getHttpProxy() {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700803 return mHttpProxy;
804 }
805
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800806 /**
Lorenzo Colitti59b34472019-01-08 09:58:59 +0900807 * Returns the NAT64 prefix in use on this link, if any.
808 *
paulhud9736de2019-03-08 16:35:20 +0800809 * @return the NAT64 prefix or {@code null}.
Lorenzo Colitti59b34472019-01-08 09:58:59 +0900810 */
811 public @Nullable IpPrefix getNat64Prefix() {
812 return mNat64Prefix;
813 }
814
815 /**
816 * Sets the NAT64 prefix in use on this link.
817 *
818 * Currently, only 96-bit prefixes (i.e., where the 32-bit IPv4 address is at the end of the
paulhud9736de2019-03-08 16:35:20 +0800819 * 128-bit IPv6 address) are supported or {@code null} for no prefix.
Lorenzo Colitti59b34472019-01-08 09:58:59 +0900820 *
821 * @param prefix the NAT64 prefix.
Lorenzo Colitti59b34472019-01-08 09:58:59 +0900822 */
paulhud9736de2019-03-08 16:35:20 +0800823 public void setNat64Prefix(@Nullable IpPrefix prefix) {
Lorenzo Colitti59b34472019-01-08 09:58:59 +0900824 if (prefix != null && prefix.getPrefixLength() != 96) {
825 throw new IllegalArgumentException("Only 96-bit prefixes are supported: " + prefix);
826 }
827 mNat64Prefix = prefix; // IpPrefix objects are immutable.
828 }
829
830 /**
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800831 * Adds a stacked link.
832 *
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900833 * If there is already a stacked link with the same interface name as link,
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800834 * that link is replaced with link. Otherwise, link is added to the list
paulhud9736de2019-03-08 16:35:20 +0800835 * of stacked links.
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800836 *
837 * @param link The link to add.
Lorenzo Colitti4faa0272013-08-08 11:00:12 +0900838 * @return true if the link was stacked, false otherwise.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700839 * @hide
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800840 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100841 @UnsupportedAppUsage
paulhud9736de2019-03-08 16:35:20 +0800842 public boolean addStackedLink(@NonNull LinkProperties link) {
843 if (link.getInterfaceName() != null) {
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800844 mStackedLinks.put(link.getInterfaceName(), link);
Lorenzo Colitti4faa0272013-08-08 11:00:12 +0900845 return true;
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800846 }
Lorenzo Colitti4faa0272013-08-08 11:00:12 +0900847 return false;
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800848 }
849
850 /**
851 * Removes a stacked link.
852 *
Lorenzo Colittif3cab632014-10-20 11:08:03 +0900853 * If there is a stacked link with the given interface name, it is
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800854 * removed. Otherwise, nothing changes.
855 *
Lorenzo Colittif3cab632014-10-20 11:08:03 +0900856 * @param iface The interface name of the link to remove.
Lorenzo Colitti4faa0272013-08-08 11:00:12 +0900857 * @return true if the link was removed, false otherwise.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700858 * @hide
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800859 */
paulhud9736de2019-03-08 16:35:20 +0800860 public boolean removeStackedLink(@NonNull String iface) {
861 LinkProperties removed = mStackedLinks.remove(iface);
862 return removed != null;
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800863 }
864
865 /**
866 * Returns all the links stacked on top of this link.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700867 * @hide
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800868 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100869 @UnsupportedAppUsage
Jeff Sharkeyeb2c2c72014-08-11 15:22:51 -0700870 public @NonNull List<LinkProperties> getStackedLinks() {
871 if (mStackedLinks.isEmpty()) {
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900872 return Collections.emptyList();
Jeff Sharkeyeb2c2c72014-08-11 15:22:51 -0700873 }
paulhud9736de2019-03-08 16:35:20 +0800874 final List<LinkProperties> stacked = new ArrayList<>();
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800875 for (LinkProperties link : mStackedLinks.values()) {
Jeff Sharkeyeb2c2c72014-08-11 15:22:51 -0700876 stacked.add(new LinkProperties(link));
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800877 }
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700878 return Collections.unmodifiableList(stacked);
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800879 }
880
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700881 /**
882 * Clears this object to its initial state.
883 */
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700884 public void clear() {
Remi NGUYEN VANead1ef42019-12-17 16:45:42 +0900885 if (mParcelSensitiveFields) {
886 throw new UnsupportedOperationException(
887 "Cannot clear LinkProperties when parcelSensitiveFields is set");
888 }
889
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700890 mIfaceName = null;
Wink Savillee8222252011-07-13 13:44:13 -0700891 mLinkAddresses.clear();
892 mDnses.clear();
dalykd9201342018-01-17 14:20:55 -0500893 mUsePrivateDns = false;
894 mPrivateDnsServerName = null;
Hongshikfa425012018-06-28 20:42:19 +0900895 mPcscfs.clear();
Robert Greenwalt8058f622012-11-09 10:52:27 -0800896 mDomains = null;
Wink Savillee8222252011-07-13 13:44:13 -0700897 mRoutes.clear();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700898 mHttpProxy = null;
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800899 mStackedLinks.clear();
sy.yun9d9b74a2013-09-02 05:24:09 +0900900 mMtu = 0;
ruibin zhangad2689c2019-05-23 19:35:30 +0800901 mDhcpServerAddress = null;
Robert Greenwalt3f05bf42014-08-06 12:00:25 -0700902 mTcpBufferSizes = null;
Lorenzo Colitti59b34472019-01-08 09:58:59 +0900903 mNat64Prefix = null;
Valentin Iftimec86ebba2019-09-24 13:32:13 +0200904 mWakeOnLanSupported = false;
Remi NGUYEN VANead1ef42019-12-17 16:45:42 +0900905 mCaptivePortalApiUrl = null;
906 mCaptivePortalData = null;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700907 }
908
909 /**
910 * Implement the Parcelable interface
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700911 */
912 public int describeContents() {
913 return 0;
914 }
915
Wink Saville1f6408a2010-08-27 11:15:18 -0700916 @Override
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700917 public String toString() {
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900918 // Space as a separator, so no need for spaces at start/end of the individual fragments.
919 final StringJoiner resultJoiner = new StringJoiner(" ", "{", "}");
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700920
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900921 if (mIfaceName != null) {
922 resultJoiner.add("InterfaceName:");
923 resultJoiner.add(mIfaceName);
924 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700925
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900926 resultJoiner.add("LinkAddresses: [");
927 if (!mLinkAddresses.isEmpty()) {
928 resultJoiner.add(TextUtils.join(",", mLinkAddresses));
929 }
930 resultJoiner.add("]");
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700931
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900932 resultJoiner.add("DnsAddresses: [");
933 if (!mDnses.isEmpty()) {
934 resultJoiner.add(TextUtils.join(",", mDnses));
935 }
936 resultJoiner.add("]");
dalykd9201342018-01-17 14:20:55 -0500937
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900938 if (mUsePrivateDns) {
939 resultJoiner.add("UsePrivateDns: true");
940 }
941
Chalard Jeanfaaf2fe2018-06-07 13:28:09 +0900942 if (mPrivateDnsServerName != null) {
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900943 resultJoiner.add("PrivateDnsServerName:");
944 resultJoiner.add(mPrivateDnsServerName);
dalykd9201342018-01-17 14:20:55 -0500945 }
946
Hongshikfa425012018-06-28 20:42:19 +0900947 if (!mPcscfs.isEmpty()) {
948 resultJoiner.add("PcscfAddresses: [");
949 resultJoiner.add(TextUtils.join(",", mPcscfs));
950 resultJoiner.add("]");
951 }
952
Chalard Jean03dbf6b2018-04-11 16:36:41 +0900953 if (!mValidatedPrivateDnses.isEmpty()) {
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900954 final StringJoiner validatedPrivateDnsesJoiner =
955 new StringJoiner(",", "ValidatedPrivateDnsAddresses: [", "]");
956 for (final InetAddress addr : mValidatedPrivateDnses) {
957 validatedPrivateDnsesJoiner.add(addr.getHostAddress());
Chalard Jean03dbf6b2018-04-11 16:36:41 +0900958 }
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900959 resultJoiner.add(validatedPrivateDnsesJoiner.toString());
Chalard Jean03dbf6b2018-04-11 16:36:41 +0900960 }
961
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900962 resultJoiner.add("Domains:");
963 resultJoiner.add(mDomains);
Robert Greenwalt8058f622012-11-09 10:52:27 -0800964
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900965 resultJoiner.add("MTU:");
966 resultJoiner.add(Integer.toString(mMtu));
sy.yun9d9b74a2013-09-02 05:24:09 +0900967
Valentin Iftimec86ebba2019-09-24 13:32:13 +0200968 if (mWakeOnLanSupported) {
969 resultJoiner.add("WakeOnLanSupported: true");
970 }
971
ruibin zhangad2689c2019-05-23 19:35:30 +0800972 if (mDhcpServerAddress != null) {
973 resultJoiner.add("ServerAddress:");
974 resultJoiner.add(mDhcpServerAddress.toString());
975 }
976
Remi NGUYEN VANead1ef42019-12-17 16:45:42 +0900977 if (mCaptivePortalApiUrl != null) {
978 resultJoiner.add("CaptivePortalApiUrl: " + mCaptivePortalApiUrl);
979 }
980
981 if (mCaptivePortalData != null) {
982 resultJoiner.add("CaptivePortalData: " + mCaptivePortalData);
983 }
984
Robert Greenwalt3f05bf42014-08-06 12:00:25 -0700985 if (mTcpBufferSizes != null) {
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900986 resultJoiner.add("TcpBufferSizes:");
987 resultJoiner.add(mTcpBufferSizes);
Robert Greenwalt3f05bf42014-08-06 12:00:25 -0700988 }
989
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900990 resultJoiner.add("Routes: [");
991 if (!mRoutes.isEmpty()) {
992 resultJoiner.add(TextUtils.join(",", mRoutes));
993 }
994 resultJoiner.add("]");
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700995
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900996 if (mHttpProxy != null) {
997 resultJoiner.add("HttpProxy:");
998 resultJoiner.add(mHttpProxy.toString());
999 }
1000
Lorenzo Colitti59b34472019-01-08 09:58:59 +09001001 if (mNat64Prefix != null) {
1002 resultJoiner.add("Nat64Prefix:");
1003 resultJoiner.add(mNat64Prefix.toString());
1004 }
1005
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +09001006 final Collection<LinkProperties> stackedLinksValues = mStackedLinks.values();
1007 if (!stackedLinksValues.isEmpty()) {
1008 final StringJoiner stackedLinksJoiner = new StringJoiner(",", "Stacked: [", "]");
1009 for (final LinkProperties lp : stackedLinksValues) {
1010 stackedLinksJoiner.add("[ " + lp + " ]");
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -08001011 }
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +09001012 resultJoiner.add(stackedLinksJoiner.toString());
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -08001013 }
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +09001014
1015 return resultJoiner.toString();
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -08001016 }
1017
1018 /**
1019 * Returns true if this link has an IPv4 address.
1020 *
1021 * @return {@code true} if there is an IPv4 address, {@code false} otherwise.
Robert Greenwaltdf2b8782014-06-06 10:30:11 -07001022 * @hide
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -08001023 */
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +09001024 @TestApi
1025 @SystemApi
paulhud9736de2019-03-08 16:35:20 +08001026 public boolean hasIpv4Address() {
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -08001027 for (LinkAddress address : mLinkAddresses) {
Hugo Benichibd87a392017-10-10 16:29:06 +09001028 if (address.getAddress() instanceof Inet4Address) {
1029 return true;
1030 }
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -08001031 }
1032 return false;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001033 }
1034
Wink Savillee8222252011-07-13 13:44:13 -07001035 /**
paulhud9736de2019-03-08 16:35:20 +08001036 * For backward compatibility.
1037 * This was annotated with @UnsupportedAppUsage in P, so we can't remove the method completely
1038 * just yet.
1039 * @return {@code true} if there is an IPv4 address, {@code false} otherwise.
1040 * @hide
1041 */
1042 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
1043 public boolean hasIPv4Address() {
1044 return hasIpv4Address();
1045 }
1046
1047 /**
Lorenzo Colitti87cfc702015-07-27 16:35:33 +09001048 * Returns true if this link or any of its stacked interfaces has an IPv4 address.
1049 *
1050 * @return {@code true} if there is an IPv4 address, {@code false} otherwise.
1051 */
paulhud9736de2019-03-08 16:35:20 +08001052 private boolean hasIpv4AddressOnInterface(String iface) {
Lorenzo Colitti89b63922015-07-30 23:41:43 +09001053 // mIfaceName can be null.
paulhud9736de2019-03-08 16:35:20 +08001054 return (Objects.equals(iface, mIfaceName) && hasIpv4Address())
1055 || (iface != null && mStackedLinks.containsKey(iface)
1056 && mStackedLinks.get(iface).hasIpv4Address());
Lorenzo Colitti87cfc702015-07-27 16:35:33 +09001057 }
1058
1059 /**
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +09001060 * Returns true if this link has a global preferred IPv6 address.
Lorenzo Colitti4faa0272013-08-08 11:00:12 +09001061 *
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +09001062 * @return {@code true} if there is a global preferred IPv6 address, {@code false} otherwise.
Robert Greenwaltdf2b8782014-06-06 10:30:11 -07001063 * @hide
Lorenzo Colitti4faa0272013-08-08 11:00:12 +09001064 */
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +09001065 @TestApi
1066 @SystemApi
paulhud9736de2019-03-08 16:35:20 +08001067 public boolean hasGlobalIpv6Address() {
Lorenzo Colitti4faa0272013-08-08 11:00:12 +09001068 for (LinkAddress address : mLinkAddresses) {
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +09001069 if (address.getAddress() instanceof Inet6Address && address.isGlobalPreferred()) {
Lorenzo Colitti4faa0272013-08-08 11:00:12 +09001070 return true;
1071 }
1072 }
1073 return false;
1074 }
1075
1076 /**
Lorenzo Colitti5ccee0c2020-04-02 04:50:41 +00001077 * Returns true if this link has an IPv4 unreachable default route.
1078 *
1079 * @return {@code true} if there is an IPv4 unreachable default route, {@code false} otherwise.
1080 * @hide
1081 */
1082 public boolean hasIpv4UnreachableDefaultRoute() {
1083 for (RouteInfo r : mRoutes) {
1084 if (r.isIPv4UnreachableDefault()) {
1085 return true;
1086 }
1087 }
1088 return false;
1089 }
1090
1091 /**
paulhud9736de2019-03-08 16:35:20 +08001092 * For backward compatibility.
1093 * This was annotated with @UnsupportedAppUsage in P, so we can't remove the method completely
1094 * just yet.
1095 * @return {@code true} if there is a global preferred IPv6 address, {@code false} otherwise.
1096 * @hide
1097 */
1098 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
1099 public boolean hasGlobalIPv6Address() {
1100 return hasGlobalIpv6Address();
1101 }
1102
1103 /**
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +09001104 * Returns true if this link has an IPv4 default route.
1105 *
1106 * @return {@code true} if there is an IPv4 default route, {@code false} otherwise.
1107 * @hide
1108 */
Aaron Huang8b322ed2019-10-02 20:07:38 +08001109 @SystemApi
paulhud9736de2019-03-08 16:35:20 +08001110 public boolean hasIpv4DefaultRoute() {
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +09001111 for (RouteInfo r : mRoutes) {
Hugo Benichibd87a392017-10-10 16:29:06 +09001112 if (r.isIPv4Default()) {
1113 return true;
1114 }
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +09001115 }
1116 return false;
1117 }
1118
1119 /**
Lorenzo Colitti5ccee0c2020-04-02 04:50:41 +00001120 * Returns true if this link has an IPv6 unreachable default route.
1121 *
1122 * @return {@code true} if there is an IPv6 unreachable default route, {@code false} otherwise.
1123 * @hide
1124 */
1125 public boolean hasIpv6UnreachableDefaultRoute() {
1126 for (RouteInfo r : mRoutes) {
1127 if (r.isIPv6UnreachableDefault()) {
1128 return true;
1129 }
1130 }
1131 return false;
1132 }
1133
1134 /**
paulhud9736de2019-03-08 16:35:20 +08001135 * For backward compatibility.
1136 * This was annotated with @UnsupportedAppUsage in P, so we can't remove the method completely
1137 * just yet.
1138 * @return {@code true} if there is an IPv4 default route, {@code false} otherwise.
1139 * @hide
1140 */
1141 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
1142 public boolean hasIPv4DefaultRoute() {
1143 return hasIpv4DefaultRoute();
1144 }
1145
1146 /**
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +09001147 * Returns true if this link has an IPv6 default route.
1148 *
1149 * @return {@code true} if there is an IPv6 default route, {@code false} otherwise.
1150 * @hide
1151 */
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +09001152 @TestApi
1153 @SystemApi
paulhud9736de2019-03-08 16:35:20 +08001154 public boolean hasIpv6DefaultRoute() {
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +09001155 for (RouteInfo r : mRoutes) {
Hugo Benichibd87a392017-10-10 16:29:06 +09001156 if (r.isIPv6Default()) {
1157 return true;
1158 }
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +09001159 }
1160 return false;
1161 }
1162
1163 /**
paulhud9736de2019-03-08 16:35:20 +08001164 * For backward compatibility.
1165 * This was annotated with @UnsupportedAppUsage in P, so we can't remove the method completely
1166 * just yet.
1167 * @return {@code true} if there is an IPv6 default route, {@code false} otherwise.
1168 * @hide
1169 */
1170 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
1171 public boolean hasIPv6DefaultRoute() {
1172 return hasIpv6DefaultRoute();
1173 }
1174
1175 /**
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +09001176 * Returns true if this link has an IPv4 DNS server.
1177 *
1178 * @return {@code true} if there is an IPv4 DNS server, {@code false} otherwise.
1179 * @hide
1180 */
Aaron Huang8b322ed2019-10-02 20:07:38 +08001181 @SystemApi
paulhud9736de2019-03-08 16:35:20 +08001182 public boolean hasIpv4DnsServer() {
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +09001183 for (InetAddress ia : mDnses) {
Hugo Benichibd87a392017-10-10 16:29:06 +09001184 if (ia instanceof Inet4Address) {
1185 return true;
1186 }
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +09001187 }
1188 return false;
1189 }
1190
1191 /**
paulhud9736de2019-03-08 16:35:20 +08001192 * For backward compatibility.
1193 * This was annotated with @UnsupportedAppUsage in P, so we can't remove the method completely
1194 * just yet.
1195 * @return {@code true} if there is an IPv4 DNS server, {@code false} otherwise.
1196 * @hide
1197 */
1198 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
1199 public boolean hasIPv4DnsServer() {
1200 return hasIpv4DnsServer();
1201 }
1202
1203 /**
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +09001204 * Returns true if this link has an IPv6 DNS server.
1205 *
1206 * @return {@code true} if there is an IPv6 DNS server, {@code false} otherwise.
1207 * @hide
1208 */
Aaron Huang8b322ed2019-10-02 20:07:38 +08001209 @SystemApi
paulhud9736de2019-03-08 16:35:20 +08001210 public boolean hasIpv6DnsServer() {
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +09001211 for (InetAddress ia : mDnses) {
Hugo Benichibd87a392017-10-10 16:29:06 +09001212 if (ia instanceof Inet6Address) {
1213 return true;
1214 }
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +09001215 }
1216 return false;
1217 }
1218
1219 /**
paulhud9736de2019-03-08 16:35:20 +08001220 * For backward compatibility.
1221 * This was annotated with @UnsupportedAppUsage in P, so we can't remove the method completely
1222 * just yet.
1223 * @return {@code true} if there is an IPv6 DNS server, {@code false} otherwise.
1224 * @hide
1225 */
1226 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
1227 public boolean hasIPv6DnsServer() {
1228 return hasIpv6DnsServer();
1229 }
1230
1231 /**
Hongshikfa425012018-06-28 20:42:19 +09001232 * Returns true if this link has an IPv4 PCSCF server.
1233 *
1234 * @return {@code true} if there is an IPv4 PCSCF server, {@code false} otherwise.
1235 * @hide
1236 */
paulhud9736de2019-03-08 16:35:20 +08001237 public boolean hasIpv4PcscfServer() {
Hongshikfa425012018-06-28 20:42:19 +09001238 for (InetAddress ia : mPcscfs) {
1239 if (ia instanceof Inet4Address) {
1240 return true;
1241 }
1242 }
1243 return false;
1244 }
1245
1246 /**
1247 * Returns true if this link has an IPv6 PCSCF server.
1248 *
1249 * @return {@code true} if there is an IPv6 PCSCF server, {@code false} otherwise.
1250 * @hide
1251 */
paulhud9736de2019-03-08 16:35:20 +08001252 public boolean hasIpv6PcscfServer() {
Hongshikfa425012018-06-28 20:42:19 +09001253 for (InetAddress ia : mPcscfs) {
1254 if (ia instanceof Inet6Address) {
1255 return true;
1256 }
1257 }
1258 return false;
1259 }
1260
1261 /**
Erik Klined3b9fd32014-10-24 21:50:20 +09001262 * Returns true if this link is provisioned for global IPv4 connectivity.
1263 * This requires an IP address, default route, and DNS server.
1264 *
1265 * @return {@code true} if the link is provisioned, {@code false} otherwise.
Erik Klinecd7ed162015-05-21 16:15:02 +09001266 * @hide
Erik Klined3b9fd32014-10-24 21:50:20 +09001267 */
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +09001268 @TestApi
1269 @SystemApi
paulhud9736de2019-03-08 16:35:20 +08001270 public boolean isIpv4Provisioned() {
1271 return (hasIpv4Address()
1272 && hasIpv4DefaultRoute()
1273 && hasIpv4DnsServer());
Erik Klined3b9fd32014-10-24 21:50:20 +09001274 }
1275
1276 /**
1277 * Returns true if this link is provisioned for global IPv6 connectivity.
1278 * This requires an IP address, default route, and DNS server.
1279 *
1280 * @return {@code true} if the link is provisioned, {@code false} otherwise.
Erik Klinecd7ed162015-05-21 16:15:02 +09001281 * @hide
Erik Klined3b9fd32014-10-24 21:50:20 +09001282 */
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +09001283 @TestApi
1284 @SystemApi
paulhud9736de2019-03-08 16:35:20 +08001285 public boolean isIpv6Provisioned() {
1286 return (hasGlobalIpv6Address()
1287 && hasIpv6DefaultRoute()
1288 && hasIpv6DnsServer());
Erik Klined3b9fd32014-10-24 21:50:20 +09001289 }
1290
1291 /**
paulhud9736de2019-03-08 16:35:20 +08001292 * For backward compatibility.
1293 * This was annotated with @UnsupportedAppUsage in P, so we can't remove the method completely
1294 * just yet.
1295 * @return {@code true} if the link is provisioned, {@code false} otherwise.
1296 * @hide
1297 */
1298 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
1299 public boolean isIPv6Provisioned() {
1300 return isIpv6Provisioned();
1301 }
1302
1303
1304 /**
Erik Klined3b9fd32014-10-24 21:50:20 +09001305 * Returns true if this link is provisioned for global connectivity,
1306 * for at least one Internet Protocol family.
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +09001307 *
1308 * @return {@code true} if the link is provisioned, {@code false} otherwise.
1309 * @hide
1310 */
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +09001311 @TestApi
1312 @SystemApi
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +09001313 public boolean isProvisioned() {
paulhud9736de2019-03-08 16:35:20 +08001314 return (isIpv4Provisioned() || isIpv6Provisioned());
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +09001315 }
1316
1317 /**
Erik Klineb36a3132015-06-26 19:21:34 +09001318 * Evaluate whether the {@link InetAddress} is considered reachable.
1319 *
1320 * @return {@code true} if the given {@link InetAddress} is considered reachable,
1321 * {@code false} otherwise.
1322 * @hide
1323 */
Remi NGUYEN VAN94a05572019-01-20 12:38:10 +09001324 @TestApi
1325 @SystemApi
paulhud9736de2019-03-08 16:35:20 +08001326 public boolean isReachable(@NonNull InetAddress ip) {
Erik Klineb36a3132015-06-26 19:21:34 +09001327 final List<RouteInfo> allRoutes = getAllRoutes();
1328 // If we don't have a route to this IP address, it's not reachable.
1329 final RouteInfo bestRoute = RouteInfo.selectBestRoute(allRoutes, ip);
1330 if (bestRoute == null) {
1331 return false;
1332 }
1333
1334 // TODO: better source address evaluation for destination addresses.
1335
1336 if (ip instanceof Inet4Address) {
1337 // For IPv4, it suffices for now to simply have any address.
paulhud9736de2019-03-08 16:35:20 +08001338 return hasIpv4AddressOnInterface(bestRoute.getInterface());
Erik Klineb36a3132015-06-26 19:21:34 +09001339 } else if (ip instanceof Inet6Address) {
1340 if (ip.isLinkLocalAddress()) {
1341 // For now, just make sure link-local destinations have
1342 // scopedIds set, since transmits will generally fail otherwise.
1343 // TODO: verify it matches the ifindex of one of the interfaces.
1344 return (((Inet6Address)ip).getScopeId() != 0);
1345 } else {
1346 // For non-link-local destinations check that either the best route
1347 // is directly connected or that some global preferred address exists.
1348 // TODO: reconsider all cases (disconnected ULA networks, ...).
paulhud9736de2019-03-08 16:35:20 +08001349 return (!bestRoute.hasGateway() || hasGlobalIpv6Address());
Erik Klineb36a3132015-06-26 19:21:34 +09001350 }
1351 }
1352
1353 return false;
1354 }
1355
1356 /**
Wink Savillee8222252011-07-13 13:44:13 -07001357 * Compares this {@code LinkProperties} interface name against the target
1358 *
1359 * @param target LinkProperties to compare.
1360 * @return {@code true} if both are identical, {@code false} otherwise.
Robert Greenwalt4f05d552014-05-18 22:01:38 -07001361 * @hide
Wink Savillee8222252011-07-13 13:44:13 -07001362 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001363 @UnsupportedAppUsage
paulhud9736de2019-03-08 16:35:20 +08001364 public boolean isIdenticalInterfaceName(@NonNull LinkProperties target) {
Aaron Huang94df39a2019-12-17 00:33:18 +08001365 return LinkPropertiesUtils.isIdenticalInterfaceName(target, this);
Wink Savillee8222252011-07-13 13:44:13 -07001366 }
1367
1368 /**
ruibin zhangad2689c2019-05-23 19:35:30 +08001369 * Compares this {@code LinkProperties} DHCP server address against the target
1370 *
1371 * @param target LinkProperties to compare.
1372 * @return {@code true} if both are identical, {@code false} otherwise.
1373 * @hide
1374 */
1375 public boolean isIdenticalDhcpServerAddress(@NonNull LinkProperties target) {
1376 return Objects.equals(mDhcpServerAddress, target.mDhcpServerAddress);
1377 }
1378
1379 /**
Robert Greenwalt4717c262012-10-31 14:32:53 -07001380 * Compares this {@code LinkProperties} interface addresses against the target
Wink Savillee8222252011-07-13 13:44:13 -07001381 *
1382 * @param target LinkProperties to compare.
1383 * @return {@code true} if both are identical, {@code false} otherwise.
Robert Greenwalt4f05d552014-05-18 22:01:38 -07001384 * @hide
Wink Savillee8222252011-07-13 13:44:13 -07001385 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001386 @UnsupportedAppUsage
paulhud9736de2019-03-08 16:35:20 +08001387 public boolean isIdenticalAddresses(@NonNull LinkProperties target) {
Aaron Huang94df39a2019-12-17 00:33:18 +08001388 return LinkPropertiesUtils.isIdenticalAddresses(target, this);
Wink Savillee8222252011-07-13 13:44:13 -07001389 }
1390
1391 /**
1392 * Compares this {@code LinkProperties} DNS addresses against the target
1393 *
1394 * @param target LinkProperties to compare.
1395 * @return {@code true} if both are identical, {@code false} otherwise.
Robert Greenwalt4f05d552014-05-18 22:01:38 -07001396 * @hide
Wink Savillee8222252011-07-13 13:44:13 -07001397 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001398 @UnsupportedAppUsage
paulhud9736de2019-03-08 16:35:20 +08001399 public boolean isIdenticalDnses(@NonNull LinkProperties target) {
Aaron Huang94df39a2019-12-17 00:33:18 +08001400 return LinkPropertiesUtils.isIdenticalDnses(target, this);
Wink Savillee8222252011-07-13 13:44:13 -07001401 }
1402
1403 /**
dalykd9201342018-01-17 14:20:55 -05001404 * Compares this {@code LinkProperties} private DNS settings against the
1405 * target.
1406 *
1407 * @param target LinkProperties to compare.
1408 * @return {@code true} if both are identical, {@code false} otherwise.
1409 * @hide
1410 */
paulhud9736de2019-03-08 16:35:20 +08001411 public boolean isIdenticalPrivateDns(@NonNull LinkProperties target) {
dalykd9201342018-01-17 14:20:55 -05001412 return (isPrivateDnsActive() == target.isPrivateDnsActive()
1413 && TextUtils.equals(getPrivateDnsServerName(),
1414 target.getPrivateDnsServerName()));
1415 }
1416
1417 /**
Chalard Jean03dbf6b2018-04-11 16:36:41 +09001418 * Compares this {@code LinkProperties} validated private DNS addresses against
1419 * the target
1420 *
1421 * @param target LinkProperties to compare.
1422 * @return {@code true} if both are identical, {@code false} otherwise.
1423 * @hide
1424 */
paulhud9736de2019-03-08 16:35:20 +08001425 public boolean isIdenticalValidatedPrivateDnses(@NonNull LinkProperties target) {
Chalard Jean03dbf6b2018-04-11 16:36:41 +09001426 Collection<InetAddress> targetDnses = target.getValidatedPrivateDnsServers();
1427 return (mValidatedPrivateDnses.size() == targetDnses.size())
1428 ? mValidatedPrivateDnses.containsAll(targetDnses) : false;
1429 }
1430
1431 /**
Hongshikfa425012018-06-28 20:42:19 +09001432 * Compares this {@code LinkProperties} PCSCF addresses against the target
1433 *
1434 * @param target LinkProperties to compare.
1435 * @return {@code true} if both are identical, {@code false} otherwise.
1436 * @hide
1437 */
paulhud9736de2019-03-08 16:35:20 +08001438 public boolean isIdenticalPcscfs(@NonNull LinkProperties target) {
Hongshikfa425012018-06-28 20:42:19 +09001439 Collection<InetAddress> targetPcscfs = target.getPcscfServers();
1440 return (mPcscfs.size() == targetPcscfs.size()) ?
1441 mPcscfs.containsAll(targetPcscfs) : false;
1442 }
1443
1444 /**
Wink Savillee8222252011-07-13 13:44:13 -07001445 * Compares this {@code LinkProperties} Routes against the target
1446 *
1447 * @param target LinkProperties to compare.
1448 * @return {@code true} if both are identical, {@code false} otherwise.
Robert Greenwalt4f05d552014-05-18 22:01:38 -07001449 * @hide
Wink Savillee8222252011-07-13 13:44:13 -07001450 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001451 @UnsupportedAppUsage
paulhud9736de2019-03-08 16:35:20 +08001452 public boolean isIdenticalRoutes(@NonNull LinkProperties target) {
Aaron Huang94df39a2019-12-17 00:33:18 +08001453 return LinkPropertiesUtils.isIdenticalRoutes(target, this);
Wink Savillee8222252011-07-13 13:44:13 -07001454 }
1455
1456 /**
1457 * Compares this {@code LinkProperties} HttpProxy against the target
1458 *
1459 * @param target LinkProperties to compare.
1460 * @return {@code true} if both are identical, {@code false} otherwise.
Robert Greenwalt4f05d552014-05-18 22:01:38 -07001461 * @hide
Wink Savillee8222252011-07-13 13:44:13 -07001462 */
Mathew Inwood55418ea2018-12-20 15:30:45 +00001463 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
paulhud9736de2019-03-08 16:35:20 +08001464 public boolean isIdenticalHttpProxy(@NonNull LinkProperties target) {
Aaron Huang94df39a2019-12-17 00:33:18 +08001465 return LinkPropertiesUtils.isIdenticalHttpProxy(target, this);
Wink Savillee8222252011-07-13 13:44:13 -07001466 }
John Wang4e900092011-04-04 12:35:42 -07001467
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -08001468 /**
1469 * Compares this {@code LinkProperties} stacked links against the target
1470 *
1471 * @param target LinkProperties to compare.
1472 * @return {@code true} if both are identical, {@code false} otherwise.
Robert Greenwalt4f05d552014-05-18 22:01:38 -07001473 * @hide
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -08001474 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001475 @UnsupportedAppUsage
paulhud9736de2019-03-08 16:35:20 +08001476 public boolean isIdenticalStackedLinks(@NonNull LinkProperties target) {
Lorenzo Colitti213f98b2013-04-01 10:47:43 +09001477 if (!mStackedLinks.keySet().equals(target.mStackedLinks.keySet())) {
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -08001478 return false;
1479 }
1480 for (LinkProperties stacked : mStackedLinks.values()) {
1481 // Hashtable values can never be null.
1482 String iface = stacked.getInterfaceName();
1483 if (!stacked.equals(target.mStackedLinks.get(iface))) {
1484 return false;
1485 }
1486 }
1487 return true;
1488 }
1489
sy.yun9d9b74a2013-09-02 05:24:09 +09001490 /**
1491 * Compares this {@code LinkProperties} MTU against the target
1492 *
Ying Wangd57de6a2013-09-06 22:53:16 -07001493 * @param target LinkProperties to compare.
sy.yun9d9b74a2013-09-02 05:24:09 +09001494 * @return {@code true} if both are identical, {@code false} otherwise.
Robert Greenwalt4f05d552014-05-18 22:01:38 -07001495 * @hide
sy.yun9d9b74a2013-09-02 05:24:09 +09001496 */
paulhud9736de2019-03-08 16:35:20 +08001497 public boolean isIdenticalMtu(@NonNull LinkProperties target) {
sy.yun9d9b74a2013-09-02 05:24:09 +09001498 return getMtu() == target.getMtu();
1499 }
1500
Robert Greenwalt3f05bf42014-08-06 12:00:25 -07001501 /**
1502 * Compares this {@code LinkProperties} Tcp buffer sizes against the target.
1503 *
1504 * @param target LinkProperties to compare.
1505 * @return {@code true} if both are identical, {@code false} otherwise.
1506 * @hide
1507 */
paulhud9736de2019-03-08 16:35:20 +08001508 public boolean isIdenticalTcpBufferSizes(@NonNull LinkProperties target) {
Robert Greenwalt3f05bf42014-08-06 12:00:25 -07001509 return Objects.equals(mTcpBufferSizes, target.mTcpBufferSizes);
1510 }
1511
John Wang4e900092011-04-04 12:35:42 -07001512 /**
Lorenzo Colitti59b34472019-01-08 09:58:59 +09001513 * Compares this {@code LinkProperties} NAT64 prefix against the target.
1514 *
1515 * @param target LinkProperties to compare.
1516 * @return {@code true} if both are identical, {@code false} otherwise.
1517 * @hide
1518 */
paulhud9736de2019-03-08 16:35:20 +08001519 public boolean isIdenticalNat64Prefix(@NonNull LinkProperties target) {
Lorenzo Colitti59b34472019-01-08 09:58:59 +09001520 return Objects.equals(mNat64Prefix, target.mNat64Prefix);
1521 }
1522
1523 /**
Valentin Iftimec86ebba2019-09-24 13:32:13 +02001524 * Compares this {@code LinkProperties} WakeOnLan supported against the target.
1525 *
1526 * @param target LinkProperties to compare.
1527 * @return {@code true} if both are identical, {@code false} otherwise.
1528 * @hide
1529 */
1530 public boolean isIdenticalWakeOnLan(LinkProperties target) {
1531 return isWakeOnLanSupported() == target.isWakeOnLanSupported();
1532 }
1533
1534 /**
Remi NGUYEN VANead1ef42019-12-17 16:45:42 +09001535 * Compares this {@code LinkProperties}'s CaptivePortalApiUrl against the target.
1536 *
1537 * @param target LinkProperties to compare.
1538 * @return {@code true} if both are identical, {@code false} otherwise.
1539 * @hide
1540 */
1541 public boolean isIdenticalCaptivePortalApiUrl(LinkProperties target) {
1542 return Objects.equals(mCaptivePortalApiUrl, target.mCaptivePortalApiUrl);
1543 }
1544
1545 /**
1546 * Compares this {@code LinkProperties}'s CaptivePortalData against the target.
1547 *
1548 * @param target LinkProperties to compare.
1549 * @return {@code true} if both are identical, {@code false} otherwise.
1550 * @hide
1551 */
1552 public boolean isIdenticalCaptivePortalData(LinkProperties target) {
1553 return Objects.equals(mCaptivePortalData, target.mCaptivePortalData);
1554 }
1555
1556 /**
Valentin Iftimec86ebba2019-09-24 13:32:13 +02001557 * Set whether the network interface supports WakeOnLAN
1558 *
1559 * @param supported WakeOnLAN supported value
1560 *
1561 * @hide
1562 */
1563 public void setWakeOnLanSupported(boolean supported) {
1564 mWakeOnLanSupported = supported;
1565 }
1566
1567 /**
1568 * Returns whether the network interface supports WakeOnLAN
1569 *
1570 * @return {@code true} if interface supports WakeOnLAN, {@code false} otherwise.
1571 */
1572 public boolean isWakeOnLanSupported() {
1573 return mWakeOnLanSupported;
1574 }
1575
1576 /**
Remi NGUYEN VANead1ef42019-12-17 16:45:42 +09001577 * Set the URL of the captive portal API endpoint to get more information about the network.
1578 * @hide
1579 */
1580 @SystemApi
1581 @TestApi
1582 public void setCaptivePortalApiUrl(@Nullable Uri url) {
1583 mCaptivePortalApiUrl = url;
1584 }
1585
1586 /**
1587 * Get the URL of the captive portal API endpoint to get more information about the network.
1588 *
1589 * <p>This is null unless the application has
1590 * {@link android.Manifest.permission.NETWORK_SETTINGS} or
1591 * {@link NetworkStack#PERMISSION_MAINLINE_NETWORK_STACK} permissions, and the network provided
1592 * the URL.
1593 * @hide
1594 */
1595 @SystemApi
1596 @TestApi
1597 @Nullable
1598 public Uri getCaptivePortalApiUrl() {
1599 return mCaptivePortalApiUrl;
1600 }
1601
1602 /**
1603 * Set the CaptivePortalData obtained from the captive portal API (RFC7710bis).
1604 * @hide
1605 */
1606 @SystemApi
1607 @TestApi
1608 public void setCaptivePortalData(@Nullable CaptivePortalData data) {
1609 mCaptivePortalData = data;
1610 }
1611
1612 /**
1613 * Get the CaptivePortalData obtained from the captive portal API (RFC7710bis).
1614 *
1615 * <p>This is null unless the application has
1616 * {@link android.Manifest.permission.NETWORK_SETTINGS} or
1617 * {@link NetworkStack#PERMISSION_MAINLINE_NETWORK_STACK} permissions.
1618 * @hide
1619 */
1620 @SystemApi
1621 @TestApi
1622 @Nullable
1623 public CaptivePortalData getCaptivePortalData() {
1624 return mCaptivePortalData;
1625 }
1626
1627 /**
John Wang4e900092011-04-04 12:35:42 -07001628 * Compares this {@code LinkProperties} instance against the target
1629 * LinkProperties in {@code obj}. Two LinkPropertieses are equal if
1630 * all their fields are equal in values.
1631 *
1632 * For collection fields, such as mDnses, containsAll() is used to check
1633 * if two collections contains the same elements, independent of order.
1634 * There are two thoughts regarding containsAll()
1635 * 1. Duplicated elements. eg, (A, B, B) and (A, A, B) are equal.
1636 * 2. Worst case performance is O(n^2).
1637 *
1638 * @param obj the object to be tested for equality.
1639 * @return {@code true} if both objects are equal, {@code false} otherwise.
1640 */
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +09001641 @Override
John Wang4e900092011-04-04 12:35:42 -07001642 public boolean equals(Object obj) {
1643 if (this == obj) return true;
1644
1645 if (!(obj instanceof LinkProperties)) return false;
1646
John Wang4e900092011-04-04 12:35:42 -07001647 LinkProperties target = (LinkProperties) obj;
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +09001648 /*
Robert Greenwalt4f05d552014-05-18 22:01:38 -07001649 * This method does not check that stacked interfaces are equal, because
1650 * stacked interfaces are not so much a property of the link as a
1651 * description of connections between links.
1652 */
dalykd9201342018-01-17 14:20:55 -05001653 return isIdenticalInterfaceName(target)
1654 && isIdenticalAddresses(target)
ruibin zhangad2689c2019-05-23 19:35:30 +08001655 && isIdenticalDhcpServerAddress(target)
dalykd9201342018-01-17 14:20:55 -05001656 && isIdenticalDnses(target)
1657 && isIdenticalPrivateDns(target)
Chalard Jean03dbf6b2018-04-11 16:36:41 +09001658 && isIdenticalValidatedPrivateDnses(target)
Hongshikfa425012018-06-28 20:42:19 +09001659 && isIdenticalPcscfs(target)
dalykd9201342018-01-17 14:20:55 -05001660 && isIdenticalRoutes(target)
1661 && isIdenticalHttpProxy(target)
1662 && isIdenticalStackedLinks(target)
1663 && isIdenticalMtu(target)
Lorenzo Colitti59b34472019-01-08 09:58:59 +09001664 && isIdenticalTcpBufferSizes(target)
Valentin Iftimec86ebba2019-09-24 13:32:13 +02001665 && isIdenticalNat64Prefix(target)
Remi NGUYEN VANead1ef42019-12-17 16:45:42 +09001666 && isIdenticalWakeOnLan(target)
1667 && isIdenticalCaptivePortalApiUrl(target)
1668 && isIdenticalCaptivePortalData(target);
Wink Savillee8222252011-07-13 13:44:13 -07001669 }
John Wang4e900092011-04-04 12:35:42 -07001670
Wink Savillee8222252011-07-13 13:44:13 -07001671 /**
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +09001672 * Compares the DNS addresses in this LinkProperties with another
1673 * LinkProperties, examining only DNS addresses on the base link.
Robert Greenwalt0a46db52011-07-14 14:28:05 -07001674 *
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +09001675 * @param target a LinkProperties with the new list of dns addresses
1676 * @return the differences between the DNS addresses.
Robert Greenwalt4f05d552014-05-18 22:01:38 -07001677 * @hide
Robert Greenwalt0a46db52011-07-14 14:28:05 -07001678 */
paulhud9736de2019-03-08 16:35:20 +08001679 public @NonNull CompareResult<InetAddress> compareDnses(@Nullable LinkProperties target) {
Robert Greenwalt0a46db52011-07-14 14:28:05 -07001680 /*
1681 * Duplicate the InetAddresses into removed, we will be removing
1682 * dns address which are common between mDnses and target
1683 * leaving the addresses that are different. And dns address which
1684 * are in target but not in mDnses are placed in the
1685 * addedAddresses.
1686 */
Rubin Xu2fc72f72017-08-22 16:35:52 +01001687 return new CompareResult<>(mDnses, target != null ? target.getDnsServers() : null);
Robert Greenwalt0a46db52011-07-14 14:28:05 -07001688 }
1689
1690 /**
Chalard Jean03dbf6b2018-04-11 16:36:41 +09001691 * Compares the validated private DNS addresses in this LinkProperties with another
1692 * LinkProperties.
1693 *
1694 * @param target a LinkProperties with the new list of validated private dns addresses
1695 * @return the differences between the DNS addresses.
1696 * @hide
1697 */
paulhud9736de2019-03-08 16:35:20 +08001698 public @NonNull CompareResult<InetAddress> compareValidatedPrivateDnses(
1699 @Nullable LinkProperties target) {
Chalard Jean03dbf6b2018-04-11 16:36:41 +09001700 return new CompareResult<>(mValidatedPrivateDnses,
1701 target != null ? target.getValidatedPrivateDnsServers() : null);
1702 }
1703
1704 /**
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +09001705 * Compares all routes in this LinkProperties with another LinkProperties,
1706 * examining both the the base link and all stacked links.
Robert Greenwalt0a46db52011-07-14 14:28:05 -07001707 *
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +09001708 * @param target a LinkProperties with the new list of routes
1709 * @return the differences between the routes.
Robert Greenwalt4f05d552014-05-18 22:01:38 -07001710 * @hide
Robert Greenwalt0a46db52011-07-14 14:28:05 -07001711 */
paulhud9736de2019-03-08 16:35:20 +08001712 public @NonNull CompareResult<RouteInfo> compareAllRoutes(@Nullable LinkProperties target) {
Robert Greenwalt0a46db52011-07-14 14:28:05 -07001713 /*
1714 * Duplicate the RouteInfos into removed, we will be removing
Lorenzo Colitti1994bc12013-03-08 19:11:40 -08001715 * routes which are common between mRoutes and target
Robert Greenwalt0a46db52011-07-14 14:28:05 -07001716 * leaving the routes that are different. And route address which
1717 * are in target but not in mRoutes are placed in added.
1718 */
Rubin Xu2fc72f72017-08-22 16:35:52 +01001719 return new CompareResult<>(getAllRoutes(), target != null ? target.getAllRoutes() : null);
Robert Greenwalt0a46db52011-07-14 14:28:05 -07001720 }
1721
Paul Jensen992f2522014-04-28 10:33:11 -04001722 /**
1723 * Compares all interface names in this LinkProperties with another
1724 * LinkProperties, examining both the the base link and all stacked links.
1725 *
1726 * @param target a LinkProperties with the new list of interface names
1727 * @return the differences between the interface names.
1728 * @hide
1729 */
paulhud9736de2019-03-08 16:35:20 +08001730 public @NonNull CompareResult<String> compareAllInterfaceNames(
1731 @Nullable LinkProperties target) {
Paul Jensen992f2522014-04-28 10:33:11 -04001732 /*
1733 * Duplicate the interface names into removed, we will be removing
1734 * interface names which are common between this and target
1735 * leaving the interface names that are different. And interface names which
1736 * are in target but not in this are placed in added.
1737 */
Rubin Xu2fc72f72017-08-22 16:35:52 +01001738 return new CompareResult<>(getAllInterfaceNames(),
1739 target != null ? target.getAllInterfaceNames() : null);
Paul Jensen992f2522014-04-28 10:33:11 -04001740 }
1741
Robert Greenwalt0a46db52011-07-14 14:28:05 -07001742
John Wang4e900092011-04-04 12:35:42 -07001743 /**
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +09001744 * Generate hashcode based on significant fields
1745 *
John Wang4e900092011-04-04 12:35:42 -07001746 * Equal objects must produce the same hash code, while unequal objects
1747 * may have the same hash codes.
1748 */
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +09001749 @Override
John Wang4e900092011-04-04 12:35:42 -07001750 public int hashCode() {
1751 return ((null == mIfaceName) ? 0 : mIfaceName.hashCode()
1752 + mLinkAddresses.size() * 31
1753 + mDnses.size() * 37
Chalard Jean03dbf6b2018-04-11 16:36:41 +09001754 + mValidatedPrivateDnses.size() * 61
Robert Greenwalt8058f622012-11-09 10:52:27 -08001755 + ((null == mDomains) ? 0 : mDomains.hashCode())
Robert Greenwaltaa70f102011-04-28 14:28:50 -07001756 + mRoutes.size() * 41
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -08001757 + ((null == mHttpProxy) ? 0 : mHttpProxy.hashCode())
sy.yun9d9b74a2013-09-02 05:24:09 +09001758 + mStackedLinks.hashCode() * 47)
Robert Greenwalt3f05bf42014-08-06 12:00:25 -07001759 + mMtu * 51
dalykd9201342018-01-17 14:20:55 -05001760 + ((null == mTcpBufferSizes) ? 0 : mTcpBufferSizes.hashCode())
1761 + (mUsePrivateDns ? 57 : 0)
ruibin zhangad2689c2019-05-23 19:35:30 +08001762 + ((null == mDhcpServerAddress) ? 0 : mDhcpServerAddress.hashCode())
Hongshikfa425012018-06-28 20:42:19 +09001763 + mPcscfs.size() * 67
Lorenzo Colitti59b34472019-01-08 09:58:59 +09001764 + ((null == mPrivateDnsServerName) ? 0 : mPrivateDnsServerName.hashCode())
Valentin Iftimec86ebba2019-09-24 13:32:13 +02001765 + Objects.hash(mNat64Prefix)
Remi NGUYEN VANead1ef42019-12-17 16:45:42 +09001766 + (mWakeOnLanSupported ? 71 : 0)
1767 + Objects.hash(mCaptivePortalApiUrl, mCaptivePortalData);
John Wang4e900092011-04-04 12:35:42 -07001768 }
1769
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001770 /**
1771 * Implement the Parcelable interface.
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001772 */
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001773 public void writeToParcel(Parcel dest, int flags) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001774 dest.writeString(getInterfaceName());
Irfan Sheriffed5d7d12010-10-01 16:08:28 -07001775 dest.writeInt(mLinkAddresses.size());
Chalard Jean03dbf6b2018-04-11 16:36:41 +09001776 for (LinkAddress linkAddress : mLinkAddresses) {
Irfan Sheriffed5d7d12010-10-01 16:08:28 -07001777 dest.writeParcelable(linkAddress, flags);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001778 }
Irfan Sheriffed5d7d12010-10-01 16:08:28 -07001779
Remi NGUYEN VANf705ea62019-12-18 14:47:06 +09001780 writeAddresses(dest, mDnses);
1781 writeAddresses(dest, mValidatedPrivateDnses);
dalykd9201342018-01-17 14:20:55 -05001782 dest.writeBoolean(mUsePrivateDns);
1783 dest.writeString(mPrivateDnsServerName);
Remi NGUYEN VANf705ea62019-12-18 14:47:06 +09001784 writeAddresses(dest, mPcscfs);
Robert Greenwalt8058f622012-11-09 10:52:27 -08001785 dest.writeString(mDomains);
Lorenzo Colitti348577e2020-01-06 19:43:59 +09001786 writeAddress(dest, mDhcpServerAddress);
sy.yun9d9b74a2013-09-02 05:24:09 +09001787 dest.writeInt(mMtu);
Robert Greenwalt3f05bf42014-08-06 12:00:25 -07001788 dest.writeString(mTcpBufferSizes);
Robert Greenwaltaa70f102011-04-28 14:28:50 -07001789 dest.writeInt(mRoutes.size());
Chalard Jean03dbf6b2018-04-11 16:36:41 +09001790 for (RouteInfo route : mRoutes) {
Robert Greenwaltaa70f102011-04-28 14:28:50 -07001791 dest.writeParcelable(route, flags);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001792 }
Robert Greenwalt992564e2011-02-09 13:56:06 -08001793
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001794 if (mHttpProxy != null) {
1795 dest.writeByte((byte)1);
1796 dest.writeParcelable(mHttpProxy, flags);
1797 } else {
1798 dest.writeByte((byte)0);
1799 }
Lorenzo Colitti59b34472019-01-08 09:58:59 +09001800 dest.writeParcelable(mNat64Prefix, 0);
1801
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +09001802 ArrayList<LinkProperties> stackedLinks = new ArrayList<>(mStackedLinks.values());
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -08001803 dest.writeList(stackedLinks);
Valentin Iftimec86ebba2019-09-24 13:32:13 +02001804
1805 dest.writeBoolean(mWakeOnLanSupported);
Remi NGUYEN VANead1ef42019-12-17 16:45:42 +09001806 dest.writeParcelable(mParcelSensitiveFields ? mCaptivePortalApiUrl : null, 0);
1807 dest.writeParcelable(mParcelSensitiveFields ? mCaptivePortalData : null, 0);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001808 }
1809
Remi NGUYEN VANf705ea62019-12-18 14:47:06 +09001810 private static void writeAddresses(@NonNull Parcel dest, @NonNull List<InetAddress> list) {
1811 dest.writeInt(list.size());
1812 for (InetAddress d : list) {
1813 writeAddress(dest, d);
1814 }
1815 }
1816
Lorenzo Colitti348577e2020-01-06 19:43:59 +09001817 private static void writeAddress(@NonNull Parcel dest, @Nullable InetAddress addr) {
1818 byte[] addressBytes = (addr == null ? null : addr.getAddress());
1819 dest.writeByteArray(addressBytes);
Remi NGUYEN VANf705ea62019-12-18 14:47:06 +09001820 if (addr instanceof Inet6Address) {
1821 final Inet6Address v6Addr = (Inet6Address) addr;
1822 final boolean hasScopeId = v6Addr.getScopeId() != 0;
1823 dest.writeBoolean(hasScopeId);
1824 if (hasScopeId) dest.writeInt(v6Addr.getScopeId());
1825 }
1826 }
1827
Lorenzo Colitti348577e2020-01-06 19:43:59 +09001828 @Nullable
Remi NGUYEN VANf705ea62019-12-18 14:47:06 +09001829 private static InetAddress readAddress(@NonNull Parcel p) throws UnknownHostException {
1830 final byte[] addr = p.createByteArray();
Lorenzo Colitti348577e2020-01-06 19:43:59 +09001831 if (addr == null) return null;
1832
Remi NGUYEN VANf705ea62019-12-18 14:47:06 +09001833 if (addr.length == INET6_ADDR_LENGTH) {
1834 final boolean hasScopeId = p.readBoolean();
1835 final int scopeId = hasScopeId ? p.readInt() : 0;
1836 return Inet6Address.getByAddress(null /* host */, addr, scopeId);
1837 }
1838
1839 return InetAddress.getByAddress(addr);
1840 }
1841
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001842 /**
1843 * Implement the Parcelable interface.
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001844 */
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07001845 public static final @android.annotation.NonNull Creator<LinkProperties> CREATOR =
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001846 new Creator<LinkProperties>() {
1847 public LinkProperties createFromParcel(Parcel in) {
1848 LinkProperties netProp = new LinkProperties();
Robert Greenwalt4717c262012-10-31 14:32:53 -07001849
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001850 String iface = in.readString();
1851 if (iface != null) {
Robert Greenwalt4717c262012-10-31 14:32:53 -07001852 netProp.setInterfaceName(iface);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001853 }
1854 int addressCount = in.readInt();
Chalard Jean03dbf6b2018-04-11 16:36:41 +09001855 for (int i = 0; i < addressCount; i++) {
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +09001856 netProp.addLinkAddress(in.readParcelable(null));
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001857 }
1858 addressCount = in.readInt();
Chalard Jean03dbf6b2018-04-11 16:36:41 +09001859 for (int i = 0; i < addressCount; i++) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001860 try {
Remi NGUYEN VANf705ea62019-12-18 14:47:06 +09001861 netProp.addDnsServer(readAddress(in));
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001862 } catch (UnknownHostException e) { }
1863 }
Chalard Jean03dbf6b2018-04-11 16:36:41 +09001864 addressCount = in.readInt();
1865 for (int i = 0; i < addressCount; i++) {
1866 try {
Remi NGUYEN VANf705ea62019-12-18 14:47:06 +09001867 netProp.addValidatedPrivateDnsServer(readAddress(in));
Chalard Jean03dbf6b2018-04-11 16:36:41 +09001868 } catch (UnknownHostException e) { }
1869 }
dalykd9201342018-01-17 14:20:55 -05001870 netProp.setUsePrivateDns(in.readBoolean());
1871 netProp.setPrivateDnsServerName(in.readString());
Hongshikfa425012018-06-28 20:42:19 +09001872 addressCount = in.readInt();
1873 for (int i = 0; i < addressCount; i++) {
1874 try {
Remi NGUYEN VANf705ea62019-12-18 14:47:06 +09001875 netProp.addPcscfServer(readAddress(in));
Hongshikfa425012018-06-28 20:42:19 +09001876 } catch (UnknownHostException e) { }
1877 }
Robert Greenwalt8058f622012-11-09 10:52:27 -08001878 netProp.setDomains(in.readString());
ruibin zhangad2689c2019-05-23 19:35:30 +08001879 try {
1880 netProp.setDhcpServerAddress((Inet4Address) InetAddress
1881 .getByAddress(in.createByteArray()));
1882 } catch (UnknownHostException e) { }
sy.yun9d9b74a2013-09-02 05:24:09 +09001883 netProp.setMtu(in.readInt());
Robert Greenwalt3f05bf42014-08-06 12:00:25 -07001884 netProp.setTcpBufferSizes(in.readString());
Robert Greenwalt992564e2011-02-09 13:56:06 -08001885 addressCount = in.readInt();
Chalard Jean03dbf6b2018-04-11 16:36:41 +09001886 for (int i = 0; i < addressCount; i++) {
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +09001887 netProp.addRoute(in.readParcelable(null));
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001888 }
1889 if (in.readByte() == 1) {
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +09001890 netProp.setHttpProxy(in.readParcelable(null));
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001891 }
Lorenzo Colitti59b34472019-01-08 09:58:59 +09001892 netProp.setNat64Prefix(in.readParcelable(null));
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -08001893 ArrayList<LinkProperties> stackedLinks = new ArrayList<LinkProperties>();
1894 in.readList(stackedLinks, LinkProperties.class.getClassLoader());
1895 for (LinkProperties stackedLink: stackedLinks) {
1896 netProp.addStackedLink(stackedLink);
1897 }
Valentin Iftimec86ebba2019-09-24 13:32:13 +02001898 netProp.setWakeOnLanSupported(in.readBoolean());
Remi NGUYEN VANead1ef42019-12-17 16:45:42 +09001899
1900 netProp.setCaptivePortalApiUrl(in.readParcelable(null));
1901 netProp.setCaptivePortalData(in.readParcelable(null));
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001902 return netProp;
1903 }
1904
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001905 public LinkProperties[] newArray(int size) {
1906 return new LinkProperties[size];
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001907 }
1908 };
w1997615afd812014-08-05 15:18:11 -07001909
Chalard Jean03dbf6b2018-04-11 16:36:41 +09001910 /**
1911 * Check the valid MTU range based on IPv4 or IPv6.
1912 * @hide
1913 */
1914 public static boolean isValidMtu(int mtu, boolean ipv6) {
1915 if (ipv6) {
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +09001916 return mtu >= MIN_MTU_V6 && mtu <= MAX_MTU;
Chalard Jean03dbf6b2018-04-11 16:36:41 +09001917 } else {
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +09001918 return mtu >= MIN_MTU && mtu <= MAX_MTU;
w1997615afd812014-08-05 15:18:11 -07001919 }
Chalard Jean03dbf6b2018-04-11 16:36:41 +09001920 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001921}