/// <summary>Encode SNMP packet for sending.</summary> /// <returns>BER encoded SNMP packet.</returns> /// <exception cref="SnmpInvalidPduTypeException"> /// Thrown when PDU being encoded is not a valid SNMP version 1 PDU. Acceptable /// protocol version 1 operations are GET, GET-NEXT, SET and RESPONSE. /// </exception> public override byte[] Encode() { if ( Pdu.Type != EPduType.Get && Pdu.Type != EPduType.GetNext && Pdu.Type != EPduType.Set && Pdu.Type != EPduType.Response) { throw new SnmpInvalidVersionException("Invalid SNMP PDU type while attempting to encode PDU: " + string.Format("0x{0:x2}", Pdu.Type)); } if (Pdu.RequestId == 0) { Random rand = new Random((int)DateTime.Now.Ticks); Pdu.RequestId = rand.Next(); } MutableByte tmpBuffer = new MutableByte(); // snmp version protocolVersion.Encode(tmpBuffer); // community string Community.Encode(tmpBuffer); // pdu Pdu.Encode(tmpBuffer); MutableByte buf = new MutableByte(); // wrap the packet into a sequence AsnType.BuildHeader(buf, SnmpConstants.SmiSequence, tmpBuffer.Length); buf.Append(tmpBuffer); return(buf); }