Networking Forums

Networking Forums > Computer Networking > Linux Networking > How pointers work in this case?

Reply
Thread Tools Display Modes

How pointers work in this case?

 
 
kernel.lover
Guest
Posts: n/a

 
      01-30-2005, 10:05 AM
Hello,
By looking into skbuff.h it is clear that paket is stored in
skbuff structure which has also many structures defined for each layer
as union.
Can it be possible to retreive any structure by casting skb->data at
any stage once that struct is push to skbuff structure?
e.g. by looking in eth.c following statement tells it works but how???
struct ethhdr *eth = (struct ethhdr *)skb->data;
Also once a structure is assigned values/pushed it in skbuff structure
can it be possible to change its values at any later stage?
e.g. in above ethernet case does it possible to change eth->h_proto in
ethernet driver file(8390.c)
regards,
cranium
 
Reply With Quote
 
 
 
 
Noah Roberts
Guest
Posts: n/a

 
      01-30-2005, 05:31 PM
kernel.lover wrote:
> Hello,
> By looking into skbuff.h it is clear that paket is stored in
> skbuff structure which has also many structures defined for each layer
> as union.
> Can it be possible to retreive any structure by casting skb->data at
> any stage once that struct is push to skbuff structure?
> e.g. by looking in eth.c following statement tells it works but how???
> struct ethhdr *eth = (struct ethhdr *)skb->data;
> Also once a structure is assigned values/pushed it in skbuff structure
> can it be possible to change its values at any later stage?
> e.g. in above ethernet case does it possible to change eth->h_proto in
> ethernet driver file(8390.c)


I am not exactly sure what you want to do, but this might help:

A union is a type that can be many types (sort of). These types all use
the same storage space, the space required by the largest type I believe
(unless you have upcast a pointer to a subtype). At any rate, sometimes
you can upcast a member of a union but you do not downcast a union
unless you are 100% sure that the data is laid out the way you are
expecting.

For instance

typedef union {
int type;
struct {
int type;
char *data;
} basic;
struct {
int type;
char *data;
types variables;
...
} complex;
} my_union;

I can upcast a struct complex to a my_union to pass to general
functions. Since I have designed the members of this union to always
have 'int type' as the first member I can then access 'type' in the
union to find out which struct to cast to. Then I downcast to that type
and use the rest of the structure.

If I upcast a struct basic and then dowcast to a struct complex my code
does undefined things. Upcasting a pointer would probably segfault upon
access to any of the variables not in struct basic.

You might say this is not 100% sure to be valid since type might be a
lie. I don't know of any real way to solve that problem but to assume
that the client functions are not using the interface in an invalid
fashion. Client functions must fill in the type information correctly
because you can't even depend on running into a segfault on access or
write. In other words 'assert' won't always run into an error when an
error is present. This is because C doesn't care what you type cast to
or from. Documentation is your 'protection'...

You should always be able to cast *up*, but casting *down* is a
different story. Even though C won't complain your program will not
operate correctly. If the data you want to access is really there it
would have to be in the structure you are looking at anyway. Structures
can attempt to 'split' fields, but I don't think this is portable,
because of type padding, and so most good programmers won't do it unless
absolutely necessary to the task.

Hope that helps. I don't know anything about the particular code you
are looking at, but that is a common way unions are used. They provide
a sort of inheritance scheme for complex types.
 
Reply With Quote
Reply

Thread Tools
Display Modes

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
Newbie in need of some pointers Bernard Network Routers 2 07-06-2006 02:49 PM
Will broadband work in this case? dw Broadband 8 04-28-2006 11:00 PM
Server share permissions work in one case but not another I__Alone@hotmail.com Windows Networking 1 12-21-2005 11:44 AM
URGENT: Why the impersonation work in one case and not the other? =?Utf-8?B?Q3liZXJEaWdnZXI=?= Windows Networking 0 01-07-2005 04:25 AM
Newbie looking for pointers Tom Wireless Networks 1 10-17-2004 01:43 AM



1 2 3 4 5 6 7 8 9 10 11