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 17 package android.net; 18 19 import android.net.LinkAddress; 20 import android.os.Parcelable; 21 import android.os.Parcel; 22 23 import java.net.InetAddress; 24 import java.net.UnknownHostException; 25 import java.util.ArrayList; 26 import java.util.List; 27 import 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 */ 49 public 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 return lp; 111 } 112 113 public String toString() { 114 StringBuffer str = new StringBuffer(); 115 116 str.append("IP address "); 117 if (ipAddress != null ) str.append(ipAddress).append(" "); 118 119 str.append("Gateway "); 120 if (gateway != null) str.append(gateway.getHostAddress()).append(" "); 121 122 str.append(" DNS servers: ["); 123 for (InetAddress dnsServer : dnsServers) { 124 str.append(" ").append(dnsServer.getHostAddress()); 125 } 126 127 str.append(" ] Domains"); 128 if (domains != null) str.append(domains); 129 return str.toString(); 130 } 131 132 public int hashCode() { 133 int result = 13; 134 result = 47 * result + (ipAddress == null ? 0 : ipAddress.hashCode()); 135 result = 47 * result + (gateway == null ? 0 : gateway.hashCode()); 136 result = 47 * result + (domains == null ? 0 : domains.hashCode()); 137 result = 47 * result + dnsServers.hashCode(); 138 return result; 139 } 140 141 @Override 142 public boolean equals(Object obj) { 143 if (this == obj) return true; 144 145 if (!(obj instanceof StaticIpConfiguration)) return false; 146 147 StaticIpConfiguration other = (StaticIpConfiguration) obj; 148 149 return other != null && 150 Objects.equals(ipAddress, other.ipAddress) && 151 Objects.equals(gateway, other.gateway) && 152 dnsServers.equals(other.dnsServers) && 153 Objects.equals(domains, other.domains); 154 } 155 156 /** Implement the Parcelable interface */ 157 public static Creator<StaticIpConfiguration> CREATOR = 158 new Creator<StaticIpConfiguration>() { 159 public StaticIpConfiguration createFromParcel(Parcel in) { 160 StaticIpConfiguration s = new StaticIpConfiguration(); 161 readFromParcel(s, in); 162 return s; 163 } 164 165 public StaticIpConfiguration[] newArray(int size) { 166 return new StaticIpConfiguration[size]; 167 } 168 }; 169 170 /** Implement the Parcelable interface */ 171 public int describeContents() { 172 return 0; 173 } 174 175 /** Implement the Parcelable interface */ 176 public void writeToParcel(Parcel dest, int flags) { 177 dest.writeParcelable(ipAddress, flags); 178 NetworkUtils.parcelInetAddress(dest, gateway, flags); 179 dest.writeInt(dnsServers.size()); 180 for (InetAddress dnsServer : dnsServers) { 181 NetworkUtils.parcelInetAddress(dest, dnsServer, flags); 182 } 183 } 184 185 protected static void readFromParcel(StaticIpConfiguration s, Parcel in) { 186 s.ipAddress = in.readParcelable(null); 187 s.gateway = NetworkUtils.unparcelInetAddress(in); 188 s.dnsServers.clear(); 189 int size = in.readInt(); 190 for (int i = 0; i < size; i++) { 191 s.dnsServers.add(NetworkUtils.unparcelInetAddress(in)); 192 } 193 } 194 } 195