commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rwins...@apache.org
Subject svn commit: r920235 - in /commons/proper/net/branches/NET_2_0/src: main/java/org/apache/commons/net/util/SubnetUtils.java test/java/org/apache/commons/net/SubnetUtilsTest.java
Date Mon, 08 Mar 2010 08:53:34 GMT
Author: rwinston
Date: Mon Mar  8 08:53:34 2010
New Revision: 920235

URL: http://svn.apache.org/viewvc?rev=920235&view=rev
Log:
NET-305 fix

Modified:
    commons/proper/net/branches/NET_2_0/src/main/java/org/apache/commons/net/util/SubnetUtils.java
    commons/proper/net/branches/NET_2_0/src/test/java/org/apache/commons/net/SubnetUtilsTest.java

Modified: commons/proper/net/branches/NET_2_0/src/main/java/org/apache/commons/net/util/SubnetUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/net/branches/NET_2_0/src/main/java/org/apache/commons/net/util/SubnetUtils.java?rev=920235&r1=920234&r2=920235&view=diff
==============================================================================
--- commons/proper/net/branches/NET_2_0/src/main/java/org/apache/commons/net/util/SubnetUtils.java
(original)
+++ commons/proper/net/branches/NET_2_0/src/main/java/org/apache/commons/net/util/SubnetUtils.java
Mon Mar  8 08:53:34 2010
@@ -38,6 +38,10 @@
     private int network = 0;
     private int broadcast = 0;
 
+    /** Whether the broadcast/network address are included in host count */
+    private boolean inclusiveHostCount = false;
+    
+    
     /**
      * Constructor that takes a CIDR-notation string, e.g. "192.168.0.1/16"
      * @param cidrNotation A CIDR-notation string, e.g. "192.168.0.1/16"
@@ -54,8 +58,29 @@
     public SubnetUtils(String address, String mask) {
         calculate(toCidrNotation(address, mask));
     }
+    
+    
+    /**
+     * Returns <code>true</code> if the return value of {@link SubnetInfo#getAddressCount()}
+     * includes the network address and broadcast addresses.
+     * @return
+     */
+    public boolean isInclusiveHostCount() {
+		return inclusiveHostCount;
+	}
 
     /**
+     * Set to <code>true</code> if you want the return value of {@link SubnetInfo#getAddressCount()}
+     * to include the network and broadcast addresses.
+     * @param inclusiveHostCount
+     */
+	public void setInclusiveHostCount(boolean inclusiveHostCount) {
+		this.inclusiveHostCount = inclusiveHostCount;
+	}
+
+
+
+	/**
      * Convenience container for subnet summary information.
      *
      */
@@ -66,8 +91,8 @@
         private int network()       { return network; }
         private int address()       { return address; }
         private int broadcast()     { return broadcast; }
-        private int low()           { return network() + 1; }
-        private int high()          { return broadcast() - 1; }
+        private int low()           { return network() + (isInclusiveHostCount() ? 0 : 1);
}
+        private int high()          { return broadcast() - (isInclusiveHostCount() ? 0 :
1); }
 
         /**
          * Returns true if the parameter <code>address</code> is in the 
@@ -89,7 +114,7 @@
         public String getAddress()                  { return format(toArray(address()));
}
         public String getLowAddress()               { return format(toArray(low())); }
         public String getHighAddress()              { return format(toArray(high())); }
-        public int getAddressCount()                { return (broadcast() - low()); }
+        public int getAddressCount()                { return (broadcast() - low() + (isInclusiveHostCount()
? 1 : 0)); }
 
         public int asInteger(String address)        { return toInteger(address); }
         
@@ -138,7 +163,7 @@
             address = matchAddress(matcher);
 
             /* Create a binary netmask from the number of bits specification /x */
-            int cidrPart = rangeCheck(Integer.parseInt(matcher.group(5)), -1, NBITS-1);
+            int cidrPart = rangeCheck(Integer.parseInt(matcher.group(5)), -1, NBITS);
             for (int j = 0; j < cidrPart; ++j) {
                 netmask |= (1 << 31-j);
             }

