Reference article:
- Porting libpcap packet capture library to arm platform under Linux
- Linux Network Programming -- detailed explanation of libpcap
- libpcap usage
- Libpcap library programming guide – packet capture
- Methods of network monitoring and sending data packets under linux (i.e. the use of libpcap and libnet class libraries)
1, libpcap Library Download
2, Cross compilation and installation of libpcap Library
-
Configure cross compilation environment
-
Download the latest version libpcap-1.10.1 tar. GZ, extract to the current directory:
tar -xzvf ./libpcap-1.10.1.tar.gz -C ./
-
Configure installation directory and cross compilation environment
./configure --prefix=/xxx/xxx/install/ --host=arm-linux-gnueabihf
-
compile
make
-
install
make install
Generate the following files in the install/lib / Directory:
-
cd enter the install directory and package the dynamic library files under the lib directory
tar -zcvf libpcap-1.10.1-install.tar.gz lib/*.so*
-
Add libpcap-1.10.1-install.exe tar. Copy the GZ compressed package to the development board and unzip it
Create a new folder: / usr/local/lib/libpcap, and then extract it into the foldersudo mkdir /usr/local/lib/libpcap
sudo tar -zxf ./libpcap-1.10.1-install.tar.gz --strip-components 1 -C /usr/local/lib/libpcap
-
Add library file search path on development board
Open LD so. Conf filesudo vi /etc/ld.so.conf.d/libc.conf
In / etc / LD so. Add the search path of the library in the conf file
/usr/local/lib/libpcap //Add according to your library path
Then ldconfig generates / etc / LD so. Cache, ldconfig -v view
sudo ldconfig
3, Application cross compilation
Cross compile applications: you need to add the - lpcap option and specify the header file and dynamic library path
arm-linux-gnueabihf-gcc ./libpcap_test.c -o ./libpcap_test -lpcap -I/xxx/include/ -L/xxx/lib/
-
View header file and dynamic library path
Libpcap is installed as a library and a set of include files. The main include files used in your program are:#include <pcap.h>
To get the correct search path for header and library files, use the standard PKG config tool:
pkg-config --libs --static --cflags libpcap
result:
-I/usr/local/include -L/usr/local/lib -lpcap
/usr/local / the path shown here is the default. When configuring, you can use the -- prefix option to specify different paths.
-
The - lpcap option needs to be added for compilation
gcc test.c -o test -lpcap
-
For projects based on GNU autotools, please use configure in the following contents ac:
# Check for required libraries PKG_CHECK_MODULES([libpcap], [libpcap>= 1.2])
And in your makefile am:
proggy_CFLAGS = $(libpcap_CFLAGS) proggy_LDADD = $(libpcap_LIBS)
4, Install libpcap on Ubuntu system (non cross compilation)
-
Installation of libpcap
sudo apt-get install libpcap-dev
-
Application compilation
gcc libpcap_test.c -o libpcap_test -lpcap
5, libpcap usage
reference resources:
- libpcap usage
- Linux Network Programming -- detailed explanation of libpcap
- Libpcap library programming guide – packet capture
6, Test on development board
Receive multiple data packets using Libpcap (libpcap_test.c);
#include <stdio.h> #include <pcap.h> #include <arpa/inet.h> #include <time.h> #include <stdlib.h> #define BUFSIZE 1514 struct ether_header { unsigned char ether_dhost[6]; //Destination mac unsigned char ether_shost[6]; //Source mac unsigned short ether_type; //Ethernet type }; /*******************************Callback function************************************/ void ethernet_protocol_callback(unsigned char *argument,const struct pcap_pkthdr *packet_heaher,const unsigned char *packet_content) { unsigned char *mac_string; // struct ether_header *ethernet_protocol; unsigned short ethernet_type; //Ethernet type printf("----------------------------------------------------\n"); printf("%s\n", ctime((time_t *)&(packet_heaher->ts.tv_sec))); //Conversion time ethernet_protocol = (struct ether_header *)packet_content; mac_string = (unsigned char *)ethernet_protocol->ether_shost;//Get source mac address printf("Mac Source Address is %02x:%02x:%02x:%02x:%02x:%02x\n",*(mac_string+0),*(mac_string+1),*(mac_string+2),*(mac_string+3),*(mac_string+4),*(mac_string+5)); mac_string = (unsigned char *)ethernet_protocol->ether_dhost;//Get destination mac printf("Mac Destination Address is %02x:%02x:%02x:%02x:%02x:%02x\n",*(mac_string+0),*(mac_string+1),*(mac_string+2),*(mac_string+3),*(mac_string+4),*(mac_string+5)); ethernet_type = ntohs(ethernet_protocol->ether_type);//Get the type of Ethernet printf("Ethernet type is :%04x\n",ethernet_type); switch(ethernet_type) { case 0x0800:printf("The network layer is IP protocol\n");break;//ip case 0x0806:printf("The network layer is ARP protocol\n");break;//arp case 0x0835:printf("The network layer is RARP protocol\n");break;//rarp default:break; } usleep(800*1000); } int main(int argc, char *argv[]) { char error_content[100]; //Error message pcap_t * pcap_handle; unsigned char *mac_string; unsigned short ethernet_type; //Ethernet type char *net_interface = NULL; //Interface name struct pcap_pkthdr protocol_header; struct ether_header *ethernet_protocol; //Get network interface net_interface = pcap_lookupdev(error_content); if(NULL == net_interface) { perror("pcap_lookupdev"); exit(-1); } pcap_handle = pcap_open_live(net_interface,BUFSIZE,1,0,error_content);//Open network interface if(pcap_loop(pcap_handle,-1,ethernet_protocol_callback,NULL) < 0) { perror("pcap_loop"); } pcap_close(pcap_handle); return 0; }
Cross compilation:
arm-linux-gnueabihf-gcc ./libpcap_test.c -o ./libpcap_test -lpcap -I/home/osrc/Projects/tools/libpcap/install/include/ -L/home/osrc/Projects/tools/libpcap/install/lib
Running on the development board requires root account permission:
sudo ./libpcap_test
The results are as follows: