c - IP DSCP = 8 使IP总长度为7位数字

标签 c network-programming ip printf

在下面的代码中:

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/

相关文章:

c# - 没有中央服务器的 P2P 握手

ip - 为什么IP数据包中有填充?

postgresql - 无法使用 Host = localhost 连接到在 docker 中运行的 Postgresql

c - 如何使用 uclibc 进行静态构建

c - parseInt 的后缀导致错误

c - 指针数组分配

php - 使用 PHP 获取 IP 信息

c - 为什么 n++ 当它是 sizeof() 函数的参数时不起作用?

java - 使用 .readlines() 方法时如何克服 "\n"

linux - 拦截来自本地机器的所有请求并为它们创建特定的响应