How to wrap winpcap

N

Nuno Magalhaes

How can I use WinPcap or PacketX.dll to catch any packets that flow on
my machine network adapter?

Is there any sample code on how to do this?

I would like something like this... and without using any additional
..dlls:

-----------------------------------------------
using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;


namespace wpcapnet
{
//for the easiest way, there are only pointers, not structures
public delegate void pcap_handler(IntPtr i, IntPtr hdr, IntPtr stat);


public class pcap
{
//char *pcap_lookupdev(char *);
[DllImport("wpcap.dll")]
static extern IntPtr pcap_lookupdev(ref int errbuf);


//pcap_t *pcap_open_live(char *, int, int, int, char *);
[DllImport("wpcap.dll")]
static extern int pcap_open_live(string device, int snaplen, int
promisc,
int to_ms, StringBuilder errbuf);


//void pcap_close(pcap_t *);
[DllImport("wpcap.dll")]
static extern void pcap_close(int device);


//int pcap_setmode(pcap_t *p, int mode);
[DllImport("wpcap.dll")]
static extern int pcap_setmode(int device, int mode);


[DllImport("wpcap.dll")]
static extern int pcap_setbuff(int device, int buffsize);


//int pcap_loop(pcap_t *, int, pcap_handler, u_char *);
[DllImport("wpcap.dll")]
static extern int pcap_loop(int device, int cnt, pcap_handler
callback,
IntPtr i);


//const u_char* pcap_next(pcap_t *, struct pcap_pkthdr *);
[DllImport("wpcap.dll")]
static extern pcap_stat pcap_next(int device, ref pcap_pkthdr
pkthdr);


//callback function
private static void OnStatistic(IntPtr i, IntPtr hdr, IntPtr stat)
{
Debug.WriteLine(DateTime.Now.ToString());
}


public pcap_handler OnStatisticPtr = new pcap_handler(OnStatistic);


public void Test()
{
StringBuilder errbuf = new StringBuilder(256);
IntPtr answ = pcap_lookupdev(ref buf);
String devicename = Marshal.PtrToStringAuto(answ);
Debug.WriteLine(devicename);


int device = pcap_open_live(devicename,1,1,1000, errbuf);
Debug.WriteLine(device.ToString());
int result = pcap_setmode(device,1);
Debug.WriteLine("setmode:"+result.ToString());


result = pcap_setbuff(device,0);


int answ2 = pcap_loop(device, 5, OnStatisticPtr, answ);


pcap_close(device);
}
}
 
N

Nuno Magalhaes

The www.pinvoke.net site is cool but I can't see any reference to
wpcap. :(
Any other sites like this which has the reference or sample code?

Thanks,
Nuno Magalhaes.
hi

take a look at www.pinvoke.net

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Nuno Magalhaes said:
How can I use WinPcap or PacketX.dll to catch any packets that flow on
my machine network adapter?

Is there any sample code on how to do this?

I would like something like this... and without using any additional
.dlls:

-----------------------------------------------
using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;


namespace wpcapnet
{
//for the easiest way, there are only pointers, not structures
public delegate void pcap_handler(IntPtr i, IntPtr hdr, IntPtr stat);


public class pcap
{
//char *pcap_lookupdev(char *);
[DllImport("wpcap.dll")]
static extern IntPtr pcap_lookupdev(ref int errbuf);


//pcap_t *pcap_open_live(char *, int, int, int, char *);
[DllImport("wpcap.dll")]
static extern int pcap_open_live(string device, int snaplen, int
promisc,
int to_ms, StringBuilder errbuf);


//void pcap_close(pcap_t *);
[DllImport("wpcap.dll")]
static extern void pcap_close(int device);


//int pcap_setmode(pcap_t *p, int mode);
[DllImport("wpcap.dll")]
static extern int pcap_setmode(int device, int mode);


[DllImport("wpcap.dll")]
static extern int pcap_setbuff(int device, int buffsize);


//int pcap_loop(pcap_t *, int, pcap_handler, u_char *);
[DllImport("wpcap.dll")]
static extern int pcap_loop(int device, int cnt, pcap_handler
callback,
IntPtr i);


//const u_char* pcap_next(pcap_t *, struct pcap_pkthdr *);
[DllImport("wpcap.dll")]
static extern pcap_stat pcap_next(int device, ref pcap_pkthdr
pkthdr);


//callback function
private static void OnStatistic(IntPtr i, IntPtr hdr, IntPtr stat)
{
Debug.WriteLine(DateTime.Now.ToString());
}


public pcap_handler OnStatisticPtr = new pcap_handler(OnStatistic);


public void Test()
{
StringBuilder errbuf = new StringBuilder(256);
IntPtr answ = pcap_lookupdev(ref buf);
String devicename = Marshal.PtrToStringAuto(answ);
Debug.WriteLine(devicename);


int device = pcap_open_live(devicename,1,1,1000, errbuf);
Debug.WriteLine(device.ToString());
int result = pcap_setmode(device,1);
Debug.WriteLine("setmode:"+result.ToString());


result = pcap_setbuff(device,0);


int answ2 = pcap_loop(device, 5, OnStatisticPtr, answ);


pcap_close(device);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top