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