Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- uint16_t Icmp::create_checksum(uint16_t type_code) {
- unsigned long checksum = 0;
- // Cast the data pointer to one that can be indexed.
- unsigned char* data = raw_data.data();
- size_t length = raw_data.size();
- // Initialise the accumulator.
- uint32_t acc=0xffff;
- acc += type_code;
- // Handle complete 16-bit blocks.
- for (size_t i=0;i+1<length;i+=2) {
- uint16_t word;
- memcpy(&word,data+i,2);
- acc+=(word);
- if (acc>0xffff) {
- acc-=0xffff;
- }
- }
- // Handle any partial block at the end of the data.
- if (length&1) {
- uint16_t word=0;
- memcpy(&word,data+length-1,1);
- acc+=(word);
- if (acc>0xffff) {
- acc-=0xffff;
- }
- }
- // Return the checksum in network byte order.
- return (~acc);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement