struct ifreq : different definition in "linux/if.h" and man page

20,008

Different header files; linux/if.h and net/if.h are similar, but unless you're working against the linux kernel and not the linux userspace, you probably want to use net/if.h.

Share:
20,008
anoop.babu
Author by

anoop.babu

Updated on July 30, 2022

Comments

  • anoop.babu
    anoop.babu almost 2 years

    In online manual page netdevice(7) - Linux man page, the definition of ifreq structure is given as:

    struct ifreq {
                   char ifr_name[IFNAMSIZ]; /* Interface name */
                   union {
                       struct sockaddr ifr_addr;
                       struct sockaddr ifr_dstaddr;
                       struct sockaddr ifr_broadaddr;
                       struct sockaddr ifr_netmask;
                       struct sockaddr ifr_hwaddr;
                       short           ifr_flags;
                       int             ifr_ifindex;
                       int             ifr_metric;
                       int             ifr_mtu;
                       struct ifmap    ifr_map;
                       char            ifr_slave[IFNAMSIZ];
                       char            ifr_newname[IFNAMSIZ];
                       char           *ifr_data;
                   };
               };
    

    But my linux header shows different definition. Below is the definition from /usr/include/linux/if.h The kernel version I'm using is Linux 3.18

    struct ifreq {
    #define IFHWADDRLEN 6
        union
        {
            char    ifrn_name[IFNAMSIZ];        /* if name, e.g. "en0" */
        } ifr_ifrn;
    
        union {
            struct  sockaddr ifru_addr;
            struct  sockaddr ifru_dstaddr;
            struct  sockaddr ifru_broadaddr;
            struct  sockaddr ifru_netmask;
            struct  sockaddr ifru_hwaddr;
            short   ifru_flags;
            int ifru_ivalue;
            int ifru_mtu;
            struct  ifmap ifru_map;
            char    ifru_slave[IFNAMSIZ];   /* Just fits the size */
            char    ifru_newname[IFNAMSIZ];
            void *  ifru_data;
            struct  if_settings ifru_settings;
        } ifr_ifru;
    };
    

    Why is this change made ? Which one should I stick to for portability with different linux distros ?

    • SingerOfTheFall
      SingerOfTheFall over 8 years
      That page you linked is almost 2 years old...
    • Some programmer dude
      Some programmer dude over 8 years
      This structure is not mandated by POSIX, it's Linux specific. So if you want portability you can't really use it at all.
    • Some programmer dude
      Some programmer dude over 8 years
      Also, if you look at e.g. /usr/include/net/if.h then you will see that all fields also have aliases by using preprocessing macros, so using just my_ifreq_ptr->ifr_name will automatically be replaced with my_ifreq_ptr->ifr_ifrn.ifrn_name by the preprocessor.
    • sergej
      sergej over 8 years
      See the git log to find out why this change was made.
    • anoop.babu
      anoop.babu over 8 years
      @JoachimPileborg, Thanks. I missed the #defines. I came across this confusion while debugging through gnu GDB. Since macros where not applicable in that state, I ran into errors. Now got it !!