1 20 21 22 23 package org.snmp4j; 24 25 import org.snmp4j.smi.*; 26 import org.snmp4j.asn1.*; 27 import java.io.IOException ; 28 import java.io.OutputStream ; 29 import java.util.Vector ; 30 import org.snmp4j.smi.Integer32; 31 import org.snmp4j.mp.SnmpConstants; 32 import java.io.Serializable ; 33 34 46 public class PDU implements BERSerializable, Serializable { 47 48 private static final long serialVersionUID = 7607672475629607472L; 49 50 53 public static final int GET = (BER.ASN_CONTEXT | BER.ASN_CONSTRUCTOR | 0x0); 54 57 public static final int GETNEXT = (BER.ASN_CONTEXT | BER.ASN_CONSTRUCTOR | 0x1); 58 61 public static final int RESPONSE = (BER.ASN_CONTEXT | BER.ASN_CONSTRUCTOR | 0x2); 62 65 public static final int SET = (BER.ASN_CONTEXT | BER.ASN_CONSTRUCTOR | 0x3); 66 70 public static final int V1TRAP = (BER.ASN_CONTEXT | BER.ASN_CONSTRUCTOR | 0x4); 71 74 public static final int GETBULK = (BER.ASN_CONTEXT | BER.ASN_CONSTRUCTOR | 0x5); 75 79 public static final int INFORM = (BER.ASN_CONTEXT | BER.ASN_CONSTRUCTOR | 0x6); 80 84 public static final int TRAP = (BER.ASN_CONTEXT | BER.ASN_CONSTRUCTOR | 0x7); 85 89 public static final int NOTIFICATION = TRAP; 90 93 public static final int REPORT = (BER.ASN_CONTEXT | BER.ASN_CONSTRUCTOR | 0x8); 94 95 96 98 101 public static final int noError = SnmpConstants.SNMP_ERROR_SUCCESS; 102 103 106 public static final int tooBig = SnmpConstants.SNMP_ERROR_TOO_BIG; 107 108 111 public static final int noSuchName = SnmpConstants.SNMP_ERROR_NO_SUCH_NAME; 112 113 116 public static final int badValue = SnmpConstants.SNMP_ERROR_BAD_VALUE; 117 118 121 public static final int readOnly = SnmpConstants.SNMP_ERROR_READ_ONLY; 122 123 126 public static final int genErr = SnmpConstants.SNMP_ERROR_GENERAL_ERROR; 127 128 132 public static final int noAccess = SnmpConstants.SNMP_ERROR_NO_ACCESS; 133 134 137 public static final int wrongType = SnmpConstants.SNMP_ERROR_WRONG_TYPE; 138 139 142 public static final int wrongLength = SnmpConstants.SNMP_ERROR_WRONG_LENGTH; 143 144 148 public static final int wrongValue = SnmpConstants.SNMP_ERROR_WRONG_VALUE; 149 150 153 public static final int wrongEncoding = 154 SnmpConstants.SNMP_ERROR_WRONG_ENCODING; 155 156 160 public static final int noCreation = SnmpConstants.SNMP_ERROR_NO_CREATION; 161 162 166 public static final int inconsistentValue = 167 SnmpConstants.SNMP_ERROR_INCONSISTENT_VALUE; 168 169 173 public static final int resourceUnavailable = 174 SnmpConstants.SNMP_ERROR_RESOURCE_UNAVAILABLE; 175 176 179 public static final int commitFailed = SnmpConstants.SNMP_ERROR_COMMIT_FAILED; 180 181 184 public static final int undoFailed = SnmpConstants.SNMP_ERROR_UNDO_FAILED; 185 186 189 public static final int authorizationError = 190 SnmpConstants.SNMP_ERROR_AUTHORIZATION_ERROR; 191 192 195 public static final int notWritable = SnmpConstants.SNMP_ERROR_NOT_WRITEABLE; 196 197 201 public static final int inconsistentName = 202 SnmpConstants.SNMP_ERROR_INCONSISTENT_NAME; 203 204 protected Vector variableBindings = new Vector (); 205 protected Integer32 errorStatus = new Integer32(); 206 protected Integer32 errorIndex = new Integer32(); 207 protected Integer32 requestID = new Integer32(); 208 protected int type = GET; 209 210 213 public PDU() { 214 } 215 216 221 public PDU(PDU other) { 222 variableBindings = (Vector ) other.variableBindings.clone(); 223 errorIndex = (Integer32) other.errorIndex.clone(); 224 errorStatus = (Integer32) other.errorStatus.clone(); 225 type = other.type; 226 requestID = (Integer32) other.requestID.clone(); 227 } 228 229 240 public void add(VariableBinding vb) { 241 if (vb.getVariable().isDynamic()) { 242 VariableBinding cvb = (VariableBinding) vb.clone(); 243 variableBindings.add(cvb); 244 } 245 else { 246 variableBindings.add(vb); 247 } 248 } 249 250 261 public void addOID(VariableBinding vb) { 262 VariableBinding cvb = new VariableBinding(vb.getOid()); 263 variableBindings.add(cvb); 264 } 265 266 274 public void addAll(VariableBinding[] vbs) { 275 variableBindings.ensureCapacity(variableBindings.size()+vbs.length); 276 for (int i=0; i<vbs.length; i++) { 277 add(vbs[i]); 278 } 279 } 280 281 292 public void addAllOIDs(VariableBinding[] vbs) { 293 variableBindings.ensureCapacity(variableBindings.size()+vbs.length); 294 for (int i=0; i<vbs.length; i++) { 295 addOID(vbs[i]); 296 } 297 } 298 299 300 301 309 public VariableBinding get(int index) { 310 return (VariableBinding)variableBindings.get(index); 311 } 312 313 324 public VariableBinding set(int index, VariableBinding vb) { 325 if (vb == null) { 326 throw new NullPointerException ("Variable binding must not be null"); 327 } 328 return (VariableBinding)variableBindings.set(index, vb); 329 } 330 331 336 public void remove(int index) { 337 variableBindings.remove(index); 338 } 339 340 345 public int size() { 346 return variableBindings.size(); 347 } 348 349 354 public Vector getVariableBindings() { 355 return variableBindings; 356 } 357 358 361 public void trim() { 362 if (variableBindings.size() > 0) { 363 variableBindings.remove(variableBindings.size() - 1); 364 } 365 } 366 367 373 public void setErrorStatus(int errorStatus) { 374 this.errorStatus.setValue(errorStatus); 375 } 376 377 383 public int getErrorStatus() { 384 return errorStatus.getValue(); 385 } 386 387 394 public String getErrorStatusText() { 395 return toErrorStatusText(errorStatus.getValue()); 396 } 397 398 408 public static final String toErrorStatusText(int errorStatus) { 409 try { 410 return SnmpConstants.SNMP_ERROR_MESSAGES[errorStatus]; 411 } 412 catch (ArrayIndexOutOfBoundsException iobex) { 413 return "Unknown error: "+errorStatus; 414 } 415 } 416 417 422 public void setErrorIndex(int errorIndex) { 423 this.errorIndex.setValue(errorIndex); 424 } 425 426 431 public int getErrorIndex() { 432 return errorIndex.getValue(); 433 } 434 435 439 public boolean isConfirmedPdu() { 440 return ((type != PDU.REPORT) && (type != PDU.RESPONSE) && 441 (type != PDU.TRAP) && (type != PDU.V1TRAP)); 442 } 443 444 public int getBERLength() { 445 int length = getBERPayloadLengthPDU(); 447 length += BER.getBERLengthOfLength(length) + 1; 448 return length; 450 } 451 452 public int getBERPayloadLength() { 453 return getBERPayloadLengthPDU(); 454 } 455 456 public void decodeBER(BERInputStream inputStream) throws IOException { 457 BER.MutableByte pduType = new BER.MutableByte(); 458 int length = BER.decodeHeader(inputStream, pduType); 459 int pduStartPos = (int)inputStream.getPosition(); 460 switch (pduType.getValue()) { 461 case PDU.SET: 462 case PDU.GET: 463 case PDU.GETNEXT: 464 case PDU.GETBULK: 465 case PDU.INFORM: 466 case PDU.REPORT: 467 case PDU.TRAP: 468 case PDU.RESPONSE: 469 break; 470 default: 471 throw new IOException ("Unsupported PDU type: "+pduType.getValue()); 472 } 473 this.type = pduType.getValue(); 474 requestID.decodeBER(inputStream); 475 errorStatus.decodeBER(inputStream); 476 errorIndex.decodeBER(inputStream); 477 478 pduType = new BER.MutableByte(); 479 int vbLength = BER.decodeHeader(inputStream, pduType); 480 if (pduType.getValue() != BER.SEQUENCE) { 481 throw new IOException ("Encountered invalid tag, SEQUENCE expected: "+ 482 pduType.getValue()); 483 } 484 int startPos = (int)inputStream.getPosition(); 486 variableBindings = new Vector (); 487 while (inputStream.getPosition() - startPos < vbLength) { 488 VariableBinding vb = new VariableBinding(); 489 vb.decodeBER(inputStream); 490 variableBindings.add(vb); 491 } 492 if (inputStream.getPosition() - startPos != vbLength) { 493 throw new IOException ("Length of VB sequence ("+vbLength+ 494 ") does not match real length: "+ 495 ((int)inputStream.getPosition()-startPos)); 496 } 497 if (BER.isCheckSequenceLength()) { 498 BER.checkSequenceLength(length, 499 (int) inputStream.getPosition() - pduStartPos, 500 this); 501 } 502 } 503 504 protected int getBERPayloadLengthPDU() { 505 int length = 0; 506 507 for (int i = 0; i < variableBindings.size(); i++) { 509 length += ((VariableBinding)variableBindings.get(i)).getBERLength(); 510 } 511 512 length += BER.getBERLengthOfLength(length) + 1; 513 514 Integer32 i32 = new Integer32(requestID.getValue()); 516 length += i32.getBERLength(); 517 i32 = errorStatus; 518 length += i32.getBERLength(); 519 i32 = errorIndex; 520 length += i32.getBERLength(); 521 i32 = null; 522 return length; 523 } 524 525 public void encodeBER(OutputStream outputStream) throws IOException { 526 BER.encodeHeader(outputStream, type, getBERPayloadLengthPDU()); 527 528 requestID.encodeBER(outputStream); 529 errorStatus.encodeBER(outputStream); 530 errorIndex.encodeBER(outputStream); 531 532 int vbLength = 0; 533 for (int i=0; i<variableBindings.size(); i++) { 534 vbLength += ((VariableBinding)variableBindings.get(i)).getBERLength(); 535 } 536 BER.encodeHeader(outputStream, BER.SEQUENCE, vbLength); 537 for (int i=0; i<variableBindings.size(); i++) { 538 ((VariableBinding)variableBindings.get(i)).encodeBER(outputStream); 539 } 540 } 541 542 546 public void clear() { 547 variableBindings.clear(); 548 setRequestID(new Integer32(0)); 549 } 550 551 556 public void setType(int type) { 557 this.type = type; 558 } 559 560 565 public int getType() { 566 return type; 567 } 568 569 public Object clone() { 570 return new PDU(this); 571 } 572 573 578 public Integer32 getRequestID() { 579 return requestID; 580 } 581 582 589 public void setRequestID(Integer32 requestID) { 590 this.requestID = requestID; 591 } 592 593 600 public static String getTypeString(int type) { 601 switch (type) { 602 case PDU.GET: 603 return "GET"; 604 case PDU.SET: 605 return "SET"; 606 case PDU.GETNEXT: 607 return "GETNEXT"; 608 case PDU.GETBULK: 609 return "GETBULK"; 610 case PDU.INFORM: 611 return "INFORM"; 612 case PDU.RESPONSE: 613 return "RESPONSE"; 614 case PDU.REPORT: 615 return "REPORT"; 616 case PDU.TRAP: 617 return "TRAP"; 618 case PDU.V1TRAP: 619 return "V1TRAP"; 620 } 621 return "unknown"; 622 } 623 624 633 public static int getTypeFromString(String type) { 634 if (type.equals("GET")) { 635 return PDU.GET; 636 } 637 else if (type.equals("SET")) { 638 return PDU.SET; 639 } 640 else if (type.equals("GETNEXT")) { 641 return PDU.GETNEXT; 642 } 643 else if (type.equals("GETBULK")) { 644 return PDU.GETBULK; 645 } 646 else if (type.equals("INFORM")) { 647 return PDU.INFORM; 648 } 649 else if (type.equals("RESPONSE")) { 650 return PDU.RESPONSE; 651 } 652 else if (type.equals("TRAP")) { 653 return PDU.TRAP; 654 } 655 else if (type.equals("V1TRAP")) { 656 return PDU.V1TRAP; 657 } 658 else if (type.equals("REPORT")) { 659 return PDU.REPORT; 660 } 661 return Integer.MIN_VALUE; 662 } 663 664 669 public String toString() { 670 StringBuffer buf = new StringBuffer (); 671 buf.append(getTypeString(type)); 672 buf.append("[requestID="); 673 buf.append(requestID); 674 buf.append(", errorStatus="); 675 buf.append(getErrorStatusText()+"("+errorStatus+")"); 676 buf.append(", errorIndex="); 677 buf.append(errorIndex); 678 buf.append(", VBS["); 679 for (int i=0; i<variableBindings.size(); i++) { 680 buf.append(variableBindings.get(i)); 681 if (i+1 < variableBindings.size()) { 682 buf.append("; "); 683 } 684 } 685 buf.append("]]"); 686 return buf.toString(); 687 } 688 689 695 public int getMaxRepetitions() { 696 return errorIndex.getValue(); 697 } 698 699 705 public void setMaxRepetitions(int maxRepetitions) { 706 this.errorIndex.setValue(maxRepetitions); 707 } 708 709 714 public int getNonRepeaters() { 715 return errorStatus.getValue(); 716 } 717 718 723 public void setNonRepeaters(int nonRepeaters) { 724 this.errorStatus.setValue(nonRepeaters); 725 } 726 727 733 public VariableBinding[] toArray() { 734 VariableBinding[] vbs = new VariableBinding[this.variableBindings.size()]; 735 this.variableBindings.toArray(vbs); 736 return vbs; 737 } 738 } 739 740 | Popular Tags |