Thread: UDP checksum code

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    26

    UDP checksum code

    Hi,

    Do any of you have UDP checksum C code? It would be even better to see how it works in some examples.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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.

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    26
    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.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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:
    Code:
    typedef enum { false, true } bool;
    Adjust case as necessary. Cheers.
    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.

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    26
    Quote Originally Posted by dwks View Post
    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:
    Code:
    typedef enum { false, true } bool;
    Adjust case as necessary. Cheers.
    Thank you dwks. I have used unsigned char instead BOOL but will try your suggestions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. crc32/checksum app
    By andwan0 in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2010, 07:45 PM
  2. CheckSum help
    By remoir in forum C Programming
    Replies: 13
    Last Post: 02-01-2009, 10:19 PM
  3. CRC / checksum
    By Zoltarc in forum C++ Programming
    Replies: 0
    Last Post: 11-30-2002, 11:05 PM
  4. How to write CRC/checksum code?
    By The V. in forum C++ Programming
    Replies: 5
    Last Post: 01-24-2002, 06:44 PM