IP header decomposition

I have to make a sniffer as an appointment for a safety course. I am using C and the pcap library. Everything works fine for me (since I received the code from the Internet and changed it). But I have some questions about the code.

u_int ip_len = (ih->ver_ihl & 0xf) * 4;   

ihhas a type ip_header, and its current indicates the IP header in the packet.
ver_ihlprovides an IP version.
I can not understand what is:& 0xf) * 4;

+5
source share
3 answers

& is bitwise and operator, in this case you are ver_ihl with 0xf, which has the effect of clearing all bits other than the least significant 4

0xff and 0x0f = 0x0f

ver_ihl 4 = + 4 = . "" , . 32- , * 4 ip_len

:

, - . - 0, 0, 1 .

0xf = 0x0f = 0000 1111

, 0x0f -, 4 0 ( 0), 4 , ( 1). , .

http://en.wikipedia.org/wiki/Bitwise_operation#AND

+3

RFC 791, IPv4:

-:

 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         |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

8 IP- IHL.

IHL: 4

- - 32    , , . ,    - 5.

, 8 IHL, . 0xF IHL, 4 - 32- 4 .

+1

ver_ihl 4- , nybble. 32- . , IP- 4 20 , ver_ihl 69. :

  01000101
& 00001111
  --------
  00000101

, , "& 0xf" 4 .

0

All Articles