union field length mismatch

Discussion in 'C Programming' started by linux.lover2004@gmail.com, May 20, 2005.

  1. Guest

    Hello,
    In linux kernel there is a struct skbuff that used to store packets. It
    has many fields defined in it. thers a union
    union
    {
    struct iphdr *iph;
    struct ipv6hdr *ipv6h;
    struct arphdr *arph;
    struct ipxhdr *ipxh;
    unsigned char *raw;
    } nh;

    I want to ask once IP header is added to skbuff structure all pointers
    in union carry same information. We know IP header is 20 bytes long.
    Then i try to send raw variable to my function in kernel module.
    processip(sb->nh.raw) where sb is of typr struct skbuff
    Then why strlen on (sb->nh.raw) doesnot give 20 bytes as result instead
    its giving 1.
    I want my function to take ip header as unsigned char and print its
    strlen as 20 and want to process on it further.
    , May 20, 2005
    #1
    1. Advertising

  2. wrote:

    > Hello,
    > In linux kernel there is a struct skbuff that used to store packets. It
    > has many fields defined in it. thers a union
    > union
    > {
    > struct iphdr *iph;
    > struct ipv6hdr *ipv6h;
    > struct arphdr *arph;
    > struct ipxhdr *ipxh;
    > unsigned char *raw;
    > } nh;
    >
    > I want to ask once IP header is added to skbuff structure all pointers
    > in union carry same information. We know IP header is 20 bytes long.
    > Then i try to send raw variable to my function in kernel module.
    > processip(sb->nh.raw) where sb is of typr struct skbuff
    > Then why strlen on (sb->nh.raw) doesnot give 20 bytes as result instead
    > its giving 1.
    > I want my function to take ip header as unsigned char and print its
    > strlen as 20 and want to process on it further.


    Can't see anything here which wolud be related to Standrad C. You should
    look in an newsgroup about unix linux programming instead.

    But here are some remarks:

    Not every [unsigned] char* points to a string here it is used as pointer to
    a memoryblock which may contain one of the four headers (I think) which
    differ in size sizeof(struct ipv6h) > sizeof(struct(iph) == 20.

    I think you should find some type tag in the sk_buff struct which will tell
    you something about the contents of nh. And if you are lucky you will find
    size information too.

    --
    Michael Knaup
    Michael Knaup, May 20, 2005
    #2
    1. Advertising

  3. SM Ryan Guest

    # union
    # {
    # struct iphdr *iph;
    # struct ipv6hdr *ipv6h;
    # struct arphdr *arph;
    # struct ipxhdr *ipxh;
    # unsigned char *raw;
    # } nh;
    #
    # I want to ask once IP header is added to skbuff structure all pointers
    # in union carry same information. We know IP header is 20 bytes long.
    # Then i try to send raw variable to my function in kernel module.
    # processip(sb->nh.raw) where sb is of typr struct skbuff
    # Then why strlen on (sb->nh.raw) doesnot give 20 bytes as result instead
    # its giving 1.

    strlen(s) is the number of nonzero chars starting from s until the first zero
    char. It is a runtime determined value. The raw packet header is not a C string
    (with respect to <string.h>) but a byte array of a specific length which is
    allowed to have zero bytes intermingled with nonzero bytes. You will have to
    determine the size some other way, perhaps an enum or #define which has the
    size, or the sizeof the struct. Note that sizeof(nh.raw) will likely return
    4 or 8, the size of data pointer, not the size of the byte array it points to.

    # I want my function to take ip header as unsigned char and print its
    # strlen as 20 and want to process on it further.

    You can't get there from here.

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    Death is the worry of the living. The dead, like myself,
    only worry about decay and necrophiliacs.
    SM Ryan, May 20, 2005
    #3
  4. Joe Pfeiffer Guest

    writes:

    > Hello,
    > In linux kernel there is a struct skbuff that used to store packets. It
    > has many fields defined in it. thers a union
    > union
    > {
    > struct iphdr *iph;
    > struct ipv6hdr *ipv6h;
    > struct arphdr *arph;
    > struct ipxhdr *ipxh;
    > unsigned char *raw;
    > } nh;
    >
    > I want to ask once IP header is added to skbuff structure all pointers
    > in union carry same information. We know IP header is 20 bytes long.
    > Then i try to send raw variable to my function in kernel module.
    > processip(sb->nh.raw) where sb is of typr struct skbuff
    > Then why strlen on (sb->nh.raw) doesnot give 20 bytes as result instead
    > its giving 1.
    > I want my function to take ip header as unsigned char and print its
    > strlen as 20 and want to process on it further.
    >


    You got three responses to this query (that I saw) the first time you
    asked it. Why have you asked again?
    --
    Joseph J. Pfeiffer, Jr., Ph.D. Phone -- (505) 646-1605
    Department of Computer Science FAX -- (505) 646-1002
    New Mexico State University http://www.cs.nmsu.edu/~pfeiffer
    Joe Pfeiffer, May 20, 2005
    #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. Matt Garman
    Replies:
    1
    Views:
    645
    Matt Garman
    Apr 25, 2004
  2. Panchal V
    Replies:
    18
    Views:
    754
    Mark A. Odell
    Feb 4, 2004
  3. Peter Dunker

    union in struct without union name

    Peter Dunker, Apr 26, 2004, in forum: C Programming
    Replies:
    2
    Views:
    847
    Chris Torek
    Apr 26, 2004
  4. NotGiven
    Replies:
    3
    Views:
    326
    Michael D. Kersey
    May 13, 2004
  5. NotGiven
    Replies:
    3
    Views:
    286
    Michael D. Kersey
    May 13, 2004
Loading...

Share This Page