Looking for some thoughts on trouble-shooting a problem. First though, I'm a
bit of a newbie to Winpcap.
For background, I have a C program that is failing with a
PacketReceivePacket error under Cygwin on Windows 7 x64. To better
illustrate the problem, I built a debug / test harness around one of the
Winpcap code samples-- basic_dump_ex.c (full modified version attached).
I modified the last bit of code to loop and re-run pcap_next_ex() after an
error:
> while((res = pcap_next_ex( adhandle, &header, &pkt_data)) >= 0){
>
> if(res == 0)
> /* Timeout elapsed */
> continue;
>
> /* convert the timestamp to readable format */
> local_tv_sec = header->ts.tv_sec;
> ltime=localtime(&local_tv_sec);
> strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);
>
> printf("%s,%.6d len:%d\n", timestr, header->ts.tv_usec, header->len);
> }
>
> if(res == -1){
> printf("Error reading the packets: %s\n", pcap_geterr(adhandle));
> /* Try the next packet one more time */
> if((res = pcap_next_ex( adhandle, &header, &pkt_data)) == -1)
> printf("Second error reading the packets: %s\n", pcap_geterr(adhandle));
> return -1;
> }
But res never again returns a non-negative number as far as I can tell (even
if I loop endlessly, continuously trying to get another packet.
So that's my question, I guess-- is any error recovery possible after
receiving a PacketReceivePacket error? Any advice on what
a PacketReceivePacket error means-- bad NIC, bad router, cosmic rays? If I
kill the program and re-start immediately, everything runs again for
somewhere between 2 and 36 hours or so.
Any thoughts would be appreciated!
Best,
--> C
#ifdef _MSC_VER
/*
* we do not want the warnings about the old deprecated and unsecure CRT functions
* since these examples can be compiled under *nix as well
*/
#define _CRT_SECURE_NO_WARNINGS
#endif
#include "pcap.h"
int main()
{
pcap_if_t *alldevs;
pcap_if_t *d;
int inum;
int i=0;
pcap_t *adhandle;
int res;
char errbuf[PCAP_ERRBUF_SIZE];
struct tm *ltime;
char timestr[16];
struct pcap_pkthdr *header;
const u_char *pkt_data;
time_t local_tv_sec;
/* Retrieve the device list */
if(pcap_findalldevs(&alldevs, errbuf) == -1)
{
fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
return -1;
}
/* Print the list */
for(d=alldevs; d; d=d->next)
{
printf("%d. %s", ++i, d->name);
if (d->description)
printf(" (%s)\n", d->description);
else
printf(" (No description available)\n");
}
if(i==0)
{
printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
return -1;
}
/* printf("Enter the interface number (1-%d):",i);
scanf("%d", &inum); */
inum = 2;
if(inum < 1 || inum > i)
{
printf("\nInterface number out of range.\n");
/* Free the device list */
pcap_freealldevs(alldevs);
return -1;
}
/* Jump to the selected adapter */
for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
/* Open the adapter */
if ((adhandle= pcap_open_live(d->name, // name of the device
65536, // portion of the packet to capture.
// 65536 grants that the whole packet will be captured on all the MACs.
1, // promiscuous mode (nonzero means promiscuous)
1000, // read timeout
errbuf // error buffer
)) == NULL)
{
fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", d->name);
/* Free the device list */
pcap_freealldevs(alldevs);
return -1;
}
printf("\nlistening on %s...\n", d->description);
/* At this point, we don't need any more the device list. Free it */
pcap_freealldevs(alldevs);
/* CJH endless loop version Retrieve the packets
while((res = pcap_next_ex( adhandle, &header, &pkt_data)) >= 0){
if(res == 0)
/* Timeout elapsed
continue;
if(res > 0) {
/* convert the timestamp to readable format
local_tv_sec = header->ts.tv_sec;
ltime=localtime(&local_tv_sec);
strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);
printf("%s,%.6d len:%d\n", timestr, header->ts.tv_usec, header->len);
}
if(res == -1){
printf("Error reading the packets: %s\n", pcap_geterr(adhandle));
/* return -1;
}
}
*/
/* Retrieve the packets */
while((res = pcap_next_ex( adhandle, &header, &pkt_data)) >= 0){
if(res == 0)
/* Timeout elapsed */
continue;
/* convert the timestamp to readable format */
local_tv_sec = header->ts.tv_sec;
ltime=localtime(&local_tv_sec);
strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);
printf("%s,%.6d len:%d\n", timestr, header->ts.tv_usec, header->len);
}
if(res == -1){
printf("Error reading the packets: %s\n", pcap_geterr(adhandle));
/* Try the next packet one more time */
if((res = pcap_next_ex( adhandle, &header, &pkt_data)) == -1) printf("Second error reading the packets: %s\n", pcap_geterr(adhandle));
return -1;
}
pcap_close(adhandle);
return 0;
}
_______________________________________________
Winpcap-users mailing list
[email protected]
https://www.winpcap.org/mailman/listinfo/winpcap-users