Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 44 additions & 40 deletions CIDRUtils.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
/*
* The MIT License
*
* Copyright (c) 2013 Edin Dazdarevic ([email protected])

* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:

* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* */
* The MIT License
*
* Copyright (c) 2013 Edin Dazdarevic ([email protected])

* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:

* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* */

package edazdarevic.commons.net;

Expand All @@ -37,23 +37,20 @@
* both IPv4 and IPv6.
*/
public class CIDRUtils {
private final String cidr;

private InetAddress inetAddress;
private InetAddress startAddress;
private InetAddress endAddress;
private final int prefixLength;

private BigInteger startAddressBigInt;
private BigInteger endAddressBigInt;

public CIDRUtils(String cidr) throws UnknownHostException {

this.cidr = cidr;

/* split CIDR to address and prefix part */
if (this.cidr.contains("/")) {
int index = this.cidr.indexOf("/");
String addressPart = this.cidr.substring(0, index);
String networkPart = this.cidr.substring(index + 1);
if (cidr.contains("/")) {
int index = cidr.indexOf("/");
String addressPart = cidr.substring(0, index);
String networkPart = cidr.substring(index + 1);

inetAddress = InetAddress.getByName(addressPart);
prefixLength = Integer.parseInt(networkPart);
Expand All @@ -77,8 +74,8 @@ private void calculate() throws UnknownHostException {
targetSize = 4;
} else {
maskBuffer = ByteBuffer.allocate(16)
.putLong(-1L)
.putLong(-1L);
.putLong(-1L)
.putLong(-1L);
targetSize = 16;
}

Expand All @@ -94,7 +91,10 @@ private void calculate() throws UnknownHostException {
byte[] endIpArr = toBytes(endIp.toByteArray(), targetSize);

this.startAddress = InetAddress.getByAddress(startIpArr);
this.startAddressBigInt = new BigInteger(1, this.startAddress.getAddress());

this.endAddress = InetAddress.getByAddress(endIpArr);
this.endAddressBigInt = new BigInteger(1, this.endAddress.getAddress());

}

Expand Down Expand Up @@ -130,13 +130,17 @@ public String getBroadcastAddress() {

public boolean isInRange(String ipAddress) throws UnknownHostException {
InetAddress address = InetAddress.getByName(ipAddress);
BigInteger start = new BigInteger(1, this.startAddress.getAddress());
BigInteger end = new BigInteger(1, this.endAddress.getAddress());
BigInteger target = new BigInteger(1, address.getAddress());
return isInRange(address);
}

int st = start.compareTo(target);
int te = target.compareTo(end);
public boolean isInRange(InetAddress address) {
BigInteger start = this.startAddressBigInt;
BigInteger end = this.endAddressBigInt;
BigInteger target = new BigInteger(1, address.getAddress());

return (st == -1 || st == 0) && (te == -1 || te == 0);
if (start.compareTo(target) > 0){
return false; //start is higher than address -> is not in range
}
return end.compareTo(target) >= 0; // end is higher or equal -> is in range
}
}