我试图了解网络是如何工作的,我正在做一些测试,发送一些软件包……无论如何
我的观点是,我找不到“协议”结构和“协议头”结构之间的真正区别.
对于ip结构,它们都大小为20个字节.
但例如:
> struct ip和struct iphdr大小为20个字节
> struct icmp大小为28个字节
> struct icmphdr大小为8个字节
我猜结构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 */
};
解决方法
struct ip和struct iphdr是相同底层结构的两个不同定义,从不同的地方引入.
struct ip在< netinet / ip.h>中定义,它是UNIX系统上合理标准的标头.
struct iphdr在< linux / ip.h>中定义.此标头(和结构)是特定于Linux的,不会出现在其他操作系统中.
如果你不确定使用哪一个,请使用struct ip;使用此结构的代码更有可能移植到非Linux系统.
struct icmp和struct icmphdr是一个更混乱的情况:
>< netinet / icmp.h>定义struct icmp和struct icmphdr.
>< linux / icmp.h>还定义了struct icmphdr,它具有与< netinet / icmp.h>中的定义类似的结构(但通常是不同的字段名称).
第一:不要包含< linux / icmp.h>除非你有充分的理由.您不能包含两个标题 – 它们会发生冲突 – 并且大多数软件都会期望netinet定义.
第二:struct icmphdr顾名思义就是标题. struct icmp定义结构化ICMP消息的内容,如目标不可达消息.

