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 | |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 19 | import android.net.NetworkUtils; |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 20 | import android.os.Parcel; |
Robert Greenwalt | 3c97f94 | 2013-01-09 16:28:19 -0800 | [diff] [blame] | 21 | import android.text.TextUtils; |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 22 | import android.util.Log; |
| 23 | |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 24 | import java.net.Inet4Address; |
| 25 | import java.util.Objects; |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 26 | |
| 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 | */ |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 33 | public class DhcpResults extends StaticIpConfiguration { |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 34 | private static final String TAG = "DhcpResults"; |
| 35 | |
Lorenzo Colitti | 577255e | 2015-09-10 18:12:18 +0900 | [diff] [blame] | 36 | public Inet4Address serverAddress; |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 37 | |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 38 | /** Vendor specific information (from RFC 2132). */ |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 39 | public String vendorInfo; |
| 40 | |
| 41 | public int leaseDuration; |
| 42 | |
| 43 | public DhcpResults() { |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 44 | super(); |
| 45 | } |
| 46 | |
| 47 | public DhcpResults(StaticIpConfiguration source) { |
| 48 | super(source); |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /** copy constructor */ |
| 52 | public DhcpResults(DhcpResults source) { |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 53 | super(source); |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 54 | |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 55 | if (source != null) { |
| 56 | // All these are immutable, so no need to make copies. |
| 57 | serverAddress = source.serverAddress; |
| 58 | vendorInfo = source.vendorInfo; |
| 59 | leaseDuration = source.leaseDuration; |
| 60 | } |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Updates the DHCP fields that need to be retained from |
| 65 | * original DHCP request if the current renewal shows them |
| 66 | * being empty. |
| 67 | */ |
| 68 | public void updateFromDhcpRequest(DhcpResults orig) { |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 69 | if (orig == null) return; |
| 70 | if (gateway == null) gateway = orig.gateway; |
| 71 | if (dnsServers.size() == 0) { |
| 72 | dnsServers.addAll(orig.dnsServers); |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Test if this DHCP lease includes vendor hint that network link is |
| 78 | * metered, and sensitive to heavy data transfers. |
| 79 | */ |
| 80 | public boolean hasMeteredHint() { |
| 81 | if (vendorInfo != null) { |
| 82 | return vendorInfo.contains("ANDROID_METERED"); |
| 83 | } else { |
| 84 | return false; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | public void clear() { |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 89 | super.clear(); |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 90 | vendorInfo = null; |
| 91 | leaseDuration = 0; |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public String toString() { |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 96 | StringBuffer str = new StringBuffer(super.toString()); |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 97 | |
| 98 | str.append(" DHCP server ").append(serverAddress); |
| 99 | str.append(" Vendor info ").append(vendorInfo); |
| 100 | str.append(" lease ").append(leaseDuration).append(" seconds"); |
| 101 | |
| 102 | return str.toString(); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public boolean equals(Object obj) { |
| 107 | if (this == obj) return true; |
| 108 | |
| 109 | if (!(obj instanceof DhcpResults)) return false; |
| 110 | |
| 111 | DhcpResults target = (DhcpResults)obj; |
| 112 | |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 113 | return super.equals((StaticIpConfiguration) obj) && |
| 114 | Objects.equals(serverAddress, target.serverAddress) && |
| 115 | Objects.equals(vendorInfo, target.vendorInfo) && |
| 116 | leaseDuration == target.leaseDuration; |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /** Implement the Parcelable interface */ |
| 120 | public static final Creator<DhcpResults> CREATOR = |
| 121 | new Creator<DhcpResults>() { |
| 122 | public DhcpResults createFromParcel(Parcel in) { |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 123 | DhcpResults dhcpResults = new DhcpResults(); |
| 124 | readFromParcel(dhcpResults, in); |
| 125 | return dhcpResults; |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | public DhcpResults[] newArray(int size) { |
| 129 | return new DhcpResults[size]; |
| 130 | } |
| 131 | }; |
| 132 | |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 133 | /** Implement the Parcelable interface */ |
| 134 | public void writeToParcel(Parcel dest, int flags) { |
| 135 | super.writeToParcel(dest, flags); |
| 136 | dest.writeInt(leaseDuration); |
| 137 | NetworkUtils.parcelInetAddress(dest, serverAddress, flags); |
| 138 | dest.writeString(vendorInfo); |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 141 | private static void readFromParcel(DhcpResults dhcpResults, Parcel in) { |
| 142 | StaticIpConfiguration.readFromParcel(dhcpResults, in); |
| 143 | dhcpResults.leaseDuration = in.readInt(); |
Lorenzo Colitti | 577255e | 2015-09-10 18:12:18 +0900 | [diff] [blame] | 144 | dhcpResults.serverAddress = (Inet4Address) NetworkUtils.unparcelInetAddress(in); |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 145 | dhcpResults.vendorInfo = in.readString(); |
| 146 | } |
| 147 | |
| 148 | // Utils for jni population - false on success |
| 149 | // Not part of the superclass because they're only used by the JNI iterface to the DHCP daemon. |
| 150 | public boolean setIpAddress(String addrString, int prefixLength) { |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 151 | try { |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 152 | Inet4Address addr = (Inet4Address) NetworkUtils.numericToInetAddress(addrString); |
| 153 | ipAddress = new LinkAddress(addr, prefixLength); |
| 154 | } catch (IllegalArgumentException|ClassCastException e) { |
| 155 | Log.e(TAG, "setIpAddress failed with addrString " + addrString + "/" + prefixLength); |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 156 | return true; |
| 157 | } |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 158 | return false; |
| 159 | } |
| 160 | |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 161 | public boolean setGateway(String addrString) { |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 162 | try { |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 163 | gateway = NetworkUtils.numericToInetAddress(addrString); |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 164 | } catch (IllegalArgumentException e) { |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 165 | Log.e(TAG, "setGateway failed with addrString " + addrString); |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 166 | return true; |
| 167 | } |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | public boolean addDns(String addrString) { |
Robert Greenwalt | 3c97f94 | 2013-01-09 16:28:19 -0800 | [diff] [blame] | 172 | if (TextUtils.isEmpty(addrString) == false) { |
| 173 | try { |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 174 | dnsServers.add(NetworkUtils.numericToInetAddress(addrString)); |
Robert Greenwalt | 3c97f94 | 2013-01-09 16:28:19 -0800 | [diff] [blame] | 175 | } catch (IllegalArgumentException e) { |
| 176 | Log.e(TAG, "addDns failed with addrString " + addrString); |
| 177 | return true; |
| 178 | } |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 179 | } |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | public boolean setServerAddress(String addrString) { |
| 184 | try { |
Lorenzo Colitti | 577255e | 2015-09-10 18:12:18 +0900 | [diff] [blame] | 185 | serverAddress = (Inet4Address) NetworkUtils.numericToInetAddress(addrString); |
| 186 | } catch (IllegalArgumentException|ClassCastException e) { |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 187 | Log.e(TAG, "setServerAddress failed with addrString " + addrString); |
| 188 | return true; |
| 189 | } |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | public void setLeaseDuration(int duration) { |
| 194 | leaseDuration = duration; |
| 195 | } |
| 196 | |
| 197 | public void setVendorInfo(String info) { |
| 198 | vendorInfo = info; |
| 199 | } |
| 200 | |
Paul Jensen | c53113b | 2014-11-05 09:35:26 -0500 | [diff] [blame] | 201 | public void setDomains(String newDomains) { |
| 202 | domains = newDomains; |
Robert Greenwalt | 8058f62 | 2012-11-09 10:52:27 -0800 | [diff] [blame] | 203 | } |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 204 | } |