예제 #1
0
파일: Program.cs 프로젝트: ypawlak/SnmpLabs
        static void Main(string[] args)
        {
            //Parse mib file into tree
            MibData mib = MibParser.Parse("RFC1213-MIB");

            mib.MibTreeRoot.PrintToConsole(string.Empty, true);

            //Select oid
            TreeNode found = null;

            while (found == null || found.ObjectType == null)
            {
                Console.WriteLine(">Enter OID");
                string       mibToFind = Console.ReadLine();
                string[]     indexPath = mibToFind.Split('.');
                Queue <long> pathQ     = new Queue <long>(indexPath.Select(x => long.Parse(x)));
                try
                {
                    found = mib.MibTreeRoot.FindByIndex(pathQ);
                }
                catch (Exception) { }

                if (found != null)
                {
                    Console.WriteLine(found.RestrictionsDescription);
                }
                else
                {
                    Console.WriteLine("Specified OID not found");
                }
            }

            while (true)
            {
                //Read and validate value
                string value;
                do
                {
                    Console.WriteLine(">Enter value for selected OBJECT-TYPE:");
                    value = Console.ReadLine();
                }while (!found.Validate(value));

                byte[] encodedBytes = BerEncoding.Encoder.Encode(found.ObjectType.DataType, value).ToArray();
                Console.WriteLine(ByteArrayToBinaryString(encodedBytes));
                Console.WriteLine(ByteArrayToHexString(encodedBytes));
                BerEncoding.DecodedObjectMeta decoded = BerEncoding.Decoder.DecodeObject(encodedBytes);
                Console.WriteLine("> Enter hex string to decode");
                string hexBytes = Console.ReadLine();
                byte[] toDecode = HexStringToByteArray(hexBytes);
                decoded = BerEncoding.Decoder.DecodeObject(toDecode);
            }
        }
예제 #2
0
        public static MibData Merge(MibData current, MibData imported)
        {
            foreach (var importedNode in imported.FlatMibTree.Where(n => n.Value.Parent != null))
            {
                //TODO: optimize for nested nodes from import
                TreeBuilder.InsertIntoTree(importedNode.Value, importedNode.Value.Parent.Name,
                                           current.FlatMibTree);
            }

            foreach (var typeKV in imported.DataTypes.Where(type => !current.DataTypes.ContainsKey(type.Key)))
            {
                current.DataTypes.Add(typeKV.Key, typeKV.Value);
            }

            return(current);
        }
예제 #3
0
        static void Main(string[] args)
        {
            MibData mib = MibParser.Parse("RFC1213-MIB");

            mib.MibTreeRoot.PrintToConsole("", true);

            //System.Diagnostics.Debug.WriteLine(
            //    string.Join("|", SmiEnums.GetEnumCustomizedTextList<SmiEnums.ObjectTypeAccess>()));
            //System.Diagnostics.Debug.WriteLine(
            //    string.Join("|", SmiEnums.GetEnumCustomizedTextList<SmiEnums.ObjectTypeStatus>()));

            //string DataTypeNameGrp = "dtName";
            //string DataTypeScopeGrp = "dtScope";
            //string DataTypeIdGrp = "dtId";
            //string DataTypeClassGrp = "dtClass";
            //string DataTypeBaseTypeGrp = "dtBaseType";
            //string DataTypeSizeGrp = "dtSize";
            //string DataTypeSizeMinGrp = "dtSizeMin";
            //string DataTypeSizeMaxGrp = "dtSizeMax";
            //System.Diagnostics.Debug.WriteLine(
            //    string.Format(@"(?<{0}>{1})\s*::=\s*\[(?<{2}>\w+)\s*(?<{3}>[0-9]+)\].*?(?<{4}>{5})\s*(?<{6}>{7})\s*(\(.*?(?<{8}>[0-9]+)\)\)|\((?<{9}>[0-9]+)..(?<{10}>[0-9]+)\)|\s*?)",
            //        DataTypeNameGrp,
            //        @"\w+",
            //        DataTypeScopeGrp,
            //        DataTypeIdGrp,
            //        DataTypeClassGrp,
            //        string.Join("|", Enum.GetNames(typeof(CustomDataType.ClassEnum))),
            //        DataTypeBaseTypeGrp,
            //        string.Join("|", SmiEnums.GetEnumCustomizedTextList<SmiEnums.DataTypeBase>()),
            //        DataTypeSizeGrp,
            //        DataTypeSizeMinGrp,
            //        DataTypeSizeMaxGrp));
            //Console.WriteLine(SmiEnums.Parse<SmiEnums.ObjectTypeAccess>("read-only"));

            //Console.WriteLine(SmiEnums.ObjectTypeStatus.mandatory.ToString());
            //Console.WriteLine(SmiEnums.Parse<SmiEnums.ObjectTypeStatus>("mandatory"));

            //MibParser.Parse(@"MibSources//", "RFC1155-SMI");
            //Console.WriteLine(MibType.Parse("CONTEXT-SPECIFIC"));
            //Console.WriteLine(MibType.GetString(MibType.Parse("CONTEXT-SPECIFIC")));
            //Console.WriteLine(MibType.Parse("APPLICATION"));
            //Console.WriteLine(MibType.GetString(MibType.Parse("APPLICATION")));
            //string DataTypeNameGrp = "dtName";
            //string DataTypeScopeGrp = "dtScope";
            //string DataTypeIdGrp = "dtId";
            //string DataTypeClassGrp = "dtClass";
            //string DataTypeBaseTypeGrp = "dtBaseType";
            //string DataTypeSize = "dtSize";
            //string DataTypeSizeMin = "dtSizeMin";
            //string DataTypeSizeMax = "dtSizeMax";
            //System.Diagnostics.Debug.WriteLine(string.Format(@"(?<{0}>\w+)\s*::=\s*\[(?<{1}>\w+)\s*(?<{2}>[0-9]+)\].*?(?<{3}>{4})\s*(?<{5}>{6})\s*(\(.*?(?<{7}>[0-9]+)\)\)|\((?<{8}>[0-9]+)..(?<{9}>[0-9]+)\)|\s*?)",
            //        DataTypeNameGrp,
            //        DataTypeScopeGrp,
            //        DataTypeIdGrp,
            //        DataTypeClassGrp,
            //        string.Join("|", Enum.GetNames(typeof(SmiType.ClassEnum))),
            //            DataTypeBaseTypeGrp,
            //            string.Join("|", SmiEnums.GetEnumCustomizedTextList<SmiEnums.BaseType>()),
            //            DataTypeSize,
            //            DataTypeSizeMin,
            //            DataTypeSizeMax));
            //tests
            //var btList = SmiEnums.GetAllEnumStrings<SmiEnums.BaseTypes>();
            //var scpList = SmiEnums.GetAllEnumStrings<SmiEnums.Scope>();
            //var test = MibParser.SmiDataTypeRegex.GetGroupNames();

            //string fTxt = FileUtils.GetAllFileAsText(@"MibSources//", "RFC1155-SMI");
            //var matches = MibParser.SmiDataTypeRegex.Matches(fTxt);
        }
