Here's some code obtained from elsewhere: static int inet_cksum(u_short *addr, int len) { register int nleft = len; register u_short *w = addr; register u_short answer; register u_int sum = 0; u_short odd_byte = 0;/* * Our algorithm is simple, using a 32 bit accumulator (sum), * we add sequential 16 bit words to it, and at the end, fold * back all the carry bits from the top 16 bits into the lower * 16 bits. */ while( nleft > 1 ) { sum += *w++; nleft -= 2; } /* mop up an odd byte, if necessary */ if( nleft == 1 ) { *(u_char *)(&odd_byte) = *(u_char *)w; sum += odd_byte; } /* * add back carry outs from top 16 bits to low 16 bits */ sum = (sum >> 16) + (sum & 0x0000ffff); /* add hi 16 to low 16 */ sum += (sum >> 16); /* add carry */ answer = ~sum; /* truncate to 16 bits */ return (answer); }
Thanks for the reply, I have one more question about this checksum. In ICMP ECHO header - there are those fields: type - 8 bits code - 8 bits checksum - 16 bits id 16 bits and seq number 16 bits in exactly this order. Which fields are being computed in checksum ? Should I use /inet_chksum/ like that: /inet_chksum(iecho,1)/ ? - where iecho is pointer to icmp_echo_hdr . Then what's the point of 16 bit chksum field for 16 bit of data ? Marcin _______________________________________________ lwip-users mailing list [email protected] http://lists.nongnu.org/mailman/listinfo/lwip-users
