Cross-Compile libpcap Source Code

02 Mar

Hi folks,
In this post I’m going to explain how to cross compile a libpcap library and use it on our target system. You can apply it to any other library as well with some minor modifications.
Let me first give a brief explanation on libpcap. It’s a library used for sending raw packets over the ethernet interface. In fact the internet protocol stack of the operating system wraps the packet with the protocol headers automatically. We can overcome this using libpcap library thus generate our own packets with custom headers and data and transmit and receive them through the network adapter.

To begin with, download the source code for libpcap from website. In my case the version is libpcap-1.2.0
Extract the zipped folder and go into the directory.

$ cd libpcap-1.2.0

Only two steps are left to complete the compilation.

$ CC=arm-linux-gcc ac_cv_linux_vers=2 ./configure --host=arm-linux --with-pcap=linux
$ make

Voila! If you don’t encounter any error (I do hope u don’t) u’ll see the cross-compiled output files in the same directory under the following names:

Now that we have our cross compiled libpcap let’s try to compile a piece of code linked to libpcap.

Create a file named PCAP.c which has a code that makes use of libpcap functions.
For instance:

#include <stdio.h>
#include <stdbool.h>
#include <pcap.h>
#include <arpa/inet.h>

char *dev; /* Network Device(s) driver */
char errbuf[PCAP_ERRBUF_SIZE];
struct bpf_program fp; /* The compiled filter */
struct bpf_program fcode; // pcap_compile
u_int netmask = 0xffffff;
struct pcap_pkthdr *header;
const u_char *pkt_data;
int receiveResult;
pcap_t *descr; /* Session(s) description */
void *contents = NULL; /* Data to inject */
int i = 0;
int flag = 1;

int InitializePCAP(void)
	if (getuid())
		printf("Error! Must be root ... exiting\n");
		return (1);
	dev = pcap_lookupdev(errbuf);
	if (dev == NULL)
		fprintf(stderr, "Couldn't find default device: %s\n", errbuf);
	printf("Device: %s\n", dev);
	/* Set the pcap description */
	descr = pcap_open_live(dev, BUFSIZ, 1, 3000, errbuf);
	if (descr == NULL)
		printf("pcap_open_live(): %s\n", errbuf);
		return (1);

int main (void)
	int result = InitializePCAP();
	return result;

Then compile your sample code by explicitly linking to libpcap library.

$ arm-linux-gcc -static PCAP.c -o emreboy -lpcap -L/home/emreboy/Desktop/libpcap-1.2.0

I chose compiling it statically in order to execute it on my target system right away.

We can install the cross compiled library files to our toolchain – which is ELDK in my case- in order not to cope with linking the library location explicitly since the compiler will already know where it is located.

$ cd libpcap-1.2.0
$ cp* /home/emreboy/ELDK/arm/usr/lib/
$ cp libpcap.a* /home/emreboy/ELDK/arm/usr/lib/
$ cp *.h /home/emreboy/ELDK/arm/usr/include

Now try compiling the same code dynamically with the following command:

$ arm-linux-gcc PCAP.c -o emreboy -lpcap

If you put the shared libraries we’ve just compiled to the the right directories in your target system i.e. to /usr/lib /lib and /usr/include
your executable file is supposed to work perfectly.

Leave a comment

Posted by on March 2, 2013 in Linux


Tags: , , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: