blob: b6e9751a520b0c10ce2bfe95be0ed900f9e844cd [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
Robert Greenwalt37e65eb2010-08-30 10:56:47 -070019import android.net.ProxyProperties;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070020import android.os.Parcelable;
21import android.os.Parcel;
22import android.util.Log;
23
24import java.net.InetAddress;
25import java.net.NetworkInterface;
26import java.net.SocketException;
27import java.net.UnknownHostException;
28import java.util.ArrayList;
29import java.util.Collection;
Robert Greenwalt37e65eb2010-08-30 10:56:47 -070030import java.util.Collections;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070031
32/**
Robert Greenwalt37e65eb2010-08-30 10:56:47 -070033 * Describes the properties of a network link.
Robert Greenwalt992564e2011-02-09 13:56:06 -080034 *
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 Greenwalt47f69fe2010-06-15 15:43:39 -070052 * @hide
53 */
Robert Greenwalt37e65eb2010-08-30 10:56:47 -070054public class LinkProperties implements Parcelable {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070055
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070056 String mIfaceName;
57 private Collection<LinkAddress> mLinkAddresses;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070058 private Collection<InetAddress> mDnses;
Robert Greenwalt992564e2011-02-09 13:56:06 -080059 private Collection<InetAddress> mGateways;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070060 private ProxyProperties mHttpProxy;
61
Robert Greenwalt37e65eb2010-08-30 10:56:47 -070062 public LinkProperties() {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070063 clear();
64 }
65
Robert Greenwalt37e65eb2010-08-30 10:56:47 -070066 // copy constructor instead of clone
67 public LinkProperties(LinkProperties source) {
Irfan Sheriffef6c1432010-08-30 20:37:17 -070068 if (source != null) {
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070069 mIfaceName = source.getInterfaceName();
70 mLinkAddresses = source.getLinkAddresses();
Irfan Sheriffef6c1432010-08-30 20:37:17 -070071 mDnses = source.getDnses();
Robert Greenwalt992564e2011-02-09 13:56:06 -080072 mGateways = source.getGateways();
Irfan Sheriffef6c1432010-08-30 20:37:17 -070073 mHttpProxy = new ProxyProperties(source.getHttpProxy());
74 }
Robert Greenwalt37e65eb2010-08-30 10:56:47 -070075 }
76
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070077 public void setInterfaceName(String iface) {
78 mIfaceName = iface;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070079 }
80
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070081 public String getInterfaceName() {
82 return mIfaceName;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070083 }
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070084
Robert Greenwalt37e65eb2010-08-30 10:56:47 -070085 public Collection<InetAddress> getAddresses() {
Irfan Sheriffed5d7d12010-10-01 16:08:28 -070086 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 Greenwalt47f69fe2010-06-15 15:43:39 -070099 }
100
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700101 public void addDns(InetAddress dns) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700102 mDnses.add(dns);
103 }
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700104
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700105 public Collection<InetAddress> getDnses() {
106 return Collections.unmodifiableCollection(mDnses);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700107 }
108
Robert Greenwalt992564e2011-02-09 13:56:06 -0800109 public void addGateway(InetAddress gateway) {
110 mGateways.add(gateway);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700111 }
Robert Greenwalt992564e2011-02-09 13:56:06 -0800112 public Collection<InetAddress> getGateways() {
113 return Collections.unmodifiableCollection(mGateways);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700114 }
115
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700116 public void setHttpProxy(ProxyProperties proxy) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700117 mHttpProxy = proxy;
118 }
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700119 public ProxyProperties getHttpProxy() {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700120 return mHttpProxy;
121 }
122
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700123 public void clear() {
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700124 mIfaceName = null;
125 mLinkAddresses = new ArrayList<LinkAddress>();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700126 mDnses = new ArrayList<InetAddress>();
Robert Greenwalt992564e2011-02-09 13:56:06 -0800127 mGateways = new ArrayList<InetAddress>();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700128 mHttpProxy = null;
129 }
130
131 /**
132 * Implement the Parcelable interface
133 * @hide
134 */
135 public int describeContents() {
136 return 0;
137 }
138
Wink Saville1f6408a2010-08-27 11:15:18 -0700139 @Override
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700140 public String toString() {
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700141 String ifaceName = (mIfaceName == null ? "" : "InterfaceName: " + mIfaceName + " ");
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700142
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700143 String linkAddresses = "LinkAddresses: [";
144 for (LinkAddress addr : mLinkAddresses) linkAddresses += addr.toString();
145 linkAddresses += "] ";
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700146
147 String dns = "DnsAddresses: [";
Wink Saville1f6408a2010-08-27 11:15:18 -0700148 for (InetAddress addr : mDnses) dns += addr.getHostAddress() + ",";
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700149 dns += "] ";
150
Robert Greenwalt992564e2011-02-09 13:56:06 -0800151 String gateways = "Gateways: [";
152 for (InetAddress gw : mGateways) gateways += gw.getHostAddress() + ",";
153 gateways += "] ";
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700154 String proxy = (mHttpProxy == null ? "" : "HttpProxy: " + mHttpProxy.toString() + " ");
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700155
Robert Greenwalt992564e2011-02-09 13:56:06 -0800156 return ifaceName + linkAddresses + gateways + dns + proxy;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700157 }
158
159 /**
160 * Implement the Parcelable interface.
161 * @hide
162 */
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700163 public void writeToParcel(Parcel dest, int flags) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700164 dest.writeString(getInterfaceName());
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700165 dest.writeInt(mLinkAddresses.size());
166 for(LinkAddress linkAddress : mLinkAddresses) {
167 dest.writeParcelable(linkAddress, flags);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700168 }
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700169
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700170 dest.writeInt(mDnses.size());
171 for(InetAddress d : mDnses) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700172 dest.writeByteArray(d.getAddress());
173 }
Robert Greenwalt992564e2011-02-09 13:56:06 -0800174
175 dest.writeInt(mGateways.size());
176 for(InetAddress gw : mGateways) {
177 dest.writeByteArray(gw.getAddress());
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700178 }
Robert Greenwalt992564e2011-02-09 13:56:06 -0800179
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700180 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 Greenwalt37e65eb2010-08-30 10:56:47 -0700192 public static final Creator<LinkProperties> CREATOR =
193 new Creator<LinkProperties>() {
194 public LinkProperties createFromParcel(Parcel in) {
195 LinkProperties netProp = new LinkProperties();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700196 String iface = in.readString();
197 if (iface != null) {
198 try {
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700199 netProp.setInterfaceName(iface);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700200 } catch (Exception e) {
201 return null;
202 }
203 }
204 int addressCount = in.readInt();
205 for (int i=0; i<addressCount; i++) {
Irfan Sheriffed5d7d12010-10-01 16:08:28 -0700206 netProp.addLinkAddress((LinkAddress)in.readParcelable(null));
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700207 }
208 addressCount = in.readInt();
209 for (int i=0; i<addressCount; i++) {
210 try {
Irfan Sheriff1cf56ab2010-08-04 15:15:49 -0700211 netProp.addDns(InetAddress.getByAddress(in.createByteArray()));
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700212 } catch (UnknownHostException e) { }
213 }
Robert Greenwalt992564e2011-02-09 13:56:06 -0800214 addressCount = in.readInt();
215 for (int i=0; i<addressCount; i++) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700216 try {
Robert Greenwalt992564e2011-02-09 13:56:06 -0800217 netProp.addGateway(InetAddress.getByAddress(in.createByteArray()));
218 } catch (UnknownHostException e) { }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700219 }
220 if (in.readByte() == 1) {
221 netProp.setHttpProxy((ProxyProperties)in.readParcelable(null));
222 }
223 return netProp;
224 }
225
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700226 public LinkProperties[] newArray(int size) {
227 return new LinkProperties[size];
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700228 }
229 };
230}