Beispiel #1
0
  /**
   * Encodes a <code>Variable</code> to an <code>OutputStream</code>.
   *
   * @param outputStream an <code>OutputStream</code>.
   * @throws IOException if an error occurs while writing to the stream.
   */
  @Override
  public void encodeBER(OutputStream outputStream) throws IOException {
    BER.encodeHeader(outputStream, type, getBERPayloadLength());

    if (type == PDU.V1TRAP) {
      enterprise.encodeBER(outputStream);
      agentAddress.encodeBER(outputStream);
      genericTrap.encodeBER(outputStream);
      specificTrap.encodeBER(outputStream);
      timestamp.encodeBER(outputStream);
    } else {
      requestID.encodeBER(outputStream);
      errorStatus.encodeBER(outputStream);
      errorIndex.encodeBER(outputStream);
    }
    int vbLength = 0;
    for (VariableBinding variableBinding : variableBindings) {
      vbLength += variableBinding.getBERLength();
    }
    BER.encodeHeader(outputStream, BER.SEQUENCE, vbLength);
    for (VariableBinding vb : variableBindings) {
      if (!isVariableV1(vb.getVariable())) {
        throw new IOException("Cannot encode Counter64 into a SNMPv1 PDU");
      }
      vb.encodeBER(outputStream);
    }
  }
Beispiel #2
0
  /**
   * Decodes a <code>Variable</code> from an <code>InputStream</code>.
   *
   * @param inputStream an <code>InputStream</code> containing a BER encoded byte stream.
   * @throws IOException
   */
  @Override
  public void decodeBER(BERInputStream inputStream) throws IOException {
    MutableByte pduType = new MutableByte();
    int length = BER.decodeHeader(inputStream, pduType);
    int pduStartPos = (int) inputStream.getPosition();

    switch (pduType.getValue()) {
      case PDU.SET:
      case PDU.GET:
      case PDU.GETNEXT:
      case PDU.V1TRAP:
      case PDU.RESPONSE:
        break;
        // The following PDU types are not supported by the SNMPv1 standard!
      case PDU.NOTIFICATION:
      case PDU.INFORM:
        if (SNMP4JSettings.isAllowSNMPv2InV1()) {
          break;
        }
        // fall through
      default:
        throw new IOException("Unsupported PDU type: " + pduType.getValue());
    }
    this.setType(pduType.getValue());
    if (getType() == PDU.V1TRAP) {
      enterprise.decodeBER(inputStream);
      agentAddress.decodeBER(inputStream);
      genericTrap.decodeBER(inputStream);
      specificTrap.decodeBER(inputStream);
      timestamp.decodeBER(inputStream);
    } else {
      requestID.decodeBER(inputStream);
      errorStatus.decodeBER(inputStream);
      errorIndex.decodeBER(inputStream);
    }
    // reusing pduType here to save memory ;-)
    pduType = new BER.MutableByte();
    int vbLength = BER.decodeHeader(inputStream, pduType);
    if (pduType.getValue() != BER.SEQUENCE) {
      throw new IOException("Encountered invalid tag, SEQUENCE expected: " + pduType.getValue());
    }
    // rest read count
    int startPos = (int) inputStream.getPosition();
    variableBindings = new Vector<>();
    while (inputStream.getPosition() - startPos < vbLength) {
      VariableBinding vb = new VariableBinding();
      vb.decodeBER(inputStream);
      if (!isVariableV1(vb.getVariable())) {
        throw new MessageException("Counter64 encountered in SNMPv1 PDU " + "(RFC 2576 §4.1.2.1)");
      }
      variableBindings.add(vb);
    }
    if (BER.isCheckSequenceLength()) {
      BER.checkSequenceLength(vbLength, (int) inputStream.getPosition() - startPos, this);
      BER.checkSequenceLength(length, (int) inputStream.getPosition() - pduStartPos, this);
    }
  }
Beispiel #3
0
 @Override
 protected int getBERPayloadLengthPDU() {
   if (getType() != PDU.V1TRAP) {
     return super.getBERPayloadLengthPDU();
   } else {
     int length = 0;
     // length for all vbs
     for (VariableBinding variableBinding : variableBindings) {
       length += variableBinding.getBERLength();
     }
     length += BER.getBERLengthOfLength(length) + 1;
     length += agentAddress.getBERLength();
     length += enterprise.getBERLength();
     length += genericTrap.getBERLength();
     length += specificTrap.getBERLength();
     length += timestamp.getBERLength();
     return length;
   }
 }
Beispiel #4
0
    public void rowChanged(MOTableRowEvent event) {
      if ((event.getType() == MOTableRowEvent.CREATE)
          || (event.getType() == MOTableRowEvent.UPDATED)) {
        // ignore
        return;
      }
      // update counter
      Counter32 counter = (Counter32) event.getRow().getValue(Snmp4jDemoMib.idxSnmp4jDemoEntryCol3);
      if (counter == null) {
        counter = new Counter32(0);
        ((MOMutableTableRow) event.getRow())
            .setValue(Snmp4jDemoMib.idxSnmp4jDemoEntryCol3, counter);
      }
      counter.increment();
      // update timestamp
      TimeStamp timestamp =
          (TimeStamp) event.getTable().getColumn(Snmp4jDemoMib.idxSnmp4jDemoEntryCol4);
      timestamp.update((MOMutableTableRow) event.getRow(), Snmp4jDemoMib.idxSnmp4jDemoEntryCol4);
      // fire notification
      Integer32 type = new Integer32(Snmp4jDemoMib.Snmp4jDemoTableRowModificationEnum.updated);
      switch (event.getType()) {
        case MOTableRowEvent.ADD:
          type.setValue(Snmp4jDemoMib.Snmp4jDemoTableRowModificationEnum.created);
          break;
        case MOTableRowEvent.DELETE:
          type.setValue(Snmp4jDemoMib.Snmp4jDemoTableRowModificationEnum.deleted);
          break;
      }
      VariableBinding[] payload = new VariableBinding[2];
      OID table = event.getTable().getOID();
      OID updateCount = new OID(table);
      updateCount.append(Snmp4jDemoMib.colSnmp4jDemoEntryCol3);
      updateCount.append(event.getRow().getIndex());

      OID modifyType = new OID(table);
      modifyType.append(Snmp4jDemoMib.colSnmp4jDemoTableRowModification);
      modifyType.append(event.getRow().getIndex());

      payload[0] = new VariableBinding(updateCount, counter);
      payload[1] = new VariableBinding(modifyType, type);
      // modules.getSnmp4jDemoMib().snmp4jDemoEvent(
      // agent.getNotificationOriginator(), new OctetString(), payload);
    }
Beispiel #5
0
 /**
  * Gets the specific trap ID. If this value is set, {@link #getGenericTrap()} must return
  * ENTERPRISE_SPECIFIC(6).
  *
  * @return an integer value > 0.
  * @throws UnsupportedOperationException if the type of this PDU is not {@link PDU#V1TRAP}.
  */
 public int getSpecificTrap() {
   checkV1TRAP();
   return specificTrap.getValue();
 }
Beispiel #6
0
 /**
  * Gets the generic trap ID. If this value is ENTERPRISE_SPECIFIC(6), then {@link
  * #getSpecificTrap()} will return the trap ID of the enterprise specific trap.
  *
  * @return an Integer32 instance with a value between 0 and 6.
  * @throws UnsupportedOperationException if the type of this PDU is not {@link PDU#V1TRAP}.
  */
 public int getGenericTrap() {
   checkV1TRAP();
   return genericTrap.getValue();
 }