예제 #1
0
        //private Limiter = new Limiter();

        public MibNode(int nodeId, string nodeName, MibNode parent)
        {
            NodeId   = nodeId;
            NodeName = nodeName;
            Children = new List <MibNode>();
            Parent   = parent;
        }
예제 #2
0
 public ObjectType(int nodeId, string nodeName, MibNode parent, string typeOfNode, AccessTypes access,
                   string status, string description, Limiter limiter = null) : base(nodeId, nodeName, parent)
 {
     TypeOfNode   = typeOfNode;
     Access       = access;
     Status       = status;
     Description  = description;
     this.limiter = limiter;
 }
예제 #3
0
        public static IEnumerable <MibNode> GetMibNodeStack(this MibNode root)
        {
            var nodes = new Stack <MibNode>(new[] { root });

            while (nodes.Any())
            {
                var node = nodes.Pop();
                yield return(node);

                foreach (var n in node.Children)
                {
                    nodes.Push(n);
                }
            }
        }
예제 #4
0
        private static void BuildString(StringBuilder builder, MibNode child)
        {
            var temp   = child;
            var idList = new List <int>();

            while (temp.Parent != null)
            {
                temp = temp.Parent;
                idList.Add(temp.NodeId);
            }
            for (var j = idList.Count - 1; j >= 0; j--)
            {
                builder.Append(idList[j].ToString());

                builder.Append('.');
            }
            builder.Append(child.NodeId);
            builder.Append(' ');
            builder.AppendLine(child.NodeName);
        }
예제 #5
0
        public static string GetLastChildrenString(this MibNode root, string nodeName = null)
        {
            var builder = new StringBuilder();


            foreach (var child in root.Children)
            {
                if (string.IsNullOrEmpty(nodeName) && child.Children.Count == 0)
                {
                    BuildString(builder, child);
                }

                if (child.NodeName == nodeName)
                {
                    BuildString(builder, child);
                }
                builder.Append(child.GetLastChildrenString(nodeName));
            }

            return(builder.ToString());
        }
예제 #6
0
        public static string GetTreeString(this MibNode root, string indent, bool last)
        {
            var builder = new StringBuilder();

            builder.Append(indent);
            if (last)
            {
                builder.Append("\\-");
                indent += "  ";
            }
            else
            {
                builder.Append("|-");
                indent += "| ";
            }
            builder.Append(root.NodeId).Append(". ").AppendLine(root.NodeName);

            for (var i = 0; i < root.Children.Count; i++)
            {
                builder.Append(root.Children[i].GetTreeString(indent, i == root.Children.Count - 1));
            }

            return(builder.ToString());
        }
예제 #7
0
        public MibNode GenerateTree()
        {
            //Hardcoded roots of the tree
            var masterNode = new MibNode(1, "ISO", null);

            masterNode.AddChild(new MibNode(3, "org", masterNode));

            var org = masterNode.GetMibNodeStack().FirstOrDefault(node => node.NodeName == "org");

            org?.AddChild(new MibNode(6, "dod", org));

            var dod = masterNode.GetMibNodeStack().FirstOrDefault(node => node.NodeName == "dod");

            dod?.AddChild(new MibNode(1, "internet", dod));

            var internet = masterNode.GetMibNodeStack().FirstOrDefault(node => node.NodeName == "internet");

            internet?.AddChild(new MibNode(2, "mgmt", internet));


            var mibText = fileReader.GetFileEntireText(ParserConst.MIBPath);

            //Load imports
            var imports = importsLoader.ParseImports(mibText); //TODO load this files

            var objectIdentifierMatch = objectIdentifierRegex.Matches(mibText);

            foreach (Match match in objectIdentifierMatch)
            {
                var groups = match.Groups;

                var name        = groups["name"].ToString().Trim(' ');
                var parent      = groups["parent"].ToString().Split(' ')[1].Trim(' ');
                var valueString = groups["parent"].ToString().Split(' ')[2].Trim(' ');
                var value       = int.Parse(valueString);

                var parentNode = masterNode.GetMibNodeStack().FirstOrDefault(node => node.NodeName == parent);
                parentNode?.AddChild(new MibNode(value, name, parentNode));
            }

            var sequenceMatch = sequenceRegex.Matches(mibText);

            foreach (Match match in sequenceMatch)
            {
                var sequenceValues = getSequenceValuesRegex.Matches(match.Groups["values"].Value);
                foreach (Match sequenceValue in sequenceValues)
                {
                    var name  = sequenceValue.Groups["name"].Value;
                    var value = sequenceValue.Groups["value"].Value;

                    //TODO
                }
            }

            var objectTypeMatch = objectTypeRegex.Matches(mibText);

            foreach (Match match in objectTypeMatch)
            {
                var objectTypeText = match.Value;
                var name           = nameOfNode.Match(objectTypeText).Groups["name"].Value.Trim(' ');
                var typeOfNode     = this.typeOfNode.Match(objectTypeText).Groups["syntax"];
                var access         = typeOfAccess.Match(objectTypeText).Groups["access"];
                var status         = this.status.Match(objectTypeText).Groups["status"];
                var description    = this.description.Match(objectTypeText).Groups["description"];
                var parentName     = parentAndId.Match(objectTypeText).Groups["parent"];
                var id             = parentAndId.Match(objectTypeText).Groups["parentId"];

                if (IsObjectComplete(name, typeOfNode, access, status, parentName, id))
                {
                    var idParsed = int.Parse(id.ToString());

                    var parentNode = masterNode.GetMibNodeStack()
                                     .FirstOrDefault(node => node.NodeName == parentName.ToString());

                    AccessTypes accessType;
                    if (access.Value.Contains("read-write"))
                    {
                        accessType = AccessTypes.ReadWrite;
                    }
                    else if (access.Value.Contains("read-only"))
                    {
                        accessType = AccessTypes.ReadOnly;
                    }
                    else
                    {
                        accessType = AccessTypes.NoAccess;
                    }

                    if (typeOfNode.ToString().Contains("{"))
                    {
                        var valueOfType = complexTypeOfNode.Match(match.Value).Value;
                        var values      = getNumbers.Matches(valueOfType);
                        var min         = values[0].Value;
                        var max         = values[values.Count - 1].Value;
                        min = min.Substring(0, min.Length - 1);
                        max = max.Substring(0, max.Length - 1);

                        var limiter = new Limiter(int.Parse(min), int.Parse(max));

                        parentNode?.AddChild(new ObjectType(idParsed, name, parentNode, typeOfNode.Value, accessType,
                                                            status.Value, description.Value, limiter));
                    }
                    else if (typeOfNode.ToString().Contains(".."))
                    {
                        var numbers = splitDotNumbers.Match(typeOfNode.Value);
                        var min     = numbers.Groups["min"].Value;
                        var max     = numbers.Groups["max"].Value;
                        var limiter = new Limiter(int.Parse(min), int.Parse(max));

                        parentNode?.AddChild(new ObjectType(idParsed, name, parentNode, typeOfNode.Value, accessType,
                                                            status.Value, description.Value, limiter));
                    }
                    else
                    {
                        parentNode?.AddChild(new ObjectType(idParsed, name, parentNode, typeOfNode.Value, accessType,
                                                            status.Value, description.Value));
                    }
                }
            }
            return(masterNode);
        }
예제 #8
0
 public void AddChild(MibNode child)
 {
     Children.Add(child);
 }
예제 #9
0
 public SNMPProcessor(IBerDecoder decoder, IBerCoder coder, MibNode master)
 {
     this.decoder = decoder;
     this.coder   = coder;
     this.master  = master;
 }