VB icon

Packet sniffer

Email
Submitted on: 1/3/2015 1:17:00 PM
By: ffbbee (from psc cd)  
Level: Beginner
User Rating: By 3 Users
Compatibility: C
Views: 1131
 
     This code shows the source ip and port the flags of the packet and the sequence number
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Packet sniffer
// Description:This code shows the source ip and port the flags of the packet and the sequence number
// By: ffbbee (from psc cd)
//**************************************

#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
int main(int argc, char *argv[])
{
int s, bytes,a=0,sy=0,f=0,r=0,u=0,p=0;
	int ac=0,syc=0,fc=0,rc=0;
struct tcphdr *tcp;
struct iphdr*ip;
struct in_addr addr;
charbuffer[4000];
s = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
if (s == -1)
{
perror("socket() failed");
return 1;
}
ip = (struct iphdr*) buffer;
tcp = (struct tcphdr*) (buffer + sizeof(struct iphdr));
while( (bytes = recv(s, buffer, sizeof(buffer), 0)) > 0)
{
addr.s_addr = ip->saddr;
		a=ntohs(tcp->ack);
		sy=ntohs(tcp->syn); 
		r=ntohs(tcp->rst);
		f=ntohs(tcp->fin);	
		p=ntohs(tcp->psh);
		u=ntohs(tcp->urg);
	if (ip->saddr!=inet_addr("192.168.0.113"))
	{	
	printf("Packet from source:%s\nwith flags ->",inet_ntoa(addr));
	if(a==256) 
	{
	printf("Ack ");
	}
	if ( sy==256)
	{printf("Syn ");
	;}
	if (f==256)
	{
	printf("Fin ");	
	}
	if (r==256)
	{
	printf("Rst ");
	}
	if (p==256)
	{
	printf("Psh ");
	}
	if (u==256)
	{
	printf("Urg");
	}
	printf("\n");
	printf("With the sequence number of ->%i\n",ntohl(tcp->seq));
	printf("From port->%i\n\n",ntohs(tcp->source));
	}
}
if (bytes == -1)
{
perror("recv() failed");
return 2;
}
return 0;
	
}


Other 3 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.