1 19 20 package com.sslexplorer.util; 21 22 import java.io.EOFException ; 23 24 26 public class CIDRNetwork { 27 28 private final String network; 29 private int networkBits; 30 private String networkAddress; 31 private String subnetMask; 32 private String broadcastAddress; 33 private int[] net; 34 private int[] subnet; 35 private String lastIP; 36 37 43 public CIDRNetwork(String network) throws IllegalArgumentException { 44 45 int index = network.indexOf("/"); 46 if (index == -1) 47 index = network.indexOf("\\"); 48 49 if (index == -1) 50 throw new IllegalArgumentException ("CIDR network should be in the format 192.168.1.0/24"); 51 52 try { 53 networkBits = Integer.parseInt(network.substring(index + 1)) - 1; 54 } catch (NumberFormatException ex) { 55 throw new IllegalArgumentException ("CIDR network setting invalid! " + network); 56 } 57 58 this.network = network; 59 subnet = IPUtils.createMaskArray(networkBits); 60 net = IPUtils.getByteAddress(network.substring(0, index)); 61 net = IPUtils.calcNetworkNumber(net, subnet); 62 networkAddress = IPUtils.createAddressString(net); 63 subnetMask = IPUtils.createAddressString(subnet); 64 } 65 66 72 public String getNextIPAddress(String startAddress, String endAddress) throws EOFException { 73 if (lastIP == null && (startAddress == null || "".equals(startAddress))) { 74 lastIP = IPUtils.createAddressString(IPUtils.calcFirstAddress(net, subnet)); 75 } else if (lastIP == null) { 76 lastIP = startAddress; 77 } else { 78 if (endAddress == null || "".equals(endAddress)) { 79 String addressString = IPUtils.createAddressString(IPUtils.calcLastAddress(net, networkBits)); 80 if (lastIP.equals(addressString)) 81 throw new EOFException ("No more IPs available"); 82 } else { 83 if (lastIP.equals(endAddress)) 84 throw new EOFException ("No more IPs available"); 85 } 86 87 lastIP = IPUtils.createAddressString(IPUtils.nextAddress(IPUtils.getByteAddress(lastIP))); 88 } 89 90 return lastIP; 91 } 92 93 96 public String getNetworkAddress() { 97 return networkAddress; 98 } 99 100 103 public int getNetworkBits() { 104 return networkBits + 1; 105 } 106 107 110 public String getSubnetMask() { 111 return subnetMask; 112 } 113 114 117 public String getBroadcastAddress() { 118 return broadcastAddress; 119 } 120 121 124 public String getCIDRString() { 125 return network; 126 } 127 128 public String toString() { 129 return getCIDRString(); 130 } 131 132 137 public boolean isValidDHCPRange(String startAddress, String endAddress) { 138 139 if (!isValidAddressForNetwork(startAddress)) 140 return false; 141 142 if (!isValidAddressForNetwork(endAddress)) 143 return false; 144 145 int[] addressOneBytes = IPUtils.getByteAddress(startAddress); 146 int[] addressTwoBytes = IPUtils.getByteAddress(endAddress); 147 boolean valid = false; 148 for (int index = 0; index < addressOneBytes.length; index++) { 149 150 if ((addressOneBytes[index] ^ subnet[index]) < (addressTwoBytes[index] ^ subnet[index])) { 151 valid = true; 152 break; 153 } 154 } 155 return valid; 156 } 157 158 162 public boolean isValidAddressForNetwork(String address) { 163 try { 164 165 if(address.equals(networkAddress)) 166 return false; 167 168 int[] bytes = IPUtils.getByteAddress(address); 169 170 boolean valid = true; 171 for (int index = 0; index < bytes.length; index++) { 173 int subnetValue = subnet[index]; 174 if (subnetValue == 0) 175 break; 176 177 if ((bytes[index] & subnetValue) != net[index]) { 178 valid = false; 179 break; 180 } 181 } 182 return valid; 183 } catch (IllegalArgumentException iae) { 184 return false; 185 } 186 } 187 } | Popular Tags |