Subject: Re: lib/1610:
To: None <andrew@wipux2.wifo.uni-mannheim.de>
From: der Mouse <mouse@Collatz.McRCIM.McGill.EDU>
List: netbsd-bugs
Date: 10/17/1995 22:23:19
> I was writing a program to scan ports when it started
> outputing something different than I expected:
> i.e. something goes wrong when you go above 1023.
> void main(void)
> {
> printf("This number %d is different from this number %d\n",
> ntohs(htons(1020) + htons(4)),1024);
> printf("This number %d is the same as this number %d\n",
> ntohs(htons(1020) + htons(3)),1023);
> printf("This number %d is the same as this number %d\n",
> ntohs(htons(1030) + htons(3)),1033);
> }
This is not a bug, assuming you're on a little-endian architecture,
which is presumably the case since the System: line says you're using
an i386. (To see ntohs(htons(x)+htons(y))==x+y on a little-endian
machine, you must have (x&255)+(y&255)<=255 and (x>>8)+(y>>8)<=255, of
course for x and y each in 0..65535. Note this is true of your "the
same as" examples and false of your "different from" example.)
You should consider network-byte-order numbers as opaque objects that
are good for nothing but (a) using in networking calls or (b) swapping
back to host order. As you have found, on a little-endian machine,
this borders on essential; one could (and indeed I would) argue that
you should do for style and portability reasons even if you don't
expect the code to be used on little-endian machines.
der Mouse
mouse@collatz.mcrcim.mcgill.edu