blob: 4e474c8e478c0ad605b14e9bc523727f5c4236d0 [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;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070021import android.os.Parcel;
Rubin Xu1bb5c082017-09-05 18:40:49 +010022import android.os.Parcelable;
John Wang4e900092011-04-04 12:35:42 -070023import android.text.TextUtils;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070024
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -080025import java.net.Inet4Address;
Lorenzo Colitti4faa0272013-08-08 11:00:12 +090026import java.net.Inet6Address;
Rubin Xu1bb5c082017-09-05 18:40:49 +010027import java.net.InetAddress;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070028import java.net.UnknownHostException;
29import java.util.ArrayList;
30import java.util.Collection;
Robert Greenwalt37e65eb2010-08-30 10:56:47 -070031import java.util.Collections;
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -080032import java.util.Hashtable;
Robert Greenwaltdf2b8782014-06-06 10:30:11 -070033import java.util.List;
Lorenzo Colittic17a1b92014-06-12 23:10:17 +090034import java.util.Objects;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070035
36/**
Robert Greenwalt37e65eb2010-08-30 10:56:47 -070037 * Describes the properties of a network link.
Robert Greenwalt992564e2011-02-09 13:56:06 -080038 *
39 * A link represents a connection to a network.
40 * It may have multiple addresses and multiple gateways,
Robert Greenwalt4f05d552014-05-18 22:01:38 -070041 * multiple dns servers but only one http proxy and one
42 * network interface.
Robert Greenwalt992564e2011-02-09 13:56:06 -080043 *
Robert Greenwalt4f05d552014-05-18 22:01:38 -070044 * Note that this is just a holder of data. Modifying it
45 * does not affect live networks.
Robert Greenwalt992564e2011-02-09 13:56:06 -080046 *
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070047 */
Robert Greenwalte595b972014-06-12 16:24:38 -070048public final class LinkProperties implements Parcelable {
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -080049 // The interface described by the network link.
Robert Greenwalt4717c262012-10-31 14:32:53 -070050 private String mIfaceName;
Lorenzo Colitti64483942013-11-15 18:43:52 +090051 private ArrayList<LinkAddress> mLinkAddresses = new ArrayList<LinkAddress>();
52 private ArrayList<InetAddress> mDnses = new ArrayList<InetAddress>();
Robert Greenwalt8058f622012-11-09 10:52:27 -080053 private String mDomains;
Lorenzo Colitti64483942013-11-15 18:43:52 +090054 private ArrayList<RouteInfo> mRoutes = new ArrayList<RouteInfo>();
Jason Monk207900c2014-04-25 15:00:09 -040055 private ProxyInfo mHttpProxy;
sy.yun9d9b74a2013-09-02 05:24:09 +090056 private int mMtu;
Robert Greenwalt3f05bf42014-08-06 12:00:25 -070057 // in the format "rmem_min,rmem_def,rmem_max,wmem_min,wmem_def,wmem_max"
58 private String mTcpBufferSizes;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070059
w1997615afd812014-08-05 15:18:11 -070060 private static final int MIN_MTU = 68;
61 private static final int MIN_MTU_V6 = 1280;
62 private static final int MAX_MTU = 10000;
63
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -080064 // Stores the properties of links that are "stacked" above this link.
65 // Indexed by interface name to allow modification and to prevent duplicates being added.
66 private Hashtable<String, LinkProperties> mStackedLinks =
67 new Hashtable<String, LinkProperties>();
68
Robert Greenwaltdf2b8782014-06-06 10:30:11 -070069 /**
70 * @hide
71 */
Robert Greenwalt0a46db52011-07-14 14:28:05 -070072 public static class CompareResult<T> {
Rubin Xu2fc72f72017-08-22 16:35:52 +010073 public final List<T> removed = new ArrayList<T>();
74 public final List<T> added = new ArrayList<T>();
75
76 public CompareResult() {}
77
78 public CompareResult(Collection<T> oldItems, Collection<T> newItems) {
79 if (oldItems != null) {
80 removed.addAll(oldItems);
81 }
82 if (newItems != null) {
83 for (T newItem : newItems) {
84 if (!removed.remove(newItem)) {
85 added.add(newItem);
86 }
87 }
88 }
89 }
Wink Savillee8222252011-07-13 13:44:13 -070090
91 @Override
92 public String toString() {
Robert Greenwalt0a46db52011-07-14 14:28:05 -070093 String retVal = "removed=[";
94 for (T addr : removed) retVal += addr.toString() + ",";
95 retVal += "] added=[";
96 for (T addr : added) retVal += addr.toString() + ",";
Wink Savillee8222252011-07-13 13:44:13 -070097 retVal += "]";
98 return retVal;
99 }
100 }
101
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -0700102 /**
103 * @hide
104 */
Erik Klinecd7ed162015-05-21 16:15:02 +0900105 public enum ProvisioningChange {
106 STILL_NOT_PROVISIONED,
107 LOST_PROVISIONING,
108 GAINED_PROVISIONING,
109 STILL_PROVISIONED,
110 }
111
112 /**
113 * Compare the provisioning states of two LinkProperties instances.
114 *
115 * @hide
116 */
117 public static ProvisioningChange compareProvisioning(
118 LinkProperties before, LinkProperties after) {
119 if (before.isProvisioned() && after.isProvisioned()) {
120 // On dualstack networks, DHCPv4 renewals can occasionally fail.
121 // 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
131 // from a dualstack network to an IPv6-only network equivalent to
132 // 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
139 // previously dualstack network is deemed a lost of provisioning.
140 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 /**
155 * @hide
156 */
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700157 public LinkProperties() {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700158 }
159
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -0700160 /**
161 * @hide
162 */
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700163 public LinkProperties(LinkProperties source) {
Irfan Sheriffef6c1432010-08-30 20:37:17 -0700164 if (source != null) {
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700165 mIfaceName = source.getInterfaceName();
Robert Greenwalt0d8acea2011-07-28 17:21:25 -0700166 for (LinkAddress l : source.getLinkAddresses()) mLinkAddresses.add(l);
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700167 for (InetAddress i : source.getDnsServers()) mDnses.add(i);
Robert Greenwalt8058f622012-11-09 10:52:27 -0800168 mDomains = source.getDomains();
Robert Greenwalt0d8acea2011-07-28 17:21:25 -0700169 for (RouteInfo r : source.getRoutes()) mRoutes.add(r);
Wink Savillebe2b0582011-05-18 15:59:04 -0700170 mHttpProxy = (source.getHttpProxy() == null) ?
Jason Monk207900c2014-04-25 15:00:09 -0400171 null : new ProxyInfo(source.getHttpProxy());
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800172 for (LinkProperties l: source.mStackedLinks.values()) {
173 addStackedLink(l);
174 }
sy.yun9d9b74a2013-09-02 05:24:09 +0900175 setMtu(source.getMtu());
Robert Greenwalt3f05bf42014-08-06 12:00:25 -0700176 mTcpBufferSizes = source.mTcpBufferSizes;
Irfan Sheriffef6c1432010-08-30 20:37:17 -0700177 }
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700178 }
179
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700180 /**
181 * Sets the interface name for this link. All {@link RouteInfo} already set for this
182 * will have their interface changed to match this new value.
183 *
184 * @param iface The name of the network interface used for this link.
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700185 * @hide
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700186 */
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700187 public void setInterfaceName(String iface) {
188 mIfaceName = iface;
Lorenzo Colitti45b9a5b2013-03-08 11:30:39 -0800189 ArrayList<RouteInfo> newRoutes = new ArrayList<RouteInfo>(mRoutes.size());
190 for (RouteInfo route : mRoutes) {
191 newRoutes.add(routeWithInterface(route));
192 }
193 mRoutes = newRoutes;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700194 }
195
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700196 /**
197 * Gets the interface name for this link. May be {@code null} if not set.
198 *
199 * @return The interface name set for this link or {@code null}.
200 */
Jeff Sharkey9da2f1e2014-08-14 12:55:00 -0700201 public @Nullable String getInterfaceName() {
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700202 return mIfaceName;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700203 }
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700204
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700205 /**
206 * @hide
207 */
208 public List<String> getAllInterfaceNames() {
209 List<String> interfaceNames = new ArrayList<String>(mStackedLinks.size() + 1);
Robert Greenwalt55187f12013-03-22 12:00:17 -0700210 if (mIfaceName != null) interfaceNames.add(new String(mIfaceName));
Lorenzo Colitti4aa9bcf2013-03-20 19:22:58 +0900211 for (LinkProperties stacked: mStackedLinks.values()) {
212 interfaceNames.addAll(stacked.getAllInterfaceNames());
213 }
214 return interfaceNames;
215 }
216
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +0900217 /**
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700218 * Returns all the addresses on this link. We often think of a link having a single address,
219 * however, particularly with Ipv6 several addresses are typical. Note that the
220 * {@code LinkProperties} actually contains {@link LinkAddress} objects which also include
221 * prefix lengths for each address. This is a simplified utility alternative to
222 * {@link LinkProperties#getLinkAddresses}.
223 *
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700224 * @return An umodifiable {@link List} of {@link InetAddress} for this link.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700225 * @hide
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +0900226 */
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700227 public List<InetAddress> getAddresses() {
228 List<InetAddress> addresses = new ArrayList<InetAddress>();
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700229 for (LinkAddress linkAddress : mLinkAddresses) {
230 addresses.add(linkAddress.getAddress());
231 }
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700232 return Collections.unmodifiableList(addresses);
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700233 }
234
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +0900235 /**
236 * Returns all the addresses on this link and all the links stacked above it.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700237 * @hide
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +0900238 */
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700239 public List<InetAddress> getAllAddresses() {
240 List<InetAddress> addresses = new ArrayList<InetAddress>();
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +0900241 for (LinkAddress linkAddress : mLinkAddresses) {
242 addresses.add(linkAddress.getAddress());
243 }
244 for (LinkProperties stacked: mStackedLinks.values()) {
245 addresses.addAll(stacked.getAllAddresses());
246 }
247 return addresses;
248 }
249
Lorenzo Colitti64483942013-11-15 18:43:52 +0900250 private int findLinkAddressIndex(LinkAddress address) {
251 for (int i = 0; i < mLinkAddresses.size(); i++) {
252 if (mLinkAddresses.get(i).isSameAddressAs(address)) {
253 return i;
254 }
Lorenzo Colitti4faa0272013-08-08 11:00:12 +0900255 }
Lorenzo Colitti64483942013-11-15 18:43:52 +0900256 return -1;
Lorenzo Colitti4faa0272013-08-08 11:00:12 +0900257 }
258
259 /**
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700260 * Adds a {@link LinkAddress} to this {@code LinkProperties} if a {@link LinkAddress} of the
261 * same address/prefix does not already exist. If it does exist it is replaced.
Lorenzo Colitti64483942013-11-15 18:43:52 +0900262 * @param address The {@code LinkAddress} to add.
263 * @return true if {@code address} was added or updated, false otherwise.
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700264 * @hide
Lorenzo Colitti64483942013-11-15 18:43:52 +0900265 */
266 public boolean addLinkAddress(LinkAddress address) {
267 if (address == null) {
268 return false;
269 }
270 int i = findLinkAddressIndex(address);
271 if (i < 0) {
272 // Address was not present. Add it.
273 mLinkAddresses.add(address);
274 return true;
275 } else if (mLinkAddresses.get(i).equals(address)) {
276 // Address was present and has same properties. Do nothing.
277 return false;
278 } else {
279 // Address was present and has different properties. Update it.
280 mLinkAddresses.set(i, address);
281 return true;
282 }
283 }
284
285 /**
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700286 * Removes a {@link LinkAddress} from this {@code LinkProperties}. Specifically, matches
287 * and {@link LinkAddress} with the same address and prefix.
288 *
289 * @param toRemove A {@link LinkAddress} specifying the address to remove.
Lorenzo Colitti4faa0272013-08-08 11:00:12 +0900290 * @return true if the address was removed, false if it did not exist.
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700291 * @hide
Lorenzo Colitti4faa0272013-08-08 11:00:12 +0900292 */
293 public boolean removeLinkAddress(LinkAddress toRemove) {
Lorenzo Colitti64483942013-11-15 18:43:52 +0900294 int i = findLinkAddressIndex(toRemove);
295 if (i >= 0) {
296 mLinkAddresses.remove(i);
297 return true;
298 }
299 return false;
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700300 }
301
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +0900302 /**
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700303 * Returns all the {@link LinkAddress} on this link. Typically a link will have
304 * one IPv4 address and one or more IPv6 addresses.
305 *
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700306 * @return An unmodifiable {@link List} of {@link LinkAddress} for this link.
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +0900307 */
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700308 public List<LinkAddress> getLinkAddresses() {
309 return Collections.unmodifiableList(mLinkAddresses);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700310 }
311
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +0900312 /**
313 * Returns all the addresses on this link and all the links stacked above it.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700314 * @hide
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +0900315 */
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700316 public List<LinkAddress> getAllLinkAddresses() {
317 List<LinkAddress> addresses = new ArrayList<LinkAddress>();
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +0900318 addresses.addAll(mLinkAddresses);
319 for (LinkProperties stacked: mStackedLinks.values()) {
320 addresses.addAll(stacked.getAllLinkAddresses());
321 }
322 return addresses;
323 }
324
Lorenzo Colitti22f407b2013-08-23 20:54:49 +0900325 /**
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700326 * Replaces the {@link LinkAddress} in this {@code LinkProperties} with
327 * the given {@link Collection} of {@link LinkAddress}.
328 *
329 * @param addresses The {@link Collection} of {@link LinkAddress} to set in this
330 * object.
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700331 * @hide
Lorenzo Colitti22f407b2013-08-23 20:54:49 +0900332 */
333 public void setLinkAddresses(Collection<LinkAddress> addresses) {
334 mLinkAddresses.clear();
335 for (LinkAddress address: addresses) {
336 addLinkAddress(address);
337 }
338 }
339
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700340 /**
Lorenzo Colitti309a75d2014-06-24 00:34:39 +0900341 * Adds the given {@link InetAddress} to the list of DNS servers, if not present.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700342 *
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700343 * @param dnsServer The {@link InetAddress} to add to the list of DNS servers.
Lorenzo Colitti309a75d2014-06-24 00:34:39 +0900344 * @return true if the DNS server was added, false if it was already present.
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700345 * @hide
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700346 */
Lorenzo Colitti309a75d2014-06-24 00:34:39 +0900347 public boolean addDnsServer(InetAddress dnsServer) {
348 if (dnsServer != null && !mDnses.contains(dnsServer)) {
349 mDnses.add(dnsServer);
350 return true;
351 }
352 return false;
353 }
354
355 /**
Erik Klinecd7ed162015-05-21 16:15:02 +0900356 * Removes the given {@link InetAddress} from the list of DNS servers.
357 *
358 * @param dnsServer The {@link InetAddress} to remove from the list of DNS servers.
359 * @return true if the DNS server was removed, false if it did not exist.
360 * @hide
361 */
362 public boolean removeDnsServer(InetAddress dnsServer) {
363 if (dnsServer != null) {
364 return mDnses.remove(dnsServer);
365 }
366 return false;
367 }
368
369 /**
Lorenzo Colitti309a75d2014-06-24 00:34:39 +0900370 * Replaces the DNS servers in this {@code LinkProperties} with
371 * the given {@link Collection} of {@link InetAddress} objects.
372 *
373 * @param addresses The {@link Collection} of DNS servers to set in this object.
374 * @hide
375 */
376 public void setDnsServers(Collection<InetAddress> dnsServers) {
377 mDnses.clear();
378 for (InetAddress dnsServer: dnsServers) {
379 addDnsServer(dnsServer);
380 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700381 }
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700382
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700383 /**
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -0700384 * Returns all the {@link InetAddress} for DNS servers on this link.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700385 *
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700386 * @return An umodifiable {@link List} of {@link InetAddress} for DNS servers on
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700387 * this link.
388 */
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700389 public List<InetAddress> getDnsServers() {
390 return Collections.unmodifiableList(mDnses);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700391 }
392
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700393 /**
394 * Sets the DNS domain search path used on this link.
395 *
396 * @param domains A {@link String} listing in priority order the comma separated
397 * domains to search when resolving host names on this link.
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700398 * @hide
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700399 */
Robert Greenwalt8058f622012-11-09 10:52:27 -0800400 public void setDomains(String domains) {
401 mDomains = domains;
402 }
403
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700404 /**
405 * Get the DNS domains search path set for this link.
406 *
407 * @return A {@link String} containing the comma separated domains to search when resolving
408 * host names on this link.
409 */
410 public String getDomains() {
411 return mDomains;
412 }
413
414 /**
415 * Sets the Maximum Transmission Unit size to use on this link. This should not be used
416 * unless the system default (1500) is incorrect. Values less than 68 or greater than
417 * 10000 will be ignored.
418 *
419 * @param mtu The MTU to use for this link.
420 * @hide
421 */
sy.yun9d9b74a2013-09-02 05:24:09 +0900422 public void setMtu(int mtu) {
423 mMtu = mtu;
424 }
425
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700426 /**
427 * Gets any non-default MTU size set for this link. Note that if the default is being used
428 * this will return 0.
429 *
430 * @return The mtu value set for this link.
431 * @hide
432 */
sy.yun9d9b74a2013-09-02 05:24:09 +0900433 public int getMtu() {
434 return mMtu;
435 }
436
Robert Greenwalt3f05bf42014-08-06 12:00:25 -0700437 /**
438 * Sets the tcp buffers sizes to be used when this link is the system default.
439 * Should be of the form "rmem_min,rmem_def,rmem_max,wmem_min,wmem_def,wmem_max".
440 *
441 * @param tcpBufferSizes The tcp buffers sizes to use.
442 *
443 * @hide
444 */
445 public void setTcpBufferSizes(String tcpBufferSizes) {
446 mTcpBufferSizes = tcpBufferSizes;
447 }
448
449 /**
450 * Gets the tcp buffer sizes.
451 *
452 * @return the tcp buffer sizes to use when this link is the system default.
453 *
454 * @hide
455 */
456 public String getTcpBufferSizes() {
457 return mTcpBufferSizes;
458 }
459
Lorenzo Colitti45b9a5b2013-03-08 11:30:39 -0800460 private RouteInfo routeWithInterface(RouteInfo route) {
461 return new RouteInfo(
462 route.getDestination(),
463 route.getGateway(),
Lorenzo Colitti4b0f8e62014-09-19 01:49:05 +0900464 mIfaceName,
465 route.getType());
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700466 }
Lorenzo Colitti45b9a5b2013-03-08 11:30:39 -0800467
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700468 /**
Lorenzo Colittic17a1b92014-06-12 23:10:17 +0900469 * Adds a {@link RouteInfo} to this {@code LinkProperties}, if not present. If the
470 * {@link RouteInfo} had an interface name set and that differs from the interface set for this
471 * {@code LinkProperties} an {@link IllegalArgumentException} will be thrown. The proper
472 * course is to add either un-named or properly named {@link RouteInfo}.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700473 *
474 * @param route A {@link RouteInfo} to add to this object.
Lorenzo Colittic17a1b92014-06-12 23:10:17 +0900475 * @return {@code false} if the route was already present, {@code true} if it was added.
476 *
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700477 * @hide
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700478 */
Lorenzo Colittic17a1b92014-06-12 23:10:17 +0900479 public boolean addRoute(RouteInfo route) {
Lorenzo Colitti45b9a5b2013-03-08 11:30:39 -0800480 if (route != null) {
481 String routeIface = route.getInterface();
482 if (routeIface != null && !routeIface.equals(mIfaceName)) {
Lorenzo Colitti1994bc12013-03-08 19:11:40 -0800483 throw new IllegalArgumentException(
Lorenzo Colitti45b9a5b2013-03-08 11:30:39 -0800484 "Route added with non-matching interface: " + routeIface +
Lorenzo Colitti1994bc12013-03-08 19:11:40 -0800485 " vs. " + mIfaceName);
Lorenzo Colitti45b9a5b2013-03-08 11:30:39 -0800486 }
Lorenzo Colittic17a1b92014-06-12 23:10:17 +0900487 route = routeWithInterface(route);
488 if (!mRoutes.contains(route)) {
489 mRoutes.add(route);
490 return true;
491 }
Lorenzo Colitti45b9a5b2013-03-08 11:30:39 -0800492 }
Lorenzo Colittic17a1b92014-06-12 23:10:17 +0900493 return false;
494 }
495
496 /**
497 * Removes a {@link RouteInfo} from this {@code LinkProperties}, if present. The route must
498 * specify an interface and the interface must match the interface of this
499 * {@code LinkProperties}, or it will not be removed.
500 *
501 * @return {@code true} if the route was removed, {@code false} if it was not present.
502 *
503 * @hide
504 */
505 public boolean removeRoute(RouteInfo route) {
506 return route != null &&
507 Objects.equals(mIfaceName, route.getInterface()) &&
508 mRoutes.remove(route);
Lorenzo Colitti45b9a5b2013-03-08 11:30:39 -0800509 }
510
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800511 /**
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700512 * Returns all the {@link RouteInfo} set on this link.
513 *
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700514 * @return An unmodifiable {@link List} of {@link RouteInfo} for this link.
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800515 */
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700516 public List<RouteInfo> getRoutes() {
517 return Collections.unmodifiableList(mRoutes);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700518 }
519
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800520 /**
Rubin Xu1bb5c082017-09-05 18:40:49 +0100521 * Make sure this LinkProperties instance contains routes that cover the local subnet
522 * of its link addresses. Add any route that is missing.
523 * @hide
524 */
525 public void ensureDirectlyConnectedRoutes() {
526 for (LinkAddress addr: mLinkAddresses) {
527 addRoute(new RouteInfo(addr, null, mIfaceName));
528 }
529 }
530
531 /**
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800532 * Returns all the routes on this link and all the links stacked above it.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700533 * @hide
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800534 */
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700535 public List<RouteInfo> getAllRoutes() {
Rubin Xu1bb5c082017-09-05 18:40:49 +0100536 List<RouteInfo> routes = new ArrayList<>();
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800537 routes.addAll(mRoutes);
538 for (LinkProperties stacked: mStackedLinks.values()) {
539 routes.addAll(stacked.getAllRoutes());
540 }
Robert Greenwalt6629bcd2013-03-15 11:28:50 -0700541 return routes;
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800542 }
543
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700544 /**
545 * Sets the recommended {@link ProxyInfo} to use on this link, or {@code null} for none.
546 * Note that Http Proxies are only a hint - the system recommends their use, but it does
547 * not enforce it and applications may ignore them.
548 *
Erik Klineb36a3132015-06-26 19:21:34 +0900549 * @param proxy A {@link ProxyInfo} defining the HTTP Proxy to use on this link.
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700550 * @hide
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700551 */
Jason Monk207900c2014-04-25 15:00:09 -0400552 public void setHttpProxy(ProxyInfo proxy) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700553 mHttpProxy = proxy;
554 }
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700555
556 /**
557 * Gets the recommended {@link ProxyInfo} (or {@code null}) set on this link.
558 *
559 * @return The {@link ProxyInfo} set on this link
560 */
Jason Monk207900c2014-04-25 15:00:09 -0400561 public ProxyInfo getHttpProxy() {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700562 return mHttpProxy;
563 }
564
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800565 /**
566 * Adds a stacked link.
567 *
568 * If there is already a stacked link with the same interfacename as link,
569 * that link is replaced with link. Otherwise, link is added to the list
570 * of stacked links. If link is null, nothing changes.
571 *
572 * @param link The link to add.
Lorenzo Colitti4faa0272013-08-08 11:00:12 +0900573 * @return true if the link was stacked, false otherwise.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700574 * @hide
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800575 */
Lorenzo Colitti4faa0272013-08-08 11:00:12 +0900576 public boolean addStackedLink(LinkProperties link) {
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800577 if (link != null && link.getInterfaceName() != null) {
578 mStackedLinks.put(link.getInterfaceName(), link);
Lorenzo Colitti4faa0272013-08-08 11:00:12 +0900579 return true;
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800580 }
Lorenzo Colitti4faa0272013-08-08 11:00:12 +0900581 return false;
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800582 }
583
584 /**
585 * Removes a stacked link.
586 *
Lorenzo Colittif3cab632014-10-20 11:08:03 +0900587 * If there is a stacked link with the given interface name, it is
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800588 * removed. Otherwise, nothing changes.
589 *
Lorenzo Colittif3cab632014-10-20 11:08:03 +0900590 * @param iface The interface name of the link to remove.
Lorenzo Colitti4faa0272013-08-08 11:00:12 +0900591 * @return true if the link was removed, false otherwise.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700592 * @hide
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800593 */
Lorenzo Colittif3cab632014-10-20 11:08:03 +0900594 public boolean removeStackedLink(String iface) {
595 if (iface != null) {
596 LinkProperties removed = mStackedLinks.remove(iface);
Lorenzo Colitti4faa0272013-08-08 11:00:12 +0900597 return removed != null;
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800598 }
Lorenzo Colitti4faa0272013-08-08 11:00:12 +0900599 return false;
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800600 }
601
602 /**
603 * Returns all the links stacked on top of this link.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700604 * @hide
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800605 */
Jeff Sharkeyeb2c2c72014-08-11 15:22:51 -0700606 public @NonNull List<LinkProperties> getStackedLinks() {
607 if (mStackedLinks.isEmpty()) {
608 return Collections.EMPTY_LIST;
609 }
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700610 List<LinkProperties> stacked = new ArrayList<LinkProperties>();
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800611 for (LinkProperties link : mStackedLinks.values()) {
Jeff Sharkeyeb2c2c72014-08-11 15:22:51 -0700612 stacked.add(new LinkProperties(link));
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800613 }
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700614 return Collections.unmodifiableList(stacked);
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800615 }
616
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700617 /**
618 * Clears this object to its initial state.
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700619 * @hide
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700620 */
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700621 public void clear() {
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700622 mIfaceName = null;
Wink Savillee8222252011-07-13 13:44:13 -0700623 mLinkAddresses.clear();
624 mDnses.clear();
Robert Greenwalt8058f622012-11-09 10:52:27 -0800625 mDomains = null;
Wink Savillee8222252011-07-13 13:44:13 -0700626 mRoutes.clear();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700627 mHttpProxy = null;
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800628 mStackedLinks.clear();
sy.yun9d9b74a2013-09-02 05:24:09 +0900629 mMtu = 0;
Robert Greenwalt3f05bf42014-08-06 12:00:25 -0700630 mTcpBufferSizes = null;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700631 }
632
633 /**
634 * Implement the Parcelable interface
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700635 */
636 public int describeContents() {
637 return 0;
638 }
639
Wink Saville1f6408a2010-08-27 11:15:18 -0700640 @Override
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700641 public String toString() {
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700642 String ifaceName = (mIfaceName == null ? "" : "InterfaceName: " + mIfaceName + " ");
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700643
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700644 String linkAddresses = "LinkAddresses: [";
John Wang4e900092011-04-04 12:35:42 -0700645 for (LinkAddress addr : mLinkAddresses) linkAddresses += addr.toString() + ",";
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700646 linkAddresses += "] ";
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700647
648 String dns = "DnsAddresses: [";
Wink Saville1f6408a2010-08-27 11:15:18 -0700649 for (InetAddress addr : mDnses) dns += addr.getHostAddress() + ",";
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700650 dns += "] ";
651
Robert Greenwalt8058f622012-11-09 10:52:27 -0800652 String domainName = "Domains: " + mDomains;
653
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -0700654 String mtu = " MTU: " + mMtu;
sy.yun9d9b74a2013-09-02 05:24:09 +0900655
Robert Greenwalt3f05bf42014-08-06 12:00:25 -0700656 String tcpBuffSizes = "";
657 if (mTcpBufferSizes != null) {
658 tcpBuffSizes = " TcpBufferSizes: " + mTcpBufferSizes;
659 }
660
Robert Greenwalt8058f622012-11-09 10:52:27 -0800661 String routes = " Routes: [";
Robert Greenwaltaa70f102011-04-28 14:28:50 -0700662 for (RouteInfo route : mRoutes) routes += route.toString() + ",";
663 routes += "] ";
Sreeram Ramachandrancc91c7b2014-06-03 18:41:43 -0700664 String proxy = (mHttpProxy == null ? "" : " HttpProxy: " + mHttpProxy.toString() + " ");
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700665
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800666 String stacked = "";
667 if (mStackedLinks.values().size() > 0) {
668 stacked += " Stacked: [";
669 for (LinkProperties link: mStackedLinks.values()) {
670 stacked += " [" + link.toString() + " ],";
671 }
672 stacked += "] ";
673 }
sy.yun9d9b74a2013-09-02 05:24:09 +0900674 return "{" + ifaceName + linkAddresses + routes + dns + domainName + mtu
Robert Greenwalt3f05bf42014-08-06 12:00:25 -0700675 + tcpBuffSizes + proxy + stacked + "}";
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800676 }
677
678 /**
679 * Returns true if this link has an IPv4 address.
680 *
681 * @return {@code true} if there is an IPv4 address, {@code false} otherwise.
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700682 * @hide
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800683 */
684 public boolean hasIPv4Address() {
685 for (LinkAddress address : mLinkAddresses) {
Hugo Benichibd87a392017-10-10 16:29:06 +0900686 if (address.getAddress() instanceof Inet4Address) {
687 return true;
688 }
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800689 }
690 return false;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700691 }
692
Wink Savillee8222252011-07-13 13:44:13 -0700693 /**
Lorenzo Colitti87cfc702015-07-27 16:35:33 +0900694 * Returns true if this link or any of its stacked interfaces has an IPv4 address.
695 *
696 * @return {@code true} if there is an IPv4 address, {@code false} otherwise.
697 */
698 private boolean hasIPv4AddressOnInterface(String iface) {
Lorenzo Colitti89b63922015-07-30 23:41:43 +0900699 // mIfaceName can be null.
700 return (Objects.equals(iface, mIfaceName) && hasIPv4Address()) ||
Lorenzo Colitti87cfc702015-07-27 16:35:33 +0900701 (iface != null && mStackedLinks.containsKey(iface) &&
702 mStackedLinks.get(iface).hasIPv4Address());
703 }
704
705 /**
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +0900706 * Returns true if this link has a global preferred IPv6 address.
Lorenzo Colitti4faa0272013-08-08 11:00:12 +0900707 *
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +0900708 * @return {@code true} if there is a global preferred IPv6 address, {@code false} otherwise.
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700709 * @hide
Lorenzo Colitti4faa0272013-08-08 11:00:12 +0900710 */
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +0900711 public boolean hasGlobalIPv6Address() {
Lorenzo Colitti4faa0272013-08-08 11:00:12 +0900712 for (LinkAddress address : mLinkAddresses) {
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +0900713 if (address.getAddress() instanceof Inet6Address && address.isGlobalPreferred()) {
Lorenzo Colitti4faa0272013-08-08 11:00:12 +0900714 return true;
715 }
716 }
717 return false;
718 }
719
720 /**
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +0900721 * Returns true if this link has an IPv4 default route.
722 *
723 * @return {@code true} if there is an IPv4 default route, {@code false} otherwise.
724 * @hide
725 */
726 public boolean hasIPv4DefaultRoute() {
727 for (RouteInfo r : mRoutes) {
Hugo Benichibd87a392017-10-10 16:29:06 +0900728 if (r.isIPv4Default()) {
729 return true;
730 }
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +0900731 }
732 return false;
733 }
734
735 /**
736 * Returns true if this link has an IPv6 default route.
737 *
738 * @return {@code true} if there is an IPv6 default route, {@code false} otherwise.
739 * @hide
740 */
741 public boolean hasIPv6DefaultRoute() {
742 for (RouteInfo r : mRoutes) {
Hugo Benichibd87a392017-10-10 16:29:06 +0900743 if (r.isIPv6Default()) {
744 return true;
745 }
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +0900746 }
747 return false;
748 }
749
750 /**
751 * Returns true if this link has an IPv4 DNS server.
752 *
753 * @return {@code true} if there is an IPv4 DNS server, {@code false} otherwise.
754 * @hide
755 */
756 public boolean hasIPv4DnsServer() {
757 for (InetAddress ia : mDnses) {
Hugo Benichibd87a392017-10-10 16:29:06 +0900758 if (ia instanceof Inet4Address) {
759 return true;
760 }
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +0900761 }
762 return false;
763 }
764
765 /**
766 * Returns true if this link has an IPv6 DNS server.
767 *
768 * @return {@code true} if there is an IPv6 DNS server, {@code false} otherwise.
769 * @hide
770 */
771 public boolean hasIPv6DnsServer() {
772 for (InetAddress ia : mDnses) {
Hugo Benichibd87a392017-10-10 16:29:06 +0900773 if (ia instanceof Inet6Address) {
774 return true;
775 }
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +0900776 }
777 return false;
778 }
779
780 /**
Erik Klined3b9fd32014-10-24 21:50:20 +0900781 * Returns true if this link is provisioned for global IPv4 connectivity.
782 * This requires an IP address, default route, and DNS server.
783 *
784 * @return {@code true} if the link is provisioned, {@code false} otherwise.
Erik Klinecd7ed162015-05-21 16:15:02 +0900785 * @hide
Erik Klined3b9fd32014-10-24 21:50:20 +0900786 */
Erik Klinecd7ed162015-05-21 16:15:02 +0900787 public boolean isIPv4Provisioned() {
Erik Klined3b9fd32014-10-24 21:50:20 +0900788 return (hasIPv4Address() &&
789 hasIPv4DefaultRoute() &&
790 hasIPv4DnsServer());
791 }
792
793 /**
794 * Returns true if this link is provisioned for global IPv6 connectivity.
795 * This requires an IP address, default route, and DNS server.
796 *
797 * @return {@code true} if the link is provisioned, {@code false} otherwise.
Erik Klinecd7ed162015-05-21 16:15:02 +0900798 * @hide
Erik Klined3b9fd32014-10-24 21:50:20 +0900799 */
Erik Klinecd7ed162015-05-21 16:15:02 +0900800 public boolean isIPv6Provisioned() {
Erik Klined3b9fd32014-10-24 21:50:20 +0900801 return (hasGlobalIPv6Address() &&
802 hasIPv6DefaultRoute() &&
803 hasIPv6DnsServer());
804 }
805
806 /**
807 * Returns true if this link is provisioned for global connectivity,
808 * for at least one Internet Protocol family.
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +0900809 *
810 * @return {@code true} if the link is provisioned, {@code false} otherwise.
811 * @hide
812 */
813 public boolean isProvisioned() {
Erik Klinecd7ed162015-05-21 16:15:02 +0900814 return (isIPv4Provisioned() || isIPv6Provisioned());
Lorenzo Colitti76ea6c62014-06-23 22:33:43 +0900815 }
816
817 /**
Erik Klineb36a3132015-06-26 19:21:34 +0900818 * Evaluate whether the {@link InetAddress} is considered reachable.
819 *
820 * @return {@code true} if the given {@link InetAddress} is considered reachable,
821 * {@code false} otherwise.
822 * @hide
823 */
824 public boolean isReachable(InetAddress ip) {
825 final List<RouteInfo> allRoutes = getAllRoutes();
826 // If we don't have a route to this IP address, it's not reachable.
827 final RouteInfo bestRoute = RouteInfo.selectBestRoute(allRoutes, ip);
828 if (bestRoute == null) {
829 return false;
830 }
831
832 // TODO: better source address evaluation for destination addresses.
833
834 if (ip instanceof Inet4Address) {
835 // For IPv4, it suffices for now to simply have any address.
Lorenzo Colitti87cfc702015-07-27 16:35:33 +0900836 return hasIPv4AddressOnInterface(bestRoute.getInterface());
Erik Klineb36a3132015-06-26 19:21:34 +0900837 } else if (ip instanceof Inet6Address) {
838 if (ip.isLinkLocalAddress()) {
839 // For now, just make sure link-local destinations have
840 // scopedIds set, since transmits will generally fail otherwise.
841 // TODO: verify it matches the ifindex of one of the interfaces.
842 return (((Inet6Address)ip).getScopeId() != 0);
843 } else {
844 // For non-link-local destinations check that either the best route
845 // is directly connected or that some global preferred address exists.
846 // TODO: reconsider all cases (disconnected ULA networks, ...).
847 return (!bestRoute.hasGateway() || hasGlobalIPv6Address());
848 }
849 }
850
851 return false;
852 }
853
854 /**
Wink Savillee8222252011-07-13 13:44:13 -0700855 * Compares this {@code LinkProperties} interface name against the target
856 *
857 * @param target LinkProperties to compare.
858 * @return {@code true} if both are identical, {@code false} otherwise.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700859 * @hide
Wink Savillee8222252011-07-13 13:44:13 -0700860 */
861 public boolean isIdenticalInterfaceName(LinkProperties target) {
862 return TextUtils.equals(getInterfaceName(), target.getInterfaceName());
863 }
864
865 /**
Robert Greenwalt4717c262012-10-31 14:32:53 -0700866 * Compares this {@code LinkProperties} interface addresses against the target
Wink Savillee8222252011-07-13 13:44:13 -0700867 *
868 * @param target LinkProperties to compare.
869 * @return {@code true} if both are identical, {@code false} otherwise.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700870 * @hide
Wink Savillee8222252011-07-13 13:44:13 -0700871 */
872 public boolean isIdenticalAddresses(LinkProperties target) {
873 Collection<InetAddress> targetAddresses = target.getAddresses();
874 Collection<InetAddress> sourceAddresses = getAddresses();
875 return (sourceAddresses.size() == targetAddresses.size()) ?
876 sourceAddresses.containsAll(targetAddresses) : false;
877 }
878
879 /**
880 * Compares this {@code LinkProperties} DNS addresses against the target
881 *
882 * @param target LinkProperties to compare.
883 * @return {@code true} if both are identical, {@code false} otherwise.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700884 * @hide
Wink Savillee8222252011-07-13 13:44:13 -0700885 */
886 public boolean isIdenticalDnses(LinkProperties target) {
Robert Greenwaltdf2b8782014-06-06 10:30:11 -0700887 Collection<InetAddress> targetDnses = target.getDnsServers();
Robert Greenwalt8058f622012-11-09 10:52:27 -0800888 String targetDomains = target.getDomains();
889 if (mDomains == null) {
890 if (targetDomains != null) return false;
891 } else {
892 if (mDomains.equals(targetDomains) == false) return false;
893 }
Wink Savillee8222252011-07-13 13:44:13 -0700894 return (mDnses.size() == targetDnses.size()) ?
895 mDnses.containsAll(targetDnses) : false;
896 }
897
898 /**
899 * Compares this {@code LinkProperties} Routes against the target
900 *
901 * @param target LinkProperties to compare.
902 * @return {@code true} if both are identical, {@code false} otherwise.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700903 * @hide
Wink Savillee8222252011-07-13 13:44:13 -0700904 */
905 public boolean isIdenticalRoutes(LinkProperties target) {
906 Collection<RouteInfo> targetRoutes = target.getRoutes();
907 return (mRoutes.size() == targetRoutes.size()) ?
908 mRoutes.containsAll(targetRoutes) : false;
909 }
910
911 /**
912 * Compares this {@code LinkProperties} HttpProxy against the target
913 *
914 * @param target LinkProperties to compare.
915 * @return {@code true} if both are identical, {@code false} otherwise.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700916 * @hide
Wink Savillee8222252011-07-13 13:44:13 -0700917 */
918 public boolean isIdenticalHttpProxy(LinkProperties target) {
919 return getHttpProxy() == null ? target.getHttpProxy() == null :
920 getHttpProxy().equals(target.getHttpProxy());
921 }
John Wang4e900092011-04-04 12:35:42 -0700922
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800923 /**
924 * Compares this {@code LinkProperties} stacked links against the target
925 *
926 * @param target LinkProperties to compare.
927 * @return {@code true} if both are identical, {@code false} otherwise.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700928 * @hide
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800929 */
930 public boolean isIdenticalStackedLinks(LinkProperties target) {
Lorenzo Colitti213f98b2013-04-01 10:47:43 +0900931 if (!mStackedLinks.keySet().equals(target.mStackedLinks.keySet())) {
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800932 return false;
933 }
934 for (LinkProperties stacked : mStackedLinks.values()) {
935 // Hashtable values can never be null.
936 String iface = stacked.getInterfaceName();
937 if (!stacked.equals(target.mStackedLinks.get(iface))) {
938 return false;
939 }
940 }
941 return true;
942 }
943
sy.yun9d9b74a2013-09-02 05:24:09 +0900944 /**
945 * Compares this {@code LinkProperties} MTU against the target
946 *
Ying Wangd57de6a2013-09-06 22:53:16 -0700947 * @param target LinkProperties to compare.
sy.yun9d9b74a2013-09-02 05:24:09 +0900948 * @return {@code true} if both are identical, {@code false} otherwise.
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700949 * @hide
sy.yun9d9b74a2013-09-02 05:24:09 +0900950 */
951 public boolean isIdenticalMtu(LinkProperties target) {
952 return getMtu() == target.getMtu();
953 }
954
Robert Greenwalt3f05bf42014-08-06 12:00:25 -0700955 /**
956 * Compares this {@code LinkProperties} Tcp buffer sizes against the target.
957 *
958 * @param target LinkProperties to compare.
959 * @return {@code true} if both are identical, {@code false} otherwise.
960 * @hide
961 */
962 public boolean isIdenticalTcpBufferSizes(LinkProperties target) {
963 return Objects.equals(mTcpBufferSizes, target.mTcpBufferSizes);
964 }
965
John Wang4e900092011-04-04 12:35:42 -0700966 @Override
967 /**
968 * Compares this {@code LinkProperties} instance against the target
969 * LinkProperties in {@code obj}. Two LinkPropertieses are equal if
970 * all their fields are equal in values.
971 *
972 * For collection fields, such as mDnses, containsAll() is used to check
973 * if two collections contains the same elements, independent of order.
974 * There are two thoughts regarding containsAll()
975 * 1. Duplicated elements. eg, (A, B, B) and (A, A, B) are equal.
976 * 2. Worst case performance is O(n^2).
977 *
978 * @param obj the object to be tested for equality.
979 * @return {@code true} if both objects are equal, {@code false} otherwise.
980 */
981 public boolean equals(Object obj) {
982 if (this == obj) return true;
983
984 if (!(obj instanceof LinkProperties)) return false;
985
John Wang4e900092011-04-04 12:35:42 -0700986 LinkProperties target = (LinkProperties) obj;
Robert Greenwalt4f05d552014-05-18 22:01:38 -0700987 /**
988 * This method does not check that stacked interfaces are equal, because
989 * stacked interfaces are not so much a property of the link as a
990 * description of connections between links.
991 */
Wink Savillee8222252011-07-13 13:44:13 -0700992 return isIdenticalInterfaceName(target) &&
993 isIdenticalAddresses(target) &&
994 isIdenticalDnses(target) &&
995 isIdenticalRoutes(target) &&
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -0800996 isIdenticalHttpProxy(target) &&
sy.yun9d9b74a2013-09-02 05:24:09 +0900997 isIdenticalStackedLinks(target) &&
Robert Greenwalt3f05bf42014-08-06 12:00:25 -0700998 isIdenticalMtu(target) &&
999 isIdenticalTcpBufferSizes(target);
Wink Savillee8222252011-07-13 13:44:13 -07001000 }
John Wang4e900092011-04-04 12:35:42 -07001001
Wink Savillee8222252011-07-13 13:44:13 -07001002 /**
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +09001003 * Compares the addresses in this LinkProperties with another
1004 * LinkProperties, examining only addresses on the base link.
Wink Savillee8222252011-07-13 13:44:13 -07001005 *
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +09001006 * @param target a LinkProperties with the new list of addresses
1007 * @return the differences between the addresses.
Robert Greenwalt4f05d552014-05-18 22:01:38 -07001008 * @hide
Wink Savillee8222252011-07-13 13:44:13 -07001009 */
Robert Greenwalt0a46db52011-07-14 14:28:05 -07001010 public CompareResult<LinkAddress> compareAddresses(LinkProperties target) {
Wink Savillee8222252011-07-13 13:44:13 -07001011 /*
1012 * Duplicate the LinkAddresses into removed, we will be removing
1013 * address which are common between mLinkAddresses and target
1014 * leaving the addresses that are different. And address which
1015 * are in target but not in mLinkAddresses are placed in the
1016 * addedAddresses.
1017 */
Rubin Xu2fc72f72017-08-22 16:35:52 +01001018 return new CompareResult<>(mLinkAddresses,
1019 target != null ? target.getLinkAddresses() : null);
John Wang4e900092011-04-04 12:35:42 -07001020 }
1021
Robert Greenwalt0a46db52011-07-14 14:28:05 -07001022 /**
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +09001023 * Compares the DNS addresses in this LinkProperties with another
1024 * LinkProperties, examining only DNS addresses on the base link.
Robert Greenwalt0a46db52011-07-14 14:28:05 -07001025 *
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +09001026 * @param target a LinkProperties with the new list of dns addresses
1027 * @return the differences between the DNS addresses.
Robert Greenwalt4f05d552014-05-18 22:01:38 -07001028 * @hide
Robert Greenwalt0a46db52011-07-14 14:28:05 -07001029 */
1030 public CompareResult<InetAddress> compareDnses(LinkProperties target) {
1031 /*
1032 * Duplicate the InetAddresses into removed, we will be removing
1033 * dns address which are common between mDnses and target
1034 * leaving the addresses that are different. And dns address which
1035 * are in target but not in mDnses are placed in the
1036 * addedAddresses.
1037 */
Rubin Xu2fc72f72017-08-22 16:35:52 +01001038 return new CompareResult<>(mDnses, target != null ? target.getDnsServers() : null);
Robert Greenwalt0a46db52011-07-14 14:28:05 -07001039 }
1040
1041 /**
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +09001042 * Compares all routes in this LinkProperties with another LinkProperties,
1043 * examining both the the base link and all stacked links.
Robert Greenwalt0a46db52011-07-14 14:28:05 -07001044 *
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +09001045 * @param target a LinkProperties with the new list of routes
1046 * @return the differences between the routes.
Robert Greenwalt4f05d552014-05-18 22:01:38 -07001047 * @hide
Robert Greenwalt0a46db52011-07-14 14:28:05 -07001048 */
Lorenzo Colittid1e0fae2013-07-31 23:23:21 +09001049 public CompareResult<RouteInfo> compareAllRoutes(LinkProperties target) {
Robert Greenwalt0a46db52011-07-14 14:28:05 -07001050 /*
1051 * Duplicate the RouteInfos into removed, we will be removing
Lorenzo Colitti1994bc12013-03-08 19:11:40 -08001052 * routes which are common between mRoutes and target
Robert Greenwalt0a46db52011-07-14 14:28:05 -07001053 * leaving the routes that are different. And route address which
1054 * are in target but not in mRoutes are placed in added.
1055 */
Rubin Xu2fc72f72017-08-22 16:35:52 +01001056 return new CompareResult<>(getAllRoutes(), target != null ? target.getAllRoutes() : null);
Robert Greenwalt0a46db52011-07-14 14:28:05 -07001057 }
1058
Paul Jensen992f2522014-04-28 10:33:11 -04001059 /**
1060 * Compares all interface names in this LinkProperties with another
1061 * LinkProperties, examining both the the base link and all stacked links.
1062 *
1063 * @param target a LinkProperties with the new list of interface names
1064 * @return the differences between the interface names.
1065 * @hide
1066 */
1067 public CompareResult<String> compareAllInterfaceNames(LinkProperties target) {
1068 /*
1069 * Duplicate the interface names into removed, we will be removing
1070 * interface names which are common between this and target
1071 * leaving the interface names that are different. And interface names which
1072 * are in target but not in this are placed in added.
1073 */
Rubin Xu2fc72f72017-08-22 16:35:52 +01001074 return new CompareResult<>(getAllInterfaceNames(),
1075 target != null ? target.getAllInterfaceNames() : null);
Paul Jensen992f2522014-04-28 10:33:11 -04001076 }
1077
Robert Greenwalt0a46db52011-07-14 14:28:05 -07001078
John Wang4e900092011-04-04 12:35:42 -07001079 @Override
1080 /**
1081 * generate hashcode based on significant fields
1082 * Equal objects must produce the same hash code, while unequal objects
1083 * may have the same hash codes.
1084 */
1085 public int hashCode() {
1086 return ((null == mIfaceName) ? 0 : mIfaceName.hashCode()
1087 + mLinkAddresses.size() * 31
1088 + mDnses.size() * 37
Robert Greenwalt8058f622012-11-09 10:52:27 -08001089 + ((null == mDomains) ? 0 : mDomains.hashCode())
Robert Greenwaltaa70f102011-04-28 14:28:50 -07001090 + mRoutes.size() * 41
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -08001091 + ((null == mHttpProxy) ? 0 : mHttpProxy.hashCode())
sy.yun9d9b74a2013-09-02 05:24:09 +09001092 + mStackedLinks.hashCode() * 47)
Robert Greenwalt3f05bf42014-08-06 12:00:25 -07001093 + mMtu * 51
1094 + ((null == mTcpBufferSizes) ? 0 : mTcpBufferSizes.hashCode());
John Wang4e900092011-04-04 12:35:42 -07001095 }
1096
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001097 /**
1098 * Implement the Parcelable interface.
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001099 */
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001100 public void writeToParcel(Parcel dest, int flags) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001101 dest.writeString(getInterfaceName());
Irfan Sheriffed5d7d12010-10-01 16:08:28 -07001102 dest.writeInt(mLinkAddresses.size());
1103 for(LinkAddress linkAddress : mLinkAddresses) {
1104 dest.writeParcelable(linkAddress, flags);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001105 }
Irfan Sheriffed5d7d12010-10-01 16:08:28 -07001106
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001107 dest.writeInt(mDnses.size());
1108 for(InetAddress d : mDnses) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001109 dest.writeByteArray(d.getAddress());
1110 }
Robert Greenwalt8058f622012-11-09 10:52:27 -08001111 dest.writeString(mDomains);
sy.yun9d9b74a2013-09-02 05:24:09 +09001112 dest.writeInt(mMtu);
Robert Greenwalt3f05bf42014-08-06 12:00:25 -07001113 dest.writeString(mTcpBufferSizes);
Robert Greenwaltaa70f102011-04-28 14:28:50 -07001114 dest.writeInt(mRoutes.size());
1115 for(RouteInfo route : mRoutes) {
1116 dest.writeParcelable(route, flags);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001117 }
Robert Greenwalt992564e2011-02-09 13:56:06 -08001118
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001119 if (mHttpProxy != null) {
1120 dest.writeByte((byte)1);
1121 dest.writeParcelable(mHttpProxy, flags);
1122 } else {
1123 dest.writeByte((byte)0);
1124 }
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -08001125 ArrayList<LinkProperties> stackedLinks = new ArrayList(mStackedLinks.values());
1126 dest.writeList(stackedLinks);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001127 }
1128
1129 /**
1130 * Implement the Parcelable interface.
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001131 */
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001132 public static final Creator<LinkProperties> CREATOR =
1133 new Creator<LinkProperties>() {
1134 public LinkProperties createFromParcel(Parcel in) {
1135 LinkProperties netProp = new LinkProperties();
Robert Greenwalt4717c262012-10-31 14:32:53 -07001136
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001137 String iface = in.readString();
1138 if (iface != null) {
Robert Greenwalt4717c262012-10-31 14:32:53 -07001139 netProp.setInterfaceName(iface);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001140 }
1141 int addressCount = in.readInt();
1142 for (int i=0; i<addressCount; i++) {
Irfan Sheriffed5d7d12010-10-01 16:08:28 -07001143 netProp.addLinkAddress((LinkAddress)in.readParcelable(null));
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001144 }
1145 addressCount = in.readInt();
1146 for (int i=0; i<addressCount; i++) {
1147 try {
Robert Greenwaltdf2b8782014-06-06 10:30:11 -07001148 netProp.addDnsServer(InetAddress.getByAddress(in.createByteArray()));
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001149 } catch (UnknownHostException e) { }
1150 }
Robert Greenwalt8058f622012-11-09 10:52:27 -08001151 netProp.setDomains(in.readString());
sy.yun9d9b74a2013-09-02 05:24:09 +09001152 netProp.setMtu(in.readInt());
Robert Greenwalt3f05bf42014-08-06 12:00:25 -07001153 netProp.setTcpBufferSizes(in.readString());
Robert Greenwalt992564e2011-02-09 13:56:06 -08001154 addressCount = in.readInt();
1155 for (int i=0; i<addressCount; i++) {
Robert Greenwaltaa70f102011-04-28 14:28:50 -07001156 netProp.addRoute((RouteInfo)in.readParcelable(null));
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001157 }
1158 if (in.readByte() == 1) {
Jason Monk207900c2014-04-25 15:00:09 -04001159 netProp.setHttpProxy((ProxyInfo)in.readParcelable(null));
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001160 }
Lorenzo Colitti419a4ce2013-03-07 10:59:25 -08001161 ArrayList<LinkProperties> stackedLinks = new ArrayList<LinkProperties>();
1162 in.readList(stackedLinks, LinkProperties.class.getClassLoader());
1163 for (LinkProperties stackedLink: stackedLinks) {
1164 netProp.addStackedLink(stackedLink);
1165 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001166 return netProp;
1167 }
1168
Robert Greenwalt37e65eb2010-08-30 10:56:47 -07001169 public LinkProperties[] newArray(int size) {
1170 return new LinkProperties[size];
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001171 }
1172 };
w1997615afd812014-08-05 15:18:11 -07001173
1174 /**
1175 * Check the valid MTU range based on IPv4 or IPv6.
1176 * @hide
1177 */
1178 public static boolean isValidMtu(int mtu, boolean ipv6) {
1179 if (ipv6) {
1180 if ((mtu >= MIN_MTU_V6 && mtu <= MAX_MTU)) return true;
1181 } else {
1182 if ((mtu >= MIN_MTU && mtu <= MAX_MTU)) return true;
1183 }
1184 return false;
1185 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001186}