How do I calculate IP-header length? - networking

I want to calculate IP-header length with following statement:
Header Length is a four-bit field that tells, as the name implies, the
length of the IP header in 32-bit words
Now I'm getting difficulties in calculating IP header length (minimum and maximum), with four-bit field.

The minimum length of an IPv4 header of a valid datagram is 20 bytes, when the value reads 5 and there are no options
Since the field is 4 bits wide, the maximum value it can store is 15, thus the maximum length of the header is 60 bytes.
Internet Header Length is the length of the internet header in 32
bit words, and thus points to the beginning of the data. Note that
the minimum value for a correct header is 5.
Which means whatever value is stored in the IHL, it should be multiplied with 32 to get the total number of bits, or with 4 to get the total number of bytes.

minimum value of header length is 20 Bytes but we don't have sufficient bits to represent 20 so we use scaling technique, i.e. 0101 (5) will represent 4 X 5 = 20 Bytes, here scaling factor is 4.
maximum value possible with 4 bits is 15.
So maximum header length possible is 4 X 15 = 60 Bytes.
Header Length | Header Length Field
20 -----> 5
24 -----> 6
28 -----> 7
.
.
.
60 -----> 15
if header length is 22 Bytes then we use padding to make it a multiple of 4 i.e. 24 Bytes

#Amit>Value in the HL filed is = the number of 4 bytes in the total IP header length.Means if the header length field is say 40,then calculate how many 4 bytes in 40?its 40/4= 10.So value in the HL field is 10
MINIMUM HL value
Minimum HL is 20 byte .ie no of 4bytes in 20 = 20/4=5.So minimum value in HL field is 5.
MAXIMUM HL value
HL is a 4 bit field.so the maximum vale that can be accomadated in that field is 15(1111) or you can calculate using the formulae 2^4-1=15.So max no: of 4 bytes can be 15.Hence the Max header length=15*4=60bytes.
Hope now things are clear.

The Internet Header Length (IHL) field is the number of 32-bit words(=4 bytes)
in the IPv4 header, including any options. Because this is also a
4-bit field, the IPv4 header is limited to a maximum of fifteen 32-bit
words(=60 bytes)
So if the value of IHL is 0101(5), then the IP-header's length is 5*4(bytes) = 20(bytes)

Related

When dealing with hexadecimal numbers how do I use bit offset and length to get a value?

I have the following number
0000C1FF61A40000
The offset or start is 36 or 0x23
The length of the number is 12 or 0xc
Can someone help me understand how to get the resulting value? I thought the offset meant what pair of hex numbers to start with and then length would be how many to grab. There definitely aren't 36 pairs, only 8. Not sure how I'd do a length of 12 with only 8.
Each hex digit represents four binary bits. Therefore your offset of 36 bits (which BTW is 0x24, not 0x23) is equivalent to 9 hex digits. So discard the rightmost 9 digits from your original number, leaving you with 0000C1F.
Then the length of the number you want is 12 bits, which is 3 hex digits. So discard all but the rightmost 3 digits, leaving you with C1F as the answer.
If the numbers of bits had not been nice multiples of 4 then you would have had to convert the original hex number into binary, then discard offset number of bits from the right, retain only the rightmost length bits from the result, and finally convert those length bits back into hex.

Calculate number of bytes sent in TCP session

I currently learn about TCP protocol and about 3 way-handshake. I cannot figure out how I can calculate the number of bytes transmited in this TCP session. I understand that in the first 3 and in the last 4 it's connection establishment and closing connection but between 4-11 I don't know to count the bytes
TCP session
Looking at frames 4 to 6 of your attachment:
172.20.1.21.1303 > 172.20.0.81.23: P 1:22(21) ack 1 win 65535
172.20.0.81.23 > 172.20.1.21.1303: P 1:13(12) ack 22 win 61299
172.20.1.21.1303 > 172.20.0.81.23: P 22:25(3) ack 13 win 65523
Frame 4 is from host A to B. The first number (1) after the 'P' flag is the (relative) sequence number of the first data byte of this segment, and the number in brackets (21) is the length of that segment in bytes.
Frame 5 is response from host B to A. The value after 'ack' (22) is host B telling host A that B has received bytes 1 to 21, and that it expects sequence number 22 next.
Frame 6 is the next segment from A to B. Sure enough, the sequence number is 22, which matches what B is expecting, and this time the length is 3 bytes.
If you then look at the final frames of the sequence, we can see in frame 12 that Host B has a sequence number of 1052, meaning it has sent 1052 bytes over the course of the connection (and the ack in frame 13 confirms this). Similarly, frame 14 shows that Host A sent 107 bytes (and the ack in frame 15 confirms this).

SQLite: How to reduce byte size of integer values?

I have a SQLite table (without row ID, but that's probably irrelevant, and without any indexes) where my rows contain the following data:
2 real values, one of which is the primary key
3 integers < 100
1 more field for integers, but currently always null
According to http://www.sqlite.org/datatype3.html, integer values can take 1, 2, 3, 4, 6 or 8 bytes according to their magnitude. Therefore I'd expect each row in my table to take up about 20 bytes. In reality, sqlite3_analyzer gives me for the table
Average payload per entry......................... 25.65
Maximum payload per entry......................... 26
which is somewhere in between the minimum value of 20 and the maximum of 32 (if all integers were stored with 4 bytes). Is it possible to give the DB a "hint" to use even smaller integer types wherever possible? Or how else can the discrepancy be explained? (I don't think it's indexes because there are none for this table.)
Similarly, on a previous table I had 2 real values + 2 small integers and each entry occupied slightly more than 24 bytes (which is also more than I would have expected).
Also, there is no way to store floats in single precision with SQLite right?
The actual record format has one integer for the header size, one integer for each column to describe the value's type, and all the data of the column values.
In this case, we have:
bytes
1 header size
6 column types
16 two real values
3 three small integers between 2 and 127
0 NULL
--
26

Header data aligned to natural length?

I was looking through RFC 3550 and ran across this line and was not sure what it meant:
All header data is aligned to its natural length, i.e., 16-bit fields are aligned on even offsets, 32-bit
fields are aligned at offsets divisible by four, etc. Octets designated as padding have the value zero.
I tried looking around to figure out using keyword searches but haven't had any luck. Any help is appreciated.
Fields that are 16 bit long (2 bytes) will start at offsets divisible by 2 bytes (byte 0, 2, 4, etc). Fields that are 32 bits will start at byte 0, 4, 8, etc.
The offset is relative to the start of the header (and of the packet).
For example, if you have a header with a 16 bit (2 byte) field A and a 32 bit (4 byte) field B it will have to be arranged as follows:
A : 2 bytes
padding(zero) : 2 bytes
B : 4 bytes
Here A starts at offset 0 and B starts at offset 4.

Total length in IPv4 header

I am trying to learn about IPV4 headers and used Wireshark to take a look at some packets.
The total length (header length + data length) is 76 bytes. The header length is 20 bytes and the data length is 24 bytes. Were is the other 32 bytes in the total length field coming from?
I think you're missing the TCP header. The data payload is indeed 24 bytes, IPv4 header is 20 bytes, TCP header must be (76 - 24 - 20) = 32 b.

Resources