home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / unix / ultrix / 9195 < prev    next >
Encoding:
Text File  |  1993-01-06  |  2.8 KB  |  88 lines

  1. Newsgroups: comp.unix.ultrix
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!darwin.sura.net!spool.mu.edu!agate!boulder!news
  3. From: barb@locutus.cs.colorado.edu (Barbara Dyker)
  4. Subject: bug in htons or sockets?
  5. Message-ID: <1993Jan6.234432.18330@colorado.edu>
  6. Sender: news@colorado.edu (The Daily Planet)
  7. Nntp-Posting-Host: locutus.cs.colorado.edu
  8. Organization: University of Colorado, Boulder
  9. Date: Wed, 6 Jan 1993 23:44:32 GMT
  10. Lines: 76
  11.  
  12.  
  13. >From the htons man page:
  14.  
  15.       These routines are defined as
  16.           null macros in the include file <netinet/in.h>.
  17.  
  18. But it doesn't work like a null macro.  The test program below
  19. should report "43526" (or 1706?) for both answers.  This is
  20. under Ultrix 4.1 thru 4.3.
  21.  
  22. ---------
  23. [3:05pm]soma:~/tmp-27% cc servbytest.c
  24. [3:05pm]soma:~/tmp-28% ./a.out
  25. port number returned by getservbyname: 43526
  26. port number returned after htons: 1706
  27. [3:05pm]soma:~/tmp-29% grep whiteppg /etc/services
  28. whiteppg        1706/tcp        da 411          # Directory Assistance
  29. [3:05pm]soma:~/tmp-30% cat servbytest.c
  30. #include <stdio.h>
  31. #include <netdb.h>
  32. #include <sys/types.h>
  33. #include <netinet/in.h>
  34.  
  35. #define SERVICE         "whiteppg"
  36.  
  37. main()
  38. {
  39.     struct servent *sp;     /* describes a service */
  40.     if ((sp = getservbyname(SERVICE,"tcp")) == NULL)
  41.         printf ("no port specified in /etc/services\n");
  42.     else {
  43.         printf ("port number returned by getservbyname: %d\n", sp->s_port);
  44.         printf ("port number returned after htons: %d\n", htons(sp->s_port));
  45.     }
  46. }
  47.  
  48.  
  49. This creates a problem for typical code (fragments) like:
  50.  
  51.         struct sockaddr_in sin;
  52.         struct servent *sp;
  53.     ...
  54.  
  55.         if ((sp = getservbyname(SERVICE,"tcp")) == NULL)
  56.                 sin.sin_port = htons(DEFAULT_PORT);
  57.         else
  58.                 sin.sin_port = htons(sp->s_port);  /*** this breaks on DEC ***/
  59.     ...
  60.  
  61.         if (bind(sock,(struct sockaddr *) &sin,sizeof(sin))) {
  62.                 perror("bind()");
  63.                 exit(1);
  64.         }
  65.  
  66.  
  67. On all other vendor systems I tested (Sun4.1, BSD4.4,
  68. KSR OSF1, NeXT, HPUX), my servbytest code reports 1706 for
  69. both numbers and using that port number as
  70. sin.sun_port works.  Ultrix wants 43526 as sin.sin_port to
  71. connect to 1706 on another system.
  72.  
  73. So the question is, where is the bug really?  Is it
  74. htons or the htons man page and all the ultrix socket code?
  75. Something is wrong.  My current fix is to just drop using
  76. htons... it's null on most machines anyway and dropping it
  77. works on ultrix too.  Is that the right way to go?
  78.  
  79. Please send any responses via email.  I don't read this group
  80. regularly.
  81.  
  82. Thanks!
  83.  
  84. Barbara J. Dyker                       Department of Computer Science
  85. Computer Systems Manager               Campus Box 430B, ECEE00-69
  86. barb@cs.colorado.edu                   University of Colorado
  87. (303) 492-2545                         Boulder, CO  80309-0430
  88.