LinkProperties.java revision 04cac40ff86e175444991c07869cb85219db1019
1/* 2 * Copyright (C) 2010 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.ProxyProperties; 20import android.os.Parcelable; 21import android.os.Parcel; 22import android.util.Log; 23 24import java.net.InetAddress; 25import java.net.NetworkInterface; 26import java.net.SocketException; 27import java.net.UnknownHostException; 28import java.util.ArrayList; 29import java.util.Collection; 30import java.util.Collections; 31 32/** 33 * Describes the properties of a network link. 34 * 35 * A link represents a connection to a network. 36 * It may have multiple addresses and multiple gateways, 37 * multiple dns servers but only one http proxy. 38 * 39 * Because it's a single network, the dns's 40 * are interchangeable and don't need associating with 41 * particular addresses. The gateways similarly don't 42 * need associating with particular addresses. 43 * 44 * A dual stack interface works fine in this model: 45 * each address has it's own prefix length to describe 46 * the local network. The dns servers all return 47 * both v4 addresses and v6 addresses regardless of the 48 * address family of the server itself (rfc4213) and we 49 * don't care which is used. The gateways will be 50 * selected based on the destination address and the 51 * source address has no relavence. 52 * @hide 53 */ 54public class LinkProperties implements Parcelable { 55 56 String mIfaceName; 57 private Collection<LinkAddress> mLinkAddresses; 58 private Collection<InetAddress> mDnses; 59 private Collection<InetAddress> mGateways; 60 private ProxyProperties mHttpProxy; 61 62 public LinkProperties() { 63 clear(); 64 } 65 66 // copy constructor instead of clone 67 public LinkProperties(LinkProperties source) { 68 if (source != null) { 69 mIfaceName = source.getInterfaceName(); 70 mLinkAddresses = source.getLinkAddresses(); 71 mDnses = source.getDnses(); 72 mGateways = source.getGateways(); 73 mHttpProxy = new ProxyProperties(source.getHttpProxy()); 74 } 75 } 76 77 public void setInterfaceName(String iface) { 78 mIfaceName = iface; 79 } 80 81 public String getInterfaceName() { 82 return mIfaceName; 83 } 84 85 public Collection<InetAddress> getAddresses() { 86 Collection<InetAddress> addresses = new ArrayList<InetAddress>(); 87 for (LinkAddress linkAddress : mLinkAddresses) { 88 addresses.add(linkAddress.getAddress()); 89 } 90 return Collections.unmodifiableCollection(addresses); 91 } 92 93 public void addLinkAddress(LinkAddress address) { 94 if (address != null) mLinkAddresses.add(address); 95 } 96 97 public Collection<LinkAddress> getLinkAddresses() { 98 return Collections.unmodifiableCollection(mLinkAddresses); 99 } 100 101 public void addDns(InetAddress dns) { 102 if (dns != null) mDnses.add(dns); 103 } 104 105 public Collection<InetAddress> getDnses() { 106 return Collections.unmodifiableCollection(mDnses); 107 } 108 109 public void addGateway(InetAddress gateway) { 110 if (gateway != null) mGateways.add(gateway); 111 } 112 public Collection<InetAddress> getGateways() { 113 return Collections.unmodifiableCollection(mGateways); 114 } 115 116 public void setHttpProxy(ProxyProperties proxy) { 117 mHttpProxy = proxy; 118 } 119 public ProxyProperties getHttpProxy() { 120 return mHttpProxy; 121 } 122 123 public void clear() { 124 mIfaceName = null; 125 mLinkAddresses = new ArrayList<LinkAddress>(); 126 mDnses = new ArrayList<InetAddress>(); 127 mGateways = new ArrayList<InetAddress>(); 128 mHttpProxy = null; 129 } 130 131 /** 132 * Implement the Parcelable interface 133 * @hide 134 */ 135 public int describeContents() { 136 return 0; 137 } 138 139 @Override 140 public String toString() { 141 String ifaceName = (mIfaceName == null ? "" : "InterfaceName: " + mIfaceName + " "); 142 143 String linkAddresses = "LinkAddresses: ["; 144 for (LinkAddress addr : mLinkAddresses) linkAddresses += addr.toString(); 145 linkAddresses += "] "; 146 147 String dns = "DnsAddresses: ["; 148 for (InetAddress addr : mDnses) dns += addr.getHostAddress() + ","; 149 dns += "] "; 150 151 String gateways = "Gateways: ["; 152 for (InetAddress gw : mGateways) gateways += gw.getHostAddress() + ","; 153 gateways += "] "; 154 String proxy = (mHttpProxy == null ? "" : "HttpProxy: " + mHttpProxy.toString() + " "); 155 156 return ifaceName + linkAddresses + gateways + dns + proxy; 157 } 158 159 /** 160 * Implement the Parcelable interface. 161 * @hide 162 */ 163 public void writeToParcel(Parcel dest, int flags) { 164 dest.writeString(getInterfaceName()); 165 dest.writeInt(mLinkAddresses.size()); 166 for(LinkAddress linkAddress : mLinkAddresses) { 167 dest.writeParcelable(linkAddress, flags); 168 } 169 170 dest.writeInt(mDnses.size()); 171 for(InetAddress d : mDnses) { 172 dest.writeByteArray(d.getAddress()); 173 } 174 175 dest.writeInt(mGateways.size()); 176 for(InetAddress gw : mGateways) { 177 dest.writeByteArray(gw.getAddress()); 178 } 179 180 if (mHttpProxy != null) { 181 dest.writeByte((byte)1); 182 dest.writeParcelable(mHttpProxy, flags); 183 } else { 184 dest.writeByte((byte)0); 185 } 186 } 187 188 /** 189 * Implement the Parcelable interface. 190 * @hide 191 */ 192 public static final Creator<LinkProperties> CREATOR = 193 new Creator<LinkProperties>() { 194 public LinkProperties createFromParcel(Parcel in) { 195 LinkProperties netProp = new LinkProperties(); 196 String iface = in.readString(); 197 if (iface != null) { 198 try { 199 netProp.setInterfaceName(iface); 200 } catch (Exception e) { 201 return null; 202 } 203 } 204 int addressCount = in.readInt(); 205 for (int i=0; i<addressCount; i++) { 206 netProp.addLinkAddress((LinkAddress)in.readParcelable(null)); 207 } 208 addressCount = in.readInt(); 209 for (int i=0; i<addressCount; i++) { 210 try { 211 netProp.addDns(InetAddress.getByAddress(in.createByteArray())); 212 } catch (UnknownHostException e) { } 213 } 214 addressCount = in.readInt(); 215 for (int i=0; i<addressCount; i++) { 216 try { 217 netProp.addGateway(InetAddress.getByAddress(in.createByteArray())); 218 } catch (UnknownHostException e) { } 219 } 220 if (in.readByte() == 1) { 221 netProp.setHttpProxy((ProxyProperties)in.readParcelable(null)); 222 } 223 return netProp; 224 } 225 226 public LinkProperties[] newArray(int size) { 227 return new LinkProperties[size]; 228 } 229 }; 230} 231