Pelles C forum

C language => Tips & tricks => Topic started by: ghost on November 25, 2008, 01:21:08 AM

Title: Using winpcap development with PellesC
Post by: ghost on November 25, 2008, 01:21:08 AM
I searched the forum but there seems no postings about using winpcap http://www.winpcap.org/ (http://www.winpcap.org/) under PellesC so this is the way I got it to work.

Download and extract the developer pack from the wincap website.
Start PellesC and select a new project: Application wizzard - dialog based example.

Include the following header directives:

Code: [Select]
#include <pcap.h>
#include <commdlg.h>


Select project options and add the following Preprocessor symbols:

Code: [Select]
WIN32,WPCAP,__MINGW32__
I added the MINGW32 symbol, because that disables the loading of the IP6_misc.h header file in the winpcap include directory. This file contains redeclarations. This means IP6 will be disabled not sure if this can be fixed easy.

Select the Linker tab and add the following library to the line:

Code: [Select]
wpcap.lib
Select the Folders tab and create an Include folder pointing to the extracted winpcap Include folder.
Do the same for the library folder.

When you compile the program you get an error message on the file bittypes.h from the wincap Include folder. The reason for this is that the int64 type is declared as _int64 and PellesC uses __int64. So change the following line:

Code: [Select]
typedef unsigned _int64 u_int64_t;to
Code: [Select]
typedef unsigned __int64 u_int64_t;
Now you can compile the dialog based program. Following is a quick and dirty function that you can use to test if you can open offline pcap files. Just add it in your program and call it form the IDOK function in MainDlgProc.

I got winpcap to work this way. If I missed some things or made some errors please let me know.

Code: [Select]
//--------------------------------------
// Open and read offline pcap file
//--------------------------------------
int winpcaptest(HWND hwnd)
{
pcap_t *fp;

struct pcap_pkthdr *header;
const u_char *pkt_data;
u_int i=0;
int res=0,iPacketcount =0;


char errbuf[PCAP_ERRBUF_SIZE];
char pcap_str[10000];
char pcap_hex[10];
char wImportFile[MAX_PATH];

        OPENFILENAMEA ofn;

        ZeroMemory(&ofn, sizeof(ofn));
ZeroMemory(wImportFile, sizeof(wImportFile));

//--------------------------------------
// Call open dialog
//--------------------------------------
        ofn.lStructSize = sizeof(ofn);
ofn.lStructSize = sizeof(ofn);
        ofn.hwndOwner = hwnd;
        ofn.lpstrFile = wImportFile;
        ofn.nMaxFile = MAX_PATH;
        ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;

if(GetOpenFileNameA(&ofn))
{
//--------------------------------------
// Open the capture file
//--------------------------------------
if ((fp = pcap_open_offline(ofn.lpstrFile, // name of the device
errbuf // error buffer
)) == NULL)
{
MessageBox(hwnd,"Unable to open the file.","winpcaptest",MB_ICONERROR);
return -1;
}
MessageBox(hwnd,"Cap file opened.","winpcaptest",MB_ICONINFORMATION);

ZeroMemory(pcap_str,sizeof(pcap_str));

//--------------------------------------
// Retrieve the packets from the file
//--------------------------------------
while((res = pcap_next_ex(fp, &header, &pkt_data)) >= 0)
{
/*
sprintf(pcap_str,"%ld:%ld (%ld)\n", header->ts.tv_sec, header->ts.tv_usec, header->len);
for (i=1; (i < header->caplen + 1 ) ; i++)
{
ZeroMemory(pcap_hex,sizeof(pcap_hex));
sprintf(pcap_hex,"%.2x ", pkt_data[i-1]);
strcat(pcap_str,pcap_hex);
if ( (i % LINE_LEN) == 0) strcat(pcap_str,"\n");
}
MessageBoxA(hwnd,pcap_str,"wincaptest",MB_ICONINFORMATION);
*/

iPacketcount++;
}

sprintf(pcap_str,"%d Packets in capture file.",iPacketcount);
MessageBoxA(hwnd,pcap_str,"wincaptest",MB_ICONINFORMATION);

//--------------------------------------
// Close capture file
//--------------------------------------
pcap_close(fp);

MessageBox(hwnd,"Cap file closed.","winpcaptest",MB_ICONINFORMATION);
}

return 1;
}