Reading uninitialized unsigned int arrays from a packet in C -
im stuck problem of reading bytes in c tcp socket server receives request python client. have following struct receive template
struct ofp_connect { uint16_t wildcards; /* identifies ports use below */ uint16_t num_components; uint8_t pad[4]; /* align 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 first 2 32 bit fields problem in_port[0] after pad field seems wrong. way being read is
uint16_t portwin, portwout, * wportin; wportin = (uint16_t*)&cflow_mod->connect.in_port; //where cflow_mod main struct encompasses connect struct template described above memcpy(&portwin, wportin, sizeof(portwin) ); dbg("inport:%d:\n", ntohs(portwin));
unfortunately doesnt give me expected inport number. can check in wireshark client sending right packet format feel way read in/out port wrong. or because of way python sends data? can provide advice on , why im going wrong? in advance.
the declaration of struct ofp_connect violates following clause of iso c standard:
6.7.2.1 structure , union specifiers ... 18 special case, last element of structure more 1 named member may have incomplete array type; called flexible array member.
note in case in_port
, out_port
should have been declared in_port[]
, out_port[]
take advantage of clause above in case have two flexible array membes, prohibited above clause. zero-length array declaration convention adopted many compilers (including gcc, example) has same semantics in case, both in_port
, out_port
share same space (essentially whatever bytes follow ofp_connect
structure). moreover, work, have allocate space after structure flexible array members. since, said, struct connect
part of larger structure, accessing in_port
returns 'value' stored in containing structure's member following connect
sub-struct