Immutable structure for Asn1TagInfo
示例#1
0
        /// <summary>
        /// Reads the variable binds.
        /// </summary>
        /// <param name="bytes">The bytes.</param>
        /// <param name="offset">The offset.</param>
        /// <param name="bytesLength">Length of the bytes.</param>
        /// <returns>
        /// VarBinds
        /// </returns>
        /// <exception cref="System.DataMisalignedException">Bad Data/Mulformated Asn1/Snmp data</exception>
        /// <exception cref="InvalidOperationException">Malformed datagram/Out of sequemce data</exception>
        public static VarBind[] ReadVarBinds(this byte[] bytes, int offset, int bytesLength)
        {
            VarBind[] values       = new VarBind[100];
            int       varbindCount = 0;

            bytesLength += offset;

            while (offset < bytesLength)
            {
                Asn1TagInfo cct    = bytes[offset++].DecodeToClassConstructType();
                int         length = 0;
                offset = ReadLength(bytes, offset, out length);

                if (ConstructType.Primitive != cct.Asn1ConstructType)
                {
                    continue;
                }

                if (cct.Asn1TagType == Asn1Tag.NotAsn1Data || cct.Asn1TagType != Asn1Tag.ObjectIdentifier)
                {
                    throw new DataMisalignedException("Bad Data/Mulformated Asn1/Snmp data");
                }

                uint[] key = ReadOids(bytes, offset, length);
                offset += length;

                // Get [TLV] type, length, value
                cct    = bytes[offset++].DecodeToClassConstructType();
                offset = ReadLength(bytes, offset, out length);
                object value;
                offset = GetVarBindValue(bytes, offset, length, cct, out value);

                //Make varbind
                MakeVarBinds(ref values, key, value, cct, varbindCount++);
            }

            if (varbindCount != values.Length)
            {
                Array.Resize(ref values, varbindCount);
            }

            return(values);
        }
        /// <summary>
        /// Makes the variable binds.
        /// </summary>
        /// <param name="varBinds">Varbind value.</param>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        /// <param name="tag">The asn1 tag.</param>
        /// <param name="varbindCount">The varbind count.</param>
        private static void MakeVarBinds(ref VarBind[] varBinds, uint[] key, object value, Asn1TagInfo tag, int varbindCount)
        {
            int length = varBinds.Length;
            if (varbindCount >= length)
            {
                Array.Resize(ref varBinds, length + 100);
            }

            varBinds[varbindCount] = new VarBind(new ObjectIdentifier(key), value, tag);
        }
        /// <summary>
        /// Gets the variable bind value.
        /// </summary>
        /// <param name="bytes">The bytes.</param>
        /// <param name="offset">The offset.</param>
        /// <param name="length">The length.</param>
        /// <param name="tagInfo">The tag information.</param>
        /// <param name="value">The value.</param>
        /// <returns>offset int</returns>
        private static int GetVarBindValue(byte[] bytes, int offset, int length, Asn1TagInfo tagInfo, out object value)
        {
            if (tagInfo.Asn1TagType == Asn1Tag.NotAsn1Data)
            {
                switch (tagInfo.Asn1SnmpTagType)
                {
                    case Asn1SnmpTag.IpAddress:
                        {
                            value = ReadIPAddress(bytes, offset);
                            break;
                        }

                    case Asn1SnmpTag.Counter:
                        {
                            value = ReadUnsignedInteger(bytes, offset, length);
                            break;
                        }

                    case Asn1SnmpTag.Counter64:
                        {
                            value = ReadUnsignedLong(bytes, offset, length);
                            break;
                        }

                    case Asn1SnmpTag.Gauge:
                        {
                            value = ReadUnsignedInteger(bytes, offset, length);
                            break;
                        }

                    case Asn1SnmpTag.UInt32:
                        {
                            value = ReadUnsignedInteger(bytes, offset, length);
                            break;
                        }

                    case Asn1SnmpTag.TimeTicks:
                        {
                            value = ReadUnsignedInteger(bytes, offset, length);
                            break;
                        }

                    default:
                        {
                            value = "NYI: " + tagInfo.Asn1SnmpTagType.ToString();
                            break;
                        }
                }
            }
            else
            {
                switch (tagInfo.Asn1TagType)
                {
                    case Asn1Tag.Null:
                        {
                            value = null;
                            break;
                        }

                    case Asn1Tag.Integer:
                        {
                            value = ReadLongInteger(bytes, offset, length);
                            break;
                        }

                    case Asn1Tag.OctetString:
                        {
                            value = ReadOctetString(bytes, offset, length);
                            break;
                        }

                    case Asn1Tag.ObjectIdentifier:
                        {
                            value = new ObjectIdentifier(bytes.ReadOids(offset, length));
                            break;
                        }

                    default:
                        {
                            value = "NYI: " + tagInfo.Asn1TagType.ToString();
                            break;
                        }
                }
            }

            return offset + length;
        }
