我使用C语言进行抓数据包的操作,然后代码如下所示,但是不知道怎样将抓到的数据包输出成wireshark的可执行文件。请教各位了!
#include <pcap.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
int main()
{
pcap_if_t *alldevs;
char errbuf[PCAP_ERRBUF_SIZE];
if(pcap_findalldevs(&alldevs, errbuf) == -1)
{
printf("Error in pcap_findalldevs: %s\n", errbuf);
exit(EXIT_FAILURE);
}
printf("Device name: %s\n", alldevs->name);
printf("Descripetion: %s\n", alldevs->description);
pcap_t *handler;
handler = pcap_open_live(alldevs[0].name, PCAP_ERRBUF_SIZE, 1, 100, errbuf);
if(handler == NULL)
{
printf("open fail\n");
return -1;
}
FILE *text = fopen("Capa.pcapng", "w");
struct pcap_pkthdr pkhdr; //packet information
while(1)
{
const unsigned char *packet = pcap_next(handler, &pkhdr); //get a packet
if(packet == NULL)
{
pcap_close(handler);
break;
}
fprintf(text, "%s\n", packet);
}
pcap_freealldevs(alldevs);
return 0;
}