public void IntegerOctetCountTests() { Assert.AreEqual(1U, Asn1.IntegerOctetCount(0)); Assert.AreEqual(1U, Asn1.IntegerOctetCount(1)); Assert.AreEqual(1U, Asn1.IntegerOctetCount(2)); Assert.AreEqual(1U, Asn1.IntegerOctetCount(-1)); Assert.AreEqual(1U, Asn1.IntegerOctetCount(-2)); Assert.AreEqual(1U, Asn1.IntegerOctetCount(0x7F)); Assert.AreEqual(2U, Asn1.IntegerOctetCount(0x80)); Assert.AreEqual(2U, Asn1.IntegerOctetCount(0x7FFF)); Assert.AreEqual(3U, Asn1.IntegerOctetCount(0x8000)); Assert.AreEqual(3U, Asn1.IntegerOctetCount(0x7FFFFF)); Assert.AreEqual(4U, Asn1.IntegerOctetCount(0x800000)); Assert.AreEqual(4U, Asn1.IntegerOctetCount(0x7FFFFFFF)); Assert.AreEqual(1U, Asn1.IntegerOctetCount(-0x80)); Assert.AreEqual(2U, Asn1.IntegerOctetCount(-0x81)); Assert.AreEqual(2U, Asn1.IntegerOctetCount(-0x8000)); Assert.AreEqual(3U, Asn1.IntegerOctetCount(-0x8001)); Assert.AreEqual(3U, Asn1.IntegerOctetCount(-0x800000)); Assert.AreEqual(4U, Asn1.IntegerOctetCount(-0x800001)); Assert.AreEqual(4U, Asn1.IntegerOctetCount(-0x7FFFFFFF)); Assert.AreEqual(4U, Asn1.IntegerOctetCount((Int32)(-0x80000000))); }
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); }