![]() |
Languages »
C# »
PInvoke
Intermediate
Raw Socket Capturing Using C#By Ali Hasan Abdul-AalProvides a C# wrapper library to WinPcap and includes an sample application, ShowWinPcapNET. |
C#.NET 1.0, Win2K, WinXP, Visual Studio, Dev
|
|
Advanced Search |
|
|
|
||||||||||||||||

The project's purpose is to capture Ethernet packets using the WinPcap engine and to take the first step toward making a packet analyzer like Ethereal or Analyzer.
As far as I saw on the 'Net there was no code or method to capture an Ethernet packet using C# and the .NET Framework, so I made a library (WinPcapNET) as a wrapper library to WinPcap, which is the base of most packet analyzers on the Win32 and other platforms.
WinPcap : The engine was copy-righted to Politecnico di Torino at http://winpcap.polito.it/ and some other mirrors; however, for some reason the site was stopped and most of the mirrors as well. You can still find it on http://www.wireshark.org/
The Engine used here is WinPcap Alpha3.0, which is the same engine used by Ethereal, Analyzer and windump.
WinPcapNET1.0 is the DLL that will communicate with the WinPcap engine. It is written in C. The key file in this DLL project is WinPcapNET1.0.cpp. It has one method to carry out the DLL's job, which is:
void CapturePacket(int driver_number1, int packet_number1)
driver_number1is the driver number installed in the machine. It can be a WAN, LAN, PPP or the NDSI driver that provides the packet passing the network interface. packet_number1 is the number of packets the method will capture before the information is available. Explanation of the Method:
driver_number1 parameter and asks the WinPcap engine to capture and serialize the packets . WinPcapNET is the core of the project which will handle the capturing and interaction with the C WinPcapNET1.0.dll and retrieve the packets to the C# application. It consists of two classes:
RawPacket class is the packet data-carrier which is used to hold the packet data and its seq-number. WinPcap class is actually a wrapper class that will initiate the capturing and retrieve a specific packet or all packets. RawPacket(string header, string packet) as the constructor. string Header string Packet which is the 2 * hexadecimal form of the packet. I made this to make it easier to handle it in the text form rather than putting it as binary in the file. It gives all public and static methods to access the capture file indirectly.
![]() |
|
This is a sample that will show how to use the two pervious packages. It's a GUI implementation and is not threaded to keep the sample as simple as possible.

Capture() method and the Capture button calls the method. The program blocks until it captures the 20 packets specified in the "Number of Packets to be captured" and then list them in the ListBox packetSize), From: Ethernet-number, To: Ethernet-number. TextBox, filling them in 16 rows.These are the key pieces of code to operate the WinPcapNET. It will handle the rest.
WinPcap.Capture(int.Parse(this.txt_device_number.Text),
int.Parse(this.txt_packet_number.Text));
WinPcap.WaitToFinish();
RawPacket[] packet = WinPcap.GetAllPackets();
// this part is discussed above in the classes and method explanation.
The code of both WinPcapNET and WinPcapNet1.0 will be submitted later after I update the code and put some final touches on it.
I'll submit WinPcapNETv2 later, God willing, and it will be completely in C# and will support real-time CHAR capturing. I'm working on it now and it will be only one library containing the structures of WinPcap itself to simplify its use.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 19 Feb 2003 Editor: Chris Maunder |
Copyright 2003 by Ali Hasan Abdul-Aal Everything else Copyright © CodeProject, 1999-2009 Web11 | Advertise on the Code Project |