home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 4 / Amiga Tools 4.iso / tools / netzwerk / tcp-ip / usno / timec / timec.c
Encoding:
C/C++ Source or Header  |  1996-02-26  |  4.0 KB  |  143 lines

  1. /* timec.c R.E.Schmidt, TS, US Naval Observatory, Feb. 1993
  2.  *
  3.  * Check local system clock against the timer server host.
  4.  *
  5.  * This program requests a service called "time". In order for
  6.  * it to function, an entry for it needs to exist in the
  7.  * /etc/services file. The port address for this service is 37/TCP.
  8.  *
  9.  * The name of the system to which the requests will be sent is
  10.  * given as a parameter to the command.
  11.  */
  12.  
  13. #include <sys/types.h>
  14. #include <sys/socket.h>
  15. #include <netinet/in.h>
  16. #include <stdio.h>
  17. #include <netdb.h>
  18. #include <time.h>
  19.  
  20. #ifdef AMIGA
  21.   #include <stdlib.h>
  22.   #include <string.h>
  23.  
  24.   #define DIFF 2085978496       /* An AMIGA pecularity involving epochs */
  25. #endif
  26.  
  27. #ifdef HPUX
  28.   #define DIFF 2085978496       /* An HP-UX pecularity involving epochs */
  29. #endif
  30.  
  31. #ifndef DIFF
  32.   #define DIFF 0
  33. #endif
  34.  
  35. #define LEN     sizeof(long)
  36.  
  37. int s;                          /* connected socket descriptor          */
  38. struct hostent *rh;             /* pointer to host info for remote host */
  39. struct servent *sport;          /* pointer to service information       */
  40. long timevar;                   /* contains time returned by time()     */
  41. char *(ctime)();                /* declare time formatting routine      */
  42. struct sockaddr_in pad_in;      /* for peer socket address              */
  43.  
  44. void main(argc, argv)
  45.   int argc;
  46.   char *argv[];
  47. {
  48.   auto int xcode = 1;
  49.   auto long tsecs;
  50.   auto char myname[40];
  51.  
  52.   if (argc == 2)
  53.   {
  54.     /* Get local host name
  55.      */
  56.     gethostname(myname, 40);
  57.  
  58.     /* clear out address structures
  59.      */
  60.     memset((char *) &pad_in, 0, sizeof(struct sockaddr_in));
  61.  
  62.     /* Set up the peer address to which we will connect.
  63.      */
  64.     pad_in.sin_family = AF_INET;
  65.  
  66.     /* Get the host information for the hostname that the user passed in.
  67.      */
  68.     if ((rh = gethostbyname(argv[1])) != NULL)
  69.     {
  70.       pad_in.sin_addr.s_addr = ((struct in_addr *) (rh->h_addr))->s_addr;
  71.  
  72.       /* Find the information for the "timesrv" server in order to get the
  73.        * needed port number.
  74.        *
  75.        * sport = getservbyname ("daytime", "tcp");
  76.        */
  77.       if ((sport = getservbyname("time", "tcp")) != NULL)
  78.       {
  79.         pad_in.sin_port = sport->s_port;
  80.  
  81.         /* Create the socket.
  82.          */
  83.         if ((s = socket(AF_INET, SOCK_STREAM, 0)) != -1)
  84.         {
  85.           /* Try to connect to the remote server at the address which was just built
  86.            * into peeraddr.
  87.            */
  88.           if (connect(s, (struct sockaddr const *) &pad_in, sizeof(struct sockaddr_in)) != -1)
  89.           {
  90.             if (recv(s,(unsigned char *) &tsecs, LEN, 0) != -1)
  91.             {
  92.               /* account for OS dependent epochs
  93.                */
  94.               tsecs = ntohl(tsecs) + DIFF;
  95.  
  96.               /* Get local time
  97.                */
  98.               time(&timevar);
  99.  
  100.               /* Print message indicating completion of task.
  101.                */
  102.               printf("Local Time.....: %24.24s at %s\n",ctime(&timevar),myname );
  103.               printf("Remote Time....: %24.24s at %s\n",ctime(&tsecs)  ,argv[1]);  /* GMT string */
  104.               printf("Local - Server.: %d secs\n", timevar - tsecs);
  105.  
  106.               /* return OK on exit.
  107.                */
  108.               xcode = 0;
  109.             }
  110.             else
  111.             {
  112.               perror(argv[0]);
  113.               fprintf(stderr, "%s: error reading result\n", argv[0]);
  114.             }
  115.           }
  116.           else
  117.           {
  118.             perror(argv[0]);
  119.             fprintf(stderr, "%s: unable to connect to remote\n", argv[0]);
  120.           }
  121.         }
  122.         else
  123.         {
  124.           perror(argv[0]);
  125.           fprintf(stderr, "%s: unable to create socket\n", argv[0]);
  126.         }
  127.       }
  128.       else
  129.         fprintf(stderr, "%s: time not found in /etc/services\n", argv[0]);
  130.     }
  131.     else
  132.       fprintf(stderr, "%s: %s not found in /etc/hosts\n", argv[0], argv[1]);
  133.   }
  134.   else
  135.   {
  136.     fprintf(stderr, "Usage: %s <remote host>\n", argv[0]);
  137.     fprintf(stderr, "Hosts: tick.usno.navy.mil\n");
  138.     fprintf(stderr, "       tock.usno.navy.mil\n");
  139.   }
  140.  
  141.   exit(xcode);
  142. }
  143.