C – Gets the ioctl file descriptor for the Ethernet port

Gets the ioctl file descriptor for the Ethernet port… here is a solution to the problem.

Gets the ioctl file descriptor for the Ethernet port

I need to get the file descriptor to use in the ioctl() call in order to use the ethernet port in Linux. Not sure how to do this.


Simply use the file descriptor of an open socket, using the device name in the ifreq structure passed to ioctl(), assuming your program has sufficient authority to do so.

From the documentation:

Linux supports some standard ioctls to
configure network devices. They can
be used on any socket’s file
descriptor regardless of the family or
type. They pass an ifreq structure:

Sockets do not need to be bound (binded) to the target device or belong to any particular family. Any open socket fd will work (again, with the appropriate permissions), just open one for your specific task, wait for ioctl() to return and close it.

For more information, see man 7 netdevice, or here if you do not have the appropriate documentation package installed (hint, this package is usually named manpages-dev or manpages-devel, depending on your distribution).

You can also view the source code of the net-tools package, which may have a different name depending on your distribution. This is where ifconfig comes from (here Debian/Ubuntu).

Sorry for the original ambiguity, I thought you were trying to configure a special multifunction device (now I don’t know why, maybe sleep inadequate).

Related Problems and Solutions