Hi,
Do any of you have UDP checksum C code? It would be even better to see how it works in some examples.
Hi,
Do any of you have UDP checksum C code? It would be even better to see how it works in some examples.
I don't, but google does... Just look for "UDP checksum code". Example: UDP Checksum Code
It's not super-complicated. Find some kind of checksum algorithm, compute the checksum for every packet you're about to send and put the checksum somewhere in the packet you'll be able to find it... maybe add an extra few bytes as a "header". Then when receiving compute the checksum again and make sure it matches.
Just curious why you want to checksum UDP packets? Normally you use UDP when you don't care much about reliability... of course perhaps you care about integrity but not order, in which case TCP would slow your program down too much but straight UDP could be exploited or something. But if you have requirements like that you probably wouldn't be asking this question. So what are you trying to do?
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Thank you, dwks. I have found this link also but because of BOOL I thought it is more C++ code but I have made some modification to be C. Just wanted to ask if someone has better options.
I dont know if I really need this UDP checksum. I use iptables and libipq and take packets from queue, modify payload and put them back to queue.
Well, checksum code probably won't hurt.... P.S. That's definitely C code, although it would work as C++ as well. Having BOOL in fact is a good indication of something being C, since C++ has "bool" built-in and it would make more sense to use that. Also, that code uses C-style casts, which works in C++ but is unlikely to be a C++ programmer's first thought. You'll find that a lot of networking code is in C, not C++.
Finally, easy ways to get "BOOL" in C are to use #defines, or <stdbool.h>, or the enum trick:
Adjust case as necessary. Cheers.Code:typedef enum { false, true } bool;
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.