Skip to content

Commit

Permalink
Fix the InetAddress/Inet4Address/Inet6Address documentation.
Browse files Browse the repository at this point in the history
Also simplify a few of the implementations, and add a few tests.

Bug: http://code.google.com/p/android/issues/detail?id=19165
Change-Id: If33d5a830068114edf722711333bf0e00098d91a
  • Loading branch information
enh-google committed Sep 29, 2011
1 parent 23ea26d commit a7428d6
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 292 deletions.
159 changes: 30 additions & 129 deletions luni/src/main/java/java/net/Inet4Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,94 +52,20 @@ public final class Inet4Address extends InetAddress {
super(AF_INET, ipaddress, hostName);
}

/**
* Returns whether the represented address is a multicast address or not.
* Valid IPv4 multicast addresses are prefixed with 1110 = 0xE.
*
* @return {@code true} if this instance represents a multicast address,
* {@code false} otherwise.
*/
@Override
public boolean isMulticastAddress() {
return (ipaddress[0] & 0xF0) == 0xE0;
}

/**
* Returns whether the represented address is the local wildcard ANY address
* or not.
*
* @return {@code true} if this instance represents the wildcard ANY
* address, {@code false} otherwise.
*/
@Override
public boolean isAnyLocalAddress() {
for (int i = 0; i < ipaddress.length; i++) {
if (ipaddress[i] != 0) {
return false;
}
}
return true;
}

