Hi, I need to get the file descriptor to use in ioctl() calls for an ethernet port in Linux. Not sure how to do this.
Just use the file descriptor of an open socket, using the name of the device in the ifreq
structure passed to ioctl()
, assuming your program has adequate permissions to do so.
From the docs:
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:
The socket need not be bound to the target device, or be of any specific family. Any open socket fd will do (again, with appropriate privileges), just open one for your specific task, wait for ioctl()
to return and close it.
See man 7 netdevice
for more, or here if you don't have the appropriate documentation packages installed (hint, the package is usually named manpages-dev
or manpages-devel
, depending on your distro)
You can also take a look at the source to the net-tools
package, which may be named differently depending on your distro. That's the source to ifconfig
(Debian / Ubuntu here).
Sorry for the original ambiguity, I thought you were trying to configure a special multi function device (not sure why now, perhaps lack of sleep).
You can do something like this fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)
Use strace
to see what functions ifconfig calls.