我正在尝试了解网络是如何工作的,正在进行一些测试,发送一些数据包...无论如何
我的观点是我找不到"协议结构"
和"协议头结构"
之间的真正区别。
对于IP结构,它们都有20个字节的大小。 但例如:
struct ip
和struct 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