Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's wrong with linux/if.h and net/if.h?

Tags:

linux

In my project, I include pfring.h, but compile error: some functions in net/if.h and linux/if.h are redefinition. I found that the pfring.h include linux/if.h So, I test a program, my test code:

#include <linux/if.h>
#include <net/if.h>

int main(void) {
    return 0;
}

It expected compile error. So, what's wrong with linux/if.h and net/if.h ? Can not I include them at once?


error message:

In file included from test.c:1:0:
/usr/include/linux/if.h:178:19: error: field 'ifru_addr' has incomplete type
/usr/include/linux/if.h:179:19: error: field 'ifru_dstaddr' has incomplete type
/usr/include/linux/if.h:180:19: error: field 'ifru_broadaddr' has incomplete type
/usr/include/linux/if.h:181:19: error: field 'ifru_netmask' has incomplete type
/usr/include/linux/if.h:182:20: error: field 'ifru_hwaddr' has incomplete type
In file included from test.c:2:0:
/usr/include/net/if.h:45:5: error: expected identifier before numeric constant
/usr/include/net/if.h:112:8: error: redefinition of 'struct ifmap'
/usr/include/linux/if.h:136:8: note: originally defined here
/usr/include/net/if.h:127:8: error: redefinition of 'struct ifreq'
/usr/include/linux/if.h:170:8: note: originally defined here
/usr/include/net/if.h:177:8: error: redefinition of 'struct ifconf'
/usr/include/linux/if.h:219:8: note: originally defined here
like image 655
大宝剑 Avatar asked Jan 06 '12 08:01

大宝剑


4 Answers

For me (on Ubuntu 12.04 x64) the following include solved the problem:

#include <sys/socket.h> // <-- This one
#include <linux/if.h>
#include <linux/if_tun.h>
like image 158
Dmitry Avatar answered Oct 28 '22 13:10

Dmitry


This problem has been resolved, add the compile flag -DHAVE_PCAP is fix. ;-)

like image 41
大宝剑 Avatar answered Oct 28 '22 14:10

大宝剑


At first let us talk about the source: The header files are from different Packages as you can see asking dpkg.

$ dpkg -S /usr/include/linux/if.h
linux-libc-dev:i386: /usr/include/linux/if.h
$ dpkg -S /usr/include/net/if.h
libc6-dev:i386: /usr/include/net/if.h

linux-libc-dev is part of linux kernel packages while libc6-dev is part of the libc6 (Standard C library in version 6).

It seams like they are interchangeable so you should only use one (not 100% sure about this). If you pick linux/if.h, you may depend on Kernel versions with your compiled binary.

All new Library versions I have in mind stick with net/if.h instead of the linux one - so you should do the same.

like image 2
A. Binzxxxxxx Avatar answered Oct 28 '22 14:10

A. Binzxxxxxx


If you are using one of the interface state flags (eg: IFF_UP, etc.), you need one more header than mentioned in other posts.

#include <sys/types.h>   // <== 
#include <sys/socket.h>
#include <linux/if.h>
like image 1
user31986 Avatar answered Oct 28 '22 15:10

user31986