예제 #1
0
        /// <summary>Decode BER encoded SNMP version 1 trap packet</summary>
        /// <param name="buffer">BER encoded buffer</param>
        /// <param name="offset">Offset in the packet to start decoding from</param>
        /// <returns>Buffer position after the decoded value.</returns>
        /// <exception cref="SnmpException">Invalid SNMP Pdu type received. Not an SNMP version 1 Trap PDU.</exception>
        /// <exception cref="SnmpException">Invalid Variable Binding list encoding.</exception>
        public override int decode(byte[] buffer, int offset)
        {
            int  headerLength;
            byte asnType = ParseHeader(buffer, ref offset, out headerLength);

            if (asnType != (byte)PduType.Trap)
            {
                throw new SnmpException("Invalid PDU type.");
            }

            if (headerLength > buffer.Length - offset)
            {
                throw new OverflowException("Packet is too short.");
            }

            offset = _enterprise.decode(buffer, offset);
            offset = _agentAddr.decode(buffer, offset);

            offset = _generic.decode(buffer, offset);

            offset = _specific.decode(buffer, offset);

            offset = _timeStamp.decode(buffer, offset);

            // clean out the current variables
            _variables.Clear();

            offset = _variables.decode(buffer, offset);

            return(offset);
        }
예제 #2
0
        /// <summary>
        /// Decode BER encoded Pdu
        /// </summary>
        /// <remarks>
        /// Decodes the protocol data unit from the passed buffer. If an error
        /// occurs during the decoding sequence then an AsnDecodingException is
        /// thrown by the method. The value is decoded using the AsnEncoder
        /// passed to the object.
        /// </remarks>
        /// <param name="buffer">BER encoded buffer</param>
        /// <param name="offset">The offset byte to begin decoding</param>
        /// <returns>Buffer position after the decoded value</returns>
        /// <exception cref="OverflowException">Thrown when header points to more data then is available.</exception>
        public override int decode(byte[] buffer, int offset)
        {
            int  headerLength;
            byte asnType = ParseHeader(buffer, ref offset, out headerLength);

            if (offset + headerLength > buffer.Length)
            {
                throw new OverflowException("Insufficient data in packet");
            }

            _asnType = asnType;

            // request id
            offset = _requestId.decode(buffer, offset);

            // error status
            offset = _errorStatus.decode(buffer, offset);

            // error index
            offset = _errorIndex.decode(buffer, offset);

            // clean out the current variables
            _vbs.Clear();

            // decode the Variable binding collection
            offset = _vbs.decode(buffer, offset);

            // if Pdu is an SNMP version 2 TRAP, remove sysUpTime and trapObjectID from the VarBinds array
            if (Type == PduType.V2Trap || Type == PduType.Inform)
            {
                if (_vbs.Count > 0)
                {
                    if (_vbs[0].Oid.Equals(SnmpConstants.SysUpTime))
                    {
                        _trapTimeStamp.Set(_vbs[0].Value);
                        _vbs.RemoveAt(0);                         // remove sysUpTime
                    }
                }
                if (_vbs.Count > 0)
                {
                    if (_vbs[0].Oid.Equals(SnmpConstants.TrapObjectId))
                    {
                        _trapObjectID.Set((Oid)_vbs[0].Value);
                        _vbs.RemoveAt(0);                         // remove sysUpTime
                    }
                }
            }

            return(offset);
        }