简体   繁体   English

从C中的数据包读取未初始化的无符号int数组

[英]Reading uninitialized unsigned int arrays from a packet in C

Im stuck with a problem of reading bytes in my C tcp socket server which receives request from a python client. 我陷入了从我的C tcp套接字服务器读取字节的问题,该服务器从python客户端接收请求。 I have the following struct as my receive template 我将以下结构作为接收模板

struct ofp_connect {
uint16_t wildcards;                /* identifies ports to use below */
uint16_t num_components;           

uint8_t  pad[4];                   /* Align to 64 bits */

uint16_t in_port[0];               
uint16_t out_port[0];           

struct ofp_tdm_port in_tport[0];   
struct ofp_tdm_port out_tport[0];

struct ofp_wave_port in_wport[0];  
struct ofp_wave_port out_wport[0];

};
OFP_ASSERT(sizeof(struct ofp_connect) == 8);

I can read the first two 32 bit fields properly but my problem is the in_port[0] after the pad field that seems to be wrong. 我可以正确读取前两个32位字段,但是我的问题是pad字段后面的in_port [0]似乎是错误的。 The way its currently being read is 当前读取的方式是

uint16_t portwin, portwout, * wportIN;
wportIN =  (uint16_t*)&cflow_mod->connect.in_port; //where cflow_mod is the main struct which encompasses connect struct template described above 
memcpy(&portwin, wportIN, sizeof(portwin) );
DBG("inport:%d:\n", ntohs(portwin));

unfortunately this doesnt give me the expected inport number. 不幸的是,这并没有给我预期的进口数量。 I can check in wireshark that the client is sending the right packet format but I feel the way I read the in/out port is wrong. 我可以检入Wireshark,确认客户端正在发送正确的数据包格式,但是我感觉读入/出端口的方式是错误的。 Or is it because of the way python sends the data? 还是因为python发送数据的方式? Can you provide some advice on where and why im going wrong? 您能否提供一些建议,说明即时消息在哪里以及为什么会出错? Thanks in advance. 提前致谢。

The declaration of struct ofp_connect violates the following clause of the ISO C standard: struct ofp_connect的声明违反了ISO C标准的以下条款:

6.7.2.1 Structure and union specifiers ... 18 As a special case, the last element of a structure with more than one named member may have an incomplete array type; 6.7.2.1结构和联合说明符... 18在特殊情况下,结构中具有多个命名成员的最后一个元素可能具有不完整的数组类型; this is called a flexible array member. 这称为灵活数组成员。

Note that in your case in_port and out_port should have been declared as in_port[] and out_port[] to take advantage of the clause above in which case you would have two flexible array membes, which is prohibited by the above clause. 请注意,在这种情况下, in_portout_port声明为in_port[]out_port[]才能利用上面的子句,在这种情况下,您将拥有两个灵活的数组成员,这是上面的子句所禁止的。 The zero-length array declaration is a convention adopted by many compilers (including gcc, for example) which has the same semantics but in your case, both in_port and out_port share the same space (essentially whatever bytes follow the ofp_connect structure). 零长度数组声明是许多编译器(例如,包括gcc)采用的约定,该约定具有相同的语义,但在您的情况下, in_portout_port 共享相同的空间 (基本上, ofp_connect结构ofp_connect任何字节)。 Moreover, for this to work, you have to allocate some space after the structure for the flexible array members. 此外,为了使此工作正常进行,您必须在结构之后为柔性数组成员分配一些空间。 Since, as you said, struct connect is part of a larger structure, accessing in_port returns the 'value' stored in the containing structure's member following the connect sub- struct 正如您所说,由于struct connect是更大结构的一部分,因此访问in_port返回connectstruct后面存储在包含结构成员中的“值”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM