Click here to Skip to main content
15,885,366 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 573.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

 
GeneralMore detail on needed changes in order to capture all of the traffic Pin
mobiledeveloper57-Jan-10 16:10
mobiledeveloper57-Jan-10 16:10 
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 
<br />
[assembly: System.Reflection.AssemblyKeyName("")]<br />
[assembly: System.Reflection.AssemblyKeyFile("d:\\data\\programming\\vcs\\key.snk")]<br />
[assembly: System.Reflection.AssemblyDelaySign(false)]<br />
[assembly: System.Reflection.AssemblyTrademark("")]<br />
[assembly: System.Reflection.AssemblyCopyright("(c) 2003 Victor Tan")]<br />
[assembly: System.Reflection.AssemblyProduct("")]<br />
[assembly: System.Reflection.AssemblyCompany("utopian_centaury@yahoo.com")]<br />
[assembly: System.Reflection.AssemblyConfiguration("")]<br />
[assembly: System.Reflection.AssemblyDescription("Winpcap port for .NET framework")]<br />
[assembly: System.Reflection.AssemblyTitle("dotnetWinpcap library")]<br />
<br />
using System;<br />
using System.Collections;<br />
using System.Runtime.InteropServices;<br />
using System.Text;<br />
using System.Threading;<br />
<br />
public class Device<br />
{<br />
  private string name;<br />
  private string description;<br />
  private string address;<br />
  private string netmask;<br />
<br />
  public Device()<br />
  {<br />
    name = null;<br />
    description = null;<br />
    address = null;<br />
    netmask = null;<br />
    return;<br />
  }<br />
  public virtual string Name<br />
  {<br />
    get<br />
    {<br />
      return name;<br />
    }<br />
    set<br />
    {<br />
      name = value;<br />
      return;<br />
    }<br />
  }<br />
  public virtual string Description<br />
  {<br />
    get<br />
    {<br />
      return description;<br />
    }<br />
    set<br />
    {<br />
      description = value;<br />
      return;<br />
    }<br />
  }<br />
  public virtual string Address<br />
  {<br />
    get<br />
    {<br />
      return address;<br />
    }<br />
    set<br />
    {<br />
      address = value;<br />
      return;<br />
    }<br />
  }<br />
  public virtual string Netmask<br />
  {<br />
    get<br />
    {<br />
      return netmask;<br />
    }<br />
    set<br />
    {<br />
      netmask = value;<br />
      return;<br />
    }<br />
  }<br />
}<br />
public class dotnetWinpCap<br />
{<br />
  public class AlreadyOpenException : Exception<br />
  {<br />
<br />
    public AlreadyOpenException()<br />
    {<br />
      return;<br />
    }<br />
    public override string Message<br />
    {<br />
      get<br />
      {<br />
        return "Device attached to object already open. Close first before reopening";<br />
      }<br />
    }<br />
  }<br />
  delegate void packet_handler(int param, int header, int pkt_data);<br />
  public enum PCAP_NEXT_EX_STATE<br />
  {<br />
    SUCCESS = 1,<br />
    TIMEOUT = 0,<br />
    ERROR = -1,<br />
    EOF = -2,<br />
    UNKNOWN = -3,<br />
  }<br />
  delegate void ReceivePacket(object sender, PacketHeader p, System.Byte[] s);<br />
  delegate void DumpEnded(object sender);<br />
  delegate void ReceivePacketInternal(object sender, int header, int data);<br />
  public struct pcap_pkthdr<br />
  {<br />
    public dotnetWinpCap.timeval ts;<br />
    public UInt32 caplen;<br />
    public UInt32 len;<br />
  }<br />
  public struct timeval<br />
  {<br />
    public UInt32 tv_sec;<br />
    public UInt32 tv_usec;<br />
  }<br />
  private struct pcap_rmtauth<br />
  {<br />
    private int type;<br />
    private string username;<br />
    private string password;<br />
  }<br />
  private struct in_addr<br />
  {<br />
    public char b1;<br />
    public char b2;<br />
    public char b3;<br />
    public char b4;<br />
    public UInt16 w1;<br />
    public UInt16 w2;<br />
    public UInt64 addr;<br />
  }<br />
  private struct sockaddr<br />
  {<br />
    public short family;<br />
    public UInt16 port;<br />
    public System.Byte[] addr;<br />
    public System.Byte[] zero;<br />
  }<br />
  private struct pcap_addr<br />
  {<br />
    public int next;<br />
    public int addr;<br />
    public int netmask;<br />
    public int broadaddr;<br />
    public int dstaddr;<br />
  }<br />
  private struct pcap_if<br />
  {<br />
    public int next;<br />
    public string name;<br />
    public string description;<br />
    public int addresses;<br />
    public UInt32 flags;<br />
  }<br />
  private Thread ListenThread;<br />
  private bool disposed;<br />
  private dotnetWinpCap.packet_handler callback;<br />
  private string fname;<br />
  private int maxb;<br />
  private int maxp;<br />
  private bool m_islistening;<br />
  private bool m_isopen;<br />
  private string m_attachedDevice;<br />
  private int pcap_t;<br />
  private int dumper;<br />
  private dotnetWinpCap.ReceivePacket OnReceivePacket;<br />
  private dotnetWinpCap.DumpEnded OnDumpEnded;<br />
  private dotnetWinpCap.ReceivePacketInternal OnReceivePacketInternal;<br />
  private StringBuilder errbuf;<br />
<br />
  public dotnetWinpCap()<br />
  {<br />
    ListenThread = null;<br />
    disposed = 0;<br />
    callback = null;<br />
    fname = "";<br />
    maxb = 0;<br />
    maxp = 0;<br />
    m_islistening = 0;<br />
    m_isopen = 0;<br />
    m_attachedDevice = null;<br />
    pcap_t = IntPtr.Zero;<br />
    dumper = IntPtr.Zero;<br />
    errbuf = new StringBuilder(256);<br />
    return;<br />
  }<br />
  public virtual string AttachedDevice<br />
  {<br />
    get<br />
    {<br />
      return m_attachedDevice;<br />
    }<br />
  }<br />
  public virtual string LastError<br />
  {<br />
    get<br />
    {<br />
      return errbuf.ToString();<br />
    }<br />
  }<br />
  public virtual bool IsListening<br />
  {<br />
    get<br />
    {<br />
      return m_islistening;<br />
    }<br />
  }<br />
  public virtual bool IsOpen<br />
  {<br />
    get<br />
    {<br />
      return m_isopen;<br />
    }<br />
  }<br />
  public event dotnetWinpCap.ReceivePacket OnReceivePacket;<br />
  private event dotnetWinpCap.DumpEnded OnDumpEnded;<br />
  private event dotnetWinpCap.ReceivePacketInternal OnReceivePacketInternal;<br />
  [DllImport("wpcap.dll", SetLastError=false)]<br />
  private static extern int pcap_findalldevs(ref int devicelist, StringBuilder errbuf);<br />
  [DllImport("wpcap.dll", SetLastError=false)]<br />
  private static extern int pcap_setbuff(int p, int kernelbufferbytes);<br />
  [DllImport("wpcap.dll", SetLastError=false)]<br />
  private static extern int pcap_live_dump(int p, string filename, int maxsize, int maxpacks);<br />
  [DllImport("wpcap.dll", SetLastError=false)]<br />
  private static extern int pcap_live_dump_ended(int p, int sync);<br />
  [DllImport("wpcap.dll", SetLastError=false)]<br />
  private static extern int pcap_dump_open(int p, string filename);<br />
  [DllImport("wpcap.dll", SetLastError=false)]<br />
  private static extern void pcap_dump(int dumper, int h, int data);<br />
  [DllImport("wpcap.dll", SetLastError=false)]<br />
  private static extern void pcap_dump_close(int dumper);<br />
  [DllImport("wpcap.dll", SetLastError=false)]<br />
  private static extern int pcap_sendpacket(int p, System.Byte[] buff, int size);<br />
  [DllImport("wpcap.dll", SetLastError=false)]<br />
  private static extern int pcap_loop(int p, int cnt, dotnetWinpCap.packet_handler callback, int user);<br />
  [DllImport("wpcap.dll", SetLastError=false)]<br />
  private static extern int pcap_open_live(string device, int snaplen, int promisc, int to_ms, StringBuilder ebuf);<br />
  [DllImport("wpcap.dll", SetLastError=false)]<br />
  private static extern System.Byte[] pcap_next(int p, int pkt_header);<br />
  [DllImport("wpcap.dll", SetLastError=false)]<br />
  private static extern int pcap_setmintocopy(int p, int size);<br />
  [DllImport("wpcap.dll", SetLastError=false)]<br />
  private static extern void pcap_freealldevs(int devicelist);<br />
  [DllImport("wpcap.dll", SetLastError=false)]<br />
  private static extern int pcap_open(string source, int snaplen, int flags, int read_timeout, int auth, StringBuilder errbuf);<br />
  [DllImport("wpcap.dll", SetLastError=false)]<br />
  private static extern int pcap_next_ex(int p, ref int pkt_header, ref int packetdata);<br />
  [DllImport("wpcap.dll", SetLastError=false)]<br />
  private static extern void pcap_close(int p);<br />
  [DllImport("Kernel32", SetLastError=false)]<br />
  private static extern bool CloseHandle(int handle);<br />
  public static ArrayList FindAllDevs()<br />
  {<br />
    ArrayList arrayList;<br />
    dotnetWinpCap.pcap_if pcap_if;<br />
    int i;<br />
    StringBuilder stringBuilder;<br />
    int i1;<br />
    Device device;<br />
    dotnetWinpCap.pcap_addr pcap_addr;<br />
    dotnetWinpCap.sockaddr sockaddr;<br />
    dotnetWinpCap.sockaddr sockaddr1;<br />
    System.StringSystem.String[] arr;<br />
    arrayList = new ArrayList();<br />
    pcap_if.addresses = IntPtr.Zero;<br />
    pcap_if.description = new StringBuilder().ToString();<br />
    pcap_if.flags = 0;<br />
    pcap_if.name = new StringBuilder().ToString();<br />
    pcap_if.next = IntPtr.Zero;<br />
    i = IntPtr.Zero;<br />
    stringBuilder = new StringBuilder(256);<br />
    i1 = IntPtr.Zero;<br />
    if (dotnetWinpCap.pcap_findalldevs(ref i, stringBuilder) != -1)<br />
    {<br />
      goto ILO_006d;<br />
    }<br />
    return null;<br />
  ILO_006d:<br />
    i1 = i;<br />
    goto ILO_026f;<br />
  ILO_0075:<br />
    device = new Device();<br />
    arrayList.Add(device);<br />
    pcap_if = ((dotnetWinpCap.pcap_if)(Marshal.PtrToStructure(i, typeof(dotnetWinpCap.pcap_if))));<br />
    device.Name = pcap_if.name;<br />
    device.Description = pcap_if.description;<br />
    if (pcap_if.addresses.ToInt32() == 0)<br />
    {<br />
      goto ILO_0267;<br />
    }<br />
    pcap_addr = ((dotnetWinpCap.pcap_addr)(Marshal.PtrToStructure(pcap_if.addresses, typeof(dotnetWinpCap.pcap_addr))));<br />
    if (pcap_addr.addr.ToInt32() == 0)<br />
    {<br />
      goto ILO_01ab;<br />
    }<br />
    sockaddr = ((dotnetWinpCap.sockaddr)(Marshal.PtrToStructure(pcap_addr.addr, typeof(dotnetWinpCap.sockaddr))));<br />
    arr = new string[7];<br />
    arr[0] = sockaddr.addr[0].ToString();<br />
    arr[1] = ".";<br />
    arr[2] = sockaddr.addr[1].ToString();<br />
    arr[3] = ".";<br />
    arr[4] = sockaddr.addr[2].ToString();<br />
    arr[5] = ".";<br />
    arr[6] = sockaddr.addr[3].ToString();<br />
    device.Address = string.Concat(arr);<br />
  ILO_01ab:<br />
    if (pcap_addr.netmask.ToInt32() == 0)<br />
    {<br />
      goto ILO_0267;<br />
    }<br />
    sockaddr1 = ((dotnetWinpCap.sockaddr)(Marshal.PtrToStructure(pcap_addr.netmask, typeof(dotnetWinpCap.sockaddr))));<br />
    arr = new string[7];<br />
    arr[0] = sockaddr1.addr[0].ToString();<br />
    arr[1] = ".";<br />
    arr[2] = sockaddr1.addr[1].ToString();<br />
    arr[3] = ".";<br />
    arr[4] = sockaddr1.addr[2].ToString();<br />
    arr[5] = ".";<br />
    arr[6] = sockaddr1.addr[3].ToString();<br />
    device.Netmask = string.Concat(arr);<br />
  ILO_0267:<br />
    i = pcap_if.next;<br />
  ILO_026f:<br />
    if (i.ToInt32() != 0)<br />
    {<br />
      goto ILO_0075;<br />
    }<br />
    dotnetWinpCap.pcap_freealldevs(i1);<br />
    return arrayList;<br />
  }<br />
  public virtual bool Open(string source, int snaplen, int flags, int read_timeout)<br />
  {<br />
    if ((pcap_t != IntPtr.Zero) == 0)<br />
    {<br />
      goto ILO_0018;<br />
    }<br />
    throw new dotnetWinpCap.AlreadyOpenException();<br />
  ILO_0018:<br />
    pcap_t = dotnetWinpCap.pcap_open(source, snaplen, flags, read_timeout, IntPtr.Zero, errbuf);<br />
    if (pcap_t.ToInt32() == 0)<br />
    {<br />
      goto ILO_0050;<br />
    }<br />
    m_isopen = 1;<br />
    m_attachedDevice = source;<br />
    return 1;<br />
  ILO_0050:<br />
    m_isopen = 0;<br />
    m_attachedDevice = null;<br />
    return 0;<br />
  }<br />
  private void Loop()<br />
  {<br />
    int i;<br />
    callback = new dotnetWinpCap.packet_handler(this.LoopCallback);<br />
    i = IntPtr.Zero;<br />
    new HandleRef(callback, i);<br />
    dotnetWinpCap.pcap_loop(pcap_t, 0, callback, IntPtr.Zero);<br />
    return;<br />
  }<br />
  private void LoopCallback(int param, int header, int pkt_data)<br />
  {<br />
    dotnetWinpCap.pcap_pkthdr pcap_pkthdr;<br />
    System.ByteSystem.Byte[] arr;<br />
    Marshal.PtrToStringAnsi(param);<br />
    pcap_pkthdr = ((dotnetWinpCap.pcap_pkthdr)(Marshal.PtrToStructure(header, typeof(dotnetWinpCap.pcap_pkthdr))));<br />
    arr = new Byte[pcap_pkthdr.caplen];<br />
    Marshal.Copy(pkt_data, arr, 0, pcap_pkthdr.caplen);<br />
    Marshal.PtrToStringAnsi(pkt_data);<br />
    return;<br />
  }<br />
  private bool OpenLive(string source, int snaplen, int promisc, int to_ms)<br />
  {<br />
    pcap_t = dotnetWinpCap.pcap_open_live(source, snaplen, promisc, to_ms, errbuf);<br />
    if (pcap_t.ToInt32() == 0)<br />
    {<br />
      goto ILO_0025;<br />
    }<br />
    return 1;<br />
  ILO_0025:<br />
    return 0;<br />
  }<br />
  private dotnetWinpCap.PCAP_NEXT_EX_STATE ReadNextInternal(out PacketHeader p, out System.Byte[] packet_data, out int pkthdr, out int pktdata)<br />
  {<br />
    int i;<br />
    dotnetWinpCap.pcap_pkthdr pcap_pkthdr;<br />
    pkthdr = IntPtr.Zero;<br />
    pktdata = IntPtr.Zero;<br />
    p = null;<br />
    packet_data = null;<br />
    if (pcap_t.ToInt32() != 0)<br />
    {<br />
      goto ILO_003c;<br />
    }<br />
    errbuf = new StringBuilder("No adapter is currently open");<br />
    return -1;<br />
  ILO_003c:<br />
    i = dotnetWinpCap.pcap_next_ex(pcap_t, ref pkthdr, ref pktdata);<br />
    if (i != 0)<br />
    {<br />
      goto ILO_0050;<br />
    }<br />
    return 0;<br />
  ILO_0050:<br />
    if (i != 1)<br />
    {<br />
      goto ILO_00cc;<br />
    }<br />
    pcap_pkthdr = ((dotnetWinpCap.pcap_pkthdr)(Marshal.PtrToStructure(pkthdr, typeof(dotnetWinpCap.pcap_pkthdr))));<br />
    p = new PacketHeader();<br />
    p.Caplength = pcap_pkthdr.caplen;<br />
    p.Length = pcap_pkthdr.len;<br />
    p.ts = pcap_pkthdr.ts;<br />
    packet_data = new Byte[((System.UInt32)(p.Length))];<br />
    Marshal.Copy(pktdata, packet_data, 0, p.Length);<br />
    return 1;<br />
  ILO_00cc:<br />
    if (i != -1)<br />
    {<br />
      goto ILO_00d2;<br />
    }<br />
    return -1;<br />
  ILO_00d2:<br />
    if (i != -2)<br />
    {<br />
      goto ILO_00da;<br />
    }<br />
    return -2;<br />
  ILO_00da:<br />
    return -3;<br />
  }<br />
  public virtual dotnetWinpCap.PCAP_NEXT_EX_STATE ReadNextInternal(out PacketHeader p, out System.Byte[] packet_data)<br />
  {<br />
    int i;<br />
    return ReadNextInternal(out p, out packet_data, out i, out i);<br />
  }<br />
  public virtual bool SendPacket(System.Byte[] packet_data)<br />
  {<br />
    int i;<br />
    i = dotnetWinpCap.pcap_sendpacket(pcap_t, packet_data, packet_data.Length);<br />
    if (i != 0)<br />
    {<br />
      goto ILO_0015;<br />
    }<br />
    return 1;<br />
  ILO_0015:<br />
    return 0;<br />
  }<br />
  private void MonitorDump()<br />
  {<br />
    if (dotnetWinpCap.pcap_live_dump_ended(pcap_t, 1) == 0)<br />
    {<br />
      goto ILO_0022;<br />
    }<br />
    if (OnDumpEnded == null)<br />
    {<br />
      goto ILO_0022;<br />
    }<br />
    OnDumpEnded.Invoke(this);<br />
  ILO_0022:<br />
    return;<br />
  }<br />
  private void DumpPacket(object sender, int header, int data)<br />
  {<br />
    if ((dumper != IntPtr.Zero) == 0)<br />
    {<br />
      goto ILO_001f;<br />
    }<br />
    dotnetWinpCap.pcap_dump(dumper, header, data);<br />
  ILO_001f:<br />
    return;<br />
  }<br />
  public virtual void StopDump()<br />
  {<br />
    dotnetWinpCap dotnetWinpCap;<br />
    dotnetWinpCap = this;<br />
    dotnetWinpCap.OnReceivePacketInternal = (Delegate.Remove(dotnetWinpCap.OnReceivePacketInternal, new dotnetWinpCap.ReceivePacketInternal(this.DumpPacket)) as dotnetWinpCap.ReceivePacketInternal);<br />
    if ((dumper != IntPtr.Zero) == 0)<br />
    {<br />
      goto ILO_004a;<br />
    }<br />
    dotnetWinpCap.pcap_dump_close(dumper);<br />
    dumper = IntPtr.Zero;<br />
  ILO_004a:<br />
    return;<br />
  }<br />
  public virtual bool StartDump(string filename)<br />
  {<br />
    bool bl;<br />
    dotnetWinpCap dotnetWinpCap;<br />
    if ((pcap_t != IntPtr.Zero) == 0)<br />
    {<br />
      goto ILO_004f;<br />
    }<br />
    try<br />
    {<br />
      dumper = dotnetWinpCap.pcap_dump_open(pcap_t, filename);<br />
      goto ILO_002b;<br />
    }<br />
    catch (object obj)<br />
    {<br />
      obj;<br />
      bl = 0;<br />
      goto ILO_0051;<br />
    }<br />
  ILO_002b:<br />
    dotnetWinpCap = this;<br />
    dotnetWinpCap.OnReceivePacketInternal = (Delegate.Combine(dotnetWinpCap.OnReceivePacketInternal, new dotnetWinpCap.ReceivePacketInternal(this.DumpPacket)) as dotnetWinpCap.ReceivePacketInternal);<br />
    return 1;<br />
  ILO_004f:<br />
    return 0;<br />
  ILO_0051:<br />
    return bl;<br />
  }<br />
  private bool StartLiveDump(string filename, int maxbytes, int maxpackets)<br />
  {<br />
    fname = filename;<br />
    maxb = maxbytes;<br />
    maxp = maxpackets;<br />
    if (dotnetWinpCap.pcap_live_dump(pcap_t, fname, maxb, maxp) != 0)<br />
    {<br />
      goto ILO_0048;<br />
    }<br />
    new Thread(new ThreadStart(this.MonitorDump));<br />
    return 1;<br />
  ILO_0048:<br />
    return 0;<br />
  }<br />
  public virtual bool SetMinToCopy(int size)<br />
  {<br />
    if (dotnetWinpCap.pcap_setmintocopy(pcap_t, size) != 0)<br />
    {<br />
      goto ILO_0010;<br />
    }<br />
    return 1;<br />
  ILO_0010:<br />
    return 0;<br />
  }<br />
  private void ReadNextLoop()<br />
  {<br />
    PacketHeader packetHeader;<br />
    System.ByteSystem.Byte[] arr;<br />
    int i;<br />
    int i1;<br />
    dotnetWinpCap.PCAP_NEXT_EX_STATE pCAP_NEXT_EX_STATE;<br />
  ILO_0000:<br />
    packetHeader = null;<br />
    arr = null;<br />
    pCAP_NEXT_EX_STATE = ReadNextInternal(out packetHeader, out arr, out i, out i1);<br />
    if (pCAP_NEXT_EX_STATE != 1)<br />
    {<br />
      goto ILO_0000;<br />
    }<br />
    if (OnReceivePacket == null)<br />
    {<br />
      goto ILO_002f;<br />
    }<br />
    OnReceivePacket.Invoke(this, packetHeader, arr);<br />
  ILO_002f:<br />
    if (OnReceivePacketInternal == null)<br />
    {<br />
      goto ILO_0000;<br />
    }<br />
    OnReceivePacketInternal.Invoke(this, i, i1);<br />
    goto ILO_0000;<br />
  }<br />
  public virtual bool SetKernelBuffer(int bytes)<br />
  {<br />
    if (dotnetWinpCap.pcap_setbuff(pcap_t, bytes) == 0)<br />
    {<br />
      goto ILO_0010;<br />
    }<br />
    return 0;<br />
  ILO_0010:<br />
    return 1;<br />
  }<br />
  public virtual void StartListen()<br />
  {<br />
    if (ListenThread == null)<br />
    {<br />
      goto ILO_0013;<br />
    }<br />
    ListenThread.Abort();<br />
  ILO_0013:<br />
    ListenThread = new Thread(new ThreadStart(this.ReadNextLoop));<br />
    ListenThread.Start();<br />
    m_islistening = 1;<br />
    return;<br />
  }<br />
  public virtual void StopListen()<br />
  {<br />
    if (ListenThread == null)<br />
    {<br />
      goto ILO_0027;<br />
    }<br />
    if (ListenThread.IsAlive == 0)<br />
    {<br />
      goto ILO_0020;<br />
    }<br />
    ListenThread.Abort();<br />
  ILO_0020:<br />
    ListenThread = null;<br />
  ILO_0027:<br />
    m_islistening = 0;<br />
    return;<br />
  }<br />
  public virtual void Close()<br />
  {<br />
    StopDump();<br />
    if (IsListening == 0)<br />
    {<br />
      goto ILO_0014;<br />
    }<br />
    StopListen();<br />
  ILO_0014:<br />
    m_isopen = 0;<br />
    m_attachedDevice = null;<br />
    if ((pcap_t != IntPtr.Zero) == 0)<br />
    {<br />
      goto ILO_004a;<br />
    }<br />
    dotnetWinpCap.pcap_close(pcap_t);<br />
    pcap_t = IntPtr.Zero;<br />
  ILO_004a:<br />
    return;<br />
  }<br />
  private void Dispose()<br />
  {<br />
    Dispose(1);<br />
    GC.SuppressFinalize(this);<br />
    return;<br />
  }<br />
  private void Dispose(bool disposing)<br />
  {<br />
    if (disposed != 0)<br />
    {<br />
      goto ILO_005a;<br />
    }<br />
    if (disposing == 0)<br />
    {<br />
      goto ILO_0032;<br />
    }<br />
    if (ListenThread == null)<br />
    {<br />
      goto ILO_0032;<br />
    }<br />
    if (ListenThread.IsAlive == 0)<br />
    {<br />
      goto ILO_002b;<br />
    }<br />
    ListenThread.Abort();<br />
  ILO_002b:<br />
    ListenThread = null;<br />
  ILO_0032:<br />
    if ((pcap_t != IntPtr.Zero) == 0)<br />
    {<br />
      goto ILO_005a;<br />
    }<br />
    dotnetWinpCap.pcap_close(pcap_t);<br />
    pcap_t = IntPtr.Zero;<br />
  ILO_005a:<br />
    disposed = 1;<br />
    return;<br />
  }<br />
  protected override void Finalize()<br />
  {<br />
    try<br />
    {<br />
      Dispose(0);<br />
      goto ILO_0010;<br />
    }<br />
    finally<br />
    {<br />
      base.Finalize();<br />
    }<br />
  ILO_0010:<br />
    return;<br />
  }<br />
}<br />
public class PacketHeader<br />
{<br />
  public dotnetWinpCap.timeval ts;<br />
  private int caplen;<br />
  private int len;<br />
<br />
  public PacketHeader(dotnetWinpCap.timeval ts, int caplen, int len)<br />
  {<br />
    caplen = 0;<br />
    len = 0;<br />
    ts = ts;<br />
    caplen = caplen;<br />
    len = len;<br />
    return;<br />
  }<br />
<br />
  public PacketHeader()<br />
  {<br />
    caplen = 0;<br />
    len = 0;<br />
    return;<br />
  }<br />
  public virtual int Caplength<br />
  {<br />
    get<br />
    {<br />
      return caplen;<br />
    }<br />
    set<br />
    {<br />
      caplen = value;<br />
      return;<br />
    }<br />
  }<br />
  public virtual int Length<br />
  {<br />
    get<br />
    {<br />
      return len;<br />
    }<br />
    set<br />
    {<br />
      len = value;<br />
      return;<br />
    }<br />
  }<br />
  public virtual DateTime TimeStamp<br />
  {<br />
    get<br />
    {<br />
      DateTime dateTime;<br />
      DateTime dateTime1;<br />
      dateTime1 = new DateTime(1970, 1, 1);<br />
      dateTime = dateTime1.AddSeconds(((System.Double)(ts.tv_sec)));<br />
      dateTime.AddMilliseconds(((System.Double)(ts.tv_usec)));<br />
      return dateTime;<br />
    }<br />
  }<br />
}<br />

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 
GeneralHeader informaton Pin
skipi836-Jun-05 14:18
skipi836-Jun-05 14:18 
GeneralRe: Header informaton Pin
orelero29-Dec-05 5:40
orelero29-Dec-05 5:40 
GeneralUsing Winpcap in MS VC++.NET Pin
Member 192319229-Apr-05 4:49
Member 192319229-Apr-05 4:49 
Generaltranslate to vb.net Pin
mvmelle26-Apr-05 23:43
mvmelle26-Apr-05 23:43 
GeneralRe: translate to vb.net Pin
hollowlife198711-Apr-06 13:50
hollowlife198711-Apr-06 13:50 
GeneralAssembling Packets Pin
RickLeinecker14-Mar-05 14:42
RickLeinecker14-Mar-05 14:42 
QuestionMinor logic move? Pin
David Vallner5-Mar-05 16:30
sussDavid Vallner5-Mar-05 16:30 

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.