简体   繁体   English

struct ip 和 struct iphdr 的区别

[英]Difference between struct ip and struct iphdr

I am trying to understand how the network is working, i'm doing some test, sending some package... anyway我试图了解网络是如何工作的,我正在做一些测试,发送一些包裹......无论如何

My point is that i can't find the real difference between "protocol" structure and "protocol header" structure .我的观点是我找不到"protocol" structure"protocol header" structure之间的真正区别。

For the ip structure, they both sized 20 bytes.对于 ip 结构,它们的大小均为 20 字节。 but for exemple:但例如:

  • struct ip and struct iphdr sized 20 bytes struct ipstruct iphdr大小为 20 字节
  • struct icmp sized 28 bytes struct icmp大小为 28 字节
  • struct icmphdr sized 8 bytes struct icmphdr大小为 8 个字节

I'm guessing that the struct icmp include a struct ip/iphdr?我猜struct icmp包含一个struct ip/iphdr? ? ?

And there is the same kind of structure with every protocol i have seen.我见过的每个协议都有相同的结构。 struct udp / struct udphdr , struct udp / struct udphdr ,

Is it link to IP_HDRINCL set on with setsockopt() ?它是否链接到使用setsockopt()设置的IP_HDRINCL

So my question is What is the real difference between them ?所以我的问题是它们之间的真正区别是什么? And When use the good one.什么时候用好的。

ip and iphdr struct: 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. */
};

And IP HDRIP 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 structure code here : https://www.cymru.com/Documents/ip_icmp.h ICMP结构代码在这里: https : //www.cymru.com/Documents/ip_icmp.h

struct ip and struct iphdr are two different definitions of the same underlying structure, brought in from different places. struct ipstruct iphdr是相同底层结构的两个不同定义,从不同的地方引入。

struct ip is defined in <netinet/ip.h> , which is a reasonably standard header on UNIX systems. struct ip<netinet/ip.h>定义,它是UNIX系统上合理标准的头。

struct iphdr is defined in <linux/ip.h> . struct iphdr<linux/ip.h>定义。 This header (and structure) are Linux-specific, and will not be present in other operating systems. 此标头(和结构)是特定于Linux的,并且不会出现在其他操作系统中。

If you're not sure which one to use, use struct ip ; 如果你不确定使用哪一个,请使用struct ip ; code which uses this structure is more likely to be portable to non-Linux systems. 使用此结构的代码更有可能移植到非Linux系统。


struct icmp and struct icmphdr are a messier situation: struct icmpstruct icmphdr是一个更混乱的情况:

  • <netinet/icmp.h> defines both struct icmp and struct icmphdr . <netinet/icmp.h>定义 struct icmpstruct icmphdr
  • <linux/icmp.h> also defines struct icmphdr , with a similar structure (but, as usual, different field names) as the definition from <netinet/icmp.h> . <linux/icmp.h> struct icmphdr <linux/icmp.h>还定义了struct icmphdr ,它具有与<netinet/icmp.h> struct icmphdr <linux/icmp.h>定义类似的结构(但通常是不同的字段名称)。

First: Don't include <linux/icmp.h> unless you have a very good reason. 第一:除非你有充分的理由,否则不要包含<linux/icmp.h> You cannot include both headers -- they will conflict -- and most software will expect the netinet definition. 不能包含两个标题 - 它们会发生冲突 - 并且大多数软件都会期望netinet定义。

Second: struct icmphdr is, as the name implies, the header. 第二: struct icmphdr顾名思义就是标题。 struct icmp defines the contents of a structured ICMP message, like a destination unreachable message. 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