FormMain.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. using System.Windows.Forms;
  33. using Lextm.SharpSnmpLib.Mib;
  34. using Lextm.SharpSnmpLib.Mib.Elements;
  35. using Lextm.SharpSnmpLib.Mib.Elements.Types;
  36. using Lextm.SharpSnmpLib.Mib.Elements.Entities;
  37. using System.IO;
  38. namespace LwipMibViewer
  39. {
  40. public partial class FormMain : Form
  41. {
  42. readonly ListViewGroup listviewgroupAbstract;
  43. readonly ListViewGroup listviewgroupElement;
  44. readonly ListViewGroup listviewgroupBaseType;
  45. readonly ListViewGroup listviewgroupTypeChain;
  46. public FormMain()
  47. {
  48. this.Font = SystemInformation.MenuFont;
  49. InitializeComponent();
  50. this.listviewgroupAbstract = new ListViewGroup("Abstract", System.Windows.Forms.HorizontalAlignment.Left);
  51. this.listviewgroupElement = new ListViewGroup("Element Properties", System.Windows.Forms.HorizontalAlignment.Left);
  52. this.listviewgroupBaseType = new ListViewGroup("Element Base Type", System.Windows.Forms.HorizontalAlignment.Left);
  53. this.listviewgroupTypeChain = new ListViewGroup("Element Type Chain", System.Windows.Forms.HorizontalAlignment.Left);
  54. this.listviewNodeDetails.Groups.AddRange(new System.Windows.Forms.ListViewGroup[] {
  55. listviewgroupAbstract,
  56. listviewgroupElement,
  57. listviewgroupBaseType,
  58. listviewgroupTypeChain});
  59. try
  60. {
  61. DirectoryInfo dirInfo = new DirectoryInfo(Path.GetDirectoryName(Application.ExecutablePath));
  62. if (dirInfo != null)
  63. {
  64. dirInfo = dirInfo.Parent;
  65. if (dirInfo != null)
  66. {
  67. dirInfo = dirInfo.Parent;
  68. if (dirInfo != null)
  69. {
  70. dirInfo = new DirectoryInfo(Path.Combine(dirInfo.FullName, "Mibs"));
  71. if (dirInfo.Exists)
  72. {
  73. MibTypesResolver.RegisterResolver(new FileSystemMibResolver(dirInfo.FullName, true));
  74. }
  75. }
  76. }
  77. }
  78. }
  79. catch
  80. { }
  81. }
  82. #region GUI Event Handler
  83. private void toolbuttonOpenMib_Click(object sender, System.EventArgs e)
  84. {
  85. if (this.dialogOpenMib.ShowDialog() == DialogResult.OK)
  86. {
  87. OpenMib(this.dialogOpenMib.FileName);
  88. }
  89. }
  90. private void treeMib_AfterSelect(object sender, TreeViewEventArgs e)
  91. {
  92. listviewNodeDetails.Items.Clear();
  93. if (e.Node != null)
  94. {
  95. MibTreeNode mtn = e.Node.Tag as MibTreeNode;
  96. if (mtn != null)
  97. {
  98. listviewNodeDetails.Items.Add(new ListViewItem(new string[] { "Abstract", mtn.NodeType.ToString() }, this.listviewgroupAbstract));
  99. listviewNodeDetails.Items.Add(new ListViewItem(new string[] { "Module", (mtn.Entity.Module != null) ? mtn.Entity.Module.Name : "" }, this.listviewgroupElement));
  100. listviewNodeDetails.Items.Add(new ListViewItem(new string[] { "Type", mtn.Entity.GetType().Name }, this.listviewgroupElement));
  101. listviewNodeDetails.Items.Add(new ListViewItem(new string[] { "Name", mtn.Entity.Name }, this.listviewgroupElement));
  102. listviewNodeDetails.Items.Add(new ListViewItem(new string[] { "Description", mtn.Entity.Description }, this.listviewgroupElement));
  103. listviewNodeDetails.Items.Add(new ListViewItem(new string[] { "OID", mtn.Entity.Value.ToString() }, this.listviewgroupElement));
  104. listviewNodeDetails.Items.Add(new ListViewItem(new string[] { "Full OID", MibTypesResolver.ResolveOid(mtn.Entity).GetOidString() }, this.listviewgroupElement));
  105. if (mtn.Entity is ObjectType)
  106. {
  107. listviewNodeDetails.Items.Add(new ListViewItem(new string[] { "Access", (mtn.Entity as ObjectType).Access.ToString() }, this.listviewgroupElement));
  108. }
  109. ITypeReferrer tr = mtn.Entity as ITypeReferrer;
  110. if (tr != null)
  111. {
  112. ShowTypeDetails(listviewNodeDetails, this.listviewgroupBaseType, tr.BaseType);
  113. ShowTypeChain(listviewNodeDetails, tr.ReferredType);
  114. }
  115. }
  116. }
  117. }
  118. #endregion
  119. #region Methods
  120. private void OpenMib(string file)
  121. {
  122. try
  123. {
  124. MibDocument md = new MibDocument(file);
  125. MibTypesResolver.ResolveTypes(md.Modules[0]);
  126. this.treeMib.Nodes.Clear();
  127. this.listviewNodeDetails.Items.Clear();
  128. MibTree mt = new MibTree(md.Modules[0] as MibModule);
  129. foreach (MibTreeNode mibTreeNode in mt.Root)
  130. {
  131. AddNode(mibTreeNode, this.treeMib.Nodes);
  132. foreach (TreeNode node in this.treeMib.Nodes)
  133. {
  134. node.Expand();
  135. }
  136. }
  137. }
  138. catch
  139. {
  140. }
  141. }
  142. private void AddNode(MibTreeNode mibNode, TreeNodeCollection parentNodes)
  143. {
  144. int imgIndex = 5; //unknown
  145. if ((mibNode.NodeType & MibTreeNodeType.Table) != 0)
  146. {
  147. imgIndex = 1;
  148. }
  149. else if ((mibNode.NodeType & MibTreeNodeType.TableRow) != 0)
  150. {
  151. imgIndex = 2;
  152. }
  153. else if ((mibNode.NodeType & MibTreeNodeType.TableCell) != 0)
  154. {
  155. imgIndex = 3;
  156. }
  157. else if ((mibNode.NodeType & MibTreeNodeType.Scalar) != 0)
  158. {
  159. imgIndex = 4;
  160. }
  161. else if ((mibNode.NodeType & MibTreeNodeType.Container) != 0)
  162. {
  163. imgIndex = 0;
  164. }
  165. TreeNode newNode = new TreeNode(mibNode.Entity.Name, imgIndex, imgIndex);
  166. newNode.Tag = mibNode;
  167. parentNodes.Add(newNode);
  168. foreach (MibTreeNode child in mibNode.ChildNodes)
  169. {
  170. AddNode(child, newNode.Nodes);
  171. }
  172. }
  173. private void ShowTypeChain(ListView lv, ITypeAssignment type)
  174. {
  175. ShowTypeDetails(lv, this.listviewgroupTypeChain, type);
  176. ITypeReferrer tr = type as ITypeReferrer;
  177. if ((tr != null) && (tr.ReferredType != null))
  178. {
  179. lv.Items.Add(new ListViewItem(new string[] { " >>>", "" }, this.listviewgroupTypeChain));
  180. ShowTypeChain(listviewNodeDetails, tr.ReferredType);
  181. }
  182. }
  183. private void ShowTypeDetails(ListView lv, ListViewGroup lvg, ITypeAssignment type)
  184. {
  185. lv.Items.Add(new ListViewItem(new string[] { "Module", (type.Module != null) ? type.Module.Name : "" }, lvg));
  186. lv.Items.Add(new ListViewItem(new string[] { "Type", type.GetType().Name }, lvg));
  187. lv.Items.Add(new ListViewItem(new string[] { "Name", type.Name }, lvg));
  188. }
  189. #endregion
  190. }
  191. }