My sniffer that uses raw sockets displays some data related to ip header in a wrong way, three unsigned shorts in particular. The rest of data output seems to be correct.
This is a piece of output of random packet:
Header information:
45 00 00 3c 73 85 00 00 80 11 45 72 c0 a8 00 60 E..<s.....Er...`
c0 a8 00 09 ....
1) IP header length: 5
2) IP protocol version: 4
3) Type of service : 0
4) Total length : 15360 - wrong!
5) Identification : 34163 - wrong!
6) Fragment offset field : 0
7) More fragment: 0
8) Don't' fragment: 0
9) Reserver to zero: 0
10) Fragment offset again: 0
11) Time to live: 128
12) Protocol: 17
13) IP checksum: 29253 - wrong!
14) Source: 192.168.0.96
15) Destination: 192.168.0.9
|
This is my data structure for IP header:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
struct IP_HEADER
{
unsigned char iph_header_len :4;
unsigned char iph_version :4;
unsigned char iph_tos;
unsigned short iph_total_length;
unsigned short iph_id;
unsigned char iph_frag_offset :5;
unsigned char iph_more_fragment :1;
unsigned char iph_dont_fragment :1;
unsigned char iph_reserved_zero :1;
unsigned char iph_frag_offset1;
unsigned char iph_ttl;
unsigned char iph_protocol;
unsigned short iph_checksum;
IN_ADDR iph_source;
IN_ADDR iph_destination;
};
|
Let's take packet length value, it is byte 3 and 4.
00 3c in hex equals 60 in decimal. This is correct value, packet is 60 bytes long, this is what output is supposed to show.
But whenever I use ip_header->iph_header_len to output data, two bytes that unsigned short occupies for some reason get twisted. Instead of 60 I get 15360 which equals 3c 00 in hex. This happens for each of three unsigned shorts in this structure. Why are they changing order?
What's wrong? I suppose this is related to alignment because it's not the first time I come across a problem with shorts inside structs. Compiler is MSVC 2010.