Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 01-19-2004, 10:59 PM   #1
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 33
ICMP Packet capture

I have managed to capture icmp packets using winpcap on windows .
#include <pcap.h>
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);
int main()
    pcap_if_t *alldevs;
    pcap_if_t *dev;
    int i=0;
    char errbuf[PCAP_ERRBUF_SIZE];
    bpf_u_int32 mask;
	 bpf_u_int32 net;
	 pcap_t *handle;
 struct bpf_program filter;
 char filter_app[] = "icmp";
 struct pcap_pkthdr header;
    /* Retrieve the device list */
    if (pcap_findalldevs(&alldevs, errbuf) == -1)
        fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
    /* Print the list */
        printf("%d. %s", ++i, dev->name);
        if (dev->description)
            printf(" \n (%s)\n", dev->description);
        else            printf(" (No description available)\n");
        pcap_lookupnet(dev->name, &net, &mask, errbuf);
        handle = pcap_open_live(dev->name, BUFSIZ, 1, 0, errbuf);
 pcap_compile(handle, &filter, filter_app, 0, net);
   pcap_setfilter(handle, &filter);
   printf("\n Sniffing %s::",dev->name);
 pcap_loop(handle, 0, packet_handler, NULL);
        printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
        return 0;
    /* We don't need any more the device list. Free it */
	return 0;
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)
    struct tm *ltime;
    char timestr[16];
    /* convert the timestamp to readable format */
    strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);
    printf(" \n %s,%.6d len:%d\n", timestr, header->ts.tv_usec, header->len);
Now I need to check if the request is for a ping . Can you please me as to how to do that ...
Old 01-19-2004, 11:38 PM   #2
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
you should read the ICMP RFC. icmp echo's have a type of 8, echo replies have a type of 0, the code for both is 0. if you dont know what type and code stand for, have a look above. have fun!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
how do i read the data in the packet that i have captured after packet capture? gajaykrishnan Programming 23 04-19-2006 05:09 AM
Ethereal Packet capture Help sucram2g Linux - Networking 2 07-20-2005 12:35 PM
Capture whole packet at once (in Perl) Barca Programming 5 02-09-2005 02:16 AM
How to capture network packet jerrytw Programming 1 01-06-2005 10:28 AM
ICMP packet requests Ch@meleon Linux - General 0 11-29-2001 12:03 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:26 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration