blob: 97be51a30641959ef48d5ca779ea711705fa2594 [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
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010019import android.annotation.UnsupportedAppUsage;
Remi NGUYEN VAN231b52b2019-01-29 15:38:52 +090020import android.net.shared.InetAddressUtils;
Robert Greenwalt4717c262012-10-31 14:32:53 -070021import android.os.Parcel;
Remi NGUYEN VANa4bcc862019-01-28 13:28:35 +090022import android.os.Parcelable;
Robert Greenwalt3c97f942013-01-09 16:28:19 -080023import android.text.TextUtils;
Robert Greenwalt4717c262012-10-31 14:32:53 -070024import android.util.Log;
25
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090026import java.net.Inet4Address;
Remi NGUYEN VANa4bcc862019-01-28 13:28:35 +090027import java.net.InetAddress;
28import java.util.ArrayList;
29import java.util.List;
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090030import java.util.Objects;
Robert Greenwalt4717c262012-10-31 14:32:53 -070031
32/**
33 * A simple object for retrieving the results of a DHCP request.
34 * Optimized (attempted) for that jni interface
Remi NGUYEN VANa4bcc862019-01-28 13:28:35 +090035 * TODO: remove this class and replace with other existing constructs
Robert Greenwalt4717c262012-10-31 14:32:53 -070036 * @hide
37 */
Remi NGUYEN VANa4bcc862019-01-28 13:28:35 +090038public final class DhcpResults implements Parcelable {
Robert Greenwalt4717c262012-10-31 14:32:53 -070039 private static final String TAG = "DhcpResults";
40
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010041 @UnsupportedAppUsage
Remi NGUYEN VANa4bcc862019-01-28 13:28:35 +090042 public LinkAddress ipAddress;
43
44 @UnsupportedAppUsage
45 public InetAddress gateway;
46
47 @UnsupportedAppUsage
48 public final ArrayList<InetAddress> dnsServers = new ArrayList<>();
49
50 @UnsupportedAppUsage
51 public String domains;
52
53 @UnsupportedAppUsage
Lorenzo Colitti577255e2015-09-10 18:12:18 +090054 public Inet4Address serverAddress;
Robert Greenwalt4717c262012-10-31 14:32:53 -070055
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090056 /** Vendor specific information (from RFC 2132). */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010057 @UnsupportedAppUsage
Robert Greenwalt4717c262012-10-31 14:32:53 -070058 public String vendorInfo;
59
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010060 @UnsupportedAppUsage
Robert Greenwalt4717c262012-10-31 14:32:53 -070061 public int leaseDuration;
62
Lorenzo Colitti77fdf232016-03-31 16:20:22 +090063 /** Link MTU option. 0 means unset. */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010064 @UnsupportedAppUsage
Lorenzo Colitti77fdf232016-03-31 16:20:22 +090065 public int mtu;
66
yuanyunli43f1bc92019-01-09 16:59:37 +080067 public String serverHostName;
68
Robert Greenwalt4717c262012-10-31 14:32:53 -070069 public DhcpResults() {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090070 super();
71 }
72
Remi NGUYEN VANa4bcc862019-01-28 13:28:35 +090073 /**
74 * Create a {@link StaticIpConfiguration} based on the DhcpResults.
75 */
76 public StaticIpConfiguration toStaticIpConfiguration() {
Remi NGUYEN VAN59a06a02019-04-01 17:09:05 +090077 return new StaticIpConfiguration.Builder()
78 .setIpAddress(ipAddress)
79 .setGateway(gateway)
80 .setDnsServers(dnsServers)
81 .setDomains(domains)
82 .build();
Remi NGUYEN VANa4bcc862019-01-28 13:28:35 +090083 }
84
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090085 public DhcpResults(StaticIpConfiguration source) {
Remi NGUYEN VANa4bcc862019-01-28 13:28:35 +090086 if (source != null) {
Remi NGUYEN VAN231b52b2019-01-29 15:38:52 +090087 ipAddress = source.getIpAddress();
88 gateway = source.getGateway();
89 dnsServers.addAll(source.getDnsServers());
90 domains = source.getDomains();
Remi NGUYEN VANa4bcc862019-01-28 13:28:35 +090091 }
Robert Greenwalt4717c262012-10-31 14:32:53 -070092 }
93
94 /** copy constructor */
95 public DhcpResults(DhcpResults source) {
Remi NGUYEN VANa4bcc862019-01-28 13:28:35 +090096 this(source == null ? null : source.toStaticIpConfiguration());
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090097 if (source != null) {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090098 serverAddress = source.serverAddress;
99 vendorInfo = source.vendorInfo;
100 leaseDuration = source.leaseDuration;
Lorenzo Colitti77fdf232016-03-31 16:20:22 +0900101 mtu = source.mtu;
yuanyunli43f1bc92019-01-09 16:59:37 +0800102 serverHostName = source.serverHostName;
Robert Greenwalt4717c262012-10-31 14:32:53 -0700103 }
104 }
105
106 /**
Remi NGUYEN VANa4bcc862019-01-28 13:28:35 +0900107 * @see StaticIpConfiguration#getRoutes(String)
108 * @hide
109 */
110 public List<RouteInfo> getRoutes(String iface) {
111 return toStaticIpConfiguration().getRoutes(iface);
112 }
113
114 /**
Robert Greenwalt4717c262012-10-31 14:32:53 -0700115 * Test if this DHCP lease includes vendor hint that network link is
116 * metered, and sensitive to heavy data transfers.
117 */
118 public boolean hasMeteredHint() {
119 if (vendorInfo != null) {
120 return vendorInfo.contains("ANDROID_METERED");
121 } else {
122 return false;
123 }
124 }
125
126 public void clear() {
Remi NGUYEN VANa4bcc862019-01-28 13:28:35 +0900127 ipAddress = null;
128 gateway = null;
129 dnsServers.clear();
130 domains = null;
131 serverAddress = null;
Robert Greenwalt4717c262012-10-31 14:32:53 -0700132 vendorInfo = null;
133 leaseDuration = 0;
Lorenzo Colitti77fdf232016-03-31 16:20:22 +0900134 mtu = 0;
yuanyunli43f1bc92019-01-09 16:59:37 +0800135 serverHostName = null;
Robert Greenwalt4717c262012-10-31 14:32:53 -0700136 }
137
138 @Override
139 public String toString() {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900140 StringBuffer str = new StringBuffer(super.toString());
Robert Greenwalt4717c262012-10-31 14:32:53 -0700141
142 str.append(" DHCP server ").append(serverAddress);
143 str.append(" Vendor info ").append(vendorInfo);
144 str.append(" lease ").append(leaseDuration).append(" seconds");
Lorenzo Colitti77fdf232016-03-31 16:20:22 +0900145 if (mtu != 0) str.append(" MTU ").append(mtu);
yuanyunli43f1bc92019-01-09 16:59:37 +0800146 str.append(" Servername ").append(serverHostName);
Robert Greenwalt4717c262012-10-31 14:32:53 -0700147
148 return str.toString();
149 }
150
151 @Override
152 public boolean equals(Object obj) {
153 if (this == obj) return true;
154
155 if (!(obj instanceof DhcpResults)) return false;
156
157 DhcpResults target = (DhcpResults)obj;
158
Remi NGUYEN VANa4bcc862019-01-28 13:28:35 +0900159 return toStaticIpConfiguration().equals(target.toStaticIpConfiguration())
160 && Objects.equals(serverAddress, target.serverAddress)
161 && Objects.equals(vendorInfo, target.vendorInfo)
yuanyunli43f1bc92019-01-09 16:59:37 +0800162 && Objects.equals(serverHostName, target.serverHostName)
Remi NGUYEN VANa4bcc862019-01-28 13:28:35 +0900163 && leaseDuration == target.leaseDuration
164 && mtu == target.mtu;
Robert Greenwalt4717c262012-10-31 14:32:53 -0700165 }
166
Remi NGUYEN VANa4bcc862019-01-28 13:28:35 +0900167 /**
168 * Implement the Parcelable interface
169 */
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700170 public static final @android.annotation.NonNull Creator<DhcpResults> CREATOR =
Robert Greenwalt4717c262012-10-31 14:32:53 -0700171 new Creator<DhcpResults>() {
172 public DhcpResults createFromParcel(Parcel in) {
Remi NGUYEN VANa4bcc862019-01-28 13:28:35 +0900173 return readFromParcel(in);
Robert Greenwalt4717c262012-10-31 14:32:53 -0700174 }
175
176 public DhcpResults[] newArray(int size) {
177 return new DhcpResults[size];
178 }
179 };
180
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900181 /** Implement the Parcelable interface */
182 public void writeToParcel(Parcel dest, int flags) {
Remi NGUYEN VANa4bcc862019-01-28 13:28:35 +0900183 toStaticIpConfiguration().writeToParcel(dest, flags);
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900184 dest.writeInt(leaseDuration);
Lorenzo Colitti77fdf232016-03-31 16:20:22 +0900185 dest.writeInt(mtu);
Remi NGUYEN VAN231b52b2019-01-29 15:38:52 +0900186 InetAddressUtils.parcelInetAddress(dest, serverAddress, flags);
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900187 dest.writeString(vendorInfo);
yuanyunli43f1bc92019-01-09 16:59:37 +0800188 dest.writeString(serverHostName);
Robert Greenwalt4717c262012-10-31 14:32:53 -0700189 }
190
Remi NGUYEN VANa4bcc862019-01-28 13:28:35 +0900191 @Override
192 public int describeContents() {
193 return 0;
194 }
195
196 private static DhcpResults readFromParcel(Parcel in) {
197 final StaticIpConfiguration s = StaticIpConfiguration.CREATOR.createFromParcel(in);
198 final DhcpResults dhcpResults = new DhcpResults(s);
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900199 dhcpResults.leaseDuration = in.readInt();
Lorenzo Colitti77fdf232016-03-31 16:20:22 +0900200 dhcpResults.mtu = in.readInt();
Remi NGUYEN VAN231b52b2019-01-29 15:38:52 +0900201 dhcpResults.serverAddress = (Inet4Address) InetAddressUtils.unparcelInetAddress(in);
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900202 dhcpResults.vendorInfo = in.readString();
yuanyunli43f1bc92019-01-09 16:59:37 +0800203 dhcpResults.serverHostName = in.readString();
Remi NGUYEN VANa4bcc862019-01-28 13:28:35 +0900204 return dhcpResults;
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900205 }
206
207 // Utils for jni population - false on success
208 // Not part of the superclass because they're only used by the JNI iterface to the DHCP daemon.
209 public boolean setIpAddress(String addrString, int prefixLength) {
Robert Greenwalt4717c262012-10-31 14:32:53 -0700210 try {
Remi NGUYEN VAN231b52b2019-01-29 15:38:52 +0900211 Inet4Address addr = (Inet4Address) InetAddresses.parseNumericAddress(addrString);
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900212 ipAddress = new LinkAddress(addr, prefixLength);
213 } catch (IllegalArgumentException|ClassCastException e) {
214 Log.e(TAG, "setIpAddress failed with addrString " + addrString + "/" + prefixLength);
Robert Greenwalt4717c262012-10-31 14:32:53 -0700215 return true;
216 }
Robert Greenwalt4717c262012-10-31 14:32:53 -0700217 return false;
218 }
219
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900220 public boolean setGateway(String addrString) {
Robert Greenwalt4717c262012-10-31 14:32:53 -0700221 try {
Remi NGUYEN VAN231b52b2019-01-29 15:38:52 +0900222 gateway = InetAddresses.parseNumericAddress(addrString);
Robert Greenwalt4717c262012-10-31 14:32:53 -0700223 } catch (IllegalArgumentException e) {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900224 Log.e(TAG, "setGateway failed with addrString " + addrString);
Robert Greenwalt4717c262012-10-31 14:32:53 -0700225 return true;
226 }
227 return false;
228 }
229
230 public boolean addDns(String addrString) {
Robert Greenwalt3c97f942013-01-09 16:28:19 -0800231 if (TextUtils.isEmpty(addrString) == false) {
232 try {
Remi NGUYEN VAN231b52b2019-01-29 15:38:52 +0900233 dnsServers.add(InetAddresses.parseNumericAddress(addrString));
Robert Greenwalt3c97f942013-01-09 16:28:19 -0800234 } catch (IllegalArgumentException e) {
235 Log.e(TAG, "addDns failed with addrString " + addrString);
236 return true;
237 }
Robert Greenwalt4717c262012-10-31 14:32:53 -0700238 }
239 return false;
240 }
241
Remi NGUYEN VANa4bcc862019-01-28 13:28:35 +0900242 public LinkAddress getIpAddress() {
243 return ipAddress;
244 }
245
246 public void setIpAddress(LinkAddress ipAddress) {
247 this.ipAddress = ipAddress;
248 }
249
250 public InetAddress getGateway() {
251 return gateway;
252 }
253
254 public void setGateway(InetAddress gateway) {
255 this.gateway = gateway;
256 }
257
258 public List<InetAddress> getDnsServers() {
259 return dnsServers;
260 }
261
262 /**
263 * Add a DNS server to this configuration.
264 */
265 public void addDnsServer(InetAddress server) {
266 dnsServers.add(server);
267 }
268
269 public String getDomains() {
270 return domains;
271 }
272
273 public void setDomains(String domains) {
274 this.domains = domains;
275 }
276
277 public Inet4Address getServerAddress() {
278 return serverAddress;
279 }
280
281 public void setServerAddress(Inet4Address addr) {
282 serverAddress = addr;
283 }
284
285 public int getLeaseDuration() {
286 return leaseDuration;
Robert Greenwalt4717c262012-10-31 14:32:53 -0700287 }
288
289 public void setLeaseDuration(int duration) {
290 leaseDuration = duration;
291 }
292
Remi NGUYEN VANa4bcc862019-01-28 13:28:35 +0900293 public String getVendorInfo() {
294 return vendorInfo;
295 }
296
Robert Greenwalt4717c262012-10-31 14:32:53 -0700297 public void setVendorInfo(String info) {
298 vendorInfo = info;
299 }
300
Remi NGUYEN VANa4bcc862019-01-28 13:28:35 +0900301 public int getMtu() {
302 return mtu;
303 }
304
305 public void setMtu(int mtu) {
306 this.mtu = mtu;
Robert Greenwalt8058f622012-11-09 10:52:27 -0800307 }
Robert Greenwalt4717c262012-10-31 14:32:53 -0700308}