Example #1
0
        /// <summary>
        /// Converts a ASN.1 Encoded Variable to a System UInt32
        /// </summary>
        /// <returns>A System UInt32 from the Encoded Value</returns>
        public UInt32 ToUInt32()
        {
            List <byte> raw      = ToBytes();
            int         position = 0;

            return(BasicEncodingRules.DecodeUInteger32(ref raw, ref position));
        }
Example #2
0
        /// <summary>
        /// Converts a ASN.1 ObjectIdentifier to a System String
        /// </summary>
        /// <returns></returns>
        public string ToObjectIdentifier()
        {
            int         pos = 0;
            List <byte> tlv = ToBytes();

            return(BasicEncodingRules.DecodeObjectId(ref tlv, ref pos));
        }
Example #3
0
        /// <summary>
        /// Converts a ASN.1 Encoded Variable to a System String
        /// </summary>
        /// <returns></returns>
        public override String ToString()
        {
            List <byte> raw      = ToBytes();
            int         position = 0;

            return(BasicEncodingRules.DecodeOctetString(BasicEncodingRules.DecodeOctetString(ref raw, ref position)));
        }
Example #4
0
        /// <summary>
        /// Constructs a Managed Protocol Data Unit from a List of Byte
        /// </summary>
        /// <param name="Pdu">The List of Byte to Construct a Protocol Data Unit from</param>
        public ProtocolDataUnit(List <byte> SnmpPacket)
        {
            int Pos = 0;
            int End = BasicEncodingRules.DecodeLength(ref SnmpPacket, ref Pos, SnmpType.Sequence);

            Version       = BasicEncodingRules.DecodeInteger32(ref SnmpPacket, ref Pos);
            CommunityName = BasicEncodingRules.DecodeOctetString(BasicEncodingRules.DecodeOctetString(ref SnmpPacket, ref Pos));
            PduType       = (SnmpType)SnmpPacket[Pos];

            BasicEncodingRules.DecodeLength(ref SnmpPacket, ref Pos, PduType);
            RequestId   = (int)BasicEncodingRules.DecodeInteger32(ref SnmpPacket, ref Pos);
            ErrorStatus = (ErrorStatus)BasicEncodingRules.DecodeInteger32(ref SnmpPacket, ref Pos);
            ErrorIndex  = (int)BasicEncodingRules.DecodeInteger32(ref SnmpPacket, ref Pos);

            //Gets PDU Length
            int pduLeng = BasicEncodingRules.DecodeLength(ref SnmpPacket, ref Pos, SnmpType.Sequence);

            if (Pos >= End)
            {
                return;
            }
            Bindings = new List <Variable>();
            while (Pos < End)
            {
                try
                {
                    BasicEncodingRules.DecodeLength(ref SnmpPacket, ref Pos, SnmpType.Sequence);
                    Bindings.Add(BasicEncodingRules.DecodeSequence(ref SnmpPacket, ref Pos));
                }
                catch
                {
                    throw;
                }
            }
        }
Example #5
0
        /// <summary>
        /// Creates a Type, Length, Value (TLV) from this Variable instance
        /// </summary>
        /// <returns>The bytes which represent this Variable as a TLV</returns>
        public List <byte> ToBytes()
        {
            List <byte> TLV = new List <byte>();

            TLV.Add((byte)TypeCode);
            TLV.AddRange(BasicEncodingRules.EncodeLength(Length));
            TLV.AddRange(Value);

            return(TLV);
        }
        /// <summary>
        /// Encodes a Binary Sequence from a List of Variables
        /// </summary>
        /// <param name="Bindings">The Variable to create a Binary Sequence from</param>
        /// <returns>A Binary Sequence</returns>
        public static List <byte> EncodeSequence(List <Variable> Bindings)
        {
            List <byte> _Sequence = new List <byte>();

            Bindings.ForEach(vb => _Sequence.AddRange(vb.ToSequence()));
            List <byte> Sequence = new List <byte>();

            Sequence.AddRange(BasicEncodingRules.EncodeSequence(SnmpType.Sequence, ref _Sequence, true));
            _Sequence = null;
            return(Sequence);
        }
Example #7
0
        /// <summary>
        /// Creates a Byte Sequence From this Variable
        /// </summary>
        /// <param name="SequenceType">The Type of the Sequence</param>
        /// <returns>The bytes which represent this variable as a Sequence</returns>
        public List <byte> ToSequence(SnmpType?SequenceType)
        {
            SequenceType = SequenceType ?? SnmpType.Sequence;
            List <byte> _Sequence = new List <byte>();

            _Sequence.AddRange(BasicEncodingRules.EncodeObjectId(Identifier));
            _Sequence.AddRange(this.ToBytes());
            List <byte> Sequence = BasicEncodingRules.EncodeSequence(SequenceType.Value, ref _Sequence, true);

            _Sequence = null;
            return(Sequence);
        }
