Find the answer to your Linux question:
Results 1 to 2 of 2
Hi. I'm having a problem in my code to capture packets over the LAN using ICAP library. Apparently there doesn't seem to be any problem, but whenever I run the ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2011
    Posts
    1

    Problem capturing packets over the LAN using ICAP


    Hi. I'm having a problem in my code to capture packets over the LAN using ICAP library. Apparently there doesn't seem to be any problem, but whenever I run the code, it gives to list of the devices (which is indeed required) but when I enter the device number, it says: "Segmentation fault". I don't understand what's the problem. Can anyone help? Here's the code:

    Code:
    int main(int argc, char **argv)
    {
         
        char errbuf[PCAP_ERRBUF_SIZE];
        pcap_t* descr;
        const u_char *packet;
        struct pcap_pkthdr hdr;     /* pcap.h */
        struct ether_header *eptr;  /* net/ethernet.h */
    
        u_char *ptr; /* printing out hardware header info */
    
        /* grab a device to peak into... */
       /* dev = pcap_lookupdev(errbuf);
    
        if(dev == NULL)
        {
            printf("%s\n",errbuf);
            exit(1);
        }
    
        printf("DEV: %s\n",dev);*/
    
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    
    
            pcap_if_t *alldevs;
    
            pcap_if_t *d;
    
            int i=0;
    
    
            pcap_t * fp;
    
           if (pcap_findalldevs(&alldevs, errbuf) == -1)
    
            {
    
                    cout<<"Error in pcap_findalldevs "<<errbuf;
    
                    exit(1);
    
            }
    
            /* Print the list */
    
            for(d= alldevs; d != NULL; d= d->next)
    
            {
    
                    cout<<++i<<" "<<d->name;
    
                    if (d->description)
    
                    cout<<d->description<<endl;
    
                    else
    
                    cout<< "(No description available)\n";
    
            }
    
            if (i == 0)
    
            {
    
                    cout<<"\nNo interfaces found! Make sure Pcap is installed.\n";
    
                    return 1;
    
            }
    
           cout<<"Enter the interface number"<< (1-i);        
    
            int inum;
    
            cin>>inum;
    
            if(inum < 1 || inum > i)
    
            {
    
                    cout<<"\nInterface number out of range.\n";
    
    
                    // Free the device list /
    
                    pcap_freealldevs(alldevs);
    
                    return 1;
    
            }
    	char *dev=d->name;
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////
    
    
    	cout<<"adada"<<endl;
        descr = pcap_open_live(dev,65536,1,0,errbuf);
    
        if(descr == NULL)
        {
          printf("pcap_open_live(): %s\n",errbuf);
          exit(1);
        }
    	      
           struct pcap_pkthdr;                                 
    
           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((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);
        }
    
        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;
    }

  2. #2
    Linux Newbie
    Join Date
    Dec 2010
    Posts
    146
    I'm not sure what's your actual motive, but there're lots of applications available which allow you to do that. For e.g. hping which uses tcl for it's API -


    while {1} {
    puts [hping recv eth0]
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •