csip.IpMatcher Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of csip-core Show documentation
Show all versions of csip-core Show documentation
The Cloud Services Integration Platform is a SoA implementation to offer a Model-as-a-Service framework, Application Programming Interface, deployment infrastructure, and service implementations for environmental modeling.
/*
* $Id: IpMatcher.java aac263945148 2019-03-15 od $
*
* This file is part of the Cloud Services Integration Platform (CSIP),
* a Model-as-a-Service framework, API and application suite.
*
* 2012-2019, Olaf David and others, OMSLab, Colorado State University.
*
* OMSLab licenses this file to you under the MIT license.
* See the LICENSE file in the project root for more information.
*/
package csip;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
/**
* Matches an IP Address or subnet mask against another address.
*
*/
class IpMatcher {
int netMask = -1;
InetAddress expectedIpAddr;
IpMatcher(String expectedIp) throws UnknownHostException {
if (expectedIp.indexOf('/') > 0) {
String[] addrAndMask = expectedIp.split("/");
expectedIp = addrAndMask[0];
netMask = Integer.parseInt(addrAndMask[1]);
}
expectedIpAddr = InetAddress.getByName(expectedIp);
}
boolean matches(String actualIp) throws UnknownHostException {
InetAddress actualIpAddr = InetAddress.getByName(actualIp);
// compare v4 with v4 vs v6 with v6
if (!expectedIpAddr.getClass().equals(actualIpAddr.getClass())) {
return false;
}
if (netMask == -1) {
return actualIpAddr.equals(expectedIpAddr);
}
int oddBits = netMask % 8;
int netMaskBytes = netMask / 8 + (oddBits == 0 ? 0 : 1);
byte[] mask = new byte[netMaskBytes];
Arrays.fill(mask, 0, oddBits == 0 ? mask.length : mask.length - 1, (byte) 0xFF);
if (oddBits != 0) {
int finalByte = (1 << oddBits) - 1;
finalByte <<= 8 - oddBits;
mask[mask.length - 1] = (byte) finalByte;
}
byte[] actAddr = actualIpAddr.getAddress();
byte[] expAddr = expectedIpAddr.getAddress();
for (int i = 0; i < mask.length; i++) {
if ((actAddr[i] & mask[i]) != (expAddr[i] & mask[i])) {
return false;
}
}
return true;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy