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 | |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 19 | import android.net.ProxyProperties; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 20 | import android.os.Parcelable; |
| 21 | import android.os.Parcel; |
| 22 | import android.util.Log; |
| 23 | |
| 24 | import java.net.InetAddress; |
| 25 | import java.net.NetworkInterface; |
| 26 | import java.net.SocketException; |
| 27 | import java.net.UnknownHostException; |
| 28 | import java.util.ArrayList; |
| 29 | import java.util.Collection; |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 30 | import java.util.Collections; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 31 | |
| 32 | /** |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 33 | * Describes the properties of a network link. |
Robert Greenwalt | 992564e | 2011-02-09 13:56:06 -0800 | [diff] [blame^] | 34 | * |
| 35 | * A link represents a connection to a network. |
| 36 | * It may have multiple addresses and multiple gateways, |
| 37 | * multiple dns servers but only one http proxy. |
| 38 | * |
| 39 | * Because it's a single network, the dns's |
| 40 | * are interchangeable and don't need associating with |
| 41 | * particular addresses. The gateways similarly don't |
| 42 | * need associating with particular addresses. |
| 43 | * |
| 44 | * A dual stack interface works fine in this model: |
| 45 | * each address has it's own prefix length to describe |
| 46 | * the local network. The dns servers all return |
| 47 | * both v4 addresses and v6 addresses regardless of the |
| 48 | * address family of the server itself (rfc4213) and we |
| 49 | * don't care which is used. The gateways will be |
| 50 | * selected based on the destination address and the |
| 51 | * source address has no relavence. |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 52 | * @hide |
| 53 | */ |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 54 | public class LinkProperties implements Parcelable { |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 55 | |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 56 | String mIfaceName; |
| 57 | private Collection<LinkAddress> mLinkAddresses; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 58 | private Collection<InetAddress> mDnses; |
Robert Greenwalt | 992564e | 2011-02-09 13:56:06 -0800 | [diff] [blame^] | 59 | private Collection<InetAddress> mGateways; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 60 | private ProxyProperties mHttpProxy; |
| 61 | |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 62 | public LinkProperties() { |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 63 | clear(); |
| 64 | } |
| 65 | |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 66 | // copy constructor instead of clone |
| 67 | public LinkProperties(LinkProperties source) { |
Irfan Sheriff | ef6c143 | 2010-08-30 20:37:17 -0700 | [diff] [blame] | 68 | if (source != null) { |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 69 | mIfaceName = source.getInterfaceName(); |
| 70 | mLinkAddresses = source.getLinkAddresses(); |
Irfan Sheriff | ef6c143 | 2010-08-30 20:37:17 -0700 | [diff] [blame] | 71 | mDnses = source.getDnses(); |
Robert Greenwalt | 992564e | 2011-02-09 13:56:06 -0800 | [diff] [blame^] | 72 | mGateways = source.getGateways(); |
Irfan Sheriff | ef6c143 | 2010-08-30 20:37:17 -0700 | [diff] [blame] | 73 | mHttpProxy = new ProxyProperties(source.getHttpProxy()); |
| 74 | } |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 77 | public void setInterfaceName(String iface) { |
| 78 | mIfaceName = iface; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 81 | public String getInterfaceName() { |
| 82 | return mIfaceName; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 83 | } |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 84 | |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 85 | public Collection<InetAddress> getAddresses() { |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 86 | Collection<InetAddress> addresses = new ArrayList<InetAddress>(); |
| 87 | for (LinkAddress linkAddress : mLinkAddresses) { |
| 88 | addresses.add(linkAddress.getAddress()); |
| 89 | } |
| 90 | return Collections.unmodifiableCollection(addresses); |
| 91 | } |
| 92 | |
| 93 | public void addLinkAddress(LinkAddress address) { |
| 94 | mLinkAddresses.add(address); |
| 95 | } |
| 96 | |
| 97 | public Collection<LinkAddress> getLinkAddresses() { |
| 98 | return Collections.unmodifiableCollection(mLinkAddresses); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 101 | public void addDns(InetAddress dns) { |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 102 | mDnses.add(dns); |
| 103 | } |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 104 | |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 105 | public Collection<InetAddress> getDnses() { |
| 106 | return Collections.unmodifiableCollection(mDnses); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Robert Greenwalt | 992564e | 2011-02-09 13:56:06 -0800 | [diff] [blame^] | 109 | public void addGateway(InetAddress gateway) { |
| 110 | mGateways.add(gateway); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 111 | } |
Robert Greenwalt | 992564e | 2011-02-09 13:56:06 -0800 | [diff] [blame^] | 112 | public Collection<InetAddress> getGateways() { |
| 113 | return Collections.unmodifiableCollection(mGateways); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 116 | public void setHttpProxy(ProxyProperties proxy) { |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 117 | mHttpProxy = proxy; |
| 118 | } |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 119 | public ProxyProperties getHttpProxy() { |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 120 | return mHttpProxy; |
| 121 | } |
| 122 | |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 123 | public void clear() { |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 124 | mIfaceName = null; |
| 125 | mLinkAddresses = new ArrayList<LinkAddress>(); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 126 | mDnses = new ArrayList<InetAddress>(); |
Robert Greenwalt | 992564e | 2011-02-09 13:56:06 -0800 | [diff] [blame^] | 127 | mGateways = new ArrayList<InetAddress>(); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 128 | mHttpProxy = null; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Implement the Parcelable interface |
| 133 | * @hide |
| 134 | */ |
| 135 | public int describeContents() { |
| 136 | return 0; |
| 137 | } |
| 138 | |
Wink Saville | 1f6408a | 2010-08-27 11:15:18 -0700 | [diff] [blame] | 139 | @Override |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 140 | public String toString() { |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 141 | String ifaceName = (mIfaceName == null ? "" : "InterfaceName: " + mIfaceName + " "); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 142 | |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 143 | String linkAddresses = "LinkAddresses: ["; |
| 144 | for (LinkAddress addr : mLinkAddresses) linkAddresses += addr.toString(); |
| 145 | linkAddresses += "] "; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 146 | |
| 147 | String dns = "DnsAddresses: ["; |
Wink Saville | 1f6408a | 2010-08-27 11:15:18 -0700 | [diff] [blame] | 148 | for (InetAddress addr : mDnses) dns += addr.getHostAddress() + ","; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 149 | dns += "] "; |
| 150 | |
Robert Greenwalt | 992564e | 2011-02-09 13:56:06 -0800 | [diff] [blame^] | 151 | String gateways = "Gateways: ["; |
| 152 | for (InetAddress gw : mGateways) gateways += gw.getHostAddress() + ","; |
| 153 | gateways += "] "; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 154 | String proxy = (mHttpProxy == null ? "" : "HttpProxy: " + mHttpProxy.toString() + " "); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 155 | |
Robert Greenwalt | 992564e | 2011-02-09 13:56:06 -0800 | [diff] [blame^] | 156 | return ifaceName + linkAddresses + gateways + dns + proxy; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Implement the Parcelable interface. |
| 161 | * @hide |
| 162 | */ |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 163 | public void writeToParcel(Parcel dest, int flags) { |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 164 | dest.writeString(getInterfaceName()); |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 165 | dest.writeInt(mLinkAddresses.size()); |
| 166 | for(LinkAddress linkAddress : mLinkAddresses) { |
| 167 | dest.writeParcelable(linkAddress, flags); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 168 | } |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 169 | |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 170 | dest.writeInt(mDnses.size()); |
| 171 | for(InetAddress d : mDnses) { |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 172 | dest.writeByteArray(d.getAddress()); |
| 173 | } |
Robert Greenwalt | 992564e | 2011-02-09 13:56:06 -0800 | [diff] [blame^] | 174 | |
| 175 | dest.writeInt(mGateways.size()); |
| 176 | for(InetAddress gw : mGateways) { |
| 177 | dest.writeByteArray(gw.getAddress()); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 178 | } |
Robert Greenwalt | 992564e | 2011-02-09 13:56:06 -0800 | [diff] [blame^] | 179 | |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 180 | if (mHttpProxy != null) { |
| 181 | dest.writeByte((byte)1); |
| 182 | dest.writeParcelable(mHttpProxy, flags); |
| 183 | } else { |
| 184 | dest.writeByte((byte)0); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Implement the Parcelable interface. |
| 190 | * @hide |
| 191 | */ |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 192 | public static final Creator<LinkProperties> CREATOR = |
| 193 | new Creator<LinkProperties>() { |
| 194 | public LinkProperties createFromParcel(Parcel in) { |
| 195 | LinkProperties netProp = new LinkProperties(); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 196 | String iface = in.readString(); |
| 197 | if (iface != null) { |
| 198 | try { |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 199 | netProp.setInterfaceName(iface); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 200 | } catch (Exception e) { |
| 201 | return null; |
| 202 | } |
| 203 | } |
| 204 | int addressCount = in.readInt(); |
| 205 | for (int i=0; i<addressCount; i++) { |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 206 | netProp.addLinkAddress((LinkAddress)in.readParcelable(null)); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 207 | } |
| 208 | addressCount = in.readInt(); |
| 209 | for (int i=0; i<addressCount; i++) { |
| 210 | try { |
Irfan Sheriff | 1cf56ab | 2010-08-04 15:15:49 -0700 | [diff] [blame] | 211 | netProp.addDns(InetAddress.getByAddress(in.createByteArray())); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 212 | } catch (UnknownHostException e) { } |
| 213 | } |
Robert Greenwalt | 992564e | 2011-02-09 13:56:06 -0800 | [diff] [blame^] | 214 | addressCount = in.readInt(); |
| 215 | for (int i=0; i<addressCount; i++) { |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 216 | try { |
Robert Greenwalt | 992564e | 2011-02-09 13:56:06 -0800 | [diff] [blame^] | 217 | netProp.addGateway(InetAddress.getByAddress(in.createByteArray())); |
| 218 | } catch (UnknownHostException e) { } |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 219 | } |
| 220 | if (in.readByte() == 1) { |
| 221 | netProp.setHttpProxy((ProxyProperties)in.readParcelable(null)); |
| 222 | } |
| 223 | return netProp; |
| 224 | } |
| 225 | |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 226 | public LinkProperties[] newArray(int size) { |
| 227 | return new LinkProperties[size]; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 228 | } |
| 229 | }; |
| 230 | } |