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

Filtering packets


In the Capturing packets recipe of this chapter, we showed a basic filter of char filter[] = "arp or tcp or udp or icmp";. In this recipe, we will take a more in-depth look at how to create a filter.

Since the libpcap library is used as the packet-capturing library for the tcpdump project, the libpcap filters take the same format as the tcpdump filter format. Any of the tcpdump filter expressions that we find on the Internet should work with libpcap. A Google search for "tcpdump filter" will return lots of results, but we will go over the basics in this recipe.

Getting ready

The filters that we will create in this recipe can be used along with the code in the Capturing packets recipe of this chapter. You should go through it to understand where to use these filters.

How to do it…

Let's create a filter.

When we create a filter for libpcap to use, we create it as a char array. We then use the pcap_compile() function to compile the expression to a bpf_program. The compiled bpf_program...