簡體   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