Exemple #1
0
 private System.Collections.Generic.Dictionary <string, string> SendPacket(SnmpOperationType pduType, VarBinding variables)
 {
     if (variables == null)
     {
         throw new System.ArgumentNullException("The variables for the " + pduType + " operation is null.");
     }
     return(this.SendPacket(pduType, new System.Collections.Generic.List <VarBinding>
     {
         variables
     }));
 }
Exemple #2
0
        private System.Collections.Generic.Dictionary <string, string> SendPacket(SnmpOperationType pduType, System.Collections.Generic.List <VarBinding> variables)
        {
            if (variables == null || variables.Count < 1)
            {
                throw new System.ArgumentNullException("The variables for the " + pduType + " operation is null or empty.");
            }
            UdpTarget        udpTarget       = null;
            IAgentParameters agentParameters = null;
            Pdu pdu = null;

            System.Collections.Generic.Dictionary <string, string> result3;
            try
            {
                udpTarget = new UdpTarget(System.Net.IPAddress.Parse(this.config.AgentIp), this.config.Port, this.config.Timeout, this.config.Retry);
                if (this.config.Version == SnmpVersionType.Ver3)
                {
                    agentParameters = new SecureAgentParameters();
                    SecureAgentParameters secureAgentParameters = agentParameters as SecureAgentParameters;
                    if (!udpTarget.Discovery(secureAgentParameters))
                    {
                        throw new SnmpException("Discovery failed: The device with ip(" + this.config.AgentIp + ") is unreachable.");
                    }
                    pdu = new ScopedPdu();
                    SnmpV3Config snmpV3Config = this.config as SnmpV3Config;
                    secureAgentParameters.SecurityName.Set(snmpV3Config.UserName);
                    secureAgentParameters.Authentication = (AuthenticationDigests)snmpV3Config.Authentication;
                    secureAgentParameters.AuthenticationSecret.Set(snmpV3Config.AuthSecret);
                    secureAgentParameters.Privacy = (PrivacyProtocols)snmpV3Config.Privacy;
                    secureAgentParameters.PrivacySecret.Set(snmpV3Config.PrivacySecret);
                    secureAgentParameters.Reportable = true;
                }
                else
                {
                    if (this.config.Version == SnmpVersionType.Ver1)
                    {
                        OctetString community = new OctetString(((SnmpV1Config)this.config).Community);
                        agentParameters = new AgentParameters(SnmpVersion.Ver1, community);
                    }
                    else
                    {
                        OctetString community = new OctetString(((SnmpV2Config)this.config).Community);
                        agentParameters = new AgentParameters(SnmpVersion.Ver2, community);
                    }
                    pdu = new Pdu();
                }
                DictionaryUtil dictionaryUtil = new DictionaryUtil();
                foreach (VarBinding current in variables)
                {
                    try
                    {
                        if (current is LeafVarBinding)
                        {
                            if (pduType.Equals(SnmpOperationType.GetTable) || pduType.Equals(SnmpOperationType.Walk))
                            {
                                pdu.Type = PduType.Get;
                            }
                            else
                            {
                                pdu.Type = (PduType)pduType;
                                if (pduType.Equals(SnmpOperationType.GetBulk))
                                {
                                    this.configBulkPdu(pdu, current.MaxRepetition);
                                }
                            }
                            System.Collections.Generic.Dictionary <string, string> result = this.ReceiveResponseWithLeafVB((LeafVarBinding)current, pdu, udpTarget, agentParameters);
                            dictionaryUtil.Add(result);
                        }
                        else
                        {
                            if (agentParameters.Version == SnmpVersion.Ver1)
                            {
                                pdu.Type = PduType.GetNext;
                            }
                            else
                            {
                                pdu.Type = PduType.GetBulk;
                                this.configBulkPdu(pdu, current.MaxRepetition);
                            }
                            System.Collections.Generic.Dictionary <string, string> result2 = this.ReceiveResponseWithTableVB((TableVarBinding)current, pdu, udpTarget, agentParameters);
                            dictionaryUtil.Add(result2);
                        }
                    }
                    catch (System.Exception ex)
                    {
                        if (!ex.Message.Contains("Invalid ASN.1 type encountered 0x00. Unable to continue decoding."))
                        {
                            throw new SnmpException(ex.Message);
                        }
                    }
                }
                result3 = dictionaryUtil.Result;
            }
            catch (System.Exception ex2)
            {
                throw new SnmpException(ex2.Message);
            }
            finally
            {
                if (udpTarget != null)
                {
                    udpTarget.Close();
                }
            }
            return(result3);
        }