home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / NCSATELN / TEL23SRC.ZIP / ENGINE / SETCLOCK.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-10-08  |  9.3 KB  |  304 lines

  1. /*
  2.     setclock.c
  3.     Provides a clock setting for  NCSA 2.3.
  4.         Reference: RFC 868
  5.  
  6.     By James Nau, College of Engineering,
  7.     University of Nebraska--Lincoln
  8. */
  9.  
  10. #define TIME_PORT 37
  11.  
  12. #include <stdio.h>
  13. #include <conio.h>
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include <dos.h>
  17. #include <ctype.h>
  18. #ifdef MSC
  19. #include <signal.h>
  20. #include <time.h>
  21. #include <dos.h>
  22. #endif
  23.  
  24.  
  25. #ifdef MEMORY_DEBUG
  26. #include "memdebug.h"
  27. #endif
  28. #include "netevent.h"
  29. #include "hostform.h"
  30. #include "whatami.h"
  31. #include "externs.h"
  32.  
  33. #include "netutils.h"
  34.  
  35. int debug = 0;          /* enable with -D option */
  36. int bypass_passwd=0;    /* whether to bypass the password check, not used */
  37.  
  38. unsigned char path_name[_MAX_DRIVE+_MAX_DIR],        /* character storage for the path name */
  39.     temp_str[20],buf[_MAX_DIR],temp_data[30];
  40.  
  41. /* Function Prototypes */
  42. int main(int argc, char *argv[]);
  43. static void randomize(void );
  44. void usage(void);
  45. void setclock(char *host);
  46.  
  47.  
  48. /*
  49.    setclock [-h filename] hostname
  50.    -h        filename is alternative CONFIG.TEL file
  51.    hostname  the NAME (or Number?) of the host to use for time
  52. */
  53.  
  54. int main(int argc, char *argv[])
  55. {
  56.    int i;
  57.    int switchcount=1;   /* how many args are present */
  58.    int gothost=0;       /* if 1, we've found the host */
  59.    char *ptr=0;         /* pointer to CONFIG.TEL file */
  60.  
  61.    char remote_host[256];    /* address of remote host */
  62.  
  63.    _splitpath(argv[0],path_name,buf,temp_str,temp_data);   /* split path up */
  64.    strcat(path_name,buf);         /* append the path name */
  65.    strcat(path_name,temp_str);    /* append filename */
  66.  
  67. /* get the command line arguments */
  68.    for (i=1; i<argc; i++)
  69.       if (argv[i][0] == '-')
  70.       {
  71.          switch (argv[i][1])
  72.          {
  73.             case 'D':
  74.                debug = 1;
  75.                if (debug) printf("Debugging now ON\n");
  76.                switchcount++;
  77.                break;
  78.             case 'h':
  79.                if (i+1 < argc)
  80.                   ptr = argv[i+1];
  81.                else
  82.                   usage();
  83.                if (debug) printf("Using config file [%s]\n",ptr);
  84.                switchcount += 2;
  85.                break;
  86.             default:
  87.                usage ();   /* Tell user how to use it */
  88.          }
  89.          if (debug) printf("argv[%d][1]=-%c\n", i, argv[i][1]);
  90.       }
  91.  
  92.       if (argc != switchcount+1) usage();
  93.       strcpy(remote_host,argv[argc-1]);
  94.       gothost++;
  95.  
  96.    if (debug) printf("remote_host [%s]\n", remote_host);
  97.  
  98. // exit (0);  /* For debugging if you have no network! */
  99.  
  100.    signal(SIGINT,SIG_IGN);   /* Microsoft intercept of break */
  101.  
  102. /* Do session initialization.  Snetinit reads config file. */
  103.    /* go find a valid CONFIG.TEL file */
  104.    if (ptr == (char *)NULL) ptr = getenv("CONFIG.TEL");
  105.    if (debug) printf("ptr after getenv [%s]\n",ptr);
  106.    if (ptr != (char *)NULL) Shostfile(ptr);
  107.  
  108.    if (Snetinit()) /* Should return 0 if network is OK */
  109.    {
  110.       printf("network init failed\n");
  111.       exit (1);
  112.    }
  113.  
  114. /* Just Do It */
  115.    setclock(remote_host);
  116.  
  117.    netshut();      /* shut down all network stuff */
  118.    exit(0);
  119.    return(0);
  120. }
  121.  
  122.  
  123. void setclock(char *remote_host)
  124. {
  125.    int from_port;
  126.    int conn_id;
  127.    struct machinfo *host_info;
  128.    char buff[5]; /* text returned from the server */
  129.    int bufflen=5;
  130.    int len;             /* length of text read from server */
  131.    int i;
  132.    int timeout=60;      /* Timeout period (I hope) */
  133.    time_t diff;         /* time now, and the difference between times */
  134.    unsigned long nettime=0L;      /* binary value of returned time */
  135.    unsigned long netoffset=0x83aa7e80; /* Jan 1, 1970 in secs from 1900 */
  136.    /* netoffset is January 1, 1970 at 00:00 in offset from 1900
  137.       where nettime starts */
  138.    char *tz;            /* pointer to TZ env var */
  139.    char yn[80];         /* Yes or no */
  140.  
  141.    struct tm *ltime;              /* for localtime */
  142.    struct dosdate_t *dosdate;     /* date structure for _dos_setdate() */
  143.    struct dostime_t *dostime;     /* time structure for _dos_settime() */
  144.  
  145.    int theclass, dat;
  146.    int event;
  147.  
  148.     /* pick a source port at random from the set of privileged ports */
  149.     randomize();
  150.     from_port = rand() % 1023;
  151.  
  152.     /* do name lookup for server */
  153.     host_info = gethostinfo(remote_host);
  154.    if (host_info == (struct machinfo *)NULL)     /* couldn't do it, message in gethostinfo */
  155.    {
  156.       printf("Couldn't lookup host [%s]\n", remote_host);
  157.       return;
  158.    }
  159.  
  160.         /* open the connection */
  161.         conn_id = connect_sock(host_info, from_port, TIME_PORT);
  162.         if (conn_id < 0)
  163.         {
  164.            netshut();
  165.            printf("connect_sock returned %d, exiting\n",conn_id);
  166.            exit (2);
  167.         }
  168.         if (debug) printf("connection ident [%d]\n",conn_id);
  169.  
  170.         if (debug)
  171.         if (!netest(conn_id))
  172.            printf("Connection OK\n");
  173.         else
  174.            printf("nettest: %d\n", netest(conn_id));
  175.  
  176. /* OK, now, we'll attempt to set an alarm to time out in timeout seconds
  177.    if we get a connection, but no response */
  178.  
  179.         Stimerset(USERCLASS, ALARM, 0, timeout);
  180.         if (debug) printf("timer set to go off in %d seconds\n", timeout);
  181.         theclass = 0;
  182.         event = 0;
  183.         while ((theclass != USERCLASS || event != ALARM) && !netest(conn_id))
  184.                 /* no alarm and good connection */
  185.         {
  186.            Stask();     /* I think I have to call this to post my alarm? */
  187.            event = Sgetevent(CONCLASS | USERCLASS, &theclass, &dat);
  188.            if (debug)
  189.               printf("event[%d] theclass[%d] dat[%d]\n",event, theclass, dat);
  190.            if (!event) continue;
  191.            if (conn_id != dat) continue;
  192.  
  193.            if (event == CONDATA)
  194.            {
  195.               len = netread(conn_id, buff, bufflen);
  196.               if (len == 0) continue;
  197.               break;
  198.            }
  199.                       
  200.         }
  201.         if (theclass == USERCLASS && event == ALARM)
  202.         {
  203.            printf("Connection timed out in %d seconds\n", timeout);
  204.            netshut();
  205.            exit (2);
  206.         }
  207.  
  208.         if (debug)
  209.         {
  210.            for (i=0; i<4; i++) printf("%2x ", 0xff&buff[i]);
  211.            printf("\n");
  212.         }
  213.  
  214.         for (i=0; i<4; i++) nettime = (nettime<<8) + (0xff&buff[i]);  /* convert to binary */
  215.  
  216.         diff =  nettime - netoffset;   /* make the time (since 1-jan-1970) */
  217.  
  218.         tz = getenv("TZ");
  219.         yn[0] = '\0';
  220.         if (tz == NULL)
  221.         {
  222.            printf("TZ environment variable not set\n");
  223.            printf("This will assume PST8PDT for a timezone\n");
  224.            printf("Is that what you want? ");
  225.            gets(yn);
  226.         }
  227.         tzset();        /* setup everything to use TZ */
  228.         ltime = localtime(&diff);
  229.  
  230.         dosdate->day = (unsigned char)ltime->tm_mday;
  231.         dosdate->month = (unsigned char)(ltime->tm_mon + 1);
  232.         dosdate->year = (unsigned int)ltime->tm_year + 1900;
  233.         dosdate->dayofweek = (unsigned char)ltime->tm_wday;
  234.  
  235.         dostime->hour = (unsigned char)ltime->tm_hour;
  236.         dostime->minute = (unsigned char)ltime->tm_min;
  237.         dostime->second = (unsigned char)ltime->tm_sec;
  238.         dostime->hsecond = 0;
  239.  
  240.         if (tz != NULL || yn[0] == 'y' || yn[0] =='Y')
  241.         {
  242.            if (_dos_setdate(dosdate))
  243.            {
  244.               printf("Error setting date\n");
  245.               printf("day  : %d\n", dosdate->day);
  246.               printf("month: %d\n", dosdate->month);
  247.               printf("year : %d\n", dosdate->year);
  248.               printf("dayow: %d\n", dosdate->dayofweek);
  249.            }
  250.            if (_dos_settime(dostime))
  251.            {
  252.               printf("Error setting time\n");
  253.               printf("hour  : %d\n",dostime->hour);
  254.               printf("minute: %d\n",dostime->minute);
  255.               printf("second: %d\n",dostime->second);
  256.            }
  257.  
  258.            printf("Time set to %s", ctime(&diff));
  259.         }
  260.         else {  /* answered 'n' to time zone question */
  261.            printf("\nTime was not set. You must enter the environment variable TZ\n");
  262.            printf("in your environment.  This should be a a three-letter time zone\n");
  263.            printf("code (such as CMT for Central/Mountain), followed by an optionally\n");
  264.            printf("signed number giving the difference (in hours) between local time\n");
  265.            printf("and Greenwich Mean Time. (6 for Central/Mountain)  This is followed\n");
  266.            printf("by a three-letter Daylight Savings Time Zone, such as CDT.\n");
  267.         };
  268.  
  269.         if (debug) printf("Closing Connection\n");
  270.         netclose(conn_id);
  271. }
  272.  
  273.  
  274. #ifdef MSC
  275. /******************************************************************
  276. *
  277. * randomize()
  278. *
  279. * replicates the randomize function of Turbo C
  280. * MSC 5.1 does not contain it so we have to write it ourselves.
  281. *
  282. */
  283.  
  284. static void randomize(void )
  285. {
  286.     srand((unsigned)time(NULL));
  287. }
  288. #endif
  289.  
  290.  
  291. void usage()
  292. {
  293.    printf("Usage: %s [-h filename] hostname\n\n", path_name);
  294.  
  295.    printf("   -h        filename is alternative CONFIG.TEL file\n");
  296.    printf("   hostname  the NAME of the host to sync clock to\n");
  297.  
  298.    printf("\nNOTE:  This program (setclock) seems to be pretty stable\n");
  299.    printf("       One thing though, I'm not sure what happens when\n");
  300.    printf("       Daylight Savings Time ends.  I think the program will\n");
  301.    printf("       work properly.  Time will tell I guess\n");
  302.    exit (-1);
  303. }
  304.