use of org.apache.commons.net.util.SubnetUtils in project deeplearning4j by deeplearning4j.
the class NetworkOrganizer method getSubset.
/**
* This method returns specified number of IP addresses from original list of addresses, that are NOT listen in primary collection
*
* @param numShards
* @param primary Collection of IP addresses that shouldn't be in result
* @return
*/
public List<String> getSubset(int numShards, Collection<String> primary) {
/**
* If netmask in unset, we'll use manual
*/
if (networkMask == null)
return getIntersections(numShards, primary);
List<String> addresses = new ArrayList<>();
SubnetUtils utils = new SubnetUtils(networkMask);
Collections.shuffle(informationCollection);
for (NetworkInformation information : informationCollection) {
for (String ip : information.getIpAddresses()) {
if (primary != null && primary.contains(ip))
continue;
if (utils.getInfo().isInRange(ip)) {
log.debug("Picked {} as {}", ip, primary == null ? "Shard" : "Backup");
addresses.add(ip);
}
if (addresses.size() >= numShards)
break;
}
if (addresses.size() >= numShards)
break;
}
return addresses;
}
use of org.apache.commons.net.util.SubnetUtils in project hadoop by apache.
the class NetUtils method getIPs.
/**
* Return an InetAddress for each interface that matches the
* given subnet specified using CIDR notation.
*
* @param subnet subnet specified using CIDR notation
* @param returnSubinterfaces
* whether to return IPs associated with subinterfaces
* @throws IllegalArgumentException if subnet is invalid
*/
public static List<InetAddress> getIPs(String subnet, boolean returnSubinterfaces) {
List<InetAddress> addrs = new ArrayList<InetAddress>();
SubnetInfo subnetInfo = new SubnetUtils(subnet).getInfo();
Enumeration<NetworkInterface> nifs;
try {
nifs = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
LOG.error("Unable to get host interfaces", e);
return addrs;
}
while (nifs.hasMoreElements()) {
NetworkInterface nif = nifs.nextElement();
// NB: adding addresses even if the nif is not up
addMatchingAddrs(nif, subnetInfo, addrs);
if (!returnSubinterfaces) {
continue;
}
Enumeration<NetworkInterface> subNifs = nif.getSubInterfaces();
while (subNifs.hasMoreElements()) {
addMatchingAddrs(subNifs.nextElement(), subnetInfo, addrs);
}
}
return addrs;
}
use of org.apache.commons.net.util.SubnetUtils in project hadoop by apache.
the class NfsExports method getMatch.
/**
* Loading a matcher from a string. The default access privilege is read-only.
* The string contains 1 or 2 parts, separated by whitespace characters, where
* the first part specifies the client hosts, and the second part (if
* existent) specifies the access privilege of the client hosts. I.e.,
*
* "client-hosts [access-privilege]"
*/
private static Match getMatch(String line) {
String[] parts = line.split("\\s+");
final String host;
AccessPrivilege privilege = AccessPrivilege.READ_ONLY;
switch(parts.length) {
case 1:
host = StringUtils.toLowerCase(parts[0]).trim();
break;
case 2:
host = StringUtils.toLowerCase(parts[0]).trim();
String option = parts[1].trim();
if ("rw".equalsIgnoreCase(option)) {
privilege = AccessPrivilege.READ_WRITE;
}
break;
default:
throw new IllegalArgumentException("Incorrectly formatted line '" + line + "'");
}
if (host.equals("*")) {
if (LOG.isDebugEnabled()) {
LOG.debug("Using match all for '" + host + "' and " + privilege);
}
return new AnonymousMatch(privilege);
} else if (CIDR_FORMAT_SHORT.matcher(host).matches()) {
if (LOG.isDebugEnabled()) {
LOG.debug("Using CIDR match for '" + host + "' and " + privilege);
}
return new CIDRMatch(privilege, new SubnetUtils(host).getInfo());
} else if (CIDR_FORMAT_LONG.matcher(host).matches()) {
if (LOG.isDebugEnabled()) {
LOG.debug("Using CIDR match for '" + host + "' and " + privilege);
}
String[] pair = host.split("/");
return new CIDRMatch(privilege, new SubnetUtils(pair[0], pair[1]).getInfo());
} else if (host.contains("*") || host.contains("?") || host.contains("[") || host.contains("]") || host.contains("(") || host.contains(")")) {
if (LOG.isDebugEnabled()) {
LOG.debug("Using Regex match for '" + host + "' and " + privilege);
}
return new RegexMatch(privilege, host);
} else if (HOSTNAME_FORMAT.matcher(host).matches()) {
if (LOG.isDebugEnabled()) {
LOG.debug("Using exact match for '" + host + "' and " + privilege);
}
return new ExactMatch(privilege, host);
} else {
throw new IllegalArgumentException("Invalid hostname provided '" + host + "'");
}
}
use of org.apache.commons.net.util.SubnetUtils in project cloudstack by apache.
the class BigSwitchBcfElement method applyNetworkACLs.
@Override
public boolean applyNetworkACLs(Network network, List<? extends NetworkACLItem> rules) throws ResourceUnavailableException {
SubnetUtils utils;
String cidr = null;
List<String> cidrList;
for (NetworkACLItem r : rules) {
if (r.getState() == NetworkACLItem.State.Revoke) {
continue;
}
cidrList = r.getSourceCidrList();
if (cidrList != null) {
if (cidrList.size() > 1 || !r.getSourcePortEnd().equals(r.getSourcePortStart())) {
throw new ResourceUnavailableException("One CIDR and one port only please.", Network.class, network.getId());
} else {
cidr = cidrList.get(0);
}
}
if (cidr == null || cidr.equalsIgnoreCase("0.0.0.0/0")) {
cidr = "";
} else {
utils = new SubnetUtils(cidr);
if (!utils.getInfo().getNetworkAddress().equals(utils.getInfo().getAddress())) {
throw new ResourceUnavailableException("Invalid CIDR in Network ACL rule.", Network.class, network.getId());
}
}
}
updateBcfRouter(network);
return true;
}
use of org.apache.commons.net.util.SubnetUtils in project cloudstack by apache.
the class NetUtils method isIpWithtInCidrRange.
public static boolean isIpWithtInCidrRange(final String ipAddress, final String cidr) {
if (!isValidIp(ipAddress)) {
return false;
}
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;
}
Aggregations