Click here to Skip to main content
Licence GPL3
First Posted 27 May 2003
Views 312,272
Downloads 6,372
Bookmarked 131 times

Packet Sniffing with Winpcap Functions Ported to a .NET Library

By | 25 Mar 2009 | Article
Using Winpcap functions in the .NET Framework
Sample Image - dotnetwinpcap.jpg

Introduction

Winpcap has been the de facto library in packet capture applications, but the problem is that 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 resources.

  • 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.

History

  • 28th May, 2003: Initial post
  • 25th Aug 2003 - Updated source code
  • 28th June, 2008: Updated source code
  • 24th March, 2009: Updated source code to include client code as requested by Ashin

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

About the Author

Victor Tan



Australia Australia

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralCross threading PinmemberOj50004:48 25 May '11  
Generalhelp to convert to C++/CLI Pinmembermiki itz clutch1:54 18 Feb '11  
GeneralPcap.Net - a full WinPcap .NET wrapper Pinmemberbrickner6:17 26 Feb '10  
GeneralThanks!! Pinmemberyuriyag23:00 3 Feb '11  
GeneralWinCe PinmemberMPH19560:12 21 Feb '10  
GeneralWinCE PinmemberMPH19560:10 21 Feb '10  
GeneralC# Sample-Project dont works with c# 2008 Pinmembereightbitwitch12:41 4 Feb '10  
GeneralRe: C# Sample-Project dont works with c# 2008 PinmemberMember 64305:09 11 May '10  
AnswerRe: C# Sample-Project dont works with c# 2008 Pinmemberboblogan4:11 9 Sep '10  
GeneralGet all Packet, inclusive Packets with Set FIN-Flag Pinmembereightbitwitch7:17 4 Feb '10  
GeneralSendPacket are not public - Not acces from VB9 Pinmembereightbitwitch7:11 4 Feb '10  
GeneralChanges required to run on Vista 64 in Visual Studio 2008 PinmemberUmopepisdn23:43 4 Jan '10  
Generalthanks Pinmember7916711522:28 8 Sep '09  
General??? please help PinmemberJordan Wis16:28 15 Aug '09  
GeneralMy vote of 1 Pinmemberlegion_10:44 9 Jul '09  
Generalcraft packet PinmemberUnruled Boy15:37 1 Jan '09  
GeneralFull source uploaded PinmemberVictor Tan7:25 28 Jun '08  
GeneralRe: Full source uploaded PinmemberAnil Maurya3:45 24 Oct '08  
GeneralRe: Full source uploaded PinmemberVictor Tan4:30 24 Oct '08  
QuestionDecoding packets PinmemberTariqHussain23:13 1 Jan '08  
hi ,
i wpuld like to know how to decode packets using
winpcap in ur project .
plz reply me as soon as possible
Thankx.
QuestionIs it possible to develop a packet sniffer with out WinPcap? PinmemberDiana Fernandez23:30 12 Nov '07  
AnswerRe: Is it possible to develop a packet sniffer with out WinPcap? Pinmemberwurzel_cidermaker6:58 28 Jun '08  
Questionexample for VB.NET 2005 ? Pinmembertecnicosht5:03 19 Jun '07  
Questionupgrade to .net 2005 ? Pinmemberranchu panchu9:50 11 Jun '07  
AnswerRe: upgrade to .net 2005 ? Pinmemberranchu panchu19:13 17 Jun '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 25 Mar 2009
Article Copyright 2003 by Victor Tan
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid