home *** CD-ROM | disk | FTP | other *** search
/ nisttime.carsoncity.k12.mi.us / nisttime.carsoncity.k12.mi.us.tar / nisttime.carsoncity.k12.mi.us / pub / daytime / tcp_port37_client.c < prev    next >
C/C++ Source or Header  |  2004-05-03  |  10KB  |  287 lines

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <sys/time.h>
  5. #include <netinet/in.h>
  6. #include <netdb.h>
  7. #include <arpa/inet.h>
  8. #include <string.h>
  9. #include <ctype.h>
  10. main(argc,argv)
  11. int argc;
  12. char *argv[];
  13. {
  14. /*
  15.     client process to connect to  the time service via port 37.
  16.     The client process uses the time message received to check (and
  17.     optionally to set) the time of the local clock.  the comparison 
  18.     assumes that the local clock keeps time in seconds from 1/1/70 
  19.     which is the UNIX standard, and it computes the time difference 
  20.     by converting the received time from seconds since 1900.0 to 
  21.     seconds since 1/1/70.  If the local machine keeps time in some 
  22.     other way, then the comparison method will have to change, but 
  23.     the rest should be okay.
  24.  
  25.     This software was developed with US Government support
  26.     and it may not be sold, restricted or licensed.  You 
  27.     may duplicate this program provided that this notice
  28.     remains in all of the copies, and you may give it to
  29.     others provided they understand and agree to this
  30.     condition.
  31.  
  32.     This program and the time protocol it uses are under 
  33.     development and the implementation may change without 
  34.     notice.
  35.  
  36.     For questions or additional information, contact:
  37.  
  38.     Judah Levine
  39.     Time and Frequency Division
  40.     NIST/847
  41.     325 Broadway
  42.     Boulder, Colorado 80305
  43.     (303) 492 7785
  44.     jlevine@boulder.nist.gov
  45. */
  46. long int address;        /* holds ip address */
  47. int pserv = 37;        /* time port number on server */
  48. char *cp;            /* server address in . notation */
  49. char *sp;            /* temporary for server address*/
  50. int aindots[4];                 /* numerical host address in dot notation*/
  51. char addrbuf[20];               /* address formatted into dot notation*/
  52. int j;
  53. struct sockaddr_in sin;        /* socket address structure */
  54. int s;                /* socket number */
  55. int length;            /* size of message */
  56. unsigned long int xxy;        /* holds received time*/
  57. struct timeval tvv;        /* holds local time */
  58. long int diff;            /* time difference, local - NIST */
  59. char cc;
  60. int stat;
  61. int sw();            /*parses command-line switches*/
  62. char let;            /*command-line letter*/
  63. long int val;            /*command line value*/
  64. struct hostent *serv0;          /*pointer to structure returned by gethost */
  65. /*
  66.     the following variables define what this program
  67.     should do and what output should be produced.  The
  68.     values below are the defaults which may be changed
  69.     by characters on the command line as described below.
  70.     The effect of each variable is as follows:
  71.  
  72. Command-Line Switch    effect
  73.     -m0        msg  = 0    Do not produce any messages;
  74.                     only time difference is written to
  75.                     standard output.
  76.  
  77.     -m1 or -M         = 1    Produce messages.
  78.  
  79.     -s0        set  = 0    Do not adjust local time.
  80.     -s1 or -S         = 1    Adjust local time without asking
  81.     -s2             = 2    Ask operator first.
  82.  
  83.         -u<j>           use server number j, where j is the index number
  84.                         of the NIST server chosen from the following list.
  85.                         the first server on the list is number 1, the
  86.                         second is number 2, etc.
  87.                         the default is to use the first server, which
  88.                         is number 1.
  89.                         note that the array uses C indexing, so that the
  90.                         first entry in the array is number 0, etc.
  91.                         thus any user response is decremented before
  92.                         being used.
  93.  
  94.         -u0             the name of the server is given as the next
  95.                         parameter on the command line. the name can
  96.                         be either a name or an ip address in dot
  97.                         notation. The entry will be taken as a name
  98.                         if the first non-blank character is not a
  99.                         digit.
  100. */
  101. int msg =1;        /*default is short messages */
  102. int set =2;        /*default is ask before setting clock */
  103. int use_serv=0;        /*use first server by default*/
  104. /*
  105.         the following is a list of the servers operated by
  106.         NIST. All of them will support the daytime protocol
  107.         in the format that this program expects.
  108.  
  109.         Each server may be specified either by name, as in
  110.         time.nist.gov or by ip as in 192.43.244.18. If the
  111.         first character of the specification is a digit, then
  112.         the numerical format is assumed. If a name is entered,
  113.         it is converted to the corresponding ip address using
  114.         the standard DNS query. The program will fail if the
  115.         DNS query cannot find the ip number of the server
  116.  
  117.         For more information about these servers, look at the
  118.         entry for the Internet Time Service on the Time and
  119.         Frequency Division homepage at www.boulder.nist.gov/timefreq
  120. */
  121. #define NUMSRV 15
  122. char *serv_ip[NUMSRV]= {"64.236.96.53"   ,/*nist1.aol-va.truetime.com*/
  123.                         "128.138.140.44" ,/*utcnist.colorado.edu*/
  124.                         "207.200.81.113" ,/*nist1.aol-ca.truetime.com*/
  125.                         "216.200.93.8"   ,/*nist1-dc.glassey.com*/
  126.                         "63.149.208.50"  ,/*nist1.datum.com*/
  127.                         "208.184.49.9"   ,/*nist1-ny.glassey.com*/
  128.                         "207.126.103.204",/*nist1-sj.glassey.com*/
  129.                         "129.6.15.28"    ,/*time-a.nist.gov*/
  130.                         "129.6.15.29"    ,/*time-b.nist.gov*/
  131.                         "132.163.4.101"  ,/*time-a.timefreq.bldrdoc.gov*/
  132.                         "132.163.4.102"  ,/*time-b.timefreq.bldrdoc.gov*/
  133.                         "132.163.4.103"  ,/*time-c.timefreq.bldrdoc.gov*/
  134.                         "192.43.244.18"  ,/*time.nist.gov*/
  135.                         "131.107.1.10"   , /*time-nw.nist.gov*/  
  136.             "utcnist.colorado.edu" /*DNS entry= 128.138.140.44*/
  137.             };
  138. /*
  139.     parse command line 
  140. */
  141.     while( sw(argc,argv,&let,&val) != 0) /*switch is present*/
  142.        {
  143.        switch(let)
  144.           {
  145.           case 'm':
  146.         msg=val;
  147.         break;
  148.           case 's':
  149.         set=val;
  150.         break;
  151.           case 'S':
  152.         set=1;
  153.         break;
  154.           case 'M':
  155.         msg=1;
  156.         break;
  157.               case 'u':
  158.                 if(val == 0)    /*next parameter specifies server*/
  159.                    {
  160.                    argc--;
  161.                    argv++;      /*skip over the switch*/
  162.                    if(argc <= 1)
  163.                         {
  164.                         printf("\n Expected server name is missing.\n\n");
  165.                         exit();
  166.                    }
  167.                    cp=argv[1];  /*save the next parameter as the server name*/
  168.                    use_serv=999;        /*set flag*/
  169.                    break;
  170.                 }
  171.                 /*  server 1 has internal index 0 */
  172.                 use_serv=val-1;
  173.                 /*  check if entry is out of range*/
  174.                 if(use_serv < 0) use_serv=0;
  175.                 if(use_serv >= NUMSRV) use_serv=NUMSRV - 1;
  176.                 break;
  177.  
  178.           default:
  179.         fprintf(stderr,"\nSwitch %c not recognized.",let);
  180.         break;
  181.        }
  182.        argc--;    /*decrement argument counter */
  183.        argv++;    /*and increment pointer */
  184.     }
  185. /*
  186.     internet address of selected server
  187. */
  188.     if(use_serv != 999) cp=serv_ip[use_serv];
  189.         if(!isdigit(*cp))       /*first char not a digit, must convert name*/
  190.            {
  191.            if( (serv0=gethostbyname(cp)) == NULL)
  192.                 {
  193.                 printf("\n Cannot resolve name %s",cp);
  194.                 exit();
  195.            }
  196.            if(serv0->h_length != 4)
  197.               {
  198.               printf("\nLength of host address (= %d) is wrong, 4 expected.",
  199.                         serv0->h_length);
  200.               exit();
  201.            }
  202.            sp= serv0->h_addr_list[0];
  203.            for(j=0; j<4; j++)   /*store and convert address*/
  204.               {
  205.               aindots[j]= *(sp++);
  206.               aindots[j] &= 0xff;       /*turn off sign extension*/
  207.            }
  208.            sprintf(addrbuf,"%d.%d.%d.%d",
  209.                 aindots[0],aindots[1],aindots[2],aindots[3]);
  210.        cp= addrbuf;
  211.         }
  212.         printf("\n Using server at address %s",cp);
  213. /*
  214.     convert address to internal format
  215. */
  216.     if( (address=inet_addr(cp) ) == -1)
  217.        {
  218.        fprintf(stderr,"\n Internet address error.");
  219.        exit(1);
  220.     }
  221.     bzero( (char *)&sin,sizeof(sin));
  222.     sin.sin_addr.s_addr=address;
  223.     sin.sin_family=AF_INET;
  224.     sin.sin_port=htons(pserv);
  225. /*
  226.     create the socket and then connect to the server
  227.     note that this is a stream socket and that record
  228.     boundaries are not preserved.
  229. */
  230.     if( (s=socket(AF_INET,SOCK_STREAM,0) ) < 0)
  231.        {
  232.        fprintf(stderr,"\n Socket creation error.");
  233.        exit(1);
  234.     }
  235.     if(connect(s, (struct sockaddr *) &sin, sizeof(sin) ) < 0)
  236.        {
  237.        perror(" time server Connect failed --");
  238.        exit(1);
  239.     }
  240.     length=read(s,&xxy,4);    /*read 32 bit time value */
  241.     gettimeofday(&tvv,0);    /*get time as soon as read completes*/
  242.     if(length <= 0) 
  243.        {
  244.        perror(" No response from server ");
  245.        close(s);
  246.        exit(1);
  247.     }
  248.     xxy= ntohl(xxy);    /*convert to local byte order*/
  249.     xxy -= 2208988800l;    /*subtract 1970.0 - 1900.0*/
  250.     if(msg != 0) printf("\n Time value received = %lu",xxy);
  251.     diff= tvv.tv_sec - xxy;
  252.     if(tvv.tv_usec > 500000l) diff++;    /*round up difference*/
  253.     if(msg != 0) printf("\n Local clock - NIST= %ld seconds.",diff);
  254.     else printf("%ld",diff);
  255.     close(s);
  256.     if(diff == 0)
  257.        {
  258.        if(msg != 0) printf("\n No adjustment is needed.\n");
  259.        exit();
  260.     }
  261.     if(set == 0) exit();        /*never adjust the time*/
  262.     if(set == 2)        /*ask before doing anything*/
  263.        {
  264.        printf("\n Do you want to adjust the local clock ? [y/n] ");
  265.        cc=getchar();
  266.     }
  267.     if( (set == 1) || (cc == 'y') || (cc == 'Y') )
  268.        {
  269.        if(diff > 2100l)
  270.           {
  271.           if(msg != 0) printf("\n Adjustment limited to -2100 s.");
  272.           diff= 2100l;
  273.        }
  274.        if(diff < -2100l)
  275.           {
  276.           if(msg != 0) printf("\n Adjustment limited to +2100 s.");
  277.          diff= -2100l;
  278.        }
  279.        tvv.tv_sec= -diff;
  280.        tvv.tv_usec = 0;
  281.        stat=adjtime(&tvv,(struct timeval *) 0);
  282.        if(stat == 0) printf("\n Adjustment was performed.\n");
  283.        else perror("Adjustment failed. ");
  284.     }
  285.     exit();
  286. }
  287.