Package org.apache.commons.net.util

Examples of org.apache.commons.net.util.SubnetUtils


        final String networkAddresses[] = new String[] { "192.0.0.0", "192.168.0.0", "192.168.0.0", "192.168.0.0" };
        final String cidrSignatures[] = new String[] { "192.168.0.1/8", "192.168.0.1/16", "192.168.0.1/24", "192.168.0.1/29" };
        final int usableAddresses[] = new int[] { 16777214, 65534, 254, 6 };

        for (int i = 0; i < masks.length; ++i) {
            SubnetUtils utils = new SubnetUtils(address, masks[i]);
            SubnetInfo info = utils.getInfo();
            assertEquals(bcastAddresses[i], info.getBroadcastAddress());
            assertEquals(cidrSignatures[i], info.getCidrSignature());
            assertEquals(lowAddresses[i], info.getLowAddress());
            assertEquals(highAddresses[i], info.getHighAddress());
            assertEquals(networkAddresses[i], info.getNetworkAddress());
View Full Code Here


         String highA[]=new String[]{ "192.168.15.7",     "192.168.15.7",     "192.168.15.7" };
         String cidrS[]=new String[]{ "192.168.15.7/30""192.168.15.7/31""192.168.15.7/32"};
         int usableAd[]=new int[]   { 4 ,                 2,                  1};

         for (int i = 0; i < masks.length; ++i) {
             SubnetUtils utils = new SubnetUtils(address, masks[i]);
             utils.setInclusiveHostCount(true);
             SubnetInfo info = utils.getInfo();
             assertEquals("ci "+masks[i], cidrS[i], info.getCidrSignature());
             assertEquals("bc "+masks[i], bcast[i], info.getBroadcastAddress());
             assertEquals("ac "+masks[i], usableAd[i], info.getAddressCount());
             assertEquals("nw "+masks[i], netwk[i], info.getNetworkAddress());
             assertEquals("lo "+masks[i], lowAd[i], info.getLowAddress());
View Full Code Here

        String cidrS[]=new String[]{ "192.168.15.7/30""192.168.15.7/31""192.168.15.7/32"};
        int usableAd[]=new int[]   { 2 ,                 0,                  0};
       // low and high addresses don't exist

        for (int i = 0; i < masks.length; ++i) {
            SubnetUtils utils = new SubnetUtils(address, masks[i]);
            utils.setInclusiveHostCount(false);
            SubnetInfo info = utils.getInfo();
            assertEquals("ci "+masks[i], cidrS[i], info.getCidrSignature());
            assertEquals("bc "+masks[i], bcast[i], info.getBroadcastAddress());
            assertEquals("nw "+masks[i], netwk[i], info.getNetworkAddress());
            assertEquals("ac "+masks[i], usableAd[i], info.getAddressCount());
            assertEquals("lo "+masks[i], lowAd[i], info.getLowAddress());
View Full Code Here

        }
    }

    // TODO Lower address test
    public void testAddresses() {
        SubnetUtils utils = new SubnetUtils("192.168.0.1/29");
        SubnetInfo info = utils.getInfo();
        assertTrue(info.isInRange("192.168.0.1"));
        // We don't count the broadcast address as usable
        assertFalse(info.isInRange("192.168.0.7"));
        assertFalse(info.isInRange("192.168.0.8"));
        assertFalse(info.isInRange("10.10.2.1"));
View Full Code Here

        assertFalse(info.isInRange("192.168.0.255"));
    }

    public void testZeroNetmaskBits() {
        try {
            new SubnetUtils("192.168.0.1/0");
            fail("Mask /0 should have generated an IllegalArgumentException");
        }
        catch (IllegalArgumentException expected) {
        }
    }
View Full Code Here

   * @return true if the given string is a subnet specified
   *     using CIDR notation, false otherwise
   */
  public static boolean isValidSubnet(String subnet) {
    try {
      new SubnetUtils(subnet);
      return true;
    } catch (IllegalArgumentException iae) {
      return false;
    }
  }
View Full Code Here

   * @throws IllegalArgumentException if subnet is invalid
   */
  public static List<InetAddress> getIPs(String subnet,
      boolean returnSubinterfaces) {
    List<InetAddress> addrs = new ArrayList<InetAddress>();
    SubnetInfo subnetInfo = new SubnetUtils(subnet).getInfo();
    Enumeration<NetworkInterface> nifs;

    try {
      nifs = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
View Full Code Here

*/
public class SubnetUtilsExample {

    public static void main(String[] args) {
        String subnet = "192.168.0.3/31";
        SubnetUtils utils = new SubnetUtils(subnet);
        SubnetInfo info = utils.getInfo();

        System.out.printf("Subnet Information for %s:\n", subnet);
        System.out.println("--------------------------------------");
        System.out.printf("IP Address:\t\t\t%s\t[%s]\n", info.getAddress(),
                Integer.toBinaryString(info.asInteger(info.getAddress())));
View Full Code Here

      return new AnonymousMatch(privilege);
    } else if (CIDR_FORMAT_SHORT.matcher(host).matches()) {
      if (LOG.isDebugEnabled()) {
        LOG.debug("Using CIDR match for '" + host + "' and " + privilege);
      }
      return new CIDRMatch(privilege, new SubnetUtils(host).getInfo());
    } else if (CIDR_FORMAT_LONG.matcher(host).matches()) {
      if (LOG.isDebugEnabled()) {
        LOG.debug("Using CIDR match for '" + host + "' and " + privilege);
      }
      String[] pair = host.split("/");
      return new CIDRMatch(privilege,
          new SubnetUtils(pair[0], pair[1]).getInfo());
    } else if (host.contains("*") || host.contains("?") || host.contains("[")
        || host.contains("]")) {
      if (LOG.isDebugEnabled()) {
        LOG.debug("Using Regex match for '" + host + "' and " + privilege);
      }
View Full Code Here

   * @return true if the given string is a subnet specified
   *     using CIDR notation, false otherwise
   */
  public static boolean isValidSubnet(String subnet) {
    try {
      new SubnetUtils(subnet);
      return true;
    } catch (IllegalArgumentException iae) {
      return false;
    }
  }
View Full Code Here

TOP

Related Classes of org.apache.commons.net.util.SubnetUtils

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.