DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
pcap.h
Go to the documentation of this file.
1// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file pcap.h
6 * @brief libpcap file framing - the classic global + per-record headers, link-type agnostic.
7 *
8 * One owner for the PCAP framing shared by every capture feature (Wi-Fi promiscuous capture,
9 * CAN / bus listen-only capture, ...): each writes its frames with the matching DLT link type so
10 * the forwarded stream is a valid `.pcap` a wired Wireshark / tcpdump opens directly. Header-only
11 * and pure (little-endian byte writes, no heap, no stdlib), host-identical.
12 *
13 * @author Douglas Quigg (dstroy0)
14 * @date 2026
15 */
16
17#ifndef DETERMINISTICESPASYNCWEBSERVER_DET_PCAP_H
18#define DETERMINISTICESPASYNCWEBSERVER_DET_PCAP_H
19
20#include <stddef.h>
21#include <stdint.h>
22
23/** @brief libpcap header sizes. */
24#define DET_PCAP_GLOBAL_HDR_LEN 24
25#define DET_PCAP_REC_HDR_LEN 16
26
27/** @brief Common libpcap DLT link-layer types. */
28#define DET_DLT_IEEE802_11 105 ///< raw 802.11 (Wi-Fi promiscuous capture)
29#define DET_DLT_CAN_SOCKETCAN 227 ///< Linux SocketCAN classic/FD frames
30#define DET_DLT_ETHERNET 1 ///< IEEE 802.3 Ethernet
31#define DET_DLT_IEEE802_15_4_NOFCS 230 ///< raw 802.15.4 MAC frame, no FCS
32#define DET_DLT_IEEE802_15_4_TAP 283 ///< 802.15.4 with a TAP pseudo-header (RSSI / channel TLVs)
33
35{
36inline void wr32le(uint8_t *p, uint32_t v)
37{
38 p[0] = (uint8_t)v;
39 p[1] = (uint8_t)(v >> 8);
40 p[2] = (uint8_t)(v >> 16);
41 p[3] = (uint8_t)(v >> 24);
42}
43inline void wr16le(uint8_t *p, uint16_t v)
44{
45 p[0] = (uint8_t)v;
46 p[1] = (uint8_t)(v >> 8);
47}
48} // namespace detpcap_detail
49
50/**
51 * @brief Write the 24-byte libpcap global header (little-endian, microsecond timestamps).
52 * @param linktype the DLT_* link type of the frames that follow (e.g. ::DET_DLT_IEEE802_11).
53 * @return ::DET_PCAP_GLOBAL_HDR_LEN, or 0 if @p cap is too small.
54 */
55inline size_t det_pcap_global_header(uint8_t *out, size_t cap, uint32_t linktype)
56{
57 if (!out || cap < DET_PCAP_GLOBAL_HDR_LEN)
58 return 0;
59 detpcap_detail::wr32le(out + 0, 0xa1b2c3d4); // magic: usec timestamps, little-endian
60 detpcap_detail::wr16le(out + 4, 2); // version major
61 detpcap_detail::wr16le(out + 6, 4); // version minor
62 detpcap_detail::wr32le(out + 8, 0); // thiszone (GMT)
63 detpcap_detail::wr32le(out + 12, 0); // sigfigs
64 detpcap_detail::wr32le(out + 16, 65535); // snaplen
65 detpcap_detail::wr32le(out + 20, linktype); // network / DLT
67}
68
69/**
70 * @brief Write a 16-byte libpcap record header for one captured frame.
71 * @return ::DET_PCAP_REC_HDR_LEN, or 0 if @p cap is too small.
72 */
73inline size_t det_pcap_record_header(uint8_t *out, size_t cap, uint32_t ts_sec, uint32_t ts_usec, uint32_t caplen,
74 uint32_t origlen)
75{
76 if (!out || cap < DET_PCAP_REC_HDR_LEN)
77 return 0;
78 detpcap_detail::wr32le(out + 0, ts_sec);
79 detpcap_detail::wr32le(out + 4, ts_usec);
80 detpcap_detail::wr32le(out + 8, caplen);
81 detpcap_detail::wr32le(out + 12, origlen);
83}
84
85#endif // DETERMINISTICESPASYNCWEBSERVER_DET_PCAP_H
void wr32le(uint8_t *p, uint32_t v)
Definition pcap.h:36
void wr16le(uint8_t *p, uint16_t v)
Definition pcap.h:43
#define DET_PCAP_GLOBAL_HDR_LEN
libpcap header sizes.
Definition pcap.h:24
size_t det_pcap_global_header(uint8_t *out, size_t cap, uint32_t linktype)
Write the 24-byte libpcap global header (little-endian, microsecond timestamps).
Definition pcap.h:55
size_t det_pcap_record_header(uint8_t *out, size_t cap, uint32_t ts_sec, uint32_t ts_usec, uint32_t caplen, uint32_t origlen)
Write a 16-byte libpcap record header for one captured frame.
Definition pcap.h:73
#define DET_PCAP_REC_HDR_LEN
Definition pcap.h:25