Rename NetworkProperties to LinkProperties

Also add copy constructors and use them when giving out data.

Change-Id: Id320eb8fb91d0bd250305ce7bb4f628570215615
This commit is contained in:
Robert Greenwalt 2010-08-30 10:56:47 -07:00 • committed by Robert Greenwalt
commit 1f1bcfe6a2
3 changed files with 58 additions and 42 deletions

View file

@ -0,0 +1,203 @@
/*
* Copyright (C) 2008 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.ProxyProperties;
import android.os.Parcelable;
import android.os.Parcel;
import android.util.Log;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
/**
* Describes the properties of a network link.
* TODO - consider adding optional fields like Apn and ApnType
* @hide
*/
public class LinkProperties implements Parcelable {
private NetworkInterface mIface;
private Collection<InetAddress> mAddresses;
private Collection<InetAddress> mDnses;
private InetAddress mGateway;
private ProxyProperties mHttpProxy;
public LinkProperties() {
clear();
}
// copy constructor instead of clone
public LinkProperties(LinkProperties source) {
mIface = source.getInterface();
mAddresses = source.getAddresses();
mDnses = source.getDnses();
mGateway = source.getGateway();
mHttpProxy = new ProxyProperties(source.getHttpProxy());
}
public void setInterface(NetworkInterface iface) {
mIface = iface;
}
public NetworkInterface getInterface() {
return mIface;
}
public String getInterfaceName() {
return (mIface == null ? null : mIface.getName());
}
public void addAddress(InetAddress address) {
mAddresses.add(address);
}
public Collection<InetAddress> getAddresses() {
return Collections.unmodifiableCollection(mAddresses);
}
public void addDns(InetAddress dns) {
mDnses.add(dns);
}
public Collection<InetAddress> getDnses() {
return Collections.unmodifiableCollection(mDnses);
}
public void setGateway(InetAddress gateway) {
mGateway = gateway;
}
public InetAddress getGateway() {
return mGateway;
}
public void setHttpProxy(ProxyProperties proxy) {
mHttpProxy = proxy;
}
public ProxyProperties getHttpProxy() {
return mHttpProxy;
}
public void clear() {
mIface = null;
mAddresses = new ArrayList<InetAddress>();
mDnses = new ArrayList<InetAddress>();
mGateway = null;
mHttpProxy = null;
}
/**
* Implement the Parcelable interface
* @hide
*/
public int describeContents() {
return 0;
}
@Override
public String toString() {
String ifaceName = (mIface == null ? "" : "InterfaceName: " + mIface.getName() + " ");
String ip = "IpAddresses: [";
for (InetAddress addr : mAddresses) ip += addr.getHostAddress() + ",";
ip += "] ";
String dns = "DnsAddresses: [";
for (InetAddress addr : mDnses) dns += addr.getHostAddress() + ",";
dns += "] ";
String proxy = (mHttpProxy == null ? "" : "HttpProxy: " + mHttpProxy.toString() + " ");
String gateway = (mGateway == null ? "" : "Gateway: " + mGateway.getHostAddress() + " ");
return ifaceName + ip + gateway + dns + proxy;
}
/**
* Implement the Parcelable interface.
* @hide
*/
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(getInterfaceName());
dest.writeInt(mAddresses.size());
//TODO: explore an easy alternative to preserve hostname
// without doing a lookup
for(InetAddress a : mAddresses) {
dest.writeByteArray(a.getAddress());
}
dest.writeInt(mDnses.size());
for(InetAddress d : mDnses) {
dest.writeByteArray(d.getAddress());
}
if (mGateway != null) {
dest.writeByte((byte)1);
dest.writeByteArray(mGateway.getAddress());
} else {
dest.writeByte((byte)0);
}
if (mHttpProxy != null) {
dest.writeByte((byte)1);
dest.writeParcelable(mHttpProxy, flags);
} else {
dest.writeByte((byte)0);
}
}
/**
* Implement the Parcelable interface.
* @hide
*/
public static final Creator<LinkProperties> CREATOR =
new Creator<LinkProperties>() {
public LinkProperties createFromParcel(Parcel in) {
LinkProperties netProp = new LinkProperties();
String iface = in.readString();
if (iface != null) {
try {
netProp.setInterface(NetworkInterface.getByName(iface));
} catch (Exception e) {
return null;
}
}
int addressCount = in.readInt();
for (int i=0; i<addressCount; i++) {
try {
netProp.addAddress(InetAddress.getByAddress(in.createByteArray()));
} catch (UnknownHostException e) { }
}
addressCount = in.readInt();
for (int i=0; i<addressCount; i++) {
try {
netProp.addDns(InetAddress.getByAddress(in.createByteArray()));
} catch (UnknownHostException e) { }
}
if (in.readByte() == 1) {
try {
netProp.setGateway(InetAddress.getByAddress(in.createByteArray()));
} catch (UnknownHostException e) {}
}
if (in.readByte() == 1) {
netProp.setHttpProxy((ProxyProperties)in.readParcelable(null));
}
return netProp;
}
public LinkProperties[] newArray(int size) {
return new LinkProperties[size];
}
};
}

