/// <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 == PduType.V2Trap || Type == PduType.Inform) { if (_vbs.Count == 0) { // add sysUpTime and trapObjectID to the VbList _vbs.Add(SnmpConstants.SysUpTime, _trapTimeStamp); _vbs.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 (_vbs.Count > 0) { // if the first Vb in the VarBinds array is not sysUpTime append it in the // encoded byte array if (!_vbs[0].Oid.Equals(SnmpConstants.SysUpTime)) { Vb sysUpTimeVb = new Vb(SnmpConstants.SysUpTime, _trapTimeStamp); _vbs.Insert(0, sysUpTimeVb); } } // if we have 2 or more Vbs in the VarBinds array check for trapObjectID Vb if (_vbs.Count > 1) { // if second Vb in the VarBinds array is not trapObjectId encode the value if (!_vbs[1].Oid.Equals(SnmpConstants.TrapObjectId)) { Vb trapObjectIdVb = new Vb(SnmpConstants.TrapObjectId, _trapObjectID); _vbs.Insert(1, trapObjectIdVb); } } } } // encode variable bindings _vbs.Encode(tmpBuffer); // Now encode the header for the PDU BuildHeader(buffer, (byte)Type, tmpBuffer.Length); buffer.Append(tmpBuffer); }
/// <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); variables.Encode(trapBuffer); MutableByte tmpBuffer = new MutableByte(); BuildHeader(tmpBuffer, (byte)EPduType.Trap, trapBuffer.Length); trapBuffer.Prepend(tmpBuffer); buffer.Append(trapBuffer); }