linux – 确定IP头的大小.为什么它与其他人不同?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了linux – 确定IP头的大小.为什么它与其他人不同?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1339字,纯文字阅读大概需要2分钟。
内容图文
![linux – 确定IP头的大小.为什么它与其他人不同?](/upload/InfoBanner/zyjiaocheng/969/3be21bbde5054881b431b3686d12168f.jpg)
看看这段代码:
if(ip_header->protocol == IPPROTO_TCP)
{
tcp_header = (struct tcphdr*)(packet + sizeof(struct ethhdr) + ip_header->ihl*4);
/* Print the Dest and Src ports */
printf("Source Port: %d\n", ntohs(tcp_header->source));
printf("Dest Port: %d\n", ntohs(tcp_header->dest));
}
我感到困惑的是,如果确定我们通常做的其他标头的大小,sizeof(struct tcphdr)或sizeof(struct ethhdr),但是对于IP标头大小,我们不做sizeof而是做ip_header-> ihl * 4.为什么会这样?这4个是什么?
以下是IP heder的结构声明:
00116 struct iphdr {
00117 #if defined(__LITTLE_ENDIAN_BITFIELD)
00118 __u8 ihl:4,
00119 version:4;
00120 #elif defined (__BIG_ENDIAN_BITFIELD)
00121 __u8 version:4,
00122 ihl:4;
00123 #else
00124 #error "Please fix <asm/byteorder.h>"
00125 #endif
00126 __u8 tos;
00127 __u16 tot_len;
00128 __u16 id;
00129 __u16 frag_off;
00130 __u8 ttl;
00131 __u8 protocol;
00132 __u16 check;
00133 __u32 saddr;
00134 __u32 daddr;
00135 /*The options start here. */
00136 };
解决方法:
这是不同测量单位的问题. IP头中的IHL字段定义如下:
Internet Header Length is the length of the internet header in 32 bit
words.
并且此大小不固定(因为有效但不鼓励的IP选项).因此,一个数据包可能有IHL = 5,下一个数据包可能有IHL = 6,依此类推.
内容总结
以上是互联网集市为您收集整理的linux – 确定IP头的大小.为什么它与其他人不同?全部内容,希望文章能够帮你解决linux – 确定IP头的大小.为什么它与其他人不同?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。