Advertisement

fit ip header into Bit Fields

Started by March 06, 2003 09:05 AM
16 comments, last by Zoomby 21 years, 10 months ago
hi I have a simple struct which I want to use for IP headers. But when I copy a buffer (with the ip header) to the struct''s memory, the both values a and b are flipped. a is not "4" and b is not "5" like it should be. What''s the problem? struct test { unsigned char a: 4; unsigned char b: 4; }; int main() { test t; unsigned char value=69; memcpy(&t,&value,1); cout<<(int)t.a<<endl; cout<<(int)t.b<<endl; } bye chris
t.a should be equal to 69 and t.b will be undefined. The reason is that you are only copying 1 byte into your 2 byte structure (assuming padding is off here.)

Are you trying to put 6 & 9 into your struct?
If that is what you are trying to do then you need an unsigned short variable.

int main(){    test t;    unsigned short value = 1545;  // value 0000110 00001001                                  // value       6        9        // this assumes memory padding/alignment is "off"!    memcpy(&t, &value, sizeof(value));        cout << (int)t.a << cout << (int)t.b << endl;}  


Hope this helps.




[edited by - Digitalfiend on March 6, 2003 12:23:16 PM]
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
Advertisement
Cool problem. I am not a bit field expert, but here is my theory.
Member a of struct test is declared to use 4 bits of an unsigned char. It uses the lowest 4 bits. Member b also uses 4 bits of the same unsigned char and gets the upper 4 bits.
69 = 0x45 = 01000101b
When this is memcpy''d into t, b gets 0100b and a gets 0101b.

Shawn

When using the Windows calculator program, always remember to clear any values from memory before exiting to prevent burn-in.
@Digitalfiend
take a closer look at the structure. it''s not a 2 byte structure, it''s a one byte strucure with two 4 bit vars.

@ShawnO
your right. but that means there''s a kind of endianism within the byte. I thought big and little endian only appears if you got a number with more than one byte?

who does exactly know whats going on?

ciao
chris
K&R says this about bit-fields:
quote:

Fields are assigned left to right on some machines and right to left on others.


In other words, the ordering of the data is implementation defined.

MSDN C Bit Field documentation says this:
quote:

Bit fields are allocated within an integer from least-significant to most-significant bit.


MSDN C++ Bit Field documentation says this:
quote:

The ordering of data declared as bit fields is from low to high bit.


FYI - The C99 standard does not require an implemenation to provide bit fields of type unsigned char.

Shawn
When using the Windows calculator program, always remember to clear any values from memory before exiting to prevent burn-in.
lol I missed those bit-field declarations

Sorry
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
Advertisement
I have no idea what this stuff means
A good question is why are you doing this? What is the point of using bit fields for network I/O? It isn''t really saving you that much data, is it?
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
First of all, it _is_ saving space, second he''s apparently trying to store an IP packet header in this struct, and he can''t just change the layout of IP packets. Now, why he''s trying to mess with IP headers at all is a mystery to me.

cu,
Prefect

Widelands - laid back, free software strategy
@Digitalfiend:

It doesn''t have to do anything with saving space. Imagine you received a buffer with an IP header (and maybe more). The IP header (and many more protocols) have a lot of flags and less than 8 bit-datatypes in it (take a look at RFC:791). So when I have a struct (with bit fields) that has the same layout as the ip header all I have to is a memcpy() from the receive buffer to the IP header-struct (and don''t iterate throught the buffer by hand and extract the parameter or so). Now I can access every parameter in the header easiely.

@Prefect:
I don''t want to change the layout of the IP packets. The struct I use in the first post is just an example of the main problem (the problem was how exactly bit fields are saved in memory)

You need to take a look at IP headers when you are:
-just interested in it (would you believe that? :-) )
-receiving ICMP echo replies.
-programing a packet sniffer.

bye
chris

This topic is closed to new replies.

Advertisement