|
Home > Archive > Unix Programming > March 2004 > question regarding binary data types...
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
question regarding binary data types...
|
|
| Vijay Kakadia 2004-03-23, 8:35 pm |
| Hi all,
I need to generate a UDP packet in the following format,
its required in binary format...
I am wondering what should be my data types for
the feilds in decribed following...
for ex.
uint8_t type;
uint16_t sequence_number;
uint16_t router_id;
.......so on;
Will this do ? if yes, do I need to padding(sending side)
and unpadding(rev. side) ...??
The LS message format should be:
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+
| type |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| sequence number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| TTL |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| origin router id |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| last-hop router id |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| number of responsible routes|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| number of links |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
then for each responsible route:
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| address of route |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| prefix size |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| MED value |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
then for each link:
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| far-end router id |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Thanks in advance,
--VJ
| |
| Pascal Bourguignon 2004-03-23, 11:34 pm |
| Vijay Kakadia <vijaykakadia@linuxmail.org> writes:
> Hi all,
>
> I need to generate a UDP packet in the following format,
> its required in binary format...
>
> I am wondering what should be my data types for
> the feilds in decribed following...
>
> for ex.
>
> uint8_t type;
> uint16_t sequence_number;
> uint16_t router_id;
> ......so on;
>
> Will this do ? if yes, do I need to padding(sending side)
> and unpadding(rev. side) ...??
No, this won't do. Half the time. The protocol specifies the order in
which the bits are to be sent. In a byte, you can assume that the
hardware is wired correctly and will send the bits the most signifiant
first. But the order of the bytes in a word changes from one
processor to the other (some are little endians, some big endians,
some something-else-endians).
It is best to define your packet as a sequence of bytes (uint8_t), and
to define a couple of function to put and get bit sequences in this
packet.
uint32_t get_bit_field(const uint8_t* packet,
int byte_offset,int bit_offset,int bit_width);
void set_bit_field(uint8_t* packet,
int byte_offset,int bit_offset,int bit_width,
uint32_t bits);
(or you could have just one bit_offset,
and recover the byte_offset = bit_offset/8).
Then, taking the example of the IP header, since your diagram is messed,
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|Version| IHL |Type of Service| Total Length |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Identification |Flags| Fragment Offset |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Time to Live | Protocol | Header Checksum |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
version = get_bit_field(packet,0,0, 4);
ihl = get_bit_field(packet,0,4, 4);
tos = get_bit_field(packet,1,0, 8);
tl = get_bit_field(packet,2,0,16);
id = get_bit_field(packet,4,0,16);
flags = get_bit_field(packet,6,0, 3);
fragment = get_bit_field(packet,6,3,13);
// etc
Or, with only a bit offset:
version = get_bit_field(packet, 0, 4);
ihl = get_bit_field(packet, 4, 4);
tos = get_bit_field(packet,16, 8);
tl = get_bit_field(packet,16,16);
id = get_bit_field(packet,32,16);
flags = get_bit_field(packet,48, 3);
fragment = get_bit_field(packet,51,13);
// etc
Manipulation of the bits can easily be done portably, byte by byte,
with the &, |, ~, << and >> operators.
--
__Pascal_Bourguignon__ http://www.informatimago.com/
There is no worse tyranny than to force a man to pay for what he doesn't
want merely because you think it would be good for him.--Robert Heinlein
http://www.theadvocates.org/
|
|
|
|
|