blob: 22b26b13c96b235e3b05abe7f5c74c5348ec2595 [file] [log] [blame]
Robert Greenwalt4717c262012-10-31 14:32:53 -07001/*
2 * Copyright (C) 2012 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
17package android.net;
18
19import android.os.Parcelable;
20import android.os.Parcel;
Robert Greenwalt3c97f942013-01-09 16:28:19 -080021import android.text.TextUtils;
Robert Greenwalt4717c262012-10-31 14:32:53 -070022import android.util.Log;
23
24import java.net.InetAddress;
25import java.net.UnknownHostException;
Robert Greenwalt4717c262012-10-31 14:32:53 -070026
27/**
28 * A simple object for retrieving the results of a DHCP request.
29 * Optimized (attempted) for that jni interface
30 * TODO - remove when DhcpInfo is deprecated. Move the remaining api to LinkProperties.
31 * @hide
32 */
33public class DhcpResults implements Parcelable {
34 private static final String TAG = "DhcpResults";
35
36 public final LinkProperties linkProperties;
37
38 public InetAddress serverAddress;
39
40 /**
41 * Vendor specific information (from RFC 2132).
42 */
43 public String vendorInfo;
44
45 public int leaseDuration;
46
47 public DhcpResults() {
48 linkProperties = new LinkProperties();
49 }
50
51 /** copy constructor */
52 public DhcpResults(DhcpResults source) {
53 if (source != null) {
54 linkProperties = new LinkProperties(source.linkProperties);
55 serverAddress = source.serverAddress;
56 leaseDuration = source.leaseDuration;
57 vendorInfo = source.vendorInfo;
58 } else {
59 linkProperties = new LinkProperties();
60 }
61 }
62
63 public DhcpResults(LinkProperties lp) {
64 linkProperties = new LinkProperties(lp);
65 }
66
67 /**
68 * Updates the DHCP fields that need to be retained from
69 * original DHCP request if the current renewal shows them
70 * being empty.
71 */
72 public void updateFromDhcpRequest(DhcpResults orig) {
73 if (orig == null || orig.linkProperties == null) return;
74 if (linkProperties.getRoutes().size() == 0) {
75 for (RouteInfo r : orig.linkProperties.getRoutes()) linkProperties.addRoute(r);
76 }
77 if (linkProperties.getDnses().size() == 0) {
78 for (InetAddress d : orig.linkProperties.getDnses()) linkProperties.addDns(d);
79 }
80 }
81
82 /**
83 * Test if this DHCP lease includes vendor hint that network link is
84 * metered, and sensitive to heavy data transfers.
85 */
86 public boolean hasMeteredHint() {
87 if (vendorInfo != null) {
88 return vendorInfo.contains("ANDROID_METERED");
89 } else {
90 return false;
91 }
92 }
93
94 public void clear() {
95 linkProperties.clear();
96 serverAddress = null;
97 vendorInfo = null;
98 leaseDuration = 0;
99 }
100
101 @Override
102 public String toString() {
103 StringBuffer str = new StringBuffer(linkProperties.toString());
104
105 str.append(" DHCP server ").append(serverAddress);
106 str.append(" Vendor info ").append(vendorInfo);
107 str.append(" lease ").append(leaseDuration).append(" seconds");
108
109 return str.toString();
110 }
111
112 @Override
113 public boolean equals(Object obj) {
114 if (this == obj) return true;
115
116 if (!(obj instanceof DhcpResults)) return false;
117
118 DhcpResults target = (DhcpResults)obj;
119
120 if (linkProperties == null) {
121 if (target.linkProperties != null) return false;
122 } else if (!linkProperties.equals(target.linkProperties)) return false;
123 if (serverAddress == null) {
124 if (target.serverAddress != null) return false;
125 } else if (!serverAddress.equals(target.serverAddress)) return false;
126 if (vendorInfo == null) {
127 if (target.vendorInfo != null) return false;
128 } else if (!vendorInfo.equals(target.vendorInfo)) return false;
129 if (leaseDuration != target.leaseDuration) return false;
130
131 return true;
132 }
133
134 /** Implement the Parcelable interface */
135 public int describeContents() {
136 return 0;
137 }
138
139 /** Implement the Parcelable interface */
140 public void writeToParcel(Parcel dest, int flags) {
141 linkProperties.writeToParcel(dest, flags);
142
143 dest.writeInt(leaseDuration);
144
145 if (serverAddress != null) {
146 dest.writeByte((byte)1);
147 dest.writeByteArray(serverAddress.getAddress());
148 } else {
149 dest.writeByte((byte)0);
150 }
151
152 dest.writeString(vendorInfo);
153 }
154
155 /** Implement the Parcelable interface */
156 public static final Creator<DhcpResults> CREATOR =
157 new Creator<DhcpResults>() {
158 public DhcpResults createFromParcel(Parcel in) {
159 DhcpResults prop = new DhcpResults((LinkProperties)in.readParcelable(null));
160
161 prop.leaseDuration = in.readInt();
162
163 if (in.readByte() == 1) {
164 try {
165 prop.serverAddress = InetAddress.getByAddress(in.createByteArray());
166 } catch (UnknownHostException e) {}
167 }
168
169 prop.vendorInfo = in.readString();
170
171 return prop;
172 }
173
174 public DhcpResults[] newArray(int size) {
175 return new DhcpResults[size];
176 }
177 };
178
179 // Utils for jni population - false on success
180 public void setInterfaceName(String interfaceName) {
181 linkProperties.setInterfaceName(interfaceName);
182 }
183
184 public boolean addLinkAddress(String addrString, int prefixLength) {
185 InetAddress addr;
186 try {
187 addr = NetworkUtils.numericToInetAddress(addrString);
188 } catch (IllegalArgumentException e) {
189 Log.e(TAG, "addLinkAddress failed with addrString " + addrString);
190 return true;
191 }
192
193 LinkAddress linkAddress = new LinkAddress(addr, prefixLength);
194 linkProperties.addLinkAddress(linkAddress);
195
196 RouteInfo routeInfo = new RouteInfo(linkAddress);
197 linkProperties.addRoute(routeInfo);
198 return false;
199 }
200
201 public boolean addGateway(String addrString) {
202 try {
203 linkProperties.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress(addrString)));
204 } catch (IllegalArgumentException e) {
205 Log.e(TAG, "addGateway failed with addrString " + addrString);
206 return true;
207 }
208 return false;
209 }
210
211 public boolean addDns(String addrString) {
Robert Greenwalt3c97f942013-01-09 16:28:19 -0800212 if (TextUtils.isEmpty(addrString) == false) {
213 try {
214 linkProperties.addDns(NetworkUtils.numericToInetAddress(addrString));
215 } catch (IllegalArgumentException e) {
216 Log.e(TAG, "addDns failed with addrString " + addrString);
217 return true;
218 }
Robert Greenwalt4717c262012-10-31 14:32:53 -0700219 }
220 return false;
221 }
222
223 public boolean setServerAddress(String addrString) {
224 try {
225 serverAddress = NetworkUtils.numericToInetAddress(addrString);
226 } catch (IllegalArgumentException e) {
227 Log.e(TAG, "setServerAddress failed with addrString " + addrString);
228 return true;
229 }
230 return false;
231 }
232
233 public void setLeaseDuration(int duration) {
234 leaseDuration = duration;
235 }
236
237 public void setVendorInfo(String info) {
238 vendorInfo = info;
239 }
240
Robert Greenwalt8058f622012-11-09 10:52:27 -0800241 public void setDomains(String domains) {
242 linkProperties.setDomains(domains);
243 }
Robert Greenwalt4717c262012-10-31 14:32:53 -0700244}