home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / k95source / timesync.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  6KB  |  236 lines

  1. /* timesync stuff for leash - 7/28/94 - evanr */
  2.  
  3. #include <winsock.h>
  4. #include <setjmp.h>
  5. #include <time.h>
  6. #include <dos.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10. #ifdef WSHELPER
  11.     #include <wshelper.h>
  12. #endif
  13. #ifdef WIN32
  14. #define _fmemcpy memcpy
  15. #endif
  16.  
  17. extern HANDLE hThisInstance;
  18. #define TM_OFFSET 2208988800
  19.  
  20. struct timezone {               /* timezone.h has a winsock.h conflict */
  21.   int     tz_minuteswest;
  22.   int     tz_dsttime;
  23. };
  24.  
  25. #ifdef WIN32
  26. int settimeofday (struct timeval *tv,struct timezone *tz) {
  27.     SYSTEMTIME systime;
  28.     struct tm *newtime;
  29.     int rc = 0;
  30.  
  31.  
  32.     newtime = gmtime((time_t *)&(tv->tv_sec));
  33.     systime.wYear = 1900+newtime->tm_year;
  34.     systime.wMonth = 1+newtime->tm_mon;
  35.     systime.wDay = newtime->tm_mday;
  36.     systime.wHour = newtime->tm_hour;
  37.     systime.wMinute = newtime->tm_min;
  38.     systime.wSecond = newtime->tm_sec;
  39.     systime.wMilliseconds = 0;
  40.     rc = SetSystemTime(&systime);
  41.     if ( !rc )
  42.         return(rc);
  43.     else 
  44.         return(GetLastError());
  45. }  
  46. #else
  47. int settimeofday (struct timeval *tv,struct timezone *tz) {
  48.   struct _dostime_t dostime;      
  49.   struct _dosdate_t dosdate;
  50.   struct tm *newtime;
  51.   
  52.   newtime = localtime((time_t *)&(tv->tv_sec));
  53.   dostime.hour = newtime->tm_hour;
  54.   dostime.minute = newtime->tm_min;   
  55.   dostime.second = newtime->tm_sec;
  56.   dostime.hsecond = 0;
  57.   dosdate.day = newtime->tm_mday;
  58.   dosdate.month = 1+newtime->tm_mon; /* burp! Microsft really sucks */
  59.   dosdate.year = 1900+newtime->tm_year; /* burp! Microsoft sucks again... */
  60.   dosdate.dayofweek = newtime->tm_wday;
  61.   
  62.   if (_dos_setdate(&dosdate) == 0 && _dos_settime(&dostime) == 0)
  63.     return 0;
  64.   return 1;    
  65. }       
  66. #endif
  67.  
  68. int gettimeofday (struct timeval *tv,struct timezone *tz) {
  69.   time_t long_time;
  70.  
  71. #ifdef COMMENT
  72.   if (getenv ("TZ") == NULL)  /* Default New York, New York, USA time */
  73.     {
  74.       _timezone = 18000;
  75.       _daylight = 1;
  76.       _tzname[0] = "EST";
  77.       _tzname[1] = "EDT";
  78.     }
  79.   else
  80. #endif
  81.     _tzset();
  82.   tz->tz_minuteswest = _timezone;
  83.   tz->tz_dsttime = _daylight;
  84.   time(&long_time);
  85.   tv->tv_sec = long_time;
  86.   tv->tv_usec = 0;
  87.   return 0;       
  88. }       
  89.  
  90. int
  91. main(int argc, char * argv[])
  92. {
  93.   char buffer[512];
  94.   struct timeval tv;
  95.   struct timezone tz;
  96.   jmp_buf top_level;
  97.   struct sockaddr_in sin;
  98.   struct servent FAR * sp;
  99.   struct hostent FAR *host;
  100.   register int s;
  101.   long hosttime;
  102.   register long *nettime; /* used to be an int */
  103.   char tmpstr[80], hostname[64], *phostname=NULL;
  104.   int attempts = 0, cc, host_retry;
  105.   WORD wVersionRequested;
  106.   WSADATA wsaData;
  107.   int err;
  108.   int check;
  109.   wVersionRequested = 0x0101;
  110.   err = WSAStartup( wVersionRequested, &wsaData );
  111.   if ( err != 0 ) {
  112.     sprintf(tmpstr, "Couldn't initialize WinSock to synchronize time\n\r"
  113.          "Error Number: %d", err);
  114.     printf(tmpstr);
  115.     WSACleanup( );
  116.     return(1);
  117.   }
  118.   /* OK, now try the resource -- to be added */
  119.   sp = getservbyname("time", "udp");
  120.   if (sp == 0)
  121.     sin.sin_port = htons(IPPORT_TIMESERVER);
  122.   else
  123.     sin.sin_port = sp->s_port;
  124.   host_retry = 0;
  125.  
  126.   if( argc > 1 ){
  127.       strncpy (hostname, argv[1], 64 );
  128.       hostname[63] = '\0';
  129.       phostname = hostname;
  130.   } else {
  131.       hostname[0] = '\0';
  132.       phostname = "time";
  133.   }
  134.   
  135.   get_host:     
  136.   while (host_retry < 5) {
  137.       host = gethostbyname(phostname); 
  138.       if ((host != NULL) && WSAGetLastError() != WSATRY_AGAIN) {
  139.           break;
  140.       }
  141.       if ( phostname == hostname ) {
  142.           printf("trouble resolving \"%s\", trying \"time\" ...\n",
  143.                   hostname);
  144.           phostname = "time";
  145.       } else if ( !strcmp(phostname,"time") ) {
  146.           printf("trouble resolving \"time\", trying \"timehost\" ...\n");
  147.           phostname = "timehost";
  148.       } else if ( !strcmp(phostname,"timehost") ) {
  149.           if ( hostname[0] ) {
  150.               printf("trouble resolving \"timehost\", retrying \"%s\" ...\n",
  151.                       hostname);
  152.               phostname = hostname;
  153.           } else {
  154.             printf("trouble resolving \"timehost\", retrying \"time\" ...\n");
  155.             phostname = "time";
  156.           }
  157.           host_retry++;
  158.       }
  159.   }
  160.   if (host == NULL) {
  161.     sprintf (tmpstr, "The timeserver host cannot be found\n");
  162.       printf(tmpstr);
  163.       WSACleanup();
  164.       return(2);
  165.   }
  166.   sin.sin_family = host->h_addrtype;
  167.   _fmemcpy ((struct sockaddr *)&sin.sin_addr, host->h_addr, 
  168.         host->h_length);
  169.   s = socket(AF_INET, SOCK_DGRAM, 0);
  170.   if (s < 0) {                                          
  171.     sprintf(tmpstr, "Couldn't open socket - Error: %d", WSAGetLastError());
  172.       printf(tmpstr);
  173.     WSACleanup();
  174.     return(3);
  175.   }
  176.   
  177.   if (connect (s, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
  178.       sprintf(tmpstr, "Couldn't connect to timeserver - Error: %d", 
  179.                WSAGetLastError());
  180.       printf(tmpstr);
  181.       WSACleanup();
  182.       return(4);
  183.   }
  184.     setjmp(top_level);
  185.     if (attempts++ > 5) {
  186.         closesocket (s);
  187.         sprintf (tmpstr, "Failed to get time from %s - Error: %d",
  188.                   phostname, WSAGetLastError());
  189.         printf(tmpstr);
  190.         WSACleanup();
  191.         return(5);
  192.     }        
  193.     send (s, buffer, 40, 0); /* Send an empty packet */
  194.     if (gettimeofday (&tv, &tz) < 0) {
  195.         sprintf(tmpstr,"Unable to get local time\n");
  196.         printf(tmpstr);
  197.         WSACleanup();
  198.         return(6);
  199.     }
  200.   
  201.     cc = recv (s, buffer, 512, 0);
  202.     if (cc < 0) {
  203.         sprintf(tmpstr, "Error receiving time from %s - Error: %d", 
  204.                  phostname,
  205.                  WSAGetLastError());
  206.         printf(tmpstr);
  207.         closesocket(s);
  208.         WSACleanup();
  209.         return(7);
  210.     }
  211.  
  212.     if (cc != 4) {
  213.         closesocket(s);
  214.         sprintf(tmpstr, "Protocol error -- received %d bytes; expected 4",
  215.                  cc);
  216.         printf(tmpstr);
  217.         WSACleanup();
  218.         return(8);
  219.     }
  220.     nettime = (long *)buffer;
  221.     hosttime = (long) ntohl (*nettime) - TM_OFFSET;
  222.     (&tv)->tv_sec = hosttime;
  223.     if (settimeofday (&tv, &tz) < 0) {
  224.         printf("Couldn't set local time of day.");
  225.         WSACleanup();
  226.         return(9);
  227.   }
  228.   
  229.   strcpy(tmpstr, ctime((time_t *)&hosttime));
  230.   tmpstr[strlen(tmpstr)-1] = '\0';
  231.   printf("Local time set to %s",tmpstr);
  232.   closesocket(s);
  233.   WSACleanup();
  234.   return(0);
  235. }
  236.