Libnet TCP 校验和错误
我正在尝试使用 libnet 库构建 TCP
数据包。我在 libnet_build_tcp
函数中使用 '0'
自动计算校验和值。然而,在计算时,校验和似乎忽略了伪标头,导致由于校验和错误而导致无用的数据包。有谁知道如何解决这个问题?
I am trying to build a TCP
packet using libnet library. I use '0'
for autocomputation of checksum value in the libnet_build_tcp
function. However, it seems the checksum ignores the pseudo-header
when being computed resulting in a useless packet because of checksum error. Does anyone know how to solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我在代码中看到的,只要您没有在
sum< 中使用
libnet_toggle_checksum(l, ptag, 1);
您的0
libnet_build_tcp()
的 /code> 参数应该会通过调用libnet_pblock_setflags(p, LIBNET_PBLOCK_DO_CHECKSUM)
。我无法真正告诉您如何解决它,但既然您是构建数据包的人,也许您可以选择创建自己的校验和?
TCP 伪报头是一个 12 字节字段,包含源 IP 地址和目标 IP 地址(均为 4 字节)、保留字段(2 字节且始终设置为零)以及 4 字节 TCP 段长度(等于 IP 地址大小)。 TCP 标头 + 负载长度。
你可以创建这样的东西:
首先创建你的变量:
然后创建实际的函数,其中 b->len 是数据包的总大小(只需添加所有标头+有效负载并获取总大小),你就可以了将标头和数据 memcpy 到 pkt_data:
只需使用 https://www.rfc-editor.org/rfc/rfc1071 计算缓冲区上的校验和:
我在现实环境中使用它以 500 万 PPS 计算校验和。
As far as I can see in the code, as long as you haven't used
libnet_toggle_checksum(l, ptag, 1);
your0
in thesum
parameter oflibnet_build_tcp()
should be causing it to autocompute a checksum for you by callinglibnet_pblock_setflags(p, LIBNET_PBLOCK_DO_CHECKSUM)
.I couldn't really tell you how to solve it but since you are the one building the packet maybe you could opt for creating your own checksum?
The TCP pseudoheader is a 12 byte field containing the source and destination IP addresses which are both 4 bytes each, a reserved field which is 2 bytes and always set to zeros, and a 4 byte TCP segment length which is equal to the size of the TCP header + the payload length.
You could create something like this:
First create your variables:
Then create the actual function where b->len is the total size of the packet (just add all the headers + the payload and get the total size) and you would just have to memcpy your header and data to pkt_data:
And just use the checksum function provided by https://www.rfc-editor.org/rfc/rfc1071 to calculate the checksum over the buffer:
I use this in a realistic environment to calculate checksums at 5 million PPS.