/// <summary> /// Wrap BER encoded SNMP information contained in the parameter <see cref="MutableByte"/> class. /// /// Information in the parameter is prepended by the SNMP version field and wrapped in a sequence header. /// /// Derived classes call this method to finalize SNMP packet encoding. /// </summary> /// <param name="buffer">Buffer containing BER encoded SNMP information</param> public virtual void Encode(MutableByte buffer) { // Encode SNMP protocol version MutableByte temp = new MutableByte(); _protocolVersion.Encode(temp); buffer.Prepend(temp); temp.Reset(); AsnType.BuildHeader(temp, SnmpConstants.SMI_SEQUENCE, buffer.Length); buffer.Prepend(temp); }
/// <summary> /// Encode SNMP packet for sending. /// </summary> /// <returns>BER encoded SNMP packet.</returns> public override byte[] Encode() { MutableByte buf = new MutableByte(); if (Pdu.Type != PduType.Get && Pdu.Type != PduType.GetNext && Pdu.Type != PduType.Set && Pdu.Type != PduType.V2Trap && Pdu.Type != PduType.Response && Pdu.Type != PduType.GetBulk && Pdu.Type != PduType.Inform) { throw new SnmpInvalidPduTypeException("Invalid SNMP PDU type while attempting to encode PDU: " + string.Format("0x{0:x2}", Pdu.Type)); } // snmp version _protocolVersion.Encode(buf); // community string _snmpCommunity.Encode(buf); // pdu _pdu.Encode(buf); // wrap the packet into a sequence MutableByte tmpBuf = new MutableByte(); AsnType.BuildHeader(tmpBuf, SnmpConstants.SMI_SEQUENCE, buf.Length); buf.Prepend(tmpBuf); return(buf); }
/// <summary>BER encode class value.</summary> /// <param name="buffer">Target buffer. Value is appended to the end of it.</param> public override void encode(MutableByte buffer) { MutableByte tmp = new MutableByte(); byte[] b = BitConverter.GetBytes(_value); for (int i = 3; i >= 0; i--) { if (b[i] != 0 || tmp.Length > 0) { tmp.Append(b[i]); } } // if (tmp.Length > 1 && tmp[0] == 0xff && (tmp[1] & 0x80) != 0) if (tmp.Length > 0 && (tmp[0] & 0x80) != 0) { tmp.Prepend(0); } else if (tmp.Length == 0) { tmp.Append(0); } BuildHeader(buffer, Type, tmp.Length); buffer.Append(tmp); }
/// <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); }
/// <summary> Used to encode the integer value into an ASN.1 buffer. /// The passed encoder defines the method for encoding the /// data. /// </summary> /// <param name="buffer">Buffer target to write the encoded data</param> public override void Encode(MutableByte buffer) { int val = _value; byte[] b = BitConverter.GetBytes(_value); MutableByte tmp = new MutableByte(); // if value is negative if (val < 0) { for (int i = 3; i >= 0; i--) { if (tmp.Length > 0 || b[i] != 0xff) { tmp.Append(b[i]); } } if (tmp.Length == 0) { // if the value is -1 then all bytes in an integer are 0xff and will be skipped above tmp.Append(0xff); } // make sure value is negative if ((tmp[0] & 0x80) == 0) { tmp.Prepend(0xff); } } else if (val == 0) { // this is just a shortcut to save processing time tmp.Append(0); } else { // byte[] b = BitConverter.GetBytes(val); for (int i = 3; i >= 0; i--) { if (b[i] != 0 || tmp.Length > 0) { tmp.Append(b[i]); } } // if buffer length is 0 then value is 0 and we have to add it to the buffer if (tmp.Length == 0) { tmp.Append(0); } else { if ((tmp[0] & 0x80) != 0) { // first bit of the first byte has to be 0 otherwise value is negative. tmp.Prepend(0); } } } // check for 9 1s at the beginning of the encoded value if ((tmp.Length > 1 && tmp[0] == 0xff && (tmp[1] & 0x80) != 0)) { tmp.Prepend(0); } BuildHeader(buffer, Type, tmp.Length); buffer.Append(tmp); }