5,145,999 members and growing! (15,864 online)
Email Password   helpLost your password?
Web Development » Internet / Network » Internet & Network     Intermediate

Packet sniffing with Winpcap functions ported to a .NET library

By Victor Tan

Using Winpcap functions in the .NET framework
C#Windows, .NET, .NET 1.0, Win2K, WinXPVS.NET2002, VS, Dev

Posted: 27 May 2003
Updated: 21 Aug 2003
Views: 157,528
Announcements



Search    
Advanced Search
Sitemap
36 votes for this Article.
Popularity: 6.50 Rating: 4.18 out of 5
3 votes, 8.3%
1
0 votes, 0.0%
2
2 votes, 5.6%
3
3 votes, 8.3%
4
28 votes, 77.8%
5

Sample Image - dotnetwinpcap.jpg

Introduction

Winpcap (winpcap.polito.it) has been the de facto library in packet capture applications, but the problem is it is only natively available for C++ and C.

This is an attempt to port some of the crucial Winpcap functions for the .NET environment. The demonstration project here is written in C#.

First of all, you need to install Winpcap from winpcap's web site and then extract the project zip file. Be sure to reference dotnetwinpcap.dll in the project if not already so.

Methods available

static ArrayList FindAllDevs() - Returns an ArrayList of Device objects, each describing an Ethernet interface on the system.

bool Open(string source, int snaplen, int flags, int read_timeout) - Opens an Ethernet interface with source as the name of the interface obtained from a Device object, snaplen is the max number of bytes to be captured from each packet, flags=1 means promiscuous mode, read_timeout is the blocking time of ReadNext before it returns.

PCAP_NEXT_EX_STATE ReadNext( out PacketHeader p, out byte[] packet_data) - Reads a next packet and return the packet details (size and timestamp) to object p, and packet raw data in packet_data (array of bytes).

void StopDump() - stops dumping of capture data to a file.

bool StartDump(string filename) - starts dumping of capture data to a file.

bool SetMinToCopy(int size) - sets the minimum number of bytes required to be received by the driver before OnReceivePacket fires. Lowering this can increase response time, but increases system calls which lowers program efficiency.

bool SetKernelBuffer(int bytes) - sets the number of bytes in the driver kernel buffer for packet capture. Increase this to avoid packet loss and improve performance. Default is 1 MB.

void StartListen() - starts listening for packets

void StopListen() - stops listening for packets

void Close() - stops all operations and releases all resource

bool SendPacket(byte[] rawdata) - sends bytes contained in rawdata over the wire. The ethernet checksum will be automatically added prior to sending the packet. Returns true if send is successful, false otherwise.

Properties

bool IsListening - true if the dotnetWinpcap object is listening, false otherwise.

string LastError - returns the last error encountered by the library, if any.

Event support

delegate void ReceivePacket (object sender, PacketHeader p, byte[] s);

event ReceivePacket OnReceivePacket;

Once StartListen() is called, OnReceivePacket will start to fire on every packet encountered, until StopListen() is called, or Close() is called.

Delegate objects of the above signature may be attached to the OnReceivePacket event to receive notification and perform further processing, as demonstrated in the demo source code.

Coming up...

The next version will include driver-level filtering of packets, depending on demand for further development.

History

25 Aug 2003 - Updated source code

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Victor Tan



Location: Australia Australia

Other popular Internet / Network articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 77 (Total in Forum: 77) (Refresh)FirstPrevNext
Subject  Author Date 
QuestionDecoding packetsmemberTariqHussain0:13 2 Jan '08  
GeneralIs it possible to develop a packet sniffer with out WinPcap?memberDiana Fernandez0:30 13 Nov '07  
Questionexample for VB.NET 2005 ?membertecnicosht6:03 19 Jun '07  
Generalupgrade to .net 2005 ?memberranchu panchu10:50 11 Jun '07  
GeneralRe: upgrade to .net 2005 ?memberranchu panchu20:13 17 Jun '07  
GeneralRe: upgrade to .net 2005 ?membercooke12321:36 13 Nov '07  
GeneralUrgent:winpcap with realbasic?memberinfantilo10:10 28 Nov '06  
Questionget the content?memberdennis02063:33 1 Nov '06  
AnswerRe: get the content?memberdennis02064:51 1 Nov '06  
Generalread a file.acpmemberDanieleBianchi6:55 21 Sep '06  
GeneralHow to get the payload of packetmemberAmol pathak21:16 17 Sep '06  
QuestionI need codes in JAVA in order to read, save and use Packets captured from VoIP traffic?? (UDP)memberandre_toro10:28 9 Aug '06  
Generaldotnetwinpcap.dll ???memberpgr_home10:08 2 Aug '06  
GeneralNo packet data!memberLady-green22:52 27 May '06  
GeneralRe: No packet data!memberspeedofspin6:31 19 Jul '06  
GeneralDo Not Decompilememberpunkbuster7:06 9 Feb '06  
GeneralAssembling packetsmembervetris11122:16 31 Jan '06  
GeneralUpdated VersionmemberShawn M Lewis16:12 24 Nov '05  
GeneralRe: Updated Versionmemberdjaxl11:16 24 Jan '06  
GeneralMail me the source code plsmembernunomag21:07 28 Oct '05  
GeneralRe: Mail me the source code plsmemberHamid Qureshi17:59 30 Oct '05  
GeneralRe: Mail me the source code plsmemberdjaxl11:14 24 Jan '06