snmppdupacket.java

来自「opennms得相关源码 请大家看看」· Java 代码 · 共 756 行 · 第 1/2 页

JAVA
756
字号
//// This file is part of the OpenNMS(R) Application.//// OpenNMS(R) is Copyright (C) 2002-2003 The OpenNMS Group, Inc.  All rights reserved.// OpenNMS(R) is a derivative work, containing both original code, included code and modified// code that was published under the GNU General Public License. Copyrights for modified // and included code are below.//// OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.// // Modifications://// 2003 Mar 20: Added code to allow the joeSNMP library to answer SNMP requests//              to be used in creating SNMP agents.//// Original code base Copyright (C) 1999-2001 Oculan Corp.  All rights reserved.//// This program is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation; either version 2 of the License, or// (at your option) any later version.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the// GNU General Public License for more details.//// You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.//// For more information contact://      OpenNMS Licensing       <license@opennms.org>//      http://www.opennms.org///      http://www.opennms.com///// Tab Size = 8//// SnmpPduPacket.java,v 1.1.1.1 2001/11/11 17:27:22 ben Exp//////// Log://	05/24/00 - Weave//		Fixed type-o where V2TRAP & V2REPORT were wrapped//		using 265 instead of 256 as was correct.////	07/18/00 - Weave//		Fixed an error in the protected constructor found by//		Naz Irizarry. The ctor was using ArrayList.set(...) to//		duplicate the list when it should have been using//		ArrayList.add(...). You cannot call set on an index until//		it has first been added.////// RFC 1902: Structure of Management Information for SNMPv2////   PDU ::= //    SEQUENCE {//      request-id   INTEGER32//      error-status INTEGER//      error-index  INTEGER//      Variable Bindings//    }//// BulkPDU ::=//    SEQUENCE {//      request-id      INTEGER32//      non-repeaters   INTEGER//      max-repetitions INTEGER//      Variable Bindings//    }////// RFC 1157: A Simple Network Management Protocol (SNMP)////   PDU ::= //    SEQUENCE {//      request-id   INTEGER//      error-status INTEGER//      error-index  INTEGER//      Variable Bindings//    }////   TrapPDU ::=//    SEQUENCE {//      enterprise    OBJECTID//	agent-address NetworkAddress//      generic-trap  INTEGER//      specific-trap INTEGER//      time-stamp    TIMETICKS//      Variable Bindings//    }//package org.opennms.protocols.snmp;import java.util.ArrayList;import java.util.Date;import java.util.Random;import org.opennms.protocols.snmp.asn1.ASN1;import org.opennms.protocols.snmp.asn1.AsnDecodingException;import org.opennms.protocols.snmp.asn1.AsnEncoder;import org.opennms.protocols.snmp.asn1.AsnEncodingException;/** * Base class for all Protocol Data Unit (PDU) implementations. The class * defines methods to handle most v1 and v2 implementation of SNMP with only * minor work needed by the derived class. *  * @see SnmpPduRequest * @see SnmpPduBulk *  * @author <a href="mailto:weave@oculan.com">Brian Weaver </a> * @version 1.1.1.1 *  */public abstract class SnmpPduPacket extends Object implements SnmpSyntax, Cloneable {    /**     * The static variable for the class. This variable is used to get a     * "unique" sequence number for each PDU     */    private static int sm_seq = 0;    /**     * The SNMP command for the pdu. See the list of command later on in the     * module for more information.     *      * @see SnmpPduPacket#GET     * @see SnmpPduPacket#GETNEXT     * @see SnmpPduPacket#SET     * @see SnmpPduPacket#RESPONSE     * @see SnmpPduPacket#INFORM     * @see SnmpPduPacket#REPORT     * @see SnmpPduPacket#GETBULK     */    private int m_command; // from pdu    /**     * The request id for this specific packet.     */    private int m_requestId; // from pdu    /**     * The peer of this packet, if we are agent     */    private SnmpPeer m_peer = null;    /**     * The list of variables for this particular PDU. The list may be quite     * large so long as the packet can be received by the remote appliction     */    private ArrayList m_variables; // from pdu    /**     * The error status in a normal pdu, is is used as the non-repeaters in the     * getbulk.     *      * @see SnmpPduRequest     * @see SnmpPduBulk     */    protected int m_errStatus; // is non-repeaters in Bulk!    /**     * The error index in a normal pdu, it is used as the maximum repititions in     * the get bulk pdu.     *      * @see SnmpPduRequest     * @see SnmpPduBulk     */    protected int m_errIndex; // is max-repition in Bulk!    /**     * Default class constructor. Initialzies all primitive members to zero, and     * allocates a new array list for the variables.     *      */    protected SnmpPduPacket() {        m_command = 0;        m_requestId = 0;        m_errStatus = 0;        m_errIndex = 0;        m_variables = new ArrayList();    }    /**     * Class copy constructor. Constructs the object with all the same values as     * the passed packet. The variables are duplicated into a new array so that     * changes to the source pdu will not affect the newly create pdu.     *      * @param second     *            The source pdu to copy values from.     *      */    protected SnmpPduPacket(SnmpPduPacket second) {        m_command = second.m_command;        m_requestId = second.m_requestId;        m_errStatus = second.m_errStatus;        m_errIndex = second.m_errIndex;        int sz = second.m_variables.size();        m_variables = new ArrayList(sz);        for (int x = 0; x < sz; x++) {            m_variables.add(x, ((SnmpVarBind) (second.m_variables.get(x))).duplicate());        }    }    /**     * creates a new pdu with the command set to the passed value.     *      * @param command     *            The type of pdu packet.     *      */    protected SnmpPduPacket(int command) {        this();        m_command = command;    }    /**     * Creates a new pdu with the spcified command and the list of variables.     *      * @param command     *            The type of pdu packet.     * @param vars     *            The variable list for the pdu.     *      */    protected SnmpPduPacket(int command, SnmpVarBind[] vars) {        this(command);        if (vars != null) {            m_variables.ensureCapacity(vars.length);            for (int x = 0; x < vars.length; x++) {                m_variables.add(vars[x].duplicate());            }        }    }    /**     * Use to sequence the all pdu request across the entire library. If the     * sequence id is equal to zero then a random number generator is created     * and is used to seed the sequence.     *      * @return The new sequnce identifier     */    public static synchronized int nextSequence() {        if (sm_seq == 0) {            Date seed = new Date();            Random rnd = new Random(seed.getTime());            sm_seq = rnd.nextInt();        }        return sm_seq++;    }    //    // V1 commands    //    // Add 256 to make them unsigned quantities!    // ASN.CONTEXT is equal 0x80 and will be sign extended. The    // value of 256 will cause the value to be appropiately unsigned!    //    /**     * Defines a SNMPv1 Get Request PDU message.     */    public static final int GET = (int) (ASN1.CONTEXT | ASN1.CONSTRUCTOR | 0) + 256;    /**     * Defines a SNMPv1 Get Next Request PDU message.     */    public static final int GETNEXT = (int) (ASN1.CONTEXT | ASN1.CONSTRUCTOR | 1) + 256;    /**     * Defines a SNMPv1 Response PDU message.     */    public static final int RESPONSE = (int) (ASN1.CONTEXT | ASN1.CONSTRUCTOR | 2) + 256;    /**     * Defines a SNMPv1 PDU Set Request message. The set request uses the     * write-only string from the session. All others use the read-only     * community string.     */    public static final int SET = (int) (ASN1.CONTEXT | ASN1.CONSTRUCTOR | 3) + 256;    //    // V2 commands    //    // Add 256 to make them unsigned quantities!    // ASN.CONTEXT is equal 0x80 and will be sign extended. The    // value of 256 will cause the value to be appropiately unsigned!    //    /**     * Defines a SNMPv2 Get Bulk Request message.     */    public static final int GETBULK = (int) (ASN1.CONTEXT | ASN1.CONSTRUCTOR | 5) + 256;    /**     * Defines a SNMPv2 Inform Request message     */    public static final int INFORM = (int) (ASN1.CONTEXT | ASN1.CONSTRUCTOR | 6) + 256;    /**     * Defines a SNMPv2 Trap message     */    public static final int V2TRAP = (int) (ASN1.CONTEXT | ASN1.CONSTRUCTOR | 7) + 256;    /**     * Defines a SNMPv2 Report message.     */    public static final int REPORT = (int) (ASN1.CONTEXT | ASN1.CONSTRUCTOR | 8) + 256;    //    // V1 errors    //    /**     * No error occured in the request. Also known as a successful request.     */    public static final int ErrNoError = 0;    /**     * The PDU was too large for the agent to process     */    public static final int ErrTooBig = 1;    /**     * There was no such object identifier defined in the agent's tables.     */    public static final int ErrNoSuchName = 2;    /**     * If the object type does not match the object value in the agent's tables.     */    public static final int ErrBadValue = 3;    /**     * Attempting to set a read-only object in the agent's tables.     */    public static final int ErrReadOnly = 4;    /**     * A generic SNMPv1 error occured.     */    public static final int ErrGenError = 5;    //    // V2 Errors    //    /**     * The specified SET request could not access the specified instance.     */    public static final int ErrNoAccess = 6;    /**     * The specified object is not the correct type.     */    public static final int ErrWrongType = 7;    /**     * The specified object is not the correct length.     */    public static final int ErrWrongLength = 8;    /**     * The specified object is not correctly encoded.     */    public static final int ErrWrongEncoding = 9;    /**     * The specified object doe not have the correct value.     */    public static final int ErrWrongValue = 10;    /**

⌨️ 快捷键说明

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