LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   PCAP: segmentation fault (https://www.linuxquestions.org/questions/linux-newbie-8/pcap-segmentation-fault-868224/)

Kavya Krishna K 03-12-2011 09:30 PM

PCAP: segmentation fault
 
Hi,,
I am new to pcap programming..I just tried with some simple examples available and I got segmentation fault..The code which I am doing is attested..

//Code goes here....

int main(int argc, char **argv)
{
int i,val;
char *dev;
char errbuf[PCAP_ERRBUF_SIZE];
pcap_t* descr;
const u_char *packet,data;
struct pcap_pkthdr hdr; /* pcap.h */
struct ether_header *eptr; /* net/ethernet.h */

u_char *ptr; /* printing out hardware header info */

if (!(dev = pcap_lookupdev(errbuf))) {
fprintf(stderr, "error: %s\n", errbuf);
return 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);

printf("val=%d is returned from pcap_next_ex\n",val);

printf("Grabbed packet of length %d\n",hdr.len);
printf("Recieved at ..... %s\n",ctime((const time_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);
}

/* copied from Steven's UNP */
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;
}


Please do reply soon....Thank in advance...

TB0ne 03-13-2011 11:54 AM

Quote:

Originally Posted by Kavya Krishna K (Post 4288690)
Hi,,
I am new to pcap programming..I just tried with some simple examples available and I got segmentation fault..The code which I am doing is attested..

//Code goes here....

Please do reply soon....Thank in advance...

Reply to WHAT, HOW??? That's not the complete source code for your program, and you say nothing about the version/distro of Linux, PCAP, what the EXACT error is, or what you're trying to do. We can't tell you anything without some useful details.

Short answer? You've got a problem in your code. Since you don't post it, or even give us a hint as to what the error is, we can't tell you more.


All times are GMT -5. The time now is 03:47 AM.