LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-15-2009, 12:49 AM   #1
Mridulj
Member
 
Registered: Jan 2008
Posts: 49

Rep: Reputation: 15
Using C and libpcap in linux


I want to build a internet traffic pattern analyser
using C and libpcap in linux . can anyone suggest how should
I proceed .
 
Old 04-15-2009, 01:20 AM   #2
pramod.srk
Member
 
Registered: Feb 2009
Posts: 47

Rep: Reputation: 15
Use pcap library ..
 
Old 04-15-2009, 11:17 PM   #3
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 270

Rep: Reputation: 33
Quote:
Originally Posted by Mridulj View Post
I want to build a internet traffic pattern analyser
what analysis you want to do?
 
Old 04-16-2009, 12:11 PM   #4
Mridulj
Member
 
Registered: Jan 2008
Posts: 49

Original Poster
Rep: Reputation: 15
packet analysis when you are connected to the internet
 
Old 04-16-2009, 11:09 PM   #5
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 270

Rep: Reputation: 33
dear mridul,

it is very obvious that any body will do only and only packet analysis. Because other than packets there will be nothing in the network.

What I am asking is . . .

what is your objective?
What information you want to consume from the packets?
What precisely you will be analyzing with respect to the collected packets?
 
Old 04-18-2009, 05:00 AM   #6
Mridulj
Member
 
Registered: Jan 2008
Posts: 49

Original Poster
Rep: Reputation: 15
we should be able to measure packet traffic going out and coming when I
connect to a network .
 
Old 04-18-2009, 11:22 PM   #7
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 270

Rep: Reputation: 33
Is it that, you want to measure the number of packets per unit time?
 
Old 04-19-2009, 01:39 PM   #8
Mridulj
Member
 
Registered: Jan 2008
Posts: 49

Original Poster
Rep: Reputation: 15
ya ..........

say ... no.of packets , wats the protocol used , length of the packet destination IP , etc .
 
Old 04-19-2009, 11:13 PM   #9
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 270

Rep: Reputation: 33
To me it seems to be homework. Doesn't matter it is or not, I will give only hints; not answers.

hint-1:

did you refered this
 
Old 04-20-2009, 01:58 AM   #10
Mridulj
Member
 
Registered: Jan 2008
Posts: 49

Original Poster
Rep: Reputation: 15
hav pcap installed in my pc

nxt Hint 2: ??
 
Old 04-20-2009, 11:14 PM   #11
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 270

Rep: Reputation: 33
hint - 2:
with the help of link I gave, in the hint-1, write a program that captures the traffic.
 
Old 04-22-2009, 04:18 AM   #12
Mridulj
Member
 
Registered: Jan 2008
Posts: 49

Original Poster
Rep: Reputation: 15
#include <stdio.h>
#include <stdlib.h>
#include <pcap.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/if_ether.h>

int main(int argc, char **argv)
{
int i;
char *dev;
char errbuf[PCAP_ERRBUF_SIZE];
pcap_t* descr;
const u_char *packet;
struct pcap_pkthdr hdr;
struct ether_header *eptr;

u_char *ptr;
dev = pcap_lookupdev(errbuf);

if(dev == NULL)
{
printf("%s\n",errbuf);
exit(1);
}

printf("DEV: %s\n",dev);

descr = pcap_open_live(dev,BUFSIZ,0,-1,errbuf);

if(descr == NULL)
{
printf("pcap_open_live(): %s\n",errbuf);
exit(1);
}

packet = pcap_next(descr,&hdr);

if(packet == NULL)
{
printf("Didn't grab packet\n");
exit(1);
}

printf("Grabbed packet of length %d\n",hdr.len);
printf("Recieved at ..... %s\n",ctime((consttime_t*)&hdr.ts.tv_sec));
printf("Ethernet address length is %d\n",ETHER_HDR_LEN);

eptr = (struct ether_header *) packet;

if (ntohs (eptr->ether_type) == ETHERTYPE_IP)
{
printf("Ethernet type hex:%x dec:%d is an IP packet\n",
ntohs(eptr->ether_type),
ntohs(eptr->ether_type));
}else if (ntohs (eptr->ether_type) == ETHERTYPE_ARP)
{
printf("Ethernet type hex:%x dec:%d is an ARP packet\n",
ntohs(eptr->ether_type),
ntohs(eptr->ether_type));
}else {
printf("Ethernet type %x not IP", ntohs(eptr->ether_type));
exit(1);
}

ptr = eptr->ether_dhost;
i = ETHER_ADDR_LEN;
printf(" Destination Address: ");
do{
printf("%s%x",(i == ETHER_ADDR_LEN) ? " " : ":",*ptr++);
}while(--i>0);
printf("\n");

ptr = eptr->ether_shost;
i = ETHER_ADDR_LEN;
printf(" Source Address: ");
do{
printf("%s%x",(i == ETHER_ADDR_LEN) ? " " : ":",*ptr++);
}while(--i>0);
printf("\n");

return 0;
}
 
Old 04-23-2009, 03:46 AM   #13
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 270

Rep: Reputation: 33
I didn't tested your code. Any way you do it on your own.

hint - 3:
now get in to "rfc 791".
 
  


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
TCP Reconstruction(C ,libpcap,linux) kalps Programming 5 03-25-2009 11:16 PM
libnet libpcap on linux filomotta Linux - Networking 1 06-01-2005 01:03 PM
problem with libnet/libpcap on Linux filomotta Programming 0 05-31-2005 07:49 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:59 AM.

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
Open Source Consulting | Domain Registration