Thread: libpcap : SIOCGIFHWADDR: No such device

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    4

    libpcap : SIOCGIFHWADDR: No such device

    Hello. I've just started using libpcap lib and been following some tutorials.

    Code:
    #include <pcap.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    void dump(const unsigned char *data_buffer, const unsigned int length);
    void pcap_fatal(const char *failed_in, const char *errbuf);
    
    int main() {
       struct pcap_pkthdr header;
       const u_char *packet;
       char errbuf[PCAP_ERRBUF_SIZE];
       char *device;
       pcap_t *pcap_handle;
       int i;
    
    pcap_handle = pcap_open_live(device, 4096, 1, 0, errbuf);
    if(pcap_handle == NULL)
       pcap_fatal("pcap_open_live", errbuf);
    
    for(i=0; i < 3; i++) {
          packet = pcap_next(pcap_handle, &header);
          printf("Got a %d byte packet\n", header.len);
          dump(packet, header.len);
       }
       pcap_close(pcap_handle);
    }
    Code:
    xyz@ubuntu:~/Desktop/Programs$ gcc -o sniff libpcap.c -l pcap
    xyz@ubuntu:~/Desktop/Programs$ ./sniff
    Fatal Error in pcap_open_live: socket: Operation not permitted
    xyz@ubuntu:~/Desktop/Programs$ sudo ./sniff
    Fatal Error in pcap_open_live: SIOCGIFHWADDR: No such device
    Apparently pcap_live_open() keeps returning NULL, therefore the error I don't quite understand. I went over dozens of topics searching for solution, but couldn't quite find it.

    As far as I get it, SIOCGIFHWADDR is an interface hardware address but I'm not sure what it's all about.

    Thanks in advance,

    Mercurial

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
       char *device;
       pcap_t *pcap_handle;
       int i;
    
    pcap_handle = pcap_open_live(device, 4096, 1, 0, errbuf);
    if(pcap_handle == NULL)
       pcap_fatal("pcap_open_live", errbuf);
    Where?

    Garbage in, garbage out as the saying goes.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    4
    Right. Thanks for that.

    Code:
    #define DEVICE_SIZE 1000
    // ...
    
    device = (char*) malloc(DEVICE_SIZE*sizeof(char));
    
    pcap_handle = pcap_open_live(device, 4096, 1, 0, errbuf);
    if(pcap_handle == NULL)
       pcap_fatal("pcap_open_live", errbuf);
    Same error though.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    How about some meaningful string, not just creating variables with the right type just to make the compiler STFU.

    Like

    pcap_handle = pcap_open_live("/dev/this/is/my/device/I/want/to/open", 4096, 1, 0, errbuf);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    4
    If I actually understood the device part, I wouldn't be posting this topic.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The other name for this one is SIGIOWTF?ADDR

    Quote Originally Posted by Mercurial View Post
    Hello. I've just started using libpcap lib and been following some tutorials.
    Apparently not the right one

    Quote Originally Posted by Mercurial View Post
    If I actually understood the device part, I wouldn't be posting this topic.
    Here's the tutorial I used to write a packet sniffer with libpcap:

    Programming with pcap

    That was a few years ago and I don't remember much, but if you open that page you'll see the second subsection, "Setting the device":

    This is terribly simple. There are two techniques for setting the device that we wish to sniff on. The first is that we can simply have the user tell us. [...] The other technique is equally simple. Look at this program:
    Code:
    dev = pcap_lookupdev(errbuf);
    In this case, pcap just sets the device on its own. "But wait, Tim," you say. "What is the deal with the errbuf string?"
    Last edited by MK27; 09-05-2011 at 08:18 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    4
    Thanks alot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. detect network device when using libpcap in ubuntu
    By contau in forum C Programming
    Replies: 0
    Last Post: 05-27-2011, 10:51 AM
  2. Parsing pcap without libpcap
    By n1mda in forum C Programming
    Replies: 9
    Last Post: 06-18-2009, 12:35 AM
  3. libpcap: How to timeout pcap_loop() ?
    By B-Con in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-18-2008, 12:57 AM
  4. SIOCGIFHWADDR not working.... :(
    By compile in forum Linux Programming
    Replies: 1
    Last Post: 04-03-2007, 09:39 AM
  5. Replies: 4
    Last Post: 06-30-2004, 03:11 PM