Click here to Skip to main content
6,295,667 members and growing! (12,885 online)
Email Password   helpLost your password?
General Programming » Internet / Network » Internet & Network     Intermediate License: The GNU General Public License (GPL)

Packet Sniffing with Winpcap Functions Ported to a .NET Library

By Victor Tan

Using Winpcap functions in the .NET Framework
C#.NET 1.0, Win2K, WinXP, Dev
Version:2 (See All)
Posted:27 May 2003
Updated:25 Mar 2009
Views:191,864
Bookmarked:98 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
39 votes for this article.
Popularity: 6.89 Rating: 4.33 out of 5
3 votes, 7.7%
1

2
2 votes, 5.1%
3
4 votes, 10.3%
4
30 votes, 76.9%
5
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 (GPL)

About the Author

Victor Tan


Member

Location: Australia Australia

Other popular Internet / Network articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 82 (Total in Forum: 82) (Refresh)FirstPrevNext
Generalcraft packet PinmemberUnruled Boy16:37 1 Jan '09  
GeneralFull source uploaded PinmemberVictor Tan8:25 28 Jun '08  
GeneralRe: Full source uploaded PinmemberAnil Maurya4:45 24 Oct '08  
GeneralRe: Full source uploaded PinmemberVictor Tan5:30 24 Oct '08  
QuestionDecoding packets PinmemberTariqHussain0:13 2 Jan '08  
GeneralIs it possible to develop a packet sniffer with out WinPcap? PinmemberDiana Fernandez0:30 13 Nov '07  
AnswerRe: Is it possible to develop a packet sniffer with out WinPcap? Pinmemberwurzel_cidermaker7:58 28 Jun '08  
Questionexample for VB.NET 2005 ? Pinmembertecnicosht6:03 19 Jun '07  
Generalupgrade to .net 2005 ? Pinmemberranchu panchu10:50 11 Jun '07  
GeneralRe: upgrade to .net 2005 ? Pinmemberranchu panchu20:13 17 Jun '07  
GeneralRe: upgrade to .net 2005 ? Pinmembercooke12321:36 13 Nov '07  
GeneralUrgent:winpcap with realbasic? Pinmemberinfantilo10:10 28 Nov '06  
Questionget the content? Pinmemberdennis02063:33 1 Nov '06  
AnswerRe: get the content? Pinmemberdennis02064:51 1 Nov '06  
Generalread a file.acp PinmemberDanieleBianchi6:55 21 Sep '06  
GeneralHow to get the payload of packet PinmemberAmol pathak21:16 17 Sep '06  
QuestionI need codes in JAVA in order to read, save and use Packets captured from VoIP traffic?? (UDP) Pinmemberandre_toro10:28 9 Aug '06  
Generaldotnetwinpcap.dll ??? Pinmemberpgr_home10:08 2 Aug '06  
GeneralNo packet data! PinmemberLady-green22:52 27 May '06  
GeneralRe: No packet data! Pinmemberspeedofspin6:31 19 Jul '06  
GeneralDo Not Decompile Pinmemberpunkbuster7:06 9 Feb '06  
GeneralAssembling packets Pinmembervetris11122:16 31 Jan '06  
GeneralUpdated Version PinmemberShawn M Lewis16:12 24 Nov '05  
GeneralRe: Updated Version Pinmemberdjaxl11:16 24 Jan '06  
GeneralMail me the source code pls Pinmembernunomag21:07 28 Oct '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 25 Mar 2009
Editor: Deeksha Shenoy
Copyright 2003 by Victor Tan
Everything else Copyright © CodeProject, 1999-2009
Web11 | Advertise on the Code Project