File: FormMain.cs

package info (click to toggle)
lwip 2.2.1%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 10,008 kB
  • sloc: ansic: 109,524; cs: 6,714; sh: 115; makefile: 112; perl: 81
file content (217 lines) | stat: -rw-r--r-- 7,382 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 *
 * This file is part of the lwIP TCP/IP stack.
 *
 * Author: Martin Hentschel <info@cl-soft.de>
 *
 */

using System.Windows.Forms;
using Lextm.SharpSnmpLib.Mib;
using Lextm.SharpSnmpLib.Mib.Elements;
using Lextm.SharpSnmpLib.Mib.Elements.Types;
using Lextm.SharpSnmpLib.Mib.Elements.Entities;
using System.IO;

namespace LwipMibViewer
{
	public partial class FormMain : Form
	{
		readonly ListViewGroup listviewgroupAbstract;
		readonly ListViewGroup listviewgroupElement;
		readonly ListViewGroup listviewgroupBaseType;
		readonly ListViewGroup listviewgroupTypeChain;

		public FormMain()
		{
			this.Font = SystemInformation.MenuFont;
			InitializeComponent();

			this.listviewgroupAbstract = new ListViewGroup("Abstract", System.Windows.Forms.HorizontalAlignment.Left);
			this.listviewgroupElement = new ListViewGroup("Element Properties", System.Windows.Forms.HorizontalAlignment.Left);
			this.listviewgroupBaseType = new ListViewGroup("Element Base Type", System.Windows.Forms.HorizontalAlignment.Left);
			this.listviewgroupTypeChain = new ListViewGroup("Element Type Chain", System.Windows.Forms.HorizontalAlignment.Left);
			this.listviewNodeDetails.Groups.AddRange(new System.Windows.Forms.ListViewGroup[] {
					 listviewgroupAbstract,
					 listviewgroupElement,
					 listviewgroupBaseType,
					 listviewgroupTypeChain});

			try
			{
				DirectoryInfo dirInfo = new DirectoryInfo(Path.GetDirectoryName(Application.ExecutablePath));
				if (dirInfo != null)
				{
					dirInfo = dirInfo.Parent;
					if (dirInfo != null)
					{
						dirInfo = dirInfo.Parent;
						if (dirInfo != null)
						{
							dirInfo = new DirectoryInfo(Path.Combine(dirInfo.FullName, "Mibs"));
							if (dirInfo.Exists)
							{
								MibTypesResolver.RegisterResolver(new FileSystemMibResolver(dirInfo.FullName, true));
							}
						}
					}
				}
			}
			catch
			{ }
		}

		#region GUI Event Handler

		private void toolbuttonOpenMib_Click(object sender, System.EventArgs e)
		{
			if (this.dialogOpenMib.ShowDialog() == DialogResult.OK)
			{
				OpenMib(this.dialogOpenMib.FileName);
			}
		}

		private void treeMib_AfterSelect(object sender, TreeViewEventArgs e)
		{
			listviewNodeDetails.Items.Clear();

			if (e.Node != null)
			{
				MibTreeNode mtn = e.Node.Tag as MibTreeNode;
				if (mtn != null)
				{
					listviewNodeDetails.Items.Add(new ListViewItem(new string[] { "Abstract", mtn.NodeType.ToString() }, this.listviewgroupAbstract));

					listviewNodeDetails.Items.Add(new ListViewItem(new string[] { "Module", (mtn.Entity.Module != null) ? mtn.Entity.Module.Name : "" }, this.listviewgroupElement));
					listviewNodeDetails.Items.Add(new ListViewItem(new string[] { "Type", mtn.Entity.GetType().Name }, this.listviewgroupElement));
					listviewNodeDetails.Items.Add(new ListViewItem(new string[] { "Name", mtn.Entity.Name }, this.listviewgroupElement));
					listviewNodeDetails.Items.Add(new ListViewItem(new string[] { "Description", mtn.Entity.Description }, this.listviewgroupElement));
					listviewNodeDetails.Items.Add(new ListViewItem(new string[] { "OID", mtn.Entity.Value.ToString() }, this.listviewgroupElement));
					listviewNodeDetails.Items.Add(new ListViewItem(new string[] { "Full OID", MibTypesResolver.ResolveOid(mtn.Entity).GetOidString() }, this.listviewgroupElement));
					if (mtn.Entity is ObjectType)
					{
						listviewNodeDetails.Items.Add(new ListViewItem(new string[] { "Access", (mtn.Entity as ObjectType).Access.ToString() }, this.listviewgroupElement));
					}

					ITypeReferrer tr = mtn.Entity as ITypeReferrer;
					if (tr != null)
					{
						ShowTypeDetails(listviewNodeDetails, this.listviewgroupBaseType, tr.BaseType);
						ShowTypeChain(listviewNodeDetails, tr.ReferredType);
					}
				}
			}
		}

		#endregion

		#region Methods

		private void OpenMib(string file)
		{
			try
			{
				MibDocument md = new MibDocument(file);
				MibTypesResolver.ResolveTypes(md.Modules[0]);

				this.treeMib.Nodes.Clear();
				this.listviewNodeDetails.Items.Clear();

				MibTree mt = new MibTree(md.Modules[0] as MibModule);
				foreach (MibTreeNode mibTreeNode in mt.Root)
				{
					AddNode(mibTreeNode, this.treeMib.Nodes);

					foreach (TreeNode node in this.treeMib.Nodes)
					{
						node.Expand();
					}
				}
			}
			catch
			{
			}
		}

		private void AddNode(MibTreeNode mibNode, TreeNodeCollection parentNodes)
		{
			int imgIndex = 5; //unknown
			if ((mibNode.NodeType & MibTreeNodeType.Table) != 0)
			{
				imgIndex = 1;
			}
			else if ((mibNode.NodeType & MibTreeNodeType.TableRow) != 0)
			{
				imgIndex = 2;
			}
			else if ((mibNode.NodeType & MibTreeNodeType.TableCell) != 0)
			{
				imgIndex = 3;
			}
			else if ((mibNode.NodeType & MibTreeNodeType.Scalar) != 0)
			{
				imgIndex = 4;
			}
			else if ((mibNode.NodeType & MibTreeNodeType.Container) != 0)
			{
				imgIndex = 0;
			}

			TreeNode newNode = new TreeNode(mibNode.Entity.Name, imgIndex, imgIndex);
			newNode.Tag = mibNode;

			parentNodes.Add(newNode);

			foreach (MibTreeNode child in mibNode.ChildNodes)
			{
				AddNode(child, newNode.Nodes);
			}
		}

		private void ShowTypeChain(ListView lv, ITypeAssignment type)
		{
			ShowTypeDetails(lv, this.listviewgroupTypeChain, type);

			ITypeReferrer tr = type as ITypeReferrer;
			if ((tr != null) && (tr.ReferredType != null))
			{
				lv.Items.Add(new ListViewItem(new string[] { " >>>", "" }, this.listviewgroupTypeChain));
				ShowTypeChain(listviewNodeDetails, tr.ReferredType);
			}
		}

		private void ShowTypeDetails(ListView lv, ListViewGroup lvg, ITypeAssignment type)
		{
			lv.Items.Add(new ListViewItem(new string[] { "Module", (type.Module != null) ? type.Module.Name : "" }, lvg));
			lv.Items.Add(new ListViewItem(new string[] { "Type", type.GetType().Name }, lvg));
			lv.Items.Add(new ListViewItem(new string[] { "Name", type.Name }, lvg));
		}

		#endregion

	}
}