blob: 71df60ad88f0fdeb8d12137275cc1601b86f807a [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.Parcelable;
21import android.os.Parcel;
Robert Greenwalt3c97f942013-01-09 16:28:19 -080022import android.text.TextUtils;
Robert Greenwalt4717c262012-10-31 14:32:53 -070023import android.util.Log;
24
25import java.net.InetAddress;
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090026import java.net.Inet4Address;
27import java.util.Objects;
Robert Greenwalt4717c262012-10-31 14:32:53 -070028
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 Colitti0a82e802014-07-31 00:48:01 +090035public class DhcpResults extends StaticIpConfiguration {
Robert Greenwalt4717c262012-10-31 14:32:53 -070036 private static final String TAG = "DhcpResults";
37
Robert Greenwalt4717c262012-10-31 14:32:53 -070038 public InetAddress serverAddress;
39
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090040 /** Vendor specific information (from RFC 2132). */
Robert Greenwalt4717c262012-10-31 14:32:53 -070041 public String vendorInfo;
42
43 public int leaseDuration;
44
45 public DhcpResults() {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090046 super();
47 }
48
49 public DhcpResults(StaticIpConfiguration source) {
50 super(source);
Robert Greenwalt4717c262012-10-31 14:32:53 -070051 }
52
53 /** copy constructor */
54 public DhcpResults(DhcpResults source) {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090055 super(source);
Robert Greenwalt4717c262012-10-31 14:32:53 -070056
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090057 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 Greenwalt4717c262012-10-31 14:32:53 -070063 }
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 Colitti0a82e802014-07-31 00:48:01 +090071 if (orig == null) return;
72 if (gateway == null) gateway = orig.gateway;
73 if (dnsServers.size() == 0) {
74 dnsServers.addAll(orig.dnsServers);
Robert Greenwalt4717c262012-10-31 14:32:53 -070075 }
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 Colitti0a82e802014-07-31 00:48:01 +090091 super.clear();
Robert Greenwalt4717c262012-10-31 14:32:53 -070092 vendorInfo = null;
93 leaseDuration = 0;
94 }
95
96 @Override
97 public String toString() {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090098 StringBuffer str = new StringBuffer(super.toString());
Robert Greenwalt4717c262012-10-31 14:32:53 -070099
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 Colitti0a82e802014-07-31 00:48:01 +0900115 return super.equals((StaticIpConfiguration) obj) &&
116 Objects.equals(serverAddress, target.serverAddress) &&
117 Objects.equals(vendorInfo, target.vendorInfo) &&
118 leaseDuration == target.leaseDuration;
Robert Greenwalt4717c262012-10-31 14:32:53 -0700119 }
120
121 /** Implement the Parcelable interface */
122 public static final Creator<DhcpResults> CREATOR =
123 new Creator<DhcpResults>() {
124 public DhcpResults createFromParcel(Parcel in) {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900125 DhcpResults dhcpResults = new DhcpResults();
126 readFromParcel(dhcpResults, in);
127 return dhcpResults;
Robert Greenwalt4717c262012-10-31 14:32:53 -0700128 }
129
130 public DhcpResults[] newArray(int size) {
131 return new DhcpResults[size];
132 }
133 };
134
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900135 /** 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 Greenwalt4717c262012-10-31 14:32:53 -0700141 }
142
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900143 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 Greenwalt4717c262012-10-31 14:32:53 -0700153 try {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900154 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 Greenwalt4717c262012-10-31 14:32:53 -0700158 return true;
159 }
Robert Greenwalt4717c262012-10-31 14:32:53 -0700160 return false;
161 }
162
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900163 public boolean setGateway(String addrString) {
Robert Greenwalt4717c262012-10-31 14:32:53 -0700164 try {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900165 gateway = NetworkUtils.numericToInetAddress(addrString);
Robert Greenwalt4717c262012-10-31 14:32:53 -0700166 } catch (IllegalArgumentException e) {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900167 Log.e(TAG, "setGateway failed with addrString " + addrString);
Robert Greenwalt4717c262012-10-31 14:32:53 -0700168 return true;
169 }
170 return false;
171 }
172
173 public boolean addDns(String addrString) {
Robert Greenwalt3c97f942013-01-09 16:28:19 -0800174 if (TextUtils.isEmpty(addrString) == false) {
175 try {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900176 dnsServers.add(NetworkUtils.numericToInetAddress(addrString));
Robert Greenwalt3c97f942013-01-09 16:28:19 -0800177 } catch (IllegalArgumentException e) {
178 Log.e(TAG, "addDns failed with addrString " + addrString);
179 return true;
180 }
Robert Greenwalt4717c262012-10-31 14:32:53 -0700181 }
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 Greenwalt8058f622012-11-09 10:52:27 -0800203 public void setDomains(String domains) {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900204 domains = domains;
Robert Greenwalt8058f622012-11-09 10:52:27 -0800205 }
Robert Greenwalt4717c262012-10-31 14:32:53 -0700206}