2009/9/4 Ismael Barros² <razielm...@gmail.com>: > I've been looking into iphlpapi/ip.h (just learned bit fields > exist...); would this implementation be fine? > > #include "pshpack1.h" > > typedef struct tagDPSP_MSG_HEADER > { > #ifdef WORDS_BIGENDIAN > DWORD size:20; > DWORD token:12; > #else > DWORD token:12; > DWORD size:20; > #endif > SOCKADDR_IN SockAddr; > } DPSP_MSG_HEADER, *LPDPSP_MSG_HEADER;
Probably not. I'm not sure iphlpapi/ip.h does things right in the first place, it seems to confuse bitfield ordering with machine byte ordering. As a general rule, I think it's best to avoid bitfields for things like writing data to a file or sending data over the network, it's just a pain. Aside from that, using the bitfields in this way messes with the bit ordering, but doesn't change the byte order. For the bitfields in iphlpapi/ip.h that's not an issue because they fit in a single byte. Just store the data in a DWORD with the appropriate masks and shifts, and byte swap that DWORD when reading/writing it.