为什么原始 ping 源代码中将 ICMP ECHO 数据长度与 timeval 的大小进行比较?
我一直在阅读原始的 ping 程序代码(http://www.ping127001.com/pingpage /ping.text)出于兴趣,只是想看看它是如何完成的。
我明白了大部分内容,但有一个条件我不明白:
if (datalen >= sizeof(struct timeval)) /* can we time 'em? */
timing = 1;
其中 datalen
是回显有效负载的长度。
我在其他 C ping 实现中看到过类似的谓词。为什么数据长度小于 timeval 结构的大小会禁止计时?
编辑:不可避免的深夜德普时刻。
I've been reading through the original ping program code (http://www.ping127001.com/pingpage/ping.text) out of interest, just to see how it was done.
I get most of it, but there is one conditional that I don't understand:
if (datalen >= sizeof(struct timeval)) /* can we time 'em? */
timing = 1;
Where datalen
is the length of the echo payload.
I've seen similar predicates in other C ping implementations. Why is it that a data length smaller than the size of a timeval struct prohibits timing?
EDIT: Inevitable late-night derp moment.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为如果您想在数据包中实际存储计时数据,则需要确保数据包足够大以存储计时数据。换句话说,计时是通过将
timeval
结构放入有效负载区域来实现的。例如,如果您在
timeval
结构的大小为 20 时为 ICMP 有效负载区域指定长度为 3,则尝试插入它并不是一个好主意:-)That's because you need to ensure the packets are big enough to store timing data if you want to actually store timing data in them. In other words, timing works by placing a
timeval
structure into the payload area.If, for example, you specified a length of 3 for the ICMP payload area when the size of the
timeval
structure was 20, it wouldn't be a good idea trying to insert it :-)