/// <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); }
/// <summary>Constructor.</summary> /// <remarks>Initializes all values to NULL and PDU type to GET</remarks> public Pdu() { errorIndex = new Integer32(); errorStatus = new Integer32(); requestId = new Integer32(); requestId.SetRandom(); base.Type = (byte)EPduType.Get; VbList = new VbCollection(); TrapSysUpTime = new TimeTicks(); trapObjectID = new Oid(); }
/// <summary> /// Constructor. /// </summary> /// <remarks> /// Initializes all values to NULL and PDU type to GET /// </remarks> public Pdu() { _vbs = null; _errorIndex = new Integer32(); _errorStatus = new Integer32(); _requestId = new Integer32(); _requestId.SetRandom(); _asnType = (byte)PduType.Get; _vbs = new VbCollection(); _trapTimeStamp = new TimeTicks(); _trapObjectID = new Oid(); }