Inheritance: SnmpDatagram
        public void Asn1BigVarBindEncodingTest()
        {
            var varBinds = new VarBind[] {
               new VarBind(new ObjectIdentifier("1.3.6.1.2.1.1.3.0")),
               new VarBind(new ObjectIdentifier("1.3.6.1.2.1.1.3.1"), 1, new Asn1TagInfo(Asn1Tag.Integer)),
               new VarBind(new ObjectIdentifier("1.3.6.1.2.1.1.3.1"), 13, new Asn1TagInfo(Asn1Tag.Integer)),
               new VarBind(new ObjectIdentifier("1.3.6.1.2.1.1.3.1"), 143, new Asn1TagInfo(Asn1Tag.Integer)),
               new VarBind(new ObjectIdentifier("1.3.6.1.2.1.1.3.1"), 122, new Asn1TagInfo(Asn1Tag.Integer)),
               new VarBind(new ObjectIdentifier("1.3.6.1.2.1.1.3.1"), 13456, new Asn1TagInfo(Asn1Tag.Integer)),
               new VarBind(new ObjectIdentifier("1.3.6.1.2.1.1.3.1"), 1334, new Asn1TagInfo(Asn1Tag.Integer)),
               new VarBind(new ObjectIdentifier("1.3.6.1.2.1.1.3.1"), 1444, new Asn1TagInfo(Asn1Tag.Integer)),
               new VarBind(new ObjectIdentifier("1.3.6.1.2.1.1.3.1"), 13, new Asn1TagInfo(Asn1Tag.Integer)),
               new VarBind(new ObjectIdentifier("1.3.6.1.2.1.1.3.1"), 10, new Asn1TagInfo(Asn1Tag.Integer)),
            };

            var packet = new SnmpDatagramV2C(
                DateTimeOffset.MinValue,
                "1.1.1.1",
                new SnmpHeader(SnmpVersion.V2C, "Community"),
                varBinds,
                PduType.SNMPv2Trap,
                509000,
                SnmpErrorStatus.TooBig,
                10000);

            var encoded = packet.ToSnmpEncodedByteArray();
            var snmpPack = encoded.ToSnmpDatagram();

            Assert.IsNull(snmpPack.VarBinds[0].Value);
            Assert.AreEqual(1, (long)snmpPack.VarBinds[1].Value);
        }
