====================================================
NIC 디바이스를 찾아, 연결하지 않고 Offline 의 Pcap 파일을 읽고 파일의 끝까지 패킷의 내용을 분석할 수 있다.
따라서
Pcap 파일을 읽는 예제 코드
/* Open the capture file */
if ((fp = pcap_open_offline(
"C:\\\\SampleTraces\\\\ip-fragments.pcap",
errbuf)) == NULL)
{
fprintf(stderr, "\\nUnable to open the file %s.\\n",
"C:\\\\SampleTraces\\\\ip-fragments.pcap");
return -1;
}
/* read and dispatch packets until EOF is reached */
pcap_loop(fp, 0, dispatcher_handler, NULL);
//////////////////////////////////////////////////////////////////////
// => packet_handler 와 기능은 똑같다고 보면 된다.
void dispatcher_handler(u_char* temp1,
const struct pcap_pkthdr* header,
const u_char* pkt_data)
{
u_int i = 0;
/* print pkt timestamp and pkt len */
printf("%ld:%ld (%ld)\\n", header->ts.tv_sec, header->ts.tv_usec, header->len);
EtherHeader* pEther = (EtherHeader*)pkt_data;
printf(
"SRC: %02X-%02X-%02X-%02X-%02X-%02X -> "
"DST: %02X-%02X-%02X-%02X-%02X-%02X, type:%04X\\n",
pEther->srcMac[0], pEther->srcMac[1], pEther->srcMac[2],
pEther->srcMac[3], pEther->srcMac[4], pEther->srcMac[5],
pEther->dstMac[0], pEther->dstMac[1], pEther->dstMac[2],
pEther->dstMac[3], pEther->dstMac[4], pEther->dstMac[5],
ntohs(pEther->type));
/* Print the packet */
for (i = 1; (i < header->caplen + 1); i++)
{
printf("%.2x ", pkt_data[i - 1]);
if ((i % LINE_LEN) == 0) printf("\\n");
}
printf("\\n\\n");
}