/// <summary>Construct and send SNMP v3 authPriv Trap</summary> /// <param name="receiver">Trap receiver IP address</param> /// <param name="receiverPort">Trap receiver UDP port number</param> /// <param name="engineId">Sender SNMP engineId</param> /// <param name="senderEngineBoots">Sender SNMP engine boots</param> /// <param name="senderEngineTime">Sender SNMP engine time</param> /// <param name="senderUserName">Security (user) name</param> /// <param name="senderUpTime">Sender upTime</param> /// <param name="trapObjectID">Trap object ID</param> /// <param name="varList">Variable binding list</param> /// <param name="authDigest"> /// Authentication digest. See <see cref="AuthenticationDigests"/> enumeration for /// available digests /// </param> /// <param name="authSecret">Authentication secret</param> /// <param name="privProtocol"> /// Privacy protocol. See <see cref="EPrivacyProtocols"/> enumeration for /// available privacy protocols. /// </param> /// <param name="privSecret">Privacy secret</param> public void SendV3Trap( IpAddress receiver, int receiverPort, byte[] engineId, int senderEngineBoots, int senderEngineTime, string senderUserName, uint senderUpTime, Oid trapObjectID, VbCollection varList, AuthenticationDigests authDigest, byte[] authSecret, EPrivacyProtocols privProtocol, byte[] privSecret ) { SnmpV3Packet packet = new SnmpV3Packet(); packet.Pdu.Type = EPduType.V2Trap; packet.AuthPriv(Encoding.UTF8.GetBytes(senderUserName), authSecret, authDigest, privSecret, privProtocol); packet.SetEngineId(engineId); packet.SetEngineTime(senderEngineBoots, senderEngineTime); packet.ScopedPdu.TrapObjectID.Set(trapObjectID); packet.ScopedPdu.TrapSysUpTime.Value = senderUpTime; packet.ScopedPdu.VbList.Add(varList); packet.MessageFlags.Reportable = false; SendV3Trap(packet, receiver, receiverPort); }
/// <summary> /// Build SNMP discovery response packet. /// </summary> /// <remarks> /// Manager application has to be able to respond to discovery requests to be able to handle /// SNMPv3 INFORM notifications. /// /// In an INFORM packet, engineId value is set to the manager stations id (unlike all other requests /// where agent is the authoritative SNMP engine). For the agent to discover appropriate manager engine /// id, boots and time values (required for authentication and privacy packet handling), manager has to /// be able to respond to the discovery request. /// </remarks> /// <param name="messageId">Message id from the received discovery packet</param> /// <param name="requestId">Request id from the received discovery packets Pdu</param> /// <param name="engineId">Local engine id</param> /// <param name="engineBoots">Number of times local SNMP engine has been restarted</param> /// <param name="engineTime">Time since the engine was started in seconds</param> /// <param name="unknownEngineIdCount">Number of discovery packets received by the local SNMP engine</param> /// <returns>SNMP v3 packet properly formatted as a response to a discovery request</returns> public static SnmpV3Packet DiscoveryResponse(int messageId, int requestId, OctetString engineId, int engineBoots, int engineTime, int unknownEngineIdCount) { SnmpV3Packet packet = new SnmpV3Packet(); packet.Pdu.Type = PduType.Report; packet.Pdu.RequestId = requestId; packet.Pdu.VbList.Add(SnmpConstants.usmStatsUnknownEngineIDs, new Integer32(unknownEngineIdCount)); // discovery response is a report packet. We don't want to receive reports about a report packet.MsgFlags.Reportable = false; packet.SetEngineId(engineId); packet.MessageId = messageId; packet.USM.EngineBoots = engineBoots; packet.USM.EngineTime = engineTime; return packet; }
/// <summary> /// Construct and send SNMP v3 noAuthNoPriv Trap /// </summary> /// <param name="receiver">Trap receiver IP address</param> /// <param name="receiverPort">Trap receiver UDP port number</param> /// <param name="engineId">Sender SNMP engineId</param> /// <param name="senderEngineBoots">Sender SNMP engine boots</param> /// <param name="senderEngineTime">Sender SNMP engine time</param> /// <param name="senderUserName">Security (user) name</param> /// <param name="senderUpTime">Sender upTime</param> /// <param name="trapObjectID">Trap object ID</param> /// <param name="varList">Variable binding list</param> public void SendV3Trap(IpAddress receiver, int receiverPort, byte[] engineId, Int32 senderEngineBoots, Int32 senderEngineTime, string senderUserName, UInt32 senderUpTime, Oid trapObjectID, VbCollection varList) { SnmpV3Packet packet = new SnmpV3Packet(); packet.Pdu.Type = PduType.V2Trap; packet.NoAuthNoPriv(ASCIIEncoding.UTF8.GetBytes(senderUserName)); packet.SetEngineId(engineId); packet.SetEngineTime(senderEngineBoots, senderEngineTime); packet.ScopedPdu.TrapObjectID.Set(trapObjectID); packet.ScopedPdu.TrapSysUpTime.Value = senderUpTime; packet.ScopedPdu.VbList.Add(varList); packet.MsgFlags.Reportable = false; SendV3Trap(packet, receiver, receiverPort); }
/// <summary> /// Build SNMP discovery response packet. /// </summary> /// <remarks> /// Manager application has to be able to respond to discovery requests to be able to handle /// SNMPv3 INFORM notifications. /// /// In an INFORM packet, engineId value is set to the manager stations id (unlike all other requests /// where agent is the authoritative SNMP engine). For the agent to discover appropriate manager engine /// id, boots and time values (required for authentication and privacy packet handling), manager has to /// be able to respond to the discovery request. /// </remarks> /// <param name="messageId">Message id from the received discovery packet</param> /// <param name="requestId">Request id from the received discovery packets Pdu</param> /// <param name="engineId">Local engine id</param> /// <param name="engineBoots">Number of times local SNMP engine has been restarted</param> /// <param name="engineTime">Time since the engine was started in seconds</param> /// <param name="unknownEngineIdCount">Number of discovery packets received by the local SNMP engine</param> /// <returns>SNMP v3 packet properly formatted as a response to a discovery request</returns> public static SnmpV3Packet DiscoveryResponse(Int32 messageId, Int32 requestId, OctetString engineId, Int32 engineBoots, Int32 engineTime, Int32 unknownEngineIdCount) { SnmpV3Packet packet = new SnmpV3Packet(); packet.Pdu.Type = PduType.Report; packet.Pdu.RequestId = requestId; packet.Pdu.VbList.Add(SnmpConstants.usmStatsUnknownEngineIDs, new Integer32(unknownEngineIdCount)); // discovery response is a report packet. We don't want to receive reports about a report packet.MsgFlags.Reportable = false; packet.SetEngineId(engineId); packet.MessageId = messageId; packet.USM.EngineBoots = engineBoots; packet.USM.EngineTime = engineTime; return packet; }
/// <summary> /// Construct and send SNMP v3 authPriv Trap /// </summary> /// <param name="receiver">Trap receiver IP address</param> /// <param name="receiverPort">Trap receiver UDP port number</param> /// <param name="engineId">Sender SNMP engineId</param> /// <param name="senderEngineBoots">Sender SNMP engine boots</param> /// <param name="senderEngineTime">Sender SNMP engine time</param> /// <param name="senderUserName">Security (user) name</param> /// <param name="senderUpTime">Sender upTime</param> /// <param name="trapObjectID">Trap object ID</param> /// <param name="varList">Variable binding list</param> /// <param name="authDigest">Authentication digest. See <see cref="AuthenticationDigests"/> enumeration for /// available digests</param> /// <param name="authSecret">Authentication secret</param> /// <param name="privProtocol">Privacy protocol. See <see cref="PrivacyProtocols"/> enumeration for /// available privacy protocols.</param> /// <param name="privSecret">Privacy secret</param> public void SendV3Trap(IpAddress receiver, int receiverPort, byte[] engineId, Int32 senderEngineBoots, Int32 senderEngineTime, string senderUserName, UInt32 senderUpTime, Oid trapObjectID, VbCollection varList, AuthenticationDigests authDigest, byte[] authSecret, PrivacyProtocols privProtocol, byte[] privSecret) { SnmpV3Packet packet = new SnmpV3Packet(); packet.Pdu.Type = PduType.V2Trap; packet.authPriv(ASCIIEncoding.UTF8.GetBytes(senderUserName), authSecret, authDigest,privSecret, privProtocol); packet.SetEngineId(engineId); packet.SetEngineTime(senderEngineBoots, senderEngineTime); packet.ScopedPdu.TrapObjectID.Set(trapObjectID); packet.ScopedPdu.TrapSysUpTime.Value = senderUpTime; packet.ScopedPdu.VbList.Add(varList); packet.MsgFlags.Reportable = false; SendV3Trap(packet, receiver, receiverPort); }