home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.ultrix
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!darwin.sura.net!spool.mu.edu!agate!boulder!news
- From: barb@locutus.cs.colorado.edu (Barbara Dyker)
- Subject: bug in htons or sockets?
- Message-ID: <1993Jan6.234432.18330@colorado.edu>
- Sender: news@colorado.edu (The Daily Planet)
- Nntp-Posting-Host: locutus.cs.colorado.edu
- Organization: University of Colorado, Boulder
- Date: Wed, 6 Jan 1993 23:44:32 GMT
- Lines: 76
-
-
- >From the htons man page:
-
- These routines are defined as
- null macros in the include file <netinet/in.h>.
-
- But it doesn't work like a null macro. The test program below
- should report "43526" (or 1706?) for both answers. This is
- under Ultrix 4.1 thru 4.3.
-
- ---------
- [3:05pm]soma:~/tmp-27% cc servbytest.c
- [3:05pm]soma:~/tmp-28% ./a.out
- port number returned by getservbyname: 43526
- port number returned after htons: 1706
- [3:05pm]soma:~/tmp-29% grep whiteppg /etc/services
- whiteppg 1706/tcp da 411 # Directory Assistance
- [3:05pm]soma:~/tmp-30% cat servbytest.c
- #include <stdio.h>
- #include <netdb.h>
- #include <sys/types.h>
- #include <netinet/in.h>
-
- #define SERVICE "whiteppg"
-
- main()
- {
- struct servent *sp; /* describes a service */
- if ((sp = getservbyname(SERVICE,"tcp")) == NULL)
- printf ("no port specified in /etc/services\n");
- else {
- printf ("port number returned by getservbyname: %d\n", sp->s_port);
- printf ("port number returned after htons: %d\n", htons(sp->s_port));
- }
- }
-
-
- This creates a problem for typical code (fragments) like:
-
- struct sockaddr_in sin;
- struct servent *sp;
- ...
-
- if ((sp = getservbyname(SERVICE,"tcp")) == NULL)
- sin.sin_port = htons(DEFAULT_PORT);
- else
- sin.sin_port = htons(sp->s_port); /*** this breaks on DEC ***/
- ...
-
- if (bind(sock,(struct sockaddr *) &sin,sizeof(sin))) {
- perror("bind()");
- exit(1);
- }
-
-
- On all other vendor systems I tested (Sun4.1, BSD4.4,
- KSR OSF1, NeXT, HPUX), my servbytest code reports 1706 for
- both numbers and using that port number as
- sin.sun_port works. Ultrix wants 43526 as sin.sin_port to
- connect to 1706 on another system.
-
- So the question is, where is the bug really? Is it
- htons or the htons man page and all the ultrix socket code?
- Something is wrong. My current fix is to just drop using
- htons... it's null on most machines anyway and dropping it
- works on ultrix too. Is that the right way to go?
-
- Please send any responses via email. I don't read this group
- regularly.
-
- Thanks!
-
- Barbara J. Dyker Department of Computer Science
- Computer Systems Manager Campus Box 430B, ECEE00-69
- barb@cs.colorado.edu University of Colorado
- (303) 492-2545 Boulder, CO 80309-0430
-