Computing the Checksum for an ICMP Packet
Posted On April 22, 2020
Recently I needed to create a client program that uses the ICMP protocol to measure round trip time (RTT) of packets going from one device to another (i.e. a ping client). This has proved to be a task that, due to it being my first foray into network programming in C, was a bit out of my wheelhouse. Because of this, I wanted to share one important thing that I learned about the ICMP specification that I did not read about enough: checksum calculation.
Computing the checksum on the packets caused me such a headache, and I could not figure out why it was so difficult for me to do so. It seemed that every other implementation I could find simply passed the packet into a checksum function and then inserted the checksum into the packet. As it turns out, there is one very important thing that must happen before that: you must first explicitly set the checksum to zero, then calculate it.
Looking back this is very clear in the ICMP specification, but it is exactly the kind of thing that you (or at least I) sometimes glance over. I wanted to put this out there in hopes that someone that is new to network programming will find the answer to their problem quickly, rather than spending hours debugging a silly mistake.