blob: 97bd5d284c711a5eb1eee8b2d75f323772bde371 [file] [log] [blame]
Robert Greenwalt4717c262012-10-31 14:32:53 -07001/*
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
17package android.net;
18
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090019import android.net.NetworkUtils;
Robert Greenwalt4717c262012-10-31 14:32:53 -070020import android.os.Parcel;
Robert Greenwalt3c97f942013-01-09 16:28:19 -080021import android.text.TextUtils;
Robert Greenwalt4717c262012-10-31 14:32:53 -070022import android.util.Log;
23
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090024import java.net.Inet4Address;
25import java.util.Objects;
Robert Greenwalt4717c262012-10-31 14:32:53 -070026
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 Colitti0a82e802014-07-31 00:48:01 +090033public class DhcpResults extends StaticIpConfiguration {
Robert Greenwalt4717c262012-10-31 14:32:53 -070034 private static final String TAG = "DhcpResults";
35
Lorenzo Colitti577255e2015-09-10 18:12:18 +090036 public Inet4Address serverAddress;
Robert Greenwalt4717c262012-10-31 14:32:53 -070037
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090038 /** Vendor specific information (from RFC 2132). */
Robert Greenwalt4717c262012-10-31 14:32:53 -070039 public String vendorInfo;
40
41 public int leaseDuration;
42
43 public DhcpResults() {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090044 super();
45 }
46
47 public DhcpResults(StaticIpConfiguration source) {
48 super(source);
Robert Greenwalt4717c262012-10-31 14:32:53 -070049 }
50
51 /** copy constructor */
52 public DhcpResults(DhcpResults source) {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090053 super(source);
Robert Greenwalt4717c262012-10-31 14:32:53 -070054
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090055 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 Greenwalt4717c262012-10-31 14:32:53 -070061 }
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 Colitti0a82e802014-07-31 00:48:01 +090069 if (orig == null) return;
70 if (gateway == null) gateway = orig.gateway;
71 if (dnsServers.size() == 0) {
72 dnsServers.addAll(orig.dnsServers);
Robert Greenwalt4717c262012-10-31 14:32:53 -070073 }
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 Colitti0a82e802014-07-31 00:48:01 +090089 super.clear();
Robert Greenwalt4717c262012-10-31 14:32:53 -070090 vendorInfo = null;
91 leaseDuration = 0;
92 }
93
94 @Override
95 public String toString() {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090096 StringBuffer str = new StringBuffer(super.toString());
Robert Greenwalt4717c262012-10-31 14:32:53 -070097
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 Colitti0a82e802014-07-31 00:48:01 +0900113 return super.equals((StaticIpConfiguration) obj) &&
114 Objects.equals(serverAddress, target.serverAddress) &&
115 Objects.equals(vendorInfo, target.vendorInfo) &&
116 leaseDuration == target.leaseDuration;
Robert Greenwalt4717c262012-10-31 14:32:53 -0700117 }
118
119 /** Implement the Parcelable interface */
120 public static final Creator<DhcpResults> CREATOR =
121 new Creator<DhcpResults>() {
122 public DhcpResults createFromParcel(Parcel in) {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900123 DhcpResults dhcpResults = new DhcpResults();
124 readFromParcel(dhcpResults, in);
125 return dhcpResults;
Robert Greenwalt4717c262012-10-31 14:32:53 -0700126 }
127
128 public DhcpResults[] newArray(int size) {
129 return new DhcpResults[size];
130 }
131 };
132
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900133 /** 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 Greenwalt4717c262012-10-31 14:32:53 -0700139 }
140
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900141 private static void readFromParcel(DhcpResults dhcpResults, Parcel in) {
142 StaticIpConfiguration.readFromParcel(dhcpResults, in);
143 dhcpResults.leaseDuration = in.readInt();
Lorenzo Colitti577255e2015-09-10 18:12:18 +0900144 dhcpResults.serverAddress = (Inet4Address) NetworkUtils.unparcelInetAddress(in);
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900145 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 Greenwalt4717c262012-10-31 14:32:53 -0700151 try {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900152 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 Greenwalt4717c262012-10-31 14:32:53 -0700156 return true;
157 }
Robert Greenwalt4717c262012-10-31 14:32:53 -0700158 return false;
159 }
160
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900161 public boolean setGateway(String addrString) {
Robert Greenwalt4717c262012-10-31 14:32:53 -0700162 try {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900163 gateway = NetworkUtils.numericToInetAddress(addrString);
Robert Greenwalt4717c262012-10-31 14:32:53 -0700164 } catch (IllegalArgumentException e) {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900165 Log.e(TAG, "setGateway failed with addrString " + addrString);
Robert Greenwalt4717c262012-10-31 14:32:53 -0700166 return true;
167 }
168 return false;
169 }
170
171 public boolean addDns(String addrString) {
Robert Greenwalt3c97f942013-01-09 16:28:19 -0800172 if (TextUtils.isEmpty(addrString) == false) {
173 try {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900174 dnsServers.add(NetworkUtils.numericToInetAddress(addrString));
Robert Greenwalt3c97f942013-01-09 16:28:19 -0800175 } catch (IllegalArgumentException e) {
176 Log.e(TAG, "addDns failed with addrString " + addrString);
177 return true;
178 }
Robert Greenwalt4717c262012-10-31 14:32:53 -0700179 }
180 return false;
181 }
182
183 public boolean setServerAddress(String addrString) {
184 try {
Lorenzo Colitti577255e2015-09-10 18:12:18 +0900185 serverAddress = (Inet4Address) NetworkUtils.numericToInetAddress(addrString);
186 } catch (IllegalArgumentException|ClassCastException e) {
Robert Greenwalt4717c262012-10-31 14:32:53 -0700187 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 Jensenc53113b2014-11-05 09:35:26 -0500201 public void setDomains(String newDomains) {
202 domains = newDomains;
Robert Greenwalt8058f622012-11-09 10:52:27 -0800203 }
Robert Greenwalt4717c262012-10-31 14:32:53 -0700204}