TIP: Click on subject to list as thread! ANSI
echo: os2prog
to: Keith Bennett
from: Jon Guthrie
date: 1994-11-27 09:13:08
subject: TCP/IP Sockets getservbyname() weirdness

21 Nov 94, Keith Bennett writes to All:

 > This question pertains to port numbers for TCP/IP sockets.  There is a
 > function named getservbyname() which takes a service name and looks it
 > up in /TCPIP/ETC/SERVICES, returning a pointer to struct servent
 > (SERVice ENTry).  My question is about the port number member (named
 > s_port) of that structure.  The number I found there was not the same
 > number as the one in the "SERVICES" file.  For small
numbers, the number
 > returned was the number in the file multiplied by 256.

Piece of cake.  The standard definition of servent says that s_port is
stored in network byte format.  That is, the bytes are swapped if you have
the "poor
taste" to run on an Intel based machine.  I read on...

 > On a coworker's
 > suggestion, I tried htonl() on the number to see if getservbyname() was
 > doing a conversion to network format.  That didn't work.  So I tried
 > htons() (which converts a short, instead of a long, to network format).
 > *That worked!*

 > That really surprised me, since the s_port member is defined as an int,
 > which is 32 bits in OS/2, and not a short.  I looked in "Unix Network
 > Programming" by W. Richard Stevens, and he explains that a port is a
 > 16-bit number.

It is.  A TCP/IP address is formed of a 32-bit IP address and a 16-bit port
number.  The IP address goes into the IP header (which determines how the
packet is routed) and the port number goes into the TCP (or UDP) header
which determines which process on the destination system gets the packet. 
The port number field in the TCP header (and the UDP header, too) is only
16 bits long.

 > I can only guess that the member was kept an int instead of the 16-bit
 > short for source code compatibility of the header file, or for
 > performance reasons.  If it was to be portable, would an htons() on a
 > 4-byte int work on every platform?  Isn't it possible that on some it
 > would look at the wrong 2 bytes?

I spent some time digging around in the Linux source for the answer to this.
I've never had the occasion to use nor have I seen anyone else use
gethostbyname() et al.

It turns out that the format for servent is "standard."  The fact
that int's are different sizes on different machines is why htons() and
htonl() refer to shorts and longs and NOT ints.  (Shorts are assumed to be
16-bits and longs are assumed to be 32-bits.) You are supposed to know that
the s_port is a short and, therefore, that you should use htons() to
convert it instead of htonl().

(Unix and suchlike are chock full of things that you are "supposed to
know", like the proper function to use with the s_port field of
servent.)

- Jon

--- GoldED/2 2.42.G0214


* Origin: The Wandering Programmer Comes Home (1:106/2000.25)
SEEN-BY: 12/2442 54/54 620/243 624/50 632/348 640/820 690/660 711/409 410 413
SEEN-BY: 711/430 807 808 809 934 942 949 712/353 623 713/888 800/1
@PATH: 30883/25 106/2000 449 116 170/400 280/1 396/1 3615/50
@PATH: 229/2 12/2442 711/409 54/54 711/808 809 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.