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