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