Transplantation and use of libpcap

Posted by FrOzeN on Sat, 29 Jan 2022 06:06:37 +0100

Reference article:

  1. Porting libpcap packet capture library to arm platform under Linux
  2. Linux Network Programming -- detailed explanation of libpcap
  3. libpcap usage
  4. Libpcap library programming guide – packet capture
  5. Methods of network monitoring and sending data packets under linux (i.e. the use of libpcap and libnet class libraries)

1, libpcap Library Download

  1. http://www.tcpdump.org/
  2. https://github.com/the-tcpdump-group/libpcap/releases

2, Cross compilation and installation of libpcap Library

  1. Configure cross compilation environment

  2. Download the latest version libpcap-1.10.1 tar. GZ, extract to the current directory:

     tar -xzvf ./libpcap-1.10.1.tar.gz -C ./ 
    
  3. Configure installation directory and cross compilation environment

    ./configure --prefix=/xxx/xxx/install/ --host=arm-linux-gnueabihf
    
  4. compile

    make
    
  5. install

    make install
    

    Generate the following files in the install/lib / Directory:

  6. 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*
    
  7. 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 folder

    sudo mkdir /usr/local/lib/libpcap
    
    sudo tar -zxf ./libpcap-1.10.1-install.tar.gz --strip-components 1 -C /usr/local/lib/libpcap
    
  8. Add library file search path on development board
    Open LD so. Conf file

    sudo 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/
  1. 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.

  2. The - lpcap option needs to be added for compilation

    gcc test.c -o test -lpcap
    
  3. 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)

  1. Installation of libpcap

    sudo apt-get install libpcap-dev
    
  2. Application compilation

    gcc libpcap_test.c -o libpcap_test -lpcap
    

5, libpcap usage

reference resources:

  1. libpcap usage
  2. Linux Network Programming -- detailed explanation of libpcap
  3. 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:

Topics: socket Network Communications