Esempio n. 1
0
        /// <summary>Encode Pdu class to BER byte buffer</summary>
        /// <remarks>
        /// Encodes the protocol data unit using the passed encoder and stores
        /// the results in the passed buffer. An exception is thrown if an
        /// error occurs with the encoding of the information.
        /// </remarks>
        /// <param name="buffer">The buffer to write the encoded information.</param>
        public override void Encode(MutableByte buffer)
        {
            MutableByte tmpBuffer = new MutableByte();

            // if request id is 0, get a random value
            if (requestId.Value == 0)
            {
                requestId.SetRandom();
            }

            requestId.Encode(tmpBuffer);
            errorStatus.Encode(tmpBuffer);
            errorIndex.Encode(tmpBuffer);

            // if V2TRAP PDU type, add sysUpTime and trapObjectID OIDs before encoding VarBind
            if (Type == EPduType.V2Trap || Type == EPduType.Inform)
            {
                if (VbList.Count == 0)
                {
                    // add sysUpTime and trapObjectID to the VbList
                    VbList.Add(SnmpConstants.SysUpTime, TrapSysUpTime);
                    VbList.Add(SnmpConstants.TrapObjectId, trapObjectID);
                }
                else
                {
                    // Make sure user didn't manually add sysUpTime and trapObjectID values
                    // to the pdu

                    // if we have more then one item in the VarBinds array check for sysUpTime
                    if (VbList.Count > 0)
                    {
                        // if the first Vb in the VarBinds array is not sysUpTime append it in the
                        // encoded byte array
                        if (!VbList[0].Oid.Equals(SnmpConstants.SysUpTime))
                        {
                            Vb sysUpTimeVb = new Vb(SnmpConstants.SysUpTime, TrapSysUpTime);
                            VbList.Insert(0, sysUpTimeVb);
                        }
                    }

                    // if we have 2 or more Vbs in the VarBinds array check for trapObjectID Vb
                    if (VbList.Count > 1)
                    {
                        // if second Vb in the VarBinds array is not trapObjectId encode the value
                        if (!VbList[1].Oid.Equals(SnmpConstants.TrapObjectId))
                        {
                            Vb trapObjectIdVb = new Vb(SnmpConstants.TrapObjectId, trapObjectID);
                            VbList.Insert(1, trapObjectIdVb);
                        }
                    }
                }
            }

            // encode variable bindings
            VbList.Encode(tmpBuffer);

            // Now encode the header for the PDU
            BuildHeader(buffer, (byte)Type, tmpBuffer.Length);
            buffer.Append(tmpBuffer);
        }
Esempio n. 2
0
        /// <summary>ASN.1 encode SNMP version 1 trap</summary>
        /// <param name="buffer"><see cref="MutableByte"/> buffer to the end of which encoded values are appended.</param>
        public override void Encode(MutableByte buffer)
        {
            MutableByte trapBuffer = new MutableByte();

            // encode the enterprise id & address
            _enterprise.Encode(trapBuffer);

            _agentAddr.Encode(trapBuffer);

            _generic.Encode(trapBuffer);

            _specific.Encode(trapBuffer);

            _timeStamp.Encode(trapBuffer);

            VbList.Encode(trapBuffer);
            MutableByte tmpBuffer = new MutableByte();

            BuildHeader(tmpBuffer, (byte)PduType.Trap, trapBuffer.Length);
            trapBuffer.Prepend(tmpBuffer);
            buffer.Append(trapBuffer);
        }