Merge "Pass network properties to ConnectivityService."

This commit is contained in:
Robert Greenwalt
2010-07-14 09:09:31 -07:00
committed by Android (Google) Code Review
18 changed files with 647 additions and 146 deletions

View File

@@ -29,6 +29,7 @@ import com.android.internal.telephony.Phone;
import com.android.internal.telephony.TelephonyIntents;
import android.net.NetworkInfo.DetailedState;
import android.net.NetworkInfo;
import android.net.NetworkProperties;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.text.TextUtils;
@@ -55,7 +56,7 @@ public class MobileDataStateTracker implements NetworkStateTracker {
private boolean mTeardownRequested = false;
private Handler mTarget;
private Context mContext;
private String mInterfaceName;
private NetworkProperties mNetworkProperties;
private boolean mPrivateDnsRouteSet = false;
private int mDefaultGatewayAddr = 0;
private boolean mDefaultRouteSet = false;
@@ -101,14 +102,6 @@ public class MobileDataStateTracker implements NetworkStateTracker {
return sDnsPropNames;
}
/**
* Return the name of our network interface.
* @return the name of our interface.
*/
public String getInterfaceName() {
return mInterfaceName;
}
public boolean isPrivateDnsRouteSet() {
return mPrivateDnsRouteSet;
}
@@ -211,9 +204,11 @@ public class MobileDataStateTracker implements NetworkStateTracker {
}
setDetailedState(DetailedState.DISCONNECTED, reason, apnName);
if (mInterfaceName != null) {
NetworkUtils.resetConnections(mInterfaceName);
if (mNetworkProperties != null) {
NetworkUtils.resetConnections(mNetworkProperties.getInterface().
getName());
}
// TODO - check this
// can't do this here - ConnectivityService needs it to clear stuff
// it's ok though - just leave it to be refreshed next time
// we connect.
@@ -229,9 +224,11 @@ public class MobileDataStateTracker implements NetworkStateTracker {
setDetailedState(DetailedState.SUSPENDED, reason, apnName);
break;
case CONNECTED:
mInterfaceName = intent.getStringExtra(Phone.DATA_IFACE_NAME_KEY);
if (mInterfaceName == null) {
Log.d(TAG, "CONNECTED event did not supply interface name.");
mNetworkProperties = intent.getParcelableExtra(
Phone.DATA_NETWORK_PROPERTIES_KEY);
if (mNetworkProperties == null) {
Log.d(TAG,
"CONNECTED event did not supply network properties.");
}
setDetailedState(DetailedState.CONNECTED, reason, apnName);
break;
@@ -565,4 +562,8 @@ public class MobileDataStateTracker implements NetworkStateTracker {
return null;
}
}
public NetworkProperties getNetworkProperties() {
return mNetworkProperties;
}
}

View File

@@ -0,0 +1,22 @@
/*
**
** Copyright (C) 2009 Qualcomm Innovation Center, Inc. All Rights Reserved.
** Copyright (C) 2009 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;
parcelable NetworkProperties;

View File

@@ -0,0 +1,196 @@
/*
* 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.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;
/**
* Describes the properties of a network interface or single address
* of an interface.
* TODO - consider adding optional fields like Apn and ApnType
* @hide
*/
public class NetworkProperties implements Parcelable {
private NetworkInterface mIface;
private Collection<InetAddress> mAddresses;
private Collection<InetAddress> mDnses;
private InetAddress mGateway;
private ProxyProperties mHttpProxy;
public NetworkProperties() {
clear();
}
public synchronized void setInterface(NetworkInterface iface) {
mIface = iface;
}
public synchronized NetworkInterface getInterface() {
return mIface;
}
public synchronized String getInterfaceName() {
return (mIface == null ? null : mIface.getName());
}
public synchronized void addAddress(InetAddress address) {
mAddresses.add(address);
}
public synchronized Collection<InetAddress> getAddresses() {
return mAddresses;
}
public synchronized void addDns(InetAddress dns) {
mDnses.add(dns);
}
public synchronized Collection<InetAddress> getDnses() {
return mDnses;
}
public synchronized void setGateway(InetAddress gateway) {
mGateway = gateway;
}
public synchronized InetAddress getGateway() {
return mGateway;
}
public synchronized void setHttpProxy(ProxyProperties proxy) {
mHttpProxy = proxy;
}
public synchronized ProxyProperties getHttpProxy() {
return mHttpProxy;
}
public synchronized 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;
}
public synchronized String toString() {
String ifaceName = (mIface == null ? "" : "InterfaceName: " + mIface.getName() + " ");
String ip = "IpAddresses: [";
for (InetAddress addr : mAddresses) ip += addr.toString() + ",";
ip += "] ";
String dns = "DnsAddresses: [";
for (InetAddress addr : mDnses) dns += addr.toString() + ",";
dns += "] ";
String proxy = (mHttpProxy == null ? "" : "HttpProxy: " + mHttpProxy.toString() + " ");
String gateway = (mGateway == null ? "" : "Gateway: " + mGateway.toString() + " ");
return ifaceName + ip + gateway + dns + proxy;
}
/**
* Implement the Parcelable interface.
* @hide
*/
public synchronized void writeToParcel(Parcel dest, int flags) {
dest.writeString(getInterfaceName());
dest.writeInt(mAddresses.size());
for(InetAddress a : mAddresses) {
dest.writeString(a.getHostName());
dest.writeByteArray(a.getAddress());
}
dest.writeInt(mDnses.size());
for(InetAddress d : mDnses) {
dest.writeString(d.getHostName());
dest.writeByteArray(d.getAddress());
}
if (mGateway != null) {
dest.writeByte((byte)1);
dest.writeString(mGateway.getHostName());
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<NetworkProperties> CREATOR =
new Creator<NetworkProperties>() {
public NetworkProperties createFromParcel(Parcel in) {
NetworkProperties netProp = new NetworkProperties();
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.readString(),
in.createByteArray()));
} catch (UnknownHostException e) { }
}
addressCount = in.readInt();
for (int i=0; i<addressCount; i++) {
try {
netProp.addDns(InetAddress.getByAddress(in.readString(),
in.createByteArray()));
} catch (UnknownHostException e) { }
}
if (in.readByte() == 1) {
try {
netProp.setGateway(InetAddress.getByAddress(in.readString(),
in.createByteArray()));
} catch (UnknownHostException e) {}
}
if (in.readByte() == 1) {
netProp.setHttpProxy((ProxyProperties)in.readParcelable(null));
}
return netProp;
}
public NetworkProperties[] newArray(int size) {
return new NetworkProperties[size];
}
};
}

