// // 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); }
static Int32 Main(String[] args) { Options options = new Options(); List <String> nonOptionArgs = options.Parse(args); if (nonOptionArgs.Count == 0) { options.PrintUsage(); return(1); } String command = nonOptionArgs[0]; if (nonOptionArgs.Count < 3) { return(options.ErrorAndUsage("Error: Missing command line arguments")); } String hostString = nonOptionArgs[1]; String oidString = nonOptionArgs[2]; IPEndPoint endPoint = EndPoints.ParseIPOrResolveHostWithOptionalPort(hostString, 161, DnsPriority.IPv4ThenIPv6); List <Byte> oidBytes = new List <Byte>(); Snmp.ParseOid(oidString, oidBytes); Byte[] oid = oidBytes.ToArray(); if (command.Equals("get", StringComparison.CurrentCultureIgnoreCase)) { if (nonOptionArgs.Count != 3) { return(options.ErrorAndUsage("Error: operation 'get' requires 2 arguments but there are {0}", nonOptionArgs.Count - 1)); } Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); Byte[] packet = new Byte[Snmp.MaximumBufferForGet( (UInt32)options.community.ArgValue.Length, (UInt32)oid.Length)]; UInt32 snmpContentLength = Snmp.SerializeGet(packet, 0, options.community.ArgValue, Snmp.NextID(), oid); socket.SendTo(packet, (Int32)snmpContentLength, 0, endPoint); } else if (command.Equals("set", StringComparison.CurrentCultureIgnoreCase)) { if (nonOptionArgs.Count != 5) { return(options.ErrorAndUsage("Error: operation 'set' requires 4 arguments but there are {0}", nonOptionArgs.Count - 1)); } Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); Byte[] value = Asn1.ParseValue(nonOptionArgs[3], nonOptionArgs[4]); Byte[] packet = new Byte[Snmp.MaximumBufferForSet( (UInt32)options.community.ArgValue.Length, (UInt32)oid.Length, (UInt32)value.Length)]; UInt32 snmpContentLength = Snmp.SerializeSet(packet, 0, options.community.ArgValue, Snmp.NextID(), oid, value); socket.SendTo(packet, (Int32)snmpContentLength, 0, endPoint); } else { Console.WriteLine("Error: Unknown operation '{0}'", command); return(1); } return(0); }