Why define the IPv6 header structure in this way?

Discussion in 'C Programming' started by Marco Peng, Nov 27, 2008.

  1. Marco Peng

    Marco Peng Guest

    RFC2460: http://www.ietf.org/rfc/rfc2460.txt
    IPv6 Header Format
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |Version| Traffic Class | Flow Label |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    | Payload Length | Next Header | Hop Limit |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    | |
    + +
    | |
    + Source Address +
    | |
    + +
    | |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    | |
    + +
    | |
    + Destination Address +
    | |
    + +
    | |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

    The header structure definition in RFC3542

    struct ip6_hdr {
    union {
    struct ip6_hdrctl {
    uint32_t ip6_un1_flow; /* 4 bits version, 8 bits TC, 20
    bits
    flow-ID */
    uint16_t ip6_un1_plen; /* payload length */
    uint8_t ip6_un1_nxt; /* next header */
    uint8_t ip6_un1_hlim; /* hop limit */
    } ip6_un1;
    uint8_t ip6_un2_vfc; /* 4 bits version, top 4 bits
    tclass */
    } ip6_ctlun;
    struct in6_addr ip6_src; /* source address */
    struct in6_addr ip6_dst; /* destination address */
    };

    #define ip6_vfc ip6_ctlun.ip6_un2_vfc
    #define ip6_flow ip6_ctlun.ip6_un1.ip6_un1_flow
    #define ip6_plen ip6_ctlun.ip6_un1.ip6_un1_plen
    #define ip6_nxt ip6_ctlun.ip6_un1.ip6_un1_nxt
    #define ip6_hlim ip6_ctlun.ip6_un1.ip6_un1_hlim
    #define ip6_hops ip6_ctlun.ip6_un1.ip6_un1_hlim

    My question is why contains a union, why not a structure??
    Thanks for any response!

    Marco.Peng
    Marco Peng, Nov 27, 2008
    #1
    1. Advertising

  2. (Gordon Burditt) writes:

    >>RFC2460: http://www.ietf.org/rfc/rfc2460.txt
    >>IPv6 Header Format

    >
    >>My question is why contains a union, why not a structure??

    >
    > If you read the comments on ip6_un1_flow and ip6_un2_vfc, it appears
    > that the traffic class sometimes has 4 bits and sometimes it has 8
    > bits. There are two variants of the first part of the header,
    > therefore a union is used for the portion of the header before
    > the source and destination address (first two rows in the below
    > diagram). Note that it DOES contain two nested structures.


    That last bit is a shade confusing! It does contain two members, but one
    is a structure and the other is just an octet.

    <snip>
    >> union {
    >> struct ip6_hdrctl {
    >> uint32_t ip6_un1_flow; /* 4 bits version, 8 bits TC, 20
    >>bits
    >> flow-ID */
    >> uint16_t ip6_un1_plen; /* payload length */
    >> uint8_t ip6_un1_nxt; /* next header */
    >> uint8_t ip6_un1_hlim; /* hop limit */
    >> } ip6_un1;
    >> uint8_t ip6_un2_vfc; /* 4 bits version, top 4 bits
    >>tclass */
    >> } ip6_ctlun;


    --
    Ben.
    Ben Bacarisse, Nov 27, 2008
    #2
    1. Advertising

  3. On Wed, 26 Nov 2008 19:41:35 -0800 (PST), Marco Peng
    <> wrote:

    >RFC2460: http://www.ietf.org/rfc/rfc2460.txt
    >IPv6 Header Format
    > +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    > |Version| Traffic Class | Flow Label |
    > +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    > | Payload Length | Next Header | Hop Limit |
    > +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    > | |
    > + +
    > | |
    > + Source Address +
    > | |
    > + +
    > | |
    > +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    > | |
    > + +
    > | |
    > + Destination Address +
    > | |
    > + +
    > | |
    > +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    >
    >The header structure definition in RFC3542
    >
    > struct ip6_hdr {
    > union {
    > struct ip6_hdrctl {
    > uint32_t ip6_un1_flow; /* 4 bits version, 8 bits TC, 20
    >bits
    > flow-ID */
    > uint16_t ip6_un1_plen; /* payload length */
    > uint8_t ip6_un1_nxt; /* next header */
    > uint8_t ip6_un1_hlim; /* hop limit */
    > } ip6_un1;
    > uint8_t ip6_un2_vfc; /* 4 bits version, top 4 bits
    >tclass */
    > } ip6_ctlun;
    > struct in6_addr ip6_src; /* source address */
    > struct in6_addr ip6_dst; /* destination address */
    > };
    >
    > #define ip6_vfc ip6_ctlun.ip6_un2_vfc
    > #define ip6_flow ip6_ctlun.ip6_un1.ip6_un1_flow
    > #define ip6_plen ip6_ctlun.ip6_un1.ip6_un1_plen
    > #define ip6_nxt ip6_ctlun.ip6_un1.ip6_un1_nxt
    > #define ip6_hlim ip6_ctlun.ip6_un1.ip6_un1_hlim
    > #define ip6_hops ip6_ctlun.ip6_un1.ip6_un1_hlim
    >
    >My question is why contains a union, why not a structure??
    >Thanks for any response!


    It contains a union because the author wanted to insure that the
    internal struct ip6_hdrctl named ip6_un1 started at the same address
    as the internal uint8_t named ip6_un2_vfc. Why he wanted this overlap
    is probably evident from the way he uses the two members in the code.

    --
    Remove del for email
    Barry Schwarz, Nov 27, 2008
    #3
  4. Marco Peng

    Marco Peng Guest

    Thank you guys~

    Marco.Peng
    Marco Peng, Nov 28, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lars Strand

    IPv6 header on raw sockets?

    Lars Strand, Feb 19, 2004, in forum: Python
    Replies:
    0
    Views:
    766
    Lars Strand
    Feb 19, 2004
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    877
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,805
    Smokey Grindel
    Dec 2, 2006
  4. mlt
    Replies:
    2
    Views:
    831
    Jean-Marc Bourguet
    Jan 31, 2009
  5. Jesse van den Kieboom

    Ipv6 on a ipv4/ipv6 hostname

    Jesse van den Kieboom, Jun 5, 2005, in forum: Ruby
    Replies:
    1
    Views:
    358
    Dibya Prakash
    Jun 5, 2005
Loading...

Share This Page