O'Reilly logo

iOS and OS X Network Programming Cookbook by Jon Hoffman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required