Esempio n. 1
0
        /// <summary>
        /// Check class equality with argument.
        ///
        /// Accepted argument types are:
        /// * Integer32 - compared against the request id
        /// * Pdu - compared against PduType, request id and contents of VarBind list
        /// </summary>
        /// <param name="obj">Integer32 or Pdu to compare</param>
        /// <returns>True if equal, otherwise false</returns>
        public bool Equals(Integer32 obj)
        {
            if (obj == null)
            {
                return(false);
            }

            if (((Integer32)obj) == _requestId)
            {
                return(true);
            }
            return(false);
        }
Esempio n. 2
0
 /// <summary>
 /// Standard constructor.
 /// </summary>
 public SnmpV3Packet()
     : base(SnmpVersion.Ver3)
 {
     _messageId      = new Integer32();
     _maxMessageSize = new Integer32(64 * 1024);
     MsgFlags        = new MsgFlags
     {
         Reportable = true // Make sure reportable is set to true by default
     };
     _securityModel = new Integer32();
     USM            = new UserSecurityModel();
     ScopedPdu      = new ScopedPdu();
 }
Esempio n. 3
0
        /// <summary>
        /// Get SNMP protocol version from the packet. This routine does not verify if version number is valid. Caller
        /// should verify that returned value represents a valid SNMP protocol version number.
        ///
        /// <code>
        /// int protocolVersion = Packet.GetProtocolVersion(inPacket, inLength);
        /// if( protocolVersion != -1 )
        /// {
        ///     if( protocolVersion == SnmpConstants.SNMPV1 || protocolVersion == SnmpConstants.SNMPV2 || protocolVersion == SnmpConstants.SNMPV3 )
        ///     {
        ///         // do something
        ///     }
        ///     else
        ///     {
        ///         Console.WriteLine("Invalid SNMP protocol version.");
        ///     }
        /// }
        /// else
        /// {
        ///     Console.WriteLine("Invalid SNMP packet.");
        /// }
        /// </code>
        /// </summary>
        /// <param name="buffer">BER encoded SNMP packet</param>
        /// <param name="bufferLength">Length of the BER encoded packet</param>
        /// <returns>Returns SNMP protocol version, if packet is not valid returned value is -1.</returns>
        public static int GetProtocolVersion(byte[] buffer, int bufferLength)
        {
            int  offset  = 0;
            int  length  = 0;
            byte asnType = AsnType.ParseHeader(buffer, ref offset, out length);

            if ((offset + length) > bufferLength)
            {
                return(-1);                // This is not a valid packet
            }
            Integer32 version = new Integer32();

            offset = version.decode(buffer, offset);
            return(version.Value);
        }
Esempio n. 4
0
        /// <summary>Standard constructor.</summary>
        public SnmpV3Packet()
            : base(ESnmpVersion.Ver3)
        {
            messageId      = new Integer32();
            maxMessageSize = new Integer32(64 * 1024);

            messageFlags = new MsgFlags
            {
                Reportable = true, // Make sure reportable is set to true by default
            };

            securityModel     = new Integer32();
            userSecurityModel = new UserSecurityModel();
            scopedPdu         = new ScopedPdu();
        }
        /// <summary>
        /// Reset USM object to default values. All OctetString and MutableByte members are reset to 0 length and
        /// privacy and authentication protocols are set to none.
        /// </summary>
        public void Reset()
        {
            _asnType        = 3;
            _engineId       = new OctetString();
            _engineBoots    = new Integer32();
            _engineTime     = new Integer32();
            _authentication = AuthenticationDigests.None;

            _securityName             = new OctetString();
            _authenticationSecret     = new MutableByte();
            _authenticationParameters = new OctetString();
            _privacySecret            = new MutableByte();
            _privacy           = PrivacyProtocols.None;
            _privacyParameters = new OctetString();
        }
        /// <summary>
        /// Return SNMP type object of the type specified by name. Supported variable types are:
        /// * <see cref="Integer32"/>
        /// * <see cref="Counter32"/>
        /// * <see cref="Gauge32"/>
        /// * <see cref="Counter64"/>
        /// * <see cref="TimeTicks"/>
        /// * <see cref="OctetString"/>
        /// * <see cref="IpAddress"/>
        /// * <see cref="Oid"/>
        /// * <see cref="Null"/>
        /// </summary>
        /// <param name="name">Name of the object type</param>
        /// <returns>New <see cref="AsnType"/> object.</returns>
        public static AsnType GetSyntaxObject(string name)
        {
            AsnType obj = null;

            if (name == "Integer32")
            {
                obj = new Integer32();
            }
            else if (name == "Counter32")
            {
                obj = new Counter32();
            }
            else if (name == "Gauge32")
            {
                obj = new Gauge32();
            }
            else if (name == "Counter64")
            {
                obj = new Counter64();
            }
            else if (name == "TimeTicks")
            {
                obj = new TimeTicks();
            }
            else if (name == "OctetString")
            {
                obj = new OctetString();
            }
            else if (name == "IpAddress")
            {
                obj = new IpAddress();
            }
            else if (name == "Oid")
            {
                obj = new Oid();
            }
            else if (name == "Null")
            {
                obj = new Null();
            }
            else
            {
                throw new ArgumentException("Invalid value type name");
            }

            return(obj);
        }
