Saving a capture file
There will be times when we want to capture packets and not view the results immediately. The libpcap library has functions to open and save the packets to a binary file. This file has the same format as a tcpdump save file.
Getting ready
Prior to running this recipe, we need to follow the Adding libpcap to your project recipe explained earlier in this chapter. We also need to go through the Capturing packets recipe. We will discuss how to modify the packet capture code in order to save the captured packets to a save file.
How to do it…
Let's save our captured packets to a file:
We begin by defining a
pcap_dumper_t
pointer and creating apcap_loop
. This will be the pointer to our save file.In our packet capture code from the Capturing packets recipe of this chapter, we want to replace the
pcap_loop
statement with the following lines:pcap_dumper_t *dumpfile=pcap_dump_open(handle, "~/pcapdump.pcap"); if(dumpfile==NULL){ NSLog(@"Error opening output file"); exit...