LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices



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

Rep: Reputation: 32
ICMP Packet capture


I have managed to capture icmp packets using winpcap on windows .
Code:
#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);
        exit(1);
    }
    
    /* Print the list */
    for(dev=alldevs;dev;dev=dev->next)
    {
        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);
pcap_close(handle);
	}
    if(i==0)
    {
        printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
        return 0;
    }
    /* We don't need any more the device list. Free it */
    pcap_freealldevs(alldevs);
	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 */
    ltime=localtime(&header->ts.tv_sec);
    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-20-2004, 12:38 AM   #2
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
http://www.faqs.org/rfcs/rfc792.html
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!
 
  


Reply


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 06:09 AM
Ethereal Packet capture Help sucram2g Linux - Networking 2 07-20-2005 01:35 PM
Capture whole packet at once (in Perl) Barca Programming 5 02-09-2005 03:16 AM
How to capture network packet jerrytw Programming 1 01-06-2005 11:28 AM
ICMP packet requests Ch@meleon Linux - General 0 11-29-2001 01:03 PM


All times are GMT -5. The time now is 06:51 PM.

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