Esempio n. 7
0
        /// <summary>
        /// Return SNMP type object of the type specified by name. Supported variable types are:
        /// <see cref="Integer32"/>, <see cref="Counter32"/>, <see cref="Gauge32"/>, <see cref="Counter64"/>,
        /// <see cref="TimeTicks"/>, <see cref="OctetString"/>, <see cref="IpAddress"/>, <see cref="Oid"/>, and
        /// <see cref="Null"/>.
        ///
        /// Type names are the same as support class names compared without case sensitivity (e.g. Integer == INTEGER).
        /// </summary>
        /// <param name="name">Name of the object type (not case sensitive)</param>
        /// <returns>New <see cref="AsnType"/> object.</returns>
        public static AsnType GetSyntaxObject(string name)
        {
            AsnType obj = null;

            if (name.ToUpper().Equals("INTEGER32") || name.ToUpper().Equals("INTEGER"))
            {
                obj = new Integer32();
            }
            else if (name.ToUpper().Equals("COUNTER32"))
            {
                obj = new Counter32();
            }
            else if (name.ToUpper().Equals("GAUGE32"))
            {
                obj = new Gauge32();
            }
            else if (name.ToUpper().Equals("COUNTER64"))
            {
                obj = new Counter64();
            }
            else if (name.ToUpper().Equals("TIMETICKS"))
            {
                obj = new TimeTicks();
            }
            else if (name.ToUpper().Equals("OCTETSTRING"))
            {
                obj = new OctetString();
            }
            else if (name.ToUpper().Equals("IPADDRESS"))
            {
                obj = new IpAddress();
            }
            else if (name.ToUpper().Equals("OID"))
            {
                obj = new Oid();
            }
            else if (name.ToUpper().Equals("NULL"))
            {
                obj = new Null();
            }
            else
            {
                throw new ArgumentException("Invalid value type name");
            }

            return(obj);
        }
Esempio n. 8
0
        /// <summary>
        /// Get SNMP protocol version from the packet. This routine does not verify if version number is valid. Caller
        /// should verify that returned value represents a valid SNMP protocol version number.
        ///
        /// <code>
        /// int protocolVersion = Packet.GetProtocolVersion(inPacket, inLength);
        /// if( protocolVersion != -1 )
        /// {
        ///     if( protocolVersion == SnmpConstants.SNMPV1 || protocolVersion == SnmpConstants.SNMPV2 || protocolVersion == SnmpConstants.SNMPV3 )
        ///     {
        ///         // do something
        ///     }
        ///     else
        ///     {
        ///         Console.WriteLine("Invalid SNMP protocol version.");
        ///     }
        /// }
        /// else
        /// {
        ///     Console.WriteLine("Invalid SNMP packet.");
        /// }
        /// </code>
        /// </summary>
        /// <param name="buffer">BER encoded SNMP packet</param>
        /// <param name="bufferLength">Length of the BER encoded packet</param>
        /// <returns>Returns SNMP protocol version, if packet is not valid returned value is -1.</returns>
        /// <exception cref="SnmpDecodingException">Thrown when invalid sequence type is found at the start of the SNMP packet being decoded</exception>
        public static int GetProtocolVersion(byte[] buffer, int bufferLength)
        {
            int  offset  = 0;
            byte asnType = AsnType.ParseHeader(buffer, ref offset, out int length);

            if ((offset + length) > bufferLength)
            {
                return(-1); // This is not a valid packet
            }
            if (asnType != SnmpConstants.SMI_SEQUENCE)
            {
                throw new SnmpDecodingException("Invalid sequence type at the start of the SNMP packet.");
            }
            Integer32 version = new Integer32();

            _ = version.decode(buffer, offset);
            return(version.Value);
        }
