Since version 4.6 the Tcpreplay Suite installs libtcpreplay, a static
library exposing the same replay engine the tcpreplay binary uses, via
tcpreplay_api.h (#133). This lets applications replay pcap files and read
live statistics programmatically instead of forking the binary and scraping
its output.
Headers install under $(includedir)/tcpreplay/ and a pkg-config file is
provided. Because the library is static, ask pkg-config for the private
dependencies too:
cc replay_stats.c $(pkg-config --cflags --libs --static libtcpreplay) -o replay_stats
sudo ./replay_stats eth0 test.pcapThe same pkg-config data drives a CMake consumer; use the _STATIC_
variables so the static library's full dependency closure is linked:
cmake_minimum_required(VERSION 3.16)
project(replay_stats C)
find_package(PkgConfig REQUIRED)
pkg_check_modules(TCPREPLAY REQUIRED libtcpreplay)
add_executable(replay_stats replay_stats.c)
target_include_directories(replay_stats PRIVATE ${TCPREPLAY_STATIC_INCLUDE_DIRS})
target_link_directories(replay_stats PRIVATE ${TCPREPLAY_STATIC_LIBRARY_DIRS})
target_link_libraries(replay_stats PRIVATE ${TCPREPLAY_STATIC_LIBRARIES})cmake -B build
cmake --build build
sudo ./build/replay_stats eth0 test.pcap(If libtcpreplay is installed to a non-default prefix, point pkg-config at
it: PKG_CONFIG_PATH=/opt/tcpreplay/lib/pkgconfig cmake -B build.)
The in-tree copies of these examples are also built by the suite's own
CMake build: cmake --build build --target replay_stats.
replay_stats.c— the minimal flow:tcpreplay_init(), configure with thetcpreplay_set_*()setters andtcpreplay_add_pcapfile(), validate/open withtcpreplay_prepare(), runtcpreplay_replay(), then read the final counters fromtcpreplay_get_stats(). The same stats call can be made from another thread while the replay is running to stream live statistics.
These examples are compiled as part of the normal build (they are not installed), so they always match the current API.
- Replaying requires the same privileges as the
tcpreplaybinary (root or CAP_NET_RAW on Linux). - The library is currently static-only and its API/ABI follows the suite's version, so rebuild your application when upgrading Tcpreplay.