union field length mismatch

L

linux.lover2004

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.
 
M

Michael Knaup

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.
 
S

SM Ryan

# 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.
 
J

Joe Pfeiffer

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top