use of org.apache.commons.net.util.SubnetUtils.SubnetInfo in project netvirt by opendaylight.
the class NaptManager method checkIpMap.
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;
}
use of org.apache.commons.net.util.SubnetUtils.SubnetInfo in project cloudbreak by hortonworks.
the class AwsResourceConnector method getSubnetCidrInRange.
private String getSubnetCidrInRange(Vpc vpc, Iterable<String> subnetCidrs, int start, int end) {
SubnetInfo vpcInfo = new SubnetUtils(vpc.getCidrBlock()).getInfo();
String lowProbe = incrementIp(vpcInfo.getLowAddress());
String highProbe = new SubnetUtils(toSubnetCidr(lowProbe)).getInfo().getHighAddress();
// start from the target subnet
for (int i = 0; i < start - 1; i++) {
lowProbe = incrementIp(lowProbe);
highProbe = incrementIp(highProbe);
}
boolean foundProbe = false;
for (int i = start; i < end; i++) {
boolean overlapping = false;
for (String subnetCidr : subnetCidrs) {
SubnetInfo subnetInfo = new SubnetUtils(subnetCidr).getInfo();
if (isInRange(lowProbe, subnetInfo) || isInRange(highProbe, subnetInfo)) {
overlapping = true;
break;
}
}
if (overlapping) {
lowProbe = incrementIp(lowProbe);
highProbe = incrementIp(highProbe);
} else {
foundProbe = true;
break;
}
}
if (foundProbe && isInRange(highProbe, vpcInfo)) {
String subnet = toSubnetCidr(lowProbe);
LOGGER.info("The following subnet cidr found: {} for VPC: {}", subnet, vpc.getVpcId());
return subnet;
} else {
return null;
}
}
use of org.apache.commons.net.util.SubnetUtils.SubnetInfo in project cloudbreak by hortonworks.
the class SubnetValidator method isValid.
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
if (value == null) {
return true;
} else if (value.isEmpty()) {
return false;
}
try {
SubnetInfo info = new SubnetUtils(value).getInfo();
long addr = toLong(info.getAddress());
long lowerAddr = toLong(info.getLowAddress()) - 1;
if (addr != lowerAddr) {
return false;
}
Ip ip = new Ip(info.getAddress());
return (new Ip("10.0.0.0").compareTo(ip) <= 0 && new Ip("10.255.255.255").compareTo(ip) >= 0) || (new Ip("172.16.0.0").compareTo(ip) <= 0 && new Ip("172.31.255.255").compareTo(ip) >= 0) || (new Ip("192.168.0.0").compareTo(ip) <= 0 && new Ip("192.168.255.255").compareTo(ip) >= 0);
} catch (RuntimeException ignored) {
return false;
}
}
use of org.apache.commons.net.util.SubnetUtils.SubnetInfo 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.SubnetInfo in project netvirt by opendaylight.
the class DhcpPktHandler method setNonNakOptions.
private void setNonNakOptions(DHCP pkt, DhcpInfo dhcpInfo) {
pkt.setOptionInt(DHCPConstants.OPT_LEASE_TIME, dhcpMgr.getDhcpLeaseTime());
if (dhcpMgr.getDhcpDefDomain() != null) {
pkt.setOptionString(DHCPConstants.OPT_DOMAIN_NAME, dhcpMgr.getDhcpDefDomain());
}
if (dhcpMgr.getDhcpLeaseTime() > 0) {
pkt.setOptionInt(DHCPConstants.OPT_REBINDING_TIME, dhcpMgr.getDhcpRebindingTime());
pkt.setOptionInt(DHCPConstants.OPT_RENEWAL_TIME, dhcpMgr.getDhcpRenewalTime());
}
SubnetUtils util = null;
SubnetInfo info = null;
util = new SubnetUtils(dhcpInfo.getCidr());
info = util.getInfo();
String gwIp = dhcpInfo.getGatewayIp();
List<String> dnServers = dhcpInfo.getDnsServers();
try {
/*
* setParameterListOptions may have initialized some of these
* options to maintain order. If we can't fill them, unset to avoid
* sending wrong information in reply.
*/
if (gwIp != null) {
pkt.setOptionInetAddr(DHCPConstants.OPT_ROUTERS, gwIp);
} else {
pkt.unsetOption(DHCPConstants.OPT_ROUTERS);
}
if (info != null) {
pkt.setOptionInetAddr(DHCPConstants.OPT_SUBNET_MASK, info.getNetmask());
pkt.setOptionInetAddr(DHCPConstants.OPT_BROADCAST_ADDRESS, info.getBroadcastAddress());
} else {
pkt.unsetOption(DHCPConstants.OPT_SUBNET_MASK);
pkt.unsetOption(DHCPConstants.OPT_BROADCAST_ADDRESS);
}
if (dnServers != null && dnServers.size() > 0) {
pkt.setOptionStrAddrs(DHCPConstants.OPT_DOMAIN_NAME_SERVERS, dnServers);
} else {
pkt.unsetOption(DHCPConstants.OPT_DOMAIN_NAME_SERVERS);
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
LOG.warn("Failed to set option", e);
}
}
Aggregations