Click here to Skip to main content
15,883,901 members
Articles / Programming Languages / C#

Packet Sniffing with Winpcap Functions Ported to a .NET Library

Rate me:
Please Sign up or sign in to vote.
4.83/5 (54 votes)
25 Mar 2009GPL32 min read 573K   15K   146   104
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

  • C#
    static ArrayList FindAllDevs()

    Returns an ArrayList of Device objects, each describing an Ethernet interface on the system.

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

  • C#
    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).

  • C#
    void StopDump()

    Stops dumping of capture data to a file.

  • C#
    bool StartDump(string filename) 

    Starts dumping of capture data to a file.

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

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

  • C#
    void StartListen()

    Starts listening for packets.

  • C#
    void StopListen()

    Stops listening for packets.

  • C#
    void Close()

    Stops all operations and releases all resources.

  • C#
    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

  • C#
    bool IsListening

    true if the dotnetWinpcap object is listening, false otherwise.

  • C#
    string LastError

    Returns the last error encountered by the library, if any.

Event Support

C#
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)


Written By
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Read the content without saving to a file Pin
Member 109677122-May-04 14:05
Member 109677122-May-04 14:05 
Generalis that all the methods and enum in winpcap is present in dotnetwinpcap Pin
murali venugopal13-Mar-04 2:44
murali venugopal13-Mar-04 2:44 
Generalthanks! Pin
mikeluedke2-Mar-04 8:22
mikeluedke2-Mar-04 8:22 
QuestionHow to send ip packets... Pin
Nagareshwar9-Jan-04 3:06
Nagareshwar9-Jan-04 3:06 
GeneralGet source and destination ip addresses Pin
dragomir7-Jan-04 3:06
dragomir7-Jan-04 3:06 
GeneralRe: Get source and destination ip addresses Pin
Member 109677113-May-04 13:51
Member 109677113-May-04 13:51 
GeneralRe: Get source and destination ip addresses Pin
Mathias Falkenberg1-Jun-04 22:34
Mathias Falkenberg1-Jun-04 22:34 
GeneralRe: Get source and destination ip addresses Pin
PyroSA11-Jul-05 15:41
PyroSA11-Jul-05 15:41 
GeneralRe: Get source and destination ip addresses Pin
orelero28-Dec-05 23:20
orelero28-Dec-05 23:20 
GeneralVery Nice Pin
Fergal Boden2-Dec-03 5:36
Fergal Boden2-Dec-03 5:36 
GeneralDelphi and ip packet Pin
ibrahim bulut18-Nov-03 23:17
ibrahim bulut18-Nov-03 23:17 
GeneralRe: Delphi and ip packet Pin
Anonymous5-Mar-05 23:23
Anonymous5-Mar-05 23:23 
GeneralSource Code Pin
rbusch4222-Oct-03 6:07
rbusch4222-Oct-03 6:07 
GeneralLooks good Pin
Giles25-Aug-03 21:48
Giles25-Aug-03 21:48 
GeneralGoo Job Pin
Mark Lai25-Aug-03 17:06
Mark Lai25-Aug-03 17:06 
GeneralStrong named version... Pin
Focht22-Aug-03 3:10
Focht22-Aug-03 3:10 
GeneralRaw Hex Pin
pro-logic20-Jun-03 20:27
pro-logic20-Jun-03 20:27 
GeneralRe: Raw Hex Pin
Victor Tan22-Jun-03 15:45
Victor Tan22-Jun-03 15:45 
QuestionNeed a Code-Friend ? Pin
firat kocak15-Jun-03 21:18
firat kocak15-Jun-03 21:18 
AnswerRe: Need a Code-Friend ? Pin
Victor Tan22-Jun-03 15:48
Victor Tan22-Jun-03 15:48 
GeneralRe: Need a Code-Friend ? Pin
firat kocak22-Jun-03 21:49
firat kocak22-Jun-03 21:49 
GeneralSource Code Pin
balramsingh10-Jun-03 20:23
balramsingh10-Jun-03 20:23 
Questiondotnetwinpcap.dll sources? Pin
Julien Couvreur29-May-03 9:30
Julien Couvreur29-May-03 9:30 
AnswerRe: dotnetwinpcap.dll sources? Pin
kikon5-Aug-03 7:43
kikon5-Aug-03 7:43 
GeneralRe: dotnetwinpcap.dll sources? Pin
Julien Couvreur5-Aug-03 8:27
Julien Couvreur5-Aug-03 8:27 

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

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