本文整理匯總了Java中org.apache.commons.net.util.SubnetUtils.getInfo方法的典型用法代碼示例。如果您正苦於以下問題:Java SubnetUtils.getInfo方法的具體用法?Java SubnetUtils.getInfo怎麽用?Java SubnetUtils.getInfo使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.commons.net.util.SubnetUtils
的用法示例。
在下文中一共展示了SubnetUtils.getInfo方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testThatFilterAndTruncateViolatorsFiltersIPsInDoNotBlockRangeSet
import org.apache.commons.net.util.SubnetUtils; //導入方法依賴的package包/類
@Test
public void testThatFilterAndTruncateViolatorsFiltersIPsInDoNotBlockRangeSet() {
config.setRateLimitViolationBlacklistPeriodInMinutes(1);
RangeSet<Integer> doNotAutoBlockIpRangeSet = TreeRangeSet.create();
SubnetUtils subnetUtils = new SubnetUtils("50.39.100.193/32");
subnetUtils.setInclusiveHostCount(true);
SubnetUtils.SubnetInfo subnetInfo = subnetUtils.getInfo();
Integer lowIpAsInt = subnetInfo.asInteger(subnetInfo.getLowAddress());
Integer highIpAsInt = subnetInfo.asInteger(subnetInfo.getHighAddress());
doNotAutoBlockIpRangeSet.add(Range.closed(lowIpAsInt, highIpAsInt));
Map<String, ViolationMetaData> violators = new HashMap<>();
violators.put("50.39.100.193", new ViolationMetaData(new Date(), 2));
Map<String, ViolationMetaData> actual = processor.filterAndTruncateViolators(config, doNotAutoBlockIpRangeSet,
violators);
assertTrue("The violators map should be empty after filtering", actual.size() == 0);
}
示例2: testThatProcessViolatorsDoesNotReAddWhatIsAlreadyBlocked
import org.apache.commons.net.util.SubnetUtils; //導入方法依賴的package包/類
@Test
public void testThatProcessViolatorsDoesNotReAddWhatIsAlreadyBlocked() {
String fakeIpSet = "foo";
config.setRateLimitAutoBlackListIpSetId(fakeIpSet);
List<SubnetUtils.SubnetInfo> currentlyAutoBlocked = new LinkedList<>();
SubnetUtils subnetUtils = new SubnetUtils("192.168.0.1/32");
subnetUtils.setInclusiveHostCount(true);
SubnetUtils.SubnetInfo info = subnetUtils.getInfo();
currentlyAutoBlocked.add(info);
doReturn(currentlyAutoBlocked).when(processor).getIpSet(fakeIpSet);
Map<String, ViolationMetaData> violators = new HashMap<>();
violators.put("192.168.0.1", new ViolationMetaData(new Date(), 10));
processor.processViolators(config, violators);
verify(awswaf, times(0)).updateIPSet(isA(UpdateIPSetRequest.class));
}
示例3: testThatProcessViolatorsAddsNewIpsToIpSet
import org.apache.commons.net.util.SubnetUtils; //導入方法依賴的package包/類
@Test
public void testThatProcessViolatorsAddsNewIpsToIpSet() {
String fakeIpSet = "foo";
config.setRateLimitAutoBlackListIpSetId(fakeIpSet);
doReturn(new LinkedList<>()).when(processor).getIpSet(fakeIpSet);
Map<String, ViolationMetaData> violators = new HashMap<>();
violators.put("192.168.0.1", new ViolationMetaData(new Date(), 10));
processor.processViolators(config, violators);
List<IPSetUpdate> expectedUpdates = new LinkedList<>();
SubnetUtils subnetUtils = new SubnetUtils("192.168.0.1/32");
subnetUtils.setInclusiveHostCount(true);
SubnetUtils.SubnetInfo info = subnetUtils.getInfo();
IPSetDescriptor descriptor = new IPSetDescriptor()
.withType(IPSetDescriptorType.IPV4)
.withValue(info.getCidrSignature());
IPSetUpdate update = new IPSetUpdate().withIPSetDescriptor(descriptor).withAction(ChangeAction.INSERT);
expectedUpdates.add(update);
verify(awswaf, times(1)).updateIPSet(new UpdateIPSetRequest()
.withIPSetId(config.getRateLimitAutoBlackListIpSetId())
.withUpdates(expectedUpdates)
.withChangeToken(CHANGE_TOKEN));
}
示例4: testIp
import org.apache.commons.net.util.SubnetUtils; //導入方法依賴的package包/類
@Test
public void testIp()
{
SubnetUtils utils = new SubnetUtils( "10.12.0.1/24" );
final SubnetUtils.SubnetInfo info = utils.getInfo();
assertEquals( "10.12.0.1", info.getAddress() );
assertEquals( "10.12.0.0", info.getNetworkAddress() );
assertEquals( "10.12.0.255", info.getBroadcastAddress() );
assertEquals( 254, info.getAddressCount() );
assertEquals( "10.12.0.1/24", info.getCidrSignature() );
assertEquals( "10.12.0.1", info.getLowAddress() );
assertEquals( "10.12.0.254", info.getHighAddress() );
assertEquals( "255.255.255.0", info.getNetmask() );
assertEquals( true, info.isInRange( "10.12.0.100" ) );
assertEquals( false, info.isInRange( "10.11.0.1" ) );
}
示例5: fromMessage
import org.apache.commons.net.util.SubnetUtils; //導入方法依賴的package包/類
public static IpRangeInventory fromMessage(APIAddIpRangeByNetworkCidrMsg msg) {
SubnetUtils utils = new SubnetUtils(msg.getNetworkCidr());
SubnetInfo subnet = utils.getInfo();
IpRangeInventory ipr = new IpRangeInventory();
ipr.setNetworkCidr(msg.getNetworkCidr());
ipr.setName(msg.getName());
ipr.setDescription(msg.getDescription());
String gateway = subnet.getLowAddress();
String startIp = NetworkUtils.longToIpv4String(NetworkUtils.ipv4StringToLong(subnet.getLowAddress()) + 1);
String endIp = subnet.getHighAddress();
ipr.setGateway(gateway);
ipr.setStartIp(startIp);
ipr.setEndIp(endIp);
ipr.setNetmask(subnet.getNetmask());
ipr.setL3NetworkUuid(msg.getL3NetworkUuid());
ipr.setUuid(msg.getResourceUuid());
return ipr;
}
示例6: validGatewayIP
import org.apache.commons.net.util.SubnetUtils; //導入方法依賴的package包/類
boolean validGatewayIP(NeutronSubnet subnet, String ipAddress) {
try {
SubnetUtils util = new SubnetUtils(subnet.getCidr());
SubnetInfo info = util.getInfo();
boolean inRange = info.isInRange(ipAddress);
if (!inRange) {
return false;
} else {
// ip available in allocation pool
Iterator<NeutronSubnet_IPAllocationPool> i = subnet.getAllocationPools().iterator();
while (i.hasNext()) {
NeutronSubnet_IPAllocationPool pool = i.next();
if (pool.contains(ipAddress)) {
return true;
}
}
return true;
}
} catch (Exception e) {
LOGGER.error("Exception : " + e);
return false;
}
}
示例7: getSubnetArray
import org.apache.commons.net.util.SubnetUtils; //導入方法依賴的package包/類
public final static SubnetInfo[] getSubnetArray(String ips)
throws IOException {
if (ips == null)
return null;
StringReader sr = new StringReader(ips);
try {
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);
}
}
示例8: testThatProcessViolatorsRemovesIpsThatShouldNoLongerBeInIpSet
import org.apache.commons.net.util.SubnetUtils; //導入方法依賴的package包/類
@Test
public void testThatProcessViolatorsRemovesIpsThatShouldNoLongerBeInIpSet() {
String fakeIpSet = "foo";
config.setRateLimitAutoBlackListIpSetId(fakeIpSet);
List<SubnetUtils.SubnetInfo> currentlyAutoBlocked = new LinkedList<>();
SubnetUtils subnetUtils = new SubnetUtils("192.168.0.1/32");
subnetUtils.setInclusiveHostCount(true);
SubnetUtils.SubnetInfo info = subnetUtils.getInfo();
currentlyAutoBlocked.add(info);
doReturn(currentlyAutoBlocked).when(processor).getIpSet(fakeIpSet);
Map<String, ViolationMetaData> violators = new HashMap<>();
processor.processViolators(config, violators);
List<IPSetUpdate> expectedUpdates = new LinkedList<>();
IPSetDescriptor descriptor = new IPSetDescriptor()
.withType(IPSetDescriptorType.IPV4)
.withValue(info.getCidrSignature());
IPSetUpdate update = new IPSetUpdate().withIPSetDescriptor(descriptor).withAction(ChangeAction.DELETE);
expectedUpdates.add(update);
verify(awswaf, times(1)).updateIPSet(new UpdateIPSetRequest()
.withIPSetId(config.getRateLimitAutoBlackListIpSetId())
.withUpdates(expectedUpdates)
.withChangeToken(CHANGE_TOKEN));
}
示例9: testThatGetDoNotBlockRangeSetBuildsARangeSet
import org.apache.commons.net.util.SubnetUtils; //導入方法依賴的package包/類
@Test
public void testThatGetDoNotBlockRangeSetBuildsARangeSet() {
// stub the black list
String black = "black";
config.setManualBlackListIpSetId(black);
List<SubnetUtils.SubnetInfo> blackList = Lists.newLinkedList();
// 192.168.0.0-192.168.0.255
SubnetUtils bSubnetUtils = new SubnetUtils("192.168.0.0/24");
bSubnetUtils.setInclusiveHostCount(true);
SubnetUtils.SubnetInfo bInfo = bSubnetUtils.getInfo();
blackList.add(bInfo);
doReturn(blackList).when(processor).getIpSet(black);
// stub the white list
String white = "white";
config.setManualWhiteListIpSetId(white);
List<SubnetUtils.SubnetInfo> whiteList = Lists.newLinkedList();
// 192.150.0.0-192.150.0.255
SubnetUtils wSubnetUtils = new SubnetUtils("192.150.0.0/24");
wSubnetUtils.setInclusiveHostCount(true);
SubnetUtils.SubnetInfo wInfo = wSubnetUtils.getInfo();
whiteList.add(wInfo);
doReturn(whiteList).when(processor).getIpSet(white);
RangeSet<Integer> doNotBlock = processor.getDoNotBlockRangeSet(config);
SubnetUtils utils = new SubnetUtils("0.0.0.0/24");
Integer onTheBlackListLow = utils.getInfo().asInteger("192.168.0.0");
Integer onTheBlackListHigh = utils.getInfo().asInteger("192.168.0.255");
Integer onTheWhiteListLow = utils.getInfo().asInteger("192.150.0.0");
Integer onTheWhiteListHigh = utils.getInfo().asInteger("192.150.0.255");
Integer shouldBeBlocked = utils.getInfo().asInteger("192.168.1.1");
assertTrue("192.168.0.0 is in the ip range represented by 192.168.0.0/24 and therefor should be in the doNotBlock range set", doNotBlock.contains(onTheBlackListLow));
assertTrue("192.168.0.255 is in the ip range represented by 192.168.0.0/24 and therefor should be in the doNotBlock range set", doNotBlock.contains(onTheBlackListHigh));
assertTrue("192.150.0.0 is in the ip range represented by 192.150.0.0/24 and therefor should be in the doNotBlock range set", doNotBlock.contains(onTheWhiteListLow));
assertTrue("192.150.0.255 is in the ip range represented by 192.150.0.0/24 and therefor should be in the doNotBlock range set", doNotBlock.contains(onTheWhiteListHigh));
assertFalse("192.168.1.1 is not in 192.168.0.0/24 or 192.150.0.0/24 and should not be in the doNotBlock range set", doNotBlock.contains(shouldBeBlocked));
}
示例10: registerMapping
import org.apache.commons.net.util.SubnetUtils; //導入方法依賴的package包/類
/**
* This method is used to inform this service of what external IP address to be used
* as mapping when requested one for the internal IP address given in the input.
*
* @param segmentId – segmentation in which the mapping to be used. Eg; routerid
* @param internal subnet prefix or ip address
* @param external subnet prefix or ip address
*/
public void registerMapping(long segmentId, IPAddress internal, IPAddress external) {
LOG.debug("registerMapping : called with segmentid {}, internalIp {}, prefix {}, externalIp {} "
+ "and prefix {} ", segmentId, internal.getIpAddress(),
internal.getPrefixLength(), external.getIpAddress(), external.getPrefixLength());
// Create Pool per ExternalIp and not for all IPs in the subnet.
// Create new Pools during getExternalAddressMapping if exhausted.
String externalIpPool;
// subnet case
if (external.getPrefixLength() != 0 && external.getPrefixLength() != NatConstants.DEFAULT_PREFIX) {
String externalSubnet = external.getIpAddress() + "/" + external.getPrefixLength();
LOG.debug("registerMapping : externalSubnet is : {}", externalSubnet);
SubnetUtils subnetUtils = new SubnetUtils(externalSubnet);
SubnetInfo subnetInfo = subnetUtils.getInfo();
externalIpPool = subnetInfo.getLowAddress();
} else { // ip case
externalIpPool = external.getIpAddress();
}
createNaptPortPool(externalIpPool);
// Store the ip to ip map in Operational DS
String internalIp = internal.getIpAddress();
if (internal.getPrefixLength() != 0) {
internalIp = internal.getIpAddress() + "/" + internal.getPrefixLength();
}
String externalIp = external.getIpAddress();
if (external.getPrefixLength() != 0) {
externalIp = external.getIpAddress() + "/" + external.getPrefixLength();
}
updateCounter(segmentId, externalIp, true);
//update the actual ip-map
IpMap ipm = new IpMapBuilder().setKey(new IpMapKey(internalIp)).setInternalIp(internalIp)
.setExternalIp(externalIp).build();
MDSALUtil.syncWrite(dataBroker, LogicalDatastoreType.OPERATIONAL,
getIpMapIdentifier(segmentId, internalIp), ipm);
LOG.debug("registerMapping : registerMapping exit after updating DS with internalIP {}, externalIP {}",
internalIp, externalIp);
}
示例11: checkIpMap
import org.apache.commons.net.util.SubnetUtils; //導入方法依賴的package包/類
protected String checkIpMap(long segmentId, String internalIp) {
LOG.debug("checkIpMap : called with segmentId {} and internalIp {}", segmentId, internalIp);
String externalIp;
// check if ip-map node is there
InstanceIdentifierBuilder<IpMapping> idBuilder =
InstanceIdentifier.builder(IntextIpMap.class).child(IpMapping.class, new IpMappingKey(segmentId));
InstanceIdentifier<IpMapping> id = idBuilder.build();
Optional<IpMapping> ipMapping = MDSALUtil.read(dataBroker, LogicalDatastoreType.OPERATIONAL, id);
if (ipMapping.isPresent()) {
List<IpMap> ipMaps = ipMapping.get().getIpMap();
for (IpMap ipMap : ipMaps) {
if (ipMap.getInternalIp().equals(internalIp)) {
LOG.debug("checkIpMap : IpMap : {}", ipMap);
externalIp = ipMap.getExternalIp();
LOG.debug("checkIpMap : successfully returning externalIp {}", externalIp);
return externalIp;
} else if (ipMap.getInternalIp().contains("/")) { // subnet case
SubnetUtils subnetUtils = new SubnetUtils(ipMap.getInternalIp());
SubnetInfo subnetInfo = subnetUtils.getInfo();
if (subnetInfo.isInRange(internalIp)) {
LOG.debug("checkIpMap : internalIp {} found to be IpMap of internalIpSubnet {}",
internalIp, ipMap.getInternalIp());
externalIp = ipMap.getExternalIp();
LOG.debug("checkIpMap : checkIpMap successfully returning externalIp {}", externalIp);
return externalIp;
}
}
}
}
// return null if not found
LOG.error("checkIpMap : failed, returning NULL for segmentId {} and internalIp {}",
segmentId, internalIp);
return null;
}
示例12: convertToClasslessRouteOption
import org.apache.commons.net.util.SubnetUtils; //導入方法依賴的package包/類
@Nonnull
protected byte[] convertToClasslessRouteOption(String dest, String router) {
ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
if (dest == null || router == null) {
return new byte[0];
}
//get prefix
Short prefix = null;
String[] parts = dest.split("/");
if (parts.length < 2) {
prefix = (short) 0;
} else {
prefix = Short.valueOf(parts[1]);
}
byteArray.write(prefix.byteValue());
SubnetUtils util = new SubnetUtils(dest);
SubnetInfo info = util.getInfo();
String strNetAddr = info.getNetworkAddress();
try {
byte[] netAddr = InetAddress.getByName(strNetAddr).getAddress();
//Strip any trailing 0s from netAddr
for (int i = 0; i < netAddr.length;i++) {
if (netAddr[i] != 0) {
byteArray.write(netAddr,i,1);
}
}
byteArray.write(InetAddress.getByName(router).getAddress());
} catch (IOException e) {
return new byte[0];
}
return byteArray.toByteArray();
}
示例13: CriteriaIP
import org.apache.commons.net.util.SubnetUtils; //導入方法依賴的package包/類
public CriteriaIP(String ipAddress, String mask, PacketDirection direction)
{
this.ipAddress = ipAddress;
this.direction = direction;
SubnetUtils subnetUtils = new SubnetUtils(ipAddress, mask);
subnetUtils.setInclusiveHostCount(true); //to allow one specific address with mask 255.255.255.255
subnetInfo = subnetUtils.getInfo();
}
示例14: validate
import org.apache.commons.net.util.SubnetUtils; //導入方法依賴的package包/類
private void validate(APIAddIpRangeByNetworkCidrMsg msg) {
try {
SubnetUtils utils = new SubnetUtils(msg.getNetworkCidr());
utils.setInclusiveHostCount(false);
SubnetInfo subnet = utils.getInfo();
if (subnet.getAddressCount() == 0) {
throw new ApiMessageInterceptionException(argerr("%s is not an allowed network cidr, because it doesn't have usable ip range", msg.getNetworkCidr()));
}
} catch (IllegalArgumentException e) {
throw new ApiMessageInterceptionException(argerr("%s is not a valid network cidr", msg.getNetworkCidr()));
}
IpRangeInventory ipr = IpRangeInventory.fromMessage(msg);
validate(ipr);
}
示例15: isCidrOverlap
import org.apache.commons.net.util.SubnetUtils; //導入方法依賴的package包/類
public static boolean isCidrOverlap(String cidr1, String cidr2) {
DebugUtils.Assert(isCidr(cidr1), String.format("%s is not a cidr", cidr1));
DebugUtils.Assert(isCidr(cidr2), String.format("%s is not a cidr", cidr2));
SubnetUtils su1 = new SubnetUtils(cidr1);
SubnetUtils su2 = new SubnetUtils(cidr2);
SubnetUtils.SubnetInfo info1 = su1.getInfo();
SubnetUtils.SubnetInfo info2 = su2.getInfo();
return isIpv4RangeOverlap(info1.getLowAddress(), info1.getHighAddress(), info2.getLowAddress(), info2.getHighAddress());
}