LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   libpcap programming problem (http://www.linuxquestions.org/questions/programming-9/libpcap-programming-problem-655473/)

tanmoy_tiger 07-13-2008 12:40 PM

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?

chakka.lokesh 07-14-2008 01:03 AM

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));
.

tanmoy_tiger 07-18-2008 10:43 AM

the problem still exist.HELP ME.

chakka.lokesh 07-21-2008 12:10 AM

Quote:

Originally Posted by tanmoy_tiger (Post 3218947)
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.....:)

tanmoy_tiger 09-27-2008 04:45 AM

the output is till same

chakka.lokesh 09-29-2008 12:44 AM

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...

admod 11-12-2013 01:44 AM

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


All times are GMT -5. The time now is 07:33 PM.