title: Difference between struct ip and struct iphdr
body: <p>I am trying to understand how the network is working, i'm doing some test, sending some package... anyway</p>
  
  <p>My point is that i can't find the real difference between <code>"protocol" structure</code> and <code>"protocol header" structure</code>.</p>
  
  <p>For the ip structure, they both sized 20 bytes.
  but for exemple:</p>
  
  <ul>
  <li><code>struct ip</code> and <code>struct iphdr</code> sized 20 bytes</li>
  <li><code>struct icmp</code> sized 28 bytes</li>
  <li><code>struct icmphdr</code> sized 8 bytes</li>
  </ul>
  
  <p>I'm guessing that the <code>struct icmp</code> include a <code>struct ip/iphdr?</code> ?</p>
  
  <p>And there is the same kind of structure with every protocol i have seen.
  <code>struct udp</code> / <code>struct udphdr</code>, </p>
  
  <p>Is it link to <code>IP_HDRINCL</code> set on with <code>setsockopt()</code> ?</p>
  
  <p>So my question is What is the real difference between them ? And When use the good one.</p>
  
  <p><strong>ip and iphdr</strong> struct:</p>
  
  <pre><code>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 &lt;asm/byteorder.h&gt;"
      #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. */
  };
  </code></pre>
  
  <p>And <strong>IP HDR</strong></p>
  
  <pre><code>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 */
  };
  </code></pre>
  
  <p>ICMP structure code here : <a href="https://www.cymru.com/Documents/ip_icmp.h" rel="noreferrer">https://www.cymru.com/Documents/ip_icmp.h</a></p>