structres and unions problem

linux.lover2004_at_gmail.com
Date: 04/15/05


Date: 15 Apr 2005 03:15:15 -0700

hello,
    There is a structure skbuff written in linux kernel.
    In skbuff structure network and link layers are written as unions.
     /* Network layer header */
        union
        {
                struct iphdr *iph;
                unsigned char *raw;
        } nh;

        /* Link layer header */
        union
        {
                struct ethhdr *ethernet;
                unsigned char *raw;
        } mac;

  when packets received by kernel it stores it as
   unsigned char *data;
  and then map it to headers union in packet as
        skb->h.raw = skb->nh.raw = skb->data ;
  then ethernet header is retrieved by
 skb->mac.raw=skb->data;
 ethhdr=skb->mac.ethernet

1)how can a unsigned char *data which assigns to skb->mac.raw recovers
as struct ethhdr *ethernet?

2) Can it be possible to map some bytes which i know is structure to a
structure from unsigned char *data? Does memcpy help me?



Relevant Pages

  • Re: byte alignment in structures and unions
    ... unsigned char byte0; ... struct each_of_four four; ... No unions and no struct needed. ... Didn't you say you didn't want to use bitwise operations? ...
    (comp.lang.c)
  • Re: Concept of union
    ... >class CVertex ... That is not what unions were developed for. ... unsigned char neither C not C++ allow writing to a union by through one ... unsigned char has an exemption) ...
    (alt.comp.lang.learn.c-cpp)