#include <pcap.h>
#include <stdio.h>
#include <stdlib.h>
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);
int main()
{
pcap_t *pH;
char errbuf[PCAP_ERRBUF_SIZE];
// FILE * hand = fopen("./dump.txt", "w+");
pcap_dumper_t * dumpF = NULL;
char *fileName = "./dump.txt";
if((pH = pcap_open_live("eth0", 65536, 0, 1000, NULL)) == NULL){
printf("\nUnable to open the adapter.%s is not supported by WinPcap\n", "eth0");
return -1;
}
printf("create the dump file %s...\n", fileName);
dumpF = pcap_dump_open(pH, fileName);
if(NULL == dumpF){
// printf("Error: %s\n", pH->errbuf);
return 255;
}
printf("start to listen %s... \n", "eth0");
pcap_loop(pH, 10, packet_handler, (u_char*)dumpF);
printf("end listening...\n");
pcap_dump_close(dumpF);
return 0;
}
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
pcap_dump(param, header, pkt_data);
return;
}
====================================================================================================================================
gcc /usr/lib/pcap.o -lpcap a.c
====================================================================================================================================