talira

A portable C/C++ library for network traffic capture

open sourceBSD-3-Clauseself-hostablefree

libpcap is The Tcpdump Group's portable C/C++ library for capturing network traffic: one API (pcap_*) over each operating system's own capture mechanism, so the same program captures packets on Linux, macOS, the BSDs, Solaris, AIX and others. Programs compile filter expressions into BSD Packet Filter programs, which run in the kernel where the platform supports it, read and write pcap savefiles (and read most pcapng files), send raw packets, put Wi-Fi adapters in monitor mode, and capture on other machines through the rpcapd daemon. It sits under tcpdump, Wireshark and many other capture tools, whose filter syntax and file format come from it. On Windows the capture driver comes from Npcap (or the unmaintained WinPcap), which ship their own builds of libpcap. It started at Lawrence Berkeley National Laboratory's Network Research Group and is BSD-licensed.

Problems it solves

  • capture network packets from my own C program on Linux, macOS and the BSDs with one API
  • only receive the packets my program cares about by applying a filter expression in the kernel
  • read and write pcap capture files from my own code
  • open a pcapng capture file in a C or C++ program
  • capture packets on a remote machine and process them in a program on my workstation
  • send raw Ethernet frames out of a network interface from a C program
  • put a Wi-Fi adapter into monitor mode from code to capture 802.11 frames
  • build my own network monitoring or intrusion detection tool on a standard packet capture library
  • use the same packet capture library that tcpdump and Wireshark are built on

Features

  • one capture API across Linux, macOS, FreeBSD, NetBSD, OpenBSD, DragonFly BSD, Solaris, AIX, QNX and GNU/Hurd
  • filter expressions compiled to BPF, run in the kernel on BPF, Linux packet socket and GNU/Hurd, otherwise in user space
  • reads pcap savefiles and most pcapng files; writes and appends pcap savefiles
  • sends packets with pcap_inject and pcap_sendpacket
  • monitor (rfmon) mode, immediate mode, non-blocking mode and timestamp type and precision selection
  • rpcapd daemon for capture on a remote machine, with TLS
  • memory-mapped receive on Linux
  • stable ABI: binary compatibility kept across releases (libpcap.so.1)

Limitations

  • Doesn't use Linux's eBPF mechanisms yet: on Linux it uses the packet socket's memory-mapped receive with classic BPF filters1
  • Can't capture on Windows by itself: Windows needs the Npcap (or unmaintained WinPcap) driver, which ships its own build of libpcap2
  • Doesn't read every pcapng file: it reads pcap files and most pcapng files3

Capabilities

Has official SDKsplan not known

Alternatives

The same job

These do what libpcap does.

  1. Npcap Packet capture library for Windows

Compare libpcap with the top 3