LinkProperties.java revision ef6c1431fa2039a4b8c604d651bb8f4dc0806581
1/* 2 * Copyright (C) 2008 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 * TODO - consider adding optional fields like Apn and ApnType 35 * @hide 36 */ 37public class LinkProperties implements Parcelable { 38 39 private NetworkInterface mIface; 40 private Collection<InetAddress> mAddresses; 41 private Collection<InetAddress> mDnses; 42 private InetAddress mGateway; 43 private ProxyProperties mHttpProxy; 44 45 public LinkProperties() { 46 clear(); 47 } 48 49 // copy constructor instead of clone 50 public LinkProperties(LinkProperties source) { 51 if (source != null) { 52 mIface = source.getInterface(); 53 mAddresses = source.getAddresses(); 54 mDnses = source.getDnses(); 55 mGateway = source.getGateway(); 56 mHttpProxy = new ProxyProperties(source.getHttpProxy()); 57 } 58 } 59 60 public void setInterface(NetworkInterface iface) { 61 mIface = iface; 62 } 63 public NetworkInterface getInterface() { 64 return mIface; 65 } 66 public String getInterfaceName() { 67 return (mIface == null ? null : mIface.getName()); 68 } 69 70 public void addAddress(InetAddress address) { 71 mAddresses.add(address); 72 } 73 public Collection<InetAddress> getAddresses() { 74 return Collections.unmodifiableCollection(mAddresses); 75 } 76 77 public void addDns(InetAddress dns) { 78 mDnses.add(dns); 79 } 80 public Collection<InetAddress> getDnses() { 81 return Collections.unmodifiableCollection(mDnses); 82 } 83 84 public void setGateway(InetAddress gateway) { 85 mGateway = gateway; 86 } 87 public InetAddress getGateway() { 88 return mGateway; 89 } 90 91 public void setHttpProxy(ProxyProperties proxy) { 92 mHttpProxy = proxy; 93 } 94 public ProxyProperties getHttpProxy() { 95 return mHttpProxy; 96 } 97 98 public void clear() { 99 mIface = null; 100 mAddresses = new ArrayList<InetAddress>(); 101 mDnses = new ArrayList<InetAddress>(); 102 mGateway = null; 103 mHttpProxy = null; 104 } 105 106 /** 107 * Implement the Parcelable interface 108 * @hide 109 */ 110 public int describeContents() { 111 return 0; 112 } 113 114 @Override 115 public String toString() { 116 String ifaceName = (mIface == null ? "" : "InterfaceName: " + mIface.getName() + " "); 117 118 String ip = "IpAddresses: ["; 119 for (InetAddress addr : mAddresses) ip += addr.getHostAddress() + ","; 120 ip += "] "; 121 122 String dns = "DnsAddresses: ["; 123 for (InetAddress addr : mDnses) dns += addr.getHostAddress() + ","; 124 dns += "] "; 125 126 String proxy = (mHttpProxy == null ? "" : "HttpProxy: " + mHttpProxy.toString() + " "); 127 String gateway = (mGateway == null ? "" : "Gateway: " + mGateway.getHostAddress() + " "); 128 129 return ifaceName + ip + gateway + dns + proxy; 130 } 131 132 /** 133 * Implement the Parcelable interface. 134 * @hide 135 */ 136 public void writeToParcel(Parcel dest, int flags) { 137 dest.writeString(getInterfaceName()); 138 dest.writeInt(mAddresses.size()); 139 //TODO: explore an easy alternative to preserve hostname 140 // without doing a lookup 141 for(InetAddress a : mAddresses) { 142 dest.writeByteArray(a.getAddress()); 143 } 144 dest.writeInt(mDnses.size()); 145 for(InetAddress d : mDnses) { 146 dest.writeByteArray(d.getAddress()); 147 } 148 if (mGateway != null) { 149 dest.writeByte((byte)1); 150 dest.writeByteArray(mGateway.getAddress()); 151 } else { 152 dest.writeByte((byte)0); 153 } 154 if (mHttpProxy != null) { 155 dest.writeByte((byte)1); 156 dest.writeParcelable(mHttpProxy, flags); 157 } else { 158 dest.writeByte((byte)0); 159 } 160 } 161 162 /** 163 * Implement the Parcelable interface. 164 * @hide 165 */ 166 public static final Creator<LinkProperties> CREATOR = 167 new Creator<LinkProperties>() { 168 public LinkProperties createFromParcel(Parcel in) { 169 LinkProperties netProp = new LinkProperties(); 170 String iface = in.readString(); 171 if (iface != null) { 172 try { 173 netProp.setInterface(NetworkInterface.getByName(iface)); 174 } catch (Exception e) { 175 return null; 176 } 177 } 178 int addressCount = in.readInt(); 179 for (int i=0; i<addressCount; i++) { 180 try { 181 netProp.addAddress(InetAddress.getByAddress(in.createByteArray())); 182 } catch (UnknownHostException e) { } 183 } 184 addressCount = in.readInt(); 185 for (int i=0; i<addressCount; i++) { 186 try { 187 netProp.addDns(InetAddress.getByAddress(in.createByteArray())); 188 } catch (UnknownHostException e) { } 189 } 190 if (in.readByte() == 1) { 191 try { 192 netProp.setGateway(InetAddress.getByAddress(in.createByteArray())); 193 } catch (UnknownHostException e) {} 194 } 195 if (in.readByte() == 1) { 196 netProp.setHttpProxy((ProxyProperties)in.readParcelable(null)); 197 } 198 return netProp; 199 } 200 201 public LinkProperties[] newArray(int size) { 202 return new LinkProperties[size]; 203 } 204 }; 205} 206