Creating an application that uses wpcap.dll
To create an application that uses wpcap.dll with Microsoft Visual C++,
follow these
steps:
- Include the file pcap.h at the beginning of every source file that
uses the functions exported by library.
- If your program uses Win32 specific functions of WinPcap, remember to include WPCAP
among the preprocessor definitions.
- If your program uses the remote capture capabilities of WinPcap, add
HAVE_REMOTE among the preprocessor definitions. Do not include
remote-ext.h directly in your source files.
- Set the options of the linker to include the wpcap.lib library
file specific for your target (x86 or x64). wpcap.lib for x86 can be
found in the \lib folder of the WinPcap developer's
pack, wpcap.lib for x64 can
be found in the \lib\x64 folder.
- Set the options of the linker to include the winsock library file
ws2_32.lib. This file is distributed with the C compiler
and contains the socket functions for Windows. It is needed by some
functions used by the samples in the tutorial.
How to properly set Microsoft Visual Studio
Visual Studio 6
- To add a preprocessor definition, you must select Settings from the Project menu, then select C/C++
from the tab control, and under the category General, you must add
the definition under the Preprocessor Definitions text box.
- To add a new library to the project with Microsoft Visual C++, you must
select Settings from the Project menu, then select Link
from the tab control, and then add the name of the new library in the Object/library
modules editbox.
- To add a new path where Microsoft Visual C++ will look for the libraries,
you must select Options from the Tools menu, then
Directories
from the tab control, Library files from the Show directories
for combobox, and the add the path in the Directories box.
- To add a new path where Microsoft Visual C++ will look for include files, you must select Options from the Tools menu, then
Directories
from the tab control, Include files from the Show directories
for combobox, and the add the path in the Directories box.
Visual Studio 2005 (needed to compile x64 applications)
- To add a preprocessor definition, you must select Properties from the Project menu, then select C/C++
from the list control on the left, and under the category Preprocessor, you must add
the definition under the Preprocessor Definitions text box.
- To add a new library to the project, you must
select Properties from the Project menu, then select Linker
from the list control on the left, and under the category Input add the name of the new library in the
Additional Dependencies text box.
- To add a new path where Microsoft Visual
Studio will look for the libraries,
you must select Options from the Tools menu, then
Project and Solutions from the list control on the left, VC++
Directories, then choose Library Files in the Show directories
for combobox, and the add the path in the box below.
- To add a new path where Microsoft Visual
Studio will look for the include files,
you must select Options from the Tools menu, then
Project and Solutions from the list control on the left, VC++
Directories, then choose Include Files in the Show directories
for combobox, and the add the path in the box below.
Sample programs
A couple of sample programs are provided to show the usage of the WinPcap API. The
source of the examples, along with all the files needed to compile and run them, can be found in the Developer's
Pack. For didactic purpose we provide here a browsable version of the
code: it is possible to click on the variables and functions to jump the
documentation of each of them. For a more complete set of samples, try WinPcap
Tutorial Section.
More...
Creating an application that uses wpcap.dll
To create an application that uses wpcap.dll with Microsoft Visual C++,
follow these
steps:
- Include the file pcap.h at the beginning of every source file that
uses the functions exported by library.
- If your program uses Win32 specific functions of WinPcap, remember to include WPCAP
among the preprocessor definitions.
- If your program uses the remote capture capabilities of WinPcap, add
HAVE_REMOTE among the preprocessor definitions. Do not include
remote-ext.h directly in your source files.
- Set the options of the linker to include the wpcap.lib library
file specific for your target (x86 or x64). wpcap.lib for x86 can be
found in the \lib folder of the WinPcap developer's
pack, wpcap.lib for x64 can
be found in the \lib\x64 folder.
- Set the options of the linker to include the winsock library file
ws2_32.lib. This file is distributed with the C compiler
and contains the socket functions for Windows. It is needed by some
functions used by the samples in the tutorial.
How to properly set Microsoft Visual Studio
Visual Studio 6
- To add a preprocessor definition, you must select Settings from the Project menu, then select C/C++
from the tab control, and under the category General, you must add
the definition under the Preprocessor Definitions text box.
- To add a new library to the project with Microsoft Visual C++, you must
select Settings from the Project menu, then select Link
from the tab control, and then add the name of the new library in the Object/library
modules editbox.
- To add a new path where Microsoft Visual C++ will look for the libraries,
you must select Options from the Tools menu, then
Directories
from the tab control, Library files from the Show directories
for combobox, and the add the path in the Directories box.
- To add a new path where Microsoft Visual C++ will look for include files, you must select Options from the Tools menu, then
Directories
from the tab control, Include files from the Show directories
for combobox, and the add the path in the Directories box.
Visual Studio 2005 (needed to compile x64 applications)
- To add a preprocessor definition, you must select Properties from the Project menu, then select C/C++
from the list control on the left, and under the category Preprocessor, you must add
the definition under the Preprocessor Definitions text box.
- To add a new library to the project, you must
select Properties from the Project menu, then select Linker
from the list control on the left, and under the category Input add the name of the new library in the
Additional Dependencies text box.
- To add a new path where Microsoft Visual
Studio will look for the libraries,
you must select Options from the Tools menu, then
Project and Solutions from the list control on the left, VC++
Directories, then choose Library Files in the Show directories
for combobox, and the add the path in the box below.
- To add a new path where Microsoft Visual
Studio will look for the include files,
you must select Options from the Tools menu, then
Project and Solutions from the list control on the left, VC++
Directories, then choose Include Files in the Show directories
for combobox, and the add the path in the box below.
Sample programs
A couple of sample programs are provided to show the usage of the WinPcap API. The
source of the examples, along with all the files needed to compile and run them, can be found in the Developer's
Pack. For didactic purpose we provide here a browsable version of the
code: it is possible to click on the variables and functions to jump the
documentation of each of them. For a more complete set of samples, try WinPcap
Tutorial Section.
Packet Dump
This program reads packets from a file or a network adapter, depending on
a command line switch. If a source is not provided, the program shows a list of
available adapters, one of which can be selected. Once the
capture is started, the program prints the timestamp, the length and the raw
contents of the packets. Once compiled, it will run on all the Win32 platforms. It
can be compiled to run on Unix as well (the makefile is provided).
Packet Filter
This is a more complete example of libpcap usage. It shows, among other
things, how to create and set filters and how to save a capture to disk. It can
be compiled under Win32 or Unix (projects and makefiles are provided).
Pcap_filter (pf.exe) is a general-purpose packet filtering application: its
input parameters are a source of packets (it can be a physical interface or a
file), a filter and an output file. It takes packets from the source until
CTRL+C is pressed or the whole file is processed, applies the filter to the
incoming packets and saves them to the output file if they satisfy the filter.
Pcap_filter can be used to dump network data according to a particular filter,
but also to extract a set of packets from a previously saved file. The format of
both input and output files is the format used by libpcap, i.e. same of WinDump, tcpdump
and many other network tools.
#include <stdlib.h>
#include <stdio.h>
#include <pcap.h>
#define MAX_PRINT 80
#define MAX_LINE 16
void usage();
void main(int argc, char **argv)
{
char *source=NULL;
char *ofilename=NULL;
char *filter=NULL;
int i;
struct bpf_program fcode;
int res;
const u_char *pkt_data;
if (argc == 1)
{
usage();
return;
}
for(i=1;i < argc; i+= 2)
{
switch (argv[i] [1])
{
case 's':
{
source=argv[i+1];
};
break;
case 'o':
{
ofilename=argv[i+1];
};
break;
case 'f':
{
filter=argv[i+1];
};
break;
}
}
if (source != NULL)
{
1514 ,
20 ,
NULL ,
errbuf)
) == NULL)
{
fprintf(stderr,"\nUnable to open the adapter.\n");
return;
}
}
else usage();
if (filter != NULL)
{
NetMask=0xffffff;
{
fprintf(stderr,"\nError compiling filter: wrong syntax.\n");
return;
}
{
fprintf(stderr,"\nError setting the filter\n");
return;
}
}
if (ofilename != NULL)
{
if (dumpfile == NULL)
{
fprintf(stderr,"\nError opening output file\n");
return;
}
}
else usage();
{
if(res == 0)
continue;
pcap_dump((
unsigned char *) dumpfile, header, pkt_data);
}
}
void usage()
{
printf("\npf - Generic Packet Filter.\n");
printf("\nUsage:\npf -s source -o output_file_name [-f filter_string]\n\n");
exit(0);
}