473,403 Members | 2,323 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

WinPCap 3.1 Wrapper: pcap_dispatch NullReferenceException

Hi All!

I am developing WinPCap 3.1 wrapper in C# and I have a problem with
capturing packets with callback function.

Here is some code:

Declarations:

//C definition:
//typedef void(* pcap_handler)(u_char *user,
// const struct pcap_pkthdr *pkt_header,
// const u_char *pkt_data)
public delegate void DispatchDelegate (IntPtr user,
out IntPtr header,
IntPtr data);

//int pcap_dispatch ( pcap_t * p,
// int cnt,
// pcap_handler callback,
// u_char * user
// )
public static extern int pcap_dispatch(IntPtr handle,
UInt32 cnt,
[MarshalAs(UnmanagedType.FunctionPtr)]DispatchDelegate callback,

IntPtr user);

Code:
{
// Opening interface, etc...
m_Callback = new Wrapper.DispatchDelegate(ProcessPacket);
Wrapper.pcap_dispatch(handler, 100, m_Callback, IntPtr.Zero);
}
// Callback function:
public static void ProcessPacket (IntPtr user, IntPtr headerPointer,
IntPtr data)
{
pcap_pkthdr header = (pcap_pkthdr) Marshal.PtrToStructure
(headerPointer, typeof(pcap_pkthdr));
DateTime date = new DateTime(1970, 1, 1, 0, 0, 0, 0);
date = date.AddSeconds(header.ts.tv_sec);
date = date.AddMilliseconds(header.ts.tv_usec / 1000);
Console.WriteLine("Time: {0}:{1}", header.ts.tv_sec,
header.ts.tv_usec);
Console.WriteLine("DateTime: " + date.ToString());
return;
}

And that is what's happening:
On the first call of the callback everything is OK.
On the second one, input data is not correct.
After the second call NullReferenceException appears and pcap_dispatch
ends.

BTW, capturing without callback works fine, so opening interfaces code
is OK.

Any ideas?
Regards,
Leszek

Sep 5 '05 #1
0 1669

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Shaun Baker | last post by:
Hi, I'm a bit of a novice at this but i'm running into a few problems trying to wrap winpcap in C#. I know I can use a special socket flag to pick up all packets across an interface but this...
1
by: Samuel R. Neff | last post by:
We have a class that is a basic wrapper around a native 3rd party C DLL. We created an NUnit test against the wrapper and it passes fine the first time. However, when we run the tests again, it...
2
by: Nuno Magalhaes | last post by:
Hello all, Below I have a function that captures a packet and sends that packet to a Receive function (wether it is sent or received). I'm asking what functions should I use (what imports...
3
by: Nuno Magalhaes | last post by:
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...
3
by: Brian Pittman | last post by:
Hi, I was wondering how to go about creating a wrapper function for uploading files to the server? I have made an attempt on my own without success. I don't get any exceptions from the...
0
by: Leszek | last post by:
Hi All! I am developing WinPCap 3.1 wrapper in C# and I have a problem with capturing packets with callback function. Here is some code: Declarations: //C definition:
1
by: Bruce | last post by:
I am using VC .NET 2003. I created a simple unmanaged class in C++. The class is contained in a library. I then created a simple managed class wrapper that calls a function in the unmanaged...
5
by: exman | last post by:
Hi ! i have a problem related to winpcap, i'm curretly making a sniffer aplication for my studies. I want to accomplish this http://www.winpcap.org/docs/man/html/group__wpcap__tut3.html but in...
1
by: Ken Fine | last post by:
I have been investigating programmatically downloading FLV content from various sites ("video scraping"??) Many interactive GUI tools do this, such as the Orbit downloader. At the heart of them...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.