1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.net;
import android.net.LinkAddress;
import android.os.Parcelable;
import android.os.Parcel;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* Class that describes static IP configuration.
*
* This class is different from LinkProperties because it represents
* configuration intent. The general contract is that if we can represent
* a configuration here, then we should be able to configure it on a network.
* The intent is that it closely match the UI we have for configuring networks.
*
* In contrast, LinkProperties represents current state. It is much more
* expressive. For example, it supports multiple IP addresses, multiple routes,
* stacked interfaces, and so on. Because LinkProperties is so expressive,
* using it to represent configuration intent as well as current state causes
* problems. For example, we could unknowingly save a configuration that we are
* not in fact capable of applying, or we could save a configuration that the
* UI cannot display, which has the potential for malicious code to hide
* hostile or unexpected configuration from the user: see, for example,
* http://b/12663469 and http://b/16893413 .
*
* @hide
*/
public class StaticIpConfiguration implements Parcelable {
public LinkAddress ipAddress;
public InetAddress gateway;
public final ArrayList<InetAddress> dnsServers;
public String domains;
public StaticIpConfiguration() {
dnsServers = new ArrayList<InetAddress>();
}
public StaticIpConfiguration(StaticIpConfiguration source) {
this();
if (source != null) {
// All of these except dnsServers are immutable, so no need to make copies.
ipAddress = source.ipAddress;
gateway = source.gateway;
dnsServers.addAll(source.dnsServers);
domains = source.domains;
}
}
public void clear() {
ipAddress = null;
gateway = null;
dnsServers.clear();
domains = null;
}
/**
* Returns the network routes specified by this object. Will typically include a
* directly-connected route for the IP address's local subnet and a default route. If the
* default gateway is not covered by the directly-connected route, it will also contain a host
* route to the gateway as well. This configuration is arguably invalid, but it used to work
* in K and earlier, and other OSes appear to accept it.
*/
public List<RouteInfo> getRoutes(String iface) {
List<RouteInfo> routes = new ArrayList<RouteInfo>(3);
if (ipAddress != null) {
RouteInfo connectedRoute = new RouteInfo(ipAddress, null, iface);
routes.add(connectedRoute);
if (gateway != null && !connectedRoute.matches(gateway)) {
routes.add(RouteInfo.makeHostRoute(gateway, iface));
}
}
if (gateway != null) {
routes.add(new RouteInfo((IpPrefix) null, gateway, iface));
}
return routes;
}
/**
* Returns a LinkProperties object expressing the data in this object. Note that the information
* contained in the LinkProperties will not be a complete picture of the link's configuration,
* because any configuration information that is obtained dynamically by the network (e.g.,
* IPv6 configuration) will not be included.
*/
public LinkProperties toLinkProperties(String iface) {
LinkProperties lp = new LinkProperties();
lp.setInterfaceName(iface);
if (ipAddress != null) {
lp.addLinkAddress(ipAddress);
}
for (RouteInfo route : getRoutes(iface)) {
lp.addRoute(route);
}
for (InetAddress dns : dnsServers) {
lp.addDnsServer(dns);
}
lp.setDomains(domains);
return lp;
}
public String toString() {
StringBuffer str = new StringBuffer();
str.append("IP address ");
if (ipAddress != null ) str.append(ipAddress).append(" ");
str.append("Gateway ");
if (gateway != null) str.append(gateway.getHostAddress()).append(" ");
str.append(" DNS servers: [");
for (InetAddress dnsServer : dnsServers) {
str.append(" ").append(dnsServer.getHostAddress());
}
str.append(" ] Domains ");
if (domains != null) str.append(domains);
return str.toString();
}
public int hashCode() {
int result = 13;
result = 47 * result + (ipAddress == null ? 0 : ipAddress.hashCode());
result = 47 * result + (gateway == null ? 0 : gateway.hashCode());
result = 47 * result + (domains == null ? 0 : domains.hashCode());
result = 47 * result + dnsServers.hashCode();
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof StaticIpConfiguration)) return false;
StaticIpConfiguration other = (StaticIpConfiguration) obj;
return other != null &&
Objects.equals(ipAddress, other.ipAddress) &&
Objects.equals(gateway, other.gateway) &&
dnsServers.equals(other.dnsServers) &&
Objects.equals(domains, other.domains);
}
/** Implement the Parcelable interface */
public static Creator<StaticIpConfiguration> CREATOR =
new Creator<StaticIpConfiguration>() {
public StaticIpConfiguration createFromParcel(Parcel in) {
StaticIpConfiguration s = new StaticIpConfiguration();
readFromParcel(s, in);
return s;
}
public StaticIpConfiguration[] newArray(int size) {
return new StaticIpConfiguration[size];
}
};
/** Implement the Parcelable interface */
public int describeContents() {
return 0;
}
/** Implement the Parcelable interface */
public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelable(ipAddress, flags);
NetworkUtils.parcelInetAddress(dest, gateway, flags);
dest.writeInt(dnsServers.size());
for (InetAddress dnsServer : dnsServers) {
NetworkUtils.parcelInetAddress(dest, dnsServer, flags);
}
dest.writeString(domains);
}
protected static void readFromParcel(StaticIpConfiguration s, Parcel in) {
s.ipAddress = in.readParcelable(null);
s.gateway = NetworkUtils.unparcelInetAddress(in);
s.dnsServers.clear();
int size = in.readInt();
for (int i = 0; i < size; i++) {
s.dnsServers.add(NetworkUtils.unparcelInetAddress(in));
}
s.domains = in.readString();
}
}
|