Modified: commons/proper/net/branches/NET_2_0/src/test/java/org/apache/commons/net/SubnetUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/net/branches/NET_2_0/src/test/java/org/apache/commons/net/SubnetUtilsTest.java?rev=920235&r1=920234&r2=920235&view=diff
==============================================================================
--- commons/proper/net/branches/NET_2_0/src/test/java/org/apache/commons/net/SubnetUtilsTest.java
(original)
+++ commons/proper/net/branches/NET_2_0/src/test/java/org/apache/commons/net/SubnetUtilsTest.java
Mon Mar  8 08:53:34 2010
@@ -68,4 +68,158 @@
     	catch (IllegalArgumentException expected) {
     	}
     }
+    
+    public void testCidrAddresses() {
+    	SubnetUtils utils = new SubnetUtils("192.168.0.1/8");
+    	utils.setInclusiveHostCount(true);
+    	SubnetInfo info = utils.getInfo();
+    	assertEquals("255.0.0.0",info.getNetmask());
+    	assertEquals(16777216, info.getAddressCount());
+
+    	utils = new SubnetUtils("192.168.0.1/9");
+    	utils.setInclusiveHostCount(true);
+    	info = utils.getInfo();
+    	assertEquals("255.128.0.0",info.getNetmask());
+    	assertEquals(8388608, info.getAddressCount());
+
+    	utils = new SubnetUtils("192.168.0.1/10");
+    	utils.setInclusiveHostCount(true);
+    	info = utils.getInfo();
+    	assertEquals("255.192.0.0",info.getNetmask());
+    	assertEquals(4194304, info.getAddressCount());
+
+    	utils = new SubnetUtils("192.168.0.1/11");
+    	utils.setInclusiveHostCount(true);
+    	info = utils.getInfo();
+    	assertEquals("255.224.0.0",info.getNetmask());
+    	assertEquals(2097152, info.getAddressCount());
+
+    	utils = new SubnetUtils("192.168.0.1/12");
+    	utils.setInclusiveHostCount(true);
+    	info = utils.getInfo();
+    	assertEquals("255.240.0.0",info.getNetmask());
+    	assertEquals(1048576, info.getAddressCount());
+
+    	utils = new SubnetUtils("192.168.0.1/13");
+    	utils.setInclusiveHostCount(true);
+    	info = utils.getInfo();
+    	assertEquals("255.248.0.0",info.getNetmask());
+    	assertEquals(524288, info.getAddressCount());
+
+    	utils = new SubnetUtils("192.168.0.1/14");
+    	utils.setInclusiveHostCount(true);
+    	info = utils.getInfo();
+    	assertEquals("255.252.0.0",info.getNetmask());
+    	assertEquals(262144, info.getAddressCount());
+
+    	utils = new SubnetUtils("192.168.0.1/15");
+    	utils.setInclusiveHostCount(true);
+    	info = utils.getInfo();
+    	assertEquals("255.254.0.0",info.getNetmask());
+    	assertEquals(131072, info.getAddressCount());
+
+    	utils = new SubnetUtils("192.168.0.1/16");
+    	utils.setInclusiveHostCount(true);
+    	info = utils.getInfo();
+    	assertEquals("255.255.0.0",info.getNetmask());
+    	assertEquals(65536, info.getAddressCount());
+
+    	utils = new SubnetUtils("192.168.0.1/17");
+    	utils.setInclusiveHostCount(true);
+    	info = utils.getInfo();
+    	assertEquals("255.255.128.0",info.getNetmask());
+    	assertEquals(32768, info.getAddressCount());
+
+    	utils = new SubnetUtils("192.168.0.1/18");
+    	utils.setInclusiveHostCount(true);
+    	info = utils.getInfo();
+    	assertEquals("255.255.192.0",info.getNetmask());
+    	assertEquals(16384, info.getAddressCount());
+
+    	utils = new SubnetUtils("192.168.0.1/19");
+    	utils.setInclusiveHostCount(true);
+    	info = utils.getInfo();
+    	assertEquals("255.255.224.0",info.getNetmask());
+    	assertEquals(8192, info.getAddressCount());
+
+    	utils = new SubnetUtils("192.168.0.1/20");
+    	utils.setInclusiveHostCount(true);
+    	info = utils.getInfo();
+    	assertEquals("255.255.240.0",info.getNetmask());
+    	assertEquals(4096, info.getAddressCount());
+
+    	utils = new SubnetUtils("192.168.0.1/21");
+    	utils.setInclusiveHostCount(true);
+    	info = utils.getInfo();
+    	assertEquals("255.255.248.0",info.getNetmask());
+    	assertEquals(2048, info.getAddressCount());
+
+    	utils = new SubnetUtils("192.168.0.1/22");
+    	utils.setInclusiveHostCount(true);
+    	info = utils.getInfo();
+    	assertEquals("255.255.252.0",info.getNetmask());
+    	assertEquals(1024, info.getAddressCount());
+
+    	utils = new SubnetUtils("192.168.0.1/23");
+    	utils.setInclusiveHostCount(true);
+    	info = utils.getInfo();
+    	assertEquals("255.255.254.0",info.getNetmask());
+    	assertEquals(512, info.getAddressCount());
+
+    	utils = new SubnetUtils("192.168.0.1/24");
+    	utils.setInclusiveHostCount(true);
+    	info = utils.getInfo();
+    	assertEquals("255.255.255.0",info.getNetmask());
+    	assertEquals(256, info.getAddressCount());
+
+    	utils = new SubnetUtils("192.168.0.1/25");
+    	utils.setInclusiveHostCount(true);
+    	info = utils.getInfo();
+    	assertEquals("255.255.255.128",info.getNetmask());
+    	assertEquals(128, info.getAddressCount());
+
+    	utils = new SubnetUtils("192.168.0.1/26");
+    	utils.setInclusiveHostCount(true);
+    	info = utils.getInfo();
+    	assertEquals("255.255.255.192",info.getNetmask());
+    	assertEquals(64, info.getAddressCount());
+
+    	utils = new SubnetUtils("192.168.0.1/27");
+    	utils.setInclusiveHostCount(true);
+    	info = utils.getInfo();
+    	assertEquals("255.255.255.224",info.getNetmask());
+    	assertEquals(32, info.getAddressCount());
+
+    	utils = new SubnetUtils("192.168.0.1/28");
+    	utils.setInclusiveHostCount(true);
+    	info = utils.getInfo();
+    	assertEquals("255.255.255.240",info.getNetmask());
+    	assertEquals(16, info.getAddressCount());
+
+    	utils = new SubnetUtils("192.168.0.1/29");
+    	utils.setInclusiveHostCount(true);
+    	info = utils.getInfo();
+    	assertEquals("255.255.255.248",info.getNetmask());
+    	assertEquals(8, info.getAddressCount());
+
+    	utils = new SubnetUtils("192.168.0.1/30");
+    	utils.setInclusiveHostCount(true);
+    	info = utils.getInfo();
+    	assertEquals("255.255.255.252",info.getNetmask());
+    	assertEquals(4, info.getAddressCount());
+
+    	utils = new SubnetUtils("192.168.0.1/31");
+    	utils.setInclusiveHostCount(true);
+    	info = utils.getInfo();
+    	assertEquals("255.255.255.254",info.getNetmask());
+    	assertEquals(2, info.getAddressCount());
+
+    	utils = new SubnetUtils("192.168.0.1/32");
+    	utils.setInclusiveHostCount(true);
+    	info = utils.getInfo();
+    	assertEquals("255.255.255.255",info.getNetmask());
+    	assertEquals(1, info.getAddressCount());
+
+
+    }
 }



Mime
View raw message