Example usage for org.apache.commons.net.util SubnetUtils setInclusiveHostCount

List of usage examples for org.apache.commons.net.util SubnetUtils setInclusiveHostCount

Introduction

In this page you can find the example usage for org.apache.commons.net.util SubnetUtils setInclusiveHostCount.

Prototype

public void setInclusiveHostCount(boolean inclusiveHostCount) 

Source Link

Document

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

Usage

From source file:com.jaeksoft.searchlib.util.NetworksUtils.java

public final static SubnetInfo[] getSubnetArray(String ips) throws IOException {
    if (ips == null)
        return null;
    StringReader sr = new StringReader(ips);
    try {//www  .  j  av a  2  s  . c o m
        List<String> lines = IOUtils.readLines(sr);
        if (lines == null)
            return null;
        SubnetInfo[] subnetArray = new SubnetInfo[lines.size()];
        int i = 0;
        for (String line : lines) {
            line = line.trim();
            if (line.isEmpty())
                continue;
            if (line.indexOf('/') == -1)
                line = line + "/32";
            SubnetUtils subnetUtils = new SubnetUtils(line);
            subnetUtils.setInclusiveHostCount(true);
            subnetArray[i++] = subnetUtils.getInfo();
        }
        return subnetArray;
    } finally {
        IOUtils.closeQuietly(sr);
    }
}

From source file:com.binarybirchtree.filters.IpFilter.java

public static boolean ipIsInList(String ip, String[] list) {
    if (ip == null)
        return false;

    int failed = 0;
    for (String listIp : list) {
        SubnetUtils subnet = new SubnetUtils(listIp);
        subnet.setInclusiveHostCount(true);

        if (!subnet.getInfo().isInRange(ip))
            failed++;/*w w w .  j a v  a 2s.  co  m*/
    }

    return failed < list.length;
}

From source file:it.greenvulcano.gvesb.identity.impl.HTTPIdentityInfo.java

@Override
protected boolean subMatchAddressMask(String addressMask) {
    if (addressMask == null) {
        return false;
    }// w  w  w  . j  a  va2  s.co  m

    SubnetUtils subnet = new SubnetUtils(addressMask);
    subnet.setInclusiveHostCount(true);

    String address = request.getRemoteAddr();
    boolean res = subnet.getInfo().isInRange(address);
    if (debug) {
        logger.debug("HTTPIdentityInfo[" + getName() + "]: AddressMask[" + subnet.getInfo().getCidrSignature()
                + ": " + address + "] -> " + res);
    }
    return res;
}

From source file:cc.arduino.net.PACSupportMethods.java

public boolean isInNet(String host, String pattern, String mask) throws UnknownHostException {
    SubnetUtils subnetUtils = new SubnetUtils(pattern, mask);
    subnetUtils.setInclusiveHostCount(true);
    return new CIDRUtils(subnetUtils.getInfo().getCidrSignature()).isInRange(host);
}

From source file:it.greenvulcano.gvesb.api.security.JaxRsIdentityInfo.java

@Override
protected boolean subMatchAddressMask(String addressMask) {
    boolean matches = false;

    if (addressMask != null) {

        SubnetUtils subnet = new SubnetUtils(addressMask);
        subnet.setInclusiveHostCount(true);

        matches = subnet.getInfo().isInRange(remoteAddress);
        if (debug) {
            logger.debug("JaxRsIdentityInfo[" + getName() + "]: AddressMask["
                    + subnet.getInfo().getCidrSignature() + ": " + remoteAddress + "] -> " + matches);
        }/*from w  w w  .j  a v a  2 s.co  m*/

    }
    return matches;
}

From source file:it.greenvulcano.gvesb.identity.impl.DummyIdentityInfo.java

