Search in sources :

Example 1 with SubnetInfo

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;
}
Also used : SubnetUtils(org.apache.commons.net.util.SubnetUtils) IntextIpMap(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.IntextIpMap) IpMappingKey(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.IpMappingKey) IpMapping(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.IpMapping) IpMap(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.ip.mapping.IpMap) IntextIpMap(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.IntextIpMap) SubnetInfo(org.apache.commons.net.util.SubnetUtils.SubnetInfo)

Example 2 with SubnetInfo

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;
    }
}
Also used : SubnetUtils(org.apache.commons.net.util.SubnetUtils) SubnetInfo(org.apache.commons.net.util.SubnetUtils.SubnetInfo)

Example 3 with SubnetInfo

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;
    }
}
Also used : SubnetUtils(org.apache.commons.net.util.SubnetUtils) SubnetInfo(org.apache.commons.net.util.SubnetUtils.SubnetInfo)

Example 4 with SubnetInfo

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;
}
Also used : SocketException(java.net.SocketException) SubnetUtils(org.apache.commons.net.util.SubnetUtils) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress) SubnetInfo(org.apache.commons.net.util.SubnetUtils.SubnetInfo)

Example 5 with SubnetInfo

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);
    }
}
Also used : SubnetUtils(org.apache.commons.net.util.SubnetUtils) UnknownHostException(java.net.UnknownHostException) SubnetInfo(org.apache.commons.net.util.SubnetUtils.SubnetInfo)

Aggregations

SubnetInfo (org.apache.commons.net.util.SubnetUtils.SubnetInfo)9 SubnetUtils (org.apache.commons.net.util.SubnetUtils)8 IntextIpMap (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.IntextIpMap)2 IpMap (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.ip.mapping.IpMap)2 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 NetworkInterface (java.net.NetworkInterface)1 SocketException (java.net.SocketException)1 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1 Nonnull (javax.annotation.Nonnull)1 IpAddress (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress)1 IpMapping (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.IpMapping)1 IpMappingKey (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.IpMappingKey)1 IpMapBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.ip.mapping.IpMapBuilder)1 IpMapKey (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.ip.mapping.IpMapKey)1