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

  • Home
  • SEARCH
  • 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 8283073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:36:20+00:00 2026-06-08T10:36:20+00:00

Yesterday, I asked if my asynchronous use of libpcap was making me lose packets

  • 0

Yesterday, I asked if my asynchronous use of libpcap was making me lose packets. Today, I looked further and it seems that the problem is not on the asynchronous use of libpcap, but on the use of pcap_next_ex. Occasionally (10 runs out of a 1000), pcap_next_ex will return before the pcap handle timeout expired, telling the program that there were no packets to be read (even though they are there).

The following proof-of-concept reproduces the problem. It depends on libpcap, pthread, boost and libcrafter (a nice C++ packet crafting library). Basically, it sends a set of TCP-SYN packets to a destination and tries to obtain the responses using libpcap. A thread calling pcap_loop is run in parallel – while the main program misses some responses (as described above), the thread captures all the packets.

#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/format.hpp>
#include <boost/make_shared.hpp>
#include <boost/shared_ptr.hpp>

#include <crafter.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <pcap.h>
#include <sys/types.h>
#include <sys/socket.h>

using namespace boost;
using namespace Crafter;
using namespace std;

int captureThreadCount = 0;

typedef vector<pair<shared_ptr<Packet>, posix_time::time_duration> > PacketTimestamp;
PacketTimestamp capturedThreadPackets;

void captureThreadCallback(u_char* user, const struct pcap_pkthdr* h, const u_char* bytes)
{
    shared_ptr<Packet> packet = make_shared<Packet>();
    packet->PacketFromIP(bytes + 16, h->caplen - 16);
    posix_time::time_duration timestamp = posix_time::seconds(h->ts.tv_sec) +
        posix_time::microseconds(h->ts.tv_usec);
    capturedThreadPackets.push_back(make_pair(packet, timestamp));
    ++captureThreadCount;
}

void* captureThread(void* arg)
{
    pcap_t* pcap = (pcap_t*) arg;
    pcap_loop(pcap, -1, captureThreadCallback, NULL);
}

int main(int argc, char* argv[])
{
    if (argc != 5) {
        cout << "Usage: " << argv[0] << " <source ip> <destination ip> <port> <# tries>" << endl;
        exit(1);
    }

    InitCrafter();

    // Parameters.
    string sourceIp = argv[1];
    string destinationIp = argv[2];
    int port = atoi(argv[3]);
    int nTries = atoi(argv[4]);

    char errorBuffer[1024];
    // Socket for sending,
    int sd = socket(PF_INET, SOCK_RAW, IPPROTO_RAW);
    // And sockaddr_in to send to.
    struct sockaddr_in sin;
    sin.sin_family = AF_INET;
    sin.sin_port = htons(port);
    sin.sin_addr.s_addr = inet_addr(destinationIp.c_str());    
    // One pcap for the main thread (calling pcap_next),
    pcap_t* pcapForNext = pcap_open_live(NULL, BUFSIZ, false, 1000, errorBuffer);
    // Another pcap for a capture thread (calling pcap_loop),
    pcap_t* pcapCapture = pcap_open_live(NULL, BUFSIZ, false, 1000, errorBuffer);
    // Both filtered for SYN+ACK or RST+ACK from destination:port to source.
    string filterExpression = (boost::format("ip src %s and dst %s and src port %d and ((tcp[tcpflags] & (tcp-syn|tcp-ack) != 0) or (tcp[tcpflags] & (tcp-rst|tcp-ack) != 0))") % destinationIp % sourceIp % port).str();
    struct bpf_program filter;
    pcap_compile(pcapForNext, &filter, filterExpression.c_str(), false, 0);
    pcap_setfilter(pcapForNext, &filter);
    pcap_setfilter(pcapCapture, &filter);
    pcap_freecode(&filter);
    // Don't forget the capture thread!
    pthread_t thread;
    pthread_create(&thread, NULL, captureThread, pcapCapture);

    // Some statistics.
    int packetsSent = 0;
    int packetsReceived = 0;
    int packetsTimeout = 0;
    int packetsFailed = 0;

    // Let's probe.
    for (int i = 0; i < nTries; ++i) {
        // Create packet,
        IP ipHeader;
        ipHeader.SetSourceIP(sourceIp);
        ipHeader.SetDestinationIP(destinationIp);
        TCP tcpHeader;
        tcpHeader.SetSrcPort(12345 + i);
        tcpHeader.SetDstPort(port);
        tcpHeader.SetFlags(TCP::SYN);
        shared_ptr<Packet> packet = make_shared<Packet>(ipHeader / tcpHeader);
        // Check the time,
        struct timeval tv;
        gettimeofday(&tv, NULL);
        posix_time::time_duration sentTime =
            posix_time::seconds(tv.tv_sec) + posix_time::microseconds(tv.tv_usec);
        // And send it.
        if (packet->SocketSend(sd) > 0) {
            cerr << "Sent packet " << i << " at " << sentTime << "." << endl;
            ++packetsSent;
        }
        else {
            cerr << "Sending packet " << i << " failed." << endl;
            continue;
        }

        struct pcap_pkthdr* pktHeader;
        const u_char* pktData;
        int r;
        // Wait for the response.
        if ((r = pcap_next_ex(pcapForNext, &pktHeader, &pktData)) > 0) {
            posix_time::time_duration timestamp =
                posix_time::seconds(pktHeader->ts.tv_sec) +
                posix_time::microseconds(pktHeader->ts.tv_usec);
            cerr << "Response " << i << " received at " << timestamp << "." << endl;
            ++packetsReceived;
        }
        else if (r == 0) {
            cerr << "Timeout receiving response for " << i << "." << endl;
            ++packetsTimeout;
        }
        else {
            cerr << "Failed receiving response for " << i << "." << endl;
            ++packetsFailed;
        }
    }

    // Wait (to ensure "fast packets" are captured by the capture thread),
    usleep(500000); // 500 ms.

    for (PacketTimestamp::iterator i = capturedThreadPackets.begin();
         i != capturedThreadPackets.end(); ++i) {
        TCP* tcpLayer = GetTCP(*i->first);
        cout << "Captured packet " << (tcpLayer->GetDstPort() - 12345) << " at " << i->second << "." << endl;
    }

    cout << "SNT=" << packetsSent <<
        ", RCV=" << packetsReceived <<
        ", TIM=" << packetsTimeout <<
        ", FLD=" << packetsFailed <<
        ", CAP=" << captureThreadCount << "." << endl;

    CleanCrafter();
}
  • 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-06-08T10:36:22+00:00Added an answer on June 8, 2026 at 10:36 am

    This seems to be a problem with libpcap using memory mapping in Linux (it happens even on the latest libpcap, 1.3.0). If libpcap is compiled without memory mapping support (as described in a message from the tcpdump mailing list archive), the problem goes away.

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

Sidebar

Related Questions

This problem is related to This question I asked yesterday. Now it seems that
I asked a question yesterday about a problem that Im having with a program
This question is about a similar javascript scope problem that I asked yesterday .
Yesterday I asked about serving byte ranges from PHP. Today my question is -
Yesterday I asked another question to calculate time. Now I realized, that it makes
I am trying to further a question I asked yesterday where I wanted to
This is linked to a question I asked yesterday. Briefly, the problem I'm having
Yesterday I asked Are GUIDs generated on Windows 2003 safe to use as session
This one is similar to a question that I asked yesterday. However my concern
I asked a related question on this yesterday, and was told what not to

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.