Search in sources :

Example 1 with SubnetUtils

use of org.apache.commons.net.util.SubnetUtils in project android by cSploit.

the class Network method initNetworkInterface.

public boolean initNetworkInterface(String iface) {
    InterfaceAddress ifaceAddress = null;
    try {
        if (iface == null)
            iface = getAvailableInterfaces().get(0);
        mInterface = NetworkInterface.getByName(iface);
        if (mInterface.getInterfaceAddresses().isEmpty()) {
            return false;
        }
        for (InterfaceAddress ia : mInterface.getInterfaceAddresses()) {
            if (Patterns.IP_ADDRESS.matcher(ia.getAddress().getHostAddress()).matches()) {
                ifaceAddress = ia;
                Logger.warning("interfaceAddress: " + ia.getAddress().getHostAddress() + "/" + Short.toString(ia.getNetworkPrefixLength()));
                break;
            } else
                Logger.error("not valid ip: " + ia.getAddress().getHostAddress() + "/" + Short.toString(ia.getNetworkPrefixLength()));
        }
        if (ifaceAddress == null) {
            return false;
        }
        SubnetUtils su = new SubnetUtils(// get(1) == ipv4
        ifaceAddress.getAddress().getHostAddress() + "/" + Short.toString(ifaceAddress.getNetworkPrefixLength()));
        mLocal = new IP4Address(su.getInfo().getAddress());
        mNetmask = new IP4Address(su.getInfo().getNetmask());
        mBase = new IP4Address(su.getInfo().getNetworkAddress());
        updateGateway();
        return true;
    } catch (Exception e) {
        Logger.error("Error: " + e.getLocalizedMessage());
    }
    return false;
}
Also used : SubnetUtils(org.apache.commons.net.util.SubnetUtils)

Example 2 with SubnetUtils

use of org.apache.commons.net.util.SubnetUtils in project cloudstack by apache.

the class BigSwitchBcfUtils method listACLbyNetwork.

public List<AclData> listACLbyNetwork(Network network) {
    List<AclData> aclList = new ArrayList<AclData>();
    List<FirewallRuleVO> fwRules;
    fwRules = _fwRulesDao.listByNetworkAndPurposeAndNotRevoked(network.getId(), Purpose.Firewall);
    List<FirewallRulesCidrsVO> fwCidrList = null;
    SubnetUtils utils;
    for (FirewallRuleVO rule : fwRules) {
        AclData acl = new AclData();
        acl.setId(rule.getUuid());
        // CloudStack Firewall interface does not have priority
        acl.setPriority((int) rule.getId());
        acl.setIpProto(rule.getProtocol());
        String cidr = null;
        Integer port = rule.getSourcePortStart();
        fwCidrList = _fwCidrsDao.listByFirewallRuleId(rule.getId());
        if (fwCidrList != null) {
            if (fwCidrList.size() > 1 || !rule.getSourcePortEnd().equals(port)) {
                continue;
            } else {
                cidr = fwCidrList.get(0).getCidr();
            }
        }
        if (cidr == null || cidr.equalsIgnoreCase("0.0.0.0/0")) {
            cidr = "";
        } else {
            utils = new SubnetUtils(cidr);
            if (!utils.getInfo().getNetworkAddress().equals(utils.getInfo().getAddress())) {
                continue;
            }
        }
        acl.setSource(acl.new AclNetwork(cidr, port));
        acl.setAction("permit");
        aclList.add(acl);
    }
    List<NetworkACLItemVO> aclItems;
    List<NetworkACLItemCidrsVO> aclCidrList;
    if (network.getNetworkACLId() != null) {
        aclItems = _aclItemDao.listByACL(network.getNetworkACLId());
        for (NetworkACLItem item : aclItems) {
            AclData acl = new AclData();
            acl.setId(item.getUuid());
            acl.setPriority(item.getNumber());
            acl.setIpProto(item.getProtocol());
            // currently BCF supports single cidr policy
            String cidr = null;
            // currently BCF supports single port policy
            Integer port = item.getSourcePortStart();
            aclCidrList = _aclItemCidrsDao.listByNetworkACLItemId(item.getId());
            if (aclCidrList != null) {
                if (aclCidrList.size() > 1 || !item.getSourcePortEnd().equals(port)) {
                    continue;
                } else {
                    cidr = aclCidrList.get(0).getCidr();
                }
            }
            if (cidr == null || cidr.equalsIgnoreCase("0.0.0.0/0")) {
                cidr = "";
            } else {
                utils = new SubnetUtils(cidr);
                if (!utils.getInfo().getNetworkAddress().equals(utils.getInfo().getAddress())) {
                    continue;
                }
            }
            acl.setSource(acl.new AclNetwork(cidr, port));
            acl.setAction(item.getAction().name());
            aclList.add(acl);
        }
    }
    return aclList;
}
Also used : SubnetUtils(org.apache.commons.net.util.SubnetUtils) ArrayList(java.util.ArrayList) NetworkACLItemCidrsVO(com.cloud.network.vpc.NetworkACLItemCidrsVO) FirewallRuleVO(com.cloud.network.rules.FirewallRuleVO) NetworkACLItemVO(com.cloud.network.vpc.NetworkACLItemVO) NetworkACLItem(com.cloud.network.vpc.NetworkACLItem) FirewallRulesCidrsVO(com.cloud.network.dao.FirewallRulesCidrsVO)

