繁体   English   中英

struct ip 和 struct iphdr 的区别

[英]Difference between struct ip and struct iphdr

我试图了解网络是如何工作的,我正在做一些测试,发送一些包裹......无论如何

我的观点是我找不到"protocol" structure"protocol header" structure之间的真正区别。

对于 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

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 <linux/icmp.h>还定义了struct icmphdr ,它具有与<netinet/icmp.h> struct icmphdr <linux/icmp.h>定义类似的结构(但通常是不同的字段名称)。

第一:除非你有充分的理由,否则不要包含<linux/icmp.h> 不能包含两个标题 - 它们会发生冲突 - 并且大多数软件都会期望netinet定义。

第二: struct icmphdr顾名思义就是标题。 struct icmp定义结构化ICMP消息的内容,如目标不可达消息。

现在的情况好像有点不一样了。在centos 7打开/usr/include/netinet/ip.h文件时,发现文件中同时存在struct iphdr和struct ip的定义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM