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