3 /* prototype of the packet handler */
4 void packet_handler(u_char
*param
, const struct pcap_pkthdr
*header
, const u_char
*pkt_data
);
6 main(int argc
, char **argv
)
13 char errbuf
[PCAP_ERRBUF_SIZE
];
14 pcap_dumper_t
*dumpfile
;
17 /* Check command line */
20 printf("usage: %s filename", argv
[0]);
24 /* Retrieve the device list on the local machine */
25 if (pcap_findalldevs(&alldevs
, errbuf
) == -1)
27 fprintf(stderr
,"Error in pcap_findalldevs: %s\n", errbuf
);
32 for(d
=alldevs
; d
; d
=d
->next
)
34 printf("%d. %s", ++i
, d
->name
);
36 printf(" (%s)\n", d
->description
);
38 printf(" (No description available)\n");
43 printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
47 printf("Enter the interface number (1-%d):",i
);
50 if(inum
< 1 || inum
> i
)
52 printf("\nInterface number out of range.\n");
53 /* Free the device list */
54 pcap_freealldevs(alldevs
);
58 /* Jump to the selected adapter */
59 for(d
=alldevs
, i
=0; i
< inum
-1 ;d
=d
->next
, i
++);
62 /* Open the adapter */
63 if ((adhandle
= pcap_open_live(d
->name
, // name of the device
64 65536, // portion of the packet to capture.
65 // 65536 grants that the whole packet will be captured on all the MACs.
66 1, // promiscuous mode (nonzero means promiscuous)
68 errbuf
// error buffer
71 fprintf(stderr
,"\nUnable to open the adapter. %s is not supported by WinPcap\n", d
->name
);
72 /* Free the device list */
73 pcap_freealldevs(alldevs
);
77 /* Open the dump file */
78 dumpfile
= pcap_dump_open(adhandle
, argv
[1]);
82 fprintf(stderr
,"\nError opening output file\n");
86 printf("\nlistening on %s... Press Ctrl+C to stop...\n", d
->description
);
88 /* At this point, we no longer need the device list. Free it */
89 pcap_freealldevs(alldevs
);
91 /* start the capture */
92 pcap_loop(adhandle
, 0, packet_handler
, (unsigned char *)dumpfile
);
98 /* Callback function invoked by libpcap for every incoming packet */
99 void packet_handler(u_char
*dumpfile
, const struct pcap_pkthdr
*header
, const u_char
*pkt_data
)
101 /* save the packet on the dump file */
102 pcap_dump(dumpfile
, header
, pkt_data
);