1 struct ip { 2 #if BYTE_ORDER == LITTLE_ENDIAN 3 u_char ip_hl:4, /* header length */ 4 ip_v:4; /* version */ 5 #endif 6 #if BYTE_ORDER == BIG_ENDIAN 7 u_char ip_v:4, /* version */ 8 ip_hl:4; /* header length */ 9 #endif10 u_char ip_tos; /* type of service */11 short ip_len; /* total length */12 u_short ip_id; /* identification */13 short ip_off; /* fragment offset field */14 #define IP_DF 0x4000 /* dont fragment flag */15 #define IP_MF 0x2000 /* more fragments flag */16 u_char ip_ttl; /* time to live */17 u_char ip_p; /* protocol */18 u_short ip_sum; /* checksum */19 struct in_addr ip_src,ip_dst; /* source and dest address */20 };
1 struct iphdr { 2 #if defined(__LITTLE_ENDIAN_BITFIELD) 3 __u8 ihl:4, 4 version:4; 5 #elif defined (__BIG_ENDIAN_BITFIELD) 6 __u8 version:4, 7 ihl:4; 8 #else 9 #error "Please fix"10 #endif11 __u8 tos;12 __u16 tot_len;13 __u16 id;14 __u16 frag_off;15 __u8 ttl;16 __u8 protocol;17 __u16 check;18 __u32 saddr;19 __u32 daddr;20 /*The options start here. */21 };
struct ip 和struct iphdr
struct ip and struct iphdr are two different definitions of the same underlying structure, brought in from different places.
struct ip is defined in <netinet/ip.h>, which is a reasonably standard header on UNIX systems.
struct iphdr is defined in <linux/ip.h>. This header (and structure) are Linux-specific, and will not be present in other operating systems.
If you're not sure which one to use, use struct ip; code which uses this structure is more likely to be portable to non-Linux systems.
struct icmp and struct icmphdr are a messier situation:<netinet/icmp.h> defines both struct icmp and struct 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>.First: Don't include <linux/icmp.h> unless you have a very good reason. You cannot include both headers -- they will conflict -- and most software will expect the netinet definition.Second: struct icmphdr is, as the name implies, the header. struct icmp defines the contents of a structured ICMP message, like a destination unreachable message.
(2)<netinet/*.h> 和 <linux/*.h>
The linux/*.h headers were really meant for internal kernel use and if Linux were being created today, these files would not even exist under /usr/include. But early on, a lot of the userspace libc (libc4 and libc5 at the time) relied on Linux headers to define types, constants, structures, etc. for use in userspace, so netinet/in.h contained just #include <linux/in.h> or similar, and the lovely tradition got started. Today the only headers in the linux tree that should be used for userspace apps are some things related to supporting specific hardware at a low level, like the Linux console, framebuffer, video4linux, etc.
In short, you should use netinet/in.h (the standard header specified by POSIX) and pretend you never saw linux/in.h. :-)
--------------------- 作者:zhongyoubing 来源:CSDN 原文:https://blog.csdn.net/zhongyoubing/article/details/77434719 版权声明:本文为博主原创文章,转载请附上博文链接!