コード例 #1
0
        public void Generate()
        {
            var i      = new Integer32(300);
            var buffer = new MutableByte();

            i.Encode(buffer);

            var expected = new byte[]
            {
                SnmpConstants.SmiInteger,   // ASN.1 Type
                0x02,                       // Length
                0x01, 0x2C,                 // 300 in big endian
            };

            Assert.Equal(expected, buffer);
        }
コード例 #2
0
        /// <summary>BER encode security model field.</summary>
        /// <remarks>
        /// USM security model is a SEQUENCE encoded inside a OCTETSTRING. To encode it, first encode the sequence
        /// of class values then "wrap" it inside a OCTETSTRING field
        /// </remarks>
        /// <param name="buffer">Buffer to store encoded USM security model header</param>
        public override void Encode(MutableByte buffer)
        {
            MutableByte tmp = new MutableByte();

            // First encode all the values that will form the sequence
            engineId.Encode(tmp);

            // Encode engine boots
            engineBoots.Encode(tmp);

            // encode engine time
            engineTime.Encode(tmp);
            securityName.Encode(tmp);

            if (authentication != AuthenticationDigests.None)
            {
                if (authenticationParameters.Length <= 0)
                {
                    // If authentication is used, set authentication parameters field to 12 bytes set to 0x00
                    authenticationParameters.Set(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 });
                }
            }
            else
            {
                authenticationParameters.Reset();
            }

            authenticationParameters.Encode(tmp);
            if (privacy != EPrivacyProtocols.None)
            {
                if (privacyParameters.Length <= 0)
                {
                    IPrivacyProtocol privProto = PrivacyProtocol.GetInstance(privacy);
                    if (privProto != null)
                    {
                        byte[] parameter = new byte[privProto.PrivacyParametersLength];
                        for (int i = 0; i < privProto.PrivacyParametersLength; i++)
                        {
                            parameter[i] = 0x00; // This is not necessary since all array members are, by default, initialized to 0
                        }
                        privacyParameters.Set(parameter);
                    }
                    else
                    {
                        throw new SnmpException(SnmpException.EErrorCode.UnsupportedPrivacyProtocol, "Unrecognized privacy protocol specified.");
                    }
                }
            }
            else
            {
                privacyParameters.Reset();
            }

            privacyParameters.Encode(tmp);
            MutableByte tmp1 = new MutableByte();

            BuildHeader(tmp1, SnmpConstants.SmiSequence, tmp.Length);
            tmp1.Append(tmp);

            BuildHeader(buffer, (byte)EAsnType.OctetString, tmp1.Length);

            buffer.Append(tmp1);
        }