libpcap network packet capture function library

tags: gcc and makefile  pcap

  • download
  • Compile and install
  • Instance

The C function interface provided by the library is used to capture data packets passing through the specified network interface.

download:
http://www.linuxfromscratch.org/blfs/view/svn/basicnet/libpcap.html

Install and compile:

tar -zxvf libpcap-1.9.1.tar.gz
./configure configuration
make compile
make install installation, the default directory /usr/local/
Copy the two directories /usr/local/lib and /usr/local/include to your own program directory, gcc -Wl,rpath=./lib -L./lib -I ./include

There may be an error, please check whether you have installed all the dependent packages bison, m4, GNU, flex and libpcap-dev:

#yum install bison
#yum install m4
#yum install flex

test.c:

#include <pcap.h>
#include <stdio.h>

int main()
{
  char errBuf[PCAP_ERRBUF_SIZE], * device;
  device = pcap_lookupdev(errBuf);
  if(device)
  {
    printf("success: device: %s\n", device);
  }
  else
  {
    printf("error: %s\n", errBuf);
  }
  return 0;
}

Makefile:

  1 CC = gcc
  2 OBJS = test.o
  3 CFLAGS = -Wl,-rpath=./lib -L./lib
  4 CFLAGS += -I./include
  5 LIBS = pcap
  6 DEST = test
  7 RM = rm -f
  8 
  9 $(DEST):$(OBJS)
 10         $(CC) -o $(DEST) $(OBJS) $(CFLAGS) $(addprefix -l, $(LIBS))
 11 clean:
 12         $(RM) $(DEST) $(OBJS)  
 13 
 14 #test: test.c
 15 #       gcc -o test test.c -lpcap -Wl,-rpath=./lib -L./lib -I./include
 16 #clean:
 17 #       rm -f test.o test

Run result: ./test

success: device: ens33

Intelligent Recommendation

Using the libpcap function library to capture packets in a Linux environment

Install libpcap Reference blog: The official website to download the libpacp compression package, the official website address:Click Download the current latest version shown in the image: Unzip libpc...

Linux network packet capture/packet capture technology comparison: napi, libpcap, afpacket, PF_RING, PACKET_MMAP, DPDK, XDP (eXpress Data Path)

Table of Contents 1. Traditional Linux network protocol stack process and performance analysis The main problem of the protocol stack Resource allocation and release for a single packet level Serial a...

Analysis of libpcap packet capture under linux

First, download the libpcap package first.http://www.tcpdump.org/#latest-release Then install, after the installation is complete, go to the tests folder of the installation root directory, compile an...

libpcap packet capture analysis project (2)

In project (1), we successfully captured the data packet and extracted the basic information in the data packet. This time we will output the various protocols used by the data packet and output the m...

libpcap packet capture analysis project (1)

First introduce the usage of libpcap library functions: Here I use two functions, pcap_open_live () and pcap_next (), you can jump through the portal to see the specific usage. The comments in the cod...

More Recommendation

libpcap packet capture analysis project (4)

In project three, we completed the parsing of a package in the "hospital mirror stream.pcapng" file. The next thing we need to do is to write the parsed data into the file, so that the TS st...

Analysis of libpcap packet capture mechanism (3)

At present, the network packet capture system under the Linux operating system is generally built on the libpcap packet capture platform. The English meaning of libpcap is Library of Packet Capture, t...

Analysis of libpcap packet capture mechanism (4)

1. Process and performance analysis of traditional Linux network protocol stack The Linux network protocol stack is a typical system for processing network packets. It includes the entire process from...

Use pf_ring to accelerate libpcap to capture packets on ubuntu16.04, Gigabit network runs smoothly without packet loss

Recently, the project needs to collect packets under the gigabit network, and the bandwidth is about 800Mb. Traditional sockets or raw sockets are easy to lose packets, so use libpcap to capture packe...

Design and implementation of network packet capture and traffic online analysis system-based on libpcap on MacOS Record this happy (DT) week

Design and implementation of network packet capture and traffic online analysis system-based on libpcap on MacOS Record this happy (DT) week Claim: Design and implement a network flow analysis system ...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top