/**
* Returns whether the represented address is a loopback address or not.
* Loopback IPv4 addresses are prefixed with: 011111111 = 127.
*
* @return {@code true} if this instance represents a lookback address,
* {@code false} otherwise.
*/
@Override
public boolean isLoopbackAddress() {
return (ipaddress[0] & 255) == 127;
@Override public boolean isAnyLocalAddress() {
return ipaddress[0] == 0 && ipaddress[1] == 0 && ipaddress[2] == 0 && ipaddress[3] == 0; // 0.0.0.0
}

/**
* Returns whether this address has a link-local scope or not.
*
* <p><a href="http://www.ietf.org/rfc/rfc3484.txt">RFC 3484</a>
* Default Address Selection for Internet Protocol Version 6 (IPv6) states
* IPv4 auto-configuration addresses, prefix 169.254/16, IPv4 loopback
* addresses, prefix 127/8, are assigned link-local scope.
*
* @return {@code true} if this instance represents a link-local address,
* {@code false} otherwise.
*/
@Override
public boolean isLinkLocalAddress() {
// The reference implementation does not return true for loopback
// addresses even though RFC 3484 says to do so
return (((ipaddress[0] & 255) == 169) && ((ipaddress[1] & 255) == 254));
@Override public boolean isLinkLocalAddress() {
// The RI does not return true for loopback addresses even though RFC 3484 says to do so.
return ((ipaddress[0] & 0xff) == 169) && ((ipaddress[1] & 0xff) == 254); // 169.254/16
}

/**
* Returns whether this address has a site-local scope or not.
*
* <p><a href="http://www.ietf.org/rfc/rfc3484.txt">RFC 3484</a>
* Default Address Selection for Internet Protocol Version 6 (IPv6) states
* IPv4 private addresses, prefixes 10/8, 172.16/12, and 192.168/16, are
* assigned site-local scope.
*
* @return {@code true} if this instance represents a site-local address,
* {@code false} otherwise.
*/
@Override
public boolean isSiteLocalAddress() {
return ((ipaddress[0] & 255) == 10) || ((ipaddress[0] & 255) == 172)
&& (((ipaddress[1] & 255) > 15) && (ipaddress[1] & 255) < 32)
|| ((ipaddress[0] & 255) == 192)
&& ((ipaddress[1] & 255) == 168);
@Override public boolean isLoopbackAddress() {
return ((ipaddress[0] & 0xff) == 127); // 127/8
}

/**
* Returns whether the address is a global multicast address or not. Valid
* MCGlobal IPv4 addresses are 224.0.1.0 - 238.255.255.255.
*
* @return {@code true} if the address is in the global multicast group,
* {@code false} otherwise.
*/
@Override
public boolean isMCGlobal() {

@Override public boolean isMCGlobal() {
// Check if we have a prefix of 1110
if (!isMulticastAddress()) {
return false;
Expand Down Expand Up @@ -168,60 +94,35 @@ public boolean isMCGlobal() {
return true;
}

/**
* Returns whether the address has a node-local scope or not. This method
* returns always {@code false} because there are no valid IPv4 node-local
* addresses.
*
* @return {@code false} for all IPv4 addresses.
*/
@Override
public boolean isMCNodeLocal() {
@Override public boolean isMCLinkLocal() {
return ((ipaddress[0] & 0xff) == 224) && (ipaddress[1] == 0) && (ipaddress[2] == 0); // 224.0.0/24
}

@Override public boolean isMCNodeLocal() {
return false;
}

/**
* Returns whether the address is a link-local multicast address or not. The
* valid range for IPv4 link-local addresses is: 224.0.0.0 to 239.0.0.255
* Hence a mask of 111000000000000000000000 = 0xE00000.
*
* @return {@code true} if this instance represents a link-local address,
* {@code false} otherwise.
*/
@Override
public boolean isMCLinkLocal() {
int address = Memory.peekInt(ipaddress, 0, ByteOrder.BIG_ENDIAN);
return (address >>> 8) == 0xE00000;
@Override public boolean isMCOrgLocal() {
return ((ipaddress[0] & 0xff) == 239) && ((ipaddress[1] & 0xfc) == 192); // 239.192/14
}

/**
* Returns whether the address is a site-local multicast address or not. The
* valid range for IPv4 site-local addresses is: 239.255.0.0 to
* 239.255.255.255 Hence a mask of 11101111 11111111 = 0xEFFF.
*
* @return {@code true} if this instance represents a site-local address,
* {@code false} otherwise.
*/
@Override
public boolean isMCSiteLocal() {
int address = Memory.peekInt(ipaddress, 0, ByteOrder.BIG_ENDIAN);
return (address >>> 16) == 0xEFFF;
@Override public boolean isMCSiteLocal() {
return ((ipaddress[0] & 0xff) == 239) && ((ipaddress[1] & 0xff) == 255); // 239.255/16
}

/**
* Returns whether the address is a organization-local multicast address or
* not. The valid range for IPv4 organization-local addresses is:
* 239.192.0.0 to 239.195.255.255 Hence masks of 11101111 11000000 to
* 11101111 11000011 are valid. 0xEFC0 to 0xEFC3
*
* @return {@code true} if this instance represents a organization-local
* address, {@code false} otherwise.
*/
@Override
public boolean isMCOrgLocal() {
int address = Memory.peekInt(ipaddress, 0, ByteOrder.BIG_ENDIAN);
int prefix = address >>> 16;
return prefix >= 0xEFC0 && prefix <= 0xEFC3;
@Override public boolean isMulticastAddress() {
return (ipaddress[0] & 0xf0) == 224; // 224/4
}

@Override public boolean isSiteLocalAddress() {
if ((ipaddress[0] & 0xff) == 10) { // 10/8
return true;
} else if (((ipaddress[0] & 0xff) == 172) && ((ipaddress[1] & 0xf0) == 16)) { // 172.16/12
return true;
} else if (((ipaddress[0] & 0xff) == 192) && ((ipaddress[1] & 0xff) == 168)) { // 192.168/16
return true;
}
return false;
}

private Object writeReplace() throws ObjectStreamException {
Expand Down
100 changes: 28 additions & 72 deletions luni/src/main/java/java/net/Inet6Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,90 +169,58 @@ private boolean compareLocalType(Inet6Address ia) {
return false;
}

/**
* Returns whether this address is an IP multicast address or not.
*/
@Override public boolean isMulticastAddress() {
// Multicast addresses are prefixed with 11111111 (255)
return ipaddress[0] == -1;
}

/**
* Returns true if this address is the unspecified wildcard address "::".
*/
@Override public boolean isAnyLocalAddress() {
return Arrays.equals(ipaddress, Inet6Address.ANY.ipaddress);
}

/**
* Returns whether this address is the loopback address or not. The only
* valid IPv6 loopback address is "::1".
* Returns whether this IPv6 address is an IPv4-compatible address or not.
* An IPv4-compatible address has the prefix {@code ::/96} and is a deprecated
* and no-longer used equivalent of the modern IPv4-mapped IPv6 addresses.
*/
@Override public boolean isLoopbackAddress() {
return Arrays.equals(ipaddress, Inet6Address.LOOPBACK.ipaddress);
public boolean isIPv4CompatibleAddress() {
for (int i = 0; i < 12; i++) {
if (ipaddress[i] != 0) {
return false;
}
}
return true;
}

/**
* Returns whether this address is a link-local address or not.
*/
@Override public boolean isLinkLocalAddress() {
// the first 10 bits need to be 1111111010 (1018)
return (ipaddress[0] == -2) && ((ipaddress[1] & 255) >>> 6) == 2;
return ((ipaddress[0] & 0xff) == 0xfe) && ((ipaddress[1] & 0xc0) == 0x80); // fe80:/10
}

/**
* Returns whether this address is a site-local address or not.
*/
@Override public boolean isSiteLocalAddress() {
// the first 10 bits need to be 1111111011 (1019)
return (ipaddress[0] == -2) && ((ipaddress[1] & 255) >>> 6) == 3;
@Override public boolean isLoopbackAddress() {
return Arrays.equals(ipaddress, Inet6Address.LOOPBACK.ipaddress);
}

/**
* Returns whether this address is a global multicast address or not. A
* valid IPv6 global multicast address is 11111111xxxx1110 or FF0E hex.
*/
@Override public boolean isMCGlobal() {
// the first byte should be 0xFF and the lower 4 bits
// of the second byte should be 0xE
return (ipaddress[0] == -1) && (ipaddress[1] & 15) == 14;
return ((ipaddress[0] & 0xff) == 0xff) && ((ipaddress[1] & 0x0f) == 0x0e); // ffxe:/16
}

@Override public boolean isMCLinkLocal() {
return ((ipaddress[0] & 0xff) == 0xff) && ((ipaddress[1] & 0x0f) == 0x02); // ffx2:/16
}

/**
* Returns whether this address is a node-local multicast address or not.
*/
@Override public boolean isMCNodeLocal() {
// the first byte should be 0xFF and the lower 4 bits
// of the second byte should be 0x1
return (ipaddress[0] == -1) && (ipaddress[1] & 15) == 1;
return ((ipaddress[0] & 0xff) == 0xff) && ((ipaddress[1] & 0x0f) == 0x01); // ffx1:/16
}

/**
* Returns whether this address is a link-local multicast address or not.
*/
@Override public boolean isMCLinkLocal() {
// the first byte should be 0xFF and the lower 4 bits
// of the second byte should be 0x2
return (ipaddress[0] == -1) && (ipaddress[1] & 15) == 2;
@Override public boolean isMCOrgLocal() {
return ((ipaddress[0] & 0xff) == 0xff) && ((ipaddress[1] & 0x0f) == 0x08); // ffx8:/16
}

/**
* Returns whether this address is a site-local multicast address or not.
*/
@Override public boolean isMCSiteLocal() {
// the first byte should be 0xFF and the lower 4 bits
// of the second byte should be 0x5
return (ipaddress[0] == -1) && (ipaddress[1] & 15) == 5;
return ((ipaddress[0] & 0xff) == 0xff) && ((ipaddress[1] & 0x0f) == 0x05); // ffx5:/16
}

/**
* Returns whether this address is a organization-local multicast address or
* not.
*/
@Override public boolean isMCOrgLocal() {
// the first byte should be 0xFF and the lower 4 bits
// of the second byte should be 0x8
return (ipaddress[0] == -1) && (ipaddress[1] & 15) == 8;
@Override public boolean isMulticastAddress() {
return ((ipaddress[0] & 0xff) == 0xff); // ff::/8
}

@Override public boolean isSiteLocalAddress() {
return ((ipaddress[0] & 0xff) == 0xfe) && ((ipaddress[1] & 0xc0) == 0xc0); // fec0:/10
}

/**
Expand All @@ -274,18 +242,6 @@ public NetworkInterface getScopedInterface() {
}
}

/**
* Returns whether this address is an IPv4-compatible address or not.
*/
public boolean isIPv4CompatibleAddress() {
for (int i = 0; i < 12; i++) {
if (ipaddress[i] != 0) {
return false;
}
}
return true;
}

private static final ObjectStreamField[] serialPersistentFields = {
new ObjectStreamField("ipaddress", byte[].class),
new ObjectStreamField("scope_id", int.class),
Expand Down
Loading

0 comments on commit a7428d6

Please sign in to comment.