[Winpcap-users] Not able to capture IPv6 packets.
Bassam A. Al-Khaffaf
bassam at palettemm.com
Mon Jan 15 09:12:24 GMT 2007
Dear all,
I am writing a radius server using winsock2 with win32 C++ on visual C++
2005 express edition on windows xp pro. The application supports both IPv4
and IPv6 and I want to extract the source and destination MAC and IP
addresses from the incoming packets.
However, I have read the winpcap documentation and learened on how to sniff
the MACs and IPs. Unfortunately, my sniffer is able to capture the IPv4
only, while I would like to have both IPv4 and IPv6 are captured, a portion
code snippet is shown below.
I don't know what is the wrong in my code, and I would appreciate if you
helped me to solve this problem.
Regards
Bassam
#include <iostream>
using std::cout;
using std::cin;
using std::cerr;
using std::endl;
#include <winsock2.h>
#include "pcap.h"
int getAdapters(void)
{
pcap_if_t *alldevs;
pcap_if_t *d;
pcap_t *adhandle;
int inum, i=0, 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;
struct bpf_program fcode;
u_int netmask;
/* Retrieve the device list from the local machine */
//if (pcap_findalldevs(&alldevs, errbuf) == -1)
if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) ==
-1)
{
cerr << "Error in pcap_findalldevs_ex: " << errbuf << endl;
exit(1);
}
/* Print the list */
for(d= alldevs; d != NULL; d= d->next)
{
cout << ++i << "." << d->name << " ";
if (d->description)
cout << d->description << endl;
else
cout << "(No description available)" << endl;
}
if (i == 0)
{
cout << "\nNo interfaces found! Make sure WinPcap is installed." <<
endl;
return -1;
}
cout << "\nEnter the interface number: " << endl;
cin >> inum;
if (inum < 1 || inum > i)
{
cout << "\nInterface number out of range." << endl;
/* 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 device */
if ((adhandle= pcap_open(d->name, 65535 , 0, 1000, NULL, errbuf)) ==
NULL)
{
cerr << "\nUnable to open the adapter. " << d->name << " is not
supported by WinPcap" << endl;
/* Free the device list */
pcap_freealldevs(alldevs);
return -1;
}
if (d->addresses != NULL)
/* Retrieve the mask of the first address of the interface */
netmask=((struct sockaddr_in
*)(d->addresses->netmask))->sin_addr.S_un.S_addr;
else
/* If the interface is without an address we suppose to be in a C
class network */
netmask=0xffffff;
//compile the filter
if (pcap_compile(adhandle, &fcode, "udp dst port 1812 or dst port 1813",
1, 0) < 0)
{
fprintf(stderr,"\nUnable to compile the packet filter. Check the
syntax.\n");
/* Free the device list */
pcap_freealldevs(alldevs);
return -1;
}
//set the filter
if (pcap_setfilter(adhandle, &fcode) < 0)
{
cerr << "\nError setting the filter." << endl;
/* Free the device list */
pcap_freealldevs(alldevs);
return -1;
}
cout << "\nlistening on " << d->description << endl;
/* At this point, we don't need any more the device list. Free it */
pcap_freealldevs(alldevs);
/* Retrieve the packets */
while((res = pcap_next_ex(adhandle, &header, &pkt_data)) >= 0)
{
if(res == 0)
continue; /* Timeout elapsed */
if(res == -1)
{
cout << "Error reading the packets: " <<
pcap_geterr(adhandle) << endl;
return -1;
}
/* 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);
cout << timestr << "." << header->ts.tv_usec << "len: " <<
header->len << endl;
cout << "Packet Data: " << endl;
for (i = 0; i < 34; i++)
cout << static_cast<int>(pkt_data[i]) << " ";
cout << endl;
}
return 0;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.winpcap.org/pipermail/winpcap-users/attachments/20070115/fdc01f0b/attachment-0001.htm
More information about the Winpcap-users
mailing list