示例#4
0
        /// <summary>
        /// Makes the variable binds.
        /// </summary>
        /// <param name="varBinds">Varbind value.</param>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        /// <param name="tag">The asn1 tag.</param>
        /// <param name="varbindCount">The varbind count.</param>
        private static void MakeVarBinds(ref VarBind[] varBinds, uint[] key, object value, Asn1TagInfo tag, int varbindCount)
        {
            int length = varBinds.Length;

            if (varbindCount >= length)
            {
                Array.Resize(ref varBinds, length + 100);
            }

            varBinds[varbindCount] = new VarBind(new ObjectIdentifier(key), value, tag);
        }
示例#5
0
        /// <summary>
        /// Gets the variable bind value.
        /// </summary>
        /// <param name="bytes">The bytes.</param>
        /// <param name="offset">The offset.</param>
        /// <param name="length">The length.</param>
        /// <param name="tagInfo">The tag information.</param>
        /// <param name="value">The value.</param>
        /// <returns>offset int</returns>
        private static int GetVarBindValue(byte[] bytes, int offset, int length, Asn1TagInfo tagInfo, out object value)
        {
            if (tagInfo.Asn1TagType == Asn1Tag.NotAsn1Data)
            {
                switch (tagInfo.Asn1SnmpTagType)
                {
                case Asn1SnmpTag.IpAddress:
                {
                    value = ReadIPAddress(bytes, offset);
                    break;
                }

                case Asn1SnmpTag.Counter:
                {
                    value = ReadUnsignedInteger(bytes, offset, length);
                    break;
                }

                case Asn1SnmpTag.Counter64:
                {
                    value = ReadUnsignedLong(bytes, offset, length);
                    break;
                }

                case Asn1SnmpTag.Gauge:
                {
                    value = ReadUnsignedInteger(bytes, offset, length);
                    break;
                }

                case Asn1SnmpTag.UInt32:
                {
                    value = ReadUnsignedInteger(bytes, offset, length);
                    break;
                }

                case Asn1SnmpTag.TimeTicks:
                {
                    value = ReadUnsignedInteger(bytes, offset, length);
                    break;
                }

                default:
                {
                    value = "NYI: " + tagInfo.Asn1SnmpTagType.ToString();
                    break;
                }
                }
            }
            else
            {
                switch (tagInfo.Asn1TagType)
                {
                case Asn1Tag.Null:
                {
                    value = null;
                    break;
                }

                case Asn1Tag.Integer:
                {
                    value = ReadLongInteger(bytes, offset, length);
                    break;
                }

                case Asn1Tag.OctetString:
                {
                    value = ReadOctetString(bytes, offset, length);
                    break;
                }

                case Asn1Tag.ObjectIdentifier:
                {
                    value = new ObjectIdentifier(bytes.ReadOids(offset, length));
                    break;
                }

                default:
                {
                    value = "NYI: " + tagInfo.Asn1TagType.ToString();
                    break;
                }
                }
            }

            return(offset + length);
        }