Author: sebb
Date: Thu Mar  2 22:34:35 2017
New Revision: 1785208

URL: http://svn.apache.org/viewvc?rev=1785208&view=rev
Log:
NET-619 SubnetUtils - improve binary netmask algorithm

This fixes #6

Modified:
    commons/proper/net/trunk/src/changes/changes.xml
    
commons/proper/net/trunk/src/main/java/org/apache/commons/net/util/SubnetUtils.java

Modified: commons/proper/net/trunk/src/changes/changes.xml
URL: 
http://svn.apache.org/viewvc/commons/proper/net/trunk/src/changes/changes.xml?rev=1785208&r1=1785207&r2=1785208&view=diff
==============================================================================
--- commons/proper/net/trunk/src/changes/changes.xml [utf-8] (original)
+++ commons/proper/net/trunk/src/changes/changes.xml [utf-8] Thu Mar  2 
22:34:35 2017
@@ -71,6 +71,9 @@ This is mainly a bug-fix release. See fu
  However it is not source compatible with releases before 3.4, as some methods 
were added to the interface NtpV3Packet in 3.4
         
 ">
+            <action issue="NET-619" type="fix" dev="sebb" due-to="Makoto 
Sakaguchi">
+            SubnetUtils - improve binary netmask algorithm
+            </action>
             <action issue="NET-613" type="fix" dev="sebb" due-to="Donald 
Kwakkel">
             System Information Leak in ftp parser
             </action>

Modified: 
commons/proper/net/trunk/src/main/java/org/apache/commons/net/util/SubnetUtils.java
URL: 
http://svn.apache.org/viewvc/commons/proper/net/trunk/src/main/java/org/apache/commons/net/util/SubnetUtils.java?rev=1785208&r1=1785207&r2=1785208&view=diff
==============================================================================
--- 
commons/proper/net/trunk/src/main/java/org/apache/commons/net/util/SubnetUtils.java
 (original)
+++ 
commons/proper/net/trunk/src/main/java/org/apache/commons/net/util/SubnetUtils.java
 Thu Mar  2 22:34:35 2017
@@ -261,10 +261,17 @@ public class SubnetUtils {
             address = matchAddress(matcher);
 
             /* Create a binary netmask from the number of bits specification 
/x */
-            int cidrPart = rangeCheck(Integer.parseInt(matcher.group(5)), 0, 
NBITS);
-            for (int j = 0; j < cidrPart; ++j) {
-                netmask |= (1 << 31 - j);
-            }
+
+            int trailingZeroes = NBITS - 
rangeCheck(Integer.parseInt(matcher.group(5)), 0, NBITS);
+            /*
+             * An IPv4 netmask consists of 32 bits, a contiguous sequence 
+             * of the specified number of ones followed by all zeros.
+             * So, it can be obtained by shifting an unsigned integer (32 
bits) to the left by
+             * the number of trailing zeros which is (32 - the # bits 
specification).
+             * Note that there is no unsigned left shift operator, so we have 
to use
+             * a long to ensure that the left-most bit is shifted out 
correctly.
+             */
+            netmask = (int) (0x0FFFFFFFFL << trailingZeroes );
 
             /* Calculate base network address */
             network = (address & netmask);


Reply via email to