views:

56

answers:

1

I'm building UDP server application in C#. I've come across a packet checksum problem.

As you probably know, each packet should carry some simple way of telling receiver if packet data is intact.

Now, UDP already has 2-byte checksum as part of header, which is optional, at least in IPv4 world. Alternative method is to have custom checksum as part of data section in each packet, and to verify it on receiver.

My question boils down to: is it better to rely on (optional) checksum in UDP packet header, or to make a custom checksum implementation as part of packet data section?

Perhaps the right answer depends on circumstances (as usual), so one circumstance here is that, even though code is written and developed in .NET on Windows, it might have to run under platform-independent Mono.NET, so eventual solution should be compatible with other platforms. I believe that custom checksum algorithm would be easily portable, but I'm not so sure about the first one.

Any thoughts?

Also, shouts about packet checksumming in general are welcome.

+1  A: 

Do you need to count errors? If the UDP checksum is wrong then any router could just throw the packet away and you'd never get anything.

Of course, if there's an error in the header (such as destination IP address) you also wouldn't get it.

Also, the UDP checksum algorithm is completely standardized, although the function call to turn checksums on or off does vary by platform, although it usually involves setsockopt.

Ben Voigt
Routers don't check the checksum; that's why it makes sense to turn them off in the first place.
Andrew McGregor
@Andrew: That's an overly broad claim, which is the main reason that it's false. Perhaps most routers don't verify the checksum, but many also do. For example, in linux-based routers the usual configuration of netfilter will discard packets with a bad checksum.
Ben Voigt
@Andrew: good point, I didn't think of counting errors before. Answer seems obvious now. As of cross-platform implementation, System.Net.Sockets.SocketOptionName.ChecksumCoverage appears to be supported on all platforms.
mr.b