1 17 18 19 20 package org.apache.lenya.ac.impl; 21 22 import java.io.File ; 23 import java.net.InetAddress ; 24 import java.net.UnknownHostException ; 25 import java.util.Arrays ; 26 27 import org.apache.lenya.ac.AccessControlException; 28 import org.apache.lenya.ac.IPRange; 29 import org.apache.lenya.ac.Machine; 30 import org.apache.lenya.net.InetAddressUtil; 31 import org.apache.log4j.Category; 32 33 44 public abstract class AbstractIPRange extends AbstractGroupable implements IPRange { 45 71 72 private static final Category log = Category.getInstance(AbstractIPRange.class); 73 74 77 public AbstractIPRange() { 78 try { 79 networkAddress = InetAddress.getLocalHost(); 80 byte[] mask = null; 81 int masklen = networkAddress.getAddress().length; 82 if (masklen == 4) { 83 84 87 mask = new byte[] { -1, -1, -1, 0 }; 88 } else { 89 90 mask = new byte[masklen]; 91 Arrays.fill(mask, (byte) -1); 92 } 93 subnetMask = InetAddress.getByAddress(mask); 94 } catch (UnknownHostException ignore) { 95 99 } 100 } 101 102 106 public AbstractIPRange(String id) { 107 111 setId(id); 112 } 113 114 private File configurationDirectory; 115 116 120 public File getConfigurationDirectory() { 121 return configurationDirectory; 122 } 123 124 127 public void setConfigurationDirectory(File configurationDirectory) { 128 this.configurationDirectory = configurationDirectory; 129 } 130 131 136 public abstract void save() throws AccessControlException; 137 138 143 public void delete() throws AccessControlException { 144 removeFromAllGroups(); 145 } 146 147 private InetAddress networkAddress; 148 149 163 public void setNetworkAddress(String address) throws AccessControlException { 164 try { 165 networkAddress = InetAddress.getByName(address); 166 } catch (UnknownHostException e) { 167 throw new AccessControlException("Failed to convert address [" + address + "]: ", e); 168 } 169 } 170 171 182 public void setNetworkAddress(byte[] address) throws AccessControlException { 183 try { 184 networkAddress = InetAddress.getByAddress(address); 185 } catch (UnknownHostException e) { 186 throw new AccessControlException("Failed to convert address [" + addr2string(address) 187 + "]: ", e); 188 } 189 } 190 191 196 public InetAddress getNetworkAddress() { 197 return networkAddress; 198 } 199 200 private InetAddress subnetMask; 201 202 218 public void setSubnetMask(String mask) throws AccessControlException { 219 try { 220 221 setSubnetMask(InetAddress.getByName(mask).getAddress()); 222 } catch (UnknownHostException e) { 223 throw new AccessControlException("Failed to convert mask [" + mask + "]: ", e); 224 } 225 226 } 227 228 242 public void setSubnetMask(byte[] mask) throws AccessControlException { 243 249 if (log.isDebugEnabled()) { 250 log.debug("CHECK_NETMASK: check " + addr2string(mask)); 251 } 252 int i = 0; 253 CHECK_NETMASK: while (i < mask.length) { 254 int b = mask[i++] & 0xff; 255 256 if (b != 0xff) { 257 258 if (log.isDebugEnabled()) { 259 log.debug("CHECK_NETMASK: first byte != 255: idx: " + (i - 1) 260 + ", mask[idx]: 0x" + b); 261 } 262 263 if (b == 0) { 264 break CHECK_NETMASK; 265 } 266 for (int tst = 0xfe; tst != 0; tst = (tst << 1) & 0xff) { 267 log.debug("CHECK_NETMASK: tst == 0x" + Integer.toHexString(tst)); 268 if (b == tst) { 269 break CHECK_NETMASK; 270 } 271 } 272 276 throw new AccessControlException("Invalid byte in mask [" + addr2string(mask) + "]"); 277 } 278 } 279 280 while (++i < mask.length) { 281 if (mask[i] != 0) { 282 285 throw new AccessControlException("Invalid non-zero byte in mask [" 286 + addr2string(mask) + "]"); 287 } 288 } 289 290 291 try { 292 subnetMask = InetAddress.getByAddress(mask); 293 } catch (UnknownHostException e) { 294 throw new AccessControlException( 295 "Failed to convert mask [" + addr2string(mask) + "]: ", e); 296 } 297 } 298 299 303 public InetAddress getSubnetMask() { 304 return subnetMask; 305 } 306 307 316 public static boolean isValidSubnet(InetAddress networkAddress, InetAddress subnetMask) { 317 321 return false; 323 } 324 325 346 public boolean contains(Machine machine) { 347 352 log.debug("Checking IP range: [" + getId() + "]"); 353 return InetAddressUtil.contains(networkAddress, subnetMask, machine.getAddress()); 354 } 355 356 361 private static String addr2string(byte[] addr) { 362 StringBuffer buf = new StringBuffer (); 363 if (addr.length > 4) { 364 365 for (int i = 0; i < addr.length; i++) { 366 if (i > 0 && (i & 1) == 0) { 367 buf.append(':'); 368 } 369 String hex = Integer.toHexString(addr[i] & 0xff); 370 if (hex.length() == 1) { 371 buf.append('0'); 372 } 373 buf.append(hex); 374 } 375 } else { 376 377 for (int i = 0; i < addr.length; i++) { 378 if (i > 0) { 379 buf.append('.'); 380 } 381 buf.append(addr[i] & 0xff); 382 } 383 } 384 return buf.toString(); 385 } 386 } | Popular Tags |