Re: IP cheksum and libnet_pblock_coalesce

Wu Yongwei <[email protected]> Tue, 28 Sep 2004 11:28:27 +0800
Newsgroups gmane.comp.security.libnet
Organization Kingnet Security, Inc.
Message-ID <[email protected]>
> Hi,
> 
> That is a known (but tricky) bug reported some times ago. David
> Baroso proposed something like :
> 
> sum += ((*(u_int8_t *)addr & 0xFF)<<8);
> 
> The difficulty is to find something working on both big and little 
> endian systems.

The expression above seems to work only on big-endian machines and 
proves wrong in my test on x86 (contrary to Mr David Barroso's 
statement). The third edition of UNIX Network Programming has something 
like this (adapted):

u_int16_t last_byte = 0;
...
if (nleft == 1) {
     *(u_int8_t*)&last_byte = *(u_int8_t*)addr;
     sum += last_byte;
}

I feel this is good.

Best regards,

Yongwei