Esempio n. 9
0
        /// <summary>
        /// Get SNMP protocol version from the packet. This routine does not verify if version number is valid. Caller
        /// should verify that returned value represents a valid SNMP protocol version number.
        ///
        /// <code>
        /// int protocolVersion = Packet.GetProtocolVersion(inPacket, inLength);
        /// if( protocolVersion != -1 )
        /// {
        ///     if( protocolVersion == SnmpConstants.SNMPV1 || protocolVersion == SnmpConstants.SNMPV2 || protocolVersion == SnmpConstants.SNMPV3 )
        ///         // do something
        ///     else
        ///         Console.WriteLine("Invalid SNMP protocol version.");
        /// }
        /// else
        ///     Console.WriteLine("Invalid SNMP packet.");
        /// </code>
        /// </summary>
        /// <param name="buffer">BER encoded SNMP packet</param>
        /// <param name="bufferLength">Length of the BER encoded packet</param>
        /// <returns>Returns SNMP protocol version, if packet is not valid returned value is -1.</returns>
        /// <exception cref="SnmpDecodingException">Thrown when invalid sequence type is found at the start of the SNMP packet being decoded</exception>
        public static ESnmpVersion GetProtocolVersion(byte[] buffer, int bufferLength)
        {
            int offset = 0;

            byte asnType = AsnType.ParseHeader(buffer, ref offset, out int length);

            if ((offset + length) > bufferLength)
            {
                throw new SnmpDecodingException("Cannot parse SNMP version from packet, input past end");
            }

            if (asnType != SnmpConstants.SmiSequence)
            {
                throw new SnmpDecodingException("Invalid sequence type at the start of the SNMP packet.");
            }

            Integer32 version = new Integer32();

            offset = version.Decode(buffer, offset);

            return((ESnmpVersion)version.Value);
        }
Esempio n. 10
0
        /// <summary>
        /// Reset the class. Initialize all member values to class defaults.
        /// </summary>
        public void Reset()
        {
            _engineId    = new OctetString();
            _engineBoots = new Integer32();
            _engineTime  = new Integer32();

            _engineTimeStamp = DateTime.MinValue;

            _privacyProtocol        = PrivacyProtocols.None;
            _authenticationProtocol = AuthenticationDigests.None;

            _privacySecret        = new MutableByte();
            _authenticationSecret = new MutableByte();

            _contextEngineId = new OctetString();
            _contextName     = new OctetString();
            _securityName    = new OctetString();

            // max message size is initialized to 64KB by default. It will be
            // to the smaller of the two values after discovery process
            _maxMessageSize = new Integer32(64 * 1024);

            _reportable = true;
        }
        /// <summary>Used to create correct variable type object for the specified encoded type</summary>
        /// <param name="asnType">ASN.1 type code</param>
        /// <returns>A new object matching type supplied or null if type was not recognized.</returns>
        public static AsnType GetSyntaxObject(byte asnType)
        {
            AsnType obj = null;

            if (asnType == SnmpConstants.SMI_INTEGER)
            {
                obj = new Integer32();
            }
            else if (asnType == SnmpConstants.SMI_COUNTER32)
            {
                obj = new Counter32();
            }
            else if (asnType == SnmpConstants.SMI_GAUGE32)
            {
                obj = new Gauge32();
            }
            else if (asnType == SnmpConstants.SMI_COUNTER64)
            {
                obj = new Counter64();
            }
            else if (asnType == SnmpConstants.SMI_TIMETICKS)
            {
                obj = new TimeTicks();
            }
            else if (asnType == SnmpConstants.SMI_STRING)
            {
                obj = new OctetString();
            }
            else if (asnType == SnmpConstants.SMI_OPAQUE)
            {
                obj = new Opaque();
            }
            else if (asnType == SnmpConstants.SMI_IPADDRESS)
            {
                obj = new IpAddress();
            }
            else if (asnType == SnmpConstants.SMI_OBJECTID)
            {
                obj = new Oid();
            }
            else if (asnType == SnmpConstants.SMI_PARTY_CLOCK)
            {
                obj = new V2PartyClock();
            }
            else if (asnType == SnmpConstants.SMI_NOSUCHINSTANCE)
            {
                obj = new NoSuchInstance();
            }
            else if (asnType == SnmpConstants.SMI_NOSUCHOBJECT)
            {
                obj = new NoSuchObject();
            }
            else if (asnType == SnmpConstants.SMI_ENDOFMIBVIEW)
            {
                obj = new EndOfMibView();
            }
            else if (asnType == SnmpConstants.SMI_NULL)
            {
                obj = new Null();
            }

            return(obj);
        }
