1 11 12 package com.sun.jmx.snmp.IPAcl; 13 14 15 16 import java.util.Vector ; 17 import java.util.Enumeration ; 18 import java.io.Serializable ; 19 import java.net.UnknownHostException ; 20 import java.net.InetAddress ; 21 22 import java.security.Principal ; 23 import java.security.acl.Group ; 24 25 import com.sun.jmx.trace.Trace; 26 27 35 36 class NetMaskImpl extends PrincipalImpl implements Group , Serializable { 37 protected byte[] subnet = null; 38 protected int prefix = -1; 39 43 public NetMaskImpl () throws UnknownHostException { 44 } 45 46 private byte[] extractSubNet(byte[] b) { 47 int addrLength = b.length; 48 byte[] subnet = null; 49 if(isDebugOn()) { 50 debug("extractSubNet","BINARY ARRAY :"); 51 StringBuffer buff = new StringBuffer (); 52 for(int i =0; i < addrLength; i++) { 53 buff.append((int)(b[i] &0xFF) +":"); 54 } 55 debug("extractSubNet", buff.toString()); 56 } 57 58 int fullyCoveredByte = prefix / 8; 60 if(fullyCoveredByte == addrLength) { 61 if(isDebugOn()) { 62 debug("extractSubNet"," The mask is the complete address, strange..." + addrLength); 63 } 64 subnet = b; 65 return subnet; 66 } 67 if(fullyCoveredByte > addrLength) { 68 if(isDebugOn()) { 69 debug("extractSubNet"," The number of covered byte is longer than the address. BUG"); 70 } 71 throw new IllegalArgumentException ("The number of covered byte is longer than the address."); 72 } 73 int partialyCoveredIndex = fullyCoveredByte; 74 if(isDebugOn()) { 75 debug("extractSubNet"," Partialy covered index : " + partialyCoveredIndex); 76 } 77 byte toDeal = b[partialyCoveredIndex]; 78 if(isDebugOn()) { 79 debug("extractSubNet"," Partialy covered byte : " + toDeal); 80 } 81 82 int nbbits = prefix % 8; 84 int subnetSize = 0; 85 86 if(nbbits == 0) 87 subnetSize = partialyCoveredIndex; 88 else 89 subnetSize = partialyCoveredIndex + 1; 90 91 if(isDebugOn()) { 92 debug("extractSubNet"," Remains : " + nbbits); 93 } 94 95 byte mask = 0; 96 for(int i = 0; i < nbbits; i++) { 97 mask |= (1 << (7 - i)); 98 } 99 if(isDebugOn()) { 100 debug("extractSubNet","Mask value" + (int) (mask & 0xFF)); 101 } 102 103 byte maskedValue = (byte) ((int)toDeal & (int)mask); 104 105 if(isDebugOn()) { 106 debug("extractSubNet","Masked byte :" + (int) (maskedValue &0xFF)); 107 } 108 subnet = new byte[subnetSize]; 109 if(isDebugOn()) { 110 debug("extractSubNet","Resulting subnet : "); 111 } 112 for(int i = 0; i < partialyCoveredIndex; i++) { 113 subnet[i] = b[i]; 114 115 if(isDebugOn()) { 116 debug("extractSubNet",(int) (subnet[i] & 0xFF) +":"); 117 } 118 } 119 120 if(nbbits != 0) { 121 subnet[partialyCoveredIndex] = maskedValue; 122 if(isDebugOn()) { 123 debug("extractSubNet"," Last subnet byte : " + (int) (subnet[partialyCoveredIndex] &0xFF)); 124 } 125 } 126 return subnet; 127 } 128 129 135 public NetMaskImpl (String a, int prefix) throws UnknownHostException { 136 super(a); 137 this.prefix = prefix; 138 subnet = extractSubNet(getAddress().getAddress()); 139 } 140 141 148 public boolean addMember(Principal p) { 149 return true; 151 } 152 153 public int hashCode() { 154 return super.hashCode(); 155 } 156 157 165 public boolean equals (Object p) { 166 if (p instanceof PrincipalImpl || p instanceof NetMaskImpl){ 167 PrincipalImpl received = (PrincipalImpl) p; 168 InetAddress addr = received.getAddress(); 169 if(isDebugOn()) { 170 debug("equals","Received Address : " + addr); 171 } 172 byte[] recAddr = addr.getAddress(); 173 for(int i = 0; i < subnet.length; i++) { 174 if(isDebugOn()) { 175 debug("equals","(recAddr[i]) :" + (recAddr[i] & 0xFF)); 176 debug("equals","(recAddr[i] & subnet[i]) :" + 177 ( (int) (recAddr[i] & (int)subnet[i]) &0xFF) + 178 "subnet[i] :" + (int) (subnet[i] &0xFF)); 179 } 180 if((recAddr[i] & subnet[i]) != subnet[i]) { 181 if(isDebugOn()) { 182 debug("equals","FALSE"); 183 } 184 return false; 185 } 186 } 187 if(isDebugOn()) { 188 debug("equals","TRUE"); 189 } 190 return true; 191 } else 192 return false; 193 } 194 200 public boolean isMember(Principal p) { 201 if ((p.hashCode() & super.hashCode()) == p.hashCode()) return true; 202 else return false; 203 } 204 205 210 public Enumeration members(){ 211 Vector v = new Vector (1); 212 v.addElement(this); 213 return v.elements(); 214 } 215 216 222 public boolean removeMember(Principal p) { 223 return true; 224 } 225 226 231 public String toString() { 232 return ("NetMaskImpl :"+ super.getAddress().toString() + "/" + prefix); 233 } 234 235 238 boolean isTraceOn() { 239 return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_SNMP); 240 } 241 242 void trace(String clz, String func, String info) { 243 Trace.send(Trace.LEVEL_TRACE, Trace.INFO_SNMP, clz, func, info); 244 } 245 246 void trace(String func, String info) { 247 trace(dbgTag, func, info); 248 } 249 250 boolean isDebugOn() { 251 return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_SNMP); 252 } 253 254 void debug(String clz, String func, String info) { 255 Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_SNMP, clz, func, info); 256 } 257 258 void debug(String clz, String func, Throwable exception) { 259 Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_SNMP, clz, func, exception); 260 } 261 262 void debug(String func, String info) { 263 debug(dbgTag, func, info); 264 } 265 266 void debug(String func, Throwable exception) { 267 debug(dbgTag, func, exception); 268 } 269 270 String dbgTag = "NetMaskImpl"; 271 } 272 273 274 | Popular Tags |