ln.snmp/channel/SnmpV1Channel.cs

50 lines
1.5 KiB
C#

using System;
using ln.snmp.types;
using System.Net;
using System.Collections.Generic;
namespace ln.snmp.channel
{
public class SnmpV1Channel : SnmpPDUChannel
{
public override SnmpVersion SnmpVersion => SnmpVersion.V1;
public OctetString CommunityString { get; set; }
public SnmpV1Channel(SNMPEngine client)
:base(client)
{
CommunityString = "public";
}
public SnmpV1Channel(SNMPEngine client,string communityString)
:base(client)
{
CommunityString = communityString;
}
public override PDU RequestResponse(PDU pdu,IPEndPoint remoteEndpoint)
{
Integer version = new Integer((int)SnmpVersion);
Sequence snmpRequest = new Sequence();
snmpRequest.Add(version);
snmpRequest.Add(CommunityString);
snmpRequest.Add(pdu);
Variable reply = SNMPClient.SNMPRequest(remoteEndpoint, snmpRequest);
Sequence sreply = reply as Sequence;
PDU responsePDU = sreply.Items[2] as PDU;
if (responsePDU.Error.LongValue != 0)
{
string indicator = responsePDU.ErrorIndex > 0 && responsePDU.ErrorIndex <= pdu.VarBinds.Items.Length ? ((pdu.VarBinds.Items[(int)responsePDU.ErrorIndex - 1] as Sequence).Items[0] as ObjectIdentifier).AsString : "";
throw new SnmpError(responsePDU.Error, responsePDU.ErrorIndex, indicator);
}
return responsePDU;
}
}
}