Book Image

iOS and OS X Network Programming Cookbook

By : Jon Hoffman
Book Image

iOS and OS X Network Programming Cookbook

By: Jon Hoffman

Overview of this book

Table of Contents (15 chapters)
iOS and OS X Network Programming Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 is the read timeout in milliseconds.

  2. We need to define the following variables:

     pcap_t *handle;
     char errbuf[PCAP_ERRBUF_SIZE];
     bpf_u_int32 localNet, netMask;
     struct bpf_program filterCode;
     char filter[] = "arp or tcp or udp or icmp";

    The three variables...