Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 1 | /* |
Wink Saville | 6e80997 | 2010-09-21 09:15:35 -0700 | [diff] [blame] | 2 | * Copyright (C) 2010 The Android Open Source Project |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.net; |
| 18 | |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 19 | import android.net.ProxyInfo; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 20 | import android.os.Parcelable; |
| 21 | import android.os.Parcel; |
John Wang | 4e90009 | 2011-04-04 12:35:42 -0700 | [diff] [blame] | 22 | import android.text.TextUtils; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 23 | |
| 24 | import java.net.InetAddress; |
Lorenzo Colitti | 419a4ce | 2013-03-07 10:59:25 -0800 | [diff] [blame] | 25 | import java.net.Inet4Address; |
Lorenzo Colitti | 4faa027 | 2013-08-08 11:00:12 +0900 | [diff] [blame] | 26 | import java.net.Inet6Address; |
Lorenzo Colitti | 419a4ce | 2013-03-07 10:59:25 -0800 | [diff] [blame] | 27 | |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 28 | import java.net.UnknownHostException; |
| 29 | import java.util.ArrayList; |
| 30 | import java.util.Collection; |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 31 | import java.util.Collections; |
Lorenzo Colitti | 419a4ce | 2013-03-07 10:59:25 -0800 | [diff] [blame] | 32 | import java.util.Hashtable; |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame^] | 33 | import java.util.List; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 34 | |
| 35 | /** |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 36 | * Describes the properties of a network link. |
Robert Greenwalt | 992564e | 2011-02-09 13:56:06 -0800 | [diff] [blame] | 37 | * |
| 38 | * A link represents a connection to a network. |
| 39 | * It may have multiple addresses and multiple gateways, |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 40 | * multiple dns servers but only one http proxy and one |
| 41 | * network interface. |
Robert Greenwalt | 992564e | 2011-02-09 13:56:06 -0800 | [diff] [blame] | 42 | * |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 43 | * Note that this is just a holder of data. Modifying it |
| 44 | * does not affect live networks. |
Robert Greenwalt | 992564e | 2011-02-09 13:56:06 -0800 | [diff] [blame] | 45 | * |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 46 | */ |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 47 | public class LinkProperties implements Parcelable { |
Lorenzo Colitti | 419a4ce | 2013-03-07 10:59:25 -0800 | [diff] [blame] | 48 | // The interface described by the network link. |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 49 | private String mIfaceName; |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 50 | private ArrayList<LinkAddress> mLinkAddresses = new ArrayList<LinkAddress>(); |
| 51 | private ArrayList<InetAddress> mDnses = new ArrayList<InetAddress>(); |
Robert Greenwalt | 8058f62 | 2012-11-09 10:52:27 -0800 | [diff] [blame] | 52 | private String mDomains; |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 53 | private ArrayList<RouteInfo> mRoutes = new ArrayList<RouteInfo>(); |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 54 | private ProxyInfo mHttpProxy; |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 55 | private int mMtu; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 56 | |
Lorenzo Colitti | 419a4ce | 2013-03-07 10:59:25 -0800 | [diff] [blame] | 57 | // Stores the properties of links that are "stacked" above this link. |
| 58 | // Indexed by interface name to allow modification and to prevent duplicates being added. |
| 59 | private Hashtable<String, LinkProperties> mStackedLinks = |
| 60 | new Hashtable<String, LinkProperties>(); |
| 61 | |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame^] | 62 | /** |
| 63 | * @hide |
| 64 | */ |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 65 | public static class CompareResult<T> { |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame^] | 66 | public List<T> removed = new ArrayList<T>(); |
| 67 | public List<T> added = new ArrayList<T>(); |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 68 | |
| 69 | @Override |
| 70 | public String toString() { |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 71 | String retVal = "removed=["; |
| 72 | for (T addr : removed) retVal += addr.toString() + ","; |
| 73 | retVal += "] added=["; |
| 74 | for (T addr : added) retVal += addr.toString() + ","; |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 75 | retVal += "]"; |
| 76 | return retVal; |
| 77 | } |
| 78 | } |
| 79 | |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 80 | public LinkProperties() { |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 83 | public LinkProperties(LinkProperties source) { |
Irfan Sheriff | ef6c143 | 2010-08-30 20:37:17 -0700 | [diff] [blame] | 84 | if (source != null) { |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 85 | mIfaceName = source.getInterfaceName(); |
Robert Greenwalt | 0d8acea | 2011-07-28 17:21:25 -0700 | [diff] [blame] | 86 | for (LinkAddress l : source.getLinkAddresses()) mLinkAddresses.add(l); |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame^] | 87 | for (InetAddress i : source.getDnsServers()) mDnses.add(i); |
Robert Greenwalt | 8058f62 | 2012-11-09 10:52:27 -0800 | [diff] [blame] | 88 | mDomains = source.getDomains(); |
Robert Greenwalt | 0d8acea | 2011-07-28 17:21:25 -0700 | [diff] [blame] | 89 | for (RouteInfo r : source.getRoutes()) mRoutes.add(r); |
Wink Saville | be2b058 | 2011-05-18 15:59:04 -0700 | [diff] [blame] | 90 | mHttpProxy = (source.getHttpProxy() == null) ? |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 91 | null : new ProxyInfo(source.getHttpProxy()); |
Lorenzo Colitti | 419a4ce | 2013-03-07 10:59:25 -0800 | [diff] [blame] | 92 | for (LinkProperties l: source.mStackedLinks.values()) { |
| 93 | addStackedLink(l); |
| 94 | } |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 95 | setMtu(source.getMtu()); |
Irfan Sheriff | ef6c143 | 2010-08-30 20:37:17 -0700 | [diff] [blame] | 96 | } |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 99 | /** |
| 100 | * Sets the interface name for this link. All {@link RouteInfo} already set for this |
| 101 | * will have their interface changed to match this new value. |
| 102 | * |
| 103 | * @param iface The name of the network interface used for this link. |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame^] | 104 | * @hide |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 105 | */ |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 106 | public void setInterfaceName(String iface) { |
| 107 | mIfaceName = iface; |
Lorenzo Colitti | 45b9a5b | 2013-03-08 11:30:39 -0800 | [diff] [blame] | 108 | ArrayList<RouteInfo> newRoutes = new ArrayList<RouteInfo>(mRoutes.size()); |
| 109 | for (RouteInfo route : mRoutes) { |
| 110 | newRoutes.add(routeWithInterface(route)); |
| 111 | } |
| 112 | mRoutes = newRoutes; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 115 | /** |
| 116 | * Gets the interface name for this link. May be {@code null} if not set. |
| 117 | * |
| 118 | * @return The interface name set for this link or {@code null}. |
| 119 | */ |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 120 | public String getInterfaceName() { |
| 121 | return mIfaceName; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 122 | } |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 123 | |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame^] | 124 | /** |
| 125 | * @hide |
| 126 | */ |
| 127 | public List<String> getAllInterfaceNames() { |
| 128 | List<String> interfaceNames = new ArrayList<String>(mStackedLinks.size() + 1); |
Robert Greenwalt | 55187f1 | 2013-03-22 12:00:17 -0700 | [diff] [blame] | 129 | if (mIfaceName != null) interfaceNames.add(new String(mIfaceName)); |
Lorenzo Colitti | 4aa9bcf | 2013-03-20 19:22:58 +0900 | [diff] [blame] | 130 | for (LinkProperties stacked: mStackedLinks.values()) { |
| 131 | interfaceNames.addAll(stacked.getAllInterfaceNames()); |
| 132 | } |
| 133 | return interfaceNames; |
| 134 | } |
| 135 | |
Lorenzo Colitti | d1e0fae | 2013-07-31 23:23:21 +0900 | [diff] [blame] | 136 | /** |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 137 | * Returns all the addresses on this link. We often think of a link having a single address, |
| 138 | * however, particularly with Ipv6 several addresses are typical. Note that the |
| 139 | * {@code LinkProperties} actually contains {@link LinkAddress} objects which also include |
| 140 | * prefix lengths for each address. This is a simplified utility alternative to |
| 141 | * {@link LinkProperties#getLinkAddresses}. |
| 142 | * |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame^] | 143 | * @return An umodifiable {@link List} of {@link InetAddress} for this link. |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 144 | * @hide |
Lorenzo Colitti | d1e0fae | 2013-07-31 23:23:21 +0900 | [diff] [blame] | 145 | */ |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame^] | 146 | public List<InetAddress> getAddresses() { |
| 147 | List<InetAddress> addresses = new ArrayList<InetAddress>(); |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 148 | for (LinkAddress linkAddress : mLinkAddresses) { |
| 149 | addresses.add(linkAddress.getAddress()); |
| 150 | } |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame^] | 151 | return Collections.unmodifiableList(addresses); |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Lorenzo Colitti | d1e0fae | 2013-07-31 23:23:21 +0900 | [diff] [blame] | 154 | /** |
| 155 | * Returns all the addresses on this link and all the links stacked above it. |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 156 | * @hide |
Lorenzo Colitti | d1e0fae | 2013-07-31 23:23:21 +0900 | [diff] [blame] | 157 | */ |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame^] | 158 | public List<InetAddress> getAllAddresses() { |
| 159 | List<InetAddress> addresses = new ArrayList<InetAddress>(); |
Lorenzo Colitti | d1e0fae | 2013-07-31 23:23:21 +0900 | [diff] [blame] | 160 | for (LinkAddress linkAddress : mLinkAddresses) { |
| 161 | addresses.add(linkAddress.getAddress()); |
| 162 | } |
| 163 | for (LinkProperties stacked: mStackedLinks.values()) { |
| 164 | addresses.addAll(stacked.getAllAddresses()); |
| 165 | } |
| 166 | return addresses; |
| 167 | } |
| 168 | |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 169 | private int findLinkAddressIndex(LinkAddress address) { |
| 170 | for (int i = 0; i < mLinkAddresses.size(); i++) { |
| 171 | if (mLinkAddresses.get(i).isSameAddressAs(address)) { |
| 172 | return i; |
| 173 | } |
Lorenzo Colitti | 4faa027 | 2013-08-08 11:00:12 +0900 | [diff] [blame] | 174 | } |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 175 | return -1; |
Lorenzo Colitti | 4faa027 | 2013-08-08 11:00:12 +0900 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /** |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 179 | * Adds a {@link LinkAddress} to this {@code LinkProperties} if a {@link LinkAddress} of the |
| 180 | * same address/prefix does not already exist. If it does exist it is replaced. |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 181 | * @param address The {@code LinkAddress} to add. |
| 182 | * @return true if {@code address} was added or updated, false otherwise. |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame^] | 183 | * @hide |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 184 | */ |
| 185 | public boolean addLinkAddress(LinkAddress address) { |
| 186 | if (address == null) { |
| 187 | return false; |
| 188 | } |
| 189 | int i = findLinkAddressIndex(address); |
| 190 | if (i < 0) { |
| 191 | // Address was not present. Add it. |
| 192 | mLinkAddresses.add(address); |
| 193 | return true; |
| 194 | } else if (mLinkAddresses.get(i).equals(address)) { |
| 195 | // Address was present and has same properties. Do nothing. |
| 196 | return false; |
| 197 | } else { |
| 198 | // Address was present and has different properties. Update it. |
| 199 | mLinkAddresses.set(i, address); |
| 200 | return true; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /** |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 205 | * Removes a {@link LinkAddress} from this {@code LinkProperties}. Specifically, matches |
| 206 | * and {@link LinkAddress} with the same address and prefix. |
| 207 | * |
| 208 | * @param toRemove A {@link LinkAddress} specifying the address to remove. |
Lorenzo Colitti | 4faa027 | 2013-08-08 11:00:12 +0900 | [diff] [blame] | 209 | * @return true if the address was removed, false if it did not exist. |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame^] | 210 | * @hide |
Lorenzo Colitti | 4faa027 | 2013-08-08 11:00:12 +0900 | [diff] [blame] | 211 | */ |
| 212 | public boolean removeLinkAddress(LinkAddress toRemove) { |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 213 | int i = findLinkAddressIndex(toRemove); |
| 214 | if (i >= 0) { |
| 215 | mLinkAddresses.remove(i); |
| 216 | return true; |
| 217 | } |
| 218 | return false; |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Lorenzo Colitti | d1e0fae | 2013-07-31 23:23:21 +0900 | [diff] [blame] | 221 | /** |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 222 | * Returns all the {@link LinkAddress} on this link. Typically a link will have |
| 223 | * one IPv4 address and one or more IPv6 addresses. |
| 224 | * |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame^] | 225 | * @return An unmodifiable {@link List} of {@link LinkAddress} for this link. |
Lorenzo Colitti | d1e0fae | 2013-07-31 23:23:21 +0900 | [diff] [blame] | 226 | */ |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame^] | 227 | public List<LinkAddress> getLinkAddresses() { |
| 228 | return Collections.unmodifiableList(mLinkAddresses); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Lorenzo Colitti | d1e0fae | 2013-07-31 23:23:21 +0900 | [diff] [blame] | 231 | /** |
| 232 | * Returns all the addresses on this link and all the links stacked above it. |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 233 | * @hide |
Lorenzo Colitti | d1e0fae | 2013-07-31 23:23:21 +0900 | [diff] [blame] | 234 | */ |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame^] | 235 | public List<LinkAddress> getAllLinkAddresses() { |
| 236 | List<LinkAddress> addresses = new ArrayList<LinkAddress>(); |
Lorenzo Colitti | d1e0fae | 2013-07-31 23:23:21 +0900 | [diff] [blame] | 237 | addresses.addAll(mLinkAddresses); |
| 238 | for (LinkProperties stacked: mStackedLinks.values()) { |
| 239 | addresses.addAll(stacked.getAllLinkAddresses()); |
| 240 | } |
| 241 | return addresses; |
| 242 | } |
| 243 | |
Lorenzo Colitti | 22f407b | 2013-08-23 20:54:49 +0900 | [diff] [blame] | 244 | /** |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 245 | * Replaces the {@link LinkAddress} in this {@code LinkProperties} with |
| 246 | * the given {@link Collection} of {@link LinkAddress}. |
| 247 | * |
| 248 | * @param addresses The {@link Collection} of {@link LinkAddress} to set in this |
| 249 | * object. |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame^] | 250 | * @hide |
Lorenzo Colitti | 22f407b | 2013-08-23 20:54:49 +0900 | [diff] [blame] | 251 | */ |
| 252 | public void setLinkAddresses(Collection<LinkAddress> addresses) { |
| 253 | mLinkAddresses.clear(); |
| 254 | for (LinkAddress address: addresses) { |
| 255 | addLinkAddress(address); |
| 256 | } |
| 257 | } |
| 258 | |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 259 | /** |
| 260 | * Adds the given {@link InetAddress} to the list of DNS servers. |
| 261 | * |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame^] | 262 | * @param dnsServer The {@link InetAddress} to add to the list of DNS servers. |
| 263 | * @hide |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 264 | */ |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame^] | 265 | public void addDnsServer(InetAddress dnsServer) { |
| 266 | if (dnsServer != null) mDnses.add(dnsServer); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 267 | } |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 268 | |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 269 | /** |
| 270 | * Returns all the {@link LinkAddress} for DNS servers on this link. |
| 271 | * |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame^] | 272 | * @return An umodifiable {@link List} of {@link InetAddress} for DNS servers on |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 273 | * this link. |
| 274 | */ |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame^] | 275 | public List<InetAddress> getDnsServers() { |
| 276 | return Collections.unmodifiableList(mDnses); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 279 | /** |
| 280 | * Sets the DNS domain search path used on this link. |
| 281 | * |
| 282 | * @param domains A {@link String} listing in priority order the comma separated |
| 283 | * domains to search when resolving host names on this link. |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame^] | 284 | * @hide |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 285 | */ |
Robert Greenwalt | 8058f62 | 2012-11-09 10:52:27 -0800 | [diff] [blame] | 286 | public void setDomains(String domains) { |
| 287 | mDomains = domains; |
| 288 | } |
| 289 | |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 290 | /** |
| 291 | * Get the DNS domains search path set for this link. |
| 292 | * |
| 293 | * @return A {@link String} containing the comma separated domains to search when resolving |
| 294 | * host names on this link. |
| 295 | */ |
| 296 | public String getDomains() { |
| 297 | return mDomains; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Sets the Maximum Transmission Unit size to use on this link. This should not be used |
| 302 | * unless the system default (1500) is incorrect. Values less than 68 or greater than |
| 303 | * 10000 will be ignored. |
| 304 | * |
| 305 | * @param mtu The MTU to use for this link. |
| 306 | * @hide |
| 307 | */ |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 308 | public void setMtu(int mtu) { |
| 309 | mMtu = mtu; |
| 310 | } |
| 311 | |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 312 | /** |
| 313 | * Gets any non-default MTU size set for this link. Note that if the default is being used |
| 314 | * this will return 0. |
| 315 | * |
| 316 | * @return The mtu value set for this link. |
| 317 | * @hide |
| 318 | */ |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 319 | public int getMtu() { |
| 320 | return mMtu; |
| 321 | } |
| 322 | |
Lorenzo Colitti | 45b9a5b | 2013-03-08 11:30:39 -0800 | [diff] [blame] | 323 | private RouteInfo routeWithInterface(RouteInfo route) { |
| 324 | return new RouteInfo( |
| 325 | route.getDestination(), |
| 326 | route.getGateway(), |
| 327 | mIfaceName); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 328 | } |
Lorenzo Colitti | 45b9a5b | 2013-03-08 11:30:39 -0800 | [diff] [blame] | 329 | |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 330 | /** |
| 331 | * Adds a {@link RouteInfo} to this {@code LinkProperties}. If the {@link RouteInfo} |
| 332 | * had an interface name set and that differs from the interface set for this |
| 333 | * {@code LinkProperties} an {@link IllegalArgumentException} will be thrown. The |
| 334 | * proper course is to add either un-named or properly named {@link RouteInfo}. |
| 335 | * |
| 336 | * @param route A {@link RouteInfo} to add to this object. |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame^] | 337 | * @hide |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 338 | */ |
Lorenzo Colitti | 45b9a5b | 2013-03-08 11:30:39 -0800 | [diff] [blame] | 339 | public void addRoute(RouteInfo route) { |
| 340 | if (route != null) { |
| 341 | String routeIface = route.getInterface(); |
| 342 | if (routeIface != null && !routeIface.equals(mIfaceName)) { |
Lorenzo Colitti | 1994bc1 | 2013-03-08 19:11:40 -0800 | [diff] [blame] | 343 | throw new IllegalArgumentException( |
Lorenzo Colitti | 45b9a5b | 2013-03-08 11:30:39 -0800 | [diff] [blame] | 344 | "Route added with non-matching interface: " + routeIface + |
Lorenzo Colitti | 1994bc1 | 2013-03-08 19:11:40 -0800 | [diff] [blame] | 345 | " vs. " + mIfaceName); |
Lorenzo Colitti | 45b9a5b | 2013-03-08 11:30:39 -0800 | [diff] [blame] | 346 | } |
| 347 | mRoutes.add(routeWithInterface(route)); |
| 348 | } |
| 349 | } |
| 350 | |
Lorenzo Colitti | 419a4ce | 2013-03-07 10:59:25 -0800 | [diff] [blame] | 351 | /** |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 352 | * Returns all the {@link RouteInfo} set on this link. |
| 353 | * |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame^] | 354 | * @return An unmodifiable {@link List} of {@link RouteInfo} for this link. |
Lorenzo Colitti | 419a4ce | 2013-03-07 10:59:25 -0800 | [diff] [blame] | 355 | */ |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame^] | 356 | public List<RouteInfo> getRoutes() { |
| 357 | return Collections.unmodifiableList(mRoutes); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Lorenzo Colitti | 419a4ce | 2013-03-07 10:59:25 -0800 | [diff] [blame] | 360 | /** |
| 361 | * Returns all the routes on this link and all the links stacked above it. |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 362 | * @hide |
Lorenzo Colitti | 419a4ce | 2013-03-07 10:59:25 -0800 | [diff] [blame] | 363 | */ |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame^] | 364 | public List<RouteInfo> getAllRoutes() { |
| 365 | List<RouteInfo> routes = new ArrayList(); |
Lorenzo Colitti | 419a4ce | 2013-03-07 10:59:25 -0800 | [diff] [blame] | 366 | routes.addAll(mRoutes); |
| 367 | for (LinkProperties stacked: mStackedLinks.values()) { |
| 368 | routes.addAll(stacked.getAllRoutes()); |
| 369 | } |
Robert Greenwalt | 6629bcd | 2013-03-15 11:28:50 -0700 | [diff] [blame] | 370 | return routes; |
Lorenzo Colitti | 419a4ce | 2013-03-07 10:59:25 -0800 | [diff] [blame] | 371 | } |
| 372 | |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 373 | /** |
| 374 | * Sets the recommended {@link ProxyInfo} to use on this link, or {@code null} for none. |
| 375 | * Note that Http Proxies are only a hint - the system recommends their use, but it does |
| 376 | * not enforce it and applications may ignore them. |
| 377 | * |
| 378 | * @param proxy A {@link ProxyInfo} defining the Http Proxy to use on this link. |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame^] | 379 | * @hide |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 380 | */ |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 381 | public void setHttpProxy(ProxyInfo proxy) { |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 382 | mHttpProxy = proxy; |
| 383 | } |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 384 | |
| 385 | /** |
| 386 | * Gets the recommended {@link ProxyInfo} (or {@code null}) set on this link. |
| 387 | * |
| 388 | * @return The {@link ProxyInfo} set on this link |
| 389 | */ |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 390 | public ProxyInfo getHttpProxy() { |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 391 | return mHttpProxy; |
| 392 | } |
| 393 | |
Lorenzo Colitti | 419a4ce | 2013-03-07 10:59:25 -0800 | [diff] [blame] | 394 | /** |
| 395 | * Adds a stacked link. |
| 396 | * |
| 397 | * If there is already a stacked link with the same interfacename as link, |
| 398 | * that link is replaced with link. Otherwise, link is added to the list |
| 399 | * of stacked links. If link is null, nothing changes. |
| 400 | * |
| 401 | * @param link The link to add. |
Lorenzo Colitti | 4faa027 | 2013-08-08 11:00:12 +0900 | [diff] [blame] | 402 | * @return true if the link was stacked, false otherwise. |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 403 | * @hide |
Lorenzo Colitti | 419a4ce | 2013-03-07 10:59:25 -0800 | [diff] [blame] | 404 | */ |
Lorenzo Colitti | 4faa027 | 2013-08-08 11:00:12 +0900 | [diff] [blame] | 405 | public boolean addStackedLink(LinkProperties link) { |
Lorenzo Colitti | 419a4ce | 2013-03-07 10:59:25 -0800 | [diff] [blame] | 406 | if (link != null && link.getInterfaceName() != null) { |
| 407 | mStackedLinks.put(link.getInterfaceName(), link); |
Lorenzo Colitti | 4faa027 | 2013-08-08 11:00:12 +0900 | [diff] [blame] | 408 | return true; |
Lorenzo Colitti | 419a4ce | 2013-03-07 10:59:25 -0800 | [diff] [blame] | 409 | } |
Lorenzo Colitti | 4faa027 | 2013-08-08 11:00:12 +0900 | [diff] [blame] | 410 | return false; |
Lorenzo Colitti | 419a4ce | 2013-03-07 10:59:25 -0800 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | /** |
| 414 | * Removes a stacked link. |
| 415 | * |
| 416 | * If there a stacked link with the same interfacename as link, it is |
| 417 | * removed. Otherwise, nothing changes. |
| 418 | * |
Lorenzo Colitti | 4faa027 | 2013-08-08 11:00:12 +0900 | [diff] [blame] | 419 | * @param link The link to remove. |
| 420 | * @return true if the link was removed, false otherwise. |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 421 | * @hide |
Lorenzo Colitti | 419a4ce | 2013-03-07 10:59:25 -0800 | [diff] [blame] | 422 | */ |
Lorenzo Colitti | 4faa027 | 2013-08-08 11:00:12 +0900 | [diff] [blame] | 423 | public boolean removeStackedLink(LinkProperties link) { |
Lorenzo Colitti | 419a4ce | 2013-03-07 10:59:25 -0800 | [diff] [blame] | 424 | if (link != null && link.getInterfaceName() != null) { |
Lorenzo Colitti | 4faa027 | 2013-08-08 11:00:12 +0900 | [diff] [blame] | 425 | LinkProperties removed = mStackedLinks.remove(link.getInterfaceName()); |
| 426 | return removed != null; |
Lorenzo Colitti | 419a4ce | 2013-03-07 10:59:25 -0800 | [diff] [blame] | 427 | } |
Lorenzo Colitti | 4faa027 | 2013-08-08 11:00:12 +0900 | [diff] [blame] | 428 | return false; |
Lorenzo Colitti | 419a4ce | 2013-03-07 10:59:25 -0800 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | /** |
| 432 | * Returns all the links stacked on top of this link. |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 433 | * @hide |
Lorenzo Colitti | 419a4ce | 2013-03-07 10:59:25 -0800 | [diff] [blame] | 434 | */ |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame^] | 435 | public List<LinkProperties> getStackedLinks() { |
| 436 | List<LinkProperties> stacked = new ArrayList<LinkProperties>(); |
Lorenzo Colitti | 419a4ce | 2013-03-07 10:59:25 -0800 | [diff] [blame] | 437 | for (LinkProperties link : mStackedLinks.values()) { |
| 438 | stacked.add(new LinkProperties(link)); |
| 439 | } |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame^] | 440 | return Collections.unmodifiableList(stacked); |
Lorenzo Colitti | 419a4ce | 2013-03-07 10:59:25 -0800 | [diff] [blame] | 441 | } |
| 442 | |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 443 | /** |
| 444 | * Clears this object to its initial state. |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame^] | 445 | * @hide |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 446 | */ |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 447 | public void clear() { |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 448 | mIfaceName = null; |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 449 | mLinkAddresses.clear(); |
| 450 | mDnses.clear(); |
Robert Greenwalt | 8058f62 | 2012-11-09 10:52:27 -0800 | [diff] [blame] | 451 | mDomains = null; |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 452 | mRoutes.clear(); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 453 | mHttpProxy = null; |
Lorenzo Colitti | 419a4ce | 2013-03-07 10:59:25 -0800 | [diff] [blame] | 454 | mStackedLinks.clear(); |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 455 | mMtu = 0; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | /** |
| 459 | * Implement the Parcelable interface |
| 460 | * @hide |
| 461 | */ |
| 462 | public int describeContents() { |
| 463 | return 0; |
| 464 | } |
| 465 | |
Wink Saville | 1f6408a | 2010-08-27 11:15:18 -0700 | [diff] [blame] | 466 | @Override |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 467 | public String toString() { |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 468 | String ifaceName = (mIfaceName == null ? "" : "InterfaceName: " + mIfaceName + " "); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 469 | |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 470 | String linkAddresses = "LinkAddresses: ["; |
John Wang | 4e90009 | 2011-04-04 12:35:42 -0700 | [diff] [blame] | 471 | for (LinkAddress addr : mLinkAddresses) linkAddresses += addr.toString() + ","; |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 472 | linkAddresses += "] "; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 473 | |
| 474 | String dns = "DnsAddresses: ["; |
Wink Saville | 1f6408a | 2010-08-27 11:15:18 -0700 | [diff] [blame] | 475 | for (InetAddress addr : mDnses) dns += addr.getHostAddress() + ","; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 476 | dns += "] "; |
| 477 | |
Robert Greenwalt | 8058f62 | 2012-11-09 10:52:27 -0800 | [diff] [blame] | 478 | String domainName = "Domains: " + mDomains; |
| 479 | |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 480 | String mtu = "MTU: " + mMtu; |
| 481 | |
Robert Greenwalt | 8058f62 | 2012-11-09 10:52:27 -0800 | [diff] [blame] | 482 | String routes = " Routes: ["; |
Robert Greenwalt | aa70f10 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 483 | for (RouteInfo route : mRoutes) routes += route.toString() + ","; |
| 484 | routes += "] "; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 485 | String proxy = (mHttpProxy == null ? "" : "HttpProxy: " + mHttpProxy.toString() + " "); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 486 | |
Lorenzo Colitti | 419a4ce | 2013-03-07 10:59:25 -0800 | [diff] [blame] | 487 | String stacked = ""; |
| 488 | if (mStackedLinks.values().size() > 0) { |
| 489 | stacked += " Stacked: ["; |
| 490 | for (LinkProperties link: mStackedLinks.values()) { |
| 491 | stacked += " [" + link.toString() + " ],"; |
| 492 | } |
| 493 | stacked += "] "; |
| 494 | } |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 495 | return "{" + ifaceName + linkAddresses + routes + dns + domainName + mtu |
| 496 | + proxy + stacked + "}"; |
Lorenzo Colitti | 419a4ce | 2013-03-07 10:59:25 -0800 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | /** |
| 500 | * Returns true if this link has an IPv4 address. |
| 501 | * |
| 502 | * @return {@code true} if there is an IPv4 address, {@code false} otherwise. |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame^] | 503 | * @hide |
Lorenzo Colitti | 419a4ce | 2013-03-07 10:59:25 -0800 | [diff] [blame] | 504 | */ |
| 505 | public boolean hasIPv4Address() { |
| 506 | for (LinkAddress address : mLinkAddresses) { |
| 507 | if (address.getAddress() instanceof Inet4Address) { |
| 508 | return true; |
| 509 | } |
| 510 | } |
| 511 | return false; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 512 | } |
| 513 | |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 514 | /** |
Lorenzo Colitti | 4faa027 | 2013-08-08 11:00:12 +0900 | [diff] [blame] | 515 | * Returns true if this link has an IPv6 address. |
| 516 | * |
| 517 | * @return {@code true} if there is an IPv6 address, {@code false} otherwise. |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame^] | 518 | * @hide |
Lorenzo Colitti | 4faa027 | 2013-08-08 11:00:12 +0900 | [diff] [blame] | 519 | */ |
| 520 | public boolean hasIPv6Address() { |
| 521 | for (LinkAddress address : mLinkAddresses) { |
| 522 | if (address.getAddress() instanceof Inet6Address) { |
| 523 | return true; |
| 524 | } |
| 525 | } |
| 526 | return false; |
| 527 | } |
| 528 | |
| 529 | /** |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 530 | * Compares this {@code LinkProperties} interface name against the target |
| 531 | * |
| 532 | * @param target LinkProperties to compare. |
| 533 | * @return {@code true} if both are identical, {@code false} otherwise. |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 534 | * @hide |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 535 | */ |
| 536 | public boolean isIdenticalInterfaceName(LinkProperties target) { |
| 537 | return TextUtils.equals(getInterfaceName(), target.getInterfaceName()); |
| 538 | } |
| 539 | |
| 540 | /** |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 541 | * Compares this {@code LinkProperties} interface addresses against the target |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 542 | * |
| 543 | * @param target LinkProperties to compare. |
| 544 | * @return {@code true} if both are identical, {@code false} otherwise. |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 545 | * @hide |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 546 | */ |
| 547 | public boolean isIdenticalAddresses(LinkProperties target) { |
| 548 | Collection<InetAddress> targetAddresses = target.getAddresses(); |
| 549 | Collection<InetAddress> sourceAddresses = getAddresses(); |
| 550 | return (sourceAddresses.size() == targetAddresses.size()) ? |
| 551 | sourceAddresses.containsAll(targetAddresses) : false; |
| 552 | } |
| 553 | |
| 554 | /** |
| 555 | * Compares this {@code LinkProperties} DNS addresses against the target |
| 556 | * |
| 557 | * @param target LinkProperties to compare. |
| 558 | * @return {@code true} if both are identical, {@code false} otherwise. |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 559 | * @hide |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 560 | */ |
| 561 | public boolean isIdenticalDnses(LinkProperties target) { |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame^] | 562 | Collection<InetAddress> targetDnses = target.getDnsServers(); |
Robert Greenwalt | 8058f62 | 2012-11-09 10:52:27 -0800 | [diff] [blame] | 563 | String targetDomains = target.getDomains(); |
| 564 | if (mDomains == null) { |
| 565 | if (targetDomains != null) return false; |
| 566 | } else { |
| 567 | if (mDomains.equals(targetDomains) == false) return false; |
| 568 | } |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 569 | return (mDnses.size() == targetDnses.size()) ? |
| 570 | mDnses.containsAll(targetDnses) : false; |
| 571 | } |
| 572 | |
| 573 | /** |
| 574 | * Compares this {@code LinkProperties} Routes against the target |
| 575 | * |
| 576 | * @param target LinkProperties to compare. |
| 577 | * @return {@code true} if both are identical, {@code false} otherwise. |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 578 | * @hide |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 579 | */ |
| 580 | public boolean isIdenticalRoutes(LinkProperties target) { |
| 581 | Collection<RouteInfo> targetRoutes = target.getRoutes(); |
| 582 | return (mRoutes.size() == targetRoutes.size()) ? |
| 583 | mRoutes.containsAll(targetRoutes) : false; |
| 584 | } |
| 585 | |
| 586 | /** |
| 587 | * Compares this {@code LinkProperties} HttpProxy against the target |
| 588 | * |
| 589 | * @param target LinkProperties to compare. |
| 590 | * @return {@code true} if both are identical, {@code false} otherwise. |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 591 | * @hide |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 592 | */ |
| 593 | public boolean isIdenticalHttpProxy(LinkProperties target) { |
| 594 | return getHttpProxy() == null ? target.getHttpProxy() == null : |
| 595 | getHttpProxy().equals(target.getHttpProxy()); |
| 596 | } |
John Wang | 4e90009 | 2011-04-04 12:35:42 -0700 | [diff] [blame] | 597 | |
Lorenzo Colitti | 419a4ce | 2013-03-07 10:59:25 -0800 | [diff] [blame] | 598 | /** |
| 599 | * Compares this {@code LinkProperties} stacked links against the target |
| 600 | * |
| 601 | * @param target LinkProperties to compare. |
| 602 | * @return {@code true} if both are identical, {@code false} otherwise. |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 603 | * @hide |
Lorenzo Colitti | 419a4ce | 2013-03-07 10:59:25 -0800 | [diff] [blame] | 604 | */ |
| 605 | public boolean isIdenticalStackedLinks(LinkProperties target) { |
Lorenzo Colitti | 213f98b | 2013-04-01 10:47:43 +0900 | [diff] [blame] | 606 | if (!mStackedLinks.keySet().equals(target.mStackedLinks.keySet())) { |
Lorenzo Colitti | 419a4ce | 2013-03-07 10:59:25 -0800 | [diff] [blame] | 607 | return false; |
| 608 | } |
| 609 | for (LinkProperties stacked : mStackedLinks.values()) { |
| 610 | // Hashtable values can never be null. |
| 611 | String iface = stacked.getInterfaceName(); |
| 612 | if (!stacked.equals(target.mStackedLinks.get(iface))) { |
| 613 | return false; |
| 614 | } |
| 615 | } |
| 616 | return true; |
| 617 | } |
| 618 | |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 619 | /** |
| 620 | * Compares this {@code LinkProperties} MTU against the target |
| 621 | * |
Ying Wang | d57de6a | 2013-09-06 22:53:16 -0700 | [diff] [blame] | 622 | * @param target LinkProperties to compare. |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 623 | * @return {@code true} if both are identical, {@code false} otherwise. |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 624 | * @hide |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 625 | */ |
| 626 | public boolean isIdenticalMtu(LinkProperties target) { |
| 627 | return getMtu() == target.getMtu(); |
| 628 | } |
| 629 | |
John Wang | 4e90009 | 2011-04-04 12:35:42 -0700 | [diff] [blame] | 630 | @Override |
| 631 | /** |
| 632 | * Compares this {@code LinkProperties} instance against the target |
| 633 | * LinkProperties in {@code obj}. Two LinkPropertieses are equal if |
| 634 | * all their fields are equal in values. |
| 635 | * |
| 636 | * For collection fields, such as mDnses, containsAll() is used to check |
| 637 | * if two collections contains the same elements, independent of order. |
| 638 | * There are two thoughts regarding containsAll() |
| 639 | * 1. Duplicated elements. eg, (A, B, B) and (A, A, B) are equal. |
| 640 | * 2. Worst case performance is O(n^2). |
| 641 | * |
| 642 | * @param obj the object to be tested for equality. |
| 643 | * @return {@code true} if both objects are equal, {@code false} otherwise. |
| 644 | */ |
| 645 | public boolean equals(Object obj) { |
| 646 | if (this == obj) return true; |
| 647 | |
| 648 | if (!(obj instanceof LinkProperties)) return false; |
| 649 | |
John Wang | 4e90009 | 2011-04-04 12:35:42 -0700 | [diff] [blame] | 650 | LinkProperties target = (LinkProperties) obj; |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 651 | /** |
| 652 | * This method does not check that stacked interfaces are equal, because |
| 653 | * stacked interfaces are not so much a property of the link as a |
| 654 | * description of connections between links. |
| 655 | */ |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 656 | return isIdenticalInterfaceName(target) && |
| 657 | isIdenticalAddresses(target) && |
| 658 | isIdenticalDnses(target) && |
| 659 | isIdenticalRoutes(target) && |
Lorenzo Colitti | 419a4ce | 2013-03-07 10:59:25 -0800 | [diff] [blame] | 660 | isIdenticalHttpProxy(target) && |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 661 | isIdenticalStackedLinks(target) && |
| 662 | isIdenticalMtu(target); |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 663 | } |
John Wang | 4e90009 | 2011-04-04 12:35:42 -0700 | [diff] [blame] | 664 | |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 665 | /** |
Lorenzo Colitti | d1e0fae | 2013-07-31 23:23:21 +0900 | [diff] [blame] | 666 | * Compares the addresses in this LinkProperties with another |
| 667 | * LinkProperties, examining only addresses on the base link. |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 668 | * |
Lorenzo Colitti | d1e0fae | 2013-07-31 23:23:21 +0900 | [diff] [blame] | 669 | * @param target a LinkProperties with the new list of addresses |
| 670 | * @return the differences between the addresses. |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 671 | * @hide |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 672 | */ |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 673 | public CompareResult<LinkAddress> compareAddresses(LinkProperties target) { |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 674 | /* |
| 675 | * Duplicate the LinkAddresses into removed, we will be removing |
| 676 | * address which are common between mLinkAddresses and target |
| 677 | * leaving the addresses that are different. And address which |
| 678 | * are in target but not in mLinkAddresses are placed in the |
| 679 | * addedAddresses. |
| 680 | */ |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 681 | CompareResult<LinkAddress> result = new CompareResult<LinkAddress>(); |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 682 | result.removed = new ArrayList<LinkAddress>(mLinkAddresses); |
| 683 | result.added.clear(); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 684 | if (target != null) { |
| 685 | for (LinkAddress newAddress : target.getLinkAddresses()) { |
| 686 | if (! result.removed.remove(newAddress)) { |
| 687 | result.added.add(newAddress); |
| 688 | } |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 689 | } |
| 690 | } |
| 691 | return result; |
John Wang | 4e90009 | 2011-04-04 12:35:42 -0700 | [diff] [blame] | 692 | } |
| 693 | |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 694 | /** |
Lorenzo Colitti | d1e0fae | 2013-07-31 23:23:21 +0900 | [diff] [blame] | 695 | * Compares the DNS addresses in this LinkProperties with another |
| 696 | * LinkProperties, examining only DNS addresses on the base link. |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 697 | * |
Lorenzo Colitti | d1e0fae | 2013-07-31 23:23:21 +0900 | [diff] [blame] | 698 | * @param target a LinkProperties with the new list of dns addresses |
| 699 | * @return the differences between the DNS addresses. |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 700 | * @hide |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 701 | */ |
| 702 | public CompareResult<InetAddress> compareDnses(LinkProperties target) { |
| 703 | /* |
| 704 | * Duplicate the InetAddresses into removed, we will be removing |
| 705 | * dns address which are common between mDnses and target |
| 706 | * leaving the addresses that are different. And dns address which |
| 707 | * are in target but not in mDnses are placed in the |
| 708 | * addedAddresses. |
| 709 | */ |
| 710 | CompareResult<InetAddress> result = new CompareResult<InetAddress>(); |
| 711 | |
| 712 | result.removed = new ArrayList<InetAddress>(mDnses); |
| 713 | result.added.clear(); |
| 714 | if (target != null) { |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame^] | 715 | for (InetAddress newAddress : target.getDnsServers()) { |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 716 | if (! result.removed.remove(newAddress)) { |
| 717 | result.added.add(newAddress); |
| 718 | } |
| 719 | } |
| 720 | } |
| 721 | return result; |
| 722 | } |
| 723 | |
| 724 | /** |
Lorenzo Colitti | d1e0fae | 2013-07-31 23:23:21 +0900 | [diff] [blame] | 725 | * Compares all routes in this LinkProperties with another LinkProperties, |
| 726 | * examining both the the base link and all stacked links. |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 727 | * |
Lorenzo Colitti | d1e0fae | 2013-07-31 23:23:21 +0900 | [diff] [blame] | 728 | * @param target a LinkProperties with the new list of routes |
| 729 | * @return the differences between the routes. |
Robert Greenwalt | 4f05d55 | 2014-05-18 22:01:38 -0700 | [diff] [blame] | 730 | * @hide |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 731 | */ |
Lorenzo Colitti | d1e0fae | 2013-07-31 23:23:21 +0900 | [diff] [blame] | 732 | public CompareResult<RouteInfo> compareAllRoutes(LinkProperties target) { |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 733 | /* |
| 734 | * Duplicate the RouteInfos into removed, we will be removing |
Lorenzo Colitti | 1994bc1 | 2013-03-08 19:11:40 -0800 | [diff] [blame] | 735 | * routes which are common between mRoutes and target |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 736 | * leaving the routes that are different. And route address which |
| 737 | * are in target but not in mRoutes are placed in added. |
| 738 | */ |
| 739 | CompareResult<RouteInfo> result = new CompareResult<RouteInfo>(); |
| 740 | |
Lorenzo Colitti | 419a4ce | 2013-03-07 10:59:25 -0800 | [diff] [blame] | 741 | result.removed = getAllRoutes(); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 742 | result.added.clear(); |
| 743 | if (target != null) { |
Lorenzo Colitti | 419a4ce | 2013-03-07 10:59:25 -0800 | [diff] [blame] | 744 | for (RouteInfo r : target.getAllRoutes()) { |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 745 | if (! result.removed.remove(r)) { |
| 746 | result.added.add(r); |
| 747 | } |
| 748 | } |
| 749 | } |
| 750 | return result; |
| 751 | } |
| 752 | |
Paul Jensen | 992f252 | 2014-04-28 10:33:11 -0400 | [diff] [blame] | 753 | /** |
| 754 | * Compares all interface names in this LinkProperties with another |
| 755 | * LinkProperties, examining both the the base link and all stacked links. |
| 756 | * |
| 757 | * @param target a LinkProperties with the new list of interface names |
| 758 | * @return the differences between the interface names. |
| 759 | * @hide |
| 760 | */ |
| 761 | public CompareResult<String> compareAllInterfaceNames(LinkProperties target) { |
| 762 | /* |
| 763 | * Duplicate the interface names into removed, we will be removing |
| 764 | * interface names which are common between this and target |
| 765 | * leaving the interface names that are different. And interface names which |
| 766 | * are in target but not in this are placed in added. |
| 767 | */ |
| 768 | CompareResult<String> result = new CompareResult<String>(); |
| 769 | |
| 770 | result.removed = getAllInterfaceNames(); |
| 771 | result.added.clear(); |
| 772 | if (target != null) { |
| 773 | for (String r : target.getAllInterfaceNames()) { |
| 774 | if (! result.removed.remove(r)) { |
| 775 | result.added.add(r); |
| 776 | } |
| 777 | } |
| 778 | } |
| 779 | return result; |
| 780 | } |
| 781 | |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 782 | |
John Wang | 4e90009 | 2011-04-04 12:35:42 -0700 | [diff] [blame] | 783 | @Override |
| 784 | /** |
| 785 | * generate hashcode based on significant fields |
| 786 | * Equal objects must produce the same hash code, while unequal objects |
| 787 | * may have the same hash codes. |
| 788 | */ |
| 789 | public int hashCode() { |
| 790 | return ((null == mIfaceName) ? 0 : mIfaceName.hashCode() |
| 791 | + mLinkAddresses.size() * 31 |
| 792 | + mDnses.size() * 37 |
Robert Greenwalt | 8058f62 | 2012-11-09 10:52:27 -0800 | [diff] [blame] | 793 | + ((null == mDomains) ? 0 : mDomains.hashCode()) |
Robert Greenwalt | aa70f10 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 794 | + mRoutes.size() * 41 |
Lorenzo Colitti | 419a4ce | 2013-03-07 10:59:25 -0800 | [diff] [blame] | 795 | + ((null == mHttpProxy) ? 0 : mHttpProxy.hashCode()) |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 796 | + mStackedLinks.hashCode() * 47) |
| 797 | + mMtu * 51; |
John Wang | 4e90009 | 2011-04-04 12:35:42 -0700 | [diff] [blame] | 798 | } |
| 799 | |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 800 | /** |
| 801 | * Implement the Parcelable interface. |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 802 | */ |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 803 | public void writeToParcel(Parcel dest, int flags) { |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 804 | dest.writeString(getInterfaceName()); |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 805 | dest.writeInt(mLinkAddresses.size()); |
| 806 | for(LinkAddress linkAddress : mLinkAddresses) { |
| 807 | dest.writeParcelable(linkAddress, flags); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 808 | } |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 809 | |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 810 | dest.writeInt(mDnses.size()); |
| 811 | for(InetAddress d : mDnses) { |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 812 | dest.writeByteArray(d.getAddress()); |
| 813 | } |
Robert Greenwalt | 8058f62 | 2012-11-09 10:52:27 -0800 | [diff] [blame] | 814 | dest.writeString(mDomains); |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 815 | dest.writeInt(mMtu); |
Robert Greenwalt | aa70f10 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 816 | dest.writeInt(mRoutes.size()); |
| 817 | for(RouteInfo route : mRoutes) { |
| 818 | dest.writeParcelable(route, flags); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 819 | } |
Robert Greenwalt | 992564e | 2011-02-09 13:56:06 -0800 | [diff] [blame] | 820 | |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 821 | if (mHttpProxy != null) { |
| 822 | dest.writeByte((byte)1); |
| 823 | dest.writeParcelable(mHttpProxy, flags); |
| 824 | } else { |
| 825 | dest.writeByte((byte)0); |
| 826 | } |
Lorenzo Colitti | 419a4ce | 2013-03-07 10:59:25 -0800 | [diff] [blame] | 827 | ArrayList<LinkProperties> stackedLinks = new ArrayList(mStackedLinks.values()); |
| 828 | dest.writeList(stackedLinks); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | /** |
| 832 | * Implement the Parcelable interface. |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 833 | */ |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 834 | public static final Creator<LinkProperties> CREATOR = |
| 835 | new Creator<LinkProperties>() { |
| 836 | public LinkProperties createFromParcel(Parcel in) { |
| 837 | LinkProperties netProp = new LinkProperties(); |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 838 | |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 839 | String iface = in.readString(); |
| 840 | if (iface != null) { |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 841 | netProp.setInterfaceName(iface); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 842 | } |
| 843 | int addressCount = in.readInt(); |
| 844 | for (int i=0; i<addressCount; i++) { |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 845 | netProp.addLinkAddress((LinkAddress)in.readParcelable(null)); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 846 | } |
| 847 | addressCount = in.readInt(); |
| 848 | for (int i=0; i<addressCount; i++) { |
| 849 | try { |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame^] | 850 | netProp.addDnsServer(InetAddress.getByAddress(in.createByteArray())); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 851 | } catch (UnknownHostException e) { } |
| 852 | } |
Robert Greenwalt | 8058f62 | 2012-11-09 10:52:27 -0800 | [diff] [blame] | 853 | netProp.setDomains(in.readString()); |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 854 | netProp.setMtu(in.readInt()); |
Robert Greenwalt | 992564e | 2011-02-09 13:56:06 -0800 | [diff] [blame] | 855 | addressCount = in.readInt(); |
| 856 | for (int i=0; i<addressCount; i++) { |
Robert Greenwalt | aa70f10 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 857 | netProp.addRoute((RouteInfo)in.readParcelable(null)); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 858 | } |
| 859 | if (in.readByte() == 1) { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 860 | netProp.setHttpProxy((ProxyInfo)in.readParcelable(null)); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 861 | } |
Lorenzo Colitti | 419a4ce | 2013-03-07 10:59:25 -0800 | [diff] [blame] | 862 | ArrayList<LinkProperties> stackedLinks = new ArrayList<LinkProperties>(); |
| 863 | in.readList(stackedLinks, LinkProperties.class.getClassLoader()); |
| 864 | for (LinkProperties stackedLink: stackedLinks) { |
| 865 | netProp.addStackedLink(stackedLink); |
| 866 | } |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 867 | return netProp; |
| 868 | } |
| 869 | |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 870 | public LinkProperties[] newArray(int size) { |
| 871 | return new LinkProperties[size]; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 872 | } |
| 873 | }; |
| 874 | } |