Thread: Reading from the Ethernet card.

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    38

    Reading from the Ethernet card.

    Hello to all.

    I'm developing a simple sniffer using, for now, the library set: libpcap. I'm using this functions to open the socket for reading.

    Code:
    	int sock;
    	char *dev;
    	char errbuf[PCAP_ERRBUF_SIZE];
    
    	/*Find the Ethernet interface*/
    	dev = pcap_lookupdev(errbuf));   
    	
    	/*Show the interface name found*/
    	printf("Reading from: %s\n",dev);
    	
    	/* Open the socket to read only Ethernet frames*/
    	sock=socket(PF_PACKET,SOCK_RAW,htons(ETH_P_ALL));    
    	
    	/* Associate the socket to the interface found */
    	setsockopt(sock,SOL_SOCKET,SO_BINDTODEVICE,dev,strlen(dev));
    This is working fine, but the problem is that my PC has two interface cards (eth0 and eth1). I would like to choose the card I want to sniff and function pcap_lookupdev() only returns the first one it finds to be up.

    Is there another way to achieve this?

    Thanks!

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    38
    Hey I found the way to get the list of interfaces. Here is how (for for those interested in the subject)

    Code:
    /* Show the list of interfaces to the user. */
    void
    show_interfaces(void)
         {
               pcap_if_t *interface_list;
               pcap_if_t *if_list_ptr;
               int result;
               int i;
               /* Ask libpcap for the list of interfaces */
               result = pcap_findalldevs(&interface_list, errbuf);
               if (result == -1) {
                    fprintf(stderr, "%s\n", errbuf);
                    exit(1);
               }
               /* Show them to the user */
               if_list_ptr = interface_list;
               i = 0;
               while (if_list_ptr) {
                    if (if_list_ptr->description) {
                        printf("%d. %s (%s)\n", i, if_list_ptr->name,
                                 if_list_ptr->description);
                    }
                    else {
                        printf("%d. %s\n", i, if_list_ptr->name);
                    }
                    if_list_ptr = if_list_ptr->next;
                    i += 1;
               }
               pcap_freealldevs(interface_list);
           }

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    1
    oh, that is really cool, I need this kind of stuff ..

    good days...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with STL container list for INformation Manager
    By jackfraust in forum C++ Programming
    Replies: 5
    Last Post: 04-22-2009, 11:12 PM
  2. Need Help With a BlackJack Program in C
    By Jp2009 in forum C Programming
    Replies: 15
    Last Post: 03-30-2009, 10:06 AM
  3. Replies: 2
    Last Post: 01-28-2008, 03:07 AM
  4. Video card mystery
    By Leeman_s in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-30-2003, 02:56 PM
  5. Replies: 2
    Last Post: 11-07-2003, 12:21 AM