示例#1
0
        public void TestParseExample1()
        {
            var i      = new Integer32();
            var result = i.Decode(IntegerExample1, 0);

            Assert.Equal(3, result);
            Assert.Equal(3, i.Value);
        }
示例#2
0
        /// <summary>Decode USM portion of the SNMP version 3 packet.</summary>
        /// <param name="buffer">Received SNMP packet BER encoded</param>
        /// <param name="offset">Offset within the buffer to start decoding USM information</param>
        /// <returns>Buffer position after the decoded value</returns>
        /// <exception cref="SnmpDecodingException">Thrown when decoding enountered invalid data type in USM information</exception>
        /// <exception cref="OverflowException">Thrown when packet is too small to contain information length specified in header</exception>
        public override int Decode(byte[] buffer, int offset)
        {
            // Grab the octet string header
            byte type = ParseHeader(buffer, ref offset, out int len);

            if (type != (byte)EAsnType.OctetString)
            {
                throw new SnmpDecodingException("Invalid value type found while looking for USM header.");
            }

            if (len > (buffer.Length - offset))
            {
                throw new OverflowException("Packet too small");
            }

            // Now grab the sequence header
            type = ParseHeader(buffer, ref offset, out len);

            if (type != SnmpConstants.SmiSequence)
            {
                throw new SnmpDecodingException("Sequence missing from USM header.");
            }

            if (len > (buffer.Length - offset))
            {
                throw new OverflowException("Packet too small");
            }

            // now grab values one at the time
            offset = engineId.Decode(buffer, offset);
            offset = engineBoots.Decode(buffer, offset);
            offset = engineTime.Decode(buffer, offset);
            offset = securityName.Decode(buffer, offset);

            int saveOffset = offset;

            offset = authenticationParameters.Decode(buffer, offset);

            if (authenticationParameters.Length > 0)
            {
                // walk through and set the authentication parameters to 0x00 in the packet
                saveOffset += 2; // Skip BER encoded variable type and length

                for (int i = 0; i < authenticationParameters.Length; i++)
                {
                    buffer[saveOffset + i] = 0x00;
                }
            }

            offset = privacyParameters.Decode(buffer, offset);
            return(offset);
        }