I am questioning how this code compiled even though struct iwreq does not have ifr_name as a member

Viewed 180

I wrote very simple code using struct iwreq. Also, I expected this will be error. But it is compiled and works.

I looked inside linux/wireless.h which has the definition of struct iwreq. And the iwreq does not ifr_name as a member.

Would someone can give me an idea? Here is the simple code.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <linux/wireless.h>

int main(void)
{
   char *intf = "eth0";
   struct iwreq iwr;

   strncpy(iwr.ifr_name, intf, sizeof(iwr.ifr_name));

   printf("main : intf = %s, iwr.ifr_name = %s\n", intf, iwr.ifr_name);
   return 0;
}
2 Answers

wireless.h includes if.h, and inside if.h you can find:

#define ifr_name    ifr_ifrn.ifrn_name  /* interface name   */

So the code is translated to:

strncpy(iwr.ifr_ifrn.ifrn_name, intf, sizeof(iwr.ifr_ifrn.ifrn_name));

/usr/include/linux/wireless.h includes linux/if.h:

#include <linux/if.h>           /* for IFNAMSIZ and co... */

And in /usr/include/linux/if.h there is:

#define ifr_name    ifr_ifrn.ifrn_name  /* interface name   */
Related