Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7434623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:55:22+00:00 2026-05-29T09:55:22+00:00

I’m writing a C application which uses the pcap library to log how much

  • 0

I’m writing a C application which uses the pcap library to log how much data (matching various packet filters) has passed through a network card. The values that I’m getting seem much too low to be correct, but I’m not sure what I’m doing wrong.

The test code below exhibits the same behavior (error checking omitted for clarity):

#include <stdio.h>
#include <pcap.h>

static int total=0;
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data){
    total += header->len;
    printf("%d\n", total);
}

int main(){
    char errbuf[1024];

    pcap_t *adhandle = pcap_open_live("en1", 65535, 0, 0, errbuf);
    pcap_setnonblock(adhandle, 1, errbuf);
    struct bpf_program fcode;
    pcap_compile(adhandle, &fcode, "port 80", 1, 0);
    pcap_setfilter(adhandle, &fcode);

    while(1){
        pcap_dispatch(adhandle, -1, packet_handler, NULL);
        sleep(1);
    }
    return 0;
}

I’m working on OSX, compiling with gcc, and I’ve tried downloading over wi-fi and wired ethernet. I expect the code above to print out the number of bytes that have matched the filter (in this case all HTTP traffic) but when I download a test file 4,357,017 bytes in size I get a value of only 95,133. My test file was a zip archive so I don’t think HTTP compression can account for the difference.

Update: I’ve modified the code to print out the size of each packet, as well as the running total, and also to report only the incoming packets (changed the filter to “src port 80”). This gives lots of packet lengths of ‘1514’ which I think is related to the MTU value of 1500, however the total remains far too low.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:55:23+00:00Added an answer on May 29, 2026 at 9:55 am

    There is no guarantee here that your program is seeing all the packets; if it doesn’t see them, it can’t count them, in which case it wouldn’t be surprising that the sum of the captured packet lengths was less than the amount of data transferred.

    First of all, you shouldn’t use non-blocking mode unless your program is doing something more than just capturing packets – polling doesn’t work better here, it can work worse. With your code, the system would run out of the BPF buffer space if more packets than fit in the buffer arrive in a second, meaning packets would be dropped and your program would never see them and would thus not count them. Prior to Lion, the default buffer size is about 32K or so, meaning there’s a very good chance that the system can run out of the BPF buffer space; Lion picks up the libpcap 1.1 change to make the default buffer size for BPF systems 512K, so that’s less likely to happen, but you still shouldn’t use non-blocking mode unless you really need it.

    In addition, you shouldn’t use non-blocking mode at all on Snow Leopard, as there’s a bug in the BPF kernel code in Snow Leopard (the FreeBSD version of the bug is PR 143855); it’s not present in Leopard or before (it was introduced in a fix to a bug fixed in Snow Leopard), and it’s fixed in Lion.

    So the first thing I’d do would be to get rid of the pcap_setnonblock(adhandle, 1, errbuf); call and the sleep(1); call, so that your program processes packets as fast as it can.

    In addition, if it doesn’t have to run on Leopard (which had an older version of libpcap that didn’t support pcap_create() or pcap_activate()), I’d raise the buffer size to 512K by doing (error checking removed for clarity):

    adhandle = pcap_create("en1", errbuf);
    pcap_set_buffer_size(adhandle, 524288);
    pcap_activate(adhandle);
    

    Finally, I’d have your program somehow provide a way to be told when to stop capturing and, when it stops, have it call pcap_stats() and report the number of dropped packets, so that you can determine whether it dropped any packets.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I need to clean up various Word 'smart' characters in user input, including but
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.