Example 3 with SubnetUtils

use of org.apache.commons.net.util.SubnetUtils in project cloudstack by apache.

the class BigSwitchBcfElement method applyFWRules.

@Override
public boolean applyFWRules(Network network, List<? extends FirewallRule> rules) throws ResourceUnavailableException {
    SubnetUtils utils;
    String cidr = null;
    List<String> cidrList;
    for (FirewallRule r : rules) {
        if (r.getState() == FirewallRule.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 Firewall rule.", Network.class, network.getId());
            }
        }
    }
    updateBcfRouter(network);
    return true;
}
Also used : SubnetUtils(org.apache.commons.net.util.SubnetUtils) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) FirewallRule(com.cloud.network.rules.FirewallRule)

Example 4 with SubnetUtils

use of org.apache.commons.net.util.SubnetUtils in project cloudstack by apache.

the class NetUtils method isNetworkorBroadcastIP.

public static boolean isNetworkorBroadcastIP(String ip, String netmask) {
    String cidr = getCidrFromGatewayAndNetmask(ip, netmask);
    final SubnetUtils subnetUtils = new SubnetUtils(cidr);
    subnetUtils.setInclusiveHostCount(false);
    final boolean isInRange = subnetUtils.getInfo().isInRange(ip);
    return !isInRange;
}
Also used : SubnetUtils(org.apache.commons.net.util.SubnetUtils)

Example 5 with SubnetUtils

use of org.apache.commons.net.util.SubnetUtils in project sling by apache.

the class WildcardHelperTest method testNullValues.

@Test
public void testNullValues() {
    SubnetUtils s = new SubnetUtils("1.2.3.4/10");
    s = new SubnetUtils("1.2.3.4", "255.255.0.0");
    try {
        WildcardHelper.wildcardAsRegex(null);
        fail("should complain");
    } catch (IllegalArgumentException iae) {
    // ok
    }
    try {
        WildcardHelper.matchesWildcard(null, "foo");
        fail("should complain");
    } catch (IllegalArgumentException iae) {
    // ok
    }
    try {
        WildcardHelper.matchesWildcard("foo", null);
        fail("should complain");
    } catch (IllegalArgumentException iae) {
    // ok
    }
}
Also used : SubnetUtils(org.apache.commons.net.util.SubnetUtils) Test(org.junit.Test)

Aggregations

SubnetUtils (org.apache.commons.net.util.SubnetUtils)10 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)2 NetworkACLItem (com.cloud.network.vpc.NetworkACLItem)2 FirewallRulesCidrsVO (com.cloud.network.dao.FirewallRulesCidrsVO)1 FirewallRule (com.cloud.network.rules.FirewallRule)1 FirewallRuleVO (com.cloud.network.rules.FirewallRuleVO)1 NetworkACLItemCidrsVO (com.cloud.network.vpc.NetworkACLItemCidrsVO)1 NetworkACLItemVO (com.cloud.network.vpc.NetworkACLItemVO)1 InetAddress (java.net.InetAddress)1 NetworkInterface (java.net.NetworkInterface)1 SocketException (java.net.SocketException)1 ArrayList (java.util.ArrayList)1 SubnetInfo (org.apache.commons.net.util.SubnetUtils.SubnetInfo)1 NetworkInformation (org.deeplearning4j.spark.models.sequencevectors.primitives.NetworkInformation)1 Test (org.junit.Test)1