Click here to Skip to main content
15,885,767 members
Articles / Programming Languages / C#

Packet Capture and Analayzer

Rate me:
Please Sign up or sign in to vote.
4.88/5 (119 votes)
23 Sep 2003CPOL8 min read 1.5M   36.1K   437  
Packet capture and analyzer program. With this program you can capture, display, analyze, save packets or load a saved packet file. It works like Etheral does.
using System;
using System.Windows.Forms;

namespace MyClasses
{

	public class PacketHTTP
	{

		public struct PACKET_HTTP
		{
			public string [] Contents;
		}


		public PacketHTTP()
		{

		}


		public static bool Parser( ref TreeNodeCollection mNode, 
			byte [] PacketData , 
			ref int Index , 
			ref ListViewItem LItem , bool DisplayData )
		{
			TreeNode mNodex;
			string Tmp = "";
			int Size = 0, i = 0;
			char [] seperator = new char[2];
			PACKET_HTTP PHttp;

			seperator[0] = (char) 13;
			seperator[1] = (char) 10;

			Size = PacketData.GetLength(0) - Index;

			mNodex = new TreeNode();
			mNodex.Text = "HTTP ( Hyper Text Transfer Protocol )";
			Function.SetPosition( ref mNodex , Index , PacketData.Length - Index , true );
	
			try
			{
				for( i = 0; i < Size; i ++ )
				{
					if( ( PacketData[ Index ]  > 31 ) | 
						( PacketData[ Index ] < 129 ) | 
						( PacketData[ Index ] == 13 ) | 
						( PacketData[ Index ] == 10 ) )
						Tmp += (char) PacketData[ Index ];
					else
						Tmp += " ";

					Index++;
				}
				
				PHttp.Contents = Tmp.Split( seperator );

				if( DisplayData )
				{
					for( i = 0; i < PHttp.Contents.GetLength(0); i ++ )
					{
						Tmp = (string) PHttp.Contents.GetValue( i );
						Tmp = Tmp.Trim();
						if( Tmp != "" )
							mNodex.Nodes.Add( Tmp + "\\r\\n" );
					}
				}
				else
				{
					mNodex.Nodes.Add("Continutiation Message");
				}
				
				LItem.SubItems[ Const.LIST_VIEW_PROTOCOL_INDEX ].Text = "HTTP";
				LItem.SubItems[ Const.LIST_VIEW_INFO_INDEX ].Text = PHttp.Contents[0];

				mNode.Add( mNodex );
				
			}
			catch( Exception Ex )
			{
				mNode.Add( mNodex );
				Tmp = "[ Malformed HTTP packet. Remaining bytes don't fit an HTTP packet. Possibly due to bad decoding ]";
				mNode.Add( Tmp );
				Tmp = "[ Exception raised is <" + Ex.GetType().ToString() + "> at packet index <" + Index.ToString() + "> ]";
				mNode.Add( Tmp );
				LItem.SubItems[ Const.LIST_VIEW_INFO_INDEX ].Text = "[ Malformed HTTP packet. Remaining bytes don't fit an HTTP packet. Possibly due to bad decoding ]";

				return false;
			}

			return true;

		}


		public static bool Parser( byte [] PacketData , 
			ref int Index , 
			ref ListViewItem LItem , bool DisplayData )
		{
			string Tmp = "";
			int Size = 0;
			int i = 0;
			char [] seperator = new char[2];
			PACKET_HTTP PHttp;

			seperator[0] = (char) 13;
			seperator[1] = (char) 10;

			Size = PacketData.GetLength(0) - Index;

			try
			{
				for( i = 0; i < Size; i ++ )
				{
					if( ( PacketData[ Index ]  > 31 ) | 
						( PacketData[ Index ] < 129 ) | 
						( PacketData[ Index ] == 13 ) | 
						( PacketData[ Index ] == 10 ) )
						Tmp += (char) PacketData[ Index ];
					else
						Tmp += " ";

					Index++;
				}
				
				PHttp.Contents = Tmp.Split( seperator );

				LItem.SubItems[ Const.LIST_VIEW_PROTOCOL_INDEX ].Text = "HTTP";
				LItem.SubItems[ Const.LIST_VIEW_INFO_INDEX ].Text = PHttp.Contents[0];

			}
			catch
			{
				Tmp = "[ Malformed HTTP packet. Remaining bytes don't fit an HTTP packet. Possibly due to bad decoding ]";
				LItem.SubItems[ Const.LIST_VIEW_INFO_INDEX ].Text = "[ Malformed HTTP packet. Remaining bytes don't fit an HTTP packet. Possibly due to bad decoding ]";

				return false;
			}

			return true;

		}

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Turkey Turkey
Hi to all...
I am an alone programmer. i am not a specialist on programming but i love it. anyone who supports source code sharing is definetely my friend.
Because i am so poor on writing about myself, anyone who wants to learn more about me can feel free to contact me...

Comments and Discussions