예제 #4
0
        public static MibData Parse(string fileName)
        {
            MibData result = InitMibData();

            string mibFileTxt = SmiParser.Utils.FileUtils.ReadFileFromOutputDirectory(MibFilesFolder, fileName);
            IEnumerable <ImportInfo> importsByFiles = ImportsParser.ParseImportInfo(mibFileTxt);

            foreach (ImportInfo singleFileImports in importsByFiles)
            {
                try
                {
                    MibData imported = Parse(singleFileImports.Filename);
                    result = Merge(result, imported);
                }
                catch (FileNotFoundException e)
                {
                    Debug.WriteLine(string.Format("File {0} has not been found", e.Message));
                }
            }

            IEnumerable <CustomDataType> dataTypes = DataTypesParser.ParseAllDataTypes(mibFileTxt);

            foreach (CustomDataType type in dataTypes)
            {
                result.DataTypes.Add(type.Name, type);
            }

            IEnumerable <AliasInfo>     parsedAliases = AliasesParser.Parse(mibFileTxt);
            IEnumerable <DataTypeAlias> aliases       = parsedAliases
                                                        .Select(
                ai =>
                GetAliasTypeFromInfo(result.DataTypes, ai))
                                                        .Where(alias => alias != null)
                                                        .ToList();

            foreach (DataTypeAlias alias in aliases)
            {
                result.DataTypes.Add(alias.Alias, alias);
            }

            IEnumerable <OidInfo> oids = OidsParser.ParseAllOids(mibFileTxt);

            foreach (OidInfo oi in oids)
            {
                var oidNode = new TreeNode()
                {
                    Children     = new List <TreeNode>(),
                    Name         = oi.Name,
                    SiblingIndex = oi.SiblingNo
                };
                TreeBuilder.InsertIntoTree(oidNode, oi.ParentExpression, result.FlatMibTree);
            }

            IDictionary <OidInfo, ObjectTypeInfo> objectTypes = ObjectTypesParser.ParseAllObjectTypes(mibFileTxt);;

            foreach (var objTypeInfoKV in objectTypes)
            {
                ObjectType objType = GetObjectTypeFromInfo(result.DataTypes, objTypeInfoKV.Value);
                if (objType == null)
                {
                    continue;
                }

                var otNode = new TreeNode()
                {
                    Children     = new List <TreeNode>(),
                    Name         = objTypeInfoKV.Key.Name,
                    SiblingIndex = objTypeInfoKV.Key.SiblingNo,
                    ObjectType   = objType
                };
                TreeBuilder.InsertIntoTree(otNode, objTypeInfoKV.Key.ParentExpression, result.FlatMibTree);
            }

            return(result);
        }