struct ip和struct iphdr之间的区别 [英] Difference between struct ip and struct iphdr
问题描述
我正在尝试了解网络是如何工作的,我正在做一些测试,发送一些软件包......无论如何
I am trying to understand how the network is working, i'm doing some test, sending some package... anyway
我的意思是我可以'找到协议结构
和协议标题结构
之间的真正区别。
My point is that i can't find the real difference between "protocol" structure
and "protocol header" structure
.
对于ip结构,它们都大小为20个字节。
但是例如:
For the ip structure, they both sized 20 bytes. but for exemple:
-
struct ip
和struct iphdr
size 20 bytes -
struct icmp
size 28 bytes -
struct icmphdr
size 8 bytes
struct ip
andstruct iphdr
sized 20 bytesstruct icmp
sized 28 bytesstruct icmphdr
sized 8 bytes
我猜这是 struct icmp
包含 struct ip / iphdr?
?
我所看到的每个协议都有相同的结构。
struct udp
/ struct udphdr
,
And there is the same kind of structure with every protocol i have seen.
struct udp
/ struct udphdr
,
是否链接到 IP_HDRINCL
设置为 setsockopt()
?
所以我的问题是它们之间真正的区别是什么?当使用好的时候。
So my question is What is the real difference between them ? And When use the good one.
ip和iphdr struct:
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
ICMP structure code here : https://www.cymru.com/Documents/ip_icmp.h
推荐答案
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
在< netinet / ip.h> <中定义/ code>,这是UNIX系统上合理标准的标头。
struct ip
is defined in <netinet/ip.h>
, which is a reasonably standard header on UNIX systems.
struct iphdr
在< LINUX / ip.h>
。此标头(和结构)是特定于Linux的,并且不会出现在其他操作系统中。
struct iphdr
is defined in <linux/ip.h>
. This header (and structure) are Linux-specific, and will not be present in other operating systems.
如果您不确定要使用哪个,请使用 struct ip
;使用此结构的代码更有可能移植到非Linux系统。
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
和 struct icmphdr
是一个更糟糕的情况:
struct icmp
and struct icmphdr
are a messier situation:
-
< netinet / icmp.h>
定义struct icmp
和struct icmphdr
。 -
< linux / icmp.h>
也定义struct icmphdr
,具有类似的结构(但通常是不同的字段名称),作为< netinet / icmp.h> $ c $的定义c>。
<netinet/icmp.h>
defines bothstruct icmp
andstruct icmphdr
.<linux/icmp.h>
also definesstruct icmphdr
, with a similar structure (but, as usual, different field names) as the definition from<netinet/icmp.h>
.
首先:不要包含< linux / icmp.h>
除非你有充分的理由。你不能包含两个标题 - 它们会发生冲突 - 大多数软件都会期望netinet定义。
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.
第二: struct icmphdr
顾名思义就是标题。 struct icmp
定义结构化ICMP消息的内容,如目标无法到达的消息。
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.
这篇关于struct ip和struct iphdr之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!