View file

@ -36,24 +36,31 @@ public class ProxyProperties implements Parcelable {
public ProxyProperties() {
}
public synchronized InetAddress getAddress() {
// copy constructor instead of clone
public ProxyProperties(ProxyProperties source) {
mProxy = source.getAddress();
mPort = source.getPort();
mExclusionList = new String(source.getExclusionList());
}
public InetAddress getAddress() {
return mProxy;
}
public synchronized void setAddress(InetAddress proxy) {
public void setAddress(InetAddress proxy) {
mProxy = proxy;
}
public synchronized int getPort() {
public int getPort() {
return mPort;
}
public synchronized void setPort(int port) {
public void setPort(int port) {
mPort = port;
}
public synchronized String getExclusionList() {
public String getExclusionList() {
return mExclusionList;
}
public synchronized void setExclusionList(String exclusionList) {
public void setExclusionList(String exclusionList) {
mExclusionList = exclusionList;
}
@ -77,7 +84,7 @@ public class ProxyProperties implements Parcelable {
* Implement the Parcelable interface.
* @hide
*/
public synchronized void writeToParcel(Parcel dest, int flags) {
public void writeToParcel(Parcel dest, int flags) {
if (mProxy != null) {
dest.writeByte((byte)1);
dest.writeString(mProxy.getHostName());

View file

@ -26,7 +26,7 @@ import android.net.ConnectivityManager;
import android.net.IConnectivityManager;
import android.net.MobileDataStateTracker;
import android.net.NetworkInfo;
import android.net.NetworkProperties;
import android.net.LinkProperties;
import android.net.NetworkStateTracker;
import android.net.NetworkUtils;
import android.net.wifi.WifiStateTracker;
@ -756,7 +756,6 @@ public class ConnectivityService extends IConnectivityManager.Stub {
* specified host is to be routed
* @param hostAddress the IP address of the host to which the route is
* desired
* todo - deprecate (only v4!)
* @return {@code true} on success, {@code false} on failure
*/
public boolean requestRouteToHost(int networkType, int hostAddress) {
@ -813,7 +812,7 @@ public class ConnectivityService extends IConnectivityManager.Stub {
return false;
}
NetworkProperties p = nt.getNetworkProperties();
LinkProperties p = nt.getLinkProperties();
if (p == null) return false;
String interfaceName = p.getInterfaceName();
@ -1258,7 +1257,7 @@ public class ConnectivityService extends IConnectivityManager.Stub {
private void addPrivateDnsRoutes(NetworkStateTracker nt) {
boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
NetworkProperties p = nt.getNetworkProperties();
LinkProperties p = nt.getLinkProperties();
if (p == null) return;
String interfaceName = p.getInterfaceName();
@ -1279,7 +1278,7 @@ public class ConnectivityService extends IConnectivityManager.Stub {
private void removePrivateDnsRoutes(NetworkStateTracker nt) {
// TODO - we should do this explicitly but the NetUtils api doesnt
// support this yet - must remove all. No worse than before
NetworkProperties p = nt.getNetworkProperties();
LinkProperties p = nt.getLinkProperties();
if (p == null) return;
String interfaceName = p.getInterfaceName();
boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
@ -1295,7 +1294,7 @@ public class ConnectivityService extends IConnectivityManager.Stub {
private void addDefaultRoute(NetworkStateTracker nt) {
NetworkProperties p = nt.getNetworkProperties();
LinkProperties p = nt.getLinkProperties();
if (p == null) return;
String interfaceName = p.getInterfaceName();
InetAddress defaultGatewayAddr = p.getGateway();
@ -1311,7 +1310,7 @@ public class ConnectivityService extends IConnectivityManager.Stub {
public void removeDefaultRoute(NetworkStateTracker nt) {
NetworkProperties p = nt.getNetworkProperties();
LinkProperties p = nt.getLinkProperties();
if (p == null) return;
String interfaceName = p.getInterfaceName();
@ -1410,7 +1409,7 @@ public class ConnectivityService extends IConnectivityManager.Stub {
NetworkStateTracker nt = mNetTrackers[i];
if (nt.getNetworkInfo().isConnected() &&
!nt.isTeardownRequested()) {
NetworkProperties p = nt.getNetworkProperties();
LinkProperties p = nt.getLinkProperties();
if (p == null) continue;
List pids = mNetRequestersPids[i];
for (int j=0; j<pids.size(); j++) {
@ -1465,7 +1464,7 @@ public class ConnectivityService extends IConnectivityManager.Stub {
// add default net's dns entries
NetworkStateTracker nt = mNetTrackers[netType];
if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
NetworkProperties p = nt.getNetworkProperties();
LinkProperties p = nt.getLinkProperties();
if (p == null) return;
Collection<InetAddress> dnses = p.getDnses();
if (mNetAttributes[netType].isDefault()) {