home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / sys / hp / 15204 < prev    next >
Encoding:
Text File  |  1993-01-22  |  6.3 KB  |  217 lines

  1. Path: sparky!uunet!cs.utexas.edu!sdd.hp.com!saimiri.primate.wisc.edu!srvr1.engin.umich.edu!destroyer!cs.ubc.ca!uw-beaver!news.tek.com!psgrain!ee.und.ac.za!tplinfm
  2. From: barrett@daisy.ee.und.ac.za (Alan P Barrett)
  3. Newsgroups: comp.sys.hp
  4. Subject: Re: rdate for HPUX...
  5. Date: 22 Jan 1993 17:45:36 +0200
  6. Organization: Dept. Elec. Eng., Univ. Natal, Durban, S. Africa
  7. Lines: 205
  8. Message-ID: <1jp4r0INNk1i@daisy.ee.und.ac.za>
  9. References: <8579@tivoli.UUCP>
  10. NNTP-Posting-Host: daisy.ee.und.ac.za
  11.  
  12. In article <8579@tivoli.UUCP>,
  13. stuart@TIVOLI.COM (Stuart Jarriel) writes:
  14. > I have been looking at the HP-specific ftp sites for rdate and havent
  15. > come across it.  I already have xntp, timed, etc..., but would like rdate 
  16. > as a simpler client for some systems.
  17.  
  18. I don't have an rdate client, but I append source for an rdate server,
  19. which I call rfc868time.
  20.  
  21. Install in /etc/inetd.conf as follows:
  22.  
  23. time    stream tcp  nowait  nobody  /usr/local/lib/rfc868time rfc868time
  24. time    dgram  udp  wait    nobody  /usr/local/lib/rfc868time rfc868time
  25.  
  26. --apb
  27. Alan Barrett, Dept. of Electronic Eng., Univ. of Natal, Durban, South Africa
  28. RFC822: barrett@ee.und.ac.za
  29.  
  30. # This is a shell archive.  Remove anything before this line,
  31. # then unpack it by saving it in a file and typing "sh file".
  32. #
  33. # Wrapped by Alan P Barrett <barrett@undeed> on Fri Jan 22 17:44:03 1993
  34. #
  35. # This archive contains:
  36. #    rfc868time.c    
  37. #
  38.  
  39. LANG=""; export LANG
  40. PATH=/bin:/usr/bin:$PATH; export PATH
  41.  
  42. echo x - rfc868time.c
  43. cat >rfc868time.c <<'@EOF'
  44. /* rfc868time.c
  45.  * A P Barrett, 1991.  Public domain.
  46.  *
  47.  * Outputs the number of seconds since 1 Jan 1900 00:00:00 UTC,
  48.  * as a 32 bit number in network byte order.
  49.  * See RFC868.
  50.  *
  51.  * Contains lots of ugly code to help it deal with UDP packets,
  52.  * and with the way some inetd implementations may invoke it with
  53.  * stdout closed.  Sorry.
  54.  *
  55.  * Tell /etc/inetd.conf something like this:
  56.  *  time  stream tcp  nowait  nobody  /usr/local/lib/rfc868time rfc868time
  57.  *  time  dgram  udp  wait    nobody  /usr/local/lib/rfc868time rfc868time
  58.  */
  59.  
  60. #include <stdio.h>
  61. #include <stdlib.h>
  62. #include <errno.h>
  63. #include <string.h>
  64. #include <time.h>
  65. #include <fcntl.h>
  66. #include <sys/types.h>
  67. #include <sys/socket.h>
  68. #include <sys/stat.h>
  69. #include <netinet/in.h>
  70.  
  71. /* #define DEBUGLOG "/usr/tmp/rfc868.log" /**/
  72.  
  73. main(argc, argv)
  74.     int argc;
  75.     char **argv;
  76. {
  77. #ifdef DEBUGLOG
  78.     FILE *logfp;        /* for debug messages */
  79. #endif
  80.     unsigned long ultime;    /* the time, in seconds from 1 Jan 1900. */
  81.     char chtime[4];        /* the four output octets */
  82.     struct stat statbuf;    /* to check if fd 1 is open */
  83.     struct sockaddr localsock;    /* our socket address */
  84.     int localsocklen = sizeof(localsock);
  85.     struct sockaddr remsock;    /* who is talking to us */
  86.     int remsocklen = sizeof(remsock);
  87.     int flags, oldflags;    /* fcntl flags */
  88.     char buf[BUFSIZ];        /* for input packet, if any */
  89.     int len;
  90.     int no_stdout = 0;        /* whether stdout was not open */
  91.     int use_sendto = 0;        /* whether we have to use sendto() */
  92.  
  93.     /*
  94.      * if fd 1 is not open then dup fd 0 to fd 1,
  95.      * in case inetd didn't do it
  96.      */
  97.     if (fstat(1, &statbuf) < 0) {
  98.     (void) dup2(0, 1);
  99.     no_stdout = 1;
  100.     }
  101.  
  102. #ifdef DEBUGLOG
  103.     /*
  104.      * open the debug log after ensuring fd 1 is open
  105.      */
  106.     logfp = fopen(DEBUGLOG, "a");
  107. #endif
  108.  
  109. #ifdef DEBUGLOG
  110.     /*
  111.      * report if stdout was not open
  112.      */
  113.     if (no_stdout) {
  114.     (void) fprintf(logfp, "%s: stdout was not open\n", argv[0]);
  115.     }
  116. #endif
  117.  
  118.     /*
  119.      * If we are being called due to a UDP packet, we have to use
  120.      * recvfrom() to read the packet and get the source address.
  121.      * Later, we will have to use sendto() instead of write() for output.
  122.      *
  123.      * Set the O_NDELAY flag to prevent the read from blocking, and
  124.      * restore the flag afterwards.
  125.      */
  126.     if (getsockname(0, &localsock, &localsocklen) == 0) {
  127.     /* our stdin is a socket */
  128. #ifdef DEBUGLOG
  129.     (void) fprintf(logfp, "%s: stdin is a socket\n", argv[0]);
  130. #endif
  131.     if (localsock.sa_family == AF_INET) {
  132.         /* it's an Internet socket */
  133. #ifdef DEBUGLOG
  134.         (void) fprintf(logfp, "%s: stdin is an AF_INET socket\n", argv[0]);
  135. #endif
  136.         /* I don't know how to tell if it's a UDP socket, so
  137.          * just set O_NDELAY and try the recvfrom now */
  138.         oldflags = fcntl(0, F_GETFL, 0);
  139.         flags = oldflags | O_NDELAY;
  140.         (void) fcntl(0, F_SETFL, flags);
  141.         len = recvfrom(0, buf, sizeof(buf), 0, &remsock, &remsocklen);
  142.         if (len > 0 && remsocklen > 0) {
  143.         /* must have been a UDP socket.
  144.          * remember to use sendto() instead of write() later. */
  145. #ifdef DEBUGLOG
  146.         (void) fprintf(logfp, "%s: stdin is a UDP socket\n", argv[0]);
  147. #endif
  148.         use_sendto = 1;
  149.         }
  150.         (void) fcntl(0, F_SETFL, oldflags);
  151.     }
  152.     }
  153.  
  154. #ifdef DEBUGLOG
  155.     /*
  156.      * report if stdout is a socket
  157.      */
  158.     if (getsockname(1, &localsock, &localsocklen) == 0) {
  159.     (void) fprintf(logfp, "%s: stdout is a socket\n", argv[0]);
  160.     if (localsock.sa_family == AF_INET) {
  161.         /* it's an Internet socket */
  162.         (void) fprintf(logfp, "%s: stdout is an AF_INET socket\n", argv[0]);
  163.     }
  164.     }
  165. #endif /* DEBUGLOG */
  166.  
  167.     /*
  168.      * get time in seconds since 1970, and hence seconds since 1900.
  169.      *
  170.      * there were 2208988800 seconds between 1 Jan 1900 and 1 Jan 1970
  171.      */
  172.     ultime = 2208988800L + (unsigned long) time((time_t *)0);
  173.  
  174.     /*
  175.      * convert to network byte order
  176.      * (could be more efficient at the expense of portability)
  177.      */
  178.     chtime[0] = (char) ((ultime>>24) & 0xff);
  179.     chtime[1] = (char) ((ultime>>16) & 0xff);
  180.     chtime[2] = (char) ((ultime>>8) & 0xff);
  181.     chtime[3] = (char) (ultime & 0xff); /* XXX:  lint says 'conversion
  182.                     from long may lose accuracy' on
  183.                     this line, but not on the
  184.                     preceding lines.  Why?  */
  185.  
  186.     /*
  187.      * output result
  188.      *
  189.      * XXX: hope that it all goes into one UDP packet, if we are being called
  190.      * via UDP.
  191.      */
  192.     if (use_sendto) {
  193.     len = sendto(1, chtime, 4, 0, &remsock, remsocklen);
  194.     } else {
  195.     len = write(1, chtime, 4);
  196.     }
  197.     if (len != 4) {
  198.     (void) fprintf(stderr, "%s: (time %lu) write failed: %s\n", argv[0],
  199.             ultime, strerror(errno));
  200. #ifdef DEBUGLOG
  201.     (void) fprintf(logfp, "%s: (time %lu) write failed: %s\n", argv[0],
  202.             ultime, strerror(errno));
  203. #endif
  204.     exit(1);
  205.     }
  206. #ifdef DEBUGLOG
  207.     (void) fprintf(logfp, "%s: (time %lu) successful\n", argv[0],
  208.             ultime);
  209. #endif
  210.     exit (0);
  211. }
  212. @EOF
  213.  
  214. chmod 644 rfc868time.c
  215.  
  216. exit 0
  217.