/// <summary>Decode BER encoded Pdu</summary> /// <remarks> /// Decodes the protocol data unit from the passed buffer. If an error /// occurs during the decoding sequence then an AsnDecodingException is /// thrown by the method. The value is decoded using the AsnEncoder /// passed to the object. /// </remarks> /// <param name="buffer">BER encoded buffer</param> /// <param name="offset">The offset byte to begin decoding</param> /// <returns>Buffer position after the decoded value</returns> /// <exception cref="OverflowException">Thrown when header points to more data then is available.</exception> public override int Decode(byte[] buffer, int offset) { byte asnType = ParseHeader(buffer, ref offset, out int headerLength); if (offset + headerLength > buffer.Length) { throw new OverflowException("Insufficient data in packet"); } base.Type = asnType; // request id offset = requestId.Decode(buffer, offset); // error status offset = errorStatus.Decode(buffer, offset); // error index offset = errorIndex.Decode(buffer, offset); // clean out the current variables VbList.Clear(); // decode the Variable binding collection offset = VbList.Decode(buffer, offset); // if Pdu is an SNMP version 2 TRAP, remove sysUpTime and trapObjectID from the VarBinds array if (Type == EPduType.V2Trap || Type == EPduType.Inform) { if (VbList.Count > 0) { if (VbList[0].Oid.Equals(SnmpConstants.SysUpTime)) { TrapSysUpTime.Set(VbList[0].Value); VbList.RemoveAt(0); // remove sysUpTime } } if (VbList.Count > 0) { if (VbList[0].Oid.Equals(SnmpConstants.TrapObjectId)) { trapObjectID.Set((Oid)VbList[0].Value); VbList.RemoveAt(0); // remove sysUpTime } } } return(offset); }
/// <summary>Return string dump of the Pdu class.</summary> /// <returns>String content of the Pdu class.</returns> public override string ToString() { StringBuilder str = new StringBuilder(); str.Append("PDU-"); switch (base.Type) { case (byte)EPduType.Get: str.Append("Get"); break; case (byte)EPduType.GetNext: str.Append("GetNext"); break; case (byte)EPduType.GetBulk: str.Append("GetBulk"); break; case (byte)EPduType.V2Trap: str.Append("V2Trap"); break; case (byte)EPduType.Response: str.Append("Response"); break; case (byte)EPduType.Inform: str.Append("Inform"); break; default: str.Append("Unknown"); break; } str.Append("\n"); str.AppendFormat("RequestId: {0}\n", RequestId); if (Type != EPduType.GetBulk) { str.AppendFormat("ErrorStatus: {0}\nError Index: {1}\n", ErrorStatus, ErrorIndex); } else { str.AppendFormat("MaxRepeaters: {0}\nNonRepeaters: {1}\n", MaxRepetitions, NonRepeaters); } if (Type == EPduType.V2Trap || Type == EPduType.Inform) { str.AppendFormat("TimeStamp: {0}\nTrapOID: {1}\n", TrapSysUpTime.ToString(), TrapObjectID.ToString()); } str.AppendFormat("VbList entries: {0}\n", VbCount); if (VbCount > 0) { foreach (Vb v in VbList) { str.AppendFormat("Vb: {0}\n", v.ToString()); } } return(str.ToString()); }