我正在创建一个简单的嗅探器,只是为了好玩,我想将转储输出到文件中。这是嗅探器的代码:
#include <stdio.h>
#include <pcap.h>
#define MAX_PACKET_NUM 5
int main(void) {
struct pcap_pkthdr header;
const u_char *packet;
char errbuf[PCAP_ERRBUF_SIZE];
char *device;
pcap_t *pcap_handle;
int i;
printf("## Now sniffing on: %s ##\n", device);
printf("## Max packets number for this session: %d\n", MAX_PACKET_NUM);
printf("[?] Sniffing...\n");
pcap_handle = pcap_open_live(device, 1000, 1, 0, errbuf);
for(i=0; i < MAX_PACKET_NUM; i++) {
packet = pcap_next(pcap_handle, &header);
printf("[!] Captured a %d bytes packet!\n", header.len);
dump(packet, header.len);
}
}
它工作正常,但是如何将转储输出到文件?我尝试使用文件流,但我真的不知道如何输出函数。如您所见,dump()函数将输出打印到屏幕上,也许有输出到文件的函数?请帮帮我!