在下面的代码中:
struct ip_header {
unsigned char ip_hl:4;
unsigned char ip_ver:4;
unsigned char ip_dscp:6;
unsigned char ip_ecn:2;
unsigned int ip_len;
unsigned int ip_id;
...
};
...
const struct ip_header * ip_hdr = (const struct ip_header *)(buffer + ETHERNET_HEADER_SIZE);
...
printf("IP DSCP: %u\n", ip_hdr->ip_dcsp);
printf("IP Total Length: %u bytes\n", ip_hdr->ip_len);
...
通常我有IP DSCP = 0
IP 总长度是某个数字 < 65535
.
每当我得到IP DSCP = 8
时,Total Length 变为 7 位数字,即 4231532 字节。
但是我们知道 unsigned int 的范围是在 0 到 65535 之间。
有人可以解释一下这里发生了什么以及IP DSCP = 8
是什么吗?意思是?或者我是否遗漏了 IP header 长度和 IP DSCP 之间的某些字段?谢谢
最佳答案
您对数据类型长度的假设,例如 int
是错的。它们是实现定义的。
如果您需要一个具有明确定义位数的变量,您可以使用
uint16_t
int16_t
如果您#include <stdint.h>
,则可以使用这些内容.
关于c - IP DSCP = 8 使IP总长度为7位数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20164009/