Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.net; |
| 18 | |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 19 | import android.annotation.UnsupportedAppUsage; |
Remi NGUYEN VAN | 231b52b | 2019-01-29 15:38:52 +0900 | [diff] [blame] | 20 | import android.net.shared.InetAddressUtils; |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 21 | import android.os.Parcel; |
Remi NGUYEN VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 22 | import android.os.Parcelable; |
Robert Greenwalt | 3c97f94 | 2013-01-09 16:28:19 -0800 | [diff] [blame] | 23 | import android.text.TextUtils; |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 24 | import android.util.Log; |
| 25 | |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 26 | import java.net.Inet4Address; |
Remi NGUYEN VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 27 | import java.net.InetAddress; |
| 28 | import java.util.ArrayList; |
| 29 | import java.util.List; |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 30 | import java.util.Objects; |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * A simple object for retrieving the results of a DHCP request. |
| 34 | * Optimized (attempted) for that jni interface |
Remi NGUYEN VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 35 | * TODO: remove this class and replace with other existing constructs |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 36 | * @hide |
| 37 | */ |
Remi NGUYEN VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 38 | public final class DhcpResults implements Parcelable { |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 39 | private static final String TAG = "DhcpResults"; |
| 40 | |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 41 | @UnsupportedAppUsage |
Remi NGUYEN VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 42 | 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 Colitti | 577255e | 2015-09-10 18:12:18 +0900 | [diff] [blame] | 54 | public Inet4Address serverAddress; |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 55 | |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 56 | /** Vendor specific information (from RFC 2132). */ |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 57 | @UnsupportedAppUsage |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 58 | public String vendorInfo; |
| 59 | |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 60 | @UnsupportedAppUsage |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 61 | public int leaseDuration; |
| 62 | |
Lorenzo Colitti | 77fdf23 | 2016-03-31 16:20:22 +0900 | [diff] [blame] | 63 | /** Link MTU option. 0 means unset. */ |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 64 | @UnsupportedAppUsage |
Lorenzo Colitti | 77fdf23 | 2016-03-31 16:20:22 +0900 | [diff] [blame] | 65 | public int mtu; |
| 66 | |
yuanyunli | 43f1bc9 | 2019-01-09 16:59:37 +0800 | [diff] [blame] | 67 | public String serverHostName; |
| 68 | |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 69 | public DhcpResults() { |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 70 | super(); |
| 71 | } |
| 72 | |
Remi NGUYEN VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 73 | /** |
| 74 | * Create a {@link StaticIpConfiguration} based on the DhcpResults. |
| 75 | */ |
| 76 | public StaticIpConfiguration toStaticIpConfiguration() { |
Remi NGUYEN VAN | 59a06a0 | 2019-04-01 17:09:05 +0900 | [diff] [blame] | 77 | return new StaticIpConfiguration.Builder() |
| 78 | .setIpAddress(ipAddress) |
| 79 | .setGateway(gateway) |
| 80 | .setDnsServers(dnsServers) |
| 81 | .setDomains(domains) |
| 82 | .build(); |
Remi NGUYEN VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 83 | } |
| 84 | |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 85 | public DhcpResults(StaticIpConfiguration source) { |
Remi NGUYEN VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 86 | if (source != null) { |
Remi NGUYEN VAN | 231b52b | 2019-01-29 15:38:52 +0900 | [diff] [blame] | 87 | ipAddress = source.getIpAddress(); |
| 88 | gateway = source.getGateway(); |
| 89 | dnsServers.addAll(source.getDnsServers()); |
| 90 | domains = source.getDomains(); |
Remi NGUYEN VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 91 | } |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /** copy constructor */ |
| 95 | public DhcpResults(DhcpResults source) { |
Remi NGUYEN VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 96 | this(source == null ? null : source.toStaticIpConfiguration()); |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 97 | if (source != null) { |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 98 | serverAddress = source.serverAddress; |
| 99 | vendorInfo = source.vendorInfo; |
| 100 | leaseDuration = source.leaseDuration; |
Lorenzo Colitti | 77fdf23 | 2016-03-31 16:20:22 +0900 | [diff] [blame] | 101 | mtu = source.mtu; |
yuanyunli | 43f1bc9 | 2019-01-09 16:59:37 +0800 | [diff] [blame] | 102 | serverHostName = source.serverHostName; |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
Remi NGUYEN VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 107 | * @see StaticIpConfiguration#getRoutes(String) |
| 108 | * @hide |
| 109 | */ |
| 110 | public List<RouteInfo> getRoutes(String iface) { |
| 111 | return toStaticIpConfiguration().getRoutes(iface); |
| 112 | } |
| 113 | |
| 114 | /** |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 115 | * 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 VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 127 | ipAddress = null; |
| 128 | gateway = null; |
| 129 | dnsServers.clear(); |
| 130 | domains = null; |
| 131 | serverAddress = null; |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 132 | vendorInfo = null; |
| 133 | leaseDuration = 0; |
Lorenzo Colitti | 77fdf23 | 2016-03-31 16:20:22 +0900 | [diff] [blame] | 134 | mtu = 0; |
yuanyunli | 43f1bc9 | 2019-01-09 16:59:37 +0800 | [diff] [blame] | 135 | serverHostName = null; |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public String toString() { |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 140 | StringBuffer str = new StringBuffer(super.toString()); |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 141 | |
| 142 | str.append(" DHCP server ").append(serverAddress); |
| 143 | str.append(" Vendor info ").append(vendorInfo); |
| 144 | str.append(" lease ").append(leaseDuration).append(" seconds"); |
Lorenzo Colitti | 77fdf23 | 2016-03-31 16:20:22 +0900 | [diff] [blame] | 145 | if (mtu != 0) str.append(" MTU ").append(mtu); |
yuanyunli | 43f1bc9 | 2019-01-09 16:59:37 +0800 | [diff] [blame] | 146 | str.append(" Servername ").append(serverHostName); |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 147 | |
| 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 VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 159 | return toStaticIpConfiguration().equals(target.toStaticIpConfiguration()) |
| 160 | && Objects.equals(serverAddress, target.serverAddress) |
| 161 | && Objects.equals(vendorInfo, target.vendorInfo) |
yuanyunli | 43f1bc9 | 2019-01-09 16:59:37 +0800 | [diff] [blame] | 162 | && Objects.equals(serverHostName, target.serverHostName) |
Remi NGUYEN VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 163 | && leaseDuration == target.leaseDuration |
| 164 | && mtu == target.mtu; |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Remi NGUYEN VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 167 | /** |
| 168 | * Implement the Parcelable interface |
| 169 | */ |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 170 | public static final @android.annotation.NonNull Creator<DhcpResults> CREATOR = |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 171 | new Creator<DhcpResults>() { |
| 172 | public DhcpResults createFromParcel(Parcel in) { |
Remi NGUYEN VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 173 | return readFromParcel(in); |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | public DhcpResults[] newArray(int size) { |
| 177 | return new DhcpResults[size]; |
| 178 | } |
| 179 | }; |
| 180 | |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 181 | /** Implement the Parcelable interface */ |
| 182 | public void writeToParcel(Parcel dest, int flags) { |
Remi NGUYEN VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 183 | toStaticIpConfiguration().writeToParcel(dest, flags); |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 184 | dest.writeInt(leaseDuration); |
Lorenzo Colitti | 77fdf23 | 2016-03-31 16:20:22 +0900 | [diff] [blame] | 185 | dest.writeInt(mtu); |
Remi NGUYEN VAN | 231b52b | 2019-01-29 15:38:52 +0900 | [diff] [blame] | 186 | InetAddressUtils.parcelInetAddress(dest, serverAddress, flags); |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 187 | dest.writeString(vendorInfo); |
yuanyunli | 43f1bc9 | 2019-01-09 16:59:37 +0800 | [diff] [blame] | 188 | dest.writeString(serverHostName); |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Remi NGUYEN VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 191 | @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 Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 199 | dhcpResults.leaseDuration = in.readInt(); |
Lorenzo Colitti | 77fdf23 | 2016-03-31 16:20:22 +0900 | [diff] [blame] | 200 | dhcpResults.mtu = in.readInt(); |
Remi NGUYEN VAN | 231b52b | 2019-01-29 15:38:52 +0900 | [diff] [blame] | 201 | dhcpResults.serverAddress = (Inet4Address) InetAddressUtils.unparcelInetAddress(in); |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 202 | dhcpResults.vendorInfo = in.readString(); |
yuanyunli | 43f1bc9 | 2019-01-09 16:59:37 +0800 | [diff] [blame] | 203 | dhcpResults.serverHostName = in.readString(); |
Remi NGUYEN VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 204 | return dhcpResults; |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 205 | } |
| 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 Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 210 | try { |
Remi NGUYEN VAN | 231b52b | 2019-01-29 15:38:52 +0900 | [diff] [blame] | 211 | Inet4Address addr = (Inet4Address) InetAddresses.parseNumericAddress(addrString); |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 212 | ipAddress = new LinkAddress(addr, prefixLength); |
| 213 | } catch (IllegalArgumentException|ClassCastException e) { |
| 214 | Log.e(TAG, "setIpAddress failed with addrString " + addrString + "/" + prefixLength); |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 215 | return true; |
| 216 | } |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 217 | return false; |
| 218 | } |
| 219 | |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 220 | public boolean setGateway(String addrString) { |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 221 | try { |
Remi NGUYEN VAN | 231b52b | 2019-01-29 15:38:52 +0900 | [diff] [blame] | 222 | gateway = InetAddresses.parseNumericAddress(addrString); |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 223 | } catch (IllegalArgumentException e) { |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 224 | Log.e(TAG, "setGateway failed with addrString " + addrString); |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 225 | return true; |
| 226 | } |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | public boolean addDns(String addrString) { |
Robert Greenwalt | 3c97f94 | 2013-01-09 16:28:19 -0800 | [diff] [blame] | 231 | if (TextUtils.isEmpty(addrString) == false) { |
| 232 | try { |
Remi NGUYEN VAN | 231b52b | 2019-01-29 15:38:52 +0900 | [diff] [blame] | 233 | dnsServers.add(InetAddresses.parseNumericAddress(addrString)); |
Robert Greenwalt | 3c97f94 | 2013-01-09 16:28:19 -0800 | [diff] [blame] | 234 | } catch (IllegalArgumentException e) { |
| 235 | Log.e(TAG, "addDns failed with addrString " + addrString); |
| 236 | return true; |
| 237 | } |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 238 | } |
| 239 | return false; |
| 240 | } |
| 241 | |
Remi NGUYEN VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 242 | 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 Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | public void setLeaseDuration(int duration) { |
| 290 | leaseDuration = duration; |
| 291 | } |
| 292 | |
Remi NGUYEN VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 293 | public String getVendorInfo() { |
| 294 | return vendorInfo; |
| 295 | } |
| 296 | |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 297 | public void setVendorInfo(String info) { |
| 298 | vendorInfo = info; |
| 299 | } |
| 300 | |
Remi NGUYEN VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 301 | public int getMtu() { |
| 302 | return mtu; |
| 303 | } |
| 304 | |
| 305 | public void setMtu(int mtu) { |
| 306 | this.mtu = mtu; |
Robert Greenwalt | 8058f62 | 2012-11-09 10:52:27 -0800 | [diff] [blame] | 307 | } |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 308 | } |