Exemple #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)
        {
            byte asnType = ParseHeader(buffer, ref offset, out int 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
            VbList.Clear();

            offset = VbList.Decode(buffer, offset);

            return(offset);
        }
Exemple #2
0
        /// <summary>Decode BER encoded variable binding.</summary>
        /// <param name="buffer">BER encoded buffer</param>
        /// <param name="offset">
        /// Offset in the data buffer from where to start decoding. Offset is
        /// passed by reference and will contain the offset of the byte immediately after the parsed
        /// variable binding.
        /// </param>
        /// <returns>Buffer position after the decoded value</returns>
        public override int Decode(byte[] buffer, int offset)
        {
            byte asnType = ParseHeader(buffer, ref offset, out int headerLength);

            if (asnType != Type)
            {
                throw new SnmpException(string.Format("Invalid ASN.1 type. Expected 0x{0:x2} received 0x{1:x2}", Type, asnType));
            }

            // verify the length
            if ((buffer.Length - offset) < headerLength)
            {
                throw new OverflowException("Buffer underflow error");
            }

            oid    = new Oid();
            offset = oid.Decode(buffer, offset);
            int saveOffset = offset;

            // Look ahead in the header to see the data type we need to parse
            asnType = ParseHeader(buffer, ref saveOffset, out headerLength);
            value   = SnmpConstants.GetSyntaxObject(asnType);

            if (value == null)
            {
                throw new SnmpDecodingException(string.Format("Invalid ASN.1 type encountered 0x{0:x2}. Unable to continue decoding.", asnType));
            }

            offset = value.Decode(buffer, offset);

            return(offset);
        }