Subject: Re: ntohs() and htons() usgae in the network stack

From: Ben Hutchings <[email-masked]>
Date: Wed, 10 Jun 2009 15:24:12 +0100

On Wed, 2009-06-10 at 17:08 +0300, Kevin Wilson wrote:
> Hello,
> I will appreciate if someone can explain this mystery to me:
> 
> While debugging some kernel module, I added printing of ports.
> I had printer both ntohs() and htons() of a port number. I got the same
> values.
[...]

Yes, these functions have exactly the same effect on the bits of the
value.  But "16-bit unsigned value in host order" (u16) and "16-bit
unsigned value in network order" (__be16) should be considered as two
different types, requiring different functions for conversion each way.
The "sparse" static analyser can treat them as separate types and can
catch missing conversions.  If we used a single conversion function this
would not be possible.

Ben.

-- 
Ben Hutchings, Senior Software Engineer, Solarflare Communications
Not speaking for my employer; that's the marketing department's job.
They asked us to note that Solarflare product names are trademarked.