| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package edazdarevic.commons.net; |
| 27 |
|
| 28 |
import java.math.BigInteger; |
| 29 |
import java.net.InetAddress; |
| 30 |
import java.net.UnknownHostException; |
| 31 |
import java.nio.ByteBuffer; |
| 32 |
import java.util.ArrayList; |
| 33 |
import java.util.List; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class CIDRUtils { |
| 40 |
private final String cidr; |
| 41 |
|
| 42 |
private InetAddress inetAddress; |
| 43 |
private InetAddress startAddress; |
| 44 |
private InetAddress endAddress; |
| 45 |
private final int prefixLength; |
| 46 |
|
| 47 |
|
| 48 |
public CIDRUtils(String cidr) throws UnknownHostException { |
| 49 |
|
| 50 |
this.cidr = cidr; |
| 51 |
|
| 52 |
|
| 53 |
if (this.cidr.contains("/")) { |
| 54 |
int index = this.cidr.indexOf("/"); |
| 55 |
String addressPart = this.cidr.substring(0, index); |
| 56 |
String networkPart = this.cidr.substring(index + 1); |
| 57 |
|
| 58 |
inetAddress = InetAddress.getByName(addressPart); |
| 59 |
prefixLength = Integer.parseInt(networkPart); |
| 60 |
|
| 61 |
calculate(); |
| 62 |
} else { |
| 63 |
throw new IllegalArgumentException("not an valid CIDR format!"); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
private void calculate() throws UnknownHostException { |
| 69 |
|
| 70 |
ByteBuffer maskBuffer; |
| 71 |
int targetSize; |
| 72 |
if (inetAddress.getAddress().length == 4) { |
| 73 |
maskBuffer = |
| 74 |
ByteBuffer |
| 75 |
.allocate(4) |
| 76 |
.putInt(-1); |
| 77 |
targetSize = 4; |
| 78 |
} else { |
| 79 |
maskBuffer = ByteBuffer.allocate(16) |
| 80 |
.putLong(-1L) |
| 81 |
.putLong(-1L); |
| 82 |
targetSize = 16; |
| 83 |
} |
| 84 |
|
| 85 |
BigInteger mask = (new BigInteger(1, maskBuffer.array())).not().shiftRight(prefixLength); |
| 86 |
|
| 87 |
ByteBuffer buffer = ByteBuffer.wrap(inetAddress.getAddress()); |
| 88 |
BigInteger ipVal = new BigInteger(1, buffer.array()); |
| 89 |
|
| 90 |
BigInteger startIp = ipVal.and(mask); |
| 91 |
BigInteger endIp = startIp.add(mask.not()); |
| 92 |
|
| 93 |
byte[] startIpArr = toBytes(startIp.toByteArray(), targetSize); |
| 94 |
byte[] endIpArr = toBytes(endIp.toByteArray(), targetSize); |
| 95 |
|
| 96 |
this.startAddress = InetAddress.getByAddress(startIpArr); |
| 97 |
this.endAddress = InetAddress.getByAddress(endIpArr); |
| 98 |
|
| 99 |
} |
| 100 |
|
| 101 |
private byte[] toBytes(byte[] array, int targetSize) { |
| 102 |
int counter = 0; |
| 103 |
List<Byte> newArr = new ArrayList<>(); |
| 104 |
while (counter < targetSize && (array.length - 1 - counter >= 0)) { |
| 105 |
newArr.add(0, array[array.length - 1 - counter]); |
| 106 |
counter++; |
| 107 |
} |
| 108 |
|
| 109 |
int size = newArr.size(); |
| 110 |
for (int i = 0; i < (targetSize - size); i++) { |
| 111 |
|
| 112 |
newArr.add(0, (byte) 0); |
| 113 |
} |
| 114 |
|
| 115 |
byte[] ret = new byte[newArr.size()]; |
| 116 |
for (int i = 0; i < newArr.size(); i++) { |
| 117 |
ret[i] = newArr.get(i); |
| 118 |
} |
| 119 |
return ret; |
| 120 |
} |
| 121 |
|
| 122 |
public String getNetworkAddress() { |
| 123 |
|
| 124 |
return this.startAddress.getHostAddress(); |
| 125 |
} |
| 126 |
|
| 127 |
public String getBroadcastAddress() { |
| 128 |
return this.endAddress.getHostAddress(); |
| 129 |
} |
| 130 |
|
| 131 |
public boolean isInRange(String ipAddress) throws UnknownHostException { |
| 132 |
InetAddress address = InetAddress.getByName(ipAddress); |
| 133 |
BigInteger start = new BigInteger(1, this.startAddress.getAddress()); |
| 134 |
BigInteger end = new BigInteger(1, this.endAddress.getAddress()); |
| 135 |
BigInteger target = new BigInteger(1, address.getAddress()); |
| 136 |
|
| 137 |
int st = start.compareTo(target); |
| 138 |
int te = target.compareTo(end); |
| 139 |
|
| 140 |
return (st == -1 || st == 0) && (te == -1 || te == 0); |
| 141 |
} |
| 142 |
} |