[comp.protocols.nfs] Problem with PC-NFS programmers Toolkit

kjetilo@gollum.uio.no (Kjetil Otter Olsen) (11/20/90)

I am using PC-NFS 3.0.1 and PC-NFS Programmers Toolkit 1.00 and the
following problem has come up:

     When I try to open a TCP socket to a port by using the number
     of a service, (and not getservbyname()) it fails.

     After _some_ debugging I found that the getservbyname() function
     returned port number 5888 for telnet, that is 23 _* 256_. ( Telnet
     is usually 23, according to Assigned Numbers ) The same weird
     thing happens for other services (SMTP, NNTP ....)

     For telnet, SMTP or NNTP this is no problem, I just use getservbyname()
     or multiply, but in some cases that is not enough... When I want
     to access a port > 256, I end up with  >256 * 256 (= >64k ).. And
     since the port is declared as an "unsigned short" in the sockaddr_in-
     struct I have a problem...

     OK, a bug in the Toolkit I assumed, but then i tried the CUTCP
     telnet that uses the Toolkit, and it has no problems with port-
     numbers > 256. Confusing to say the least.. Since no source
     is available for the CUTCP software at the moment I can not find
     out what they have done to overcome the problems :-(
     (My CUTCP/CUTE is:  CUTE;2.2TN/NFS-A   dated 13th feb 1990.)

The question:

     Does anyone have any idea how i can get PC-NFS Programmers Toolkit to
     open a socket to a port > 256 ??

     Maybe someone in the CUTCP/CUTE team can share their knowledge with me?

Please answer by E-mail, or News, or fax, or phone or drop in :-)

                         Thanks in advance.

--Kjetil
-------------------
Kjetil Otter Olsen		|*|	kjetilo@gollum.uio.no
PC / UNIX / network consultant	|*|	kjetilo@usit.uio.no
Central Computing Senter	|*|	otter@ifi.uio.no
University of Oslo, Norway	|*|	Kjetil.Olsen@use.uio.no
Phone: +47-2-45 34 88			FAX: +47-2-45 57 70
-------------------

geertj@philica.ica.philips.nl (Geert Jan de Groot) (11/21/90)

In article <901019.190237.kjetilo@otter> kjetilo@gollum.uio.no (Kjetil Otter Olsen) writes:
>I am using PC-NFS 3.0.1 and PC-NFS Programmers Toolkit 1.00 and the
>following problem has come up:
>
>     When I try to open a TCP socket to a port by using the number
>     of a service, (and not getservbyname()) it fails.
>
>     After _some_ debugging I found that the getservbyname() function
>     returned port number 5888 for telnet, that is 23 _* 256_. ( Telnet
>     is usually 23, according to Assigned Numbers ) The same weird
>     thing happens for other services (SMTP, NNTP ....)

It's not weird, this is supposed to happen. Internet networking uses
the right byte order, while (broken) intel processors do it the wrong way.
There are library routines to convert between the two; outsmarting them
will burn you, as you just have learned.

There should be some macro's in an include file which swap bytes if
the processor is of the wrong type. 
Check page 276 e.a of Comer's networking book, about the ntohs, htons,
ntohl, and htonl macros. Or check page 192 of the toolkit manual.

>     For telnet, SMTP or NNTP this is no problem, I just use getservbyname()
>     or multiply, but in some cases that is not enough... When I want
>     to access a port > 256, I end up with  >256 * 256 (= >64k ).. And
>     since the port is declared as an "unsigned short" in the sockaddr_in-
>     struct I have a problem...

Nope, it's swapping bytes. Port 0x1234 must be 0x3412. Got it?
DON'T JUST SWAP BYTES. If your program gets moved to a non-broken CPU,
you'll be confused again. The macro's mentioned above take care of this.

>     OK, a bug in the Toolkit I assumed, but then i tried the CUTCP
>     telnet that uses the Toolkit, and it has no problems with port-
>     numbers > 256. 

There are many bugs in the toolkit, but not this one..

Geert Jan


--8<--nip-nip---------------------------------------------------------------

Geert Jan de Groot,		Email: geertj@ica.philips.nl
Philips ICA,			       ..!hp4nl!philica!geertj
Weisshausstrasse,		Ham: PE1HZG 
5100 Aachen, West-Germany

phone: +49 241 6003 714		 "Programs are like waffles:
fax:   +49 241 6003 709		 you should always throw the first one out"
[Standard disclaimers apply]	 - Sutherland

gday@digigw.digital.co.jp (Gordon Day) (11/30/90)

[ problem with port numbers when using PC-NFS Toolkit deleted ]

1. RTFM

2. This is a socket problem, and shouldn't be posted to comp.protocols.nfs

3. Here's what's wrong anyway (see below)

Try using ntohs().  The port number you are getting back from getservbyname()
is in the _correct_ (i.e. network) byte order already.  You don't have to do
anything to it when assigning the port number to the server "name" before 
your connect() or sendto() call.  You may have been programming before on
a system where the host byte order was the same as the network byte order,
so the port number was "readable".  This is not generally the case, and you 
should be converting to/from network byte order when appropriate (e.g. 
printing it out :-).

Example (not tested, of course :-))

      struct hostent *hp;
      struct servent *sp;
      struct sockaddr_in server_addr;
      unsigned short port;

   .
   .  <stuff>
   .

      if ((hp = gethostbyname("myserver")) == NULL){
              printf("``myserver'' is an unkown host\n");
              exit(1);
      }
      memcpy((caddr_t)&server_addr.sin_addr, hp->h_addr, hp->h_length);
      server_addr.sin_family = AF_INET;

#ifdef GUESSING_A_PORT_NUMBER
      port = htons((unsigned short)23);  /* notice the htons() */
#else
		setservent(1);
      if ((sp = getservbyname("telnet","tcp")) == NULL){
              printf("unable to get telent tcp service port\n");
              exit(1);
      }
      port = sp->s_port;   /* no htons() needed */
#endif
      server_addr.sin_port = sp->s_port;
      printf("telnet port (network byte order) = %u", (unsigned int)port); 
      printf("telnet port (host byte order) = %u", (unsigned int)ntohs(port)); 

   .
   .   <more stuff like: socket(), bind(), connect(), sendto(), etc>
   .


Hope this helps,

Gordon W. Day