libpcap provides functions for user-level packet capture, used in low-level network monitoring. Following program captures the packets from the wireless interface of the device on which the compiled binary will be running. We have compiled this on ubuntu 16.04, and run it on laptop with wifi interface as wlan0, hence it will capture the wifi packets from the laptop.

Command line execution

Terminalbash
sed in low-level network monitoring. Following program captures the packets from the wireless interface of the device on which the compiled binary will be running. We have compiled this on ubuntu 16.04, and run it on laptop with wifi interface as wlan0, hence it will capture the wifi packets from the laptop.  $ vim sniffex.c  /*  * sniffex.c  *  * Sniffer example of TCP/IP packet capture using libpcap.  *   * Version 0.1.1 (2005-07-05)  * Copyright (c) 2005 The Tcpdump Group  *  * This software is intended to be used as a practical example and   * demonstration of the libpcap library; available at:  * http://www.tcpdump.org/  *  ****************************************************************************  *  * This software is a modification of Tim Carstens' "sniffer.c"  * demonstration source code, released as follows:  *   * sniffer.c  * Copyright (c) 2002 Tim Carstens  * 2002-01-07  * Demonstration of using libpcap  * timcarst -at- yahoo -dot- com  *   * "sniffer.c" is distributed under these terms:  *   * Redistribution and use in source and binary forms, with or without  * modification, are permitted provided that the following conditions  * are met:  * 1. Redistributions of source code must retain the above copyright  *    notice, this list of conditions and the following disclaimer.  * 2. Redistributions in binary form must reproduce the above copyright  *    notice, this list of conditions and the following disclaimer in the  *    documentation and/or other materials provided with the distribution.  * 4. The name "Tim Carstens" may not be used to endorse or promote  *    products derived from this software without prior written permission  *  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF  * SUCH DAMAGE.  * <end of "sniffer.c" terms>  *  * This software, "sniffex.c", is a derivative work of "sniffer.c" and is  * covered by the following terms:  *  * Redistribution and use in source and binary forms, with or without  * modification, are permitted provided that the following conditions  * are met:  * 1. Because this is a derivative work, you must comply with the "sniffer.c"  *    terms reproduced above.  * 2. Redistributions of source code must retain the Tcpdump Group copyright  *    notice at the top of this source file, this list of conditions and the  *    following disclaimer.  * 3. Redistributions in binary form must reproduce the above copyright  *    notice, this list of conditions and the following disclaimer in the  *    documentation and/or other materials provided with the distribution.  * 4. The names "tcpdump" or "libpcap" may not be used to endorse or promote  *    products derived from this software without prior written permission.  *  * THERE IS ABSOLUTELY NO WARRANTY FOR THIS PROGRAM.  * BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY  * FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN  * OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES  * PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED  * OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS  * TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE  * PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,  * REPAIR OR CORRECTION.  *   * IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING  * WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR  * REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,  * INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING  * OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED  * TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY  * YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER  * PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE  * POSSIBILITY OF SUCH DAMAGES.  * <end of "sniffex.c" terms>  *   ****************************************************************************  *  * Below is an excerpt from an email from Guy Harris on the tcpdump-workers  * mail list when someone asked, "How do I get the length of the TCP  * payload?" Guy Harris' slightly snipped response (edited by him to  * speak of the IPv4 header length and TCP data offset without referring  * to bitfield structure members) is reproduced below:  *   * The Ethernet size is always 14 bytes.  *   * <snip>...</snip>  *  * In fact, you *MUST* assume the Ethernet header is 14 bytes, *and*, if   * you're using structures, you must use structures where the members   * always have the same size on all platforms, because the sizes of the   * fields in Ethernet - and IP, and TCP, and... - headers are defined by   * the protocol specification, not by the way a particular platform's C   * compiler works.)  *  * The IP header size, in bytes, is the value of the IP header length,  * as extracted from the "ip_vhl" field of "struct sniff_ip" with  * the "IP_HL()" macro, times 4 ("times 4" because it's in units of  * 4-byte words).  If that value is less than 20 - i.e., if the value  * extracted wit

Implementation details

$ vim sniffex.c /* * sniffex.c * * Sniffer example of TCP/IP packet capture using libpcap. * * Version 0.1.1 (2005-07-05) * Copyright (c) 2005 The Tcpdump Group * * This software is intended to be used as a practical example and * demonstration of the libpcap library; available at: * http://www.tcpdump.org/ * **************************************************************************** * * This software is a modification of Tim Carstens' "sniffer.c" * demonstration source code, released as follows: * * sniffer.c * Copyright (c) 2002 Tim Carstens * 2002-01-07 * Demonstration of using libpcap * timcarst -at- yahoo -dot- com * * "sniffer.c" is distributed under these terms: * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution.

Gotchas and common issues

  • Permission checks - verify user access rights and sudo privileges before executing system-level operations.

  • Environment configuration - double-check path variables and dependency versions to prevent runtime failures.

  • Backup safeguards - maintain configuration backups before applying system or database modifications.

Following these steps ensures clean configuration and reliable execution for sniffex – c program to capture wifi packets using libpcap.