List of usage examples for org.apache.commons.net.util SubnetUtils getInfo
public final SubnetInfo getInfo()
From source file:com.ardikars.opennetcut.app.NetworkScanner.java
public NetworkScanner(PacketHandler handler) { SubnetUtils su = new SubnetUtils(StaticField.NETWORK_ADDRESS.toString(), StaticField.NETMASK_ADDRESS.toString()); String[] strips = su.getInfo().getAllAddresses(); for (String ip : strips) { ips.add(Inet4Address.valueOf(ip.trim())); }/*from w w w .j av a 2 s.c o m*/ this.handler = handler; this.index = 0; }
From source file:com.vmware.photon.controller.cloudstore.xenon.entity.SubnetAllocatorService.java
private void seedWithOneAvailableSubnet(String rootCidr) throws InterruptedException, TimeoutException, BadRequestException, DocumentNotFoundException, UnknownHostException { SubnetUtils subnetUtils = new SubnetUtils(rootCidr); SubnetUtils.SubnetInfo subnetInfo = subnetUtils.getInfo(); Long lowIp, highIp;/*from w w w . j av a 2 s. c o m*/ InetAddress lowIpAddress = InetAddresses.forString(subnetInfo.getLowAddress()); if (lowIpAddress instanceof Inet4Address) { lowIp = IpHelper.ipToLong((Inet4Address) lowIpAddress); } else { throw new IllegalArgumentException("lowIpAddress not an IPv4 address"); } InetAddress highIpAddress = InetAddresses.forString(subnetInfo.getHighAddress()); if (highIpAddress instanceof Inet4Address) { highIp = IpHelper.ipToLong((Inet4Address) highIpAddress); } else { throw new IllegalArgumentException("highIpAddress not an IPv4 address"); } DhcpSubnetService.State subnet = new DhcpSubnetService.State(); subnet.lowIp = lowIp; subnet.highIp = highIp; Operation postOperation = Operation.createPost(this, DhcpSubnetService.FACTORY_LINK).setBody(subnet); ServiceUtils.doServiceOperation(this, postOperation); }
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 w w w .ja v a 2 s. c o 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.evilsocket.dsploit.net.Network.java
private IP4Address getNetmask() throws UnknownHostException { IP4Address result = new IP4Address(mInfo.netmask); if (System.getSettings().getBoolean("WIDE_SCAN", false)) { SubnetUtils privateNetwork; for (String cidr_notation : PRIVATE_NETWORKS) { privateNetwork = new SubnetUtils(cidr_notation); if (privateNetwork.getInfo().isInRange(mLocal.toString())) { result = new IP4Address(privateNetwork.getInfo().getNetmask()); break; }//from w w w. java 2s .co m } } return result; }
From source file:com.ethlo.geodata.importer.file.FileIpLookupImporter.java
@Override public long importData() throws IOException { final Map.Entry<Date, File> ipDataFile = super.fetchResource(DataType.IP, url); final AtomicInteger count = new AtomicInteger(0); final File csvFile = ipDataFile.getValue(); final long total = IoUtils.lineCount(csvFile); final ProgressListener prg = new ProgressListener( l -> publish(new DataLoadedEvent(this, DataType.IP, Operation.IMPORT, l, total))); final IpLookupImporter ipLookupImporter = new IpLookupImporter(csvFile); final JsonFactory f = new JsonFactory(); f.enable(JsonGenerator.Feature.ESCAPE_NON_ASCII); f.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET); final ObjectMapper mapper = new ObjectMapper(f); final byte newLine = (byte) "\n".charAt(0); logger.info("Writing IP data to file {}", getFile().getAbsolutePath()); try (final OutputStream out = new BufferedOutputStream(new FileOutputStream(getFile()))) { ipLookupImporter.processFile(entry -> { final String strGeoNameId = findMapValue(entry, "geoname_id", "represented_country_geoname_id", "registered_country_geoname_id"); final String strGeoNameCountryId = findMapValue(entry, "represented_country_geoname_id", "registered_country_geoname_id"); final Long geonameId = strGeoNameId != null ? Long.parseLong(strGeoNameId) : null; final Long geonameCountryId = strGeoNameCountryId != null ? Long.parseLong(strGeoNameCountryId) : null;/*w w w . j a v a 2s . co m*/ if (geonameId != null) { final SubnetUtils u = new SubnetUtils(entry.get("network")); final long lower = UnsignedInteger .fromIntBits(InetAddresses .coerceToInteger(InetAddresses.forString(u.getInfo().getLowAddress()))) .longValue(); final long upper = UnsignedInteger .fromIntBits(InetAddresses .coerceToInteger(InetAddresses.forString(u.getInfo().getHighAddress()))) .longValue(); final Map<String, Object> paramMap = new HashMap<>(5); paramMap.put("geoname_id", geonameId); paramMap.put("geoname_country_id", geonameCountryId); paramMap.put("first", lower); paramMap.put("last", upper); try { mapper.writeValue(out, paramMap); out.write(newLine); } catch (IOException exc) { throw new DataAccessResourceFailureException(exc.getMessage(), exc); } } if (count.get() % 100_000 == 0) { logger.info("Processed {}", count.get()); } count.getAndIncrement(); prg.update(); }); } return total; }
From source file:com.vmware.photon.controller.housekeeper.xenon.SubnetIPLeaseSyncServiceTest.java
private void createDhcpSubnet(TestEnvironment env) throws Throwable { String cidr = "192.168.0.0/16"; SubnetUtils subnetUtils = new SubnetUtils(cidr); SubnetUtils.SubnetInfo subnetInfo = subnetUtils.getInfo(); DhcpSubnetService.State subnetState = new DhcpSubnetService.State(); subnetState.subnetId = SUBNET_ID;//from ww w. j ava 2 s . c o m subnetState.version = 1L; subnetState.lowIp = IpHelper.ipStringToLong(subnetInfo.getLowAddress()); subnetState.highIp = IpHelper.ipStringToLong(subnetInfo.getHighAddress()); subnetState.lowIpDynamic = subnetState.lowIp + 1; subnetState.highIpDynamic = subnetState.highIp - 1; subnetState.cidr = cidr; URI hostUri = env.getHosts()[0].getUri(); subnetState.dhcpAgentEndpoint = hostUri.getScheme() + "://" + hostUri.getHost() + ":" + hostUri.getPort(); env.sendPostAndWaitForReplication(DhcpSubnetService.FACTORY_LINK, subnetState); }
From source file:com.dianping.cat.system.page.router.config.RouterConfigManager.java
private void refreshNetInfo() { Map<String, List<SubnetInfo>> subNetInfos = new HashMap<String, List<SubnetInfo>>(); for (Entry<String, NetworkPolicy> netPolicy : m_routerConfig.getNetworkPolicies().entrySet()) { ArrayList<SubnetInfo> infos = new ArrayList<SubnetInfo>(); if (!DEFAULT.equals(netPolicy.getKey())) { for (Entry<String, Network> network : netPolicy.getValue().getNetworks().entrySet()) { try { SubnetUtils subnetUtils = new SubnetUtils(network.getValue().getId()); SubnetInfo netInfo = subnetUtils.getInfo(); infos.add(netInfo);//w ww .ja v a 2 s .com } catch (Exception e) { Cat.logError(e); } } subNetInfos.put(netPolicy.getKey(), infos); } } m_subNetInfos = subNetInfos; m_ipToGroupInfo = new HashMap<String, String>(); }
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:com.cloud.utils.net.NetUtils.java
public static boolean isIpWithtInCidrRange(final String ipAddress, final String cidr) { if (!isValidIp(ipAddress)) { return false; }//from w w w. j av a 2s . c o 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:eu.smartenit.sdn.floodlight090.dtm.DTM.java
public synchronized short getReactiveWithoutReferenceOutOfPortNumber() { logger.debug("getOutOfPortNumberReactiveWithoutReference() begin"); if (configData == null) { throw new IllegalArgumentException("DTM has not been configured yet"); }//from w ww . ja v a 2 s . c o m if (cVectorMap.isEmpty()) { throw new IllegalArgumentException("Compensation vector empty"); } OFPacketIn pi = getOFPacketIn(); String piDestAddr = intToStringIp( new OFMatch().loadFromPacket(pi.getPacketData(), pi.getInPort()).getNetworkDestination()); logger.debug("Network dest = " + intToStringIp( new OFMatch().loadFromPacket(pi.getPacketData(), pi.getInPort()).getNetworkDestination())); short[] selectedDaRouterOfPortNumber = null; for (ConfigDataEntry entry : configData.getEntries()) { String subnet = entry.getRemoteDcPrefix().getPrefix() + "/" + Integer.toString(entry.getRemoteDcPrefix().getPrefixLength()); SubnetUtils utils = new SubnetUtils(subnet); if (utils.getInfo().isInRange(piDestAddr)) { selectedDaRouterOfPortNumber = new short[] { (short) entry.getTunnels().get(0).getDaRouterOfPortNumber(), (short) entry.getTunnels().get(1).getDaRouterOfPortNumber() }; logger.debug("IP " + piDestAddr + " in range " + subnet); } } if (selectedDaRouterOfPortNumber == null) { logger.debug("Destination host unreachable! Wrong IP address: " + piDestAddr); logger.debug("getOutOfPortNumberReactiveWithoutReference"); return 0; } logger.debug("Selected DA router ports: " + Arrays.toString(selectedDaRouterOfPortNumber)); double[] C = { daRouterCVectorMap.get(selectedDaRouterOfPortNumber[0]), daRouterCVectorMap.get(selectedDaRouterOfPortNumber[1]), }; if (C[0] >= 0) { daRouterOfPortNumber = selectedDaRouterOfPortNumber[0]; } else if (C[1] >= 0) { daRouterOfPortNumber = selectedDaRouterOfPortNumber[1]; } else { daRouterOfPortNumber = 0; } logger.debug("Summary:"); logger.debug("\t OFPortNumber: " + daRouterOfPortNumber); logger.debug("getOutOfPortNumberReactiveWithoutReference() end"); return (short) daRouterOfPortNumber; }