0

What is the best way to calculate a UDP datagram checksum? Out of Python's MD5, SHA256 or any other method, which method can surely help in identifying a corrupt packet? Also, my datagram is of the format:

packet = struct.pack('HH', seq_num, checksum) + payload

, where payload is the message I'm sending to the receiver. So, should I calculate the checksum in this case for the packet ?

Jarvis
  • 8,494
  • 3
  • 27
  • 58
  • `UDP` for high speed and non-block communication, message checksum and packet checksum very different points. My opinion : disable packet checksum and add a sub header for additional checksum information data. Maybe i am wrong but i thing "no checksum required on a `UDP` communication(on packet header)" cos you waste a lot time on calculate every `UDP` header checksum. – dsgdfg Mar 14 '17 at 07:55

1 Answers1

0

None of them. The checksum is just a simple mathematical sum of the bytes, not a hash.

Just read wikipedia's page for UDP:

Checksum is the 16-bit one's complement of the one's complement sum of a pseudo header of information from the IP header, the UDP header, and the data, padded with zero octets at the end (if necessary) to make a multiple of two octets.

Hint cf that answer for an implementation

Community
  • 1
  • 1
zmo
  • 24,463
  • 4
  • 54
  • 90
  • I guess you are talking about two different things. The OP wants to implement his own application layer checksum, while you are talking about the UDP checksum. – Klaus D. Mar 14 '17 at 05:27
  • It's not clear to me this is the case. He asks about "which method can surely help in identifying a corrupt packet?" Which is exactly the role of the UDP header. He talks about datagram, which is UDP for packet… – zmo Mar 14 '17 at 05:30
  • I guess he did not know of that UDP feature. It would explain why he asked. – Klaus D. Mar 14 '17 at 05:35
  • Can I know which UDP feature are you talking about ? @KlausD. As you can see, I am designing my own UDP datagram. – Jarvis Mar 14 '17 at 08:01
  • @Jarvis ok so then to be clear: md5, sha and all hashing operations are relatively complex, slow and expensive operations that's unrealistic to apply to *every* datagram sent between computers. A checksum's purpose is to detect corruption, and the sum of all bytes method is a very simple method. – zmo Mar 14 '17 at 08:07
  • That being said, I believe the network stack is supposed to care of dropping all corrupt datagrams by default, making it up to the application layer to detect missing bits (if those matter) and request them again. – zmo Mar 14 '17 at 08:10