1 11 package com.sun.jmx.snmp; 12 13 import java.net.InetAddress ; 14 import java.io.Serializable ; 15 import java.net.UnknownHostException ; 16 import java.util.StringTokenizer ; 17 import java.util.Arrays ; 18 import java.util.NoSuchElementException ; 19 20 import com.sun.jmx.snmp.internal.SnmpTools; 21 22 28 public class SnmpEngineId implements Serializable { 29 byte[] engineId = null; 30 String hexString = null; 31 String humanString = null; 32 36 SnmpEngineId(String hexString) { 37 engineId = SnmpTools.ascii2binary(hexString); 38 this.hexString = hexString.toLowerCase(); 39 } 40 44 SnmpEngineId(byte[] bin) { 45 engineId = bin; 46 hexString = SnmpTools.binary2ascii(bin).toLowerCase(); 47 } 48 49 53 public String getReadableId() { 54 return humanString; 55 } 56 57 61 public String toString() { 62 return hexString; 63 } 64 68 public byte[] getBytes() { 69 return engineId; 70 } 71 72 75 void setStringValue(String val) { 76 humanString = val; 77 } 78 79 static void validateId(String str) throws IllegalArgumentException { 80 byte[] arr = SnmpTools.ascii2binary(str); 81 validateId(arr); 82 } 83 84 static void validateId(byte[] arr) throws IllegalArgumentException { 85 86 if(arr.length < 5) throw new IllegalArgumentException ("Id size lower than 5 bytes."); 87 if(arr.length > 32) throw new IllegalArgumentException ("Id size greater than 32 bytes."); 88 89 if( ((arr[0] & 0x80) == 0) && arr.length != 12) 91 throw new IllegalArgumentException ("Very first bit = 0 and length != 12 octets"); 92 93 byte[] zeroedArrays = new byte[arr.length]; 94 if(Arrays.equals(zeroedArrays, arr)) throw new IllegalArgumentException ("Zeroed Id."); 95 byte[] FFArrays = new byte[arr.length]; 96 Arrays.fill(FFArrays, (byte)0xFF); 97 if(Arrays.equals(FFArrays, arr)) throw new IllegalArgumentException ("0xFF Id."); 98 99 } 100 101 113 public static SnmpEngineId createEngineId(byte[] arr) throws IllegalArgumentException { 114 if( (arr == null) || arr.length == 0) return null; 115 validateId(arr); 116 return new SnmpEngineId(arr); 117 } 118 119 123 public static SnmpEngineId createEngineId() { 124 byte[] address = null; 125 byte[] engineid = new byte[13]; 126 int iana = 42; 127 long mask = 0xFF; 128 long time = System.currentTimeMillis(); 129 130 engineid[0] = (byte) ( (iana & 0xFF000000) >> 24 ); 131 engineid[0] |= 0x80; 132 engineid[1] = (byte) ( (iana & 0x00FF0000) >> 16 ); 133 engineid[2] = (byte) ( (iana & 0x0000FF00) >> 8 ); 134 engineid[3] = (byte) (iana & 0x000000FF); 135 engineid[4] = 0x05; 136 137 engineid[5] = (byte) ( (time & (mask << 56)) >>> 56 ); 138 engineid[6] = (byte) ( (time & (mask << 48) ) >>> 48 ); 139 engineid[7] = (byte) ( (time & (mask << 40) ) >>> 40 ); 140 engineid[8] = (byte) ( (time & (mask << 32) ) >>> 32 ); 141 engineid[9] = (byte) ( (time & (mask << 24) ) >>> 24 ); 142 engineid[10] = (byte) ( (time & (mask << 16) ) >>> 16 ); 143 engineid[11] = (byte) ( (time & (mask << 8) ) >>> 8 ); 144 engineid[12] = (byte) (time & mask); 145 146 return new SnmpEngineId(engineid); 147 } 148 149 156 public SnmpOid toOid() { 157 long[] oid = new long[engineId.length + 1]; 158 oid[0] = engineId.length; 159 for(int i = 1; i <= engineId.length; i++) 160 oid[i] = (long) (engineId[i-1] & 0xFF); 161 return new SnmpOid(oid); 162 } 163 164 200 public static SnmpEngineId createEngineId(String str) 201 throws IllegalArgumentException , UnknownHostException { 202 return createEngineId(str, null); 203 } 204 205 228 public static SnmpEngineId createEngineId(String str, String separator) 229 throws IllegalArgumentException , UnknownHostException { 230 if(str == null) return null; 231 232 if(str.startsWith("0x") || str.startsWith("0X")) { 233 validateId(str); 234 return new SnmpEngineId(str); 235 } 236 separator = separator == null ? ":" : separator; 237 StringTokenizer token = new StringTokenizer (str, 238 separator, 239 true); 240 241 String address = null; 242 String port = null; 243 String iana = null; 244 int objPort = 161; 245 int objIana = 42; 246 InetAddress objAddress = null; 247 SnmpEngineId eng = null; 248 try { 249 try { 251 address = token.nextToken(); 252 }catch(NoSuchElementException e) { 253 throw new IllegalArgumentException ("Passed string is invalid : ["+str+"]"); 254 } 255 if(!address.equals(separator)) { 256 objAddress = InetAddress.getByName(address); 257 try { 258 token.nextToken(); 259 }catch(NoSuchElementException e) { 260 eng = SnmpEngineId.createEngineId(objAddress, 262 objPort, 263 objIana); 264 eng.setStringValue(str); 265 return eng; 266 } 267 } 268 else 269 objAddress = InetAddress.getLocalHost(); 270 271 try { 273 port = token.nextToken(); 274 }catch(NoSuchElementException e) { 275 eng = SnmpEngineId.createEngineId(objAddress, 277 objPort, 278 objIana); 279 eng.setStringValue(str); 280 return eng; 281 } 282 283 if(!port.equals(separator)) { 284 objPort = Integer.parseInt(port); 285 try { 286 token.nextToken(); 287 }catch(NoSuchElementException e) { 288 eng = SnmpEngineId.createEngineId(objAddress, 290 objPort, 291 objIana); 292 eng.setStringValue(str); 293 return eng; 294 } 295 } 296 297 try { 299 iana = token.nextToken(); 300 }catch(NoSuchElementException e) { 301 eng = SnmpEngineId.createEngineId(objAddress, 303 objPort, 304 objIana); 305 eng.setStringValue(str); 306 return eng; 307 } 308 309 if(!iana.equals(separator)) 310 objIana = Integer.parseInt(iana); 311 312 eng = SnmpEngineId.createEngineId(objAddress, 313 objPort, 314 objIana); 315 eng.setStringValue(str); 316 317 return eng; 318 319 } catch(Exception e) { 320 throw new IllegalArgumentException ("Passed string is invalid : ["+str+"]. Check that the used separator ["+ separator + "] is compatible with IPv6 address format."); 321 } 322 323 } 324 325 335 public static SnmpEngineId createEngineId(int port) 336 throws UnknownHostException { 337 int suniana = 42; 338 InetAddress address = null; 339 address = InetAddress.getLocalHost(); 340 return createEngineId(address, port, suniana); 341 } 342 352 public static SnmpEngineId createEngineId(InetAddress address, int port) 353 throws IllegalArgumentException { 354 int suniana = 42; 355 if(address == null) 356 throw new IllegalArgumentException ("InetAddress is null."); 357 return createEngineId(address, port, suniana); 358 } 359 360 369 public static SnmpEngineId createEngineId(int port, int iana) throws UnknownHostException { 370 InetAddress address = null; 371 address = InetAddress.getLocalHost(); 372 return createEngineId(address, port, iana); 373 } 374 375 383 public static SnmpEngineId createEngineId(InetAddress addr, 384 int port, 385 int iana) { 386 if(addr == null) throw new IllegalArgumentException ("InetAddress is null."); 387 byte[] address = addr.getAddress(); 388 byte[] engineid = new byte[9 + address.length]; 389 engineid[0] = (byte) ( (iana & 0xFF000000) >> 24 ); 390 engineid[0] |= 0x80; 391 engineid[1] = (byte) ( (iana & 0x00FF0000) >> 16 ); 392 engineid[2] = (byte) ( (iana & 0x0000FF00) >> 8 ); 393 394 engineid[3] = (byte) (iana & 0x000000FF); 395 engineid[4] = 0x05; 396 397 if(address.length == 4) 398 engineid[4] = 0x01; 399 400 if(address.length == 16) 401 engineid[4] = 0x02; 402 403 for(int i = 0; i < address.length; i++) { 404 engineid[i + 5] = address[i]; 405 } 406 407 engineid[5 + address.length] = (byte) ( (port & 0xFF000000) >> 24 ); 408 engineid[6 + address.length] = (byte) ( (port & 0x00FF0000) >> 16 ); 409 engineid[7 + address.length] = (byte) ( (port & 0x0000FF00) >> 8 ); 410 engineid[8 + address.length] = (byte) ( port & 0x000000FF ); 411 412 return new SnmpEngineId(engineid); 413 } 414 415 423 public static SnmpEngineId createEngineId(int iana, InetAddress addr) 424 { 425 if(addr == null) throw new IllegalArgumentException ("InetAddress is null."); 426 byte[] address = addr.getAddress(); 427 byte[] engineid = new byte[5 + address.length]; 428 engineid[0] = (byte) ( (iana & 0xFF000000) >> 24 ); 429 engineid[0] |= 0x80; 430 engineid[1] = (byte) ( (iana & 0x00FF0000) >> 16 ); 431 engineid[2] = (byte) ( (iana & 0x0000FF00) >> 8 ); 432 433 engineid[3] = (byte) (iana & 0x000000FF); 434 if(address.length == 4) 435 engineid[4] = 0x01; 436 437 if(address.length == 16) 438 engineid[4] = 0x02; 439 440 for(int i = 0; i < address.length; i++) { 441 engineid[i + 5] = address[i]; 442 } 443 444 return new SnmpEngineId(engineid); 445 } 446 447 456 public static SnmpEngineId createEngineId(InetAddress addr) { 457 return createEngineId(42, addr); 458 } 459 460 461 465 public boolean equals(Object a) { 466 if(!(a instanceof SnmpEngineId) ) return false; 467 return hexString.equals(((SnmpEngineId) a).toString()); 468 } 469 470 public int hashCode() { 471 return hexString.hashCode(); 472 } 473 } 474 | Popular Tags |