blob: 1a3a6f8aa64c9d8b9ae356d96686b14a1d3717cb [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
Robert Greenwalt4717c262012-10-31 14:32:53 -070067 public DhcpResults() {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090068 super();
69 }
70
Remi NGUYEN VANa4bcc862019-01-28 13:28:35 +090071 /**
72 * Create a {@link StaticIpConfiguration} based on the DhcpResults.
73 */
74 public StaticIpConfiguration toStaticIpConfiguration() {
75 final StaticIpConfiguration s = new StaticIpConfiguration();
76 // All these except dnsServers are immutable, so no need to make copies.
Remi NGUYEN VAN231b52b2019-01-29 15:38:52 +090077 s.setIpAddress(ipAddress);
78 s.setGateway(gateway);
79 for (InetAddress addr : dnsServers) {
80 s.addDnsServer(addr);
81 }
82 s.setDomains(domains);
Remi NGUYEN VANa4bcc862019-01-28 13:28:35 +090083 return s;
84 }
85
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090086 public DhcpResults(StaticIpConfiguration source) {
Remi NGUYEN VANa4bcc862019-01-28 13:28:35 +090087 if (source != null) {
Remi NGUYEN VAN231b52b2019-01-29 15:38:52 +090088 ipAddress = source.getIpAddress();
89 gateway = source.getGateway();
90 dnsServers.addAll(source.getDnsServers());
91 domains = source.getDomains();
Remi NGUYEN VANa4bcc862019-01-28 13:28:35 +090092 }
Robert Greenwalt4717c262012-10-31 14:32:53 -070093 }
94
95 /** copy constructor */
96 public DhcpResults(DhcpResults source) {
Remi NGUYEN VANa4bcc862019-01-28 13:28:35 +090097 this(source == null ? null : source.toStaticIpConfiguration());
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090098 if (source != null) {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090099 serverAddress = source.serverAddress;
100 vendorInfo = source.vendorInfo;
101 leaseDuration = source.leaseDuration;
Lorenzo Colitti77fdf232016-03-31 16:20:22 +0900102 mtu = source.mtu;
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;
Robert Greenwalt4717c262012-10-31 14:32:53 -0700135 }
136
137 @Override
138 public String toString() {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900139 StringBuffer str = new StringBuffer(super.toString());
Robert Greenwalt4717c262012-10-31 14:32:53 -0700140
141 str.append(" DHCP server ").append(serverAddress);
142 str.append(" Vendor info ").append(vendorInfo);
143 str.append(" lease ").append(leaseDuration).append(" seconds");
Lorenzo Colitti77fdf232016-03-31 16:20:22 +0900144 if (mtu != 0) str.append(" MTU ").append(mtu);
Robert Greenwalt4717c262012-10-31 14:32:53 -0700145
146 return str.toString();
147 }
148
149 @Override
150 public boolean equals(Object obj) {
151 if (this == obj) return true;
152
153 if (!(obj instanceof DhcpResults)) return false;
154
155 DhcpResults target = (DhcpResults)obj;
156
Remi NGUYEN VANa4bcc862019-01-28 13:28:35 +0900157 return toStaticIpConfiguration().equals(target.toStaticIpConfiguration())
158 && Objects.equals(serverAddress, target.serverAddress)
159 && Objects.equals(vendorInfo, target.vendorInfo)
160 && leaseDuration == target.leaseDuration
161 && mtu == target.mtu;
Robert Greenwalt4717c262012-10-31 14:32:53 -0700162 }
163
Remi NGUYEN VANa4bcc862019-01-28 13:28:35 +0900164 /**
165 * Implement the Parcelable interface
166 */
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700167 public static final @android.annotation.NonNull Creator<DhcpResults> CREATOR =
Robert Greenwalt4717c262012-10-31 14:32:53 -0700168 new Creator<DhcpResults>() {
169 public DhcpResults createFromParcel(Parcel in) {
Remi NGUYEN VANa4bcc862019-01-28 13:28:35 +0900170 return readFromParcel(in);
Robert Greenwalt4717c262012-10-31 14:32:53 -0700171 }
172
173 public DhcpResults[] newArray(int size) {
174 return new DhcpResults[size];
175 }
176 };
177
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900178 /** Implement the Parcelable interface */
179 public void writeToParcel(Parcel dest, int flags) {
Remi NGUYEN VANa4bcc862019-01-28 13:28:35 +0900180 toStaticIpConfiguration().writeToParcel(dest, flags);
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900181 dest.writeInt(leaseDuration);
Lorenzo Colitti77fdf232016-03-31 16:20:22 +0900182 dest.writeInt(mtu);
Remi NGUYEN VAN231b52b2019-01-29 15:38:52 +0900183 InetAddressUtils.parcelInetAddress(dest, serverAddress, flags);
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900184 dest.writeString(vendorInfo);
Robert Greenwalt4717c262012-10-31 14:32:53 -0700185 }
186
Remi NGUYEN VANa4bcc862019-01-28 13:28:35 +0900187 @Override
188 public int describeContents() {
189 return 0;
190 }
191
192 private static DhcpResults readFromParcel(Parcel in) {
193 final StaticIpConfiguration s = StaticIpConfiguration.CREATOR.createFromParcel(in);
194 final DhcpResults dhcpResults = new DhcpResults(s);
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900195 dhcpResults.leaseDuration = in.readInt();
Lorenzo Colitti77fdf232016-03-31 16:20:22 +0900196 dhcpResults.mtu = in.readInt();
Remi NGUYEN VAN231b52b2019-01-29 15:38:52 +0900197 dhcpResults.serverAddress = (Inet4Address) InetAddressUtils.unparcelInetAddress(in);
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900198 dhcpResults.vendorInfo = in.readString();
Remi NGUYEN VANa4bcc862019-01-28 13:28:35 +0900199 return dhcpResults;
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900200 }
201
202 // Utils for jni population - false on success
203 // Not part of the superclass because they're only used by the JNI iterface to the DHCP daemon.
204 public boolean setIpAddress(String addrString, int prefixLength) {
Robert Greenwalt4717c262012-10-31 14:32:53 -0700205 try {
Remi NGUYEN VAN231b52b2019-01-29 15:38:52 +0900206 Inet4Address addr = (Inet4Address) InetAddresses.parseNumericAddress(addrString);
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900207 ipAddress = new LinkAddress(addr, prefixLength);
208 } catch (IllegalArgumentException|ClassCastException e) {
209 Log.e(TAG, "setIpAddress failed with addrString " + addrString + "/" + prefixLength);
Robert Greenwalt4717c262012-10-31 14:32:53 -0700210 return true;
211 }
Robert Greenwalt4717c262012-10-31 14:32:53 -0700212 return false;
213 }
214
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900215 public boolean setGateway(String addrString) {
Robert Greenwalt4717c262012-10-31 14:32:53 -0700216 try {
Remi NGUYEN VAN231b52b2019-01-29 15:38:52 +0900217 gateway = InetAddresses.parseNumericAddress(addrString);
Robert Greenwalt4717c262012-10-31 14:32:53 -0700218 } catch (IllegalArgumentException e) {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900219 Log.e(TAG, "setGateway failed with addrString " + addrString);
Robert Greenwalt4717c262012-10-31 14:32:53 -0700220 return true;
221 }
222 return false;
223 }
224
225 public boolean addDns(String addrString) {
Robert Greenwalt3c97f942013-01-09 16:28:19 -0800226 if (TextUtils.isEmpty(addrString) == false) {
227 try {
Remi NGUYEN VAN231b52b2019-01-29 15:38:52 +0900228 dnsServers.add(InetAddresses.parseNumericAddress(addrString));
Robert Greenwalt3c97f942013-01-09 16:28:19 -0800229 } catch (IllegalArgumentException e) {
230 Log.e(TAG, "addDns failed with addrString " + addrString);
231 return true;
232 }
Robert Greenwalt4717c262012-10-31 14:32:53 -0700233 }
234 return false;
235 }
236
Remi NGUYEN VANa4bcc862019-01-28 13:28:35 +0900237 public LinkAddress getIpAddress() {
238 return ipAddress;
239 }
240
241 public void setIpAddress(LinkAddress ipAddress) {
242 this.ipAddress = ipAddress;
243 }
244
245 public InetAddress getGateway() {
246 return gateway;
247 }
248
249 public void setGateway(InetAddress gateway) {
250 this.gateway = gateway;
251 }
252
253 public List<InetAddress> getDnsServers() {
254 return dnsServers;
255 }
256
257 /**
258 * Add a DNS server to this configuration.
259 */
260 public void addDnsServer(InetAddress server) {
261 dnsServers.add(server);
262 }
263
264 public String getDomains() {
265 return domains;
266 }
267
268 public void setDomains(String domains) {
269 this.domains = domains;
270 }
271
272 public Inet4Address getServerAddress() {
273 return serverAddress;
274 }
275
276 public void setServerAddress(Inet4Address addr) {
277 serverAddress = addr;
278 }
279
280 public int getLeaseDuration() {
281 return leaseDuration;
Robert Greenwalt4717c262012-10-31 14:32:53 -0700282 }
283
284 public void setLeaseDuration(int duration) {
285 leaseDuration = duration;
286 }
287
Remi NGUYEN VANa4bcc862019-01-28 13:28:35 +0900288 public String getVendorInfo() {
289 return vendorInfo;
290 }
291
Robert Greenwalt4717c262012-10-31 14:32:53 -0700292 public void setVendorInfo(String info) {
293 vendorInfo = info;
294 }
295
Remi NGUYEN VANa4bcc862019-01-28 13:28:35 +0900296 public int getMtu() {
297 return mtu;
298 }
299
300 public void setMtu(int mtu) {
301 this.mtu = mtu;
Robert Greenwalt8058f622012-11-09 10:52:27 -0800302 }
Robert Greenwalt4717c262012-10-31 14:32:53 -0700303}