我正在尝试为 CAPWAP 协议创建客户端 C 代码。我尝试使用位字段结构实现 CAPWAP 标头。但是在使用 sendto() 通过套接字发送这个结构之后,当我使用wireshark 嗅探数据包时,我发现在它们之间添加了一些额外的位。我不知道这是从哪里来的。请求帮助。提前致谢。
我尝试评论结构的最后几个成员以使其 4 字节对齐。问题仍然存在。
这是原始标题
struct cw_header
{
unsigned preamble : 8;
unsigned hlen : 5;
unsigned rid : 5;
unsigned wbid : 5;
unsigned t : 1;
unsigned f : 1;
unsigned l : 1;
unsigned w : 1;
unsigned m : 1;
unsigned k : 1;
unsigned flags : 3;
unsigned fragment_id : 16;
unsigned fragment_offset : 13;
unsigned reserved : 3;
uint32_t mac_length : 8;
uint32_t mac_addr[6];
uint32_t padding : 8;
};
我试着评论这些
//uint32_t mac_length : 8;
//uint32_t mac_addr[6];
//uint32_t padding : 8;
这是填充结构的地方
struct cw_header create_cw_header()
{
struct cw_header cw_header;
cw_header.preamble = 0;
cw_header.hlen = 1;
cw_header.rid = 1;
cw_header.wbid = 1;
cw_header.t = 0;
cw_header.f = 0;
cw_header.l = 1;
cw_header.w = 0;
cw_header.m = 1;
cw_header.k = 0;
cw_header.flags = 0;
cw_header.fragment_id = 0; ;
cw_header.fragment_offset = 0;
cw_header.reserved = 0;
cw_header.mac_length = 6;
get_mac_address(cw_header.mac_addr);
cw_header.padding = 0;
return cw_header;
};
这是前 32 位的wireshark 的输出
0000 0000 0010 0001 0000 0100 0000 1010
预期结果:位应按结构中提到的顺序错误:在结构成员之间添加额外位