@Override
protected boolean subMatchAddressMask(String addressMask) {

    if (addressMask == null) {
        return false;
    }// ww w. j  a  va 2  s  .  co  m

    SubnetUtils subnet = new SubnetUtils(addressMask);
    subnet.setInclusiveHostCount(true);

    boolean res = false;
    String address = null;
    for (String a : addresses) {
        address = a;
        res = subnet.getInfo().isInRange(address);
        System.out.println("[" + address + "] -> " + addressMask + " : " + res);
        if (res) {
            break;
        }
    }
    System.out.println("DummyIdentityInfo[" + getName() + "]: AddressMask["
            + subnet.getInfo().getCidrSignature() + ": " + address + "] -> " + res);
    return res;
}

From source file:com.pushinginertia.commons.net.IpAddressRange.java

protected IpAddressRange(final String cidrNotation) throws IllegalArgumentException {
    // 1. compute lo/hi addresses from given cidr block
    final SubnetUtils su = new SubnetUtils(cidrNotation);
    su.setInclusiveHostCount(true);
    final SubnetUtils.SubnetInfo info = su.getInfo();
    final String loa = info.getLowAddress();
    final String hia = info.getHighAddress();
    info.getCidrSignature();// ww  w.j av  a  2 s . c o  m

    // 2. assign values
    this.cidrNotationList = Arrays.asList(cidrNotation);
    this.lowAddress = new IpAddress(loa);
    this.highAddress = new IpAddress(hia);
}

From source file:com.binarybirchtree.filters.IpFilter.java

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
        FilterChain filterChain) throws ServletException, IOException {
    // Do not block localhost.
    if (request.getRemoteAddr().equals("127.0.0.1")) {
        filterChain.doFilter(request, response);
        return;//from w  w w .  j  a  v a  2 s .c o  m
    }

    boolean allowed = true;

    // Refuse connections that circumvented Cloudflare.
    // Checking the latest IP from the X-Forwarded-For header on Heroku, since request.getRemoteAddr() seems to return an IP in Heroku's internal network.
    if (!ipIsInList(request.getRemoteAddr(), internalIps) || (getForwardedIp(request, 1) != null
            && !ipIsInList(getForwardedIp(request, 1), bypassCloudflareIps)
            && !ipIsInList(getForwardedIp(request, 1), cloudflareIps)))
        allowed = false;

    if (allowed) {
        // Check if the IP before Cloudflare is blacklisted.
        String proxiedIp = getForwardedIp(request, 2);

        if (proxiedIp != null) {
            for (String ip : blacklistIps) {
                SubnetUtils subnet = new SubnetUtils(ip);
                subnet.setInclusiveHostCount(true);
                if (!subnet.getInfo().isInRange(proxiedIp)) {
                    allowed = false;
                    break;
                }
            }
        }
    }

    // If the request failed one of the tests, send an error response and do not continue processing the request.
    if (!allowed) {
        response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
        return;
    }

    // If the request passed the tests, allow it to be processed normally.
    filterChain.doFilter(request, response);
}

From source file:de.yaio.commons.net.IpAddressMatcher.java

/** 
 * creates an IpAddressMatcher/*from   w  w  w. java 2 s.  c  o m*/
 * @param ipList                 list of ip-addresses with/without netmask
 */
public IpAddressMatcher(List<String> ipList) {
    this.ipList = new ArrayList<SubnetUtils>();
    for (String ip : ipList) {
        if (StringUtils.isBlank(ip)) {
            continue;
        }
        SubnetUtils ipMatcher;
        if (ip.indexOf("/") > 0) {
            ipMatcher = new SubnetUtils(ip);
        } else {
            ipMatcher = new SubnetUtils(ip + "/32");
        }
        ipMatcher.setInclusiveHostCount(true);
        this.ipList.add(ipMatcher);
    }
}

From source file:com.cloud.utils.net.NetUtils.java

public static boolean isNetworkorBroadcastIP(String ip, String netmask) {
    String cidr = getCidrFromGatewayAndNetmask(ip, netmask);
    final SubnetUtils subnetUtils = new SubnetUtils(cidr);
    subnetUtils.setInclusiveHostCount(false);
    final boolean isInRange = subnetUtils.getInfo().isInRange(ip);
    return !isInRange;
}