Winpcap / Libpcap

From Carboogle

Hello,

This is a quick "how to" to setup Libpcap under Cygwin. For Windows, it is just faster downloading the WinPcap Developer's Packs and install the files in Cygwin directories.

Contents

For Cygwin

Setup Winpcap

1. Download and unzip the pack. We will use for this example WpdPack_4_0_1.zip.
2. Copy libraries like this:

  • WpdPack\Lib\libpacket.a to cygwin\lib\
  • WpdPack\Lib\libwpcap.a to cygwin\lib\

3. Create a folder cygwin\usr\include\pcap\
4. Copy all headers from WpdPack\Include to cygwin\usr\include\winpcap\
5. Be sure you have installed Winpcap libraries and that they are in your path by typing:

  • which Packet.dll
  • which wpcap.dll

For me they are in /cygdrive/c/WINDOWS/system32/

Building example using Cygwin

basic_dump

Open a cygwin prompt to WpdPack\Examples-pcap\basic_dump\ and execute:

CFLAGS="-g -Wall -mno-cygwin -I /usr/include/pcap"
LIBS="-lwpcap"
PROG="basic_dump"
gcc $CFLAGS -c $PROG.c
gcc $CFLAGS -o $PROG.exe $PROG.o $LIBS
./$PROG.exe

basic_dump_ex

CFLAGS="-g -Wall -mno-cygwin -I /usr/include/pcap"
LIBS="-lwpcap"
PROG="basic_dump_ex"
gcc $CFLAGS -c $PROG.c
gcc $CFLAGS -o $PROG.exe $PROG.o $LIBS
./$PROG.exe

iflist

CFLAGS="-g -Wall -mno-cygwin -I /usr/include/pcap"
LIBS="-lwpcap"
PROG="iflist"
gcc $CFLAGS -c $PROG.c
gcc $CFLAGS -o $PROG.exe $PROG.o $LIBS
./$PROG.exe

pcap_filter (and others)

I think you can catch the pattern ;) Only replace PROG=... by the program name and it should compile.

UDPdump

CFLAGS="-g -Wall -mno-cygwin -I /usr/include/pcap"
LIBS="-lwpcap -lwsock32"
PROG="UDPdump"
gcc $CFLAGS -c $PROG.c
gcc $CFLAGS -o $PROG.exe $PROG.o $LIBS
./$PROG.exe

You can test by doing a Time Synchronization with pool.ntp.org for example.

For MinGW

Requirements

Basic setup:

  1. Unpack Winpcap sources into D:\data\download\winpcap\
  2. Unpack Airpcap dev. pack into D:\data\download\Airpcap_Devpack\

Compiling Winpcap

If you are using the same MinGW environment as mine, it is possible that you need to apply the following patch before launching the compilation. I have reported a bug in september 2007 relative to this compilation issue on winpcap bugs mailing list (Patch - Problems compiling Winpcap 4.0.1 with MinGW)

  1. --- /d/data/download/gai_strerror.c Sun Jan 22 15:14:18 2006
  2. +++ ../libpcap/Win32/Src/gai_strerror.c Tue Sep 18 21:29:11 2007
  3. @@ -67,6 +67,7 @@
  4. #define EAI_MAX (sizeof(ai_errlist)/sizeof(ai_errlist[0]))
  5. #endif
  6.  
  7. +#ifndef gai_strerror
  8. char *
  9. WSAAPI gai_strerrorA(int ecode)
  10. {
  11. @@ -74,4 +75,4 @@
  12. return ai_errlist[ecode];
  13. return "Unknown error";
  14. }
  15. -
  16. +#endif

You can add it manually by editing the file .\winpcap\wpcap\libpcap\Win32\Src\gai_strerror.c and adding the above defines around the function gai_strerrorA

Once the patch applied, you are ready to compile, as written in the readme. Open a console from D:\data\download\winpcap\ and execute the commands:

cd packetNtx/Dll/Project/
make
cd ../../../wpcap/PRJ/
make

Here you should have an error:

Can't open .lib file: ../lib/libwpcap.a
collect2: ld returned 1 exit status
make: *** [main] Error 1

It it just because you need to manually create the lib folder D:\data\download\winpcap\wpcap\lib So after creating this folder, just retype

make

And you should have the following files (for the total):

  • D:\data\download\winpcap\packetNtx\Dll\Project\libpacket.a
  • D:\data\download\winpcap\wpcap\lib\libwpcap.a
  • D:\data\download\winpcap\packetNtx\Dll\Project\Packet.dll
  • D:\data\download\winpcap\wpcap\libpcap\rpcapd\win32-pthreads\pthreadVC.dll
  • D:\data\download\winpcap\wpcap\PRJ\wpcap.dll

Installing Winpcap in MinGW

The procedure is quite the same as for Cygwin.

1. Copy all the *.a files you built into /local/lib
2. Copy the *.dll files into /local/bin
3. Copy all headers from WpdPack\Include to /local/include/pcap
4. Optional: To facilitate further compilations, i use to copy /local/lib/libwpcap.a to /local/lib/libpcap.a because a lot of Linux programs rely on the original libpcap library which create the second library and not the first one.

Building example using MinGW

basic_dump_ex

In a bash console execute with MinGW:

SRC="basic_dump_ex"
CFLAGS="-I /local/include/pcap"
LDFLAGS="-L /local/lib -lpcap"
gcc $CFLAGS -o $SRC.exe $SRC.c $LDFLAGS

iflist

SRC="iflist"
CFLAGS="-I /local/include/pcap"
LDFLAGS="-L /local/lib -lpcap"
gcc $CFLAGS -o $SRC.exe $SRC.c $LDFLAGS

UDPdump

SRC="udpdump"
CFLAGS="-I /local/include/pcap"
LDFLAGS="-L /local/lib -lpcap -lws2_32"
gcc $CFLAGS -o $SRC.exe $SRC.c $LDFLAGS

Mycila.com

Mycila projects

Other projects

ports

articles

lessons