List of usage examples for org.apache.commons.net.util SubnetUtils SubnetUtils
public SubnetUtils(String cidrNotation)
From source file:com.topsec.tsm.sim.asset.web.AssetListController.java
@RequestMapping("stopDiscover") @ResponseBody/*w w w . j a va2 s .c om*/ public Object stopDiscover(@RequestParam(value = "networkAddress", required = false) String networkAddressParam, @RequestParam(value = "netmask", defaultValue = "24") int netmask, @RequestParam(value = "scanNodeId") String scanNodeId) { if (StringUtil.isBlank(networkAddressParam) || networkAddressParam.equals(IpAddress.getLocalIp().getLocalhostAddress())) { networkAddressParam = IpAddress.getLocalIpv4Address().toString(); } Node collectorNode = nodeMgr.getChildOrSelf(scanNodeId, NodeDefinition.NODE_TYPE_COLLECTOR); SubnetUtils subnetUtils = new SubnetUtils(networkAddressParam + "/" + netmask); String networkAddress = subnetUtils.getInfo().getNetworkAddress(); SubNet sbt = dcvAssetManager.getSubNet(collectorNode.getIp(), networkAddress, netmask); if (sbt != null) { sbt.setScannedCount(sbt.getAssetCount()); } HashMap<String, Object> params = new HashMap<String, Object>(3); params.put("networkAddress", networkAddress); params.put("netmask", netmask); try { NodeUtil.dispatchCommand(NodeUtil.getRoute(collectorNode), MessageDefinition.CMD_STOP_DISCOVER, params, 5000); } catch (CommunicationException e) { logger.warn("???!"); return false; } return true; }
From source file:com.cloud.utils.net.NetUtils.java
public static boolean isIpWithtInCidrRange(final String ipAddress, final String cidr) { if (!isValidIp(ipAddress)) { return false; }//from ww w. j a va 2 s . co m if (!isValidCIDR(cidr)) { return false; } // check if the gatewayip is the part of the ip range being added. // RFC 3021 - 31-Bit Prefixes on IPv4 Point-to-Point Links // GW Netmask Stat IP End IP // 192.168.24.0 - 255.255.255.254 - 192.168.24.0 - 192.168.24.1 // https://tools.ietf.org/html/rfc3021 // Added by Wilder Rodrigues final SubnetUtils subnetUtils = new SubnetUtils(cidr); subnetUtils.setInclusiveHostCount(true); final boolean isInRange = subnetUtils.getInfo().isInRange(ipAddress); return isInRange; }
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; }
From source file:nfinity.FindPeer.java
public String ScanNetwork() { try {/* w w w . java 2s. c o m*/ Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces(); for (NetworkInterface netint : Collections.list(nets)) { Enumeration<InetAddress> inetAddresses = netint.getInetAddresses(); for (InetAddress inetAddress : Collections.list(inetAddresses)) { if (!inetAddress.getClass().toString().equals("class java.net.Inet6Address")) { if (!inetAddress.isLoopbackAddress()) { try { SubnetUtils utils = new SubnetUtils(inetAddress.getHostAddress() + "/" + netint.getInterfaceAddresses().get(1).getNetworkPrefixLength()); String[] allIps = utils.getInfo().getAllAddresses(); return ConnectIP(allIps, inetAddress); } catch (IllegalArgumentException ex) { int prefix = NetworkInterface.getByInetAddress(inetAddress).getInterfaceAddresses() .get(0).getNetworkPrefixLength(); if (!inetAddress.isLoopbackAddress()) { System.out .println("IP: " + inetAddress.getHostAddress() + " Prefix: " + prefix); SubnetUtils utils = new SubnetUtils( inetAddress.getHostAddress() + "/" + prefix); String[] allIps = utils.getInfo().getAllAddresses(); return ConnectIP(allIps, inetAddress); } } } } } } } catch (SocketException ex) { System.out.println("Connection failed " + ex.getMessage()); } return "Peer not found"; }
From source file:org.apache.commons.net.examples.cidr.SubnetUtilsExample.java
public static void main(String[] args) { String subnet = "192.168.0.3/31"; SubnetUtils utils = new SubnetUtils(subnet); SubnetInfo info = utils.getInfo();// w w w. j a v a 2 s. c o m 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()))); System.out.printf("Netmask:\t\t\t%s\t[%s]%n", info.getNetmask(), Integer.toBinaryString(info.asInteger(info.getNetmask()))); System.out.printf("CIDR Representation:\t\t%s%n%n", info.getCidrSignature()); System.out.printf("Supplied IP Address:\t\t%s%n%n", info.getAddress()); System.out.printf("Network Address:\t\t%s\t[%s]%n", info.getNetworkAddress(), Integer.toBinaryString(info.asInteger(info.getNetworkAddress()))); System.out.printf("Broadcast Address:\t\t%s\t[%s]%n", info.getBroadcastAddress(), Integer.toBinaryString(info.asInteger(info.getBroadcastAddress()))); System.out.printf("Low Address:\t\t\t%s\t[%s]%n", info.getLowAddress(), Integer.toBinaryString(info.asInteger(info.getLowAddress()))); System.out.printf("High Address:\t\t\t%s\t[%s]%n", info.getHighAddress(), Integer.toBinaryString(info.asInteger(info.getHighAddress()))); System.out.printf("Total usable addresses: \t%d%n", Long.valueOf(info.getAddressCountLong())); System.out.printf("Address List: %s%n%n", Arrays.toString(info.getAllAddresses())); final String prompt = "Enter an IP address (e.g. 192.168.0.10):"; System.out.println(prompt); Scanner scanner = new Scanner(System.in); while (scanner.hasNextLine()) { String address = scanner.nextLine(); System.out.println("The IP address [" + address + "] is " + (info.isInRange(address) ? "" : "not ") + "within the subnet [" + subnet + "]"); System.out.println(prompt); } scanner.close(); }
From source file:org.apache.hadoop.util.MachineList.java
/** * Accepts a collection of ip/cidr/host addresses * /*from ww w .j a v a 2 s .com*/ * @param hostEntries * @param addressFactory addressFactory to convert host to InetAddress */ public MachineList(Collection<String> hostEntries, InetAddressFactory addressFactory) { this.addressFactory = addressFactory; if (hostEntries != null) { if ((hostEntries.size() == 1) && (hostEntries.contains(WILDCARD_VALUE))) { all = true; ipAddresses = null; hostNames = null; cidrAddresses = null; } else { all = false; Set<String> ips = new HashSet<String>(); List<SubnetUtils.SubnetInfo> cidrs = new LinkedList<SubnetUtils.SubnetInfo>(); Set<String> hosts = new HashSet<String>(); for (String hostEntry : hostEntries) { //ip address range if (hostEntry.indexOf("/") > -1) { try { SubnetUtils subnet = new SubnetUtils(hostEntry); subnet.setInclusiveHostCount(true); cidrs.add(subnet.getInfo()); } catch (IllegalArgumentException e) { LOG.warn("Invalid CIDR syntax : " + hostEntry); throw e; } } else if (InetAddresses.isInetAddress(hostEntry)) { //ip address ips.add(hostEntry); } else { //hostname hosts.add(hostEntry); } } ipAddresses = (ips.size() > 0) ? ips : null; cidrAddresses = (cidrs.size() > 0) ? cidrs : null; hostNames = (hosts.size() > 0) ? hosts : null; } } else { all = false; ipAddresses = null; hostNames = null; cidrAddresses = null; } }
From source file:org.apache.marmotta.platform.security.util.IPv4SubnetInfo.java
public IPv4SubnetInfo(Inet4Address address, int netmask) { apacheInfo = new SubnetUtils(address.getHostAddress() + "/" + netmask).getInfo(); }
From source file:org.apache.metron.alerts.adapters.RangeChecker.java
static boolean checkRange(Set<String> CIDR_networks, String ip) { for (String network : CIDR_networks) { System.out.println("Looking at range: " + network + " and ip " + ip); SubnetUtils utils = new SubnetUtils(network); if (utils.getInfo().isInRange(ip)) { System.out.println(ip + " in range " + network); return true; }/*from w w w. j a v a 2 s . c om*/ } //no matches return false; }
From source file:org.apache.nifi.cluster.firewall.impl.FileBasedClusterNodeFirewall.java
private void parseConfig(final File config) throws IOException { // clear old information subnetInfos.clear();//w ww. j a v a 2 s . co m try (BufferedReader br = new BufferedReader(new FileReader(config))) { String ipOrHostLine; String ipCidr; int totalIpsAdded = 0; while ((ipOrHostLine = br.readLine()) != null) { // cleanup whitespace ipOrHostLine = ipOrHostLine.trim(); if (ipOrHostLine.isEmpty() || ipOrHostLine.startsWith("#")) { // skip empty lines or comments continue; } else if (ipOrHostLine.contains("#")) { // parse out comments in IP containing lines ipOrHostLine = ipOrHostLine.substring(0, ipOrHostLine.indexOf("#")).trim(); } // if given a complete IP, then covert to CIDR if (ipOrHostLine.contains("/")) { ipCidr = ipOrHostLine; } else if (ipOrHostLine.contains("\\")) { logger.warn( "CIDR IP notation uses forward slashes '/'. Replacing backslash '\\' with forward slash'/' for '{}'", ipOrHostLine); ipCidr = ipOrHostLine.replace("\\", "/"); } else { try { ipCidr = InetAddress.getByName(ipOrHostLine).getHostAddress(); if (!ipOrHostLine.equals(ipCidr)) { logger.debug("Resolved host '{}' to ip '{}'", ipOrHostLine, ipCidr); } ipCidr += "/32"; logger.debug("Adding CIDR to exact IP: '{}'", ipCidr); } catch (final UnknownHostException uhe) { logger.warn("Firewall is skipping unknown host address: '{}'", ipOrHostLine); continue; } } try { logger.debug("Adding CIDR IP to firewall: '{}'", ipCidr); final SubnetUtils subnetUtils = new SubnetUtils(ipCidr); subnetUtils.setInclusiveHostCount(true); subnetInfos.add(subnetUtils.getInfo()); totalIpsAdded++; } catch (final IllegalArgumentException iae) { logger.warn("Firewall is skipping invalid CIDR address: '{}'", ipOrHostLine); } } if (totalIpsAdded == 0) { logger.info("No IPs added to firewall. Firewall will accept all requests."); } else { logger.info( "Added {} IP(s) to firewall. Only requests originating from the configured IPs will be accepted.", totalIpsAdded); } } }
From source file:org.apache.sling.discovery.base.connectors.ping.wl.SubnetWhitelistEntry.java
public SubnetWhitelistEntry(String cidrNotation) { subnetInfo = new SubnetUtils(cidrNotation).getInfo(); }