您可以创建 pcap 标头和格式,规范很简单,您不需要 pcap.h 之外的外部库。
struct pcap_file_header {
bpf_u_int32 magic;
u_short version_major;
u_short version_minor;
bpf_int32 thiszone; /* gmt to local correction */
bpf_u_int32 sigfigs; /* accuracy of timestamps */
bpf_u_int32 snaplen; /* max length saved portion of each pkt */
bpf_u_int32 linktype; /* data link type (LINKTYPE_*) */
};
所以你的文件先为pcap文件创建一个header,例如:
struct pcap_file_header pheader;
pheader.magic = 0xA1B2C3D4; // MAGIC NUMBER FOR TCPDUMP
pheader.version_major = PCAP_VERSION_MAJOR;
pheader.version_minor = PCAP_VERSION_MINOR;
pheader.thiszone = 0;
pheader.sigfigs = 0;
pheader.snaplen = 1500;
pheader.linktype = 1;
对于编写每个数据包,您需要一个如下结构:
typedef struct {
int32_t t0;
int32_t t1;
int32_t len;
int32_t caplen;
} pcap_header_writeable;
所以你会在文件上写:
pcap_header_writeable header;
header.t0 = 0;
header.t1 = 0;
header.len = length;
header.caplen = length;
// write the header on the file
// write the packet on the file after the header