home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / hamradio / wattcp.zip / NTIME.C < prev    next >
Text File  |  1991-05-29  |  4KB  |  143 lines

  1.  
  2. /******************************************************************************
  3.     NTIME - set dos clock from internet
  4.             see RFC 868
  5.  
  6.     Copyright (C) 1991, University of Waterloo
  7.     portions Copyright (C) 1990, National Center for Supercomputer Applications
  8.  
  9.     This program is free software; you can redistribute it and/or modify
  10.     it, but you may not sell it.
  11.  
  12.     This program is distributed in the hope that it will be useful,
  13.     but without any warranty; without even the implied warranty of
  14.     merchantability or fitness for a particular purpose.
  15.  
  16.         Erick Engelke                   or via E-Mail
  17.         Faculty of Engineering
  18.         University of Waterloo          Erick@development.watstar.uwaterloo.ca
  19.         200 University Ave.,
  20.         Waterloo, Ont., Canada
  21.         N2L 3G1
  22.  
  23. ******************************************************************************/
  24.  
  25. #include <stdio.h>
  26. #include <tcp.h>
  27. #include <dos.h>
  28.  
  29. #define TCP_TIME 1
  30.  
  31.  
  32. /* Notes:
  33.  * The time is the number of seconds since 00:00 (midnight) 1 January 1900
  34.  * GMT, such that the time 1 is 12:00:01 am on 1 January 1900 GMT; this
  35.  * base will serve until the year 2036.
  36.  *
  37.  * For example:
  38.  *
  39.  *     2,208,988,800L corresponds to 00:00  1 Jan 1970 GMT, start of UNIX time
  40.  *
  41.  */
  42.  
  43. #define TIME_PORT 37
  44. #define BASE_TIME 2208988800L
  45.  
  46. /*
  47.  * ntime() given the host address, returns an Internet based time, not an
  48.  *         UNIX or DOS time.  The UNIX time may be derived by subtracting
  49.  *       BASE_TIME from the returned value.
  50.  */
  51.  
  52. long ntime(host)
  53. longword host;
  54. {
  55.     tcp_Socket telsock;
  56.     static tcp_Socket *s;
  57.     int status;
  58.     long temptime;
  59.  
  60.     s = &telsock;
  61.     status = 0;
  62.     temptime = 0L;
  63.  
  64. #ifdef TCP_TIME
  65.     if (!tcp_open( s, 0, host, TIME_PORT, NULL )) {
  66.     puts("Sorry, unable to connect to that machine right now!");
  67.     return( 1 );
  68.     }
  69.     printf("waiting...\r");
  70.     sock_wait_established(s, sock_delay , NULL, &status);
  71.     printf("connected \n");
  72. #else
  73.     if (!udp_open( s, 0, host, TIME_PORT, NULL )) {
  74.     puts("Sorry, unable to connect to that machine right now!");
  75.     return( 1 );
  76.     }
  77.     sock_write( s, "\n", 1 );
  78. #endif TCP_TIME
  79.  
  80.     while ( 1 ) {
  81.     sock_tick( s, &status );
  82.  
  83.     if (sock_dataready( s ) >= 4 ) {
  84.         sock_read( s, &temptime, sizeof( long ));
  85.  
  86.         temptime = ntohl( temptime );    /* convert byte orderring */
  87.         sock_close( s );
  88. return( temptime );
  89.         sock_wait_closed( s, sock_delay, NULL, &status );
  90.         break;
  91.     }
  92.     }
  93.  
  94. sock_err:
  95.     switch (status) {
  96.     case 1 : /* foreign host closed */
  97.          return( temptime );
  98.     case -1: /* timeout */
  99.          printf("\nConnection timed out!");
  100.          return( 0 );
  101.     default: printf("Aborting");
  102.          return( 0 );
  103.     }
  104. }
  105.  
  106.  
  107. main(int argc, char **argv )
  108. {
  109.     longword host;
  110.     longword newtime;
  111.     longword addminutes;
  112.     struct date dstruct;
  113.     struct time tstruct;
  114.  
  115.     if (argc < 2) {
  116.     puts("   DAYTIME  server  [addminutes]");
  117.     exit( 3 );
  118.     }
  119.  
  120.     if (argc == 3 )
  121.     addminutes = atol( argv[2] ) * 60L;
  122.     else
  123.     addminutes = 0L;
  124.  
  125.     sock_init();
  126.  
  127.     if ( host = resolve( argv[1])) {
  128.     if ( newtime = ntime( host )) {
  129.         newtime = newtime - BASE_TIME + addminutes;    /* now in UNIX format */
  130.         unixtodos( newtime, &dstruct, &tstruct );
  131.         settime( &tstruct );
  132.         setdate( &dstruct );
  133.         printf("Time set to %s", ctime( &newtime ));
  134.         exit( 0 );
  135.     }
  136.     printf("Unable to get the time from that host\n");
  137.     exit( 1 );
  138.     }
  139.  
  140.     printf("Could not resolve host '%s'\n", argv[1]);
  141.     exit( 3 );
  142. }
  143.