blob: 8c5f60395ca94eec4ebbe7f5cf29aed7deecf06f [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
Lorenzo Colitti77fdf232016-03-31 16:20:22 +090043 /** Link MTU option. 0 means unset. */
44 public int mtu;
45
Robert Greenwalt4717c262012-10-31 14:32:53 -070046 public DhcpResults() {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090047 super();
48 }
49
50 public DhcpResults(StaticIpConfiguration source) {
51 super(source);
Robert Greenwalt4717c262012-10-31 14:32:53 -070052 }
53
54 /** copy constructor */
55 public DhcpResults(DhcpResults source) {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090056 super(source);
Robert Greenwalt4717c262012-10-31 14:32:53 -070057
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090058 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 Colitti77fdf232016-03-31 16:20:22 +090063 mtu = source.mtu;
Robert Greenwalt4717c262012-10-31 14:32:53 -070064 }
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 Colitti0a82e802014-07-31 00:48:01 +090080 super.clear();
Robert Greenwalt4717c262012-10-31 14:32:53 -070081 vendorInfo = null;
82 leaseDuration = 0;
Lorenzo Colitti77fdf232016-03-31 16:20:22 +090083 mtu = 0;
Robert Greenwalt4717c262012-10-31 14:32:53 -070084 }
85
86 @Override
87 public String toString() {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +090088 StringBuffer str = new StringBuffer(super.toString());
Robert Greenwalt4717c262012-10-31 14:32:53 -070089
90 str.append(" DHCP server ").append(serverAddress);
91 str.append(" Vendor info ").append(vendorInfo);
92 str.append(" lease ").append(leaseDuration).append(" seconds");
Lorenzo Colitti77fdf232016-03-31 16:20:22 +090093 if (mtu != 0) str.append(" MTU ").append(mtu);
Robert Greenwalt4717c262012-10-31 14:32:53 -070094
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 Colitti0a82e802014-07-31 00:48:01 +0900106 return super.equals((StaticIpConfiguration) obj) &&
107 Objects.equals(serverAddress, target.serverAddress) &&
108 Objects.equals(vendorInfo, target.vendorInfo) &&
Lorenzo Colitti77fdf232016-03-31 16:20:22 +0900109 leaseDuration == target.leaseDuration &&
110 mtu == target.mtu;
Robert Greenwalt4717c262012-10-31 14:32:53 -0700111 }
112
113 /** Implement the Parcelable interface */
114 public static final Creator<DhcpResults> CREATOR =
115 new Creator<DhcpResults>() {
116 public DhcpResults createFromParcel(Parcel in) {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900117 DhcpResults dhcpResults = new DhcpResults();
118 readFromParcel(dhcpResults, in);
119 return dhcpResults;
Robert Greenwalt4717c262012-10-31 14:32:53 -0700120 }
121
122 public DhcpResults[] newArray(int size) {
123 return new DhcpResults[size];
124 }
125 };
126
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900127 /** Implement the Parcelable interface */
128 public void writeToParcel(Parcel dest, int flags) {
129 super.writeToParcel(dest, flags);
130 dest.writeInt(leaseDuration);
Lorenzo Colitti77fdf232016-03-31 16:20:22 +0900131 dest.writeInt(mtu);
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900132 NetworkUtils.parcelInetAddress(dest, serverAddress, flags);
133 dest.writeString(vendorInfo);
Robert Greenwalt4717c262012-10-31 14:32:53 -0700134 }
135
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900136 private static void readFromParcel(DhcpResults dhcpResults, Parcel in) {
137 StaticIpConfiguration.readFromParcel(dhcpResults, in);
138 dhcpResults.leaseDuration = in.readInt();
Lorenzo Colitti77fdf232016-03-31 16:20:22 +0900139 dhcpResults.mtu = in.readInt();
Lorenzo Colitti577255e2015-09-10 18:12:18 +0900140 dhcpResults.serverAddress = (Inet4Address) NetworkUtils.unparcelInetAddress(in);
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900141 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 Greenwalt4717c262012-10-31 14:32:53 -0700147 try {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900148 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 Greenwalt4717c262012-10-31 14:32:53 -0700152 return true;
153 }
Robert Greenwalt4717c262012-10-31 14:32:53 -0700154 return false;
155 }
156
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900157 public boolean setGateway(String addrString) {
Robert Greenwalt4717c262012-10-31 14:32:53 -0700158 try {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900159 gateway = NetworkUtils.numericToInetAddress(addrString);
Robert Greenwalt4717c262012-10-31 14:32:53 -0700160 } catch (IllegalArgumentException e) {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900161 Log.e(TAG, "setGateway failed with addrString " + addrString);
Robert Greenwalt4717c262012-10-31 14:32:53 -0700162 return true;
163 }
164 return false;
165 }
166
167 public boolean addDns(String addrString) {
Robert Greenwalt3c97f942013-01-09 16:28:19 -0800168 if (TextUtils.isEmpty(addrString) == false) {
169 try {
Lorenzo Colitti0a82e802014-07-31 00:48:01 +0900170 dnsServers.add(NetworkUtils.numericToInetAddress(addrString));
Robert Greenwalt3c97f942013-01-09 16:28:19 -0800171 } catch (IllegalArgumentException e) {
172 Log.e(TAG, "addDns failed with addrString " + addrString);
173 return true;
174 }
Robert Greenwalt4717c262012-10-31 14:32:53 -0700175 }
176 return false;
177 }
178
179 public boolean setServerAddress(String addrString) {
180 try {
Lorenzo Colitti577255e2015-09-10 18:12:18 +0900181 serverAddress = (Inet4Address) NetworkUtils.numericToInetAddress(addrString);
182 } catch (IllegalArgumentException|ClassCastException e) {
Robert Greenwalt4717c262012-10-31 14:32:53 -0700183 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 Jensenc53113b2014-11-05 09:35:26 -0500197 public void setDomains(String newDomains) {
198 domains = newDomains;
Robert Greenwalt8058f622012-11-09 10:52:27 -0800199 }
Robert Greenwalt4717c262012-10-31 14:32:53 -0700200}