Esempio n. 12
0
 /// <summary>
 /// Constructor. Initialize SNMP version as supplied.
 /// </summary>
 /// <param name="protocolVersion">Protocol version. Acceptable values are SnmpConstants.SNMPV1,
 /// SnmpConstants.SNMPV2 and SnmpConstants.SNMPV3</param>
 public SnmpPacket(SnmpVersion protocolVersion)
 {
     _protocolVersion = new Integer32((int)protocolVersion);
 }
Esempio n. 13
0
 /// <summary>
 /// Constructor. Sets SNMP version to SNMPV1.
 /// </summary>
 public SnmpPacket()
 {
     _protocolVersion = new Integer32((int)SnmpVersion.Ver1);
 }
Esempio n. 14
0
 /// <summary>Constructor. Initialize SNMP version as supplied. </summary>
 /// <param name="protocolVersion">
 /// Protocol version. Acceptable values are SnmpConstants.SNMPV1,
 /// SnmpConstants.SNMPV2 and SnmpConstants.SNMPV3
 /// </param>
 public SnmpPacket(ESnmpVersion protocolVersion)
 {
     this.protocolVersion = new Integer32((int)protocolVersion);
 }
 /// <summary>
 /// Standard constructor
 /// </summary>
 public AgentParameters()
 {
     _version   = new Integer32((int)SnmpVersion.Ver1);
     _community = new OctetString("public");
 }
Esempio n. 16
0
        /// <summary>Used to create correct variable type object for the specified encoded type</summary>
        /// <param name="asnType">ASN.1 type code</param>
        /// <returns>A new object matching type supplied or null if type was not recognized.</returns>
        public static AsnType GetSyntaxObject(ESMIDataTypeCode asnType)
        {
            AsnType obj = null;

            if (asnType == ESMIDataTypeCode.Integer)
            {
                obj = new Integer32();
            }
            else if (asnType == ESMIDataTypeCode.Counter32)
            {
                obj = new Counter32();
            }
            else if (asnType == ESMIDataTypeCode.Gauge32)
            {
                obj = new Gauge32();
            }
            else if (asnType == ESMIDataTypeCode.Counter64)
            {
                obj = new Counter64();
            }
            else if (asnType == ESMIDataTypeCode.TimeTicks)
            {
                obj = new TimeTicks();
            }
            else if (asnType == ESMIDataTypeCode.OctetString)
            {
                obj = new OctetString();
            }
            else if (asnType == ESMIDataTypeCode.Opaque)
            {
                obj = new Opaque();
            }
            else if (asnType == ESMIDataTypeCode.IPAddress)
            {
                obj = new IpAddress();
            }
            else if (asnType == ESMIDataTypeCode.ObjectId)
            {
                obj = new Oid();
            }
            else if (asnType == ESMIDataTypeCode.PartyClock)
            {
                obj = new V2PartyClock();
            }
            else if (asnType == ESMIDataTypeCode.NoSuchInstance)
            {
                obj = new NoSuchInstance();
            }
            else if (asnType == ESMIDataTypeCode.NoSuchObject)
            {
                obj = new NoSuchObject();
            }
            else if (asnType == ESMIDataTypeCode.EndOfMibView)
            {
                obj = new EndOfMibView();
            }
            else if (asnType == ESMIDataTypeCode.Null)
            {
                obj = new Null();
            }

            return(obj);
        }
Esempio n. 17
0
 /// <summary>Standard constructor</summary>
 public AgentParameters()
 {
     version   = new Integer32((int)ESnmpVersion.Ver1);
     community = new OctetString("public");
     disableReplySourceCheck = false;
 }