LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


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

Rep: Reputation: 15
structres and unions problem


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

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


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

Original Poster
Rep: Reputation: 15
hello,
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?
 
  


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:53 AM.

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