/frameworks/base/core/java/android/net/LinkProperties.java

https://gitlab.com/brian0218/rk3066_r-box_android4.2.2_sdk · Java · 435 lines · 242 code · 47 blank · 146 comment · 55 complexity · 0f0a6773f46ba000267eed1e093c8af5 MD5 · raw file

  1. /*
  2. * Copyright (C) 2010 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package android.net;
  17. import android.net.ProxyProperties;
  18. import android.os.Parcelable;
  19. import android.os.Parcel;
  20. import android.text.TextUtils;
  21. import java.net.InetAddress;
  22. import java.net.UnknownHostException;
  23. import java.util.ArrayList;
  24. import java.util.Collection;
  25. import java.util.Collections;
  26. /**
  27. * Describes the properties of a network link.
  28. *
  29. * A link represents a connection to a network.
  30. * It may have multiple addresses and multiple gateways,
  31. * multiple dns servers but only one http proxy.
  32. *
  33. * Because it's a single network, the dns's
  34. * are interchangeable and don't need associating with
  35. * particular addresses. The gateways similarly don't
  36. * need associating with particular addresses.
  37. *
  38. * A dual stack interface works fine in this model:
  39. * each address has it's own prefix length to describe
  40. * the local network. The dns servers all return
  41. * both v4 addresses and v6 addresses regardless of the
  42. * address family of the server itself (rfc4213) and we
  43. * don't care which is used. The gateways will be
  44. * selected based on the destination address and the
  45. * source address has no relavence.
  46. * @hide
  47. */
  48. public class LinkProperties implements Parcelable {
  49. String mIfaceName;
  50. private Collection<LinkAddress> mLinkAddresses = new ArrayList<LinkAddress>();
  51. private Collection<InetAddress> mDnses = new ArrayList<InetAddress>();
  52. private Collection<RouteInfo> mRoutes = new ArrayList<RouteInfo>();
  53. private ProxyProperties mHttpProxy;
  54. public static class CompareResult<T> {
  55. public Collection<T> removed = new ArrayList<T>();
  56. public Collection<T> added = new ArrayList<T>();
  57. @Override
  58. public String toString() {
  59. String retVal = "removed=[";
  60. for (T addr : removed) retVal += addr.toString() + ",";
  61. retVal += "] added=[";
  62. for (T addr : added) retVal += addr.toString() + ",";
  63. retVal += "]";
  64. return retVal;
  65. }
  66. }
  67. public LinkProperties() {
  68. clear();
  69. }
  70. // copy constructor instead of clone
  71. public LinkProperties(LinkProperties source) {
  72. if (source != null) {
  73. mIfaceName = source.getInterfaceName();
  74. for (LinkAddress l : source.getLinkAddresses()) mLinkAddresses.add(l);
  75. for (InetAddress i : source.getDnses()) mDnses.add(i);
  76. for (RouteInfo r : source.getRoutes()) mRoutes.add(r);
  77. mHttpProxy = (source.getHttpProxy() == null) ?
  78. null : new ProxyProperties(source.getHttpProxy());
  79. }
  80. }
  81. public void setInterfaceName(String iface) {
  82. mIfaceName = iface;
  83. }
  84. public String getInterfaceName() {
  85. return mIfaceName;
  86. }
  87. public Collection<InetAddress> getAddresses() {
  88. Collection<InetAddress> addresses = new ArrayList<InetAddress>();
  89. for (LinkAddress linkAddress : mLinkAddresses) {
  90. addresses.add(linkAddress.getAddress());
  91. }
  92. return Collections.unmodifiableCollection(addresses);
  93. }
  94. public void addLinkAddress(LinkAddress address) {
  95. if (address != null) mLinkAddresses.add(address);
  96. }
  97. public Collection<LinkAddress> getLinkAddresses() {
  98. return Collections.unmodifiableCollection(mLinkAddresses);
  99. }
  100. public void addDns(InetAddress dns) {
  101. if (dns != null) mDnses.add(dns);
  102. }
  103. public Collection<InetAddress> getDnses() {
  104. return Collections.unmodifiableCollection(mDnses);
  105. }
  106. public void addRoute(RouteInfo route) {
  107. if (route != null) mRoutes.add(route);
  108. }
  109. public Collection<RouteInfo> getRoutes() {
  110. return Collections.unmodifiableCollection(mRoutes);
  111. }
  112. public void setHttpProxy(ProxyProperties proxy) {
  113. mHttpProxy = proxy;
  114. }
  115. public ProxyProperties getHttpProxy() {
  116. return mHttpProxy;
  117. }
  118. public void clear() {
  119. mIfaceName = null;
  120. mLinkAddresses.clear();
  121. mDnses.clear();
  122. mRoutes.clear();
  123. mHttpProxy = null;
  124. }
  125. /**
  126. * Implement the Parcelable interface
  127. * @hide
  128. */
  129. public int describeContents() {
  130. return 0;
  131. }
  132. @Override
  133. public String toString() {
  134. String ifaceName = (mIfaceName == null ? "" : "InterfaceName: " + mIfaceName + " ");
  135. String linkAddresses = "LinkAddresses: [";
  136. for (LinkAddress addr : mLinkAddresses) linkAddresses += addr.toString() + ",";
  137. linkAddresses += "] ";
  138. String dns = "DnsAddresses: [";
  139. for (InetAddress addr : mDnses) dns += addr.getHostAddress() + ",";
  140. dns += "] ";
  141. String routes = "Routes: [";
  142. for (RouteInfo route : mRoutes) routes += route.toString() + ",";
  143. routes += "] ";
  144. String proxy = (mHttpProxy == null ? "" : "HttpProxy: " + mHttpProxy.toString() + " ");
  145. return ifaceName + linkAddresses + routes + dns + proxy;
  146. }
  147. /**
  148. * Compares this {@code LinkProperties} interface name against the target
  149. *
  150. * @param target LinkProperties to compare.
  151. * @return {@code true} if both are identical, {@code false} otherwise.
  152. */
  153. public boolean isIdenticalInterfaceName(LinkProperties target) {
  154. return TextUtils.equals(getInterfaceName(), target.getInterfaceName());
  155. }
  156. /**
  157. * Compares this {@code LinkProperties} interface name against the target
  158. *
  159. * @param target LinkProperties to compare.
  160. * @return {@code true} if both are identical, {@code false} otherwise.
  161. */
  162. public boolean isIdenticalAddresses(LinkProperties target) {
  163. Collection<InetAddress> targetAddresses = target.getAddresses();
  164. Collection<InetAddress> sourceAddresses = getAddresses();
  165. return (sourceAddresses.size() == targetAddresses.size()) ?
  166. sourceAddresses.containsAll(targetAddresses) : false;
  167. }
  168. /**
  169. * Compares this {@code LinkProperties} DNS addresses against the target
  170. *
  171. * @param target LinkProperties to compare.
  172. * @return {@code true} if both are identical, {@code false} otherwise.
  173. */
  174. public boolean isIdenticalDnses(LinkProperties target) {
  175. Collection<InetAddress> targetDnses = target.getDnses();
  176. return (mDnses.size() == targetDnses.size()) ?
  177. mDnses.containsAll(targetDnses) : false;
  178. }
  179. /**
  180. * Compares this {@code LinkProperties} Routes against the target
  181. *
  182. * @param target LinkProperties to compare.
  183. * @return {@code true} if both are identical, {@code false} otherwise.
  184. */
  185. public boolean isIdenticalRoutes(LinkProperties target) {
  186. Collection<RouteInfo> targetRoutes = target.getRoutes();
  187. return (mRoutes.size() == targetRoutes.size()) ?
  188. mRoutes.containsAll(targetRoutes) : false;
  189. }
  190. /**
  191. * Compares this {@code LinkProperties} HttpProxy against the target
  192. *
  193. * @param target LinkProperties to compare.
  194. * @return {@code true} if both are identical, {@code false} otherwise.
  195. */
  196. public boolean isIdenticalHttpProxy(LinkProperties target) {
  197. return getHttpProxy() == null ? target.getHttpProxy() == null :
  198. getHttpProxy().equals(target.getHttpProxy());
  199. }
  200. @Override
  201. /**
  202. * Compares this {@code LinkProperties} instance against the target
  203. * LinkProperties in {@code obj}. Two LinkPropertieses are equal if
  204. * all their fields are equal in values.
  205. *
  206. * For collection fields, such as mDnses, containsAll() is used to check
  207. * if two collections contains the same elements, independent of order.
  208. * There are two thoughts regarding containsAll()
  209. * 1. Duplicated elements. eg, (A, B, B) and (A, A, B) are equal.
  210. * 2. Worst case performance is O(n^2).
  211. *
  212. * @param obj the object to be tested for equality.
  213. * @return {@code true} if both objects are equal, {@code false} otherwise.
  214. */
  215. public boolean equals(Object obj) {
  216. if (this == obj) return true;
  217. if (!(obj instanceof LinkProperties)) return false;
  218. LinkProperties target = (LinkProperties) obj;
  219. return isIdenticalInterfaceName(target) &&
  220. isIdenticalAddresses(target) &&
  221. isIdenticalDnses(target) &&
  222. isIdenticalRoutes(target) &&
  223. isIdenticalHttpProxy(target);
  224. }
  225. /**
  226. * Return two lists, a list of addresses that would be removed from
  227. * mLinkAddresses and a list of addresses that would be added to
  228. * mLinkAddress which would then result in target and mLinkAddresses
  229. * being the same list.
  230. *
  231. * @param target is a LinkProperties with the new list of addresses
  232. * @return the removed and added lists.
  233. */
  234. public CompareResult<LinkAddress> compareAddresses(LinkProperties target) {
  235. /*
  236. * Duplicate the LinkAddresses into removed, we will be removing
  237. * address which are common between mLinkAddresses and target
  238. * leaving the addresses that are different. And address which
  239. * are in target but not in mLinkAddresses are placed in the
  240. * addedAddresses.
  241. */
  242. CompareResult<LinkAddress> result = new CompareResult<LinkAddress>();
  243. result.removed = new ArrayList<LinkAddress>(mLinkAddresses);
  244. result.added.clear();
  245. if (target != null) {
  246. for (LinkAddress newAddress : target.getLinkAddresses()) {
  247. if (! result.removed.remove(newAddress)) {
  248. result.added.add(newAddress);
  249. }
  250. }
  251. }
  252. return result;
  253. }
  254. /**
  255. * Return two lists, a list of dns addresses that would be removed from
  256. * mDnses and a list of addresses that would be added to
  257. * mDnses which would then result in target and mDnses
  258. * being the same list.
  259. *
  260. * @param target is a LinkProperties with the new list of dns addresses
  261. * @return the removed and added lists.
  262. */
  263. public CompareResult<InetAddress> compareDnses(LinkProperties target) {
  264. /*
  265. * Duplicate the InetAddresses into removed, we will be removing
  266. * dns address which are common between mDnses and target
  267. * leaving the addresses that are different. And dns address which
  268. * are in target but not in mDnses are placed in the
  269. * addedAddresses.
  270. */
  271. CompareResult<InetAddress> result = new CompareResult<InetAddress>();
  272. result.removed = new ArrayList<InetAddress>(mDnses);
  273. result.added.clear();
  274. if (target != null) {
  275. for (InetAddress newAddress : target.getDnses()) {
  276. if (! result.removed.remove(newAddress)) {
  277. result.added.add(newAddress);
  278. }
  279. }
  280. }
  281. return result;
  282. }
  283. /**
  284. * Return two lists, a list of routes that would be removed from
  285. * mRoutes and a list of routes that would be added to
  286. * mRoutes which would then result in target and mRoutes
  287. * being the same list.
  288. *
  289. * @param target is a LinkProperties with the new list of routes
  290. * @return the removed and added lists.
  291. */
  292. public CompareResult<RouteInfo> compareRoutes(LinkProperties target) {
  293. /*
  294. * Duplicate the RouteInfos into removed, we will be removing
  295. * routes which are common between mDnses and target
  296. * leaving the routes that are different. And route address which
  297. * are in target but not in mRoutes are placed in added.
  298. */
  299. CompareResult<RouteInfo> result = new CompareResult<RouteInfo>();
  300. result.removed = new ArrayList<RouteInfo>(mRoutes);
  301. result.added.clear();
  302. if (target != null) {
  303. for (RouteInfo r : target.getRoutes()) {
  304. if (! result.removed.remove(r)) {
  305. result.added.add(r);
  306. }
  307. }
  308. }
  309. return result;
  310. }
  311. @Override
  312. /**
  313. * generate hashcode based on significant fields
  314. * Equal objects must produce the same hash code, while unequal objects
  315. * may have the same hash codes.
  316. */
  317. public int hashCode() {
  318. return ((null == mIfaceName) ? 0 : mIfaceName.hashCode()
  319. + mLinkAddresses.size() * 31
  320. + mDnses.size() * 37
  321. + mRoutes.size() * 41
  322. + ((null == mHttpProxy) ? 0 : mHttpProxy.hashCode()));
  323. }
  324. /**
  325. * Implement the Parcelable interface.
  326. * @hide
  327. */
  328. public void writeToParcel(Parcel dest, int flags) {
  329. dest.writeString(getInterfaceName());
  330. dest.writeInt(mLinkAddresses.size());
  331. for(LinkAddress linkAddress : mLinkAddresses) {
  332. dest.writeParcelable(linkAddress, flags);
  333. }
  334. dest.writeInt(mDnses.size());
  335. for(InetAddress d : mDnses) {
  336. dest.writeByteArray(d.getAddress());
  337. }
  338. dest.writeInt(mRoutes.size());
  339. for(RouteInfo route : mRoutes) {
  340. dest.writeParcelable(route, flags);
  341. }
  342. if (mHttpProxy != null) {
  343. dest.writeByte((byte)1);
  344. dest.writeParcelable(mHttpProxy, flags);
  345. } else {
  346. dest.writeByte((byte)0);
  347. }
  348. }
  349. /**
  350. * Implement the Parcelable interface.
  351. * @hide
  352. */
  353. public static final Creator<LinkProperties> CREATOR =
  354. new Creator<LinkProperties>() {
  355. public LinkProperties createFromParcel(Parcel in) {
  356. LinkProperties netProp = new LinkProperties();
  357. String iface = in.readString();
  358. if (iface != null) {
  359. try {
  360. netProp.setInterfaceName(iface);
  361. } catch (Exception e) {
  362. return null;
  363. }
  364. }
  365. int addressCount = in.readInt();
  366. for (int i=0; i<addressCount; i++) {
  367. netProp.addLinkAddress((LinkAddress)in.readParcelable(null));
  368. }
  369. addressCount = in.readInt();
  370. for (int i=0; i<addressCount; i++) {
  371. try {
  372. netProp.addDns(InetAddress.getByAddress(in.createByteArray()));
  373. } catch (UnknownHostException e) { }
  374. }
  375. addressCount = in.readInt();
  376. for (int i=0; i<addressCount; i++) {
  377. netProp.addRoute((RouteInfo)in.readParcelable(null));
  378. }
  379. if (in.readByte() == 1) {
  380. netProp.setHttpProxy((ProxyProperties)in.readParcelable(null));
  381. }
  382. return netProp;
  383. }
  384. public LinkProperties[] newArray(int size) {
  385. return new LinkProperties[size];
  386. }
  387. };
  388. }