/// <summary> /// Initializes a new instance of the <see cref="SnmpPDU"/> struct with decoupling the reference of varBinds /// </summary> /// <param name="pduType">Type of the pdu.</param> /// <param name="requestId">The request identifier.</param> /// <param name="errorStatus">The error status.</param> /// <param name="errorIndex">Index of the error.</param> /// <param name="varBinds">The variable binds.</param> public SnmpPDU(PduType pduType, int requestId, SnmpErrorStatus errorStatus, int errorIndex, VarBind[] varBinds) { PduType = pduType; RequestId = requestId; ErrorStatus = errorStatus; ErrorIndex = errorIndex; VarBinds = new ReadOnlyCollection <VarBind>((VarBind[])varBinds.Clone()); }
/// <summary> /// Initializes a new instance of the <see cref="SnmpV2cPDU"/> struct without decoupling refrence for varbinds. /// </summary> /// <param name="pduType">Type of the pdu.</param> /// <param name="varBinds">The variable binds.</param> /// <param name="requestId">The request identifier.</param> /// <param name="errorStatus">The error status.</param> /// <param name="errorIndex">Index of the error.</param> internal SnmpV2cPDU(PduType pduType, VarBind[] varBinds, int requestId, SnmpErrorStatus errorStatus, int errorIndex) { PduType = pduType; VarBinds = new ReadOnlyCollection<VarBind>(varBinds); RequestId = requestId; ErrorStatus = errorStatus; ErrorIndex = errorIndex; }
/// <summary> /// Initializes a new instance of the <see cref="SnmpPDU"/> struct without decoupling refrence for varbinds. /// </summary> /// <param name="pduType">Type of the pdu.</param> /// <param name="varBinds">The variable binds.</param> /// <param name="requestId">The request identifier.</param> /// <param name="errorStatus">The error status.</param> /// <param name="errorIndex">Index of the error.</param> internal SnmpPDU(PduType pduType, VarBind[] varBinds, int requestId, SnmpErrorStatus errorStatus, int errorIndex) { PduType = pduType; VarBinds = new ReadOnlyCollection <VarBind>(varBinds); RequestId = requestId; ErrorStatus = errorStatus; ErrorIndex = errorIndex; }
/// <summary> /// Initializes a new instance of the <see cref="SnmpV2cPDU"/> struct with decoupling the reference of varBinds /// </summary> /// <param name="pduType">Type of the pdu.</param> /// <param name="requestId">The request identifier.</param> /// <param name="errorStatus">The error status.</param> /// <param name="errorIndex">Index of the error.</param> /// <param name="varBinds">The variable binds.</param> public SnmpV2cPDU(PduType pduType, int requestId, SnmpErrorStatus errorStatus, int errorIndex, VarBind[] varBinds) { PduType = pduType; RequestId = requestId; ErrorStatus = errorStatus; ErrorIndex = errorIndex; VarBinds = new ReadOnlyCollection<VarBind>((VarBind[])varBinds.Clone()); }
/// <summary> /// Converts the Asn.1 encoded byte array to SNMP Datagram. /// </summary> /// <param name="bytes">The asn.1 encoded bytes.</param> /// <returns> /// SnmpPacket /// </returns> /// <exception cref="System.ArgumentNullException">bytes</exception> /// <exception cref="System.IO.InvalidDataException">Snmp Version V3 not supported</exception> /// <exception cref="System.NotImplementedException">SNMP v1 traps are not yet implemented</exception> public static SnmpDatagram ToSnmpDatagram(this byte[] bytes) { if (bytes == null || bytes.Length == 0) { throw new ArgumentNullException("bytes"); } int offset = 0; int length; offset = bytes.NextValueLength(offset, -1, -1, -1, out length); offset = bytes.NextValueLength(offset, -1, -1, (int)Asn1Tag.Integer, out length); SnmpVersion snmpVersion = (SnmpVersion)bytes.ReadInteger(offset, length); offset += length; if (snmpVersion == SnmpVersion.V3) { throw new InvalidDataException("Snmp Version V3 not supported"); } offset = bytes.NextValueLength(offset, -1, -1, (int)Asn1Tag.OctetString, out length); string community = bytes.ReadOctetString(offset, length); offset += length; PduType pduType = (PduType)(bytes[offset++] & 0x1F); if (pduType == PduType.Trap) { throw new NotImplementedException("SNMP v1 traps are not yet implemented"); } offset = bytes.ReadLength(offset, out length); offset = bytes.NextValueLength(offset, -1, -1, (int)Asn1Tag.Integer, out length); int requestId = bytes.ReadInteger(offset, length); offset += length; offset = bytes.NextValueLength(offset, -1, -1, (int)Asn1Tag.Integer, out length); SnmpErrorStatus errorStatus = (SnmpErrorStatus)bytes.ReadInteger(offset, length); offset += length; offset = bytes.NextValueLength(offset, -1, -1, (int)Asn1Tag.Integer, out length); int errorIndex = bytes.ReadInteger(offset, length); offset += length; offset = bytes.NextValueLength(offset, (int)Asn1Class.Universal, (int)ConstructType.Constructed, (int)Asn1Tag.Sequence, out length); VarBind[] varBinds = bytes.ReadVarBinds(offset, length); return(new SnmpDatagram(new SnmpHeader(snmpVersion, community), new SnmpPDU(pduType, varBinds, requestId, errorStatus, errorIndex))); }
/// <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)); } }
public SnmpDatagramV2C( DateTimeOffset receivedTime, string sourceIpAddress, SnmpHeader header, IReadOnlyList <VarBind> varBinds, PduType pduType, int requestId, SnmpErrorStatus errorStatus, int errorIndex) : base(receivedTime, sourceIpAddress, header, varBinds) { this.PduType = pduType; this.RequestId = requestId; this.ErrorStatus = errorStatus; this.ErrorIndex = errorIndex; }
// returns offset of varbind list public UInt32 Deserialize(Byte[] packet, UInt32 offset) { this.packetBytes = packet; UInt32 initialOffset = offset; // // SNMP Message Type // if (packet[offset] != Asn1.TYPE_SEQUENCE) { throw new FormatException(String.Format( "Expected snmp packet to be of type Sequence but is {0}", Asn1.GetTypeOrNumberString(packet[offset]))); } offset++; UInt32 sequenceLength = Asn1.ParseDefiniteLength(packet, ref offset); UInt32 sequenceOffsetLimit = offset + sequenceLength; if (packet.Length - offset < sequenceLength) { throw new FormatException(String.Format( "Failed to parse snmp packet because it was truncated from {0} bytes to {1}", sequenceLength + (offset - initialOffset), packet.Length - initialOffset)); } // // Version // if (packet[offset] != Asn1.TYPE_INTEGER) { throw new FormatException(String.Format( "Expected snmp version to be of type Integer but is {0}", Asn1.GetTypeOrNumberString(packet[offset]))); } offset++; this.version = Asn1.ParseIntegerToByte(packet, ref offset); if (this.version != 0) { throw new NotImplementedException(String.Format("Snmp version {0} (binary code {1}) is not implemented yet", this.version + 1, this.version)); } // // Community // if (packet[offset] != Asn1.TYPE_OCTET_STRING) { throw new FormatException(String.Format( "Expected snmp community to be of type OctetString but is '{0}'", Asn1.GetTypeOrNumberString(packet[offset]))); } offset++; Int32 communityLength = (Byte)Asn1.ParseDefiniteLength(packet, ref offset); if (communityLength < 0) { throw new FormatException(String.Format("Expected community octet string length to be positive but is {0}", communityLength)); } offset += (UInt32)communityLength; // // PDU Type // Byte pduTypeByte = packet[offset]; offset++; if (pduTypeByte < 0xA0) { throw new FormatException(String.Format("Expected SNMP pdu type to be >= 0xA0 but is {0:2X}", pduTypeByte)); } // // Use similar logic to parse similar types // Get, GetNext, GetResponse, Set if (pduTypeByte <= 0xA3) { SnmpPduType pduType = (SnmpPduType)(pduTypeByte - 0xA0); UInt32 pduLength = Asn1.ParseDefiniteLength(packet, ref offset); if (offset + pduLength != sequenceOffsetLimit) { throw new FormatException(String.Format( "Snmp packet is malformed, the pdu which ends at {0} does not end at the same place as the sequence at {1}", offset + pduLength, sequenceOffsetLimit)); } // // Request ID // if (packet[offset] != Asn1.TYPE_INTEGER) { throw new FormatException(String.Format( "Expected Snmp{0}Pdu to start with the Integer type but started with '{1}'", pduType, Asn1.GetTypeOrNumberString(packet[offset]))); } offset++; this.requestID = Asn1.ParseIntegerToInt32(packet, ref offset); // // Error Status // if (packet[offset] != Asn1.TYPE_INTEGER) { throw new FormatException(String.Format( "Expected Snmp{0}Pdu ErrorStatus to be an Integer type but is '{1}'", pduType, Asn1.GetTypeOrNumberString(packet[offset]))); } offset++; this.errorStatus = (SnmpErrorStatus)Asn1.ParseIntegerToInt32(packet, ref offset); // // Error Index // if (packet[offset] != Asn1.TYPE_INTEGER) { throw new FormatException(String.Format( "Expected Snmp{0}Pdu ErrorIndex to be an Integer type but is '{1}'", pduType, Asn1.GetTypeOrNumberString(packet[offset]))); } offset++; this.errorIndex = Asn1.ParseIntegerToInt32(packet, ref offset); // // Varbind List // if (packet[offset] != Asn1.TYPE_SEQUENCE) { throw new FormatException(String.Format( "Expected varbind list to be of type Sequence but is {0}", Asn1.GetTypeOrNumberString(packet[offset]))); } offset++; UInt32 varbindListLength = Asn1.ParseDefiniteLength(packet, ref offset); if (offset + varbindListLength != sequenceOffsetLimit) { throw new FormatException(String.Format( "Snmp packet is malformed, the varbind list which ends at {0} does not end at the same place as the sequence at {1}", offset + varbindListLength, sequenceOffsetLimit)); } return(offset); // return the offset to the varbind variables } else { throw new NotImplementedException(String.Format("Snmp pdu type {0} is not implemented yet", pduTypeByte)); } }
public SnmpDatagramV2C( DateTimeOffset receivedTime, string sourceIpAddress, SnmpHeader header, IReadOnlyList<VarBind> varBinds, PduType pduType, int requestId, SnmpErrorStatus errorStatus, int errorIndex) : base(receivedTime, sourceIpAddress, header, varBinds) { this.PduType = pduType; this.RequestId = requestId; this.ErrorStatus = errorStatus; this.ErrorIndex = errorIndex; }
/// <summary> /// Converts the Asn.1 encoded byte array to SNMP Datagram. /// </summary> /// <param name="byteSegment">The asn.1 encoded bytes.</param> /// <param name="timestamp">The timestamp when the packet has been received.</param> /// <param name="sourceIpAddress">The source IP address of the packet.</param> /// <returns> /// SnmpPacket /// </returns> /// <exception cref="System.ArgumentNullException">bytes</exception> /// <exception cref="System.IO.InvalidDataException">Snmp Version V3 not supported</exception> public static SnmpDatagram ToSnmpDatagram( this ArraySegment <byte> byteSegment, DateTimeOffset timestamp, string sourceIpAddress) { var bytes = byteSegment.Array; if (bytes == null || bytes.Length == 0) { throw new ArgumentNullException("byteSegment"); } int offset = byteSegment.Offset; int length; offset = bytes.NextValueLength(offset, -1, -1, -1, out length); offset = bytes.NextValueLength(offset, -1, -1, (int)Asn1Tag.Integer, out length); SnmpVersion snmpVersion = (SnmpVersion)bytes.ReadInteger(offset, length); offset += length; if (snmpVersion == SnmpVersion.V3) { throw new InvalidDataException("Snmp Version V3 not supported"); } offset = bytes.NextValueLength(offset, -1, -1, (int)Asn1Tag.OctetString, out length); string community = bytes.ReadOctetString(offset, length); offset += length; PduType pduType = (PduType)(bytes[offset++] & 0x1F); offset = bytes.ReadLength(offset, out length); if (snmpVersion == SnmpVersion.V1 && pduType == PduType.Trap) { offset = bytes.NextValueLength(offset, -1, -1, (int)Asn1Tag.ObjectIdentifier, out length); ObjectIdentifier oid = new ObjectIdentifier(bytes.ReadOids(offset, length)); offset += length; offset = bytes.NextValueLength(offset, -1, -1, (int)Asn1SnmpTag.IpAddress, out length); IPAddress ipAddress = bytes.ReadIpAddress(offset); offset += length; offset = bytes.NextValueLength(offset, -1, -1, (int)Asn1Tag.Integer, out length); GenericTrap genericTrap = (GenericTrap)bytes.ReadInteger(offset, length); offset += length; offset = bytes.NextValueLength(offset, -1, -1, (int)Asn1Tag.Integer, out length); int specificTrap = bytes.ReadInteger(offset, length); offset += length; offset = bytes.NextValueLength(offset, -1, -1, (int)Asn1SnmpTag.TimeTicks, out length); uint timeStamp = bytes.ReadUnsignedInteger(offset, length); offset += length; offset = bytes.NextValueLength(offset, (int)Asn1Class.Universal, (int)ConstructType.Constructed, (int)Asn1Tag.Sequence, out length); VarBind[] varBinds = bytes.ReadVarBinds(offset, length); return(new SnmpDatagramV1( timestamp, sourceIpAddress, new SnmpHeader(snmpVersion, community), varBinds)); } else { offset = bytes.NextValueLength(offset, -1, -1, (int)Asn1Tag.Integer, out length); int requestId = bytes.ReadInteger(offset, length); offset += length; offset = bytes.NextValueLength(offset, -1, -1, (int)Asn1Tag.Integer, out length); SnmpErrorStatus errorStatus = (SnmpErrorStatus)bytes.ReadInteger(offset, length); offset += length; offset = bytes.NextValueLength(offset, -1, -1, (int)Asn1Tag.Integer, out length); int errorIndex = bytes.ReadInteger(offset, length); offset += length; offset = bytes.NextValueLength(offset, (int)Asn1Class.Universal, (int)ConstructType.Constructed, (int)Asn1Tag.Sequence, out length); VarBind[] varBinds = bytes.ReadVarBinds(offset, length); return(new SnmpDatagramV2C( timestamp, sourceIpAddress, new SnmpHeader(snmpVersion, community), varBinds, pduType, requestId, errorStatus, errorIndex)); } }
/// <summary> /// Initializes a new instance of the <see cref="SnmpDatagram"/> class. /// </summary> /// <param name="pduType">Type of the pdu.</param> /// <param name="snmpVersion">The SNMP version.</param> /// <param name="community">The community.</param> /// <param name="requestId">The request identifier.</param> /// <param name="errorStatus">The error status.</param> /// <param name="errorIndex">Index of the error.</param> /// <param name="varBinds">The variable binds.</param> public SnmpDatagram(PduType pduType, SnmpVersion snmpVersion, string community, int requestId, SnmpErrorStatus errorStatus, int errorIndex, VarBind[] varBinds) { Header = new SnmpHeader(snmpVersion, community); PDU = new SnmpPDU(pduType, requestId, errorStatus, errorIndex, varBinds); }
/// <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="SnmpDatagram"/> class. /// </summary> /// <param name="pduType">Type of the pdu.</param> /// <param name="snmpVersion">The SNMP version.</param> /// <param name="community">The community.</param> /// <param name="requestId">The request identifier.</param> /// <param name="errorStatus">The error status.</param> /// <param name="errorIndex">Index of the error.</param> /// <param name="varBinds">The variable binds.</param> public SnmpDatagram(PduType pduType, SnmpVersion snmpVersion, string community, int requestId, SnmpErrorStatus errorStatus, int errorIndex, VarBind[] varBinds) { Header = new SnmpHeader(snmpVersion, community); PduV2c = new SnmpV2cPDU(pduType, requestId, errorStatus, errorIndex, varBinds); PduV1 = default(SnmpV1PDU); }