Example #8
0
        /// <summary>
        /// Creates a Varaible from a List of bytes
        /// </summary>
        /// <param name="bytes">The List of bytes</param>
        /// <returns>A Variable constructs from the given bytes</returns>
        public static Variable FromBytes(List <byte> bytes)
        {
            int      position = 0;
            Variable result   = new Variable();

            if (bytes[position].ToSnmpType() == SnmpType.ObjectIdentifier)
            {
                result.Identifier = BasicEncodingRules.DecodeObjectId(ref bytes, ref position);
            }
            result.TypeCode = bytes[position].ToSnmpType();
            int Length = BasicEncodingRules.DecodeLength(ref bytes, ref position, result.TypeCode);

            result.Value = bytes.GetRange(position, Length);
            return(result);
        }
Example #9
0
 /// <summary>
 /// Converts a ASN.1 Encoded DateTime to a System DateTime
 /// </summary>
 /// <returns></returns>
 public DateTime ToDateTime()
 {
     try
     {
         List <byte> raw      = ToBytes();
         int         position = 0;
         uint        value    = BasicEncodingRules.DecodeUInteger32(ref raw, ref position, TypeCode);
         //use 1, 1, 0 until when we figure out why im 2 hrs off.
         return((new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).AddSeconds(value));
     }
     catch
     {
         //Maybe return Not a Date Time somehow?
         throw;
     }
 }
Example #10
0
        /// <summary>
        /// Constructs the byte representation of the ProtocolDataUnit
        /// </summary>
        /// <returns>A Byte List which represents the ProtocolDataUnit</returns>
        public List <byte> ToPdu()
        {
            List <byte> Pdu          = new List <byte>();
            List <byte> _RequestId   = BasicEncodingRules.EncodeInteger32(this.RequestId);
            List <byte> _ErrorStatus = BasicEncodingRules.EncodeInteger32((int)this.ErrorStatus);
            List <byte> _ErrorIndex  = BasicEncodingRules.EncodeInteger32(this.ErrorIndex);
            List <byte> _Bindings    = BasicEncodingRules.EncodeSequence(this.Bindings);

            Pdu.AddRange(_RequestId);
            Pdu.AddRange(_ErrorStatus);
            Pdu.AddRange(_ErrorIndex);
            Pdu.AddRange(_Bindings);
            Pdu.InsertRange(0, BasicEncodingRules.EncodeSequenceHeader(PduType, Pdu.Count, true));
            Pdu.TrimExcess();
            return(Pdu);
        }
Example #11
0
        /// <summary>
        /// Constructs the byte representation of the SnmpPacket
        /// </summary>
        /// <returns>A Byte List which represents this ProtocolDataUnit as a SnmpPacket</returns>
        public List <byte> ToPacket(PmppEndPoint?EndPoint)
        {
            try
            {
                List <byte> Bytes = new List <byte>();

                Bytes.AddRange(BasicEncodingRules.EncodeInteger32(this.version));
                Bytes.AddRange(BasicEncodingRules.EncodeOctetString(this.CommunityName));
                Bytes.AddRange(ToPdu());
                Bytes.InsertRange(0, BasicEncodingRules.EncodeSequenceHeader(SnmpType.Sequence, Bytes.Count, true));

                Bytes.TrimExcess();
                if (EndPoint.HasValue)
                {
                    Bytes = Bytes.PmppEncode(EndPoint.Value.Address, EndPoint.Value.Control, EndPoint.Value.ProtocolIdentifier);
                }

                return(Bytes);
            }
            catch
            {
                throw;
            }
        }
Example #12
0
 /// <summary>
 /// Creates a Null Variable
 /// </summary>
 /// <returns>A Null Variable</returns>
 public static Variable CreateNull()
 {
     return(Variable.FromBytes(BasicEncodingRules.EncodeNull(true)));
 }
Example #13
0
 /// <summary>
 /// Creates a OctetString Variable
 /// </summary>
 /// <param name="value">The System.string Value of the Variable</param>
 /// <returns>A OctetString Variable with the given value</returns>
 public static Variable CreateOctetString(string value)
 {
     return(Variable.FromBytes(BasicEncodingRules.EncodeOctetString(value)));
 }
Example #14
0
 /// <summary>
 /// Creates a UInteger 32 Variable
 /// </summary>
 /// <param name="value">The System.UInteger32 Value of the Variable</param>
 /// <returns>A UInteger 32 Variable with the given value</returns>
 public static Variable CreateUInteger32(uint value)
 {
     return(Variable.FromBytes(BasicEncodingRules.EncodeUInteger32(value)));
 }