// // The Maximum Buffer assumes every unknown length requires 5 octets // public static UInt32 MaximumBufferForSet(UInt32 communityLength, UInt32 oidLength, UInt32 valueAsnLength) { return (1U + 5U + // SNMP Sequence 3U + // Version 1U + // Community Asn1.DefiniteLengthOctetCount(communityLength) + communityLength + 1U + 5U + // PDU Type 2U + 5U + // Request ID 3U + // Error Status 3U + // Error Index 1U + 5U + // Varbind List 1U + 5U + // Varbind 1U + // Oid Asn1.DefiniteLengthOctetCount(oidLength) + oidLength + valueAsnLength); // Value }
public static UInt32 SerializeSnmp(Byte[] packet, UInt32 offset, String community, Byte pduAsnType, Int32 requestID, IList <Byte> oid, IList <Byte> asnValue) { // // Calculate lengths // UInt32 varbindLength = 1U + Asn1.DefiniteLengthOctetCount((UInt32)oid.Count) + (UInt32)oid.Count + // Oid (UInt32)asnValue.Count; // Value UInt32 varbindListLength = 1U + Asn1.DefiniteLengthOctetCount(varbindLength) + varbindLength; // Varbind UInt32 pduLength = 2U + Asn1.IntegerOctetCount(requestID) + // Request ID 3U + // Error Status 3U + // Error Index 1U + Asn1.DefiniteLengthOctetCount(varbindListLength) + varbindListLength; // VarbindList UInt32 snmpSequenceLength = 3U + // Version 1U + Asn1.IntegerOctetCount(community.Length) + (UInt32)community.Length + // Comunity 1U + Asn1.DefiniteLengthOctetCount(pduLength) + pduLength; // Pdu // // TODO: check that packet is large enough // // // SNMP Message Type // packet[offset++] = Asn1.TYPE_SEQUENCE; offset = packet.SetInteger(offset, snmpSequenceLength); // // Version // packet[offset++] = Asn1.TYPE_INTEGER; packet[offset++] = 1; packet[offset++] = 0; // Version 1 // // Community // packet[offset++] = Asn1.TYPE_OCTET_STRING; offset = packet.SetInteger(offset, (UInt32)community.Length); for (int i = 0; i < community.Length; i++) { packet[offset++] = (Byte)community[i]; } // // PDU Type // packet[offset++] = pduAsnType; offset = packet.SetInteger(offset, pduLength); // // Request ID // packet[offset++] = Asn1.TYPE_INTEGER; Byte requestIDOctetCount = Asn1.IntegerOctetCount(requestID); packet[offset++] = requestIDOctetCount; offset = Asn1.SetInteger(packet, offset, requestID); // // Error Status // packet[offset++] = Asn1.TYPE_INTEGER; packet[offset++] = 1; packet[offset++] = 0; // // Error Index // packet[offset++] = Asn1.TYPE_INTEGER; packet[offset++] = 1; packet[offset++] = 0; // // Variable Bindings // packet[offset++] = Asn1.TYPE_SEQUENCE; offset = packet.SetInteger(offset, varbindListLength); // // Varbind // packet[offset++] = Asn1.TYPE_SEQUENCE; offset = packet.SetInteger(offset, varbindLength); // OID packet[offset++] = Asn1.TYPE_OBJECT_ID; offset = packet.SetInteger(offset, (UInt32)oid.Count); for (Int32 i = 0; i < oid.Count; i++) { packet[offset++] = oid[i]; } // Value for (int i = 0; i < asnValue.Count; i++) { packet[offset++] = asnValue[i]; } return(offset); }