本文整理汇总了Java中org.apache.commons.net.util.SubnetUtils类的典型用法代码示例。如果您正苦于以下问题:Java SubnetUtils类的具体用法?Java SubnetUtils怎么用?Java SubnetUtils使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SubnetUtils类属于org.apache.commons.net.util包,在下文中一共展示了SubnetUtils类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: isIpWithtInCidrRange
import org.apache.commons.net.util.SubnetUtils; //导入依赖的package包/类
public static boolean isIpWithtInCidrRange(final String ipAddress, final String cidr) {
if (!isValidIp4(ipAddress)) {
return false;
}
if (!isValidIp4Cidr(cidr)) {
return false;
}
if (cidr.equals("0.0.0.0/0")) {
return true;
}
// 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;
}
示例2: findNodeByPodIP
import org.apache.commons.net.util.SubnetUtils; //导入依赖的package包/类
String findNodeByPodIP(String podIP) {
for (ImmutablePair<Netmask, ImmutableMap<NetworkAddress, String>> entry : podSubnetToNode) {
Netmask netmask = entry.getLeft();
ImmutableMap<NetworkAddress, String> networkToNode = entry.getRight();
// Computes the subnet that results from the netmask applied to the pod IP.
SubnetInfo podSubnetToCheck;
try {
podSubnetToCheck = new SubnetUtils(podIP, netmask.getValue()).getInfo();
} catch (IllegalArgumentException e) {
log.warn(e);
continue;
}
String networkAddress = podSubnetToCheck.getNetworkAddress();
String nodeName = networkToNode.get(new NetworkAddress(networkAddress));
if (nodeName != null) { // The cluster node is in charge of this pod IP subnet.
return nodeName;
}
}
return "";
}
示例3: getCollection
import org.apache.commons.net.util.SubnetUtils; //导入依赖的package包/类
/**
* returns the contents of the MachineList as a Collection<String>
* This can be used for testing
* @return contents of the MachineList
*/
@VisibleForTesting
public Collection<String> getCollection() {
Collection<String> list = new ArrayList<String>();
if (all) {
list.add("*");
} else {
if (ipAddresses != null) {
list.addAll(ipAddresses);
}
if (hostNames != null) {
list.addAll(hostNames);
}
if (cidrAddresses != null) {
for(SubnetUtils.SubnetInfo cidrAddress : cidrAddresses) {
list.add(cidrAddress.getCidrSignature());
}
}
}
return list;
}
示例4: validateRules
import org.apache.commons.net.util.SubnetUtils; //导入依赖的package包/类
/**
* Ensure that the rules conform to standard security group practices.
*/
private static void validateRules(List<Rule> rules) {
for (Rule rule : rules) {
validateRuleName(rule.name);
Protocol ruleProtocol = Protocol.validateProtocol(rule.protocol);
// IP range must be in CIDR notation or "*".
// creating new SubnetUtils to validate
if (!ANY.equals(rule.ipRangeCidr)) {
new SubnetUtils(rule.ipRangeCidr);
}
// port validation has no meaning for ICMP protocol
if (!Protocol.ICMPv4.equals(ruleProtocol) &&
!Protocol.ICMPv6.equals(ruleProtocol)) {
validatePorts(rule.ports);
}
}
}
示例5: isIpInValidRange
import org.apache.commons.net.util.SubnetUtils; //导入依赖的package包/类
/**
* If the ip is in the same network as the cidr, it returns true
*
* @param ipAddress The ip address under test
* @param cidr The cidr that defines the expected network address
* @param ipVersion
* @return
*/
public static boolean isIpInValidRange(String ipAddress,
String cidr, IPVersion ipVersion) {
if (!IPVersion.IPv4.equals(ipVersion)) {
throw new UnsupportedOperationException(
"Support for IPv6 IP address is not yet implemented");
}
SubnetUtils utils = new SubnetUtils(cidr);
utils.setInclusiveHostCount(true);
if (!utils.getInfo().getAddress().equals(utils.getInfo().getNetworkAddress())) {
String err = String.format("Invalid CIDR: %s. Host identifier of the"
+ " CIDR contains non zero bits.", cidr);
throw new RuntimeException(err);
}
return utils.getInfo().isInRange(ipAddress);
}
示例6: eval
import org.apache.commons.net.util.SubnetUtils; //导入依赖的package包/类
public void eval() {
String ip_string = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(input_ip.start, input_ip.end, input_ip.buffer);
String cidr_string = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(input_cidr.start, input_cidr.end, input_cidr.buffer);
int result = 0;
utils = new org.apache.commons.net.util.SubnetUtils(cidr_string);
if( utils.getInfo().isInRange( ip_string ) ){
result = 1;
}
else{
result = 0;
}
out.value = result;
}
示例7: 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);
}
示例8: 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));
}
示例9: 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));
}
示例10: testThatGetIpSetReturnsAListOfSubNetInfoObjects
import org.apache.commons.net.util.SubnetUtils; //导入依赖的package包/类
@Test
public void testThatGetIpSetReturnsAListOfSubNetInfoObjects() {
GetIPSetResult result = mock(GetIPSetResult.class);
IPSet ipSet = mock(IPSet.class);
when(result.getIPSet()).thenReturn(ipSet);
List<IPSetDescriptor> descriptors = new LinkedList<>();
descriptors.add(new IPSetDescriptor().withType(IPSetDescriptorType.IPV4).withValue("192.168.0.1/32"));
descriptors.add(new IPSetDescriptor().withType(IPSetDescriptorType.IPV4).withValue("192.168.0.2/32"));
descriptors.add(new IPSetDescriptor().withType(IPSetDescriptorType.IPV4).withValue("192.168.0.3/32"));
when(ipSet.getIPSetDescriptors()).thenReturn(descriptors);
when(awswaf.getIPSet(isA(GetIPSetRequest.class))).thenReturn(result);
List<SubnetUtils.SubnetInfo> list = processor.getIpSet("DOES NOT MATTER");
assertTrue("The list should contain 3 items", list.size() == 3);
}
示例11: 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" ) );
}
示例12: execute
import org.apache.commons.net.util.SubnetUtils; //导入依赖的package包/类
public void execute() throws EnvironmentModificationException, PeerException
{
Set<Peer> newPeers = peerManager.resolve( topology.getAllPeers() );
//remove already participating peers
newPeers.removeAll( environment.getPeers() );
if ( newPeers.isEmpty() )
{
return;
}
//obtain reserved net resources
final Map<Peer, UsedNetworkResources> reservedNetResources = obtainReservedNetResources( newPeers );
//check availability of network resources
SubnetUtils subnetUtils = new SubnetUtils( environment.getSubnetCidr() );
final String containerSubnet = subnetUtils.getInfo().getNetworkAddress();
checkResourceAvailablility( reservedNetResources, containerSubnet );
//reserve network resources
reserveNetworkResources( newPeers, containerSubnet );
}
示例13: getNetworkIPs
import org.apache.commons.net.util.SubnetUtils; //导入依赖的package包/类
/**
* Takes the interfaceIPs and fetches every IP which can be assigned on their network
* @param networkIPs The IPs which are assigned to the Network Interfaces
* @return Every single IP which can be assigned on the Networks the computer is connected to
*/
private static Queue<String> getNetworkIPs(TreeSet<String> interfaceIPs) {
Queue<String> networkIPs = new LinkedBlockingQueue<String>();
for (Iterator<String> it = interfaceIPs.iterator(); it.hasNext();) {
try {
// gets every ip which can be assigned on the given network
SubnetUtils utils = new SubnetUtils(it.next());
String[] addresses = utils.getInfo().getAllAddresses();
for (int i = 0; i < addresses.length; i++) {
networkIPs.add(addresses[i]);
}
} catch (Exception ex) {
}
}
return networkIPs;
}
示例14: 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;
}
示例15: apply
import org.apache.commons.net.util.SubnetUtils; //导入依赖的package包/类
@Override
public Object apply(List<Object> list) {
if(list.size() < 2) {
throw new IllegalStateException("IN_SUBNET expects at least two args: [ip, cidr1, cidr2, ...]"
+ " where cidr is the subnet mask in cidr form"
);
}
String ip = (String) list.get(0);
if(ip == null) {
return false;
}
boolean inSubnet = false;
for(int i = 1;i < list.size() && !inSubnet;++i) {
String cidr = (String) list.get(i);
if(cidr == null) {
continue;
}
inSubnet |= new SubnetUtils(cidr).getInfo().isInRange(ip);
}
return inSubnet;
}