IP 标头的分解

发布于 2024-08-25 10:57:20 字数 309 浏览 7 评论 0原文

我必须做一个嗅探器作为安全课程的作业。我正在使用 C 和 pcap 库。我一切都运转良好(因为我从互联网上获得了代码并进行了更改)。但我对代码有一些疑问。

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

ih 的类型为 ip_header,它当前指向数据包中的 IP 标头。
ver_ihl 给出 IP 的版本。
我不知道什么是: & 0xf) * 4;

I have to do a sniffer as an assignment for the security course. I am using C and the pcap library. I got everything working well (since I got a code from the internet and changed it). But I have some questions about the code.

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

ih is of type ip_header, and its currently pointing the to IP header in the packet.
ver_ihl gives the version of the IP.
I can't figure out what is: & 0xf) * 4;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

↙厌世 2024-09-01 10:57:20

&是按位与运算符,在本例中,您将 ver_ihl 与 0xf 进行与运算,其效果是清除除最低有效位 4

0xff & 之外的所有位。 0x0f = 0x0f

ver_ihl 定义为前 4 位 = 版本 + 第二个 4 = 互联网标头长度。 and 运算删除版本数据,仅留下长度数据。长度被记录为 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

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

0xff & 0x0f = 0x0f

ver_ihl is defined as first 4 bits = version + second 4 = Internet header length. The and operation removes the version data leaving the length data by itself. The length is recorded as count of 32 bit words so the *4 turns ip_len into the count of bytes in the header

In response to your comment:

bitwise and ands the corresponding bits in the operands. When you and anything with 0 it becomes 0 and anything with 1 stays the same.

0xf = 0x0f = binary 0000 1111

So when you and 0x0f with anything the first 4 bits are set to 0 (as you are anding them against 0) and the last 4 bits remain as in the other operand (as you are anding them against 1). This is a common technique called bit masking.

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

安穩 2024-09-01 10:57:20

阅读定义 IPv4 的 RFC 791

互联网标头内容摘要如下:

<前><代码> 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
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- +-+-+-+-+-+-+-+
|版本|国际人道法 |服务类型|总长度|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- +-+-+-+-+-+-+-+

IP 标头的前 8 位是版本和 IHL 字段的组合。

IHL:4 位

Internet 标头长度是 Internet 标头的长度,单位为 32
位字,因此指向数据的开头。注意
正确标头的最小值是 5。

您所拥有的代码正在执行的操作是取出前 8 位,并删除 IHL 部分,然后将其转换为字节数。与0xF的按位AND将隔离IHL字段,并且乘以4是因为32位字中有4个字节。

Reading from RFC 791 that defines IPv4:

A summary of the contents of the internet header follows:

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

The first 8 bits of the IP header are a combination of the version, and the IHL field.

IHL: 4 bits

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.

What the code you have is doing, is taking the first 8 bits there, and chopping out the IHL portion, then converting it to the number of bytes. The bitwise AND by 0xF will isolate the IHL field, and the multiply by 4 is there because there are 4 bytes in a 32-bit word.

贪恋 2024-09-01 10:57:20

ver_ihl 字段包含两个 4 位整数,打包为低位和高位 nybble。长度指定为 32 位字的数量。因此,如果您有一个版本 4 IP 帧,带有 20 个字节的标头,则 ver_ihl 字段值为 69。然后您将得到以下结果:

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

所以,是的,“&0xf”屏蔽了低 4 位位。

The ver_ihl field contains two 4-bit integers, packed as the low and high nybble. The length is specified as a number of 32-bit words. So, if you have a Version 4 IP frame, with 20 bytes of header, you'd have a ver_ihl field value of 69. Then you'd have this:

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

So, yes, the "&0xf" masks out the low 4 bits.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文