|
-
June 6th, 2011, 03:42 PM
#1
PCap Library
I'm able to do most everything in the PCap library successfully, except install this callback function. The line I bolded in the code below is where my program just freezes at. However, I tried using this same code in a console application, and it worked just fine. What's the deal?
Code:
void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet)
{
return;
}
void RawPacket::SetupFilter(pcap_if_t* Device)
{
char Error[256];
struct bpf_program fp;
char filter_exp[] = "src net 128.0.27.111";
bpf_u_int32 mask;
bpf_u_int32 net;
if (pcap_lookupnet(Device->name, &net, &mask, Error) == -1) {
fprintf(stderr, "Can't get netmask for device %s\n", Device->name);
net = 0;
mask = 0;
}
handle = pcap_open_live(Device->name, BUFSIZ, FALSE, 1000, Error);
if(!handle) {
fprintf(stderr, "Couldn't open device %s: %s\n", Device->name, Error);
return;
}
if (pcap_datalink(handle) != DLT_EN10MB) {
fprintf(stderr, "%s is not an Ethernet\n", Device->name);
return;
}
if (pcap_compile(handle, &fp, filter_exp, 0, net) == -1) {
fprintf(stderr, "Couldn't parse filter %s: %s\n", filter_exp, pcap_geterr(handle));
return;
}
if (pcap_setfilter(handle, &fp) == -1) {
fprintf(stderr, "Couldn't install filter %s: %s\n", filter_exp, pcap_geterr(handle));
return;
}
if (pcap_loop(handle, 10, got_packet, NULL) == -1) {
fprintf(stderr, "Couldn't install callback: %s\n", pcap_geterr(handle));
return;
}
pcap_freecode(&fp);
pcap_close(handle);
filterSetup = true;
}
Stack trace:
ntdll.dll!7d61c846()
[Frames below may be incorrect and/or missing, no symbols loaded for ntdll.dll]
kernel32.dll!7d4d8c9e()
kernel32.dll!7d4d8c0d()
Packet.dll!00d23c3a()
wpcap.dll!10016810()
> UDP Packet Test.exe!RawPacket::SetupFilter(pcap_if * Device) Line 359 + 0x14 bytes C++
-
June 6th, 2011, 04:20 PM
#2
Re: PCap Library
So I just tried sending 10 packets, and it finally returned from that function.
Which confuses me, because I thought what pcap_loop() was supposed to do was install an asynchronous receive callback function, and then return.
Am I doing something wrong?
-
June 7th, 2011, 09:32 AM
#3
Re: PCap Library
After reading this, the operation you stated seems about right as you have specified 10 as the packet count to collect prior to returning.
Code:
pcap_loop(handle, 10, got_packet, NULL)
-
June 7th, 2011, 02:54 PM
#4
Re: PCap Library
 Originally Posted by jeron
After reading this, the operation you stated seems about right as you have specified 10 as the packet count to collect prior to returning.
Code:
pcap_loop(handle, 10, got_packet, NULL)
Yea, I realized this yesterday. I misunderstood what pcap_loop() was supposed to do. Not sure what the point of pcap_next() is. I thought pcap_next() was synchronous receive, and pcap_loop() was asynchronous receive. Turns out they're both synchronous receive.
Well whatever, I've found my solution. Thanks.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|