1 17 18 19 20 package org.apache.lenya.net; 21 22 import java.net.InetAddress ; 23 import java.net.UnknownHostException ; 24 25 import org.apache.log4j.Category; 26 27 30 public class InetAddressUtil { 31 32 private static final Category log = Category.getInstance(InetAddressUtil.class); 33 34 37 private InetAddressUtil() { 38 } 39 40 48 public static boolean contains(InetAddress network, InetAddress netmask, InetAddress ip) { 49 if(log.isDebugEnabled()) { 50 log.debug("======================================="); 51 log.debug("Checking IP address: " + ip + " in " + network + " / " + netmask); 52 } 53 54 byte[] networkBytes = network.getAddress(); 55 byte[] netmaskBytes = netmask.getAddress(); 56 byte[] ipBytes = ip.getAddress(); 57 58 59 if(networkBytes.length != netmaskBytes.length 60 || netmaskBytes.length != ipBytes.length) 61 { 62 75 if(log.isDebugEnabled()) { 76 log.debug 77 ("Network address " + network + ", subnet mask " 78 + netmask + " and/or host address " + ip 79 + " have different sizes! (return false ...)"); 80 log.debug("======================================="); 81 } 82 return false; 83 } 84 85 86 for(int i=0; i<netmaskBytes.length; i++) { 87 int mask = netmaskBytes[i] & 0xff; 88 if((networkBytes[i] & mask) != (ipBytes[i] & mask)) { 89 if(log.isDebugEnabled()) { 90 log.debug 91 (ip + " is not in " + network + " / " + netmask); 92 log.debug("======================================="); 93 } 94 return false; 95 } 96 } 97 if(log.isDebugEnabled()) { 98 log.debug 99 (ip + " is in " + network + " / " + netmask); 100 log.debug("======================================="); 101 } 102 return true; 103 } 104 105 118 public static int getClassPart(InetAddress ip, int partNumber) { 119 String [] parts = ip.getHostAddress().split("\\."); 120 String part = parts[partNumber]; 121 return new Integer (part).intValue(); 122 } 123 124 136 public static int checkNetmask(InetAddress netmask) { 137 String [] parts = netmask.getHostAddress().split("\\."); 138 Integer [] numbers = new Integer [4]; 139 for (int i = 0; i < 4; i++) { 140 numbers[i] = new Integer (parts[i]); 141 } 142 143 for (int i = 0; i < 4; i++) { 144 log.debug(".checkNetmask(): Check part: " + numbers[i]); 145 if (0 <= numbers[i].intValue() && numbers[i].intValue() <= 255) { 146 if (numbers[i].intValue() != 255) { 147 for (int k = i + 1; k < 4; k++) { 148 if (numbers[k].intValue() != 0) { 149 log.error(".checkNetmask(): Illegal Netmask: " + netmask); 150 return -1; 151 } 152 } 153 return i; 154 } else { 155 continue; 156 } 157 } else { 158 log.error(".checkNetmask(): Illegal Netmask: " + netmask); 160 return -1; 161 } 162 } 163 if (log.isDebugEnabled()) { 164 log.debug("All parts equal 255: " + netmask); 165 } 166 return 3; 167 } 168 169 182 public static InetAddress getAddress(String string) throws UnknownHostException { 183 String [] strings = string.split("\\."); 184 185 InetAddress address; 186 byte[] numbers = new byte[strings.length]; 187 for (int i = 0; i < strings.length; i++) { 188 int number = Integer.parseInt(strings[i]); 189 if (number > 127) { 190 number = number - 256; 191 } 192 numbers[i] = (byte) number; 193 } 194 195 address = InetAddress.getByAddress(numbers); 196 return address; 197 } 198 199 } 200 | Popular Tags |