示例#2
0
        /// <summary>
        /// To Asn.1/Snmp Encoded byte array.
        /// </summary>
        /// <param name="snmpPacket">The SNMP packet.</param>
        /// <returns>Asn.1 encoded byte array</returns>
        /// <exception cref="System.ArgumentNullException">snmpPacket</exception>
        public static byte[] ToSnmpEncodedByteArray(this SnmpDatagramV2C snmpPacket)
        {
            byte[] dataBytes = new byte[8194];
            int    length    = dataBytes.EncodeVarBinds(0, snmpPacket.VarBinds);

            Array.Resize(ref dataBytes, length);

            int headerLength = SnmpV2MessageHeaderLength + snmpPacket.Header.Community.Length;

            byte[] headerBytes = new byte[headerLength];

            int offset = 0;

            //Encode version
            offset = headerBytes.EncodeInteger(offset, (int)snmpPacket.Header.Version);

            //Encode Community String
            offset = headerBytes.EncodeOctetString(offset, snmpPacket.Header.Community);

            //Encode PDU Type
            offset = headerBytes.EncodeClassConstructType(offset, Asn1Class.ContextSpecific, ConstructType.Constructed, (byte)snmpPacket.PduType);

            int commonPduControlFieldLength =
                1 //pduType
                + snmpPacket.RequestId.GetIntegerLength()
                + ((int)snmpPacket.ErrorStatus).GetIntegerLength()
                + snmpPacket.ErrorIndex.GetIntegerLength()
                + length; //length of varbind values


            //Encode PDU length
            offset = headerBytes.EncodeLength(offset, commonPduControlFieldLength);

            //Encode RequestId
            offset = headerBytes.EncodeInteger(offset, snmpPacket.RequestId);

            //Encode ErrorStatus
            offset = headerBytes.EncodeInteger(offset, (int)snmpPacket.ErrorStatus);

            //Encode ErrorIndex
            offset = headerBytes.EncodeInteger(offset, snmpPacket.ErrorIndex);

            //Encode VarBinds Length
            offset = headerBytes.EncodeClassConstructType(offset, Asn1Class.Universal, ConstructType.Constructed, (byte)Asn1Tag.Sequence);
            offset = headerBytes.EncodeLength(offset, length);

            byte[] allBytes  = new byte[6];
            int    newOffset = 0;

            newOffset = allBytes.EncodeClassConstructType(newOffset, Asn1Class.Universal, ConstructType.Constructed, (byte)Asn1Tag.Sequence);
            newOffset = allBytes.EncodeLength(newOffset, offset + length);
            //Resize and append varbinds to header
            Array.Resize(ref allBytes, newOffset + offset + length);
            Array.Copy(headerBytes, 0, allBytes, newOffset, offset);
            Array.Copy(dataBytes, 0, allBytes, newOffset + offset, length);

            return(allBytes);
        }
        public void Asn1GaugeEncodingTest()
        {
            var sysUpTime = new VarBind(new ObjectIdentifier("1.3.6.1.2.1.1.3.0"),
            506009u, new Asn1TagInfo(Asn1SnmpTag.Gauge));

            var packet = new SnmpDatagramV2C(
                DateTimeOffset.MinValue,
                "1.1.1.1",
                new SnmpHeader(SnmpVersion.V2C, "Community"),
                new[] { sysUpTime },
                PduType.SNMPv2Trap,
                50000,
                SnmpErrorStatus.NoError,
                0);

            var encoded = packet.ToSnmpEncodedByteArray();
            var snmpPack = encoded.ToSnmpDatagram();

            Assert.AreEqual(506009u, (uint)snmpPack.VarBinds[0].Value);
        }
        public void Asn1NullAndNotNullEncodingTest()
        {
            var oidWithNull = new VarBind(new ObjectIdentifier("1.3.6.1.2.1.1.3.0"));
            var oidWithoutNull = new VarBind(new ObjectIdentifier("1.3.6.1.2.1.1.3.1"),
               1, new Asn1TagInfo(Asn1Tag.Integer));

            var packet = new SnmpDatagramV2C(
                DateTimeOffset.MinValue,
                "1.1.1.1",
                new SnmpHeader(SnmpVersion.V2C, "Community"),
                new[] { oidWithNull, oidWithoutNull },
                PduType.SNMPv2Trap,
                50000,
                SnmpErrorStatus.NoError,
                0);

            var encoded = packet.ToSnmpEncodedByteArray();
            var snmpPack = encoded.ToSnmpDatagram();

            Assert.IsNull(snmpPack.VarBinds[0].Value);
            Assert.AreEqual(1, (long)snmpPack.VarBinds[1].Value);
        }
        public void Asn1IpAdressEncodingTest()
        {
            var sysUpTime = new VarBind(new ObjectIdentifier("1.3.6.1.2.1.1.3.0"),
            new System.Net.IPAddress(123453), new Asn1TagInfo(Asn1SnmpTag.IpAddress));

            var packet = new SnmpDatagramV2C(
                DateTimeOffset.MinValue,
                "1.1.1.1",
                new SnmpHeader(SnmpVersion.V2C, "Community"),
                new[] { sysUpTime },
                PduType.SNMPv2Trap,
                50000,
                SnmpErrorStatus.NoError,
                0);

            var encoded = packet.ToSnmpEncodedByteArray();
            var snmpPack = encoded.ToSnmpDatagram();

            Assert.AreEqual("61.226.1.0", snmpPack.VarBinds[0].Value.ToString());
        }