I have a working version of a homemade DNS server/relay written in C and compiled in linux for my router. The process involves encoding/decoding/ammending DNS messages which are accessed via a pointer of type:
void *data
My original version moved this pointer along to access the different data points, however as the code became more complicated, I decided to instead access the data directly without ever moving the pointer to avoid any errors should I forget to return the pointer to its original position. The pointer is thus left to always point to the beginning of the data.
My current version accesses data as follows:
ntohs( *(uint16_t*)&(((char*)data)[pos]) )
to decode the 16bit unsigned integer starting at position pos
as an example.
Can this seemingly simple task be done more efficiently/succinctly in C?