I spent some time trying to calculate the UDP checksum, but every time I observe the packets in Wireshark, it says that the checksum is incorrect. Here's the code:
uint16_t compute_udp_checksum(IP *ip, UDP *u,
void *data, int data_len)
{
uint32_t sum = 0;
uint16_t *arr = NULL;
void *buffer = NULL;
int size = 0;
PSEUDO_HDR *ps = NULL;
size = PS_SIZE + UDP_SIZE + data_len;
if (size % 2)
size += 1;
buffer = malloc(size);
if (!buffer)
{
perror("malloc");
return 0;
}
ps = create_pseudo_hdr(ip, u);
if (!ps)
{
free(buffer);
perror("malloc");
return 0;
}
memset(buffer, 0, size);
memcpy(buffer, ps, PS_SIZE);
memcpy(buffer + PS_SIZE, u, UDP_SIZE);
memcpy(buffer + PS_SIZE + UDP_SIZE, data, data_len);
arr = (uint16_t *) buffer;
int i = size;
while (i > 1)
{
sum += *arr++;
i -= 2;
}
sum = (sum & 0xFFFF) + (sum >> 16);
return ~sum;
}
As I said, when I sent the packet into the network, Wireshark reports (on the receiving end) that the checksum is incorrect. Any help would be appreciated.
Thank you.