2 * Copyright (C) 2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 import android.net.NetworkUtils;
20 import android.os.Parcel;
21 import android.text.TextUtils;
22 import android.util.Log;
24 import java.net.Inet4Address;
25 import java.util.Objects;
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.
33 public class DhcpResults extends StaticIpConfiguration {
34 private static final String TAG = "DhcpResults";
36 public Inet4Address serverAddress;
38 /** Vendor specific information (from RFC 2132). */
39 public String vendorInfo;
41 public int leaseDuration;
43 /** Link MTU option. 0 means unset. */
46 public DhcpResults() {
50 public DhcpResults(StaticIpConfiguration source) {
54 /** copy constructor */
55 public DhcpResults(DhcpResults source) {
59 // All these are immutable, so no need to make copies.
60 serverAddress = source.serverAddress;
61 vendorInfo = source.vendorInfo;
62 leaseDuration = source.leaseDuration;
68 * Test if this DHCP lease includes vendor hint that network link is
69 * metered, and sensitive to heavy data transfers.
71 public boolean hasMeteredHint() {
72 if (vendorInfo != null) {
73 return vendorInfo.contains("ANDROID_METERED");
87 public String toString() {
88 StringBuffer str = new StringBuffer(super.toString());
90 str.append(" DHCP server ").append(serverAddress);
91 str.append(" Vendor info ").append(vendorInfo);
92 str.append(" lease ").append(leaseDuration).append(" seconds");
93 if (mtu != 0) str.append(" MTU ").append(mtu);
95 return str.toString();
99 public boolean equals(Object obj) {
100 if (this == obj) return true;
102 if (!(obj instanceof DhcpResults)) return false;
104 DhcpResults target = (DhcpResults)obj;
106 return super.equals((StaticIpConfiguration) obj) &&
107 Objects.equals(serverAddress, target.serverAddress) &&
108 Objects.equals(vendorInfo, target.vendorInfo) &&
109 leaseDuration == target.leaseDuration &&
113 /** Implement the Parcelable interface */
114 public static final Creator<DhcpResults> CREATOR =
115 new Creator<DhcpResults>() {
116 public DhcpResults createFromParcel(Parcel in) {
117 DhcpResults dhcpResults = new DhcpResults();
118 readFromParcel(dhcpResults, in);
122 public DhcpResults[] newArray(int size) {
123 return new DhcpResults[size];
127 /** Implement the Parcelable interface */
128 public void writeToParcel(Parcel dest, int flags) {
129 super.writeToParcel(dest, flags);
130 dest.writeInt(leaseDuration);
132 NetworkUtils.parcelInetAddress(dest, serverAddress, flags);
133 dest.writeString(vendorInfo);
136 private static void readFromParcel(DhcpResults dhcpResults, Parcel in) {
137 StaticIpConfiguration.readFromParcel(dhcpResults, in);
138 dhcpResults.leaseDuration = in.readInt();
139 dhcpResults.mtu = in.readInt();
140 dhcpResults.serverAddress = (Inet4Address) NetworkUtils.unparcelInetAddress(in);
141 dhcpResults.vendorInfo = in.readString();
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) {
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);
157 public boolean setGateway(String addrString) {
159 gateway = NetworkUtils.numericToInetAddress(addrString);
160 } catch (IllegalArgumentException e) {
161 Log.e(TAG, "setGateway failed with addrString " + addrString);
167 public boolean addDns(String addrString) {
168 if (TextUtils.isEmpty(addrString) == false) {
170 dnsServers.add(NetworkUtils.numericToInetAddress(addrString));
171 } catch (IllegalArgumentException e) {
172 Log.e(TAG, "addDns failed with addrString " + addrString);
179 public boolean setServerAddress(String addrString) {
181 serverAddress = (Inet4Address) NetworkUtils.numericToInetAddress(addrString);
182 } catch (IllegalArgumentException|ClassCastException e) {
183 Log.e(TAG, "setServerAddress failed with addrString " + addrString);
189 public void setLeaseDuration(int duration) {
190 leaseDuration = duration;
193 public void setVendorInfo(String info) {
197 public void setDomains(String newDomains) {
198 domains = newDomains;