1 11 12 package com.sun.jmx.snmp; 13 14 import com.sun.jmx.snmp.SnmpSecurityParameters; 15 import java.util.Vector ; 18 import java.net.InetAddress ; 19 20 import com.sun.jmx.trace.Trace; 23 24 import com.sun.jmx.snmp.SnmpStatusException; 25 33 public abstract class SnmpMsg implements SnmpDefinitions { 34 41 public int version = 0; 42 43 50 public byte[] data = null; 51 52 55 public int dataLength = 0; 56 57 62 public InetAddress address = null; 63 64 69 public int port = 0; 70 73 public SnmpSecurityParameters securityParameters = null; 74 79 public static int getProtocolVersion(byte[] data) 80 throws SnmpStatusException { 81 int version = 0; 82 BerDecoder bdec = null; 83 try { 84 bdec = new BerDecoder(data); 85 bdec.openSequence(); 86 version = bdec.fetchInteger(); 87 } 88 catch(BerException x) { 89 throw new SnmpStatusException("Invalid encoding") ; 90 } 91 try { 92 bdec.closeSequence(); 93 } 94 catch(BerException x) { 95 } 96 return version; 97 } 98 99 104 public abstract int getRequestId(byte[] data) throws SnmpStatusException; 105 106 115 public abstract int encodeMessage(byte[] outputBytes) 116 throws SnmpTooBigException; 117 118 126 public abstract void decodeMessage(byte[] inputBytes, int byteCount) 127 throws SnmpStatusException; 128 129 148 public abstract void encodeSnmpPdu(SnmpPdu pdu, int maxDataLength) 149 throws SnmpStatusException, SnmpTooBigException; 150 151 152 160 public abstract SnmpPdu decodeSnmpPdu() 161 throws SnmpStatusException; 162 163 172 public static String dumpHexBuffer(byte [] b, int offset, int len) { 173 StringBuffer buf = new StringBuffer (len << 1) ; 174 int k = 1 ; 175 int flen = offset + len ; 176 177 for (int i = offset; i < flen ; i++) { 178 int j = b[i] & 0xFF ; 179 buf.append(Character.forDigit((j >>> 4) , 16)) ; 180 buf.append(Character.forDigit((j & 0x0F), 16)) ; 181 k++ ; 182 if (k%16 == 0) { 183 buf.append('\n') ; 184 k = 1 ; 185 } else 186 buf.append(' ') ; 187 } 188 return buf.toString() ; 189 } 190 191 196 public String printMessage() { 197 StringBuffer sb = new StringBuffer () ; 198 sb.append("Version: ") ; 199 sb.append(version) ; 200 sb.append("\n") ; 201 if (data == null) { 202 sb.append("Data: null") ; 203 } 204 else { 205 sb.append("Data: {\n") ; 206 sb.append(dumpHexBuffer(data, 0, dataLength)) ; 207 sb.append("\n}\n") ; 208 } 209 210 return sb.toString() ; 211 } 212 213 216 public void encodeVarBindList(BerEncoder benc, 217 SnmpVarBind[] varBindList) 218 throws SnmpStatusException, SnmpTooBigException { 219 int encodedVarBindCount = 0 ; 223 try { 224 benc.openSequence() ; 225 if (varBindList != null) { 226 for (int i = varBindList.length - 1 ; i >= 0 ; i--) { 227 SnmpVarBind bind = varBindList[i] ; 228 if (bind != null) { 229 benc.openSequence() ; 230 encodeVarBindValue(benc, bind.value) ; 231 benc.putOid(bind.oid.longValue()) ; 232 benc.closeSequence() ; 233 encodedVarBindCount++ ; 234 } 235 } 236 } 237 benc.closeSequence() ; 238 } 239 catch(ArrayIndexOutOfBoundsException x) { 240 throw new SnmpTooBigException(encodedVarBindCount) ; 241 } 242 } 243 244 247 void encodeVarBindValue(BerEncoder benc, 248 SnmpValue v)throws SnmpStatusException { 249 if (v == null) { 250 benc.putNull() ; 251 } 252 else if (v instanceof SnmpIpAddress) { 253 benc.putOctetString(((SnmpIpAddress)v).byteValue(), SnmpValue.IpAddressTag) ; 254 } 255 else if (v instanceof SnmpCounter) { 256 benc.putInteger(((SnmpCounter)v).longValue(), SnmpValue.CounterTag) ; 257 } 258 else if (v instanceof SnmpGauge) { 259 benc.putInteger(((SnmpGauge)v).longValue(), SnmpValue.GaugeTag) ; 260 } 261 else if (v instanceof SnmpTimeticks) { 262 benc.putInteger(((SnmpTimeticks)v).longValue(), SnmpValue.TimeticksTag) ; 263 } 264 else if (v instanceof SnmpOpaque) { 265 benc.putOctetString(((SnmpOpaque)v).byteValue(), SnmpValue.OpaqueTag) ; 266 } 267 else if (v instanceof SnmpInt) { 268 benc.putInteger(((SnmpInt)v).intValue()) ; 269 } 270 else if (v instanceof SnmpString) { 271 benc.putOctetString(((SnmpString)v).byteValue()) ; 272 } 273 else if (v instanceof SnmpOid) { 274 benc.putOid(((SnmpOid)v).longValue()) ; 275 } 276 else if (v instanceof SnmpCounter64) { 277 if (version == snmpVersionOne) { 278 throw new SnmpStatusException("Invalid value for SNMP v1 : " + v) ; 279 } 280 benc.putInteger(((SnmpCounter64)v).longValue(), SnmpValue.Counter64Tag) ; 281 } 282 else if (v instanceof SnmpNull) { 283 int tag = ((SnmpNull)v).getTag() ; 284 if ((version == snmpVersionOne) && (tag != SnmpValue.NullTag)) { 285 throw new SnmpStatusException("Invalid value for SNMP v1 : " + v) ; 286 } 287 if ((version == snmpVersionTwo) && 288 (tag != SnmpValue.NullTag) && 289 (tag != SnmpVarBind.errNoSuchObjectTag) && 290 (tag != SnmpVarBind.errNoSuchInstanceTag) && 291 (tag != SnmpVarBind.errEndOfMibViewTag)) { 292 throw new SnmpStatusException("Invalid value " + v) ; 293 } 294 benc.putNull(tag) ; 295 } 296 else { 297 throw new SnmpStatusException("Invalid value " + v) ; 298 } 299 300 } 301 302 303 306 public SnmpVarBind[] decodeVarBindList(BerDecoder bdec) 307 throws BerException { 308 bdec.openSequence() ; 309 Vector tmp = new Vector () ; 310 while (bdec.cannotCloseSequence()) { 311 SnmpVarBind bind = new SnmpVarBind() ; 312 bdec.openSequence() ; 313 bind.oid = new SnmpOid(bdec.fetchOid()) ; 314 bind.setSnmpValue(decodeVarBindValue(bdec)) ; 315 bdec.closeSequence() ; 316 tmp.addElement(bind) ; 317 } 318 bdec.closeSequence() ; 319 SnmpVarBind[] varBindList= new SnmpVarBind[tmp.size()] ; 320 tmp.copyInto(varBindList); 321 return varBindList ; 322 } 323 324 325 328 SnmpValue decodeVarBindValue(BerDecoder bdec) 329 throws BerException { 330 SnmpValue result = null ; 331 int tag = bdec.getTag() ; 332 333 switch(tag) { 336 337 case BerDecoder.IntegerTag : 341 try { 342 result = new SnmpInt(bdec.fetchInteger()) ; 343 } catch(RuntimeException r) { 344 throw new BerException(); 345 } 347 break ; 348 case BerDecoder.OctetStringTag : 349 try { 350 result = new SnmpString(bdec.fetchOctetString()) ; 351 } catch(RuntimeException r) { 352 throw new BerException(); 353 } 355 break ; 356 case BerDecoder.OidTag : 357 try { 358 result = new SnmpOid(bdec.fetchOid()) ; 359 } catch(RuntimeException r) { 360 throw new BerException(); 361 } 363 break ; 364 case BerDecoder.NullTag : 365 bdec.fetchNull() ; 366 try { 367 result = new SnmpNull() ; 368 } catch(RuntimeException r) { 369 throw new BerException(); 370 } 372 break ; 373 374 case SnmpValue.IpAddressTag : 378 try { 379 result = new SnmpIpAddress(bdec.fetchOctetString(tag)) ; 380 } catch (RuntimeException r) { 381 throw new BerException(); 382 } 384 break ; 385 case SnmpValue.CounterTag : 386 try { 387 result = new SnmpCounter(bdec.fetchIntegerAsLong(tag)) ; 388 } catch(RuntimeException r) { 389 throw new BerException(); 390 } 392 break ; 393 case SnmpValue.GaugeTag : 394 try { 395 result = new SnmpGauge(bdec.fetchIntegerAsLong(tag)) ; 396 } catch(RuntimeException r) { 397 throw new BerException(); 398 } 400 break ; 401 case SnmpValue.TimeticksTag : 402 try { 403 result = new SnmpTimeticks(bdec.fetchIntegerAsLong(tag)) ; 404 } catch(RuntimeException r) { 405 throw new BerException(); 406 } 408 break ; 409 case SnmpValue.OpaqueTag : 410 try { 411 result = new SnmpOpaque(bdec.fetchOctetString(tag)) ; 412 } catch(RuntimeException r) { 413 throw new BerException(); 414 } 416 break ; 417 418 case SnmpValue.Counter64Tag : 422 if (version == snmpVersionOne) { 423 throw new BerException(BerException.BAD_VERSION) ; 424 } 425 try { 426 result = new SnmpCounter64(bdec.fetchIntegerAsLong(tag)) ; 427 } catch(RuntimeException r) { 428 throw new BerException(); 429 } 431 break ; 432 433 case SnmpVarBind.errNoSuchObjectTag : 434 if (version == snmpVersionOne) { 435 throw new BerException(BerException.BAD_VERSION) ; 436 } 437 bdec.fetchNull(tag) ; 438 result = SnmpVarBind.noSuchObject ; 439 break ; 440 441 case SnmpVarBind.errNoSuchInstanceTag : 442 if (version == snmpVersionOne) { 443 throw new BerException(BerException.BAD_VERSION) ; 444 } 445 bdec.fetchNull(tag) ; 446 result = SnmpVarBind.noSuchInstance ; 447 break ; 448 449 case SnmpVarBind.errEndOfMibViewTag : 450 if (version == snmpVersionOne) { 451 throw new BerException(BerException.BAD_VERSION) ; 452 } 453 bdec.fetchNull(tag) ; 454 result = SnmpVarBind.endOfMibView ; 455 break ; 456 457 default: 458 throw new BerException() ; 459 460 } 461 462 return result ; 463 } 464 465 } 466 | Popular Tags |