LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 07-13-2008, 11:40 AM   #1
tanmoy_tiger
LQ Newbie
 
Registered: Jun 2008
Posts: 6

Rep: Reputation: 0
Red face libpcap programming problem


hi,i am trying write a libpcap program as follow:


/*Sniffer program.Program name is appdev.c*/

#include<stdio.h>
#include<pcap.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<netinet/in.h>
void process_packet(u_char *arg,struct pcap_pkthdr *header,u_char *packet)
{
printf("\npacket is captured");
}
int main()
{
char *dev;
char errbuf[PCAP_ERRBUF_SIZE];
bpf_u_int32 mask,net;
struct bpf_program fp;
char filter[]="ip";
pcap_t *p;
struct in_addr addr;
dev=pcap_lookupdev(errbuf);
if(dev==NULL)
{
printf("\nError:%s",errbuf);
exit(1);
}
else
printf("\nDEVICE:%s",dev);
if(pcap_lookupnet(dev,&net,&mask,errbuf)==-1)
{
printf("\nError:%s",errbuf);
exit(1);
}
else
{
addr.s_addr=net;
printf("\nNet:%s",inet_ntoa(addr));
addr.s_addr=mask;
printf("\nMask:%s",inet_ntoa(addr));
}
p=pcap_open_live(dev,60000,0,2000,errbuf);
if(p==NULL)
{
printf("\nError:%s",errbuf);
exit(1);
}
else
printf("\nDevice is opened.");
if(pcap_compile(p,&fp,filter,1,net)==-1)
{
printf("\nError:%s",pcap_geterr(p));
exit(1);
}
else
printf("\nFilter is compiled.");
if(pcap_setfilter(p,&fp)==-1)
{
printf("\nError:%s",pcap_geterr(p));
exit(1);
}
else
printf("\nFilter is set.");
pcap_loop(p,2,process_packet,NULL);
pcap_freecode(&fp);
pcap_close(p);
return 1;
}


But i only get this in terminal:

tanmoy:/home/tanmoy/Desktop/tdas # gcc appdev.c -lpcap
appdev.c: In function ‘main’:
appdev.c:63: warning: passing argument 3 of ‘pcap_loop’ from incompatible pointer type
tanmoy:/home/tanmoy/Desktop/tdas # ./a.out

DEVICE:eth0
Net:192.168.1.0
Mask:255.255.255.0
Device is opened.
Filter is compiled.




and nothing else.What should i do?
 
Old 07-14-2008, 12:03 AM   #2
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 211

Rep: Reputation: 32
Quote:

if(pcap_setfilter(p,&fp)==-1)
{
printf("\nError:%s",pcap_geterr(p));
exit(1);
}
in the above specified printf, put it as follows and try once

Quote:
printf("\nError:%s\n",pcap_geterr(p));
.
 
Old 07-18-2008, 09:43 AM   #3
tanmoy_tiger
LQ Newbie
 
Registered: Jun 2008
Posts: 6

Original Poster
Rep: Reputation: 0
the problem still exist.HELP ME.
 
Old 07-20-2008, 11:10 PM   #4
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 211

Rep: Reputation: 32
Quote:
Originally Posted by tanmoy_tiger View Post
the problem still exist.HELP ME.
Dear tanmoy_tiger,

I hope you responded to my post.

first of all I didn't gave u any solution. so definitely, the problem will persist. The purpose why I posted is to have a look at the output. some times, the buffer won't get flushed and the output won't get displayed. Because of that reason, I asked you to keep the \n there so that the output will be get displayed on the screen.

can you please tell me what the output you got from that printf?

then I shall try some thing.....

Last edited by chakka.lokesh; 07-20-2008 at 11:19 PM.
 
Old 09-27-2008, 03:45 AM   #5
tanmoy_tiger
LQ Newbie
 
Registered: Jun 2008
Posts: 6

Original Poster
Rep: Reputation: 0
the output is till same
 
Old 09-28-2008, 11:44 PM   #6
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 211

Rep: Reputation: 32
Quote:
void process_packet(u_char *arg,struct pcap_pkthdr *header,u_char *packet)
I guess this function should be as follows:

Quote:
void process_packet(void*)
try this.....
I hope the warning may vanish...
 
Old 11-12-2013, 12:44 AM   #7
admod
LQ Newbie
 
Registered: Sep 2010
Posts: 12

Rep: Reputation: 0
Exclamation Libpcap implementation doubt

Dearest Friends,

I saw a good stuff "packet capturing tutorial" regarding the Libpcap from URL:http://www.thegeekstuff.com/2012/10/...using-libpcap/
A great one congrats ... But as my C Lab session i need to implement same in my college lab . So we setup Ubuntu 12.04 LTS over windows 7. What the next items we need to run the sniffing program over GCC ?
Please advise with a reply and an improved code .
Thanks,
Anes
 
  


Reply

Tags
libpcap, programing


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
libpcap programming tanmoy_tiger Linux - Newbie 1 06-24-2008 11:20 PM
problem with libpcap ARCIS_house Linux - Newbie 9 06-05-2006 12:41 AM
problem installing libpcap aaronruss Linux - Newbie 0 06-05-2004 03:09 PM
libpcap 0.8.3 installation problem open2rock Linux - Software 3 05-15-2004 01:23 AM
mandrake 9.2 libpcap problem ziegen Mandriva 1 11-07-2003 01:24 AM


All times are GMT -5. The time now is 04:22 PM.

Main Menu
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