⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pduv1.java

📁 snmp4j
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*_############################################################################
  _##
  _##  SNMP4J - PDUv1.java
  _##
  _##  Copyright 2003-2005  Frank Fock and Jochen Katz (SNMP4J.org)
  _##
  _##  Licensed under the Apache License, Version 2.0 (the "License");
  _##  you may not use this file except in compliance with the License.
  _##  You may obtain a copy of the License at
  _##
  _##      http://www.apache.org/licenses/LICENSE-2.0
  _##
  _##  Unless required by applicable law or agreed to in writing, software
  _##  distributed under the License is distributed on an "AS IS" BASIS,
  _##  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  _##  See the License for the specific language governing permissions and
  _##  limitations under the License.
  _##
  _##########################################################################*/



package org.snmp4j;

import java.io.*;
import java.util.*;
import org.snmp4j.asn1.*;
import org.snmp4j.asn1.BER.*;
import org.snmp4j.smi.*;
import org.snmp4j.smi.OID;
import org.snmp4j.log.*;
// for JavaDoc
import org.snmp4j.mp.SnmpConstants;

/**
 * The <code>PDUv1</class> represents SNMPv1 PDUs. The behavior of this class
 * is identical to its superclass {@link PDU} for the PDU type {@link PDU#GET},
 * {@link PDU#GETNEXT}, and {@link PDU#SET}. The other SNMPv2 PDU types
 * implemented by <code>PDU</code> are not supported. In contrast to its super
 * class, <code>PDUv1</code> implements the {@link PDU#V1TRAP} type.
 *
 * <p>To support this type, access methods are provided to get and set the
 * enterprise <code>OID</code>, generic, specific, and timestamp of a SNMPv1
 * trap PDU.
 *
 * <p>The constants defined for generic SNMPv1 traps are included in this class.
 * The descriptions are taken from the SNMPv2-MIB (RFC 3418). The corresponding
 * OIDs are defined in {@link SnmpConstants}.
 *
 * @author Frank Fock
 * @version 1.1
 */
public class PDUv1 extends PDU {

  /**
   * A coldStart(0) trap signifies that the SNMP entity,
   * supporting a notification originator application, is
   * reinitializing itself and that its configuration may
   * have been altered.
   */
  public static final int COLDSTART = 0;

  /**
   * A warmStart(1) trap signifies that the SNMP entity,
   * supporting a notification originator application,
   * is reinitializing itself such that its configuration
   * is unaltered.
   */
  public static final int WARMSTART = 1;

  /**
   * A linkDown(2) trap signifies that the SNMP entity, acting in
   * an agent role, has detected that the ifOperStatus object for
   * one of its communication links is about to enter the down
   * state from some other state (but not from the notPresent
   * state).  This other state is indicated by the included value
   * of ifOperStatus.
   */
  public static final int LINKDOWN = 2;

  /**
   * A linkUp(3) trap signifies that the SNMP entity, acting in an
   * agent role, has detected that the ifOperStatus object for
   * one of its communication links left the down state and
   * transitioned into some other state (but not into the
   * notPresent state).  This other state is indicated by the
   * included value of ifOperStatus.
   */
  public static final int LINKUP = 3;

  /**
   * An authenticationFailure(4) trap signifies that the SNMP
   * entity has received a protocol message that is not
   * properly authenticated.  While all implementations
   * of SNMP entities MAY be capable of generating this
   * trap, the snmpEnableAuthenTraps object indicates
   * whether this trap will be generated.
   */
  public static final int AUTHENTICATIONFAILURE = 4;

  /**
   * If the generic trap identifier is <code>ENTERPRISE_SPECIFIC</code>(6), then
   * the enterprise specific trap ID is given by the specificTrap member field.
   */
  public static final int ENTERPRISE_SPECIFIC = 6;

  private static final String OPERATION_NOT_SUPPORTED =
      "Operation not supported for SNMPv1 PDUs";

  private OID enterprise = new OID();
  private IpAddress agentAddress = new IpAddress("0.0.0.0");
  private Integer32 genericTrap = new Integer32(0);
  private Integer32 specificTrap = new Integer32(0);
  private TimeTicks timestamp = new TimeTicks(0);


  public PDUv1() {
    setType(V1TRAP);
  }

  /**
   * Decodes a <code>Variable</code> from an <code>InputStream</code>.
   *
   * @param inputStream an <code>InputStream</code> containing a BER encoded
   *   byte stream.
   * @throws IOException
   */
  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;
      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);
      variableBindings.add(vb);
    }
    if (BER.isCheckSequenceLength()) {
      BER.checkSequenceLength(vbLength,
                              (int) inputStream.getPosition() - startPos, this);
      BER.checkSequenceLength(length,
                              (int) inputStream.getPosition() - pduStartPos, this);
    }
  }

  /**
   * 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.
   */
  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 (int i=0; i<variableBindings.size(); i++) {
      vbLength += ((VariableBinding)variableBindings.get(i)).getBERLength();
    }
    BER.encodeHeader(outputStream, BER.SEQUENCE, vbLength);
    for (int i=0; i<variableBindings.size(); i++) {
      ((VariableBinding)variableBindings.get(i)).encodeBER(outputStream);
    }
  }

  protected int getBERPayloadLengthPDU() {
    if (getType() != PDU.V1TRAP) {
      return super.getBERPayloadLengthPDU();
    }
    else {
      int length = 0;
      // length for all vbs
      for (int i = 0; i < variableBindings.size(); i++) {
        length += ((VariableBinding)variableBindings.get(i)).getBERLength();
      }
      length += BER.getBERLengthOfLength(length) + 1;
      length += agentAddress.getBERLength();
      length += enterprise.getBERLength();
      length += genericTrap.getBERLength();
      length += specificTrap.getBERLength();
      length += timestamp.getBERLength();
      return length;
    }
  }

  /**
   * This method is not supported for SNMPv1 PDUs and will throw a
   * {@link java.lang.UnsupportedOperationException}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -