• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  *    this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  *    this list of conditions and the following disclaimer in the documentation
12  *    and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
25  * OF SUCH DAMAGE.
26  *
27  * This file is part of the lwIP TCP/IP stack.
28  *
29  * Author: Martin Hentschel <info@cl-soft.de>
30  *
31  */
32 
33 using System.Windows.Forms;
34 using Lextm.SharpSnmpLib.Mib;
35 using Lextm.SharpSnmpLib.Mib.Elements;
36 using Lextm.SharpSnmpLib.Mib.Elements.Types;
37 using Lextm.SharpSnmpLib.Mib.Elements.Entities;
38 using System.IO;
39 
40 namespace LwipMibViewer
41 {
42 	public partial class FormMain : Form
43 	{
44 		readonly ListViewGroup listviewgroupAbstract;
45 		readonly ListViewGroup listviewgroupElement;
46 		readonly ListViewGroup listviewgroupBaseType;
47 		readonly ListViewGroup listviewgroupTypeChain;
48 
FormMain()49 		public FormMain()
50 		{
51 			this.Font = SystemInformation.MenuFont;
52 			InitializeComponent();
53 
54 			this.listviewgroupAbstract = new ListViewGroup("Abstract", System.Windows.Forms.HorizontalAlignment.Left);
55 			this.listviewgroupElement = new ListViewGroup("Element Properties", System.Windows.Forms.HorizontalAlignment.Left);
56 			this.listviewgroupBaseType = new ListViewGroup("Element Base Type", System.Windows.Forms.HorizontalAlignment.Left);
57 			this.listviewgroupTypeChain = new ListViewGroup("Element Type Chain", System.Windows.Forms.HorizontalAlignment.Left);
58 			this.listviewNodeDetails.Groups.AddRange(new System.Windows.Forms.ListViewGroup[] {
59 					 listviewgroupAbstract,
60 					 listviewgroupElement,
61 					 listviewgroupBaseType,
62 					 listviewgroupTypeChain});
63 
64 			try
65 			{
66 				DirectoryInfo dirInfo = new DirectoryInfo(Path.GetDirectoryName(Application.ExecutablePath));
67 				if (dirInfo != null)
68 				{
69 					dirInfo = dirInfo.Parent;
70 					if (dirInfo != null)
71 					{
72 						dirInfo = dirInfo.Parent;
73 						if (dirInfo != null)
74 						{
75 							dirInfo = new DirectoryInfo(Path.Combine(dirInfo.FullName, "Mibs"));
76 							if (dirInfo.Exists)
77 							{
78 								MibTypesResolver.RegisterResolver(new FileSystemMibResolver(dirInfo.FullName, true));
79 							}
80 						}
81 					}
82 				}
83 			}
84 			catch
85 			{ }
86 		}
87 
88 		#region GUI Event Handler
89 
toolbuttonOpenMib_Click(object sender, System.EventArgs e)90 		private void toolbuttonOpenMib_Click(object sender, System.EventArgs e)
91 		{
92 			if (this.dialogOpenMib.ShowDialog() == DialogResult.OK)
93 			{
94 				OpenMib(this.dialogOpenMib.FileName);
95 			}
96 		}
97 
treeMib_AfterSelect(object sender, TreeViewEventArgs e)98 		private void treeMib_AfterSelect(object sender, TreeViewEventArgs e)
99 		{
100 			listviewNodeDetails.Items.Clear();
101 
102 			if (e.Node != null)
103 			{
104 				MibTreeNode mtn = e.Node.Tag as MibTreeNode;
105 				if (mtn != null)
106 				{
107 					listviewNodeDetails.Items.Add(new ListViewItem(new string[] { "Abstract", mtn.NodeType.ToString() }, this.listviewgroupAbstract));
108 
109 					listviewNodeDetails.Items.Add(new ListViewItem(new string[] { "Module", (mtn.Entity.Module != null) ? mtn.Entity.Module.Name : "" }, this.listviewgroupElement));
110 					listviewNodeDetails.Items.Add(new ListViewItem(new string[] { "Type", mtn.Entity.GetType().Name }, this.listviewgroupElement));
111 					listviewNodeDetails.Items.Add(new ListViewItem(new string[] { "Name", mtn.Entity.Name }, this.listviewgroupElement));
112 					listviewNodeDetails.Items.Add(new ListViewItem(new string[] { "Description", mtn.Entity.Description }, this.listviewgroupElement));
113 					listviewNodeDetails.Items.Add(new ListViewItem(new string[] { "OID", mtn.Entity.Value.ToString() }, this.listviewgroupElement));
114 					listviewNodeDetails.Items.Add(new ListViewItem(new string[] { "Full OID", MibTypesResolver.ResolveOid(mtn.Entity).GetOidString() }, this.listviewgroupElement));
115 					if (mtn.Entity is ObjectType)
116 					{
117 						listviewNodeDetails.Items.Add(new ListViewItem(new string[] { "Access", (mtn.Entity as ObjectType).Access.ToString() }, this.listviewgroupElement));
118 					}
119 
120 					ITypeReferrer tr = mtn.Entity as ITypeReferrer;
121 					if (tr != null)
122 					{
123 						ShowTypeDetails(listviewNodeDetails, this.listviewgroupBaseType, tr.BaseType);
124 						ShowTypeChain(listviewNodeDetails, tr.ReferredType);
125 					}
126 				}
127 			}
128 		}
129 
130 		#endregion
131 
132 		#region Methods
133 
OpenMib(string file)134 		private void OpenMib(string file)
135 		{
136 			try
137 			{
138 				MibDocument md = new MibDocument(file);
139 				MibTypesResolver.ResolveTypes(md.Modules[0]);
140 
141 				this.treeMib.Nodes.Clear();
142 				this.listviewNodeDetails.Items.Clear();
143 
144 				MibTree mt = new MibTree(md.Modules[0] as MibModule);
145 				foreach (MibTreeNode mibTreeNode in mt.Root)
146 				{
147 					AddNode(mibTreeNode, this.treeMib.Nodes);
148 
149 					foreach (TreeNode node in this.treeMib.Nodes)
150 					{
151 						node.Expand();
152 					}
153 				}
154 			}
155 			catch
156 			{
157 			}
158 		}
159 
AddNode(MibTreeNode mibNode, TreeNodeCollection parentNodes)160 		private void AddNode(MibTreeNode mibNode, TreeNodeCollection parentNodes)
161 		{
162 			int imgIndex = 5; //unknown
163 			if ((mibNode.NodeType & MibTreeNodeType.Table) != 0)
164 			{
165 				imgIndex = 1;
166 			}
167 			else if ((mibNode.NodeType & MibTreeNodeType.TableRow) != 0)
168 			{
169 				imgIndex = 2;
170 			}
171 			else if ((mibNode.NodeType & MibTreeNodeType.TableCell) != 0)
172 			{
173 				imgIndex = 3;
174 			}
175 			else if ((mibNode.NodeType & MibTreeNodeType.Scalar) != 0)
176 			{
177 				imgIndex = 4;
178 			}
179 			else if ((mibNode.NodeType & MibTreeNodeType.Container) != 0)
180 			{
181 				imgIndex = 0;
182 			}
183 
184 			TreeNode newNode = new TreeNode(mibNode.Entity.Name, imgIndex, imgIndex);
185 			newNode.Tag = mibNode;
186 
187 			parentNodes.Add(newNode);
188 
189 			foreach (MibTreeNode child in mibNode.ChildNodes)
190 			{
191 				AddNode(child, newNode.Nodes);
192 			}
193 		}
194 
ShowTypeChain(ListView lv, ITypeAssignment type)195 		private void ShowTypeChain(ListView lv, ITypeAssignment type)
196 		{
197 			ShowTypeDetails(lv, this.listviewgroupTypeChain, type);
198 
199 			ITypeReferrer tr = type as ITypeReferrer;
200 			if ((tr != null) && (tr.ReferredType != null))
201 			{
202 				lv.Items.Add(new ListViewItem(new string[] { " >>>", "" }, this.listviewgroupTypeChain));
203 				ShowTypeChain(listviewNodeDetails, tr.ReferredType);
204 			}
205 		}
206 
ShowTypeDetails(ListView lv, ListViewGroup lvg, ITypeAssignment type)207 		private void ShowTypeDetails(ListView lv, ListViewGroup lvg, ITypeAssignment type)
208 		{
209 			lv.Items.Add(new ListViewItem(new string[] { "Module", (type.Module != null) ? type.Module.Name : "" }, lvg));
210 			lv.Items.Add(new ListViewItem(new string[] { "Type", type.GetType().Name }, lvg));
211 			lv.Items.Add(new ListViewItem(new string[] { "Name", type.Name }, lvg));
212 		}
213 
214 		#endregion
215 
216 	}
217 }
218