/// <summary> /// 异步SetRequest /// </summary> /// <param name="pdu"></param> /// <param name="callback"></param> /// <returns></returns> public override bool SetRequestAsync(Pdu pdu, SnmpAsyncResponse callback) { bool rs = false; if (m_target == null) { Log.Error("SNMP m_target = null."); return(false); } if (pdu == null) { Log.Error("SNMP请求参数pdu为空"); return(false); } pdu.Type = PduType.Set; try { rs = m_target.RequestAsync(pdu, m_Param, callback); if (!rs) { Log.Error("SNMP异步请求错误"); } } catch (Exception e) { Log.Error(e.Message.ToString()); throw e; } return(rs); }
/// <summary> /// 异步Set操作 /// </summary> /// <param name="lmtPdu"></param> /// <param name="requestId"></param> /// <param name="strIpAddr"></param> /// <returns></returns> public int SnmpSetAsync(CDTLmtbPdu lmtPdu, out long requestId, string strIpAddr) { requestId = 0; Log.Debug("========== SnmpGetSync() Start =========="); string msg = string.Format("pars: lmtPdu={0}, requestId={1}, strIpAddr={2}" , lmtPdu, requestId, strIpAddr); Log.Debug(msg); if (string.IsNullOrEmpty(strIpAddr)) { Log.Error("参数strIpAddr为空"); return(-1); } if (lmtPdu == null) { Log.Error("参数lmtPdu为空"); return(-1); } // TODO: 根据基站ip获取Lmtor信息 //LMTORINFO* pLmtorInfo = CDTAppStatusInfo::GetInstance()->GetLmtorInfo(remoteIpAddr); SnmpHelper snmp = m_SnmpAsync; if (null == snmp) { msg = string.Format("基站[{0}]的snmp连接不存在,无法下发snmp命令"); Log.Error(msg); return(-1); } // TODO lmtPdu.setReqMsgType((int)PduType.Set); Pdu pdu = new Pdu(); requestId = pdu.RequestId; // TODO: bool rs = LmtPdu2SnmpPdu(out pdu, lmtPdu, strIpAddr); if (!rs) { Log.Error("LmtPdu2SnmpPdu()转换错误"); } SnmpAsyncResponse snmpCallback = new SnmpAsyncResponse(this.SnmpCallbackFun); rs = snmp.SetRequestAsync(pdu, snmpCallback); if (!rs) { Log.Error("SNMP 异步请求错误"); return(-1); } return(0); }
/// <summary> /// Make an async discovery request for protocol version 3. /// </summary> /// <param name="param">Agent parameters</param> /// <param name="callback">Callback method</param> /// <returns>True if operation was correctly initiated, otherwise false.</returns> public bool DiscoveryAsync(SecureAgentParameters param, SnmpAsyncResponse callback) { Pdu p = new Pdu(); return(RequestAsync(p, param, callback)); }
/// <summary> /// Make SNMP request. With this method you can make blocked SNMP version 1, 2 and 3 requests of type GET, /// GET-NEXT, GET-BULK, SET and REPORT (request types have to compatible with the SNMP protocol version you /// are using). /// /// This method will pass through any exceptions thrown by parsing classes/methods so see individual packet /// classes, ASN.1 type classes, authentication, privacy, etc. classes for exceptions thrown. /// </summary> /// <param name="pdu">Pdu class (do not pass ScopedPdu)</param> /// <param name="agentParameters">Security information for the request. Use <see cref="AgentParameters"/> /// for SNMP versions 1 and 2 requests. Use <see cref="SecureAgentParameters"/> for SNMP version 3 /// requests.</param> /// <param name="responseCallback">Callback that receives the result of the async operation.</param> /// <returns>True if async request was successfully initiated, otherwise false.</returns> public bool RequestAsync(Pdu pdu, IAgentParameters agentParameters, SnmpAsyncResponse responseCallback) { if (IsBusy) { return(false); // class is busy } _response = null; _response += responseCallback; _agentParameters = agentParameters; byte[] outPacket; if (agentParameters.Version == SnmpVersion.Ver3) { SecureAgentParameters secparams = (SecureAgentParameters)agentParameters; if (secparams.Authentication != AuthenticationDigests.None && secparams.AuthenticationSecret.Length <= 0) { // _response(AsyncRequestResult.AuthenticationError, null); return(false); } if (secparams.Privacy != PrivacyProtocols.None && secparams.PrivacySecret.Length <= 0) { // _response(AsyncRequestResult.PrivacyError, null); return(false); } _noSourceCheck = false; // this option is not valid for SNMP v3 requests ScopedPdu outPdu = new ScopedPdu(pdu); outPdu.ContextEngineId.Set(secparams.EngineId); outPdu.ContextName.Set(secparams.ContextName); SnmpV3Packet packet = new SnmpV3Packet(outPdu); secparams.InitializePacket(packet); try { if (secparams.HasCachedKeys) { outPacket = packet.encode(secparams.AuthenticationKey, secparams.PrivacyKey); } else { outPacket = packet.encode(); } } catch (Exception ex) { ex.GetType(); _response(AsyncRequestResult.EncodeError, packet); return(false); } } else if (agentParameters.Version == (int)SnmpVersion.Ver1) { AgentParameters param = (AgentParameters)agentParameters; _noSourceCheck = param.DisableReplySourceCheck; SnmpV1Packet packet = new SnmpV1Packet(); packet.Pdu.Set(pdu); packet.Community.Set(param.Community); try { outPacket = packet.encode(); } catch (Exception ex) { ex.GetType(); _response(AsyncRequestResult.EncodeError, packet); return(false); } } else if (agentParameters.Version == SnmpVersion.Ver2) { AgentParameters param = (AgentParameters)agentParameters; _noSourceCheck = param.DisableReplySourceCheck; SnmpV2Packet packet = new SnmpV2Packet(); packet.Pdu.Set(pdu); packet.Community.Set(param.Community); try { outPacket = packet.encode(); } catch (Exception ex) { ex.GetType(); _response(AsyncRequestResult.EncodeError, packet); return(false); } } else { throw new SnmpInvalidVersionException("Unsupported SNMP version."); } if (!base.RequestAsync(_address, _port, outPacket, outPacket.Length, _timeout, _retry, new SnmpAsyncCallback(AsyncResponse))) { return(false); } return(true); }
public abstract bool SetRequestAsync(Pdu pdu, SnmpAsyncResponse callback);
/// <summary> /// Make SNMP request. With this method you can make blocked SNMP version 1, 2 and 3 requests of type GET, /// GET-NEXT, GET-BULK, SET and REPORT (request types have to compatible with the SNMP protocol version you /// are using). /// /// This method will pass through any exceptions thrown by parsing classes/methods so see individual packet /// classes, ASN.1 type classes, authentication, privacy, etc. classes for exceptions thrown. /// </summary> /// <param name="pdu">Pdu class (do not pass ScopedPdu)</param> /// <param name="agentParameters">Security information for the request. Use <see cref="AgentParameters"/> /// for SNMP versions 1 and 2 requests. Use <see cref="SecureAgentParameters"/> for SNMP version 3 /// requests.</param> /// <param name="responseCallback">Callback that receives the result of the async operation.</param> /// <returns>True if async request was successfully initiated, otherwise false.</returns> public bool RequestAsync(Pdu pdu, IAgentParameters agentParameters, SnmpAsyncResponse responseCallback) { if (IsBusy) { return false; // class is busy } _response = null; _response += responseCallback; _agentParameters = agentParameters; byte[] outPacket; if (agentParameters.Version == SnmpVersion.Ver3) { SecureAgentParameters secparams = (SecureAgentParameters)agentParameters; if (secparams.Authentication != AuthenticationDigests.None && secparams.AuthenticationSecret.Length <= 0) { // _response(AsyncRequestResult.AuthenticationError, null); return false; } if (secparams.Privacy != PrivacyProtocols.None && secparams.PrivacySecret.Length <= 0) { // _response(AsyncRequestResult.PrivacyError, null); return false; } _noSourceCheck = false; // this option is not valid for SNMP v3 requests ScopedPdu outPdu = new ScopedPdu(pdu); outPdu.ContextEngineId.Set(secparams.EngineId); outPdu.ContextName.Set(secparams.ContextName); SnmpV3Packet packet = new SnmpV3Packet(outPdu); secparams.InitializePacket(packet); try { if (secparams.HasCachedKeys) outPacket = packet.encode(secparams.AuthenticationKey, secparams.PrivacyKey); else outPacket = packet.encode(); } catch( Exception ex ) { ex.GetType(); _response(AsyncRequestResult.EncodeError, packet); return false; } } else if (agentParameters.Version == (int)SnmpVersion.Ver1) { AgentParameters param = (AgentParameters)agentParameters; _noSourceCheck = param.DisableReplySourceCheck; SnmpV1Packet packet = new SnmpV1Packet(); packet.Pdu.Set(pdu); packet.Community.Set(param.Community); try { outPacket = packet.encode(); } catch( Exception ex) { ex.GetType(); _response(AsyncRequestResult.EncodeError, packet); return false; } } else if (agentParameters.Version == SnmpVersion.Ver2) { AgentParameters param = (AgentParameters)agentParameters; _noSourceCheck = param.DisableReplySourceCheck; SnmpV2Packet packet = new SnmpV2Packet(); packet.Pdu.Set(pdu); packet.Community.Set(param.Community); try { outPacket = packet.encode(); } catch( Exception ex ) { ex.GetType(); _response(AsyncRequestResult.EncodeError, packet); return false; } } else { throw new SnmpInvalidVersionException("Unsupported SNMP version."); } if( ! base.RequestAsync(_address, _port, outPacket, outPacket.Length, _timeout, _retry, new SnmpAsyncCallback(AsyncResponse) ) ) { return false; } return true; }
/// <summary> /// Make an async discovery request for protocol version 3. /// </summary> /// <param name="param">Agent parameters</param> /// <param name="callback">Callback method</param> /// <returns>True if operation was correctly initiated, otherwise false.</returns> public bool DiscoveryAsync(SecureAgentParameters param, SnmpAsyncResponse callback) { Pdu p = new Pdu(); return RequestAsync(p, param, callback); }