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