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;
}
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;
}
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;
}
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;
}
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
}
}
Aggregations