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 | |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 67 | public DhcpResults() { |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 68 | super(); |
| 69 | } |
| 70 | |
Remi NGUYEN VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 71 | /** |
| 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 VAN | 231b52b | 2019-01-29 15:38:52 +0900 | [diff] [blame] | 77 | s.setIpAddress(ipAddress); |
| 78 | s.setGateway(gateway); |
| 79 | for (InetAddress addr : dnsServers) { |
| 80 | s.addDnsServer(addr); |
| 81 | } |
| 82 | s.setDomains(domains); |
Remi NGUYEN VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 83 | return s; |
| 84 | } |
| 85 | |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 86 | public DhcpResults(StaticIpConfiguration source) { |
Remi NGUYEN VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 87 | if (source != null) { |
Remi NGUYEN VAN | 231b52b | 2019-01-29 15:38:52 +0900 | [diff] [blame] | 88 | ipAddress = source.getIpAddress(); |
| 89 | gateway = source.getGateway(); |
| 90 | dnsServers.addAll(source.getDnsServers()); |
| 91 | domains = source.getDomains(); |
Remi NGUYEN VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 92 | } |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /** copy constructor */ |
| 96 | public DhcpResults(DhcpResults source) { |
Remi NGUYEN VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 97 | this(source == null ? null : source.toStaticIpConfiguration()); |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 98 | if (source != null) { |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 99 | serverAddress = source.serverAddress; |
| 100 | vendorInfo = source.vendorInfo; |
| 101 | leaseDuration = source.leaseDuration; |
Lorenzo Colitti | 77fdf23 | 2016-03-31 16:20:22 +0900 | [diff] [blame] | 102 | mtu = source.mtu; |
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; |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public String toString() { |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 139 | StringBuffer str = new StringBuffer(super.toString()); |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 140 | |
| 141 | str.append(" DHCP server ").append(serverAddress); |
| 142 | str.append(" Vendor info ").append(vendorInfo); |
| 143 | str.append(" lease ").append(leaseDuration).append(" seconds"); |
Lorenzo Colitti | 77fdf23 | 2016-03-31 16:20:22 +0900 | [diff] [blame] | 144 | if (mtu != 0) str.append(" MTU ").append(mtu); |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 145 | |
| 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 VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 157 | 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 Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Remi NGUYEN VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 164 | /** |
| 165 | * Implement the Parcelable interface |
| 166 | */ |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 167 | public static final @android.annotation.NonNull Creator<DhcpResults> CREATOR = |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 168 | new Creator<DhcpResults>() { |
| 169 | public DhcpResults createFromParcel(Parcel in) { |
Remi NGUYEN VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 170 | return readFromParcel(in); |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | public DhcpResults[] newArray(int size) { |
| 174 | return new DhcpResults[size]; |
| 175 | } |
| 176 | }; |
| 177 | |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 178 | /** Implement the Parcelable interface */ |
| 179 | public void writeToParcel(Parcel dest, int flags) { |
Remi NGUYEN VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 180 | toStaticIpConfiguration().writeToParcel(dest, flags); |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 181 | dest.writeInt(leaseDuration); |
Lorenzo Colitti | 77fdf23 | 2016-03-31 16:20:22 +0900 | [diff] [blame] | 182 | dest.writeInt(mtu); |
Remi NGUYEN VAN | 231b52b | 2019-01-29 15:38:52 +0900 | [diff] [blame] | 183 | InetAddressUtils.parcelInetAddress(dest, serverAddress, flags); |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 184 | dest.writeString(vendorInfo); |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Remi NGUYEN VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 187 | @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 Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 195 | dhcpResults.leaseDuration = in.readInt(); |
Lorenzo Colitti | 77fdf23 | 2016-03-31 16:20:22 +0900 | [diff] [blame] | 196 | dhcpResults.mtu = in.readInt(); |
Remi NGUYEN VAN | 231b52b | 2019-01-29 15:38:52 +0900 | [diff] [blame] | 197 | dhcpResults.serverAddress = (Inet4Address) InetAddressUtils.unparcelInetAddress(in); |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 198 | dhcpResults.vendorInfo = in.readString(); |
Remi NGUYEN VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 199 | return dhcpResults; |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 200 | } |
| 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 Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 205 | try { |
Remi NGUYEN VAN | 231b52b | 2019-01-29 15:38:52 +0900 | [diff] [blame] | 206 | Inet4Address addr = (Inet4Address) InetAddresses.parseNumericAddress(addrString); |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 207 | ipAddress = new LinkAddress(addr, prefixLength); |
| 208 | } catch (IllegalArgumentException|ClassCastException e) { |
| 209 | Log.e(TAG, "setIpAddress failed with addrString " + addrString + "/" + prefixLength); |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 210 | return true; |
| 211 | } |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 212 | return false; |
| 213 | } |
| 214 | |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 215 | public boolean setGateway(String addrString) { |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 216 | try { |
Remi NGUYEN VAN | 231b52b | 2019-01-29 15:38:52 +0900 | [diff] [blame] | 217 | gateway = InetAddresses.parseNumericAddress(addrString); |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 218 | } catch (IllegalArgumentException e) { |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 219 | Log.e(TAG, "setGateway failed with addrString " + addrString); |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 220 | return true; |
| 221 | } |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | public boolean addDns(String addrString) { |
Robert Greenwalt | 3c97f94 | 2013-01-09 16:28:19 -0800 | [diff] [blame] | 226 | if (TextUtils.isEmpty(addrString) == false) { |
| 227 | try { |
Remi NGUYEN VAN | 231b52b | 2019-01-29 15:38:52 +0900 | [diff] [blame] | 228 | dnsServers.add(InetAddresses.parseNumericAddress(addrString)); |
Robert Greenwalt | 3c97f94 | 2013-01-09 16:28:19 -0800 | [diff] [blame] | 229 | } catch (IllegalArgumentException e) { |
| 230 | Log.e(TAG, "addDns failed with addrString " + addrString); |
| 231 | return true; |
| 232 | } |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 233 | } |
| 234 | return false; |
| 235 | } |
| 236 | |
Remi NGUYEN VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 237 | 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 Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | public void setLeaseDuration(int duration) { |
| 285 | leaseDuration = duration; |
| 286 | } |
| 287 | |
Remi NGUYEN VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 288 | public String getVendorInfo() { |
| 289 | return vendorInfo; |
| 290 | } |
| 291 | |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 292 | public void setVendorInfo(String info) { |
| 293 | vendorInfo = info; |
| 294 | } |
| 295 | |
Remi NGUYEN VAN | a4bcc86 | 2019-01-28 13:28:35 +0900 | [diff] [blame] | 296 | public int getMtu() { |
| 297 | return mtu; |
| 298 | } |
| 299 | |
| 300 | public void setMtu(int mtu) { |
| 301 | this.mtu = mtu; |
Robert Greenwalt | 8058f62 | 2012-11-09 10:52:27 -0800 | [diff] [blame] | 302 | } |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 303 | } |