Use LinkProperties for IP and proxy configuration

Change-Id: I4ae817fb00141e9a742216b7fd02dca1ed228270
This commit is contained in:
Irfan Sheriff
2010-10-05 16:12:25 -07:00
parent fb7c96415b
commit e0b2c0fdc4
2 changed files with 67 additions and 16 deletions

View File

@@ -34,26 +34,26 @@ public class LinkAddress implements Parcelable {
private final InetAddress address;
/**
* Network prefix
* Network prefix length
*/
private final int prefix;
private final int prefixLength;
public LinkAddress(InetAddress address, InetAddress mask) {
this.address = address;
this.prefix = computeprefix(mask);
this.prefixLength = computeprefixLength(mask);
}
public LinkAddress(InetAddress address, int prefix) {
public LinkAddress(InetAddress address, int prefixLength) {
this.address = address;
this.prefix = prefix;
this.prefixLength = prefixLength;
}
public LinkAddress(InterfaceAddress interfaceAddress) {
this.address = interfaceAddress.getAddress();
this.prefix = interfaceAddress.getNetworkPrefixLength();
this.prefixLength = interfaceAddress.getNetworkPrefixLength();
}
private static int computeprefix(InetAddress mask) {
private static int computeprefixLength(InetAddress mask) {
int count = 0;
for (byte b : mask.getAddress()) {
for (int i = 0; i < 8; ++i) {
@@ -67,12 +67,12 @@ public class LinkAddress implements Parcelable {
@Override
public String toString() {
return (address == null ? "" : (address.getHostAddress() + "/" + prefix));
return (address == null ? "" : (address.getHostAddress() + "/" + prefixLength));
}
/**
* Compares this {@code LinkAddress} instance against the specified address
* in {@code obj}. Two addresses are equal if their InetAddress and prefix
* in {@code obj}. Two addresses are equal if their InetAddress and prefixLength
* are equal
*
* @param obj the object to be tested for equality.
@@ -85,7 +85,7 @@ public class LinkAddress implements Parcelable {
}
LinkAddress linkAddress = (LinkAddress) obj;
return this.address.equals(linkAddress.address) &&
this.prefix == linkAddress.prefix;
this.prefixLength == linkAddress.prefixLength;
}
/**
@@ -98,8 +98,8 @@ public class LinkAddress implements Parcelable {
/**
* Get network prefix length
*/
public int getNetworkPrefix() {
return prefix;
public int getNetworkPrefixLength() {
return prefixLength;
}
/**
@@ -118,7 +118,7 @@ public class LinkAddress implements Parcelable {
if (address != null) {
dest.writeByte((byte)1);
dest.writeByteArray(address.getAddress());
dest.writeInt(prefix);
dest.writeInt(prefixLength);
} else {
dest.writeByte((byte)0);
}
@@ -132,14 +132,14 @@ public class LinkAddress implements Parcelable {
new Creator<LinkAddress>() {
public LinkAddress createFromParcel(Parcel in) {
InetAddress address = null;
int prefix = 0;
int prefixLength = 0;
if (in.readByte() == 1) {
try {
address = InetAddress.getByAddress(in.createByteArray());
prefix = in.readInt();
prefixLength = in.readInt();
} catch (UnknownHostException e) { }
}
return new LinkAddress(address, prefix);
return new LinkAddress(address, prefixLength);
}
public LinkAddress[] newArray(int size) {

View File

@@ -145,6 +145,57 @@ public class NetworkUtils {
return inetAddress;
}
/**
* Convert a IPv4 address from an InetAddress to an integer
* @param inetAddr is an InetAddress corresponding to the IPv4 address
* @return the IP address as an integer in network byte order
*/
public static int inetAddressToInt(InetAddress inetAddr)
throws IllegalArgumentException {
byte [] addr = inetAddr.getAddress();
if (addr.length != 4) {
throw new IllegalArgumentException("Not an IPv4 address");
}
return ((addr[3] & 0xff) << 24) | ((addr[2] & 0xff) << 16) |
((addr[1] & 0xff) << 8) | (addr[0] & 0xff);
}
/**
* Convert a network prefix length to an IPv4 netmask integer
* @param prefixLength
* @return the IPv4 netmask as an integer in network byte order
*/
public static int prefixLengthToNetmaskInt(int prefixLength)
throws IllegalArgumentException {
if (prefixLength < 0 || prefixLength > 32) {
throw new IllegalArgumentException("Invalid prefix length (0 <= prefix <= 32)");
}
int value = 0xffffffff << (32 - prefixLength);
return Integer.reverseBytes(value);
}
public static boolean isIpAddress(String address) {
//TODO: Add NetworkUtils support for IPv6 configuration and
//remove IPv4 validation and use a generic InetAddress validation
try {
String[] parts = address.split("\\.");
if (parts.length != 4) {
return false;
}
int a = Integer.parseInt(parts[0]);
if (a < 0 || a > 255) return false;
a = Integer.parseInt(parts[1]);
if (a < 0 || a > 255) return false;
a = Integer.parseInt(parts[2]);
if (a < 0 || a > 255) return false;
a = Integer.parseInt(parts[3]);
if (a < 0 || a > 255) return false;
} catch (NumberFormatException ex) {
return false;
}
return true;
}
/**
* Add a default route through the specified gateway.
* @param interfaceName interface on which the route should be added