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
/
nistimen.c
< prev
next >
Wrap
C/C++ Source or Header
|
2002-01-02
|
9KB
|
342 lines
#include <4bsddefs.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <dos.h>
void main(argc,argv)
int argc;
char *argv[];
{
/*
client process to connect to the daytime service via port 13.
The client process uses the time message received to check (and
optionally to set) the time of the local clock. the comparison
assumes that the local clock keeps time in seconds from 1/1/70
which is the UNIX standard, and it computes the time difference
by converting the received MJD to seconds since 1/1/70 and adding
the received hr, min and sec, also converted to seconds. If the
local machine keeps time in some other way, then the comparison
method will have to change, but the rest should be okay.
This software was developed with US Government support
and it may not be sold, restricted or licensed. You
may duplicate this program provided that this notice
remains in all of the copies, and you may give it to
others provided they understand and agree to this
condition.
This program and the time protocol it uses are under
development and the implementation may change without
notice.
For questions or additional information, contact:
Judah Levine
Time and Frequency Division
NIST/847
325 Broadway
Boulder, Colorado 80303
(303) 492 7785
jlevine@boulder.nist.gov
*/
long int address; /* holds ip address */
int pserv = 13; /* daytime port number on server */
char *cp; /* server address in . notation */
struct sockaddr_in sin; /* socket address structure */
int s; /* socket number */
int length; /* size of message */
char buf[132]; /* holds message */
long int mjd0 = 40587l; /* MJD of 1/1/70 = origin of UNIX time system*/
struct timeval tvv,tgg; /* holds local time */
struct timezone tzp; /* holds local time zone information*/
long int mjd; /* holds parsed received mjd*/
int yr,mo,dy,hr,min,sec; /* holds parsed received time */
int dst,ls,health; /* received flags: dst, ls and health */
long int diff; /* time difference, local - NIST */
char cc;
int stat;
int isw(); /*parses command-line switches*/
char let; /*command-line letter*/
long int val; /*command line value*/
int hrat,minat,secat; /*cmos time */
int yrat,moat,dayat; /*cmos date */
int hribm,minibm,secibm; /*memory time */
int yribm,moibm,dayibm; /*memory date */
long int diffat; /*cmos clock difference */
int unpbcd(); /*unpacks BCD time from cmos clock*/
long int cvt2jd(); /*converts yr-mo-day to mjd */
void adjtime(); /*adjusts time on local system*/
/*
the following variables define what this program
should do and what output should be produced. The
values below are the defaults which may be changed
by characters on the command line as described below.
The effect of each variable is as follows:
Command-Line Switch effect
-m0 msg = 0 Do not produce any messages;
only time difference is written to
standard output.
-m1 or -M = 1 Produce short messages.
-m2 = 2 Produce longer messages.
-s0 set = 0 Do not adjust local time.
-s1 or -S = 1 Adjust local time if server is healthy.
-s2 = 2 Adjust local time even if server is
unhealthy.
-s3 = 3 Ask operator first.
-c or -C cmos clock present
*/
int msg =1; /*default is short messages */
int set =3; /*default is ask before setting clock */
int cmos = 0; /*default is no CMOS clock */
/*
parse command line
*/
while( sw(argc,argv,&let,&val) != 0) /*switch is present*/
{
switch(let)
{
case 'm':
msg=val;
break;
case 's':
set=val;
break;
case 'S':
set=1;
break;
case 'M':
msg=1;
break;
case 'c':
case 'C':
cmos=1;
break;
default:
fprintf(stderr,"\nSwitch %c not recognized.",let);
break;
}
argc--; /*decrement argument counter */
argv++; /*and increment pointer */
}
/*
internet address of server named time_a.timefreq.bldrdoc.gov
*/
cp="132.163.4.101";
/*
convert address to internal format
*/
if( (address=inet_addr(cp) ) == -1)
{
fprintf(stderr,"\n Internet address error.");
exit(1);
}
bzero( (char *)&sin,sizeof(sin));
sin.sin_addr.s_addr=address;
sin.sin_family=AF_INET;
sin.sin_port=htons(pserv);
/*
create the socket and then connect to the server
note that this is a stream socket and that record
boundaries are not preserved.
*/
if( (s=socket(AF_INET,SOCK_STREAM,0) ) < 0)
{
fprintf(stderr,"\n Socket creation error.");
exit(1);
}
if(connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0)
{
perror(" time server Connect failed --");
exit(1);
}
length=read(s,buf,80);
gettimeofday(&tvv,&tzp); /*get time as soon as read completes*/
if(cmos != 0)
{
_AH=0x2c;
geninterrupt(0x21); /*get memory clock time*/
hribm=_CH;
minibm=_CL;
secibm=_DH;
_AH=0x2a;
geninterrupt(0x21);
yribm=_CX;
moibm=_DH;
dayibm=_DL;
yribm -= 1900;
_AH=2;
geninterrupt(0x1a); /*get cmos time*/
hrat=_CH;
minat=_CL;
secat=_DH;
_AH=4;
geninterrupt(0x1a); /*and cmos date*/
yrat=_CL;
moat=_DH;
dayat=_DL;
yrat=unpbcd(yrat);
moat=unpbcd(moat);
dayat=unpbcd(dayat);
hrat=unpbcd(hrat);
minat=unpbcd(minat);
secat=unpbcd(secat);
}
if(length <= 0)
{
fprintf(stderr,"\n No response from server.");
close(s);
exit(1);
}
buf[length]= '\0'; /*add terminating null */
if(msg != 0)
{
printf("\n Time message received:");
printf("\n D L");
printf("\n MJD YY MM DD HH MM SS ST S H Adv.");
printf("%s\n",buf);
}
close(s);
/*
now compute difference between local time and
received time.
*/
sscanf(buf," %ld %2d-%2d-%2d %2d:%2d:%2d %d %d %d",
&mjd,&yr,&mo,&dy,&hr,&min,&sec,&dst,&ls,&health);
if(msg == 2) /*longer messages selected */
{
if( (dst == 0) || (dst > 51) )
{
printf("\nStandard Time now in effect.");
if(dst > 51)
printf("\nChange to Daylight Saving Time in %d days.",dst-51);
}
if( (dst <= 50) && (dst > 1) )
{
printf("\nDaylight Saving Time now in effect.");
if( (dst > 1) && (dst < 50) )
printf("\nChange to Standard Time in %d days.",dst-1);
}
if(dst == 1) printf("\nStandard time begins at 2am today.");
if(dst == 51)printf("\nDaylight Saving Time begins at 2am today.");
switch (ls)
{
case 1:
printf("\nLeap Second to be added at the end of this month.");
break;
case 2:
printf("\nSecond to be dropped at the end of this month.");
break;
default:
printf("\nNo leap second is scheduled.");
}
switch(health)
{
case 0:
printf("\nServer reports fully healthy status.");
break;
case 1:
printf("\nCaution -- Server may not be on time.");
break;
case 2:
printf("\nWarning -- Server may have large time error.");
break;
default:
printf("\nServer time error is not known.");
break;
}
}
/*
convert received time to seconds since 1/1/70 and subtract
this from the local time in the same units.
Since no correction has been made for the network delay,
this difference cannot be taken too seriously, although
the delay in a local network should be no more than about
0.05 seconds.
*/
diff=tvv.tv_sec - 86400l*(mjd-mjd0) - 3600l*(long int)hr
-60l*(long int) min -(long int) sec;
if(tvv.tv_usec >= 500000l) diff++; /*round microseconds */
/*
test the cmos clock by computing the difference between
the cmos clock and the local RAM clock and then adding
the difference between the RAM clock and NIST. This
gets around the need to do a time-zone conversion in
this comparison.
*/
if(cmos != 0) /*test CMOS clock too */
{
diffat=86400l*(cvt2jd(yrat,moat,dayat) -
cvt2jd(yribm,moibm,dayibm)) + 3600l*(hrat-hribm)
+ 60l*(minat-minibm) +(secat-secibm) + diff;
}
else diffat = 0; /*cmos clock not tested*/
if(msg == 0) /*no messages except time diff.*/
printf("%ld",diff);
else
printf("\nSystem Clock - NIST = % ld second(s).",diff);
if(cmos != 0)
printf("\nCMOS Clock - NIST = % ld second(s).",diffat);
if( (diff == 0) && (diffat == 0) )
{
if(msg != 0) printf("\n No adjustment is needed.\n");
exit(0);
}
if(set == 0) exit(0);
if( (health != 0) && (msg != 0) )
printf("\n Server is not healthy, adjustment not recommended.");
cc = 'n'; /*default is not to set */
if(set == 3) /*action depends on answer to query*/
{
printf("\n Do you want to adjust the time ? [y/n] ");
cc=getchar();
}
if( (set == 2) || /*always set */
( (set == 1) && (health == 0) ) || /*set if healthy*/
(cc == 'y') || /*set if response yes*/
(cc == 'Y') )
{
adjtime(-diff);
}
exit(0);
}
/*
*/
int sopen(path,access,shflag,mode)
char *path;
int access;
int shflag;
int mode;
{
int j;
j=open(path,access | shflag,mode);
return(j);
}
void adjtime(isec)
long int isec;
{
/*
adjusts time of the local clock by isec
seconds. time is changed in 1 step
note that for versions of DOS after 3.3,
this call also sets the CMOS clock if it
is present.
*/
struct timeval tpa;
struct timezone tza;
int hr,min,sec; /*time*/
int yr,mo,day; /*date*/
gettimeofday(&tpa,&tza);
isec += tpa.tv_sec;
stime(&isec);
}