Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between linux/if.h and net/if.h?

Tags:

c

linux

The comments say that net/if.h is part of the GNU C Library and linux/if.h says it is "An implementation of the TCP/IP protocol suite for the LINUX operating system."

But why are there two of them and what is the use of each one?

like image 789
Marste Avatar asked Nov 19 '13 21:11

Marste


2 Answers

Initially, both provide data structures to manage network interfaces.linux/if.h is specifically more related to interact with Linux kernel's network interfaces. Actually, it states so at the beginning of the file:

/*
 * INET     An implementation of the TCP/IP protocol suite for the LINUX
 *      operating system.  INET is implemented using the  BSD Socket
 *      interface as the means of communication with the user level.
 *
 *      Global definitions for the INET interface module.
 */

net/if.h is glibc's file for same purpose and is initially more user space focused.

Is important to note that, if you're going to use both (they share some structures but not all), include order is quite important, suggesting, in most cases, to include linux/if.h before net/if.h.

like image 136
jcm Avatar answered Nov 02 '22 11:11

jcm


linux/if.h is part of the kernel (or imported in the libc from the kernel), and net/if.h is part of the C library.

Thus, net/if.h is what your processes can see, and linux/if.h what the kernel wants. The kernel internals (which doesn't see libc) include the kernel headers. net/if.h is what your hardcore networking tool includes. :-)

Far-far ago there were incompatibility problems between the two. This resulted f.e. don't working ifconfig, etc. tools. Currently (since around 10-15 yrs) they are well integrated and backward compatible.

like image 40
peterh Avatar answered Nov 02 '22 11:11

peterh