PacketMonitorForm.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. * Mentalis.org Packet Monitor
  3. *
  4. * Copyright ?2003, The KPD-Team
  5. * All rights reserved.
  6. * http://www.mentalis.org/
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * - Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * - Neither the name of the KPD-Team, nor the names of its contributors
  16. * may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  23. * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  24. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  25. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  26. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  28. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  30. * OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. using System;
  33. using System.Drawing;
  34. using System.Collections;
  35. using System.ComponentModel;
  36. using System.Windows.Forms;
  37. using System.Data;
  38. using System.Net;
  39. namespace Org.Mentalis.Network.PacketMonitor
  40. {
  41. public class PacketMonitorForm : System.Windows.Forms.Form
  42. {
  43. private delegate void UpdatePacketList(Packet p);
  44. public PacketMonitorForm()
  45. {
  46. // Required for Windows Form Designer support
  47. InitializeComponent();
  48. // initialize packet monitor
  49. Initialize();
  50. }
  51. protected override void Dispose(bool disposing)
  52. {
  53. if (disposing)
  54. {
  55. if (components != null)
  56. {
  57. components.Dispose();
  58. }
  59. }
  60. base.Dispose(disposing);
  61. }
  62. #region Windows Form Designer generated code
  63. /// <summary>
  64. /// Required method for Designer support - do not modify
  65. /// the contents of this method with the code editor.
  66. /// </summary>
  67. private void InitializeComponent()
  68. {
  69. this.components = new System.ComponentModel.Container();
  70. System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(PacketMonitorForm));
  71. this.ToolBar = new System.Windows.Forms.ToolBar();
  72. this.StartButton = new System.Windows.Forms.ToolBarButton();
  73. this.HostsMenu = new System.Windows.Forms.ContextMenu();
  74. this.StopButton = new System.Windows.Forms.ToolBarButton();
  75. this.ClearButton = new System.Windows.Forms.ToolBarButton();
  76. this.AboutButton = new System.Windows.Forms.ToolBarButton();
  77. this.ToobarImages = new System.Windows.Forms.ImageList(this.components);
  78. this.MainMenu = new System.Windows.Forms.MainMenu(this.components);
  79. this.FileMenu = new System.Windows.Forms.MenuItem();
  80. this.MonitorMenuItem = new System.Windows.Forms.MenuItem();
  81. this.StopMenuItem = new System.Windows.Forms.MenuItem();
  82. this.ClearMenuItem = new System.Windows.Forms.MenuItem();
  83. this.Splitter1MenuItem = new System.Windows.Forms.MenuItem();
  84. this.ExitMenuItem = new System.Windows.Forms.MenuItem();
  85. this.HelpMenu = new System.Windows.Forms.MenuItem();
  86. this.AboutMenuItem = new System.Windows.Forms.MenuItem();
  87. this.StatusBar = new System.Windows.Forms.StatusBar();
  88. this.PacketList = new System.Windows.Forms.ListView();
  89. this.TimeHeader = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
  90. this.ProtocolHeader = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
  91. this.SourceHeader = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
  92. this.DestinationHeader = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
  93. this.LengthHeader = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
  94. this.SuspendLayout();
  95. //
  96. // ToolBar
  97. //
  98. this.ToolBar.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;
  99. this.ToolBar.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
  100. this.StartButton,
  101. this.StopButton,
  102. this.ClearButton,
  103. this.AboutButton});
  104. this.ToolBar.Cursor = System.Windows.Forms.Cursors.Default;
  105. this.ToolBar.DropDownArrows = true;
  106. this.ToolBar.ImageList = this.ToobarImages;
  107. this.ToolBar.Location = new System.Drawing.Point(0, 0);
  108. this.ToolBar.Name = "ToolBar";
  109. this.ToolBar.ShowToolTips = true;
  110. this.ToolBar.Size = new System.Drawing.Size(522, 28);
  111. this.ToolBar.TabIndex = 1;
  112. this.ToolBar.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.OnToolBarClick);
  113. //
  114. // StartButton
  115. //
  116. this.StartButton.DropDownMenu = this.HostsMenu;
  117. this.StartButton.ImageIndex = 0;
  118. this.StartButton.Name = "StartButton";
  119. this.StartButton.Style = System.Windows.Forms.ToolBarButtonStyle.DropDownButton;
  120. this.StartButton.ToolTipText = "Start monitoring";
  121. //
  122. // StopButton
  123. //
  124. this.StopButton.ImageIndex = 1;
  125. this.StopButton.Name = "StopButton";
  126. this.StopButton.ToolTipText = "Stop monitoring";
  127. //
  128. // ClearButton
  129. //
  130. this.ClearButton.ImageIndex = 3;
  131. this.ClearButton.Name = "ClearButton";
  132. this.ClearButton.ToolTipText = "Clear packet list";
  133. //
  134. // AboutButton
  135. //
  136. this.AboutButton.ImageIndex = 2;
  137. this.AboutButton.Name = "AboutButton";
  138. this.AboutButton.ToolTipText = "About...";
  139. //
  140. // ToobarImages
  141. //
  142. this.ToobarImages.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("ToobarImages.ImageStream")));
  143. this.ToobarImages.TransparentColor = System.Drawing.Color.Magenta;
  144. this.ToobarImages.Images.SetKeyName(0, "");
  145. this.ToobarImages.Images.SetKeyName(1, "");
  146. this.ToobarImages.Images.SetKeyName(2, "");
  147. this.ToobarImages.Images.SetKeyName(3, "");
  148. //
  149. // MainMenu
  150. //
  151. this.MainMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  152. this.FileMenu,
  153. this.HelpMenu});
  154. //
  155. // FileMenu
  156. //
  157. this.FileMenu.Index = 0;
  158. this.FileMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  159. this.MonitorMenuItem,
  160. this.StopMenuItem,
  161. this.ClearMenuItem,
  162. this.Splitter1MenuItem,
  163. this.ExitMenuItem});
  164. this.FileMenu.Text = "&File";
  165. //
  166. // MonitorMenuItem
  167. //
  168. this.MonitorMenuItem.Index = 0;
  169. this.MonitorMenuItem.Text = "Monitor";
  170. //
  171. // StopMenuItem
  172. //
  173. this.StopMenuItem.Index = 1;
  174. this.StopMenuItem.Text = "&Stop monitoring";
  175. this.StopMenuItem.Click += new System.EventHandler(this.StopMenuItem_Click);
  176. //
  177. // ClearMenuItem
  178. //
  179. this.ClearMenuItem.Index = 2;
  180. this.ClearMenuItem.Text = "&Clear packet list";
  181. this.ClearMenuItem.Click += new System.EventHandler(this.ClearMenuItem_Click);
  182. //
  183. // Splitter1MenuItem
  184. //
  185. this.Splitter1MenuItem.Index = 3;
  186. this.Splitter1MenuItem.Text = "-";
  187. //
  188. // ExitMenuItem
  189. //
  190. this.ExitMenuItem.Index = 4;
  191. this.ExitMenuItem.Text = "E&xit";
  192. this.ExitMenuItem.Click += new System.EventHandler(this.ExitMenuItem_Click);
  193. //
  194. // HelpMenu
  195. //
  196. this.HelpMenu.Index = 1;
  197. this.HelpMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  198. this.AboutMenuItem});
  199. this.HelpMenu.Text = "&Help";
  200. //
  201. // AboutMenuItem
  202. //
  203. this.AboutMenuItem.Index = 0;
  204. this.AboutMenuItem.Text = "&About";
  205. this.AboutMenuItem.Click += new System.EventHandler(this.AboutMenuItem_Click);
  206. //
  207. // StatusBar
  208. //
  209. this.StatusBar.Location = new System.Drawing.Point(0, 320);
  210. this.StatusBar.Name = "StatusBar";
  211. this.StatusBar.Size = new System.Drawing.Size(522, 17);
  212. this.StatusBar.TabIndex = 3;
  213. //
  214. // PacketList
  215. //
  216. this.PacketList.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
  217. this.TimeHeader,
  218. this.ProtocolHeader,
  219. this.SourceHeader,
  220. this.DestinationHeader,
  221. this.LengthHeader});
  222. this.PacketList.Dock = System.Windows.Forms.DockStyle.Fill;
  223. this.PacketList.FullRowSelect = true;
  224. this.PacketList.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
  225. this.PacketList.Location = new System.Drawing.Point(0, 28);
  226. this.PacketList.MultiSelect = false;
  227. this.PacketList.Name = "PacketList";
  228. this.PacketList.Size = new System.Drawing.Size(522, 292);
  229. this.PacketList.TabIndex = 4;
  230. this.PacketList.UseCompatibleStateImageBehavior = false;
  231. this.PacketList.View = System.Windows.Forms.View.Details;
  232. this.PacketList.DoubleClick += new System.EventHandler(this.OnPacketDoubleClick);
  233. //
  234. // TimeHeader
  235. //
  236. this.TimeHeader.Text = "Time";
  237. this.TimeHeader.Width = 120;
  238. //
  239. // ProtocolHeader
  240. //
  241. this.ProtocolHeader.Text = "Protocol";
  242. //
  243. // SourceHeader
  244. //
  245. this.SourceHeader.Text = "Source";
  246. this.SourceHeader.Width = 130;
  247. //
  248. // DestinationHeader
  249. //
  250. this.DestinationHeader.Text = "Destination";
  251. this.DestinationHeader.Width = 130;
  252. //
  253. // LengthHeader
  254. //
  255. this.LengthHeader.Text = "Length";
  256. //
  257. // PacketMonitorForm
  258. //
  259. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  260. this.ClientSize = new System.Drawing.Size(522, 337);
  261. this.Controls.Add(this.PacketList);
  262. this.Controls.Add(this.StatusBar);
  263. this.Controls.Add(this.ToolBar);
  264. this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
  265. this.Menu = this.MainMenu;
  266. this.Name = "PacketMonitorForm";
  267. this.Text = "Mentalis.org Packet Monitor";
  268. this.Closing += new System.ComponentModel.CancelEventHandler(this.PacketMonitormForm_Closing);
  269. this.ResumeLayout(false);
  270. this.PerformLayout();
  271. }
  272. #endregion
  273. static void Main()
  274. {
  275. try
  276. {
  277. Application.Run(new PacketMonitorForm());
  278. }
  279. catch (Exception e)
  280. {
  281. MessageBox.Show(e.Message, "Mentalis.org Packet Monitor", MessageBoxButtons.OK, MessageBoxIcon.Error);
  282. }
  283. }
  284. private void Initialize()
  285. {
  286. // get all interfaces on this computer and list them
  287. IPAddress[] hosts = Dns.Resolve(Dns.GetHostName()).AddressList;
  288. if (hosts.Length == 0)
  289. throw new NotSupportedException("This computer does not have non-loopback interfaces installed!");
  290. for (int i = 0; i < hosts.Length; i++)
  291. {
  292. MonitorMenuItem.MenuItems.Add(hosts[i].ToString(), new EventHandler(this.OnHostsClick));
  293. HostsMenu.MenuItems.Add(hosts[i].ToString(), new EventHandler(this.OnHostsClick));
  294. }
  295. m_PacketMonitors = new PacketMonitor[HostsMenu.MenuItems.Count];
  296. for (int i = 0; i < m_PacketMonitors.Length; i++)
  297. {
  298. m_PacketMonitors[i] = new PacketMonitor(hosts[i]);
  299. m_PacketMonitors[i].NewPacket += new NewPacketEventHandler(this.OnNewPacket);
  300. }
  301. m_Packets = new ArrayList();
  302. }
  303. private void PacketMonitormForm_Closing(object sender, CancelEventArgs e)
  304. {
  305. OnToolBarClick(this, new ToolBarButtonClickEventArgs(StopButton));
  306. }
  307. public void OnToolBarClick(object sender, ToolBarButtonClickEventArgs e)
  308. {
  309. if (e.Button == StopButton)
  310. { // stop listening on all interfaces
  311. for (int i = 0; i < m_PacketMonitors.Length; i++)
  312. {
  313. m_PacketMonitors[i].Stop();
  314. HostsMenu.MenuItems[i].Checked = false;
  315. MonitorMenuItem.MenuItems[i].Checked = false;
  316. }
  317. StatusBar.Text = "Stopped monitoring";
  318. }
  319. else if (e.Button == StartButton)
  320. { // start listening on all interfaces
  321. for (int i = 0; i < m_PacketMonitors.Length; i++)
  322. {
  323. try
  324. {
  325. m_PacketMonitors[i].Start();
  326. }
  327. catch (Exception ex)
  328. {
  329. MessageBox.Show(this, "There was a problem starting the packet monitor for interface " + m_PacketMonitors[i].IP.ToString() + "\r\n\r\n[" + ex.Message + "]", "Mentalis.org Packet Monitor", MessageBoxButtons.OK, MessageBoxIcon.Error);
  330. }
  331. HostsMenu.MenuItems[i].Checked = true;
  332. MonitorMenuItem.MenuItems[i].Checked = true;
  333. }
  334. StatusBar.Text = "Monitoring all interfaces";
  335. }
  336. else if (e.Button == ClearButton)
  337. { // clear the packet list
  338. PacketList.Items.Clear();
  339. m_Packets.Clear();
  340. StatusBar.Text = "Cleared packet list";
  341. }
  342. else if (e.Button == AboutButton)
  343. {
  344. AboutForm af = new AboutForm();
  345. af.ShowDialog(this);
  346. }
  347. }
  348. public void OnHostsClick(object sender, EventArgs e)
  349. {
  350. // start or stop listening on the specified interface
  351. int index = ((MenuItem)sender).Index;
  352. HostsMenu.MenuItems[index].Checked = !HostsMenu.MenuItems[index].Checked;
  353. MonitorMenuItem.MenuItems[index].Checked = HostsMenu.MenuItems[index].Checked;
  354. if (HostsMenu.MenuItems[index].Checked)
  355. {
  356. m_PacketMonitors[index].Start();
  357. StatusBar.Text = "Monitoring " + m_PacketMonitors[index].IP.ToString();
  358. }
  359. else
  360. {
  361. m_PacketMonitors[index].Stop();
  362. StatusBar.Text = "Stopped monitoring " + m_PacketMonitors[index].IP.ToString();
  363. }
  364. }
  365. public void OnPacketDoubleClick(object sender, EventArgs e)
  366. {
  367. ListView l = (ListView)sender;
  368. if (l.SelectedItems.Count > 0)
  369. {
  370. PacketForm pf = new PacketForm((Packet)m_Packets[l.SelectedItems[0].Index]);
  371. pf.Show();
  372. }
  373. }
  374. private void OnNewPacket(PacketMonitor pm, Packet p)
  375. {
  376. // add the new packet to the list
  377. m_Packets.Add(p);
  378. m_PacketsSize += p.TotalLength;
  379. this.Invoke(new UpdatePacketList(OnUpdatePacketList), p);
  380. //PacketList.Items.Add(new ListViewItem(new string[] { p.Time.ToString(), p.Protocol.ToString(), p.Source, p.Destination, p.TotalLength.ToString() }));
  381. //StatusBar.Text = string.Format("Intercepted {0} packet(s) [{1} bytes]", m_Packets.Count, m_PacketsSize);
  382. //UpdatePacketList
  383. }
  384. private void OnUpdatePacketList(Packet p)
  385. {
  386. PacketList.Items.Add(new ListViewItem(new string[] { p.Time.ToString(), p.Protocol.ToString(), p.Source, p.Destination, p.TotalLength.ToString() }));
  387. StatusBar.Text = string.Format("Intercepted {0} packet(s) [{1} bytes] [{2} KB] [{3} MB]", m_Packets.Count, m_PacketsSize, m_PacketsSize / 1024, m_PacketsSize / 1024 / 1024);
  388. }
  389. private void StopMenuItem_Click(object sender, System.EventArgs e)
  390. {
  391. OnToolBarClick(this, new ToolBarButtonClickEventArgs(StopButton));
  392. }
  393. private void ClearMenuItem_Click(object sender, System.EventArgs e)
  394. {
  395. OnToolBarClick(this, new ToolBarButtonClickEventArgs(ClearButton));
  396. }
  397. private void AboutMenuItem_Click(object sender, System.EventArgs e)
  398. {
  399. OnToolBarClick(this, new ToolBarButtonClickEventArgs(AboutButton));
  400. }
  401. private void ExitMenuItem_Click(object sender, System.EventArgs e)
  402. {
  403. this.Close();
  404. }
  405. private System.Windows.Forms.ListView PacketList;
  406. private System.Windows.Forms.MainMenu MainMenu;
  407. private System.Windows.Forms.ImageList ToobarImages;
  408. private System.Windows.Forms.ToolBar ToolBar;
  409. private System.Windows.Forms.StatusBar StatusBar;
  410. private System.Windows.Forms.ToolBarButton StartButton;
  411. private System.Windows.Forms.ToolBarButton StopButton;
  412. private System.Windows.Forms.ToolBarButton AboutButton;
  413. private System.Windows.Forms.ToolBarButton ClearButton;
  414. private System.Windows.Forms.MenuItem FileMenu;
  415. private System.Windows.Forms.MenuItem ExitMenuItem;
  416. private System.Windows.Forms.MenuItem HelpMenu;
  417. private System.Windows.Forms.MenuItem AboutMenuItem;
  418. private System.Windows.Forms.ColumnHeader TimeHeader;
  419. private System.Windows.Forms.ColumnHeader ProtocolHeader;
  420. private System.Windows.Forms.ColumnHeader SourceHeader;
  421. private System.Windows.Forms.ColumnHeader DestinationHeader;
  422. private System.Windows.Forms.ColumnHeader LengthHeader;
  423. private System.Windows.Forms.MenuItem MonitorMenuItem;
  424. private System.Windows.Forms.MenuItem StopMenuItem;
  425. private System.Windows.Forms.MenuItem ClearMenuItem;
  426. private System.Windows.Forms.MenuItem Splitter1MenuItem;
  427. private System.Windows.Forms.ContextMenu HostsMenu;
  428. private System.ComponentModel.IContainer components;
  429. private PacketMonitor[] m_PacketMonitors;
  430. private ArrayList m_Packets;
  431. private int m_PacketsSize = 0;
  432. }
  433. }