netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* ntohs() and htons() usgae in the network stack
@ 2009-06-10 14:08 Kevin Wilson
  2009-06-10 14:10 ` Florian Weimer
  2009-06-10 14:24 ` Ben Hutchings
  0 siblings, 2 replies; 3+ messages in thread
From: Kevin Wilson @ 2009-06-10 14:08 UTC (permalink / raw)
  To: netdev

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. Since I used low port number, I decided to find out from which
port there begins to be a difference. So I tried to write in a simple
kernel module this loop, which tests all ports up till 65536 (since
port number is 16 bit wide):
	...
	int i;
	for (i=0;i<65536;i++)
	 if (ntohs(i) != htons(i))
		printk("found inequality: ntohs(i) != htons(i)\n");

And when running it it did **not** find such a port where ntohs(i) !=
htons ( "found inequality:.. was **not** printed).

So I wondered : when is ntohs(i) != htons(i) ?
Are they always equal (I doubt it); or am I missing something silly here?

I am testing it on x86_64 Intel machine (little endian).
Rgs,
Kevin

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: ntohs() and htons() usgae in the network stack
  2009-06-10 14:08 ntohs() and htons() usgae in the network stack Kevin Wilson
@ 2009-06-10 14:10 ` Florian Weimer
  2009-06-10 14:24 ` Ben Hutchings
  1 sibling, 0 replies; 3+ messages in thread
From: Florian Weimer @ 2009-06-10 14:10 UTC (permalink / raw)
  To: Kevin Wilson; +Cc: netdev

* Kevin Wilson:

> So I wondered : when is ntohs(i) != htons(i) ?

Never, for ordinary endieanness.  The operation is an involution, that
is, its own inverse (like mirroring).

-- 
Florian Weimer                <fweimer@bfk.de>
BFK edv-consulting GmbH       http://www.bfk.de/
Kriegsstraße 100              tel: +49-721-96201-1
D-76133 Karlsruhe             fax: +49-721-96201-99

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: ntohs() and htons() usgae in the network stack
  2009-06-10 14:08 ntohs() and htons() usgae in the network stack Kevin Wilson
  2009-06-10 14:10 ` Florian Weimer
@ 2009-06-10 14:24 ` Ben Hutchings
  1 sibling, 0 replies; 3+ messages in thread
From: Ben Hutchings @ 2009-06-10 14:24 UTC (permalink / raw)
  To: Kevin Wilson; +Cc: netdev

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.


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2009-06-10 14:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-06-10 14:08 ntohs() and htons() usgae in the network stack Kevin Wilson
2009-06-10 14:10 ` Florian Weimer
2009-06-10 14:24 ` Ben Hutchings

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).