Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Why define the IPv6 header structure in this way?

Reply
Thread Tools

Why define the IPv6 header structure in this way?

 
 
Marco Peng
Guest
Posts: n/a
 
      11-27-2008
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

 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      11-27-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) (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.
 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      11-27-2008
On Wed, 26 Nov 2008 19:41:35 -0800 (PST), Marco Peng
<(E-Mail Removed)> 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
 
Reply With Quote
 
Marco Peng
Guest
Posts: n/a
 
      11-28-2008
Thank you guys~

Marco.Peng
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Header files with "header.h" or <header.h> ?? mlt C++ 2 01-31-2009 02:54 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Ipv6 on a ipv4/ipv6 hostname Jesse van den Kieboom Ruby 1 06-05-2005 12:57 PM
Support ESP Header in IPv6 Kamil Olszewski Cisco 1 04-09-2005 03:51 PM
IPv6 header on raw sockets? Lars Strand Python 0 02-19-2004 01:59 PM



Advertisments