'offsetof'에 해당되는 글 1건

  1. 2009.06.05 offsetof() - stddef.h
Linux API/network2009.06.05 16:03
ifconfig.c를 들여다 보고 있자니 신기한 녀석들이 나타나시었다.

struct arg1opt {
    const char *name;
    unsigned short selector;
    unsigned short ifr_offset;
};

#define ifreq_offsetof(x)  offsetof(struct ifreq, x)

static const struct arg1opt Arg1Opt[] = {
    {"SIOCSIFMETRIC",  SIOCSIFMETRIC,  ifreq_offsetof(ifr_metric)},
    {"SIOCSIFMTU",     SIOCSIFMTU,     ifreq_offsetof(ifr_mtu)},
    {"SIOCSIFTXQLEN",  SIOCSIFTXQLEN,  ifreq_offsetof(ifr_qlen)},
    {"SIOCSIFDSTADDR", SIOCSIFDSTADDR, ifreq_offsetof(ifr_dstaddr)},
    {"SIOCSIFNETMASK", SIOCSIFNETMASK, ifreq_offsetof(ifr_netmask)},
    {"SIOCSIFBRDADDR", SIOCSIFBRDADDR, ifreq_offsetof(ifr_broadaddr)},
#ifdef BB_FEATURE_IFCONFIG_HW
    {"SIOCSIFHWADDR",  SIOCSIFHWADDR,  ifreq_offsetof(ifr_hwaddr)},
#endif
    {"SIOCSIFDSTADDR", SIOCSIFDSTADDR, ifreq_offsetof(ifr_dstaddr)},
#ifdef SIOCSKEEPALIVE
    {"SIOCSKEEPALIVE", SIOCSKEEPALIVE, ifreq_offsetof(ifr_data)},
#endif
#ifdef SIOCSOUTFILL
    {"SIOCSOUTFILL",   SIOCSOUTFILL,   ifreq_offsetof(ifr_data)},
#endif
#ifdef BB_FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ
    {"SIOCSIFMAP",     SIOCSIFMAP,     ifreq_offsetof(ifr_map.mem_start)},
    {"SIOCSIFMAP",     SIOCSIFMAP,     ifreq_offsetof(ifr_map.base_addr)},
    {"SIOCSIFMAP",     SIOCSIFMAP,     ifreq_offsetof(ifr_map.irq)},
#endif
    /* Last entry if for unmatched (possibly hostname) arg. */
    {"SIOCSIFADDR",    SIOCSIFADDR,    ifreq_offsetof(ifr_addr)},
};

$ man offsetof
#include <stddef.h>
size_t offsetof(type, member);

머. 매크로로 해도 될텐데 왜 굳이 함수형으로 했을까는 조금 의문이지만,
간단하게 표현하자면 . 연산자를 붙여주는 역활을 한다.

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

       int main()
       {
           struct s {
               int i;
               char c;
               double d;
               char a[];
           };

           /* Output is compiler dependent */

           printf("offsets: i=%ld; c=%ld; d=%ld a=%ld\n",
                   (long) offsetof(struct s, i),    // s.i ?
                   (long) offsetof(struct s, c),    // s.c ?
                   (long) offsetof(struct s, d),    // s.d ?
                   (long) offsetof(struct s, a));    // s.a ?
           printf("sizeof(struct s)=%ld\n", (long) sizeof(struct s));

           exit(EXIT_SUCCESS);
       }


생각을 해보니..

$ vi /usr/inclue/net/if.h
152 # define ifr_name       ifr_ifrn.ifrn_name      /* interface name       */
153 # define ifr_hwaddr     ifr_ifru.ifru_hwaddr    /* MAC address          */
154 # define ifr_addr       ifr_ifru.ifru_addr      /* address              */
155 # define ifr_dstaddr    ifr_ifru.ifru_dstaddr   /* other end of p-p lnk */
156 # define ifr_broadaddr  ifr_ifru.ifru_broadaddr /* broadcast address    */
157 # define ifr_netmask    ifr_ifru.ifru_netmask   /* interface net mask   */
158 # define ifr_flags      ifr_ifru.ifru_flags     /* flags                */
159 # define ifr_metric     ifr_ifru.ifru_ivalue    /* metric               */
160 # define ifr_mtu        ifr_ifru.ifru_mtu       /* mtu                  */
161 # define ifr_map        ifr_ifru.ifru_map       /* device map           */
162 # define ifr_slave      ifr_ifru.ifru_slave     /* slave device         */
163 # define ifr_data       ifr_ifru.ifru_data      /* for use by interface */
164 # define ifr_ifindex    ifr_ifru.ifru_ivalue    /* interface index      */
165 # define ifr_bandwidth  ifr_ifru.ifru_ivalue    /* link bandwidth       */
166 # define ifr_qlen       ifr_ifru.ifru_ivalue    /* queue length         */
167 # define ifr_newname    ifr_ifru.ifru_newname   /* New name             */

이런식으로도 매크로 정의가 되어 있는데, 여러가지 방법으로 사용하는 이유가 있을까 라는 생각도 든다.

'Linux API > network' 카테고리의 다른 글

gateway 정보  (0) 2009.06.05
linux에서 ip/mac address 받아오기 관련 링크  (0) 2009.06.05
network 관련 include 할 파일 목록  (0) 2009.06.05
offsetof() - stddef.h  (0) 2009.06.05
SIOCGIF가 모야?  (0) 2009.06.05
C언어로 MAC 어드레스 받아오기 (Linux)  (1) 2009.06.04
Posted by 구차니
TAG

댓글을 달아 주세요