Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.apache.commons.net.util;
 
A class that performs some subnet calculations given a network address and a subnet mask.

Author(s):
<rwinston@apache.org>
Since:
2.0
See also:
"http://www.faqs.org/rfcs/rfc1519.html"
 
 public class SubnetUtils {
 
     private static final String IP_ADDRESS = "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})";
     private static final String SLASH_FORMAT =  + "/(\\d{1,3})";
     private static final Pattern addressPattern = Pattern.compile();
     private static final Pattern cidrPattern = Pattern.compile();
     private static final int NBITS = 32;
 
     private int netmask = 0;
     private int address = 0;
     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"

Parameters:
cidrNotation A CIDR-notation string, e.g. "192.168.0.1/16"
Throws:
java.lang.IllegalArgumentException if the parameter is invalid, i.e. does not match n.n.n.n/m where n=1-3 decimal digits, m = 1-3 decimal digits in range 1-32
 
     public SubnetUtils(String cidrNotation) {
         calculate(cidrNotation);
     }

    
Constructor that takes a dotted decimal address and a dotted decimal mask.

Parameters:
address An IP address, e.g. "192.168.0.1"
mask A dotted decimal netmask e.g. "255.255.0.0"
Throws:
java.lang.IllegalArgumentException if the address or mask is invalid, i.e. does not match n.n.n.n where n=1-3 decimal digits and the mask is not all zeros
 
     public SubnetUtils(String addressString mask) {
         calculate(toCidrNotation(addressmask));
     }


    
Returns true if the return value of SubnetUtils.SubnetInfo.getAddressCount() includes the network address and broadcast addresses.

Since:
2.2
 
     public boolean isInclusiveHostCount() {
         return ;
     }

    
Set to true if you want the return value of SubnetUtils.SubnetInfo.getAddressCount() to include the network and broadcast addresses.

Parameters:
inclusiveHostCount
Since:
2.2
 
     public void setInclusiveHostCount(boolean inclusiveHostCount) {
         this. = inclusiveHostCount;
     }



    
Convenience container for subnet summary information.
 
     public final class SubnetInfo {
         private SubnetInfo() {}
 
         private int netmask()       { return ; }
         private int network()       { return ; }
         private int address()       { return ; }
         private int broadcast()     { return ; }
 
        private int low() {
            return (isInclusiveHostCount() ? network() :
                broadcast() - network() > 1 ? network() + 1 : 0);
        }
        private int high() {
            return (isInclusiveHostCount() ? broadcast() :
                broadcast() - network() > 1 ? broadcast() -1  : 0);
        }

        
Returns true if the parameter address is in the range of usable endpoint addresses for this subnet. This excludes the network and broadcast adresses.

Parameters:
address A dot-delimited IPv4 address, e.g. "192.168.0.1"
Returns:
True if in range, false otherwise
        public boolean isInRange(String address) {
            return isInRange(toInteger(address));
        }
        private boolean isInRange(int address) {
            int diff = address - low();
            return (diff >= 0 && (diff <= (high() - low())));
        }
        public String getBroadcastAddress() {
            return format(toArray(broadcast()));
        }
        public String getNetworkAddress() {
            return format(toArray(network()));
        }
        public String getNetmask() {
            return format(toArray(netmask()));
        }
        public String getAddress() {
            return format(toArray(address()));
        }

        
Return the low address as a dotted IP address. Will be zero for CIDR/31 and CIDR/32 if the inclusive flag is false.

Returns:
the IP address in dotted format, may be "0.0.0.0" if there is no valid address
        public String getLowAddress() {
            return format(toArray(low()));
        }

        
Return the high address as a dotted IP address. Will be zero for CIDR/31 and CIDR/32 if the inclusive flag is false.

Returns:
the IP address in dotted format, may be "0.0.0.0" if there is no valid address
        public String getHighAddress() {
            return format(toArray(high()));
        }

        
Get the count of available addresses. Will be zero for CIDR/31 and CIDR/32 if the inclusive flag is false.

Returns:
the count of addresses, may be zero.
        public int getAddressCount()                {
            int count = broadcast() - network() + (isInclusiveHostCount() ? 1 : -1);
            return count < 0 ? 0 : count;
        }
        public int asInteger(String address) {
            return toInteger(address);
        }
        public String getCidrSignature() {
            return toCidrNotation(
                    format(toArray(address())),
                    format(toArray(netmask()))
            );
        }
        public String[] getAllAddresses() {
            int ct = getAddressCount();
            String[] addresses = new String[ct];
            if (ct == 0) {
                return addresses;
            }
            for (int add = low(), j=0; add <= high(); ++add, ++j) {
                addresses[j] = format(toArray(add));
            }
            return addresses;
        }

        

Since:
2.2
        @Override
        public String toString() {
            final StringBuilder buf = new StringBuilder();
            buf.append("CIDR Signature:\t[").append(getCidrSignature()).append("]")
                .append(" Netmask: [").append(getNetmask()).append("]\n")
                .append("Network:\t[").append(getNetworkAddress()).append("]\n")
                .append("Broadcast:\t[").append(getBroadcastAddress()).append("]\n")
                 .append("First Address:\t[").append(getLowAddress()).append("]\n")
                 .append("Last Address:\t[").append(getHighAddress()).append("]\n")
                 .append("# Addresses:\t[").append(getAddressCount()).append("]\n");
            return buf.toString();
        }
    }

    
Return a SubnetUtils.SubnetInfo instance that contains subnet-specific statistics

Returns:
new instance
    public final SubnetInfo getInfo() { return new SubnetInfo(); }
    /*
     * Initialize the internal fields from the supplied CIDR mask
     */
    private void calculate(String mask) {
        Matcher matcher = .matcher(mask);
        if (matcher.matches()) {
             = matchAddress(matcher);
            /* Create a binary netmask from the number of bits specification /x */
            int cidrPart = rangeCheck(Integer.parseInt(matcher.group(5)), 0, );
            for (int j = 0; j < cidrPart; ++j) {
                 |= (1 << 31-j);
            }
            /* Calculate base network address */
             = ( & );
            /* Calculate broadcast address */
             =  | ~();
        } else {
            throw new IllegalArgumentException("Could not parse [" + mask + "]");
        }
    }
    /*
     * Convert a dotted decimal format address to a packed integer format
     */
    private int toInteger(String address) {
        Matcher matcher = .matcher(address);
        if (matcher.matches()) {
            return matchAddress(matcher);
        } else {
            throw new IllegalArgumentException("Could not parse [" + address + "]");
        }
    }
    /*
     * Convenience method to extract the components of a dotted decimal address and
     * pack into an integer using a regex match
     */
    private int matchAddress(Matcher matcher) {
        int addr = 0;
        for (int i = 1; i <= 4; ++i) {
            int n = (rangeCheck(Integer.parseInt(matcher.group(i)), -1, 255));
            addr |= ((n & 0xff) << 8*(4-i));
        }
        return addr;
    }
    /*
     * Convert a packed integer address into a 4-element array
     */
    private int[] toArray(int val) {
        int ret[] = new int[4];
        for (int j = 3; j >= 0; --j) {
            ret[j] |= ((val >>> 8*(3-j)) & (0xff));
        }
        return ret;
    }
    /*
     * Convert a 4-element array into dotted decimal format
     */
    private String format(int[] octets) {
        StringBuilder str = new StringBuilder();
        for (int i =0; i < octets.length; ++i){
            str.append(octets[i]);
            if (i != octets.length - 1) {
                str.append(".");
            }
        }
        return str.toString();
    }
    /*
     * Convenience function to check integer boundaries.
     * Checks if a value x is in the range (begin,end].
     * Returns x if it is in range, throws an exception otherwise.
     */
    private int rangeCheck(int valueint beginint end) {
        if (value > begin && value <= end) { // (begin,end]
            return value;
        }
        throw new IllegalArgumentException("Value [" + value + "] not in range ("+begin+","+end+"]");
    }
    /*
     * Count the number of 1-bits in a 32-bit integer using a divide-and-conquer strategy
     * see Hacker's Delight section 5.1
     */
    int pop(int x) {
        x = x - ((x >>> 1) & 0x55555555);
        x = (x & 0x33333333) + ((x >>> 2) & 0x33333333);
        x = (x + (x >>> 4)) & 0x0F0F0F0F;
        x = x + (x >>> 8);
        x = x + (x >>> 16);
        return x & 0x0000003F;
    }
    /* Convert two dotted decimal addresses to a single xxx.xxx.xxx.xxx/yy format
     * by counting the 1-bit population in the mask address. (It may be better to count
     * NBITS-#trailing zeroes for this case)
     */
    private String toCidrNotation(String addrString mask) {
        return addr + "/" + pop(toInteger(mask));
    }
New to GrepCode? Check out our FAQ X