Skip to content

Commit

Permalink
Fix checksum calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
woelke committed Aug 24, 2017
1 parent b7bd2d0 commit 93b5ace
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions mcproxy/src/utils/mroute_socket.cpp
Expand Up @@ -157,17 +157,23 @@ u_int16_t mroute_socket::calc_checksum(const unsigned char* buf, int buf_size) c

u_int16_t* b = (u_int16_t*)buf;
int sum = 0;
int csum;

for (int i = 0; i < buf_size / 2; i++) {
ADD_SIGNED_NUM_U16(sum, b[i]);
//sum +=b[i];
sum +=b[i];
}

if (buf_size % 2 == 1) {
//sum += buf[buf_size-1];
ADD_SIGNED_NUM_U16(sum, buf[buf_size - 1]);
sum += buf[buf_size-1];
}

// fold checksum
csum = sum & 0xFFFF;
sum = sum >> 16;
sum += csum;
// fold again in case of overflow.
sum += sum >> 16;

return ~sum;
}

Expand Down

0 comments on commit 93b5ace

Please sign in to comment.