Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3287780
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:31:54+00:00 2026-05-17T20:31:54+00:00

I am calculating UDP checksum using the following function (found it somewhere): uint16_t udp_checksum(const

  • 0

I am calculating UDP checksum using the following function (found it somewhere):

   uint16_t udp_checksum(const void *buff, size_t len, in_addr_t src_addr, in_addr_t dest_addr)
     {
             const uint16_t *buf=(const uint16_t *)buff;
             uint16_t *ip_src=(uint16_t *)&src_addr,
                      *ip_dst=(uint16_t *)&dest_addr;
             uint32_t sum;
             size_t length=len;


         // Calculate the sum                                      
         sum = 0;
         while (len > 1)
         {
                 sum += *buf++;
                 if (sum & 0x80000000)
                         sum = (sum & 0xFFFF) + (sum >> 16);
                 len -= 2;
         }

         if ( len & 1 )
                 // Add the padding if the packet length is odd         
                 sum += *((uint8_t *)buf);

         // Add the pseudo-header                                       
         sum += *(ip_src++);
         sum += *ip_src;

         sum += *(ip_dst++);
         sum += *ip_dst;

         sum += htons(IPROTO_UDP);
         sum += htons(length);

     // Add the carries                                              
         while (sum >> 16)
                 sum = (sum & 0xFFFF) + (sum >> 16);

         // Return the one's complement of sum                           
         return ( (uint16_t)(~sum)  );
 }



    int form_checksums(char * buff)
    {
      // Get IP and UDP headers
      IP_Header* ipHdr  = (IP_Header*)(buff);
      struct UDP_Header* udpHdr = (struct UDP_Header*) (buff + 4*ipHdr->ihl);

      //---- Form and fill IP checksum now--------------------------------------
      ipHdr->check = 0;
      ipHdr->check = in_cksum((unsigned short *)ipHdr, sizeof(*ipHdr));


      //---- calculate and fill udp checksum now ---
      udpHdr->checksum = 0;

      udpHdr->checksum = udp_checksum(buff + 4*ipHdr->ihl, udpHdr->length, ipHdr->saddr, ipHdr->daddr); 

      return 0;
    }

Wireshark shows that the wrong UDP checksum is calculated. I don’t see any problem in the function. What can be going wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T20:31:55+00:00Added an answer on May 17, 2026 at 8:31 pm

    UDP checksum computation requires an UDP pseudo-header.

    Here are some code samples from my libraries that might help:

    // SmartBuffer is a stream-like buffer class
    uint16_t SmartBuffer::checksum(const void* buf, size_t buflen)
    {
        assert(buf);
    
        uint32_t r = 0;
        size_t len = buflen;
    
        const uint16_t* d = reinterpret_cast<const uint16_t*>(buf);
    
        while (len > 1)
        {
            r += *d++;
            len -= sizeof(uint16_t);
        }
    
        if (len)
        {
            r += *reinterpret_cast<const uint8_t*>(d);
        }
    
        while (r >> 16)
        {
            r = (r & 0xffff) + (r >> 16);
        }
    
        return static_cast<uint16_t>(~r);
    }
    

    The UDPFrame checksum computing:

    uint16_t UDPFrame::computeChecksum(const ipv4_header& ih) const
    {
        udp_pseudo_header uph;
    
        memset(&uph, 0x00, sizeof(uph));
    
        uph.source = ih.source;
        uph.destination = ih.destination;
        uph.mbz = 0x00;
        uph.type = ih.protocol;
        uph.length = getData()->length;
    
        systools::SmartBuffer tmp(sizeof(uph) + d_data.size());
    
        tmp.appendValue(uph);
        tmp.append(d_data); // d_data is the UDP frame payload
    
        return tmp.checksum();
    }
    

    Anyway, keep in mind that usually wireshark warns you that a wrong value for the checksum can be computed due to UDP checksum offload.

    Perhaps your checksum function is indeed wrong but a reliable way of beeing sure is to try to receive your UDP frames.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.