JDK-7107883 : getNetworkPrefixLength() does not return correct prefix length
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 6u27,7,8
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: linux,windows_7
  • CPU: x86
  • Submitted: 2011-11-03
  • Updated: 2014-01-23
  • Resolved: 2013-05-31
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8
8 b94Fixed
Related Reports
Duplicate :  
Duplicate :  
Description
In Ipv4 environment, the result of InterfaceAddress.getNetworkPrefixLength() for network interface
which does not have broadcast flag(ex. loopback address) is always 0.

Correct prefix length should be returns even in the above situation.

CONFIGURATION:
JDK: jdk7/jdk6u27
OS : linux(generic)

REPRODUCE:
1. Compile the attached test case
2. launch 
  "java GetBrodCastAddrAndPrefixLen <interface name>"
 
ex. 
---
$ java GetBrodCastAddrAndPrefixLen lo
<0:0:0:0:0:0:0:1%1>
getBroadcast():null
getNetworkPrefixLength():128
<127.0.0.1>
getBroadcast():null
getNetworkPrefixLength():0
---

The last line of result should not be 0.

Comments
diff --git a/src/solaris/native/java/net/NetworkInterface.c b/src/solaris/native/java/net/NetworkInterface.c --- a/src/solaris/native/java/net/NetworkInterface.c +++ b/src/solaris/native/java/net/NetworkInterface.c @@ -658,9 +658,9 @@ jobject createNetworkInterface(JNIEnv *e if (ia2Obj) { setInetAddress_addr(env, ia2Obj, htonl(((struct sockaddr_in*)addrP->brdcast)->sin_addr.s_addr)); (*env)->SetObjectField(env, ibObj, ni_ib4broadcastID, ia2Obj); - (*env)->SetShortField(env, ibObj, ni_ib4maskID, addrP->mask); } } + (*env)->SetShortField(env, ibObj, ni_ib4maskID, addrP->mask); (*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj); } } @@ -887,15 +887,12 @@ netif *addif(JNIEnv *env, int sock, cons addrP->mask = prefix; addrP->next = 0; if (family == AF_INET) { - /* - * Deal with broadcast addr & subnet mask - */ + // Deal with broadcast addr & subnet mask struct sockaddr * brdcast_to = (struct sockaddr *) ((char *) addrP + sizeof(netaddr) + addr_size); addrP->brdcast = getBroadcast(env, sock, name, brdcast_to ); - if (addrP->brdcast && (mask = getSubnet(env, sock, name)) != -1) { + if ((mask = getSubnet(env, sock, name)) != -1) addrP->mask = mask; - } }
2013-05-30

The unix native implementation that determines the network interface's broadcast address and network prefix/subnet mask, incorrectly ties the mask to the existence of a broadcast address. This should be changed so that the network prefix/subnet mask, if available, is exposed through the Java API, getNetworkPrefixLength.
2013-05-30