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 8636849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:19:36+00:00 2026-06-12T10:19:36+00:00

I am trying to capture packets with libpcap . Here is my code: int

  • 0

I am trying to capture packets with libpcap. Here is my code:

int main (int argc, char **argv) {
    char *dev = "eth0";
    char errbuf[PCAP_BUFFER_SIZE];
    pcap_t *handle;

    char filter[] = "tcp and src port 80";
    struct bpf_program fp;
    bpf_u_int32 mask, net;

    handle = pcap_open_live(dev, SNAP_LEN, 1, 1000, errbuf);
    pcap_compile(handle, &fp, filter,0,net);
    pcap_setfilter(handle, &fp);
    pcap_loop(handle, -1, got_packet, NULL);

    pcap_freecode(&fp);
    pcap_close(handle);

    return 0;
}

And

void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet) {
    static int count = 1;
    static int http_count = 1;
    const struct sniff_ethernet *ethernet;
    const struct sniff_ip *ip;
    const struct sniff_tcp *tcp;

    int size_ip;
    int size_tcp;
    int size_payload;

    count++;

    ethernet = (struct sniff_ethernet*) (packet);
    ip = (struct sniff_ip*) (packet + SIZE_ETHERNET);
    size_ip = IP_HL(ip)*4;
    if (size_ip  < 20){
        printf("Invalid IP header %d", size_ip);
        return;
    }

    if (ip->ip_p != IPPROTO_TCP){
        printf("Not TCP\n");
        return;
    }

    tcp = (struct sniff_tcp*) (packet +SIZE_ETHERNET+size_ip);
    size_tcp = TH_OFF(tcp) * 4;
    if (size_tcp < 20) {
        printf("Invalid TCP header");
        return;
    }

    if ((tcp->th_flags & TH_ACK) != 0) {
        const char *payload = (const char *) (packet + SIZE_ETHERNET + size_ip + size_tcp);
        size_payload = ntohs(ip->ip_len)- (size_ip + size_tcp);
        std::cout << payload << "\n";       
        if (count == 4)
            exit(0);
    }

The parameters are:

#define SNAP_LEN    65535
#define SIZE_ETHERNET   14
#define ETHER_ADDR_LEN  6
#define PCAP_BUFFER_SIZE 65535

Now, the whole of the fourth packet is not printed. I dumped the packet using tcpdump and it gets the whole packet but my code does not. Is something wrong here?

  • 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-12T10:19:38+00:00Added an answer on June 12, 2026 at 10:19 am

    This turned out to be a problem with printing. I used the function print_payload and it works fine now.

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

Sidebar

Related Questions

I trying to capture packets using SharpPcap library. I'm able to return the packets
I'm trying to capture packets from two devices on my network. I have tcpdump
I'm using SharpPcap to capture packets. I'm trying to get Traffic Class value and
I'm trying to capture the output here. If, at the python prompt, I run
I'm trying to capture the following pattern everything except data-extra Here is the search
Case : Again trying to capture packets through my NIC, I have developed 2
I am trying capture UIIMage with UIGraphicsBeginImageContext(); but my code capture the whole screen
I'm trying to capture packets from my iPhone app. It does not use HTTP
I'm trying to capture tcp packets from a GPS device(client) configured to my server's
I am trying to code to capture the packet loss on computers, but the

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.