blob: 3a792065155e4ba9dd3b7ea132cb15a05fb9a761 [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;
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010022import android.annotation.UnsupportedAppUsage;
Mathew Inwood31755f92018-12-20 13:53:36 +000023import android.os.Build;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070024import android.os.Parcel;
Rubin Xu1bb5c082017-09-05 18:40:49 +010025import android.os.Parcelable;
John Wang4e900092011-04-04 12:35:42 -070026import android.text.TextUtils;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070027
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -080028import java.net.Inet4Address;
Lorenzo Colitti4faa0272013-08-08 11:00:12 +090029import java.net.Inet6Address;
Rubin Xu1bb5c082017-09-05 18:40:49 +010030import java.net.InetAddress;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070031import java.net.UnknownHostException;
32import java.util.ArrayList;
33import java.util.Collection;
Robert Greenwalt37e65eb2010-08-30 10:56:47 -070034import java.util.Collections;
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -080035import java.util.Hashtable;
Robert Greenwaltdf2b8782014-06-06 10:30:11 -070036import java.util.List;
Lorenzo Colittic17a1b92014-06-12 23:10:17 +090037import java.util.Objects;
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +090038import java.util.StringJoiner;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070039
40/**
Robert Greenwalt37e65eb2010-08-30 10:56:47 -070041 * Describes the properties of a network link.
Robert Greenwalt992564e2011-02-09 13:56:06 -080042 *
43 * A link represents a connection to a network.
44 * It may have multiple addresses and multiple gateways,
Robert Greenwalt4f05d552014-05-18 22:01:38 -070045 * multiple dns servers but only one http proxy and one
46 * network interface.
Robert Greenwalt992564e2011-02-09 13:56:06 -080047 *
Robert Greenwalt4f05d552014-05-18 22:01:38 -070048 * Note that this is just a holder of data. Modifying it
49 * does not affect live networks.
Robert Greenwalt992564e2011-02-09 13:56:06 -080050 *
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070051 */
Robert Greenwalte595b972014-06-12 16:24:38 -070052public final class LinkProperties implements Parcelable {
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -080053 // The interface described by the network link.
Mathew Inwood31755f92018-12-20 13:53:36 +000054 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Robert Greenwalt4717c262012-10-31 14:32:53 -070055 private String mIfaceName;
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +090056 private ArrayList<LinkAddress> mLinkAddresses = new ArrayList<>();
57 private ArrayList<InetAddress> mDnses = new ArrayList<>();
58 private ArrayList<InetAddress> mValidatedPrivateDnses = new ArrayList<>();
dalykd9201342018-01-17 14:20:55 -050059 private boolean mUsePrivateDns;
60 private String mPrivateDnsServerName;
Robert Greenwalt8058f622012-11-09 10:52:27 -080061 private String mDomains;
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +090062 private ArrayList<RouteInfo> mRoutes = new ArrayList<>();
Jason Monk207900c2014-04-25 15:00:09 -040063 private ProxyInfo mHttpProxy;
sy.yun9d9b74a2013-09-02 05:24:09 +090064 private int mMtu;
Robert Greenwalt3f05bf42014-08-06 12:00:25 -070065 // in the format "rmem_min,rmem_def,rmem_max,wmem_min,wmem_def,wmem_max"
66 private String mTcpBufferSizes;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070067
w1997615afd812014-08-05 15:18:11 -070068 private static final int MIN_MTU = 68;
69 private static final int MIN_MTU_V6 = 1280;
70 private static final int MAX_MTU = 10000;
71
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -080072 // Stores the properties of links that are "stacked" above this link.
73 // Indexed by interface name to allow modification and to prevent duplicates being added.
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +090074 private Hashtable<String, LinkProperties> mStackedLinks = new Hashtable<>();
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -080075
Robert Greenwaltdf2b8782014-06-06 10:30:11 -070076 /**
77 * @hide
78 */
Robert Greenwalt0a46db52011-07-14 14:28:05 -070079 public static class CompareResult<T> {
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +090080 public final List<T> removed = new ArrayList<>();
81 public final List<T> added = new ArrayList<>();
Rubin Xu2fc72f72017-08-22 16:35:52 +010082
83 public CompareResult() {}
84
85 public CompareResult(Collection<T> oldItems, Collection<T> newItems) {
86 if (oldItems != null) {
87 removed.addAll(oldItems);
88 }
89 if (newItems != null) {
90 for (T newItem : newItems) {
91 if (!removed.remove(newItem)) {
92 added.add(newItem);
93 }
94 }
95 }
96 }
Wink Savillee8222252011-07-13 13:44:13 -070097
98 @Override
99 public String toString() {
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900100 return "removed=[" + TextUtils.join(",", removed)
101 + "] added=[" + TextUtils.join(",", added)
102 + "]";
Wink Savillee8222252011-07-13 13:44:13 -0700103 }
104 }
105
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -0700106 /**
107 * @hide
108 */
Erik Klinecd7ed162015-05-21 16:15:02 +0900109 public enum ProvisioningChange {
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100110 @UnsupportedAppUsage
Erik Klinecd7ed162015-05-21 16:15:02 +0900111 STILL_NOT_PROVISIONED,
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100112 @UnsupportedAppUsage
Erik Klinecd7ed162015-05-21 16:15:02 +0900113 LOST_PROVISIONING,
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100114 @UnsupportedAppUsage
Erik Klinecd7ed162015-05-21 16:15:02 +0900115 GAINED_PROVISIONING,
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100116 @UnsupportedAppUsage
Erik Klinecd7ed162015-05-21 16:15:02 +0900117 STILL_PROVISIONED,
118 }
119
120 /**
121 * Compare the provisioning states of two LinkProperties instances.
122 *
123 * @hide
124 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100125 @UnsupportedAppUsage
Erik Klinecd7ed162015-05-21 16:15:02 +0900126 public static ProvisioningChange compareProvisioning(
127 LinkProperties before, LinkProperties after) {
128 if (before.isProvisioned() && after.isProvisioned()) {
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900129 // On dual-stack networks, DHCPv4 renewals can occasionally fail.
Erik Klinecd7ed162015-05-21 16:15:02 +0900130 // When this happens, IPv6-reachable services continue to function
131 // normally but IPv4-only services (naturally) fail.
132 //
133 // When an application using an IPv4-only service reports a bad
134 // network condition to the framework, attempts to re-validate
135 // the network succeed (since we support IPv6-only networks) and
136 // nothing is changed.
137 //
138 // For users, this is confusing and unexpected behaviour, and is
139 // not necessarily easy to diagnose. Therefore, we treat changing
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900140 // from a dual-stack network to an IPv6-only network equivalent to
Erik Klinecd7ed162015-05-21 16:15:02 +0900141 // a total loss of provisioning.
142 //
143 // For one such example of this, see b/18867306.
144 //
Erik Kline1ad4e222015-08-14 12:16:55 +0900145 // Additionally, losing IPv6 provisioning can result in TCP
146 // connections getting stuck until timeouts fire and other
147 // baffling failures. Therefore, loss of either IPv4 or IPv6 on a
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900148 // previously dual-stack network is deemed a lost of provisioning.
Erik Kline1ad4e222015-08-14 12:16:55 +0900149 if ((before.isIPv4Provisioned() && !after.isIPv4Provisioned()) ||
150 (before.isIPv6Provisioned() && !after.isIPv6Provisioned())) {
Erik Klinecd7ed162015-05-21 16:15:02 +0900151 return ProvisioningChange.LOST_PROVISIONING;
152 }
153 return ProvisioningChange.STILL_PROVISIONED;
154 } else if (before.isProvisioned() && !after.isProvisioned()) {
155 return ProvisioningChange.LOST_PROVISIONING;
156 } else if (!before.isProvisioned() && after.isProvisioned()) {
157 return ProvisioningChange.GAINED_PROVISIONING;
158 } else { // !before.isProvisioned() && !after.isProvisioned()
159 return ProvisioningChange.STILL_NOT_PROVISIONED;
160 }
161 }
162
163 /**
164 * @hide
165 */
paulhu7610bc72018-12-12 17:52:57 +0800166 @SystemApi
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700167 public LinkProperties() {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700168 }
169
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -0700170 /**
171 * @hide
172 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100173 @UnsupportedAppUsage
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700174 public LinkProperties(LinkProperties source) {
Irfan Sheriffef6c1432010-08-30 20:37:17 -0700175 if (source != null) {
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900176 mIfaceName = source.mIfaceName;
177 mLinkAddresses.addAll(source.mLinkAddresses);
178 mDnses.addAll(source.mDnses);
179 mValidatedPrivateDnses.addAll(source.mValidatedPrivateDnses);
dalykd9201342018-01-17 14:20:55 -0500180 mUsePrivateDns = source.mUsePrivateDns;
181 mPrivateDnsServerName = source.mPrivateDnsServerName;
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900182 mDomains = source.mDomains;
183 mRoutes.addAll(source.mRoutes);
184 mHttpProxy = (source.mHttpProxy == null) ? null : new ProxyInfo(source.mHttpProxy);
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800185 for (LinkProperties l: source.mStackedLinks.values()) {
186 addStackedLink(l);
187 }
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900188 setMtu(source.mMtu);
Robert Greenwalt3f05bf42014-08-06 12:00:25 -0700189 mTcpBufferSizes = source.mTcpBufferSizes;
Irfan Sheriffef6c1432010-08-30 20:37:17 -0700190 }
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700191 }
192
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700193 /**
194 * Sets the interface name for this link. All {@link RouteInfo} already set for this
195 * will have their interface changed to match this new value.
196 *
197 * @param iface The name of the network interface used for this link.
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700198 * @hide
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700199 */
paulhu7610bc72018-12-12 17:52:57 +0800200 @SystemApi
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700201 public void setInterfaceName(String iface) {
202 mIfaceName = iface;
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900203 ArrayList<RouteInfo> newRoutes = new ArrayList<>(mRoutes.size());
Lorenzo Colitti45b9a5b2013-03-08 11:30:39 -0800204 for (RouteInfo route : mRoutes) {
205 newRoutes.add(routeWithInterface(route));
206 }
207 mRoutes = newRoutes;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700208 }
209
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700210 /**
211 * Gets the interface name for this link. May be {@code null} if not set.
212 *
213 * @return The interface name set for this link or {@code null}.
214 */
Jeff Sharkey9da2f1e2014-08-14 12:55:00 -0700215 public @Nullable String getInterfaceName() {
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700216 return mIfaceName;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700217 }
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700218
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700219 /**
220 * @hide
221 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100222 @UnsupportedAppUsage
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700223 public List<String> getAllInterfaceNames() {
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900224 List<String> interfaceNames = new ArrayList<>(mStackedLinks.size() + 1);
225 if (mIfaceName != null) interfaceNames.add(mIfaceName);
Lorenzo Colitti4aa9bcf2013-03-20 19:22:58 +0900226 for (LinkProperties stacked: mStackedLinks.values()) {
227 interfaceNames.addAll(stacked.getAllInterfaceNames());
228 }
229 return interfaceNames;
230 }
231
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +0900232 /**
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700233 * Returns all the addresses on this link. We often think of a link having a single address,
234 * however, particularly with Ipv6 several addresses are typical. Note that the
235 * {@code LinkProperties} actually contains {@link LinkAddress} objects which also include
236 * prefix lengths for each address. This is a simplified utility alternative to
237 * {@link LinkProperties#getLinkAddresses}.
238 *
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900239 * @return An unmodifiable {@link List} of {@link InetAddress} for this link.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700240 * @hide
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +0900241 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100242 @UnsupportedAppUsage
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700243 public List<InetAddress> getAddresses() {
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900244 List<InetAddress> addresses = new ArrayList<>();
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700245 for (LinkAddress linkAddress : mLinkAddresses) {
246 addresses.add(linkAddress.getAddress());
247 }
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700248 return Collections.unmodifiableList(addresses);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700249 }
250
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +0900251 /**
252 * Returns all the addresses on this link and all the links stacked above it.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700253 * @hide
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +0900254 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100255 @UnsupportedAppUsage
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700256 public List<InetAddress> getAllAddresses() {
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900257 List<InetAddress> addresses = new ArrayList<>();
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +0900258 for (LinkAddress linkAddress : mLinkAddresses) {
259 addresses.add(linkAddress.getAddress());
260 }
261 for (LinkProperties stacked: mStackedLinks.values()) {
262 addresses.addAll(stacked.getAllAddresses());
263 }
264 return addresses;
265 }
266
Lorenzo Colitti64483942013-11-15 18:43:52 +0900267 private int findLinkAddressIndex(LinkAddress address) {
268 for (int i = 0; i < mLinkAddresses.size(); i++) {
269 if (mLinkAddresses.get(i).isSameAddressAs(address)) {
270 return i;
271 }
Lorenzo Colitti4faa0272013-08-08 11:00:12 +0900272 }
Lorenzo Colitti64483942013-11-15 18:43:52 +0900273 return -1;
Lorenzo Colitti4faa0272013-08-08 11:00:12 +0900274 }
275
276 /**
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700277 * Adds a {@link LinkAddress} to this {@code LinkProperties} if a {@link LinkAddress} of the
278 * same address/prefix does not already exist. If it does exist it is replaced.
Lorenzo Colitti64483942013-11-15 18:43:52 +0900279 * @param address The {@code LinkAddress} to add.
280 * @return true if {@code address} was added or updated, false otherwise.
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700281 * @hide
Lorenzo Colitti64483942013-11-15 18:43:52 +0900282 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100283 @UnsupportedAppUsage
Lorenzo Colitti64483942013-11-15 18:43:52 +0900284 public boolean addLinkAddress(LinkAddress address) {
285 if (address == null) {
286 return false;
287 }
288 int i = findLinkAddressIndex(address);
289 if (i < 0) {
290 // Address was not present. Add it.
291 mLinkAddresses.add(address);
292 return true;
293 } else if (mLinkAddresses.get(i).equals(address)) {
294 // Address was present and has same properties. Do nothing.
295 return false;
296 } else {
297 // Address was present and has different properties. Update it.
298 mLinkAddresses.set(i, address);
299 return true;
300 }
301 }
302
303 /**
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700304 * Removes a {@link LinkAddress} from this {@code LinkProperties}. Specifically, matches
305 * and {@link LinkAddress} with the same address and prefix.
306 *
307 * @param toRemove A {@link LinkAddress} specifying the address to remove.
Lorenzo Colitti4faa0272013-08-08 11:00:12 +0900308 * @return true if the address was removed, false if it did not exist.
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700309 * @hide
Lorenzo Colitti4faa0272013-08-08 11:00:12 +0900310 */
311 public boolean removeLinkAddress(LinkAddress toRemove) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900312 int i = findLinkAddressIndex(toRemove);
313 if (i >= 0) {
314 mLinkAddresses.remove(i);
315 return true;
316 }
317 return false;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700318 }
319
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +0900320 /**
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700321 * Returns all the {@link LinkAddress} on this link. Typically a link will have
322 * one IPv4 address and one or more IPv6 addresses.
323 *
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700324 * @return An unmodifiable {@link List} of {@link LinkAddress} for this link.
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +0900325 */
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700326 public List<LinkAddress> getLinkAddresses() {
327 return Collections.unmodifiableList(mLinkAddresses);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700328 }
329
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +0900330 /**
331 * Returns all the addresses on this link and all the links stacked above it.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700332 * @hide
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +0900333 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100334 @UnsupportedAppUsage
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700335 public List<LinkAddress> getAllLinkAddresses() {
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900336 List<LinkAddress> addresses = new ArrayList<>(mLinkAddresses);
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +0900337 for (LinkProperties stacked: mStackedLinks.values()) {
338 addresses.addAll(stacked.getAllLinkAddresses());
339 }
340 return addresses;
341 }
342
Lorenzo Colitti22f407b2013-08-23 20:54:49 +0900343 /**
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700344 * Replaces the {@link LinkAddress} in this {@code LinkProperties} with
345 * the given {@link Collection} of {@link LinkAddress}.
346 *
347 * @param addresses The {@link Collection} of {@link LinkAddress} to set in this
348 * object.
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700349 * @hide
Lorenzo Colitti22f407b2013-08-23 20:54:49 +0900350 */
paulhu7610bc72018-12-12 17:52:57 +0800351 @SystemApi
Lorenzo Colitti22f407b2013-08-23 20:54:49 +0900352 public void setLinkAddresses(Collection<LinkAddress> addresses) {
353 mLinkAddresses.clear();
354 for (LinkAddress address: addresses) {
355 addLinkAddress(address);
356 }
357 }
358
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700359 /**
Lorenzo Colitti309a75d2014-06-24 00:34:39 +0900360 * Adds the given {@link InetAddress} to the list of DNS servers, if not present.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700361 *
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700362 * @param dnsServer The {@link InetAddress} to add to the list of DNS servers.
Lorenzo Colitti309a75d2014-06-24 00:34:39 +0900363 * @return true if the DNS server was added, false if it was already present.
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700364 * @hide
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700365 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100366 @UnsupportedAppUsage
Lorenzo Colitti309a75d2014-06-24 00:34:39 +0900367 public boolean addDnsServer(InetAddress dnsServer) {
368 if (dnsServer != null && !mDnses.contains(dnsServer)) {
369 mDnses.add(dnsServer);
370 return true;
371 }
372 return false;
373 }
374
375 /**
Erik Klinecd7ed162015-05-21 16:15:02 +0900376 * Removes the given {@link InetAddress} from the list of DNS servers.
377 *
378 * @param dnsServer The {@link InetAddress} to remove from the list of DNS servers.
379 * @return true if the DNS server was removed, false if it did not exist.
380 * @hide
381 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100382 @UnsupportedAppUsage
Erik Klinecd7ed162015-05-21 16:15:02 +0900383 public boolean removeDnsServer(InetAddress dnsServer) {
384 if (dnsServer != null) {
385 return mDnses.remove(dnsServer);
386 }
387 return false;
388 }
389
390 /**
Lorenzo Colitti309a75d2014-06-24 00:34:39 +0900391 * Replaces the DNS servers in this {@code LinkProperties} with
392 * the given {@link Collection} of {@link InetAddress} objects.
393 *
Chalard Jean03dbf6b2018-04-11 16:36:41 +0900394 * @param dnsServers The {@link Collection} of DNS servers to set in this object.
Lorenzo Colitti309a75d2014-06-24 00:34:39 +0900395 * @hide
396 */
paulhu7610bc72018-12-12 17:52:57 +0800397 @SystemApi
Lorenzo Colitti309a75d2014-06-24 00:34:39 +0900398 public void setDnsServers(Collection<InetAddress> dnsServers) {
399 mDnses.clear();
400 for (InetAddress dnsServer: dnsServers) {
401 addDnsServer(dnsServer);
402 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700403 }
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700404
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700405 /**
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -0700406 * Returns all the {@link InetAddress} for DNS servers on this link.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700407 *
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900408 * @return An unmodifiable {@link List} of {@link InetAddress} for DNS servers on
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700409 * this link.
410 */
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700411 public List<InetAddress> getDnsServers() {
412 return Collections.unmodifiableList(mDnses);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700413 }
414
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700415 /**
dalykd9201342018-01-17 14:20:55 -0500416 * Set whether private DNS is currently in use on this network.
417 *
418 * @param usePrivateDns The private DNS state.
419 * @hide
420 */
421 public void setUsePrivateDns(boolean usePrivateDns) {
422 mUsePrivateDns = usePrivateDns;
423 }
424
425 /**
426 * Returns whether private DNS is currently in use on this network. When
427 * private DNS is in use, applications must not send unencrypted DNS
428 * queries as doing so could reveal private user information. Furthermore,
429 * if private DNS is in use and {@link #getPrivateDnsServerName} is not
430 * {@code null}, DNS queries must be sent to the specified DNS server.
431 *
432 * @return {@code true} if private DNS is in use, {@code false} otherwise.
433 */
434 public boolean isPrivateDnsActive() {
435 return mUsePrivateDns;
436 }
437
438 /**
439 * Set the name of the private DNS server to which private DNS queries
440 * should be sent when in strict mode. This value should be {@code null}
441 * when private DNS is off or in opportunistic mode.
442 *
443 * @param privateDnsServerName The private DNS server name.
444 * @hide
445 */
446 public void setPrivateDnsServerName(@Nullable String privateDnsServerName) {
447 mPrivateDnsServerName = privateDnsServerName;
448 }
449
450 /**
451 * Returns the private DNS server name that is in use. If not {@code null},
452 * private DNS is in strict mode. In this mode, applications should ensure
453 * that all DNS queries are encrypted and sent to this hostname and that
454 * queries are only sent if the hostname's certificate is valid. If
455 * {@code null} and {@link #isPrivateDnsActive} is {@code true}, private
456 * DNS is in opportunistic mode, and applications should ensure that DNS
457 * queries are encrypted and sent to a DNS server returned by
458 * {@link #getDnsServers}. System DNS will handle each of these cases
459 * correctly, but applications implementing their own DNS lookups must make
460 * sure to follow these requirements.
461 *
462 * @return The private DNS server name.
463 */
464 public @Nullable String getPrivateDnsServerName() {
465 return mPrivateDnsServerName;
466 }
467
468 /**
Chalard Jean03dbf6b2018-04-11 16:36:41 +0900469 * Adds the given {@link InetAddress} to the list of validated private DNS servers,
470 * if not present. This is distinct from the server name in that these are actually
471 * resolved addresses.
472 *
473 * @param dnsServer The {@link InetAddress} to add to the list of validated private DNS servers.
474 * @return true if the DNS server was added, false if it was already present.
475 * @hide
476 */
477 public boolean addValidatedPrivateDnsServer(InetAddress dnsServer) {
478 if (dnsServer != null && !mValidatedPrivateDnses.contains(dnsServer)) {
479 mValidatedPrivateDnses.add(dnsServer);
480 return true;
481 }
482 return false;
483 }
484
485 /**
486 * Removes the given {@link InetAddress} from the list of validated private DNS servers.
487 *
488 * @param dnsServer The {@link InetAddress} to remove from the list of validated private DNS
489 * servers.
490 * @return true if the DNS server was removed, false if it did not exist.
491 * @hide
492 */
493 public boolean removeValidatedPrivateDnsServer(InetAddress dnsServer) {
494 if (dnsServer != null) {
495 return mValidatedPrivateDnses.remove(dnsServer);
496 }
497 return false;
498 }
499
500 /**
501 * Replaces the validated private DNS servers in this {@code LinkProperties} with
502 * the given {@link Collection} of {@link InetAddress} objects.
503 *
504 * @param dnsServers The {@link Collection} of validated private DNS servers to set in this
505 * object.
506 * @hide
507 */
508 public void setValidatedPrivateDnsServers(Collection<InetAddress> dnsServers) {
509 mValidatedPrivateDnses.clear();
510 for (InetAddress dnsServer: dnsServers) {
511 addValidatedPrivateDnsServer(dnsServer);
512 }
513 }
514
515 /**
516 * Returns all the {@link InetAddress} for validated private DNS servers on this link.
517 * These are resolved from the private DNS server name.
518 *
519 * @return An umodifiable {@link List} of {@link InetAddress} for validated private
520 * DNS servers on this link.
521 * @hide
522 */
523 public List<InetAddress> getValidatedPrivateDnsServers() {
524 return Collections.unmodifiableList(mValidatedPrivateDnses);
525 }
526
527 /**
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700528 * Sets the DNS domain search path used on this link.
529 *
530 * @param domains A {@link String} listing in priority order the comma separated
531 * domains to search when resolving host names on this link.
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700532 * @hide
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700533 */
paulhu7610bc72018-12-12 17:52:57 +0800534 @SystemApi
Robert Greenwalt8058f622012-11-09 10:52:27 -0800535 public void setDomains(String domains) {
536 mDomains = domains;
537 }
538
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700539 /**
540 * Get the DNS domains search path set for this link.
541 *
542 * @return A {@link String} containing the comma separated domains to search when resolving
543 * host names on this link.
544 */
545 public String getDomains() {
546 return mDomains;
547 }
548
549 /**
550 * Sets the Maximum Transmission Unit size to use on this link. This should not be used
551 * unless the system default (1500) is incorrect. Values less than 68 or greater than
552 * 10000 will be ignored.
553 *
554 * @param mtu The MTU to use for this link.
555 * @hide
556 */
paulhu7610bc72018-12-12 17:52:57 +0800557 @SystemApi
sy.yun9d9b74a2013-09-02 05:24:09 +0900558 public void setMtu(int mtu) {
559 mMtu = mtu;
560 }
561
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700562 /**
563 * Gets any non-default MTU size set for this link. Note that if the default is being used
564 * this will return 0.
565 *
566 * @return The mtu value set for this link.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700567 */
sy.yun9d9b74a2013-09-02 05:24:09 +0900568 public int getMtu() {
569 return mMtu;
570 }
571
Robert Greenwalt3f05bf42014-08-06 12:00:25 -0700572 /**
573 * Sets the tcp buffers sizes to be used when this link is the system default.
574 * Should be of the form "rmem_min,rmem_def,rmem_max,wmem_min,wmem_def,wmem_max".
575 *
576 * @param tcpBufferSizes The tcp buffers sizes to use.
577 *
578 * @hide
579 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100580 @UnsupportedAppUsage
Robert Greenwalt3f05bf42014-08-06 12:00:25 -0700581 public void setTcpBufferSizes(String tcpBufferSizes) {
582 mTcpBufferSizes = tcpBufferSizes;
583 }
584
585 /**
586 * Gets the tcp buffer sizes.
587 *
588 * @return the tcp buffer sizes to use when this link is the system default.
589 *
590 * @hide
591 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100592 @UnsupportedAppUsage
Robert Greenwalt3f05bf42014-08-06 12:00:25 -0700593 public String getTcpBufferSizes() {
594 return mTcpBufferSizes;
595 }
596
Lorenzo Colitti45b9a5b2013-03-08 11:30:39 -0800597 private RouteInfo routeWithInterface(RouteInfo route) {
598 return new RouteInfo(
599 route.getDestination(),
600 route.getGateway(),
Lorenzo Colitti4b0f8e62014-09-19 01:49:05 +0900601 mIfaceName,
602 route.getType());
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700603 }
Lorenzo Colitti45b9a5b2013-03-08 11:30:39 -0800604
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700605 /**
Lorenzo Colittic17a1b92014-06-12 23:10:17 +0900606 * Adds a {@link RouteInfo} to this {@code LinkProperties}, if not present. If the
607 * {@link RouteInfo} had an interface name set and that differs from the interface set for this
608 * {@code LinkProperties} an {@link IllegalArgumentException} will be thrown. The proper
609 * course is to add either un-named or properly named {@link RouteInfo}.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700610 *
611 * @param route A {@link RouteInfo} to add to this object.
Lorenzo Colittic17a1b92014-06-12 23:10:17 +0900612 * @return {@code false} if the route was already present, {@code true} if it was added.
613 *
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700614 * @hide
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700615 */
paulhu7610bc72018-12-12 17:52:57 +0800616 @SystemApi
Lorenzo Colittic17a1b92014-06-12 23:10:17 +0900617 public boolean addRoute(RouteInfo route) {
Lorenzo Colitti45b9a5b2013-03-08 11:30:39 -0800618 if (route != null) {
619 String routeIface = route.getInterface();
620 if (routeIface != null && !routeIface.equals(mIfaceName)) {
Lorenzo Colitti1994bc12013-03-08 19:11:40 -0800621 throw new IllegalArgumentException(
Lorenzo Colitti45b9a5b2013-03-08 11:30:39 -0800622 "Route added with non-matching interface: " + routeIface +
Lorenzo Colitti1994bc12013-03-08 19:11:40 -0800623 " vs. " + mIfaceName);
Lorenzo Colitti45b9a5b2013-03-08 11:30:39 -0800624 }
Lorenzo Colittic17a1b92014-06-12 23:10:17 +0900625 route = routeWithInterface(route);
626 if (!mRoutes.contains(route)) {
627 mRoutes.add(route);
628 return true;
629 }
Lorenzo Colitti45b9a5b2013-03-08 11:30:39 -0800630 }
Lorenzo Colittic17a1b92014-06-12 23:10:17 +0900631 return false;
632 }
633
634 /**
635 * Removes a {@link RouteInfo} from this {@code LinkProperties}, if present. The route must
636 * specify an interface and the interface must match the interface of this
637 * {@code LinkProperties}, or it will not be removed.
638 *
639 * @return {@code true} if the route was removed, {@code false} if it was not present.
640 *
641 * @hide
642 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100643 @UnsupportedAppUsage
Lorenzo Colittic17a1b92014-06-12 23:10:17 +0900644 public boolean removeRoute(RouteInfo route) {
645 return route != null &&
646 Objects.equals(mIfaceName, route.getInterface()) &&
647 mRoutes.remove(route);
Lorenzo Colitti45b9a5b2013-03-08 11:30:39 -0800648 }
649
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800650 /**
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700651 * Returns all the {@link RouteInfo} set on this link.
652 *
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700653 * @return An unmodifiable {@link List} of {@link RouteInfo} for this link.
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800654 */
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700655 public List<RouteInfo> getRoutes() {
656 return Collections.unmodifiableList(mRoutes);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700657 }
658
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800659 /**
Rubin Xu1bb5c082017-09-05 18:40:49 +0100660 * Make sure this LinkProperties instance contains routes that cover the local subnet
661 * of its link addresses. Add any route that is missing.
662 * @hide
663 */
664 public void ensureDirectlyConnectedRoutes() {
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900665 for (LinkAddress addr : mLinkAddresses) {
Rubin Xu1bb5c082017-09-05 18:40:49 +0100666 addRoute(new RouteInfo(addr, null, mIfaceName));
667 }
668 }
669
670 /**
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800671 * Returns all the routes on this link and all the links stacked above it.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700672 * @hide
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800673 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100674 @UnsupportedAppUsage
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700675 public List<RouteInfo> getAllRoutes() {
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900676 List<RouteInfo> routes = new ArrayList<>(mRoutes);
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800677 for (LinkProperties stacked: mStackedLinks.values()) {
678 routes.addAll(stacked.getAllRoutes());
679 }
Robert Greenwalt6629bcd2013-03-15 11:28:50 -0700680 return routes;
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800681 }
682
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700683 /**
684 * Sets the recommended {@link ProxyInfo} to use on this link, or {@code null} for none.
685 * Note that Http Proxies are only a hint - the system recommends their use, but it does
686 * not enforce it and applications may ignore them.
687 *
Erik Klineb36a3132015-06-26 19:21:34 +0900688 * @param proxy A {@link ProxyInfo} defining the HTTP Proxy to use on this link.
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700689 * @hide
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700690 */
paulhu7610bc72018-12-12 17:52:57 +0800691 @SystemApi
Jason Monk207900c2014-04-25 15:00:09 -0400692 public void setHttpProxy(ProxyInfo proxy) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700693 mHttpProxy = proxy;
694 }
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700695
696 /**
697 * Gets the recommended {@link ProxyInfo} (or {@code null}) set on this link.
698 *
699 * @return The {@link ProxyInfo} set on this link
700 */
Jason Monk207900c2014-04-25 15:00:09 -0400701 public ProxyInfo getHttpProxy() {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700702 return mHttpProxy;
703 }
704
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800705 /**
706 * Adds a stacked link.
707 *
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900708 * If there is already a stacked link with the same interface name as link,
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800709 * that link is replaced with link. Otherwise, link is added to the list
710 * of stacked links. If link is null, nothing changes.
711 *
712 * @param link The link to add.
Lorenzo Colitti4faa0272013-08-08 11:00:12 +0900713 * @return true if the link was stacked, false otherwise.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700714 * @hide
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800715 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100716 @UnsupportedAppUsage
Lorenzo Colitti4faa0272013-08-08 11:00:12 +0900717 public boolean addStackedLink(LinkProperties link) {
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800718 if (link != null && link.getInterfaceName() != null) {
719 mStackedLinks.put(link.getInterfaceName(), link);
Lorenzo Colitti4faa0272013-08-08 11:00:12 +0900720 return true;
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800721 }
Lorenzo Colitti4faa0272013-08-08 11:00:12 +0900722 return false;
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800723 }
724
725 /**
726 * Removes a stacked link.
727 *
Lorenzo Colittif3cab632014-10-20 11:08:03 +0900728 * If there is a stacked link with the given interface name, it is
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800729 * removed. Otherwise, nothing changes.
730 *
Lorenzo Colittif3cab632014-10-20 11:08:03 +0900731 * @param iface The interface name of the link to remove.
Lorenzo Colitti4faa0272013-08-08 11:00:12 +0900732 * @return true if the link was removed, false otherwise.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700733 * @hide
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800734 */
Lorenzo Colittif3cab632014-10-20 11:08:03 +0900735 public boolean removeStackedLink(String iface) {
736 if (iface != null) {
737 LinkProperties removed = mStackedLinks.remove(iface);
Lorenzo Colitti4faa0272013-08-08 11:00:12 +0900738 return removed != null;
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800739 }
Lorenzo Colitti4faa0272013-08-08 11:00:12 +0900740 return false;
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800741 }
742
743 /**
744 * Returns all the links stacked on top of this link.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700745 * @hide
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800746 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100747 @UnsupportedAppUsage
Jeff Sharkeyeb2c2c72014-08-11 15:22:51 -0700748 public @NonNull List<LinkProperties> getStackedLinks() {
749 if (mStackedLinks.isEmpty()) {
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900750 return Collections.emptyList();
Jeff Sharkeyeb2c2c72014-08-11 15:22:51 -0700751 }
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900752 List<LinkProperties> stacked = new ArrayList<>();
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800753 for (LinkProperties link : mStackedLinks.values()) {
Jeff Sharkeyeb2c2c72014-08-11 15:22:51 -0700754 stacked.add(new LinkProperties(link));
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800755 }
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700756 return Collections.unmodifiableList(stacked);
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800757 }
758
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700759 /**
760 * Clears this object to its initial state.
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700761 * @hide
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700762 */
paulhu7610bc72018-12-12 17:52:57 +0800763 @SystemApi
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700764 public void clear() {
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700765 mIfaceName = null;
Wink Savillee8222252011-07-13 13:44:13 -0700766 mLinkAddresses.clear();
767 mDnses.clear();
dalykd9201342018-01-17 14:20:55 -0500768 mUsePrivateDns = false;
769 mPrivateDnsServerName = null;
Robert Greenwalt8058f622012-11-09 10:52:27 -0800770 mDomains = null;
Wink Savillee8222252011-07-13 13:44:13 -0700771 mRoutes.clear();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700772 mHttpProxy = null;
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800773 mStackedLinks.clear();
sy.yun9d9b74a2013-09-02 05:24:09 +0900774 mMtu = 0;
Robert Greenwalt3f05bf42014-08-06 12:00:25 -0700775 mTcpBufferSizes = null;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700776 }
777
778 /**
779 * Implement the Parcelable interface
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700780 */
781 public int describeContents() {
782 return 0;
783 }
784
Wink Saville1f6408a2010-08-27 11:15:18 -0700785 @Override
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700786 public String toString() {
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900787 // Space as a separator, so no need for spaces at start/end of the individual fragments.
788 final StringJoiner resultJoiner = new StringJoiner(" ", "{", "}");
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700789
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900790 if (mIfaceName != null) {
791 resultJoiner.add("InterfaceName:");
792 resultJoiner.add(mIfaceName);
793 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700794
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900795 resultJoiner.add("LinkAddresses: [");
796 if (!mLinkAddresses.isEmpty()) {
797 resultJoiner.add(TextUtils.join(",", mLinkAddresses));
798 }
799 resultJoiner.add("]");
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700800
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900801 resultJoiner.add("DnsAddresses: [");
802 if (!mDnses.isEmpty()) {
803 resultJoiner.add(TextUtils.join(",", mDnses));
804 }
805 resultJoiner.add("]");
dalykd9201342018-01-17 14:20:55 -0500806
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900807 if (mUsePrivateDns) {
808 resultJoiner.add("UsePrivateDns: true");
809 }
810
Chalard Jeanfaaf2fe2018-06-07 13:28:09 +0900811 if (mPrivateDnsServerName != null) {
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900812 resultJoiner.add("PrivateDnsServerName:");
813 resultJoiner.add(mPrivateDnsServerName);
dalykd9201342018-01-17 14:20:55 -0500814 }
815
Chalard Jean03dbf6b2018-04-11 16:36:41 +0900816 if (!mValidatedPrivateDnses.isEmpty()) {
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900817 final StringJoiner validatedPrivateDnsesJoiner =
818 new StringJoiner(",", "ValidatedPrivateDnsAddresses: [", "]");
819 for (final InetAddress addr : mValidatedPrivateDnses) {
820 validatedPrivateDnsesJoiner.add(addr.getHostAddress());
Chalard Jean03dbf6b2018-04-11 16:36:41 +0900821 }
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900822 resultJoiner.add(validatedPrivateDnsesJoiner.toString());
Chalard Jean03dbf6b2018-04-11 16:36:41 +0900823 }
824
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900825 resultJoiner.add("Domains:");
826 resultJoiner.add(mDomains);
Robert Greenwalt8058f622012-11-09 10:52:27 -0800827
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900828 resultJoiner.add("MTU:");
829 resultJoiner.add(Integer.toString(mMtu));
sy.yun9d9b74a2013-09-02 05:24:09 +0900830
Robert Greenwalt3f05bf42014-08-06 12:00:25 -0700831 if (mTcpBufferSizes != null) {
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900832 resultJoiner.add("TcpBufferSizes:");
833 resultJoiner.add(mTcpBufferSizes);
Robert Greenwalt3f05bf42014-08-06 12:00:25 -0700834 }
835
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900836 resultJoiner.add("Routes: [");
837 if (!mRoutes.isEmpty()) {
838 resultJoiner.add(TextUtils.join(",", mRoutes));
839 }
840 resultJoiner.add("]");
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700841
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900842 if (mHttpProxy != null) {
843 resultJoiner.add("HttpProxy:");
844 resultJoiner.add(mHttpProxy.toString());
845 }
846
847 final Collection<LinkProperties> stackedLinksValues = mStackedLinks.values();
848 if (!stackedLinksValues.isEmpty()) {
849 final StringJoiner stackedLinksJoiner = new StringJoiner(",", "Stacked: [", "]");
850 for (final LinkProperties lp : stackedLinksValues) {
851 stackedLinksJoiner.add("[ " + lp + " ]");
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800852 }
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900853 resultJoiner.add(stackedLinksJoiner.toString());
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800854 }
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +0900855
856 return resultJoiner.toString();
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800857 }
858
859 /**
860 * Returns true if this link has an IPv4 address.
861 *
862 * @return {@code true} if there is an IPv4 address, {@code false} otherwise.
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700863 * @hide
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800864 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100865 @UnsupportedAppUsage
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800866 public boolean hasIPv4Address() {
867 for (LinkAddress address : mLinkAddresses) {
Hugo Benichibd87a392017-10-10 16:29:06 +0900868 if (address.getAddress() instanceof Inet4Address) {
869 return true;
870 }
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800871 }
872 return false;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700873 }
874
Wink Savillee8222252011-07-13 13:44:13 -0700875 /**
Lorenzo Colitti87cfc702015-07-27 16:35:33 +0900876 * Returns true if this link or any of its stacked interfaces has an IPv4 address.
877 *
878 * @return {@code true} if there is an IPv4 address, {@code false} otherwise.
879 */
880 private boolean hasIPv4AddressOnInterface(String iface) {
Lorenzo Colitti89b63922015-07-30 23:41:43 +0900881 // mIfaceName can be null.
882 return (Objects.equals(iface, mIfaceName) && hasIPv4Address()) ||
Lorenzo Colitti87cfc702015-07-27 16:35:33 +0900883 (iface != null && mStackedLinks.containsKey(iface) &&
884 mStackedLinks.get(iface).hasIPv4Address());
885 }
886
887 /**
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +0900888 * Returns true if this link has a global preferred IPv6 address.
Lorenzo Colitti4faa0272013-08-08 11:00:12 +0900889 *
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +0900890 * @return {@code true} if there is a global preferred IPv6 address, {@code false} otherwise.
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700891 * @hide
Lorenzo Colitti4faa0272013-08-08 11:00:12 +0900892 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100893 @UnsupportedAppUsage
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +0900894 public boolean hasGlobalIPv6Address() {
Lorenzo Colitti4faa0272013-08-08 11:00:12 +0900895 for (LinkAddress address : mLinkAddresses) {
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +0900896 if (address.getAddress() instanceof Inet6Address && address.isGlobalPreferred()) {
Lorenzo Colitti4faa0272013-08-08 11:00:12 +0900897 return true;
898 }
899 }
900 return false;
901 }
902
903 /**
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +0900904 * Returns true if this link has an IPv4 default route.
905 *
906 * @return {@code true} if there is an IPv4 default route, {@code false} otherwise.
907 * @hide
908 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100909 @UnsupportedAppUsage
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +0900910 public boolean hasIPv4DefaultRoute() {
911 for (RouteInfo r : mRoutes) {
Hugo Benichibd87a392017-10-10 16:29:06 +0900912 if (r.isIPv4Default()) {
913 return true;
914 }
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +0900915 }
916 return false;
917 }
918
919 /**
920 * Returns true if this link has an IPv6 default route.
921 *
922 * @return {@code true} if there is an IPv6 default route, {@code false} otherwise.
923 * @hide
924 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100925 @UnsupportedAppUsage
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +0900926 public boolean hasIPv6DefaultRoute() {
927 for (RouteInfo r : mRoutes) {
Hugo Benichibd87a392017-10-10 16:29:06 +0900928 if (r.isIPv6Default()) {
929 return true;
930 }
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +0900931 }
932 return false;
933 }
934
935 /**
936 * Returns true if this link has an IPv4 DNS server.
937 *
938 * @return {@code true} if there is an IPv4 DNS server, {@code false} otherwise.
939 * @hide
940 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100941 @UnsupportedAppUsage
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +0900942 public boolean hasIPv4DnsServer() {
943 for (InetAddress ia : mDnses) {
Hugo Benichibd87a392017-10-10 16:29:06 +0900944 if (ia instanceof Inet4Address) {
945 return true;
946 }
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +0900947 }
948 return false;
949 }
950
951 /**
952 * Returns true if this link has an IPv6 DNS server.
953 *
954 * @return {@code true} if there is an IPv6 DNS server, {@code false} otherwise.
955 * @hide
956 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100957 @UnsupportedAppUsage
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +0900958 public boolean hasIPv6DnsServer() {
959 for (InetAddress ia : mDnses) {
Hugo Benichibd87a392017-10-10 16:29:06 +0900960 if (ia instanceof Inet6Address) {
961 return true;
962 }
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +0900963 }
964 return false;
965 }
966
967 /**
Erik Klined3b9fd32014-10-24 21:50:20 +0900968 * Returns true if this link is provisioned for global IPv4 connectivity.
969 * This requires an IP address, default route, and DNS server.
970 *
971 * @return {@code true} if the link is provisioned, {@code false} otherwise.
Erik Klinecd7ed162015-05-21 16:15:02 +0900972 * @hide
Erik Klined3b9fd32014-10-24 21:50:20 +0900973 */
Erik Klinecd7ed162015-05-21 16:15:02 +0900974 public boolean isIPv4Provisioned() {
Erik Klined3b9fd32014-10-24 21:50:20 +0900975 return (hasIPv4Address() &&
976 hasIPv4DefaultRoute() &&
977 hasIPv4DnsServer());
978 }
979
980 /**
981 * Returns true if this link is provisioned for global IPv6 connectivity.
982 * This requires an IP address, default route, and DNS server.
983 *
984 * @return {@code true} if the link is provisioned, {@code false} otherwise.
Erik Klinecd7ed162015-05-21 16:15:02 +0900985 * @hide
Erik Klined3b9fd32014-10-24 21:50:20 +0900986 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100987 @UnsupportedAppUsage
Erik Klinecd7ed162015-05-21 16:15:02 +0900988 public boolean isIPv6Provisioned() {
Erik Klined3b9fd32014-10-24 21:50:20 +0900989 return (hasGlobalIPv6Address() &&
990 hasIPv6DefaultRoute() &&
991 hasIPv6DnsServer());
992 }
993
994 /**
995 * Returns true if this link is provisioned for global connectivity,
996 * for at least one Internet Protocol family.
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +0900997 *
998 * @return {@code true} if the link is provisioned, {@code false} otherwise.
999 * @hide
1000 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001001 @UnsupportedAppUsage
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +09001002 public boolean isProvisioned() {
Erik Klinecd7ed162015-05-21 16:15:02 +09001003 return (isIPv4Provisioned() || isIPv6Provisioned());
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +09001004 }
1005
1006 /**
Erik Klineb36a3132015-06-26 19:21:34 +09001007 * Evaluate whether the {@link InetAddress} is considered reachable.
1008 *
1009 * @return {@code true} if the given {@link InetAddress} is considered reachable,
1010 * {@code false} otherwise.
1011 * @hide
1012 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001013 @UnsupportedAppUsage
Erik Klineb36a3132015-06-26 19:21:34 +09001014 public boolean isReachable(InetAddress ip) {
1015 final List<RouteInfo> allRoutes = getAllRoutes();
1016 // If we don't have a route to this IP address, it's not reachable.
1017 final RouteInfo bestRoute = RouteInfo.selectBestRoute(allRoutes, ip);
1018 if (bestRoute == null) {
1019 return false;
1020 }
1021
1022 // TODO: better source address evaluation for destination addresses.
1023
1024 if (ip instanceof Inet4Address) {
1025 // For IPv4, it suffices for now to simply have any address.
Lorenzo Colitti87cfc702015-07-27 16:35:33 +09001026 return hasIPv4AddressOnInterface(bestRoute.getInterface());
Erik Klineb36a3132015-06-26 19:21:34 +09001027 } else if (ip instanceof Inet6Address) {
1028 if (ip.isLinkLocalAddress()) {
1029 // For now, just make sure link-local destinations have
1030 // scopedIds set, since transmits will generally fail otherwise.
1031 // TODO: verify it matches the ifindex of one of the interfaces.
1032 return (((Inet6Address)ip).getScopeId() != 0);
1033 } else {
1034 // For non-link-local destinations check that either the best route
1035 // is directly connected or that some global preferred address exists.
1036 // TODO: reconsider all cases (disconnected ULA networks, ...).
1037 return (!bestRoute.hasGateway() || hasGlobalIPv6Address());
1038 }
1039 }
1040
1041 return false;
1042 }
1043
1044 /**
Wink Savillee8222252011-07-13 13:44:13 -07001045 * Compares this {@code LinkProperties} interface name against the target
1046 *
1047 * @param target LinkProperties to compare.
1048 * @return {@code true} if both are identical, {@code false} otherwise.
Robert Greenwalt4f05d552014-05-18 22:01:38 -07001049 * @hide
Wink Savillee8222252011-07-13 13:44:13 -07001050 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001051 @UnsupportedAppUsage
Wink Savillee8222252011-07-13 13:44:13 -07001052 public boolean isIdenticalInterfaceName(LinkProperties target) {
1053 return TextUtils.equals(getInterfaceName(), target.getInterfaceName());
1054 }
1055
1056 /**
Robert Greenwalt4717c262012-10-31 14:32:53 -07001057 * Compares this {@code LinkProperties} interface addresses against the target
Wink Savillee8222252011-07-13 13:44:13 -07001058 *
1059 * @param target LinkProperties to compare.
1060 * @return {@code true} if both are identical, {@code false} otherwise.
Robert Greenwalt4f05d552014-05-18 22:01:38 -07001061 * @hide
Wink Savillee8222252011-07-13 13:44:13 -07001062 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001063 @UnsupportedAppUsage
Wink Savillee8222252011-07-13 13:44:13 -07001064 public boolean isIdenticalAddresses(LinkProperties target) {
1065 Collection<InetAddress> targetAddresses = target.getAddresses();
1066 Collection<InetAddress> sourceAddresses = getAddresses();
1067 return (sourceAddresses.size() == targetAddresses.size()) ?
1068 sourceAddresses.containsAll(targetAddresses) : false;
1069 }
1070
1071 /**
1072 * Compares this {@code LinkProperties} DNS addresses against the target
1073 *
1074 * @param target LinkProperties to compare.
1075 * @return {@code true} if both are identical, {@code false} otherwise.
Robert Greenwalt4f05d552014-05-18 22:01:38 -07001076 * @hide
Wink Savillee8222252011-07-13 13:44:13 -07001077 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001078 @UnsupportedAppUsage
Wink Savillee8222252011-07-13 13:44:13 -07001079 public boolean isIdenticalDnses(LinkProperties target) {
Robert Greenwaltdf2b8782014-06-06 10:30:11 -07001080 Collection<InetAddress> targetDnses = target.getDnsServers();
Robert Greenwalt8058f622012-11-09 10:52:27 -08001081 String targetDomains = target.getDomains();
1082 if (mDomains == null) {
1083 if (targetDomains != null) return false;
1084 } else {
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +09001085 if (!mDomains.equals(targetDomains)) return false;
Robert Greenwalt8058f622012-11-09 10:52:27 -08001086 }
Wink Savillee8222252011-07-13 13:44:13 -07001087 return (mDnses.size() == targetDnses.size()) ?
Chalard Jean03dbf6b2018-04-11 16:36:41 +09001088 mDnses.containsAll(targetDnses) : false;
Wink Savillee8222252011-07-13 13:44:13 -07001089 }
1090
1091 /**
dalykd9201342018-01-17 14:20:55 -05001092 * Compares this {@code LinkProperties} private DNS settings against the
1093 * target.
1094 *
1095 * @param target LinkProperties to compare.
1096 * @return {@code true} if both are identical, {@code false} otherwise.
1097 * @hide
1098 */
1099 public boolean isIdenticalPrivateDns(LinkProperties target) {
1100 return (isPrivateDnsActive() == target.isPrivateDnsActive()
1101 && TextUtils.equals(getPrivateDnsServerName(),
1102 target.getPrivateDnsServerName()));
1103 }
1104
1105 /**
Chalard Jean03dbf6b2018-04-11 16:36:41 +09001106 * Compares this {@code LinkProperties} validated private DNS addresses against
1107 * the target
1108 *
1109 * @param target LinkProperties to compare.
1110 * @return {@code true} if both are identical, {@code false} otherwise.
1111 * @hide
1112 */
1113 public boolean isIdenticalValidatedPrivateDnses(LinkProperties target) {
1114 Collection<InetAddress> targetDnses = target.getValidatedPrivateDnsServers();
1115 return (mValidatedPrivateDnses.size() == targetDnses.size())
1116 ? mValidatedPrivateDnses.containsAll(targetDnses) : false;
1117 }
1118
1119 /**
Wink Savillee8222252011-07-13 13:44:13 -07001120 * Compares this {@code LinkProperties} Routes against the target
1121 *
1122 * @param target LinkProperties to compare.
1123 * @return {@code true} if both are identical, {@code false} otherwise.
Robert Greenwalt4f05d552014-05-18 22:01:38 -07001124 * @hide
Wink Savillee8222252011-07-13 13:44:13 -07001125 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001126 @UnsupportedAppUsage
Wink Savillee8222252011-07-13 13:44:13 -07001127 public boolean isIdenticalRoutes(LinkProperties target) {
1128 Collection<RouteInfo> targetRoutes = target.getRoutes();
1129 return (mRoutes.size() == targetRoutes.size()) ?
Chalard Jean03dbf6b2018-04-11 16:36:41 +09001130 mRoutes.containsAll(targetRoutes) : false;
Wink Savillee8222252011-07-13 13:44:13 -07001131 }
1132
1133 /**
1134 * Compares this {@code LinkProperties} HttpProxy against the target
1135 *
1136 * @param target LinkProperties to compare.
1137 * @return {@code true} if both are identical, {@code false} otherwise.
Robert Greenwalt4f05d552014-05-18 22:01:38 -07001138 * @hide
Wink Savillee8222252011-07-13 13:44:13 -07001139 */
Mathew Inwood31755f92018-12-20 13:53:36 +00001140 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Wink Savillee8222252011-07-13 13:44:13 -07001141 public boolean isIdenticalHttpProxy(LinkProperties target) {
1142 return getHttpProxy() == null ? target.getHttpProxy() == null :
Chalard Jean03dbf6b2018-04-11 16:36:41 +09001143 getHttpProxy().equals(target.getHttpProxy());
Wink Savillee8222252011-07-13 13:44:13 -07001144 }
John Wang4e900092011-04-04 12:35:42 -07001145
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -08001146 /**
1147 * Compares this {@code LinkProperties} stacked links against the target
1148 *
1149 * @param target LinkProperties to compare.
1150 * @return {@code true} if both are identical, {@code false} otherwise.
Robert Greenwalt4f05d552014-05-18 22:01:38 -07001151 * @hide
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -08001152 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001153 @UnsupportedAppUsage
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -08001154 public boolean isIdenticalStackedLinks(LinkProperties target) {
Lorenzo Colitti213f98b2013-04-01 10:47:43 +09001155 if (!mStackedLinks.keySet().equals(target.mStackedLinks.keySet())) {
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -08001156 return false;
1157 }
1158 for (LinkProperties stacked : mStackedLinks.values()) {
1159 // Hashtable values can never be null.
1160 String iface = stacked.getInterfaceName();
1161 if (!stacked.equals(target.mStackedLinks.get(iface))) {
1162 return false;
1163 }
1164 }
1165 return true;
1166 }
1167
sy.yun9d9b74a2013-09-02 05:24:09 +09001168 /**
1169 * Compares this {@code LinkProperties} MTU against the target
1170 *
Ying Wangd57de6a2013-09-06 22:53:16 -07001171 * @param target LinkProperties to compare.
sy.yun9d9b74a2013-09-02 05:24:09 +09001172 * @return {@code true} if both are identical, {@code false} otherwise.
Robert Greenwalt4f05d552014-05-18 22:01:38 -07001173 * @hide
sy.yun9d9b74a2013-09-02 05:24:09 +09001174 */
1175 public boolean isIdenticalMtu(LinkProperties target) {
1176 return getMtu() == target.getMtu();
1177 }
1178
Robert Greenwalt3f05bf42014-08-06 12:00:25 -07001179 /**
1180 * Compares this {@code LinkProperties} Tcp buffer sizes against the target.
1181 *
1182 * @param target LinkProperties to compare.
1183 * @return {@code true} if both are identical, {@code false} otherwise.
1184 * @hide
1185 */
1186 public boolean isIdenticalTcpBufferSizes(LinkProperties target) {
1187 return Objects.equals(mTcpBufferSizes, target.mTcpBufferSizes);
1188 }
1189
John Wang4e900092011-04-04 12:35:42 -07001190 /**
1191 * Compares this {@code LinkProperties} instance against the target
1192 * LinkProperties in {@code obj}. Two LinkPropertieses are equal if
1193 * all their fields are equal in values.
1194 *
1195 * For collection fields, such as mDnses, containsAll() is used to check
1196 * if two collections contains the same elements, independent of order.
1197 * There are two thoughts regarding containsAll()
1198 * 1. Duplicated elements. eg, (A, B, B) and (A, A, B) are equal.
1199 * 2. Worst case performance is O(n^2).
1200 *
1201 * @param obj the object to be tested for equality.
1202 * @return {@code true} if both objects are equal, {@code false} otherwise.
1203 */
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +09001204 @Override
John Wang4e900092011-04-04 12:35:42 -07001205 public boolean equals(Object obj) {
1206 if (this == obj) return true;
1207
1208 if (!(obj instanceof LinkProperties)) return false;
1209
John Wang4e900092011-04-04 12:35:42 -07001210 LinkProperties target = (LinkProperties) obj;
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +09001211 /*
Robert Greenwalt4f05d552014-05-18 22:01:38 -07001212 * This method does not check that stacked interfaces are equal, because
1213 * stacked interfaces are not so much a property of the link as a
1214 * description of connections between links.
1215 */
dalykd9201342018-01-17 14:20:55 -05001216 return isIdenticalInterfaceName(target)
1217 && isIdenticalAddresses(target)
1218 && isIdenticalDnses(target)
1219 && isIdenticalPrivateDns(target)
Chalard Jean03dbf6b2018-04-11 16:36:41 +09001220 && isIdenticalValidatedPrivateDnses(target)
dalykd9201342018-01-17 14:20:55 -05001221 && isIdenticalRoutes(target)
1222 && isIdenticalHttpProxy(target)
1223 && isIdenticalStackedLinks(target)
1224 && isIdenticalMtu(target)
1225 && isIdenticalTcpBufferSizes(target);
Wink Savillee8222252011-07-13 13:44:13 -07001226 }
John Wang4e900092011-04-04 12:35:42 -07001227
Wink Savillee8222252011-07-13 13:44:13 -07001228 /**
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +09001229 * Compares the addresses in this LinkProperties with another
1230 * LinkProperties, examining only addresses on the base link.
Wink Savillee8222252011-07-13 13:44:13 -07001231 *
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +09001232 * @param target a LinkProperties with the new list of addresses
1233 * @return the differences between the addresses.
Robert Greenwalt4f05d552014-05-18 22:01:38 -07001234 * @hide
Wink Savillee8222252011-07-13 13:44:13 -07001235 */
Robert Greenwalt0a46db52011-07-14 14:28:05 -07001236 public CompareResult<LinkAddress> compareAddresses(LinkProperties target) {
Wink Savillee8222252011-07-13 13:44:13 -07001237 /*
1238 * Duplicate the LinkAddresses into removed, we will be removing
1239 * address which are common between mLinkAddresses and target
1240 * leaving the addresses that are different. And address which
1241 * are in target but not in mLinkAddresses are placed in the
1242 * addedAddresses.
1243 */
Rubin Xu2fc72f72017-08-22 16:35:52 +01001244 return new CompareResult<>(mLinkAddresses,
1245 target != null ? target.getLinkAddresses() : null);
John Wang4e900092011-04-04 12:35:42 -07001246 }
1247
Robert Greenwalt0a46db52011-07-14 14:28:05 -07001248 /**
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +09001249 * Compares the DNS addresses in this LinkProperties with another
1250 * LinkProperties, examining only DNS addresses on the base link.
Robert Greenwalt0a46db52011-07-14 14:28:05 -07001251 *
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +09001252 * @param target a LinkProperties with the new list of dns addresses
1253 * @return the differences between the DNS addresses.
Robert Greenwalt4f05d552014-05-18 22:01:38 -07001254 * @hide
Robert Greenwalt0a46db52011-07-14 14:28:05 -07001255 */
1256 public CompareResult<InetAddress> compareDnses(LinkProperties target) {
1257 /*
1258 * Duplicate the InetAddresses into removed, we will be removing
1259 * dns address which are common between mDnses and target
1260 * leaving the addresses that are different. And dns address which
1261 * are in target but not in mDnses are placed in the
1262 * addedAddresses.
1263 */
Rubin Xu2fc72f72017-08-22 16:35:52 +01001264 return new CompareResult<>(mDnses, target != null ? target.getDnsServers() : null);
Robert Greenwalt0a46db52011-07-14 14:28:05 -07001265 }
1266
1267 /**
Chalard Jean03dbf6b2018-04-11 16:36:41 +09001268 * Compares the validated private DNS addresses in this LinkProperties with another
1269 * LinkProperties.
1270 *
1271 * @param target a LinkProperties with the new list of validated private dns addresses
1272 * @return the differences between the DNS addresses.
1273 * @hide
1274 */
1275 public CompareResult<InetAddress> compareValidatedPrivateDnses(LinkProperties target) {
1276 return new CompareResult<>(mValidatedPrivateDnses,
1277 target != null ? target.getValidatedPrivateDnsServers() : null);
1278 }
1279
1280 /**
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +09001281 * Compares all routes in this LinkProperties with another LinkProperties,
1282 * examining both the the base link and all stacked links.
Robert Greenwalt0a46db52011-07-14 14:28:05 -07001283 *
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +09001284 * @param target a LinkProperties with the new list of routes
1285 * @return the differences between the routes.
Robert Greenwalt4f05d552014-05-18 22:01:38 -07001286 * @hide
Robert Greenwalt0a46db52011-07-14 14:28:05 -07001287 */
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +09001288 public CompareResult<RouteInfo> compareAllRoutes(LinkProperties target) {
Robert Greenwalt0a46db52011-07-14 14:28:05 -07001289 /*
1290 * Duplicate the RouteInfos into removed, we will be removing
Lorenzo Colitti1994bc12013-03-08 19:11:40 -08001291 * routes which are common between mRoutes and target
Robert Greenwalt0a46db52011-07-14 14:28:05 -07001292 * leaving the routes that are different. And route address which
1293 * are in target but not in mRoutes are placed in added.
1294 */
Rubin Xu2fc72f72017-08-22 16:35:52 +01001295 return new CompareResult<>(getAllRoutes(), target != null ? target.getAllRoutes() : null);
Robert Greenwalt0a46db52011-07-14 14:28:05 -07001296 }
1297
Paul Jensen992f2522014-04-28 10:33:11 -04001298 /**
1299 * Compares all interface names in this LinkProperties with another
1300 * LinkProperties, examining both the the base link and all stacked links.
1301 *
1302 * @param target a LinkProperties with the new list of interface names
1303 * @return the differences between the interface names.
1304 * @hide
1305 */
1306 public CompareResult<String> compareAllInterfaceNames(LinkProperties target) {
1307 /*
1308 * Duplicate the interface names into removed, we will be removing
1309 * interface names which are common between this and target
1310 * leaving the interface names that are different. And interface names which
1311 * are in target but not in this are placed in added.
1312 */
Rubin Xu2fc72f72017-08-22 16:35:52 +01001313 return new CompareResult<>(getAllInterfaceNames(),
1314 target != null ? target.getAllInterfaceNames() : null);
Paul Jensen992f2522014-04-28 10:33:11 -04001315 }
1316
Robert Greenwalt0a46db52011-07-14 14:28:05 -07001317
John Wang4e900092011-04-04 12:35:42 -07001318 /**
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +09001319 * Generate hashcode based on significant fields
1320 *
John Wang4e900092011-04-04 12:35:42 -07001321 * Equal objects must produce the same hash code, while unequal objects
1322 * may have the same hash codes.
1323 */
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +09001324 @Override
John Wang4e900092011-04-04 12:35:42 -07001325 public int hashCode() {
1326 return ((null == mIfaceName) ? 0 : mIfaceName.hashCode()
1327 + mLinkAddresses.size() * 31
1328 + mDnses.size() * 37
Chalard Jean03dbf6b2018-04-11 16:36:41 +09001329 + mValidatedPrivateDnses.size() * 61
Robert Greenwalt8058f622012-11-09 10:52:27 -08001330 + ((null == mDomains) ? 0 : mDomains.hashCode())
Robert Greenwaltaa70f102011-04-28 14:28:50 -07001331 + mRoutes.size() * 41
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -08001332 + ((null == mHttpProxy) ? 0 : mHttpProxy.hashCode())
sy.yun9d9b74a2013-09-02 05:24:09 +09001333 + mStackedLinks.hashCode() * 47)
Robert Greenwalt3f05bf42014-08-06 12:00:25 -07001334 + mMtu * 51
dalykd9201342018-01-17 14:20:55 -05001335 + ((null == mTcpBufferSizes) ? 0 : mTcpBufferSizes.hashCode())
1336 + (mUsePrivateDns ? 57 : 0)
1337 + ((null == mPrivateDnsServerName) ? 0 : mPrivateDnsServerName.hashCode());
John Wang4e900092011-04-04 12:35:42 -07001338 }
1339
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001340 /**
1341 * Implement the Parcelable interface.
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001342 */
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001343 public void writeToParcel(Parcel dest, int flags) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001344 dest.writeString(getInterfaceName());
Irfan Sheriffed5d7d12010-10-01 16:08:28 -07001345 dest.writeInt(mLinkAddresses.size());
Chalard Jean03dbf6b2018-04-11 16:36:41 +09001346 for (LinkAddress linkAddress : mLinkAddresses) {
Irfan Sheriffed5d7d12010-10-01 16:08:28 -07001347 dest.writeParcelable(linkAddress, flags);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001348 }
Irfan Sheriffed5d7d12010-10-01 16:08:28 -07001349
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001350 dest.writeInt(mDnses.size());
Chalard Jean03dbf6b2018-04-11 16:36:41 +09001351 for (InetAddress d : mDnses) {
1352 dest.writeByteArray(d.getAddress());
1353 }
1354 dest.writeInt(mValidatedPrivateDnses.size());
1355 for (InetAddress d : mValidatedPrivateDnses) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001356 dest.writeByteArray(d.getAddress());
1357 }
dalykd9201342018-01-17 14:20:55 -05001358 dest.writeBoolean(mUsePrivateDns);
1359 dest.writeString(mPrivateDnsServerName);
Robert Greenwalt8058f622012-11-09 10:52:27 -08001360 dest.writeString(mDomains);
sy.yun9d9b74a2013-09-02 05:24:09 +09001361 dest.writeInt(mMtu);
Robert Greenwalt3f05bf42014-08-06 12:00:25 -07001362 dest.writeString(mTcpBufferSizes);
Robert Greenwaltaa70f102011-04-28 14:28:50 -07001363 dest.writeInt(mRoutes.size());
Chalard Jean03dbf6b2018-04-11 16:36:41 +09001364 for (RouteInfo route : mRoutes) {
Robert Greenwaltaa70f102011-04-28 14:28:50 -07001365 dest.writeParcelable(route, flags);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001366 }
Robert Greenwalt992564e2011-02-09 13:56:06 -08001367
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001368 if (mHttpProxy != null) {
1369 dest.writeByte((byte)1);
1370 dest.writeParcelable(mHttpProxy, flags);
1371 } else {
1372 dest.writeByte((byte)0);
1373 }
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +09001374 ArrayList<LinkProperties> stackedLinks = new ArrayList<>(mStackedLinks.values());
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -08001375 dest.writeList(stackedLinks);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001376 }
1377
1378 /**
1379 * Implement the Parcelable interface.
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001380 */
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001381 public static final Creator<LinkProperties> CREATOR =
1382 new Creator<LinkProperties>() {
1383 public LinkProperties createFromParcel(Parcel in) {
1384 LinkProperties netProp = new LinkProperties();
Robert Greenwalt4717c262012-10-31 14:32:53 -07001385
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001386 String iface = in.readString();
1387 if (iface != null) {
Robert Greenwalt4717c262012-10-31 14:32:53 -07001388 netProp.setInterfaceName(iface);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001389 }
1390 int addressCount = in.readInt();
Chalard Jean03dbf6b2018-04-11 16:36:41 +09001391 for (int i = 0; i < addressCount; i++) {
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +09001392 netProp.addLinkAddress(in.readParcelable(null));
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001393 }
1394 addressCount = in.readInt();
Chalard Jean03dbf6b2018-04-11 16:36:41 +09001395 for (int i = 0; i < addressCount; i++) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001396 try {
Robert Greenwaltdf2b8782014-06-06 10:30:11 -07001397 netProp.addDnsServer(InetAddress.getByAddress(in.createByteArray()));
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001398 } catch (UnknownHostException e) { }
1399 }
Chalard Jean03dbf6b2018-04-11 16:36:41 +09001400 addressCount = in.readInt();
1401 for (int i = 0; i < addressCount; i++) {
1402 try {
1403 netProp.addValidatedPrivateDnsServer(
1404 InetAddress.getByAddress(in.createByteArray()));
1405 } catch (UnknownHostException e) { }
1406 }
dalykd9201342018-01-17 14:20:55 -05001407 netProp.setUsePrivateDns(in.readBoolean());
1408 netProp.setPrivateDnsServerName(in.readString());
Robert Greenwalt8058f622012-11-09 10:52:27 -08001409 netProp.setDomains(in.readString());
sy.yun9d9b74a2013-09-02 05:24:09 +09001410 netProp.setMtu(in.readInt());
Robert Greenwalt3f05bf42014-08-06 12:00:25 -07001411 netProp.setTcpBufferSizes(in.readString());
Robert Greenwalt992564e2011-02-09 13:56:06 -08001412 addressCount = in.readInt();
Chalard Jean03dbf6b2018-04-11 16:36:41 +09001413 for (int i = 0; i < addressCount; i++) {
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +09001414 netProp.addRoute(in.readParcelable(null));
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001415 }
1416 if (in.readByte() == 1) {
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +09001417 netProp.setHttpProxy(in.readParcelable(null));
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001418 }
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -08001419 ArrayList<LinkProperties> stackedLinks = new ArrayList<LinkProperties>();
1420 in.readList(stackedLinks, LinkProperties.class.getClassLoader());
1421 for (LinkProperties stackedLink: stackedLinks) {
1422 netProp.addStackedLink(stackedLink);
1423 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001424 return netProp;
1425 }
1426
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001427 public LinkProperties[] newArray(int size) {
1428 return new LinkProperties[size];
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001429 }
1430 };
w1997615afd812014-08-05 15:18:11 -07001431
Chalard Jean03dbf6b2018-04-11 16:36:41 +09001432 /**
1433 * Check the valid MTU range based on IPv4 or IPv6.
1434 * @hide
1435 */
1436 public static boolean isValidMtu(int mtu, boolean ipv6) {
1437 if (ipv6) {
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +09001438 return mtu >= MIN_MTU_V6 && mtu <= MAX_MTU;
Chalard Jean03dbf6b2018-04-11 16:36:41 +09001439 } else {
Chalard Jeanb8cfa1d2018-06-07 13:27:00 +09001440 return mtu >= MIN_MTU && mtu <= MAX_MTU;
w1997615afd812014-08-05 15:18:11 -07001441 }
Chalard Jean03dbf6b2018-04-11 16:36:41 +09001442 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001443}