struct ip和struct iphdr的区别

17

我正在尝试了解网络是如何工作的,正在进行一些测试,发送一些数据包...无论如何

我的观点是我找不到"协议结构""协议头结构"之间的真正区别。

对于IP结构,它们都有20个字节的大小。 但例如:

  • struct ipstruct iphdr大小为20个字节
  • struct icmp大小为28个字节
  • struct icmphdr大小为8个字节

我猜struct icmp包含一个struct ip/iphdr?

每个我见过的协议都有同样类型的结构。 struct udp / struct udphdr,

这是否与使用setsockopt()设置的IP_HDRINCL有关?

所以我的问题是它们之间的真正区别是什么?使用哪个好。

ip和iphdr结构:

struct iphdr {
    #if defined(__LITTLE_ENDIAN_BITFIELD)
        __u8    ihl:4,
                version:4;
    #elif defined (__BIG_ENDIAN_BITFIELD)
        __u8    version:4,
                ihl:4;
    #else
        #error  "Please fix <asm/byteorder.h>"
    #endif
         __u8   tos;
         __u16  tot_len;
         __u16  id;
         __u16  frag_off;
         __u8   ttl;
         __u8   protocol;
         __u16  check;
         __u32  saddr;
         __u32  daddr;
         /*The options start here. */
};

还有IP HDR

struct ip {
#if BYTE_ORDER == LITTLE_ENDIAN 
    u_char  ip_hl:4,        /* header length */
        ip_v:4;         /* version */
#endif
#if BYTE_ORDER == BIG_ENDIAN 
    u_char  ip_v:4,         /* version */
        ip_hl:4;        /* header length */
#endif
    u_char  ip_tos;         /* type of service */
    short   ip_len;         /* total length */
    u_short ip_id;          /* identification */
    short   ip_off;         /* fragment offset field */
#define IP_DF 0x4000            /* dont fragment flag */
#define IP_MF 0x2000            /* more fragments flag */
    u_char  ip_ttl;         /* time to live */
    u_char  ip_p;           /* protocol */
    u_short ip_sum;         /* checksum */
    struct  in_addr ip_src,ip_dst;  /* source and dest address */
};

这里是ICMP结构代码:https://www.cymru.com/Documents/ip_icmp.h


为什么这两段代码没有使用相同的类型名称?请注意,除了 "_Bool、int、signed int、unsigned int" 之外的位域类型(如上所示)是实现定义行为。 - chux - Reinstate Monica
Chux,我没有创建这个结构,它们都在netinet/ip.h中定义。 - albttx
3
IPv4头部的最小尺寸为20个八位字节,但选项可能会使其更大。ICMP是一种作为IP负载封装的协议,它的头部不是IP头部。 - Ron Maupin
IP 伪头 谷歌是你的朋友。 - wildplasser
现在的情况似乎有些不同。当我在CentOS 7中打开/usr/include/netinet/ip.h文件时,我发现struct iphdr和struct ip的定义同时存在于该文件中。 - Chuancey
显示剩余5条评论
1个回答

28

struct ipstruct iphdr是相同底层结构的两个不同定义,分别来自不同的位置。

struct ip定义在<netinet/ip.h>头文件中,在UNIX系统中是一个相当标准的头文件。

struct iphdr定义在<linux/ip.h>中。这个头文件(和结构)是特定于Linux的,在其他操作系统中将不存在。

如果您不确定使用哪个,请使用struct ip;使用此结构的代码更有可能在非Linux系统上移植。


struct icmpstruct icmphdr是一个比较混乱的情况:

  • <netinet/icmp.h>定义了struct icmpstruct icmphdr
  • <linux/icmp.h>也定义了struct icmphdr,具有类似的结构(但通常使用不同的字段名称)作为来自<netinet/icmp.h>的定义。

首先:除非你有非常好的理由,否则不要包含<linux/icmp.h>。您不能同时包含两个头文件--它们会发生冲突--并且大多数软件将期望netinet定义。

其次:struct icmphdr是头部,正如名称所示。 struct icmp定义了结构化ICMP消息的内容,例如目标不可达消息。


谢谢!那么 icmp / icmphdr 怎么样? - albttx
1
对于在Linux系统之外使用struct ip的可移植性建议,给予一点赞。 - AgnosticDev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接