View the Most Wanted LQ Wiki articles.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 04-15-2005, 05:17 AM   #1
Registered: Dec 2004
Posts: 44

Rep: Reputation: 15
structres and unions problem

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

/* Link layer header */
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

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?
Old 04-15-2005, 07:22 AM   #2
Senior Member
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
Just by assigning the raw to its place, you get the "header view" for same data.
That is somewhat the whole point of unions.

With memcpy you can do it to arbitrary structures, with union, you don't have to.
struct foo {
} bar;
unsigned char *data;
memcpy(&bar,data,sizeof(struct foo))
Old 04-15-2005, 08:31 AM   #3
Registered: Dec 2004
Posts: 44

Original Poster
Rep: Reputation: 15
if i add a new header in skbuff will that require me to have new union to be inserted(trying to check how a packet can be transfered with new header) or its ok to have add only structure in skbuff then how to map it from unsigned char *data; in receive routine? if new header is added between TCP and IP or IP and Ethernet then how to retreive new header from data variable?


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
array of unions in c alaios Programming 3 09-19-2005 09:02 AM
array of unions (c code) alaios Programming 6 09-16-2005 11:43 AM
perl problem? apache problem? cgi problem? WorldBuilder Linux - Software 1 09-17-2003 07:45 PM

All times are GMT -5. The time now is 12:48 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration