Capturing packets

In this recipe, we will show you how to use the libpcap library to capture packets. We will also introduce some basic libpcap concepts, such as the pcap handler and filters.

Getting ready

Prior to running this recipe, we need to follow the Adding libpcap to your project recipe presented earlier in this chapter.

How to do it…

Let's capture some packets by following the ensuing steps:

  1. We start off by defining the following three symbols for use in our code:
    #define SNAPLEN 65535
    #define PROMISC 1
    #define TIMEOUT 500

    The SNAPLEN constant defines the maximum size of the packet to be captured. The PROMISC constant specifies whether we want to set the interface to the promiscuous mode or not; 1 is true and 0 is false. The TIMEOUT constant ...

Get iOS and OS X Network Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.