View File

@@ -45,22 +45,17 @@ public interface NetworkStateTracker {
*/
public NetworkInfo getNetworkInfo();
/**
* Fetch NetworkProperties for the network
*/
public NetworkProperties getNetworkProperties();
/**
* Return the system properties name associated with the tcp buffer sizes
* for this network.
*/
public String getTcpBufferSizesPropName();
/**
* Return the DNS property names for this network.
*/
public String[] getDnsPropNames();
/**
* Fetch interface name of the interface
*/
public String getInterfaceName();
/**
* Check if private DNS route is set for the network
*/

View File

@@ -32,13 +32,37 @@ public class NetworkUtils {
public native static int disableInterface(String interfaceName);
/** Add a route to the specified host via the named interface. */
public native static int addHostRoute(String interfaceName, int hostaddr);
public static int addHostRoute(String interfaceName, InetAddress hostaddr) {
int v4Int = v4StringToInt(hostaddr.getHostAddress());
if (v4Int != 0) {
return addHostRouteNative(interfaceName, v4Int);
} else {
return -1;
}
}
private native static int addHostRouteNative(String interfaceName, int hostaddr);
/** Add a default route for the named interface. */
public native static int setDefaultRoute(String interfaceName, int gwayAddr);
public static int setDefaultRoute(String interfaceName, InetAddress gwayAddr) {
int v4Int = v4StringToInt(gwayAddr.getHostAddress());
if (v4Int != 0) {
return setDefaultRouteNative(interfaceName, v4Int);
} else {
return -1;
}
}
private native static int setDefaultRouteNative(String interfaceName, int hostaddr);
/** Return the gateway address for the default route for the named interface. */
public native static int getDefaultRoute(String interfaceName);
public static InetAddress getDefaultRoute(String interfaceName) {
int addr = getDefaultRouteNative(interfaceName);
try {
return InetAddress.getByAddress(v4IntToArray(addr));
} catch (UnknownHostException e) {
return null;
}
}
private native static int getDefaultRouteNative(String interfaceName);
/** Remove host routes that uses the named interface. */
public native static int removeHostRoutes(String interfaceName);
@@ -105,27 +129,30 @@ public class NetworkUtils {
private native static boolean configureNative(
String interfaceName, int ipAddress, int netmask, int gateway, int dns1, int dns2);
/**
* Look up a host name and return the result as an int. Works if the argument
* is an IP address in dot notation. Obviously, this can only be used for IPv4
* addresses.
* @param hostname the name of the host (or the IP address)
* @return the IP address as an {@code int} in network byte order
*/
public static int lookupHost(String hostname) {
InetAddress inetAddress;
// The following two functions are glue to tie the old int-based address scheme
// to the new InetAddress scheme. They should go away when we go fully to InetAddress
// TODO - remove when we switch fully to InetAddress
public static byte[] v4IntToArray(int addr) {
byte[] addrBytes = new byte[4];
addrBytes[0] = (byte)(addr & 0xff);
addrBytes[1] = (byte)((addr >> 8) & 0xff);
addrBytes[2] = (byte)((addr >> 16) & 0xff);
addrBytes[3] = (byte)((addr >> 24) & 0xff);
return addrBytes;
}
public static int v4StringToInt(String str) {
int result = 0;
String[] array = str.split("\\.");
if (array.length != 4) return 0;
try {
inetAddress = InetAddress.getByName(hostname);
} catch (UnknownHostException e) {
return -1;
result = Integer.parseInt(array[3]);
result = (result << 8) + Integer.parseInt(array[2]);
result = (result << 8) + Integer.parseInt(array[1]);
result = (result << 8) + Integer.parseInt(array[0]);
} catch (NumberFormatException e) {
return 0;
}
byte[] addrBytes;
int addr;
addrBytes = inetAddress.getAddress();
addr = ((addrBytes[3] & 0xff) << 24)
| ((addrBytes[2] & 0xff) << 16)
| ((addrBytes[1] & 0xff) << 8)
| (addrBytes[0] & 0xff);
return addr;
return result;
}
}

View File

@@ -0,0 +1,108 @@
/*
* Copyright (C) 2007 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.os.Parcel;
import android.os.Parcelable;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* A container class for the http proxy info
* @hide
*/
public class ProxyProperties implements Parcelable {
private InetAddress mProxy;
private int mPort;
private String mExclusionList;
public ProxyProperties() {
}
public synchronized InetAddress getAddress() {
return mProxy;
}
public synchronized void setAddress(InetAddress proxy) {
mProxy = proxy;
}
public synchronized int getPort() {
return mPort;
}
public synchronized void setPort(int port) {
mPort = port;
}
public synchronized String getExclusionList() {
return mExclusionList;
}
public synchronized void setExclusionList(String exclusionList) {
mExclusionList = exclusionList;
}
/**
* Implement the Parcelable interface
* @hide
*/
public int describeContents() {
return 0;
}
/**
* Implement the Parcelable interface.
* @hide
*/
public synchronized void writeToParcel(Parcel dest, int flags) {
if (mProxy != null) {
dest.writeByte((byte)1);
dest.writeString(mProxy.getHostName());
dest.writeByteArray(mProxy.getAddress());
} else {
dest.writeByte((byte)0);
}
dest.writeInt(mPort);
dest.writeString(mExclusionList);
}
/**
* Implement the Parcelable interface.
* @hide
*/
public static final Creator<ProxyProperties> CREATOR =
new Creator<ProxyProperties>() {
public ProxyProperties createFromParcel(Parcel in) {
ProxyProperties proxyProperties = new ProxyProperties();
if (in.readByte() == 1) {
try {
proxyProperties.setAddress(InetAddress.getByAddress(in.readString(),
in.createByteArray()));
} catch (UnknownHostException e) {}
}
proxyProperties.setPort(in.readInt());
proxyProperties.setExclusionList(in.readString());
return proxyProperties;
}
public ProxyProperties[] newArray(int size) {
return new ProxyProperties[size];
}
};
};