我想讀取pcap文件像一些數據結構像矢量或數組,然後在應用程序中使用收集的數據(只有選擇一個像包長度,時間戳)。我發現了一些示例應用程序讀取PCAP:閱讀pcap文件向量/數組
#include <stdio.h>
#include <pcap.h>
#define LINE_LEN 16
void dispatcher_handler(u_char *, const struct pcap_pkthdr *, const u_char *);
int main(int argc, char **argv)
{
pcap_t *fp;
char errbuf[PCAP_ERRBUF_SIZE];
if(argc != 2)
{
printf("usage: %s filename", argv[0]);
return -1;
}
/* Open the capture file */
if ((fp = pcap_open_offline(argv[1], // name of the device
errbuf // error buffer
)) == NULL)
{
fprintf(stderr,"\nUnable to open the file %s.\n", argv[1]);
return -1;
}
/* read and dispatch packets until EOF is reached */
pcap_loop(fp, 0, dispatcher_handler, NULL);
pcap_close(fp);
return 0;
}
void dispatcher_handler(u_char *temp1,
const struct pcap_pkthdr *header,
const u_char *pkt_data)
{
u_int i=0;
/*
* unused variable
*/
(VOID*)temp1;
/* print pkt timestamp and pkt len */
printf("%ld:%ld (%ld)\n", header->ts.tv_sec, header->ts.tv_usec, header->len);
printf("\n\n");
}
問題是與pcap_loop()
。我找到了documentation,但唯一的信息是,這是讀取整個文件,直到文件結束。我一直試圖將文件視爲典型的FILE
,並在while循環中讀取,直到EOF
,但它不起作用,因爲我不能簡單地將fp
視爲FILE
。
此外,我沒有看到任何可能性將指針傳遞給pcap_handler
以稍後檢索它。
有人可以建議我怎麼能做到這一點嗎?