Example #1
0
        /// <summary>Decode received SNMP packet.</summary>
        /// <param name="buffer">BER encoded packet buffer</param>
        /// <param name="length">BER encoded packet buffer length</param>
        /// <returns>Buffer position after the decoded packet.</returns>
        /// <exception cref="SnmpException">Thrown when invalid encoding has been found in the packet</exception>
        /// <exception cref="OverflowException">Thrown when parsed header points to more data then is available in the packet</exception>
        /// <exception cref="SnmpInvalidVersionException">Thrown when parsed packet is not SNMP version 1</exception>
        /// <exception cref="SnmpInvalidPduTypeException">Thrown when received PDU is of a type not supported by SNMP version 1</exception>
        public override int Decode(byte[] buffer, int length)
        {
            MutableByte buf = new MutableByte(buffer, length);

            int offset = 0;

            offset = base.Decode(buffer, buffer.Length);

            if (protocolVersion.Value != (int)ESnmpVersion.Ver1)
            {
                throw new SnmpInvalidVersionException("Invalid protocol version");
            }

            offset = Community.Decode(buf, offset);
            int  tmpOffset = offset;
            byte asnType   = AsnType.ParseHeader(buf, ref tmpOffset, out int headerLength);

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

            if (asnType != (byte)EPduType.Get && asnType != (byte)EPduType.GetNext && asnType != (byte)EPduType.Set && asnType != (byte)EPduType.Response)
            {
                throw new SnmpInvalidPduTypeException("Invalid SNMP operation received: " + string.Format("0x{0:x2}", asnType));
            }

            // Now process the Protocol Data Unit
            offset = Pdu.Decode(buf, offset);

            return(length);
        }