Tuesday, October 6, 2020

Ensuring packet bounds alignment on connection reader

When designing an instant messaging application, I went with what seemed the most obvious approach to consuming variable-sized packets; I put 4 bytes at the start of the packet, indicating the length.

It occurs to me, however, that a client might send a malformed or ‘lying’ packet that has a too-large length. In that case, it’s possible packets could be constructed from misaligned bytes, resulting in a bunch of trash.

My possible solution to this is to have a sort of constant 32bit ‘identifier’ (it would always be the same) after the length so I can pretty much ensure packet alignment.

Is this a good approach? Also, if there’s any useful tips/articles anyone has for protocol design, that would be much appreciated.



No comments:

Post a Comment