Click here to Skip to main content
15,868,099 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 571.1K   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

 
AnswerRe: get the content? Pin
dennis02061-Nov-06 3:51
dennis02061-Nov-06 3:51 
Generalread a file.acp Pin
DanieleBianchi21-Sep-06 5:55
DanieleBianchi21-Sep-06 5:55 
QuestionHow to get the payload of packet Pin
Amol pathak17-Sep-06 20:16
Amol pathak17-Sep-06 20:16 
QuestionI need codes in JAVA in order to read, save and use Packets captured from VoIP traffic?? (UDP) Pin
andre_toro9-Aug-06 9:28
andre_toro9-Aug-06 9:28 
Questiondotnetwinpcap.dll ??? Pin
pgr_home2-Aug-06 9:08
professionalpgr_home2-Aug-06 9:08 
GeneralNo packet data! Pin
Lady-green27-May-06 21:52
Lady-green27-May-06 21:52 
GeneralRe: No packet data! Pin
speedofspin19-Jul-06 5:31
speedofspin19-Jul-06 5:31 
GeneralMore detail on needed changes in order to capture all of the traffic Pin
mobiledeveloper57-Jan-10 16:10
mobiledeveloper57-Jan-10 16:10 
Actually, the function is dotnetWinpCap.cs->ReadNextInternal

This set of changes worked for me:

string packetdata=(string)Marshal.PtrToStringAnsi(pktdata);
CloseHandle(pktdata);
/*
//byte[] packetdata=(byte[])
// clean up pkthdr
CloseHandle(pkthdr);
pkthdr=IntPtr.Zero;
*/

p=new PacketHeader();

//p.Caplength=(int)packetheader.caplen;
//p.Length=(int)packetheader.len;

p.Caplength = (int)packetdata.Length;
p.Length = (int)packetdata.Length;

p.ts = packetheader.ts;
packet_data=new byte[p.Length];
Marshal.Copy(pktdata, packet_data,0,p.Length);
return PCAP_NEXT_EX_STATE.SUCCESS;

Thanks.
GeneralDo Not Decompile Pin
punkbuster9-Feb-06 6:06
punkbuster9-Feb-06 6:06 
GeneralAssembling packets Pin
vetriselvan_na31-Jan-06 21:16
vetriselvan_na31-Jan-06 21:16 
GeneralUpdated Version Pin
Shawn M Lewis24-Nov-05 15:12
Shawn M Lewis24-Nov-05 15:12 
GeneralRe: Updated Version Pin
djaxl24-Jan-06 10:16
djaxl24-Jan-06 10:16 
GeneralMail me the source code pls Pin
nunomag28-Oct-05 20:07
nunomag28-Oct-05 20:07 
GeneralRe: Mail me the source code pls Pin
Hamid Qureshi30-Oct-05 16:59
Hamid Qureshi30-Oct-05 16:59 
GeneralRe: Mail me the source code pls Pin
djaxl24-Jan-06 10:14
djaxl24-Jan-06 10:14 
GeneralRe: Mail me the source code pls Pin
nunomag24-Jan-06 23:35
nunomag24-Jan-06 23:35 
GeneralRe: Mail me the source code pls Pin
punkbuster6-Feb-06 11:55
punkbuster6-Feb-06 11:55 
GeneralRe: Mail me the source code pls Pin
ranchu panchu17-Jun-07 19:16
ranchu panchu17-Jun-07 19:16 
Questiondotnetwinpcap? Pin
bujia3-Oct-05 23:31
bujia3-Oct-05 23:31 
Generaldissecting IP packets in c# Pin
jose17058314-Sep-05 5:38
jose17058314-Sep-05 5:38 
Question17how to write an IDS? Pin
Member 216236312-Aug-05 3:05
Member 216236312-Aug-05 3:05 
QuestionContinue Project? Pin
eugenevd1-Aug-05 3:39
eugenevd1-Aug-05 3:39 
AnswerRe: Continue Project? Pin
windrago26-Feb-06 20:26
windrago26-Feb-06 20:26 
QuestionCommercial use? Pin
linevty0422-Jul-05 9:44
linevty0422-Jul-05 9:44 
GeneralMilliseconds Pin
skipi8316-Jun-05 6:41
skipi8316-Jun-05 6:41 

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.