/// <summary> /// Initializes a new instance of the <see cref="SnmpTrapV2C"/> class. /// </summary> /// <param name="snmpDatagram">The snmp datagram.</param> public SnmpTrapV2C(SnmpDatagram snmpDatagram) { if (snmpDatagram.Header.Version != SnmpVersion.V2C || snmpDatagram.PduV2c.PduType == PduType.Trap) { throw new InvalidDataException("Not a Valid V2c Trap"); } ObjectIdentifier trapOid = new ObjectIdentifier("1.3.6.1.6.3.1.1.4.1.0"); ObjectIdentifier sysUpTimeOid = new ObjectIdentifier("1.3.6.1.2.1.1.3.0"); PduV2c = snmpDatagram.PduV2c; Header = snmpDatagram.Header; TrapOid = default(ObjectIdentifier); SysUpTime = 0; VarBind varBind; if (PduV2c.VarBinds.SearchFirstSubOidWith(sysUpTimeOid, out varBind) && varBind.Asn1TypeInfo.Asn1SnmpTagType == Asn1SnmpTag.TimeTicks) { SysUpTime = (uint)varBind.Value; } if (PduV2c.VarBinds.SearchFirstSubOidWith(trapOid, out varBind) && varBind.Asn1TypeInfo.Asn1TagType == Asn1Tag.ObjectIdentifier) { TrapOid = (ObjectIdentifier)varBind.Value; } }
/// <summary> /// Initializes a new instance of the <see cref="SnmpTrapV2C"/> class. /// </summary> /// <param name="snmpDatagram">The snmp datagram.</param> public SnmpTrapV2C(SnmpDatagram snmpDatagram) { if(snmpDatagram.Header.Version != SnmpVersion.V2C || snmpDatagram.PduV2c.PduType == PduType.Trap) { throw new InvalidDataException("Not a Valid V2c Trap"); } ObjectIdentifier trapOid = new ObjectIdentifier("1.3.6.1.6.3.1.1.4.1.0"); ObjectIdentifier sysUpTimeOid = new ObjectIdentifier("1.3.6.1.2.1.1.3.0"); PduV2c = snmpDatagram.PduV2c; Header = snmpDatagram.Header; TrapOid = default(ObjectIdentifier); SysUpTime = 0; VarBind varBind; if (PduV2c.VarBinds.SearchFirstSubOidWith(sysUpTimeOid, out varBind) && varBind.Asn1TypeInfo.Asn1SnmpTagType == Asn1SnmpTag.TimeTicks) { SysUpTime = (uint)varBind.Value; } if (PduV2c.VarBinds.SearchFirstSubOidWith(trapOid, out varBind) && varBind.Asn1TypeInfo.Asn1TagType == Asn1Tag.ObjectIdentifier) { TrapOid = (ObjectIdentifier)varBind.Value; } }
/// <summary> /// Initializes a new instance of the <see cref="SnmpTrapV1"/> class. /// </summary> /// <param name="snmpDatagram">The snmp datagram.</param> public SnmpTrapV1(SnmpDatagram snmpDatagram) { if (snmpDatagram.Header.Version != SnmpVersion.V1 && snmpDatagram.PduV1.PduType != PduType.Trap) { throw new InvalidDataException("Not a Valid V1 Trap"); } PduV1 = snmpDatagram.PduV1; Header = snmpDatagram.Header; TrapOid = PduV1.Enterprise; SysUpTime = PduV1.TimeStamp; }
public static bool TryParseSnmpDatagram(this IUdpDatagram udpDatagram, out SnmpDatagram snmpDatagram) { try { var segment = udpDatagram.Data.AsByteArraySegment(); snmpDatagram = segment.ToSnmpDatagram(udpDatagram.ReceivedTime, udpDatagram.PacketHeader.SourceIpAddress.ToString()); return(true); } catch { snmpDatagram = default(SnmpDatagram); return(false); } }
public static bool TryParseSnmpDatagram(this IUdpDatagram udpDatagram, out SnmpDatagram snmpDatagram) { try { var segment = udpDatagram.Data.AsByteArraySegment(); snmpDatagram = segment.ToSnmpDatagram(udpDatagram.ReceivedTime, udpDatagram.PacketHeader.SourceIpAddress.ToString()); return true; } catch { snmpDatagram = default(SnmpDatagram); return false; } }
/// <summary> /// Initializes a new instance of the <see cref="SnmpSimpleDatagram" /> class. /// </summary> /// <param name="snmpDatagramInput">The SNMP datagram input.</param> public SnmpSimpleDatagram(SnmpDatagram snmpDatagramInput) { snmpDatagram = snmpDatagramInput; Version = snmpDatagram.Header.Version; Community = snmpDatagram.Header.Community; PDUType = snmpDatagram.PduV2c.PduType; RequestId = snmpDatagram.PduV2c.RequestId; ErrorStatus = snmpDatagram.PduV2c.ErrorStatus; ErrorIndex = snmpDatagram.PduV2c.ErrorIndex; VarBinds = new List <KeyValuePair <string, object> >(); for (int i = 0; i < snmpDatagram.PduV2c.VarBinds.Count; i++) { var varBind = snmpDatagram.PduV2c.VarBinds[i]; VarBinds.Add(new KeyValuePair <string, object>(varBind.Oid.ToString(), varBind.Value)); } }
/// <summary> /// Initializes a new instance of the <see cref="SnmpSimpleDatagram" /> class. /// </summary> /// <param name="snmpDatagramInput">The SNMP datagram input.</param> public SnmpSimpleDatagram(SnmpDatagram snmpDatagramInput) { snmpDatagram = snmpDatagramInput; Version = snmpDatagram.Header.Version; Community = snmpDatagram.Header.Community; PDUType = snmpDatagram.PduV2c.PduType; RequestId = snmpDatagram.PduV2c.RequestId; ErrorStatus = snmpDatagram.PduV2c.ErrorStatus; ErrorIndex = snmpDatagram.PduV2c.ErrorIndex; VarBinds = new List<KeyValuePair<string, object>>(); for(int i=0 ; i< snmpDatagram.PduV2c.VarBinds.Count; i++) { var varBind = snmpDatagram.PduV2c.VarBinds[i]; VarBinds.Add(new KeyValuePair<string, object>(varBind.Oid.ToString(), varBind.Value)); } }
/// <summary> /// To Asn.1/Snmp Encoded byte array. /// </summary> /// <param name="snmpPacket">The SNMP packet.</param> /// <returns>Asn.1 encoded byte array</returns> /// <exception cref="System.ArgumentNullException">snmpPacket</exception> public static byte[] ToSnmpEncodedByteArray(this SnmpDatagram snmpPacket) { if (snmpPacket == null) { throw new ArgumentNullException("snmpPacket"); } if (snmpPacket.Header.Version == SnmpVersion.V3) { throw new InvalidDataException("Snmp Version V3 not supported"); } byte[] dataBytes = new byte[8194]; int length = dataBytes.EncodeVarBinds(0, snmpPacket.PDU.VarBinds); Array.Resize(ref dataBytes, length); int headerLength = SnmpV2MessageHeaderLength + snmpPacket.Header.Community.Length; byte[] headerBytes = new byte[headerLength]; int offset = 0; //Encode version offset = headerBytes.EncodeInteger(offset, (int)snmpPacket.Header.Version); //Encode Community String offset = headerBytes.EncodeOctetString(offset, snmpPacket.Header.Community); //Encode PDU Type offset = headerBytes.EncodeClassConstructType(offset, Asn1Class.ContextSpecific, ConstructType.Constructed, (byte)snmpPacket.PDU.PduType); int CommonPduControlFieldLength = 1 //pduType + snmpPacket.PDU.RequestId.GetIntegerLength() + ((int)snmpPacket.PDU.ErrorStatus).GetIntegerLength() + snmpPacket.PDU.ErrorIndex.GetIntegerLength() + length; //length of varbind values //Encode PDU length offset = headerBytes.EncodeLength(offset, CommonPduControlFieldLength); //Encode RequestId offset = headerBytes.EncodeInteger(offset, snmpPacket.PDU.RequestId); //Encode ErrorStatus offset = headerBytes.EncodeInteger(offset, (int)snmpPacket.PDU.ErrorStatus); //Encode ErrorIndex offset = headerBytes.EncodeInteger(offset, snmpPacket.PDU.ErrorIndex); //Encode VarBinds Length offset = headerBytes.EncodeClassConstructType(offset, Asn1Class.Universal, ConstructType.Constructed, (byte)Asn1Tag.Sequence); offset = headerBytes.EncodeLength(offset, length); byte[] allBytes = new byte[6]; int newOffset = 0; newOffset = allBytes.EncodeClassConstructType(newOffset, Asn1Class.Universal, ConstructType.Constructed, (byte)Asn1Tag.Sequence); newOffset = allBytes.EncodeLength(newOffset, offset + length); //Resize and append varbinds to header Array.Resize(ref allBytes, newOffset + offset + length); Array.Copy(headerBytes, 0, allBytes, newOffset, offset); Array.Copy(dataBytes, 0, allBytes, newOffset + offset, length); return(allBytes); }
/// <summary> /// Initializes a new instance of the <see cref="SnmpDatagram"/> class. /// Copy construtor /// </summary> /// <param name="snmpPacket">The SNMP packet.</param> public SnmpDatagram(SnmpDatagram snmpPacket) { Header = snmpPacket.Header; PduV1 = snmpPacket.PduV1; PduV2c = snmpPacket.PduV2c; }