示例#1
0
  /** Return the BER encoding of this object. */
  protected byte[] getBEREncoding() {

    ByteArrayOutputStream outBytes = new ByteArrayOutputStream();

    byte type = SNMPBERCodec.SNMPUNKNOWNOBJECT;

    // calculate encoding for length of data
    byte[] len = SNMPBERCodec.encodeLength(data.length);

    // encode T,L,V info
    outBytes.write(type);
    outBytes.write(len, 0, len.length);
    outBytes.write(data, 0, data.length);

    return outBytes.toByteArray();
  }
  /** Return BER encoding for this object identifier. */
  protected byte[] getBEREncoding() {
    ByteArrayOutputStream outBytes = new ByteArrayOutputStream();

    // write contents of array of values
    byte[] data = encodeArray();

    // calculate encoding for length of data
    byte[] len = SNMPBERCodec.encodeLength(data.length);

    // encode T,L,V info
    outBytes.write(tag.getByte());
    outBytes.write(len, 0, len.length);
    outBytes.write(data, 0, data.length);

    return outBytes.toByteArray();
  }
  /**
   * The run() method for the trap interface's listener. Just waits for trap or inform messages to
   * come in on port 162, then dispatches the recieved PDUs to each of the registered listeners by
   * calling their processTrap() or processInform() methods.
   */
  @Override
  public void run() {

    int errorStatus = 0;
    int errorIndex = 0;

    while (!receiveThread.isInterrupted()) {

      try {

        DatagramPacket inPacket =
            new DatagramPacket(new byte[receiveBufferSize], receiveBufferSize);

        dSocket.receive(inPacket);

        byte[] encodedMessage = inPacket.getData();

        // get IP address of sender, to supply with SNMPv2 traps
        // which don't include this in the PDU
        InetAddress agentIPAddress = inPacket.getAddress();

        /*
        errorLog.println("Message bytes length (in): " + inPacket.getLength());

        errorLog.println("Message bytes (in):");
        for (int i = 0; i < encodedMessage.length; ++i)
        {
            errorLog.print(hexByte(encodedMessage[i]) + " ");
        }
        errorLog.println("\n");
        */

        SNMPMessage receivedMessage =
            new SNMPMessage(SNMPBERCodec.extractNextTLV(encodedMessage, 0).value);

        String communityName = receivedMessage.getCommunityName();

        Object receivedPDU = receivedMessage.getPDUAsObject();

        if (!(receivedPDU instanceof SNMPv1TrapPDU)
            && !(receivedPDU instanceof SNMPv2TrapPDU)
            && !(receivedPDU instanceof SNMPv2InformRequestPDU)) {
          throw new SNMPBadValueException(
              "PDU received that's not a v1 or v2 trap or inform request; message payload of type "
                  + receivedPDU.getClass().toString());
        }

        // pass the received trap PDU to the processTrap or procesv2Trap method of any listeners
        if (receivedPDU instanceof SNMPv1TrapPDU) {
          for (int i = 0; i < v1TrapListenerVector.size(); i++) {
            SNMPv1TrapListener listener = (SNMPv1TrapListener) v1TrapListenerVector.elementAt(i);

            listener.processv1Trap((SNMPv1TrapPDU) receivedPDU, communityName);
          }
        } else if (receivedPDU instanceof SNMPv2TrapPDU) {
          for (int i = 0; i < v2TrapListenerVector.size(); i++) {
            SNMPv2TrapListener listener = (SNMPv2TrapListener) v2TrapListenerVector.elementAt(i);

            listener.processv2Trap((SNMPv2TrapPDU) receivedPDU, communityName, agentIPAddress);
          }
        } else if (receivedPDU instanceof SNMPv2InformRequestPDU) {
          for (int i = 0; i < v2InformRequestListenerVector.size(); i++) {
            SNMPv2InformRequestListener listener =
                (SNMPv2InformRequestListener) v2InformRequestListenerVector.elementAt(i);

            listener.processv2InformRequest(
                (SNMPv2InformRequestPDU) receivedPDU, communityName, agentIPAddress);
          }
        }

      } catch (IOException e) {
        // just report the problem
        errorLog.println("IOException during request processing: " + e.toString());
        errorLog.flush();
      } catch (SNMPBadValueException e) {
        // just report the problem
        errorLog.println("SNMPBadValueException during request processing: " + e.toString());
        errorLog.flush();
      } catch (Exception e) {
        // just report the problem
        errorLog.println("Exception during request processing: " + e.toString());
        errorLog.flush();
      }
    }
  }