struct ip 和 struct iphdr 的区别
Difference between struct ip and struct iphdr
我正在尝试了解网络是如何工作的,我正在做一些测试,发送一些包裹...无论如何
我的观点是我找不到 "protocol" structure
和 "protocol header" structure
之间的真正区别。
对于ip结构,它们的大小都是20字节。
但例如:
struct ip
和 struct iphdr
大小为 20 字节
struct icmp
大小为 28 字节
struct icmphdr
大小为 8 个字节
我猜 struct icmp
包含一个 struct ip/iphdr?
?
我见过的每个协议都有相同的结构。
struct udp
/ struct udphdr
,
是否link到IP_HDRINCL
设置为setsockopt()
?
所以我的问题是它们之间的真正区别是什么?什么时候用好的。
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 ip
和struct iphdr
是同一底层结构的两个不同定义,来自不同的地方。
struct ip
在 <netinet/ip.h>
中定义,这是 UNIX 系统上的合理标准 header。
struct iphdr
是在 <linux/ip.h>
中定义的。此 header(和结构)是 Linux-specific,不会出现在其他操作系统中。
如果您不确定使用哪一个,请使用 struct ip
;使用这种结构的代码更有可能移植到 non-Linux 系统。
struct icmp
和 struct icmphdr
情况比较混乱:
<netinet/icmp.h>
定义了 struct icmp
和 struct icmphdr
.
<linux/icmp.h>
还定义了 struct icmphdr
,其结构与 <netinet/icmp.h>
. 中的定义类似(但字段名称通常不同)
首先:不要包含 <linux/icmp.h>
除非你有充分的理由。您 不能 包括两个 header -- 它们会发生冲突 -- 大多数软件都需要 netinet 定义。
其二:struct icmphdr
顾名思义就是header。 struct icmp
定义结构化 ICMP 消息的内容,如目标无法到达的消息。
我正在尝试了解网络是如何工作的,我正在做一些测试,发送一些包裹...无论如何
我的观点是我找不到 "protocol" structure
和 "protocol header" structure
之间的真正区别。
对于ip结构,它们的大小都是20字节。 但例如:
struct ip
和struct iphdr
大小为 20 字节struct icmp
大小为 28 字节struct icmphdr
大小为 8 个字节
我猜 struct icmp
包含一个 struct ip/iphdr?
?
我见过的每个协议都有相同的结构。
struct udp
/ struct udphdr
,
是否link到IP_HDRINCL
设置为setsockopt()
?
所以我的问题是它们之间的真正区别是什么?什么时候用好的。
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 ip
和struct iphdr
是同一底层结构的两个不同定义,来自不同的地方。
struct ip
在 <netinet/ip.h>
中定义,这是 UNIX 系统上的合理标准 header。
struct iphdr
是在 <linux/ip.h>
中定义的。此 header(和结构)是 Linux-specific,不会出现在其他操作系统中。
如果您不确定使用哪一个,请使用 struct ip
;使用这种结构的代码更有可能移植到 non-Linux 系统。
struct icmp
和 struct icmphdr
情况比较混乱:
<netinet/icmp.h>
定义了struct icmp
和struct icmphdr
.<linux/icmp.h>
还定义了struct icmphdr
,其结构与<netinet/icmp.h>
. 中的定义类似(但字段名称通常不同)
首先:不要包含 <linux/icmp.h>
除非你有充分的理由。您 不能 包括两个 header -- 它们会发生冲突 -- 大多数软件都需要 netinet 定义。
其二:struct icmphdr
顾名思义就是header。 struct icmp
定义结构化 ICMP 消息的内容,如目标无法到达的消息。