<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: C Packet Sniffer Code with Libpcap and Linux Sockets (BSD)</title>
	<atom:link href="http://www.binarytides.com/blog/c-packet-sniffer-code-with-libpcap-and-linux-sockets-bsd/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.binarytides.com/blog/c-packet-sniffer-code-with-libpcap-and-linux-sockets-bsd/</link>
	<description>Socket Programming , Game Programming , PHP , Mysql , Ubuntu etc.</description>
	<lastBuildDate>Thu, 09 Feb 2012 14:29:53 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Binary Tides</title>
		<link>http://www.binarytides.com/blog/c-packet-sniffer-code-with-libpcap-and-linux-sockets-bsd/comment-page-1/#comment-30548</link>
		<dc:creator>Binary Tides</dc:creator>
		<pubDate>Wed, 07 Dec 2011 15:15:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.binarytides.com/blog/?p=49#comment-30548</guid>
		<description>code has been updated and now uses devname in pcap_open_live function</description>
		<content:encoded><![CDATA[<p>code has been updated and now uses devname in pcap_open_live function</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Binary Tides</title>
		<link>http://www.binarytides.com/blog/c-packet-sniffer-code-with-libpcap-and-linux-sockets-bsd/comment-page-1/#comment-30547</link>
		<dc:creator>Binary Tides</dc:creator>
		<pubDate>Wed, 07 Dec 2011 15:13:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.binarytides.com/blog/?p=49#comment-30547</guid>
		<description>missing header files have been included and code updated.</description>
		<content:encoded><![CDATA[<p>missing header files have been included and code updated.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sreeram Vasudevan</title>
		<link>http://www.binarytides.com/blog/c-packet-sniffer-code-with-libpcap-and-linux-sockets-bsd/comment-page-1/#comment-28523</link>
		<dc:creator>Sreeram Vasudevan</dc:creator>
		<pubDate>Thu, 22 Sep 2011 18:16:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.binarytides.com/blog/?p=49#comment-28523</guid>
		<description>Hi All,
I need to sniff a network packet and change the destination address to the malicious website address for the DNS queries by the victim. This needs to be done using pcap library. could anyone please help me out with how can I get the IP packet structure using pcap ?

Thanks !</description>
		<content:encoded><![CDATA[<p>Hi All,<br />
I need to sniff a network packet and change the destination address to the malicious website address for the DNS queries by the victim. This needs to be done using pcap library. could anyone please help me out with how can I get the IP packet structure using pcap ?</p>
<p>Thanks !</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: can u help me to correct the error in printing ip address it is printing in reverse ex:1.1.168.192</title>
		<link>http://www.binarytides.com/blog/c-packet-sniffer-code-with-libpcap-and-linux-sockets-bsd/comment-page-1/#comment-22119</link>
		<dc:creator>can u help me to correct the error in printing ip address it is printing in reverse ex:1.1.168.192</dc:creator>
		<pubDate>Thu, 09 Jun 2011 10:08:52 +0000</pubDate>
		<guid isPermaLink="false">http://www.binarytides.com/blog/?p=49#comment-22119</guid>
		<description>help me to correct my mistake in printing ip address (it is printing in reverse like 1.1.168.192)  i am working in Linux env</description>
		<content:encoded><![CDATA[<p>help me to correct my mistake in printing ip address (it is printing in reverse like 1.1.168.192)  i am working in Linux env</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sysqa</title>
		<link>http://www.binarytides.com/blog/c-packet-sniffer-code-with-libpcap-and-linux-sockets-bsd/comment-page-1/#comment-19281</link>
		<dc:creator>Sysqa</dc:creator>
		<pubDate>Tue, 03 May 2011 11:43:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.binarytides.com/blog/?p=49#comment-19281</guid>
		<description>I made some mistake is PrintData...

Code:
void PrintData (unsigned char *data, int Size)
{
    int i, j;
    for(i = 0 ; i &lt; Size ; i++)
    {
if( i != 0 &amp;&amp; i % 16 == 0)   //if one line of hex printing is complete...
{
    fprintf(logfile, &quot;         &quot;);
    for(j = i - 16 ; j &lt; i ; j++)
    {
if(data[j] &gt;= 32 &amp;&amp; data[j] &lt;= 128)
{
    fprintf(logfile, &quot;%c&quot;, (unsigned char)data[j]); //if its a number or alphabet
}
else
{
    fprintf(logfile, &quot;.&quot;); //otherwise print a dot
}
    }
    fprintf(logfile, &quot;\n&quot;);
}

if(i % 16 == 0)
{
    fprintf(logfile,&quot;   &quot;);
}
fprintf(logfile, &quot; %02X&quot;, (unsigned int)data[i]);

if(i == Size - 1)  //print the last spaces
{
    for(j = 0; j &lt; 15 - i % 16; j++)
    {
fprintf(logfile,&quot;   &quot;); //extra spaces
    }
    fprintf(logfile,&quot;         &quot;);
    for(j = i - i % 16; j &lt;= i; j++)
    {
if(data[j] &gt;= 32 &amp;&amp; data[j] &lt;= 128)
{
    fprintf(logfile, &quot;%c&quot;, (unsigned char)data[j]);
}
else
{
    fprintf(logfile, &quot;.&quot;);
}
    }
    fprintf(logfile, &quot;\n&quot;);
}
    }
}</description>
		<content:encoded><![CDATA[<p>I made some mistake is PrintData&#8230;</p>
<p>Code:<br />
void PrintData (unsigned char *data, int Size)<br />
{<br />
    int i, j;<br />
    for(i = 0 ; i &lt; Size ; i++)<br />
    {<br />
if( i != 0 &amp;&amp; i % 16 == 0)   //if one line of hex printing is complete&#8230;<br />
{<br />
    fprintf(logfile, &#8221;         &#8220;);<br />
    for(j = i &#8211; 16 ; j &lt; i ; j++)<br />
    {<br />
if(data[j] &gt;= 32 &amp;&amp; data[j] &lt;= 128)<br />
{<br />
    fprintf(logfile, &#8220;%c&#8221;, (unsigned char)data[j]); //if its a number or alphabet<br />
}<br />
else<br />
{<br />
    fprintf(logfile, &#8220;.&#8221;); //otherwise print a dot<br />
}<br />
    }<br />
    fprintf(logfile, &#8220;\n&#8221;);<br />
}</p>
<p>if(i % 16 == 0)<br />
{<br />
    fprintf(logfile,&#8221;   &#8220;);<br />
}<br />
fprintf(logfile, &#8221; %02X&#8221;, (unsigned int)data[i]);</p>
<p>if(i == Size &#8211; 1)  //print the last spaces<br />
{<br />
    for(j = 0; j &lt; 15 &#8211; i % 16; j++)<br />
    {<br />
fprintf(logfile,&#8221;   &#8220;); //extra spaces<br />
    }<br />
    fprintf(logfile,&#8221;         &#8220;);<br />
    for(j = i &#8211; i % 16; j &lt;= i; j++)<br />
    {<br />
if(data[j] &gt;= 32 &amp;&amp; data[j] &lt;= 128)<br />
{<br />
    fprintf(logfile, &#8220;%c&#8221;, (unsigned char)data[j]);<br />
}<br />
else<br />
{<br />
    fprintf(logfile, &#8220;.&#8221;);<br />
}<br />
    }<br />
    fprintf(logfile, &#8220;\n&#8221;);<br />
}<br />
    }<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sysqa</title>
		<link>http://www.binarytides.com/blog/c-packet-sniffer-code-with-libpcap-and-linux-sockets-bsd/comment-page-1/#comment-19277</link>
		<dc:creator>Sysqa</dc:creator>
		<pubDate>Tue, 03 May 2011 10:52:33 +0000</pubDate>
		<guid isPermaLink="false">http://www.binarytides.com/blog/?p=49#comment-19277</guid>
		<description>The other thing.
If you ask the user choose a device, why didn&#039;t you type devname in the pcap_open_live function.

Code:
//Ask user which device to sniff
printf(&quot;Enter the number of the device you want to sniff : &quot;);
    scanf(&quot;%d&quot; , &amp;n);
    //devname = *(devs + count - 1);
    devname = *(devs + n);
    printf(&quot;devname: %s\n&quot;,devname);
    //Open the device for sniffing
    printf(&quot;Opening device for sniffing ... \n&quot;);
    //handle = pcap_open_live(&quot;eth0&quot; , 65536 , 1 , 0 , errbuf);
    handle = pcap_open_live(devname , 65536 , 1 , 0 , errbuf);</description>
		<content:encoded><![CDATA[<p>The other thing.<br />
If you ask the user choose a device, why didn&#8217;t you type devname in the pcap_open_live function.</p>
<p>Code:<br />
//Ask user which device to sniff<br />
printf(&#8220;Enter the number of the device you want to sniff : &#8220;);<br />
    scanf(&#8220;%d&#8221; , &amp;n);<br />
    //devname = *(devs + count &#8211; 1);<br />
    devname = *(devs + n);<br />
    printf(&#8220;devname: %s\n&#8221;,devname);<br />
    //Open the device for sniffing<br />
    printf(&#8220;Opening device for sniffing &#8230; \n&#8221;);<br />
    //handle = pcap_open_live(&#8220;eth0&#8243; , 65536 , 1 , 0 , errbuf);<br />
    handle = pcap_open_live(devname , 65536 , 1 , 0 , errbuf);</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sysqa</title>
		<link>http://www.binarytides.com/blog/c-packet-sniffer-code-with-libpcap-and-linux-sockets-bsd/comment-page-1/#comment-19268</link>
		<dc:creator>Sysqa</dc:creator>
		<pubDate>Tue, 03 May 2011 09:32:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.binarytides.com/blog/?p=49#comment-19268</guid>
		<description>The missing includes
pcap.h
stdio.h
string.h //need to memset
net/ethernet.h
netinet/in.h //need inet_ntoa
arpa/inet.h //need inet_ntoa
netinet/ip_icmp.h //Provides declarations for icmp header
netinet/udp.h //Provides declarations for udp header
netinet/tcp.h //Provides declarations for tcp header
netinet/ip.h //Provides declarations for ip header</description>
		<content:encoded><![CDATA[<p>The missing includes<br />
pcap.h<br />
stdio.h<br />
string.h //need to memset<br />
net/ethernet.h<br />
netinet/in.h //need inet_ntoa<br />
arpa/inet.h //need inet_ntoa<br />
netinet/ip_icmp.h //Provides declarations for icmp header<br />
netinet/udp.h //Provides declarations for udp header<br />
netinet/tcp.h //Provides declarations for tcp header<br />
netinet/ip.h //Provides declarations for ip header</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sysqa</title>
		<link>http://www.binarytides.com/blog/c-packet-sniffer-code-with-libpcap-and-linux-sockets-bsd/comment-page-1/#comment-19267</link>
		<dc:creator>Sysqa</dc:creator>
		<pubDate>Tue, 03 May 2011 09:26:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.binarytides.com/blog/?p=49#comment-19267</guid>
		<description>I revised some warning and mistake!

Code:
/*
 Packet sniffer using libpcap library
*/
#include
#include
#include //need to memset
#include
#include  //need inet_ntoa
#include  //need inet_ntoa
#include //Provides declarations for icmp header
#include //Provides declarations for udp header
#include //Provides declarations for tcp header
#include //Provides declarations for ip header

void process_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet);
void process_ip_packet(unsigned char* , int);
void print_ip_header(unsigned char* , int);
//void print_tcp_header(unsigned char* buffer , int size);
void print_tcp_packet(unsigned char* buffer , int size);
//void print_udp_header(unsigned char* , int);
void print_udp_packet(unsigned char* , int);
void print_icmp_packet(unsigned char* , int);
void PrintData (unsigned char* , int);

FILE *logfile;
struct sockaddr_in source,dest;

/*struct sockaddr_in
{
    short   sin_family; // must be AF_INET
    u_short sin_port;
    struct  in_addr sin_addr;
    char    sin_zero[8]; // Not used, must be zero
};*/

int main()
{
    //pcap_if_t alldevsp[100] , *device;
    pcap_if_t *alldevsp, *device;
    pcap_t *handle; //Handle of the device that shall be sniffed

    char errbuf[100] , *devname , **devs;
    int count = 1 , n;
    //First get the list of available devices
    printf(&quot;Finding available devices ... \n&quot;);
    if(pcap_findalldevs(&amp;alldevsp, errbuf))
    {
	printf(&quot;Error finding devices : %s\n&quot; , errbuf);
	//exit(1);
    }
    printf(&quot;Done\n&quot;);
    //Print the available devices
    printf(&quot;\nAvailable Devices are :\n&quot;);
    device = alldevsp;
    while(device != NULL)
    {
	*(devs + count) = device-&gt;name;
	printf(&quot;%d. %s - %s\n&quot;, count++ , device-&gt;name , device-&gt;description);
	device = device-&gt;next;
    }
    //Ask user which device to sniff
    printf(&quot;Enter the number of the device you want to sniff : &quot;);
    scanf(&quot;%d&quot; , &amp;n);
    devname = *(devs + count - 1);
    //Open the device for sniffing
    printf(&quot;Opening device for sniffing ... \n&quot;);
    handle = pcap_open_live(&quot;eth0&quot; , 65536 , 1 , 0 , errbuf);
    if (handle == NULL)
    {
	fprintf(stderr, &quot;Couldn&#039;t open device eth0 : %s\n&quot; , errbuf);
	//exit(1);
    }
    printf(&quot;Done\n&quot;);

    logfile=fopen(&quot;log.txt&quot;,&quot;w&quot;);
    if(logfile==NULL)
    {
	printf(&quot;Unable to create file.\n&quot;);
    }

    //Put the device in sniff loop
    pcap_loop(handle , -1 , process_packet , NULL);
    return 0;
}

void process_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet)
{
    int size = header-&gt;len;
    struct ether_header *ethh;
    ethh = (struct ether_header *)packet;
    //Print the ethernet header in the log file

    if(ntohs(ethh-&gt;ether_type) == ETHERTYPE_IP)
    {
	//process_ip_packet(packet + sizeof *ethh , size - sizeof ethh);
	process_ip_packet((unsigned char*)(packet + sizeof *ethh), size - sizeof ethh);
	printf(&quot;%d\n&quot; , sizeof *ethh);
	fflush(stdout);
    }
    //return 0;
}

void process_ip_packet(unsigned char* buffer, int size)
{
    //Get the IP Header part of this packet
    struct iphdr *iph = (struct iphdr*)buffer;
    switch (iph-&gt;protocol) //Check the Protocol and do accordingly...
    {
	case 1:  //ICMP Protocol
	    print_icmp_packet(buffer , size);
	    break;
	case 2:  //IGMP Protocol
	    break;
	case 6:  //TCP Protocol
	    print_tcp_packet(buffer , size);
	    break;
	case 17: //UDP Protocol
	    print_udp_packet(buffer , size);
	    break;
	default: //Some Other Protocol like ARP etc.
	break;
    }
}

void print_ip_header(unsigned char* Buffer, int Size)
{
    unsigned short iphdrlen;

    struct iphdr *iph = (struct iphdr *)Buffer;
    iphdrlen =iph-&gt;ihl*4;

    memset(&amp;source, 0, sizeof(source));
    //memset(&amp;(source.sin_zero), 0, 8); //other solution
    source.sin_addr.s_addr = iph-&gt;saddr;

    memset(&amp;dest, 0, sizeof(dest));
    //memset(&amp;(dest.sin_zero), 0, 8); //other solution
    dest.sin_addr.s_addr = iph-&gt;daddr;

    fprintf(logfile,&quot;\n&quot;);
    fprintf(logfile,&quot;IP Header\n&quot;);
    fprintf(logfile,&quot;   &#124;-IP Version        : %d\n&quot;,(unsigned int)iph-&gt;version);
    fprintf(logfile,&quot;   &#124;-IP Header Length  : %d DWORDS or %d Bytes\n&quot;,(unsigned int)iph-&gt;ihl,((unsigned int)(iph-&gt;ihl))*4);
    fprintf(logfile,&quot;   &#124;-Type Of Service   : %d\n&quot;,(unsigned int)iph-&gt;tos);
    fprintf(logfile,&quot;   &#124;-IP Total Length   : %d  Bytes(Size of Packet)\n&quot;,ntohs(iph-&gt;tot_len));
    fprintf(logfile,&quot;   &#124;-Identification    : %d\n&quot;,ntohs(iph-&gt;id));
    //fprintf(logfile,&quot;   &#124;-Reserved ZERO Field   : %d\n&quot;,(unsigned int)iphdr-&gt;ip_reserved_zero);
    //fprintf(logfile,&quot;   &#124;-Dont Fragment Field   : %d\n&quot;,(unsigned int)iphdr-&gt;ip_dont_fragment);
    //fprintf(logfile,&quot;   &#124;-More Fragment Field   : %d\n&quot;,(unsigned int)iphdr-&gt;ip_more_fragment);
    fprintf(logfile,&quot;   &#124;-TTL      : %d\n&quot;,(unsigned int)iph-&gt;ttl);
    fprintf(logfile,&quot;   &#124;-Protocol : %d\n&quot;,(unsigned int)iph-&gt;protocol);
    fprintf(logfile,&quot;   &#124;-Checksum : %d\n&quot;,ntohs(iph-&gt;check));
    fprintf(logfile,&quot;   &#124;-Source IP        : %s\n&quot;,inet_ntoa(source.sin_addr));
    fprintf(logfile,&quot;   &#124;-Destination IP   : %s\n&quot;,inet_ntoa(dest.sin_addr));
}

void print_tcp_packet(unsigned char* Buffer, int Size)
{
    unsigned short iphdrlen;

    struct iphdr *iph = (struct iphdr *)Buffer;
    iphdrlen = iph-&gt;ihl*4;

    struct tcphdr *tcph=(struct tcphdr*)(Buffer + iphdrlen);

    fprintf(logfile,&quot;\n\n***********************TCP Packet*************************\n&quot;);

    print_ip_header(Buffer,Size);

    fprintf(logfile,&quot;\n&quot;);
    fprintf(logfile,&quot;TCP Header\n&quot;);
    fprintf(logfile,&quot;   &#124;-Source Port      : %u\n&quot;,ntohs(tcph-&gt;source));
    fprintf(logfile,&quot;   &#124;-Destination Port : %u\n&quot;,ntohs(tcph-&gt;dest));
    fprintf(logfile,&quot;   &#124;-Sequence Number    : %u\n&quot;,ntohl(tcph-&gt;seq));
    fprintf(logfile,&quot;   &#124;-Acknowledge Number : %u\n&quot;,ntohl(tcph-&gt;ack_seq));
    fprintf(logfile,&quot;   &#124;-Header Length      : %d DWORDS or %d BYTES\n&quot; ,(unsigned int)tcph-&gt;doff,(unsigned int)tcph-&gt;doff*4);
    //fprintf(logfile,&quot;   &#124;-CWR Flag : %d\n&quot;,(unsigned int)tcph-&gt;cwr);
    //fprintf(logfile,&quot;   &#124;-ECN Flag : %d\n&quot;,(unsigned int)tcph-&gt;ece);
    fprintf(logfile,&quot;   &#124;-Urgent Flag          : %d\n&quot;,(unsigned int)tcph-&gt;urg);
    fprintf(logfile,&quot;   &#124;-Acknowledgement Flag : %d\n&quot;,(unsigned int)tcph-&gt;ack);
    fprintf(logfile,&quot;   &#124;-Push Flag            : %d\n&quot;,(unsigned int)tcph-&gt;psh);
    fprintf(logfile,&quot;   &#124;-Reset Flag           : %d\n&quot;,(unsigned int)tcph-&gt;rst);
    fprintf(logfile,&quot;   &#124;-Synchronise Flag     : %d\n&quot;,(unsigned int)tcph-&gt;syn);
    fprintf(logfile,&quot;   &#124;-Finish Flag          : %d\n&quot;,(unsigned int)tcph-&gt;fin);
    fprintf(logfile,&quot;   &#124;-Window         : %d\n&quot;,ntohs(tcph-&gt;window));
    fprintf(logfile,&quot;   &#124;-Checksum       : %d\n&quot;,ntohs(tcph-&gt;check));
    fprintf(logfile,&quot;   &#124;-Urgent Pointer : %d\n&quot;,tcph-&gt;urg_ptr);
    fprintf(logfile,&quot;\n&quot;);
    fprintf(logfile,&quot;                        DATA Dump                         &quot;);
    fprintf(logfile,&quot;\n&quot;);

    fprintf(logfile,&quot;IP Header\n&quot;);
    PrintData(Buffer,iphdrlen);

    fprintf(logfile,&quot;TCP Header\n&quot;);
    PrintData(Buffer+iphdrlen,tcph-&gt;doff*4);

    fprintf(logfile,&quot;Data Payload\n&quot;);
    PrintData(Buffer + iphdrlen + tcph-&gt;doff*4 , (Size - tcph-&gt;doff*4-iph-&gt;ihl*4) );

    fprintf(logfile,&quot;\n###########################################################&quot;);
}

void print_udp_packet(unsigned char *Buffer , int Size)
{
    unsigned short iphdrlen;

    struct iphdr *iph = (struct iphdr *)Buffer;
    iphdrlen = iph-&gt;ihl*4;

    struct udphdr *udph = (struct udphdr*)(Buffer + iphdrlen);

    fprintf(logfile,&quot;\n\n***********************UDP Packet*************************\n&quot;);

    print_ip_header(Buffer,Size);  

    fprintf(logfile,&quot;\nUDP Header\n&quot;);
    fprintf(logfile,&quot;   &#124;-Source Port      : %d\n&quot; , ntohs(udph-&gt;source));
    fprintf(logfile,&quot;   &#124;-Destination Port : %d\n&quot; , ntohs(udph-&gt;dest));
    fprintf(logfile,&quot;   &#124;-UDP Length       : %d\n&quot; , ntohs(udph-&gt;len));
    fprintf(logfile,&quot;   &#124;-UDP Checksum     : %d\n&quot; , ntohs(udph-&gt;check));

    fprintf(logfile,&quot;\n&quot;);
    fprintf(logfile,&quot;IP Header\n&quot;);
    PrintData(Buffer , iphdrlen);

    fprintf(logfile,&quot;UDP Header\n&quot;);
    PrintData(Buffer+iphdrlen , sizeof udph);

    fprintf(logfile,&quot;Data Payload\n&quot;);
    PrintData(Buffer + iphdrlen + sizeof udph ,( Size - sizeof udph - iph-&gt;ihl * 4 ));

    fprintf(logfile,&quot;\n###########################################################&quot;);
}

void print_icmp_packet(unsigned char* Buffer , int Size)
{
    unsigned short iphdrlen;

    struct iphdr *iph = (struct iphdr *)Buffer;
    iphdrlen = iph-&gt;ihl*4;

    struct icmphdr *icmph = (struct icmphdr *)(Buffer + iphdrlen);

    fprintf(logfile,&quot;\n\n***********************ICMP Packet*************************\n&quot;);

    print_ip_header(Buffer , Size);

    fprintf(logfile,&quot;\n&quot;);

    fprintf(logfile,&quot;ICMP Header\n&quot;);
    fprintf(logfile,&quot;   &#124;-Type : %d&quot;,(unsigned int)(icmph-&gt;type));

    if((unsigned int)(icmph-&gt;type) == 11)
    {
	fprintf(logfile,&quot;  (TTL Expired)\n&quot;);
    }
    else if((unsigned int)(icmph-&gt;type) == ICMP_ECHOREPLY)
    {
	fprintf(logfile,&quot;  (ICMP Echo Reply)\n&quot;);
    }
    else
    {
	fprintf(logfile,&quot;\n&quot;);
    }
    fprintf(logfile,&quot;   &#124;-Code : %d\n&quot;,(unsigned int)(icmph-&gt;code));
    fprintf(logfile,&quot;   &#124;-Checksum : %d\n&quot;,ntohs(icmph-&gt;checksum));
    //fprintf(logfile,&quot;   &#124;-ID       : %d\n&quot;,ntohs(icmph-&gt;id));
    //fprintf(logfile,&quot;   &#124;-Sequence : %d\n&quot;,ntohs(icmph-&gt;sequence));
    fprintf(logfile,&quot;\n&quot;);

    fprintf(logfile,&quot;IP Header\n&quot;);
    PrintData(Buffer,iphdrlen);

    fprintf(logfile,&quot;UDP Header\n&quot;);
    PrintData(Buffer + iphdrlen , sizeof icmph);

    fprintf(logfile,&quot;Data Payload\n&quot;);
    PrintData(Buffer + iphdrlen + sizeof icmph , (Size - sizeof icmph - iph-&gt;ihl * 4));

    fprintf(logfile,&quot;\n###########################################################&quot;);
}

void PrintData (unsigned char* data , int Size)
{
    int i,j;
    for(i=0 ; i &lt; Size ; i++)
    {
	if( i!=0 &amp;&amp; i%16==0)   //if one line of hex printing is complete...
	{
	    fprintf(logfile,&quot;         &quot;);
	    for(j=i-16 ; j&lt;i&gt;=32 &amp;&amp; data[j]&lt;=128)
		{
		    fprintf(logfile,&quot;%c&quot;,(unsigned char)data[j]); //if its a number or alphabet
		}
		else
		{
		    fprintf(logfile,&quot;.&quot;); //otherwise print a dot
		}
	    }
	    fprintf(logfile,&quot;\n&quot;);
	}

	if(i%16==0) fprintf(logfile,&quot;   &quot;);
	{
	    fprintf(logfile,&quot; %02X&quot;,(unsigned int)data[i]);
	}

	if( i==Size-1)  //print the last spaces
	{
	    for(j=0;j&lt;15-i%16;j++) fprintf(logfile,&quot;   &quot;); //extra spaces
	    {
		fprintf(logfile,&quot;         &quot;);
	    }
	    for(j=i-i%16 ; j=32 &amp;&amp; data[j]&lt;=128)
		{
		    fprintf(logfile,&quot;%c&quot;,(unsigned char)data[j]);
		}
		else
		{
		    fprintf(logfile,&quot;.&quot;);
		}
	    }
	    fprintf(logfile,&quot;\n&quot;);
	}
    }
}</description>
		<content:encoded><![CDATA[<p>I revised some warning and mistake!</p>
<p>Code:<br />
/*<br />
 Packet sniffer using libpcap library<br />
*/<br />
#include<br />
#include<br />
#include //need to memset<br />
#include<br />
#include  //need inet_ntoa<br />
#include  //need inet_ntoa<br />
#include //Provides declarations for icmp header<br />
#include //Provides declarations for udp header<br />
#include //Provides declarations for tcp header<br />
#include //Provides declarations for ip header</p>
<p>void process_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet);<br />
void process_ip_packet(unsigned char* , int);<br />
void print_ip_header(unsigned char* , int);<br />
//void print_tcp_header(unsigned char* buffer , int size);<br />
void print_tcp_packet(unsigned char* buffer , int size);<br />
//void print_udp_header(unsigned char* , int);<br />
void print_udp_packet(unsigned char* , int);<br />
void print_icmp_packet(unsigned char* , int);<br />
void PrintData (unsigned char* , int);</p>
<p>FILE *logfile;<br />
struct sockaddr_in source,dest;</p>
<p>/*struct sockaddr_in<br />
{<br />
    short   sin_family; // must be AF_INET<br />
    u_short sin_port;<br />
    struct  in_addr sin_addr;<br />
    char    sin_zero[8]; // Not used, must be zero<br />
};*/</p>
<p>int main()<br />
{<br />
    //pcap_if_t alldevsp[100] , *device;<br />
    pcap_if_t *alldevsp, *device;<br />
    pcap_t *handle; //Handle of the device that shall be sniffed</p>
<p>    char errbuf[100] , *devname , **devs;<br />
    int count = 1 , n;<br />
    //First get the list of available devices<br />
    printf(&#8220;Finding available devices &#8230; \n&#8221;);<br />
    if(pcap_findalldevs(&amp;alldevsp, errbuf))<br />
    {<br />
	printf(&#8220;Error finding devices : %s\n&#8221; , errbuf);<br />
	//exit(1);<br />
    }<br />
    printf(&#8220;Done\n&#8221;);<br />
    //Print the available devices<br />
    printf(&#8220;\nAvailable Devices are :\n&#8221;);<br />
    device = alldevsp;<br />
    while(device != NULL)<br />
    {<br />
	*(devs + count) = device-&gt;name;<br />
	printf(&#8220;%d. %s &#8211; %s\n&#8221;, count++ , device-&gt;name , device-&gt;description);<br />
	device = device-&gt;next;<br />
    }<br />
    //Ask user which device to sniff<br />
    printf(&#8220;Enter the number of the device you want to sniff : &#8220;);<br />
    scanf(&#8220;%d&#8221; , &amp;n);<br />
    devname = *(devs + count &#8211; 1);<br />
    //Open the device for sniffing<br />
    printf(&#8220;Opening device for sniffing &#8230; \n&#8221;);<br />
    handle = pcap_open_live(&#8220;eth0&#8243; , 65536 , 1 , 0 , errbuf);<br />
    if (handle == NULL)<br />
    {<br />
	fprintf(stderr, &#8220;Couldn&#8217;t open device eth0 : %s\n&#8221; , errbuf);<br />
	//exit(1);<br />
    }<br />
    printf(&#8220;Done\n&#8221;);</p>
<p>    logfile=fopen(&#8220;log.txt&#8221;,&#8221;w&#8221;);<br />
    if(logfile==NULL)<br />
    {<br />
	printf(&#8220;Unable to create file.\n&#8221;);<br />
    }</p>
<p>    //Put the device in sniff loop<br />
    pcap_loop(handle , -1 , process_packet , NULL);<br />
    return 0;<br />
}</p>
<p>void process_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet)<br />
{<br />
    int size = header-&gt;len;<br />
    struct ether_header *ethh;<br />
    ethh = (struct ether_header *)packet;<br />
    //Print the ethernet header in the log file</p>
<p>    if(ntohs(ethh-&gt;ether_type) == ETHERTYPE_IP)<br />
    {<br />
	//process_ip_packet(packet + sizeof *ethh , size &#8211; sizeof ethh);<br />
	process_ip_packet((unsigned char*)(packet + sizeof *ethh), size &#8211; sizeof ethh);<br />
	printf(&#8220;%d\n&#8221; , sizeof *ethh);<br />
	fflush(stdout);<br />
    }<br />
    //return 0;<br />
}</p>
<p>void process_ip_packet(unsigned char* buffer, int size)<br />
{<br />
    //Get the IP Header part of this packet<br />
    struct iphdr *iph = (struct iphdr*)buffer;<br />
    switch (iph-&gt;protocol) //Check the Protocol and do accordingly&#8230;<br />
    {<br />
	case 1:  //ICMP Protocol<br />
	    print_icmp_packet(buffer , size);<br />
	    break;<br />
	case 2:  //IGMP Protocol<br />
	    break;<br />
	case 6:  //TCP Protocol<br />
	    print_tcp_packet(buffer , size);<br />
	    break;<br />
	case 17: //UDP Protocol<br />
	    print_udp_packet(buffer , size);<br />
	    break;<br />
	default: //Some Other Protocol like ARP etc.<br />
	break;<br />
    }<br />
}</p>
<p>void print_ip_header(unsigned char* Buffer, int Size)<br />
{<br />
    unsigned short iphdrlen;</p>
<p>    struct iphdr *iph = (struct iphdr *)Buffer;<br />
    iphdrlen =iph-&gt;ihl*4;</p>
<p>    memset(&amp;source, 0, sizeof(source));<br />
    //memset(&amp;(source.sin_zero), 0, 8); //other solution<br />
    source.sin_addr.s_addr = iph-&gt;saddr;</p>
<p>    memset(&amp;dest, 0, sizeof(dest));<br />
    //memset(&amp;(dest.sin_zero), 0, 8); //other solution<br />
    dest.sin_addr.s_addr = iph-&gt;daddr;</p>
<p>    fprintf(logfile,&#8221;\n&#8221;);<br />
    fprintf(logfile,&#8221;IP Header\n&#8221;);<br />
    fprintf(logfile,&#8221;   |-IP Version        : %d\n&#8221;,(unsigned int)iph-&gt;version);<br />
    fprintf(logfile,&#8221;   |-IP Header Length  : %d DWORDS or %d Bytes\n&#8221;,(unsigned int)iph-&gt;ihl,((unsigned int)(iph-&gt;ihl))*4);<br />
    fprintf(logfile,&#8221;   |-Type Of Service   : %d\n&#8221;,(unsigned int)iph-&gt;tos);<br />
    fprintf(logfile,&#8221;   |-IP Total Length   : %d  Bytes(Size of Packet)\n&#8221;,ntohs(iph-&gt;tot_len));<br />
    fprintf(logfile,&#8221;   |-Identification    : %d\n&#8221;,ntohs(iph-&gt;id));<br />
    //fprintf(logfile,&#8221;   |-Reserved ZERO Field   : %d\n&#8221;,(unsigned int)iphdr-&gt;ip_reserved_zero);<br />
    //fprintf(logfile,&#8221;   |-Dont Fragment Field   : %d\n&#8221;,(unsigned int)iphdr-&gt;ip_dont_fragment);<br />
    //fprintf(logfile,&#8221;   |-More Fragment Field   : %d\n&#8221;,(unsigned int)iphdr-&gt;ip_more_fragment);<br />
    fprintf(logfile,&#8221;   |-TTL      : %d\n&#8221;,(unsigned int)iph-&gt;ttl);<br />
    fprintf(logfile,&#8221;   |-Protocol : %d\n&#8221;,(unsigned int)iph-&gt;protocol);<br />
    fprintf(logfile,&#8221;   |-Checksum : %d\n&#8221;,ntohs(iph-&gt;check));<br />
    fprintf(logfile,&#8221;   |-Source IP        : %s\n&#8221;,inet_ntoa(source.sin_addr));<br />
    fprintf(logfile,&#8221;   |-Destination IP   : %s\n&#8221;,inet_ntoa(dest.sin_addr));<br />
}</p>
<p>void print_tcp_packet(unsigned char* Buffer, int Size)<br />
{<br />
    unsigned short iphdrlen;</p>
<p>    struct iphdr *iph = (struct iphdr *)Buffer;<br />
    iphdrlen = iph-&gt;ihl*4;</p>
<p>    struct tcphdr *tcph=(struct tcphdr*)(Buffer + iphdrlen);</p>
<p>    fprintf(logfile,&#8221;\n\n***********************TCP Packet*************************\n&#8221;);</p>
<p>    print_ip_header(Buffer,Size);</p>
<p>    fprintf(logfile,&#8221;\n&#8221;);<br />
    fprintf(logfile,&#8221;TCP Header\n&#8221;);<br />
    fprintf(logfile,&#8221;   |-Source Port      : %u\n&#8221;,ntohs(tcph-&gt;source));<br />
    fprintf(logfile,&#8221;   |-Destination Port : %u\n&#8221;,ntohs(tcph-&gt;dest));<br />
    fprintf(logfile,&#8221;   |-Sequence Number    : %u\n&#8221;,ntohl(tcph-&gt;seq));<br />
    fprintf(logfile,&#8221;   |-Acknowledge Number : %u\n&#8221;,ntohl(tcph-&gt;ack_seq));<br />
    fprintf(logfile,&#8221;   |-Header Length      : %d DWORDS or %d BYTES\n&#8221; ,(unsigned int)tcph-&gt;doff,(unsigned int)tcph-&gt;doff*4);<br />
    //fprintf(logfile,&#8221;   |-CWR Flag : %d\n&#8221;,(unsigned int)tcph-&gt;cwr);<br />
    //fprintf(logfile,&#8221;   |-ECN Flag : %d\n&#8221;,(unsigned int)tcph-&gt;ece);<br />
    fprintf(logfile,&#8221;   |-Urgent Flag          : %d\n&#8221;,(unsigned int)tcph-&gt;urg);<br />
    fprintf(logfile,&#8221;   |-Acknowledgement Flag : %d\n&#8221;,(unsigned int)tcph-&gt;ack);<br />
    fprintf(logfile,&#8221;   |-Push Flag            : %d\n&#8221;,(unsigned int)tcph-&gt;psh);<br />
    fprintf(logfile,&#8221;   |-Reset Flag           : %d\n&#8221;,(unsigned int)tcph-&gt;rst);<br />
    fprintf(logfile,&#8221;   |-Synchronise Flag     : %d\n&#8221;,(unsigned int)tcph-&gt;syn);<br />
    fprintf(logfile,&#8221;   |-Finish Flag          : %d\n&#8221;,(unsigned int)tcph-&gt;fin);<br />
    fprintf(logfile,&#8221;   |-Window         : %d\n&#8221;,ntohs(tcph-&gt;window));<br />
    fprintf(logfile,&#8221;   |-Checksum       : %d\n&#8221;,ntohs(tcph-&gt;check));<br />
    fprintf(logfile,&#8221;   |-Urgent Pointer : %d\n&#8221;,tcph-&gt;urg_ptr);<br />
    fprintf(logfile,&#8221;\n&#8221;);<br />
    fprintf(logfile,&#8221;                        DATA Dump                         &#8220;);<br />
    fprintf(logfile,&#8221;\n&#8221;);</p>
<p>    fprintf(logfile,&#8221;IP Header\n&#8221;);<br />
    PrintData(Buffer,iphdrlen);</p>
<p>    fprintf(logfile,&#8221;TCP Header\n&#8221;);<br />
    PrintData(Buffer+iphdrlen,tcph-&gt;doff*4);</p>
<p>    fprintf(logfile,&#8221;Data Payload\n&#8221;);<br />
    PrintData(Buffer + iphdrlen + tcph-&gt;doff*4 , (Size &#8211; tcph-&gt;doff*4-iph-&gt;ihl*4) );</p>
<p>    fprintf(logfile,&#8221;\n###########################################################&#8221;);<br />
}</p>
<p>void print_udp_packet(unsigned char *Buffer , int Size)<br />
{<br />
    unsigned short iphdrlen;</p>
<p>    struct iphdr *iph = (struct iphdr *)Buffer;<br />
    iphdrlen = iph-&gt;ihl*4;</p>
<p>    struct udphdr *udph = (struct udphdr*)(Buffer + iphdrlen);</p>
<p>    fprintf(logfile,&#8221;\n\n***********************UDP Packet*************************\n&#8221;);</p>
<p>    print_ip_header(Buffer,Size);  </p>
<p>    fprintf(logfile,&#8221;\nUDP Header\n&#8221;);<br />
    fprintf(logfile,&#8221;   |-Source Port      : %d\n&#8221; , ntohs(udph-&gt;source));<br />
    fprintf(logfile,&#8221;   |-Destination Port : %d\n&#8221; , ntohs(udph-&gt;dest));<br />
    fprintf(logfile,&#8221;   |-UDP Length       : %d\n&#8221; , ntohs(udph-&gt;len));<br />
    fprintf(logfile,&#8221;   |-UDP Checksum     : %d\n&#8221; , ntohs(udph-&gt;check));</p>
<p>    fprintf(logfile,&#8221;\n&#8221;);<br />
    fprintf(logfile,&#8221;IP Header\n&#8221;);<br />
    PrintData(Buffer , iphdrlen);</p>
<p>    fprintf(logfile,&#8221;UDP Header\n&#8221;);<br />
    PrintData(Buffer+iphdrlen , sizeof udph);</p>
<p>    fprintf(logfile,&#8221;Data Payload\n&#8221;);<br />
    PrintData(Buffer + iphdrlen + sizeof udph ,( Size &#8211; sizeof udph &#8211; iph-&gt;ihl * 4 ));</p>
<p>    fprintf(logfile,&#8221;\n###########################################################&#8221;);<br />
}</p>
<p>void print_icmp_packet(unsigned char* Buffer , int Size)<br />
{<br />
    unsigned short iphdrlen;</p>
<p>    struct iphdr *iph = (struct iphdr *)Buffer;<br />
    iphdrlen = iph-&gt;ihl*4;</p>
<p>    struct icmphdr *icmph = (struct icmphdr *)(Buffer + iphdrlen);</p>
<p>    fprintf(logfile,&#8221;\n\n***********************ICMP Packet*************************\n&#8221;);</p>
<p>    print_ip_header(Buffer , Size);</p>
<p>    fprintf(logfile,&#8221;\n&#8221;);</p>
<p>    fprintf(logfile,&#8221;ICMP Header\n&#8221;);<br />
    fprintf(logfile,&#8221;   |-Type : %d&#8221;,(unsigned int)(icmph-&gt;type));</p>
<p>    if((unsigned int)(icmph-&gt;type) == 11)<br />
    {<br />
	fprintf(logfile,&#8221;  (TTL Expired)\n&#8221;);<br />
    }<br />
    else if((unsigned int)(icmph-&gt;type) == ICMP_ECHOREPLY)<br />
    {<br />
	fprintf(logfile,&#8221;  (ICMP Echo Reply)\n&#8221;);<br />
    }<br />
    else<br />
    {<br />
	fprintf(logfile,&#8221;\n&#8221;);<br />
    }<br />
    fprintf(logfile,&#8221;   |-Code : %d\n&#8221;,(unsigned int)(icmph-&gt;code));<br />
    fprintf(logfile,&#8221;   |-Checksum : %d\n&#8221;,ntohs(icmph-&gt;checksum));<br />
    //fprintf(logfile,&#8221;   |-ID       : %d\n&#8221;,ntohs(icmph-&gt;id));<br />
    //fprintf(logfile,&#8221;   |-Sequence : %d\n&#8221;,ntohs(icmph-&gt;sequence));<br />
    fprintf(logfile,&#8221;\n&#8221;);</p>
<p>    fprintf(logfile,&#8221;IP Header\n&#8221;);<br />
    PrintData(Buffer,iphdrlen);</p>
<p>    fprintf(logfile,&#8221;UDP Header\n&#8221;);<br />
    PrintData(Buffer + iphdrlen , sizeof icmph);</p>
<p>    fprintf(logfile,&#8221;Data Payload\n&#8221;);<br />
    PrintData(Buffer + iphdrlen + sizeof icmph , (Size &#8211; sizeof icmph &#8211; iph-&gt;ihl * 4));</p>
<p>    fprintf(logfile,&#8221;\n###########################################################&#8221;);<br />
}</p>
<p>void PrintData (unsigned char* data , int Size)<br />
{<br />
    int i,j;<br />
    for(i=0 ; i &lt; Size ; i++)<br />
    {<br />
	if( i!=0 &amp;&amp; i%16==0)   //if one line of hex printing is complete&#8230;<br />
	{<br />
	    fprintf(logfile,&quot;         &quot;);<br />
	    for(j=i-16 ; j<i>=32 &amp;&amp; data[j]&lt;=128)<br />
		{<br />
		    fprintf(logfile,&quot;%c&quot;,(unsigned char)data[j]); //if its a number or alphabet<br />
		}<br />
		else<br />
		{<br />
		    fprintf(logfile,&quot;.&quot;); //otherwise print a dot<br />
		}<br />
	    }<br />
	    fprintf(logfile,&quot;\n&quot;);<br />
	}</p>
<p>	if(i%16==0) fprintf(logfile,&quot;   &quot;);<br />
	{<br />
	    fprintf(logfile,&quot; %02X&quot;,(unsigned int)data[i]);<br />
	}</p>
<p>	if( i==Size-1)  //print the last spaces<br />
	{<br />
	    for(j=0;j&lt;15-i%16;j++) fprintf(logfile,&quot;   &quot;); //extra spaces<br />
	    {<br />
		fprintf(logfile,&quot;         &quot;);<br />
	    }<br />
	    for(j=i-i%16 ; j=32 &amp;&amp; data[j]&lt;=128)<br />
		{<br />
		    fprintf(logfile,&quot;%c&quot;,(unsigned char)data[j]);<br />
		}<br />
		else<br />
		{<br />
		    fprintf(logfile,&quot;.&quot;);<br />
		}<br />
	    }<br />
	    fprintf(logfile,&quot;\n&quot;);<br />
	}<br />
    }<br />
}</i></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tanmai - Coco D</title>
		<link>http://www.binarytides.com/blog/c-packet-sniffer-code-with-libpcap-and-linux-sockets-bsd/comment-page-1/#comment-141</link>
		<dc:creator>Tanmai - Coco D</dc:creator>
		<pubDate>Thu, 28 May 2009 07:09:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.binarytides.com/blog/?p=49#comment-141</guid>
		<description>Thanks a lot. I&#039;ve just started out with libpcap, so this serves as a good intro.</description>
		<content:encoded><![CDATA[<p>Thanks a lot. I&#8217;ve just started out with libpcap, so this serves as a good intro.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

