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

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

Introduction

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

Prototype

public SubnetUtils(String cidrNotation) 

Source Link

Document

Constructor that takes a CIDR-notation string, e.g.

Usage

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

@Override
protected boolean subMatchAddressMask(String addressMask) {
    if (addressMask == null) {
        return false;
    }/*from   w w w  .j ava 2s. c  o 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:ch.cyberduck.core.SystemConfigurationProxy.java

/**
 * @param hostname Hostname or CIDR notation
 * @return True if host is excluded in native proxy configuration
 *//*from   www. j  av  a  2  s  . co m*/
private boolean isHostExcluded(String hostname) {
    if (!hostname.contains(".")) {
        // Non fully qualified hostname
        if (this.isSimpleHostnameExcludedNative()) {
            return true;
        }
    }
    for (String exception : this.getProxyExceptionsNative()) {
        if (StringUtils.isBlank(exception)) {
            continue;
        }
        if (this.matches(exception, hostname)) {
            return true;
        }
        try {
            SubnetUtils subnet = new SubnetUtils(exception);
            try {
                String ip = Inet4Address.getByName(hostname).getHostAddress();
                if (subnet.getInfo().isInRange(ip)) {
                    return true;
                }
            } catch (UnknownHostException e) {
                // Should not happen as we resolve addresses before attempting to connect
                // in ch.cyberduck.core.Resolver
                log.warn(e.getMessage());
            }
        } catch (IllegalArgumentException e) {
            // A hostname pattern but not CIDR. Does not
            // match n.n.n.n/m where n=1-3 decimal digits, m = 1-3 decimal digits in range 1-32
            log.debug("Invalid CIDR notation:" + e.getMessage());
        }
    }
    return false;
}

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

@Override
protected boolean subMatchAddressMask(String addressMask) {

    if (addressMask == null) {
        return false;
    }//from w  w  w  . j  av  a 2 s  .  c om

    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:net.itransformers.topologyviewer.menu.handlers.graphTools.searchMenuHandlers.SearchByIpMenuHandler.java

@Override
public void actionPerformed(ActionEvent e) {

    //                Map<String, Map<String, GraphMLMetadata<G>>> test1 = graphmlLoader();

    //        String key = JOptionPane.showInputDialog(frame, "Choose IP", JOptionPane.QUESTION_MESSAGE);
    final String ip = JOptionPane.showInputDialog(frame, "Enter IP", "Value", JOptionPane.QUESTION_MESSAGE);

    GraphViewerPanel viewerPanel = (GraphViewerPanel) frame.getTabbedPane().getSelectedComponent();
    Set<String> foundVertexes;
    foundVertexes = viewerPanel.FindNodeByKey("RoutePrefixes", new Object() {
        @Override//from w w  w  .  ja v a  2  s . c  o m
        public boolean equals(Object obj) {
            String s = (String) obj;
            String[] ipRanges = s.split(",");
            for (String ipRangeNotTrimmed : ipRanges) {
                String ipRange = ipRangeNotTrimmed.trim();
                if (ipRange.equals("") || ipRange.equals("0.0.0.0/0"))
                    continue;
                try {
                    SubnetUtils subnet = new SubnetUtils(ipRange);
                    if (subnet.getInfo().isInRange(ip)) {
                        return true;
                    }
                } catch (IllegalArgumentException iae) {
                    logger.error("Can not parse ip or ip range:" + ipRange + ", ip:" + ip);
                    System.out.println("Can not parse ip or ip range:" + ipRange + ", ip:" + ip);
                    iae.printStackTrace();
                    continue;
                }
            }
            return false;
        }
    });
    if (!foundVertexes.isEmpty()) {
        Iterator it = foundVertexes.iterator();
        if (foundVertexes.size() == 1) {
            Object element = it.next();
            System.out.println("Redrawing around " + element.toString());
            viewerPanel.SetPickedState(element.toString());
            viewerPanel.Animator(element.toString());
        } else {
            JOptionPane.showMessageDialog(frame, "Multiple Nodes with ip " + ip + " found :\n" + foundVertexes,
                    "Error", JOptionPane.ERROR_MESSAGE);
        }
    } else {
        JOptionPane.showMessageDialog(frame, "Can not find node with ip " + ip, "Error",
                JOptionPane.ERROR_MESSAGE);
    }
}

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 ww.jav a2  s . c  om*/

    }
    return matches;
}

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);//  w w w. j ava2  s  .  c o m
    final SubnetUtils.SubnetInfo info = su.getInfo();
    final String loa = info.getLowAddress();
    final String hia = info.getHighAddress();
    info.getCidrSignature();

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

From source file:com.googlesource.gerrit.plugins.motd.MotdFileBasedConfig.java

private List<Subnet> allSubnets() throws ConfigInvalidException, IOException {
    if (!config.getFile().exists()) {
        log.warn("Config file " + config.getFile() + " does not exist; not providing messages");
        return Collections.emptyList();
    }/*www .  j ava  2 s .  c  o m*/
    if (config.getFile().length() == 0) {
        log.info("Config file " + config.getFile() + " is empty; not providing messages");
        return Collections.emptyList();
    }

    try {
        config.load();
    } catch (ConfigInvalidException e) {
        throw new ConfigInvalidException(
                String.format("Config file %s is invalid: %s", config.getFile(), e.getMessage()), e);
    } catch (IOException e) {
        throw new IOException(String.format("Cannot read %s: %s", config.getFile(), e.getMessage()), e);
    }

    String[] motdLines = config.getStringList("gerrit", null, "motd");
    motd = Joiner.on("\n").useForNull("").join(motdLines);

    ImmutableList.Builder<Subnet> subnetlist = ImmutableList.builder();
    for (SubnetConfig c : allSubnets(config)) {
        if (c.getMotd().isEmpty()) {
            log.warn("Subnet configuration for " + c.getSubnet() + " has no message");
            continue;
        }

        try {
            SubnetUtils su = new SubnetUtils(c.getSubnet());
            Subnet subnet = new Subnet(c, su);
            subnetlist.add(subnet);
        } catch (IllegalArgumentException e) {
            log.warn("Subnet '" + c.getSubnet() + "' is invalid; skipping");
            continue;
        }
    }

    return subnetlist.build();
}

From source file:com.vmware.photon.controller.model.adapters.vsphere.CustomizationClient.java

private String cidr2mask(String subnetCIDR) {
    return new SubnetUtils(subnetCIDR).getInfo().getNetmask();
}

From source file:com.jtechme.apphub.FDroidApp.java

public static void initWifiSettings() {
    port = 8888;/*www . j  a v  a 2 s . c o  m*/
    ipAddressString = null;
    subnetInfo = new SubnetUtils("0.0.0.0/32").getInfo();
    ssid = "";
    bssid = "";
}

From source file:com.vmware.photon.controller.model.resources.SubnetService.java

private void validateState(SubnetState state) {
    Utils.validateState(getStateDescription(), state);

    // do we have a subnet in CIDR notation
    // creating new SubnetUtils to validate
    new SubnetUtils(state.subnetCIDR);
}